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

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: 
                     20: //#define ENABLE_DEBUG
                     21: #ifdef ENABLE_DEBUG
                     22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_DASM
                     24:        #define ENABLE_DEBUG_SYSCALL
                     25:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       26:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       27:        
                     28:        #ifdef EXPORT_DEBUG_TO_FILE
                     29:                FILE* fdebug = NULL;
                     30:        #else
                     31:                #define fdebug stderr
                     32:        #endif
                     33:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     34:                #define unimplemented_10h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
                     38:                #define unimplemented_1ah fatalerror
                     39:                #define unimplemented_21h fatalerror
                     40:                #define unimplemented_2fh fatalerror
1.1.1.24  root       41:                #define unimplemented_33h fatalerror
1.1.1.22  root       42:                #define unimplemented_67h fatalerror
                     43:                #define unimplemented_xms fatalerror
                     44:        #endif
                     45: #endif
                     46: #ifndef unimplemented_10h
                     47:        #define unimplemented_10h nolog
                     48: #endif
1.1.1.25  root       49: #ifndef unimplemented_14h
                     50:        #define unimplemented_14h nolog
                     51: #endif
1.1.1.22  root       52: #ifndef unimplemented_15h
                     53:        #define unimplemented_15h nolog
                     54: #endif
                     55: #ifndef unimplemented_16h
                     56:        #define unimplemented_16h nolog
                     57: #endif
                     58: #ifndef unimplemented_1ah
                     59:        #define unimplemented_1ah nolog
                     60: #endif
                     61: #ifndef unimplemented_21h
                     62:        #define unimplemented_21h nolog
                     63: #endif
                     64: #ifndef unimplemented_2fh
                     65:        #define unimplemented_2fh nolog
                     66: #endif
1.1.1.24  root       67: #ifndef unimplemented_33h
                     68:        #define unimplemented_33h nolog
                     69: #endif
1.1.1.22  root       70: #ifndef unimplemented_67h
                     71:        #define unimplemented_67h nolog
                     72: #endif
                     73: #ifndef unimplemented_xms
                     74:        #define unimplemented_xms nolog
                     75: #endif
                     76: 
                     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 ctrl_break = false;
                    126: bool stay_busy = false;
                    127: UINT32 iops = 0;
1.1.1.19  root      128: bool support_ems = false;
                    129: #ifdef SUPPORT_XMS
                    130: bool support_xms = false;
                    131: #endif
1.1.1.29  root      132: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      133: 
                    134: BOOL is_vista_or_later;
                    135: 
                    136: inline void maybe_idle()
                    137: {
                    138:        // if it appears to be in a tight loop, assume waiting for input
                    139:        // allow for one updated video character, for a spinning cursor
                    140:        if(!stay_busy && iops < 1000 && vram_length_char <= 1 && vram_length_attr <= 1) {
                    141:                Sleep(10);
                    142:        }
                    143:        iops = 0;
                    144: }
1.1.1.12  root      145: 
1.1       root      146: /* ----------------------------------------------------------------------------
1.1.1.3   root      147:        MAME i86/i386
1.1       root      148: ---------------------------------------------------------------------------- */
                    149: 
1.1.1.10  root      150: #ifndef __BIG_ENDIAN__
1.1       root      151: #define LSB_FIRST
1.1.1.10  root      152: #endif
1.1       root      153: 
                    154: #ifndef INLINE
                    155: #define INLINE inline
                    156: #endif
                    157: #define U64(v) UINT64(v)
                    158: 
                    159: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    160: #define logerror(...)
                    161: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    162: #define popmessage(...)
                    163: 
                    164: /*****************************************************************************/
1.1.1.10  root      165: /* src/emu/devcpu.h */
                    166: 
                    167: // CPU interface functions
                    168: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    169: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    170: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    171: 
                    172: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    173: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    174: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    175: 
                    176: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    177: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    178: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    179: 
                    180: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    181: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    182: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    183: 
                    184: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    185: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    186: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    187: 
1.1.1.14  root      188: #define CPU_MODEL_STR(name)                    #name
                    189: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    190: 
1.1.1.10  root      191: /*****************************************************************************/
                    192: /* src/emu/didisasm.h */
                    193: 
                    194: // Disassembler constants
                    195: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    196: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    197: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    198: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    199: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    200: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    201: 
                    202: /*****************************************************************************/
1.1       root      203: /* src/emu/diexec.h */
                    204: 
                    205: // I/O line states
                    206: enum line_state
                    207: {
                    208:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    209:        ASSERT_LINE,                            // assert an interrupt immediately
                    210:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    211:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    212: };
                    213: 
                    214: // I/O line definitions
                    215: enum
                    216: {
                    217:        INPUT_LINE_IRQ = 0,
                    218:        INPUT_LINE_NMI
                    219: };
                    220: 
                    221: /*****************************************************************************/
1.1.1.10  root      222: /* src/emu/dimemory.h */
1.1       root      223: 
1.1.1.10  root      224: // Translation intentions
                    225: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    226: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    227: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    228: 
                    229: const int TRANSLATE_READ            = 0;        // translate for read
                    230: const int TRANSLATE_WRITE           = 1;        // translate for write
                    231: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    232: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    233: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    234: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    235: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    236: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    237: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      238: 
1.1.1.10  root      239: /*****************************************************************************/
                    240: /* src/emu/emucore.h */
1.1       root      241: 
1.1.1.10  root      242: // constants for expression endianness
                    243: enum endianness_t
                    244: {
                    245:        ENDIANNESS_LITTLE,
                    246:        ENDIANNESS_BIG
                    247: };
1.1       root      248: 
1.1.1.10  root      249: // declare native endianness to be one or the other
                    250: #ifdef LSB_FIRST
                    251: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    252: #else
                    253: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    254: #endif
                    255: 
                    256: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    257: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    258: 
                    259: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    260: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    261: 
                    262: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    263: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      264: 
                    265: /*****************************************************************************/
                    266: /* src/emu/memory.h */
                    267: 
1.1.1.10  root      268: // address spaces
                    269: enum address_spacenum
                    270: {
                    271:        AS_0,                           // first address space
                    272:        AS_1,                           // second address space
                    273:        AS_2,                           // third address space
                    274:        AS_3,                           // fourth address space
                    275:        ADDRESS_SPACES,                 // maximum number of address spaces
                    276: 
                    277:        // alternate address space names for common use
                    278:        AS_PROGRAM = AS_0,              // program address space
                    279:        AS_DATA = AS_1,                 // data address space
                    280:        AS_IO = AS_2                    // I/O address space
                    281: };
                    282: 
1.1       root      283: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      284: //typedef UINT32       offs_t;
1.1       root      285: 
                    286: // read accessors
                    287: UINT8 read_byte(offs_t byteaddress)
                    288: {
1.1.1.4   root      289: #if defined(HAS_I386)
1.1       root      290:        if(byteaddress < MAX_MEM) {
                    291:                return mem[byteaddress];
1.1.1.3   root      292: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    293: //             return read_byte(byteaddress & 0xfffff);
1.1       root      294:        }
                    295:        return 0;
1.1.1.4   root      296: #else
                    297:        return mem[byteaddress];
                    298: #endif
1.1       root      299: }
                    300: 
                    301: UINT16 read_word(offs_t byteaddress)
                    302: {
1.1.1.14  root      303:        if(byteaddress == 0x41c) {
                    304:                // pointer to first free slot in keyboard buffer
                    305:                // XXX: the buffer itself doesn't actually exist in DOS memory
                    306:                if(key_buf_char->count() == 0) {
                    307:                        maybe_idle();
                    308:                }
                    309:                return (UINT16)key_buf_char->count();
                    310:        }
1.1.1.4   root      311: #if defined(HAS_I386)
1.1       root      312:        if(byteaddress < MAX_MEM - 1) {
                    313:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      314: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    315: //             return read_word(byteaddress & 0xfffff);
1.1       root      316:        }
                    317:        return 0;
1.1.1.4   root      318: #else
                    319:        return *(UINT16 *)(mem + byteaddress);
                    320: #endif
1.1       root      321: }
                    322: 
                    323: UINT32 read_dword(offs_t byteaddress)
                    324: {
1.1.1.4   root      325: #if defined(HAS_I386)
1.1       root      326:        if(byteaddress < MAX_MEM - 3) {
                    327:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      328: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    329: //             return read_dword(byteaddress & 0xfffff);
1.1       root      330:        }
                    331:        return 0;
1.1.1.4   root      332: #else
                    333:        return *(UINT32 *)(mem + byteaddress);
                    334: #endif
1.1       root      335: }
                    336: 
                    337: // write accessors
1.1.1.14  root      338: #ifdef USE_THREAD
                    339: void vram_flush_char()
                    340: {
                    341:        if(vram_length_char != 0) {
                    342:                DWORD num;
1.1.1.23  root      343:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      344:                vram_length_char = vram_last_length_char = 0;
                    345:        }
                    346: }
                    347: 
                    348: void vram_flush_attr()
                    349: {
                    350:        if(vram_length_attr != 0) {
                    351:                DWORD num;
1.1.1.23  root      352:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      353:                vram_length_attr = vram_last_length_attr = 0;
                    354:        }
                    355: }
                    356: 
                    357: void vram_flush()
                    358: {
                    359:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    360:                EnterCriticalSection(&vram_crit_sect);
                    361:                vram_flush_char();
                    362:                vram_flush_attr();
                    363:                LeaveCriticalSection(&vram_crit_sect);
                    364:        }
                    365: }
                    366: #endif
                    367: 
                    368: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      369: {
1.1.1.14  root      370: #ifdef USE_THREAD
                    371:        static offs_t first_offset_char, last_offset_char;
                    372:        
                    373:        if(vram_length_char != 0) {
                    374:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    375:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    376:                        return;
                    377:                }
                    378:                if(offset != last_offset_char + 2) {
                    379:                        vram_flush_char();
                    380:                }
                    381:        }
                    382:        if(vram_length_char == 0) {
                    383:                first_offset_char = offset;
                    384:                vram_coord_char.X = (offset >> 1) % scr_width;
                    385:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    386:        }
                    387:        scr_char[vram_length_char++] = data;
                    388:        last_offset_char = offset;
                    389: #else
1.1.1.8   root      390:        COORD co;
                    391:        DWORD num;
                    392:        
1.1.1.14  root      393:        co.X = (offset >> 1) % scr_width;
                    394:        co.Y = (offset >> 1) / scr_width;
                    395:        scr_char[0] = data;
1.1.1.23  root      396:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      397: #endif
                    398: }
                    399: 
                    400: void write_text_vram_attr(offs_t offset, UINT8 data)
                    401: {
                    402: #ifdef USE_THREAD
                    403:        static offs_t first_offset_attr, last_offset_attr;
                    404:        
                    405:        if(vram_length_attr != 0) {
                    406:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    407:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    408:                        return;
                    409:                }
                    410:                if(offset != last_offset_attr + 2) {
                    411:                        vram_flush_attr();
                    412:                }
                    413:        }
                    414:        if(vram_length_attr == 0) {
                    415:                first_offset_attr = offset;
                    416:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    417:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    418:        }
                    419:        scr_attr[vram_length_attr++] = data;
                    420:        last_offset_attr = offset;
                    421: #else
                    422:        COORD co;
                    423:        DWORD num;
1.1.1.8   root      424:        
1.1.1.14  root      425:        co.X = (offset >> 1) % scr_width;
                    426:        co.Y = (offset >> 1) / scr_width;
                    427:        scr_attr[0] = data;
1.1.1.23  root      428:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      429: #endif
                    430: }
                    431: 
                    432: void write_text_vram_byte(offs_t offset, UINT8 data)
                    433: {
                    434:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      435:        if(offset & 1) {
1.1.1.14  root      436:                write_text_vram_attr(offset, data);
1.1.1.8   root      437:        } else {
1.1.1.14  root      438:                write_text_vram_char(offset, data);
1.1.1.8   root      439:        }
1.1.1.14  root      440:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      441: }
                    442: 
                    443: void write_text_vram_word(offs_t offset, UINT16 data)
                    444: {
1.1.1.14  root      445:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      446:        if(offset & 1) {
1.1.1.14  root      447:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    448:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      449:        } else {
1.1.1.14  root      450:                write_text_vram_char(offset    , (data     ) & 0xff);
                    451:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      452:        }
1.1.1.14  root      453:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      454: }
                    455: 
                    456: void write_text_vram_dword(offs_t offset, UINT32 data)
                    457: {
1.1.1.14  root      458:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      459:        if(offset & 1) {
1.1.1.14  root      460:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    461:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    462:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    463:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    464:        } else {
                    465:                write_text_vram_char(offset    , (data      ) & 0xff);
                    466:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    467:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    468:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      469:        }
1.1.1.14  root      470:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      471: }
                    472: 
1.1       root      473: void write_byte(offs_t byteaddress, UINT8 data)
                    474: {
1.1.1.8   root      475:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      476:                mem[byteaddress] = data;
1.1.1.8   root      477:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      478:                if(!restore_console_on_exit) {
                    479:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      480:                }
1.1.1.8   root      481:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    482:                mem[byteaddress] = data;
                    483:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    484:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    485:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      486:                }
                    487:                mem[byteaddress] = data;
1.1.1.4   root      488: #if defined(HAS_I386)
1.1.1.3   root      489:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      490: #else
                    491:        } else {
                    492: #endif
1.1.1.3   root      493:                mem[byteaddress] = data;
1.1       root      494:        }
                    495: }
                    496: 
                    497: void write_word(offs_t byteaddress, UINT16 data)
                    498: {
1.1.1.8   root      499:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      500:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    501:                        COORD co;
                    502:                        co.X = data & 0xff;
                    503:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      504:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      505:                }
1.1.1.3   root      506:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      507:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      508:                if(!restore_console_on_exit) {
                    509:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      510:                }
1.1.1.8   root      511:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    512:                *(UINT16 *)(mem + byteaddress) = data;
                    513:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    514:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    515:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      516:                }
                    517:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      518: #if defined(HAS_I386)
1.1.1.3   root      519:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      520: #else
                    521:        } else {
                    522: #endif
1.1.1.3   root      523:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      524:        }
                    525: }
                    526: 
                    527: void write_dword(offs_t byteaddress, UINT32 data)
                    528: {
1.1.1.8   root      529:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      530:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      531:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      532:                if(!restore_console_on_exit) {
                    533:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      534:                }
1.1.1.8   root      535:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    536:                *(UINT32 *)(mem + byteaddress) = data;
                    537:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    538:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    539:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      540:                }
                    541:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      542: #if defined(HAS_I386)
1.1.1.3   root      543:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      544: #else
                    545:        } else {
                    546: #endif
1.1.1.3   root      547:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      548:        }
                    549: }
                    550: 
                    551: #define read_decrypted_byte read_byte
                    552: #define read_decrypted_word read_word
                    553: #define read_decrypted_dword read_dword
                    554: 
1.1.1.3   root      555: #define read_raw_byte read_byte
                    556: #define write_raw_byte write_byte
                    557: 
                    558: #define read_word_unaligned read_word
                    559: #define write_word_unaligned write_word
                    560: 
                    561: #define read_io_word_unaligned read_io_word
                    562: #define write_io_word_unaligned write_io_word
                    563: 
1.1       root      564: UINT8 read_io_byte(offs_t byteaddress);
                    565: UINT16 read_io_word(offs_t byteaddress);
                    566: UINT32 read_io_dword(offs_t byteaddress);
                    567: 
                    568: void write_io_byte(offs_t byteaddress, UINT8 data);
                    569: void write_io_word(offs_t byteaddress, UINT16 data);
                    570: void write_io_dword(offs_t byteaddress, UINT32 data);
                    571: 
                    572: /*****************************************************************************/
                    573: /* src/osd/osdcomm.h */
                    574: 
                    575: /* Highly useful macro for compile-time knowledge of an array size */
                    576: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    577: 
1.1.1.3   root      578: #if defined(HAS_I386)
1.1.1.10  root      579:        static CPU_TRANSLATE(i386);
                    580:        #include "mame/lib/softfloat/softfloat.c"
                    581:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      582:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      583: #elif defined(HAS_I286)
1.1.1.10  root      584:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      585: #else
1.1.1.10  root      586:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      587: #endif
1.1.1.22  root      588: #ifdef ENABLE_DEBUG_DASM
1.1.1.10  root      589:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1.1.22  root      590:        int dasm = 0;
1.1       root      591: #endif
                    592: 
1.1.1.3   root      593: #if defined(HAS_I386)
                    594:        #define SREG(x)                         m_sreg[x].selector
                    595:        #define SREG_BASE(x)                    m_sreg[x].base
                    596: 
                    597:        int cpu_type, cpu_step;
                    598: #else
                    599:        #define REG8(x)                         m_regs.b[x]
                    600:        #define REG16(x)                        m_regs.w[x]
                    601:        #define SREG(x)                         m_sregs[x]
                    602:        #define SREG_BASE(x)                    m_base[x]
                    603:        #define m_CF                            m_CarryVal
                    604:        #define m_a20_mask                      AMASK
                    605:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    606:        #if defined(HAS_I286)
                    607:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    608:        #else
                    609:                #define i386_set_a20_line(x)
                    610:        #endif
                    611:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    612: #endif
1.1       root      613: 
                    614: void i386_jmp_far(UINT16 selector, UINT32 address)
                    615: {
1.1.1.3   root      616: #if defined(HAS_I386)
1.1       root      617:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      618:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      619:        } else {
1.1.1.3   root      620:                SREG(CS) = selector;
                    621:                m_performed_intersegment_jump = 1;
                    622:                i386_load_segment_descriptor(CS);
                    623:                m_eip = address;
                    624:                CHANGE_PC(m_eip);
1.1       root      625:        }
1.1.1.3   root      626: #elif defined(HAS_I286)
                    627:        i80286_code_descriptor(selector, address, 1);
                    628: #else
                    629:        SREG(CS) = selector;
                    630:        i386_load_segment_descriptor(CS);
                    631:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    632: #endif
1.1       root      633: }
                    634: 
1.1.1.24  root      635: /*
                    636: void i386_call_far(UINT16 selector, UINT32 address)
                    637: {
                    638: #if defined(HAS_I386)
                    639:        if(PROTECTED_MODE && !V8086_MODE) {
                    640:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    641:        } else {
                    642:                PUSH16(SREG(CS));
                    643:                PUSH16(m_eip);
                    644:                SREG(CS) = selector;
                    645:                m_performed_intersegment_jump = 1;
                    646:                i386_load_segment_descriptor(CS);
                    647:                m_eip = address;
                    648:                CHANGE_PC(m_eip);
                    649:        }
                    650: #else
                    651:        UINT16 ip = m_pc - SREG_BASE(CS);
                    652:        UINT16 cs = SREG(CS);
                    653: #if defined(HAS_I286)
                    654:        i80286_code_descriptor(selector, address, 2);
                    655: #else
                    656:        SREG(CS) = selector;
                    657:        i386_load_segment_descriptor(CS);
                    658:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    659: #endif
                    660:        PUSH(cs);
                    661:        PUSH(ip);
                    662:        CHANGE_PC(m_pc);
                    663: #endif
                    664: }
                    665: */
                    666: 
1.1.1.29  root      667: UINT16 i386_read_stack()
                    668: {
                    669: #if defined(HAS_I386)
                    670:        UINT32 ea, new_esp;
                    671:        if( STACK_32BIT ) {
                    672:                new_esp = REG32(ESP) + 2;
                    673:                ea = i386_translate(SS, new_esp - 2, 0);
                    674:        } else {
                    675:                new_esp = REG16(SP) + 2;
                    676:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    677:        }
                    678:        return READ16(ea);
                    679: #else
                    680:        UINT16 sp = m_regs.w[SP] + 2;
                    681:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    682: #endif
                    683: }
                    684: 
1.1       root      685: /* ----------------------------------------------------------------------------
                    686:        main
                    687: ---------------------------------------------------------------------------- */
                    688: 
1.1.1.28  root      689: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                    690: {
                    691:        if(dwCtrlType == CTRL_BREAK_EVENT) {
                    692:                // try to finish this program normally
                    693:                m_halted = true;
                    694:                return TRUE;
                    695:        } else if(dwCtrlType == CTRL_C_EVENT) {
                    696:                ctrl_c_pressed = true;
                    697:                return TRUE;
                    698:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                    699:                // this program will be terminated abnormally, do minimum end process
                    700:                exit_handler();
                    701:                exit(1);
                    702:        }
                    703:        return FALSE;
                    704: }
                    705: 
                    706: void exit_handler()
                    707: {
                    708:        if(temp_file_created) {
                    709:                DeleteFile(temp_file_path);
                    710:                temp_file_created = false;
                    711:        }
                    712:        if(key_buf_char != NULL) {
                    713:                key_buf_char->release();
                    714:                delete key_buf_char;
                    715:                key_buf_char = NULL;
                    716:        }
                    717:        if(key_buf_scan != NULL) {
                    718:                key_buf_scan->release();
                    719:                delete key_buf_scan;
                    720:                key_buf_scan = NULL;
                    721:        }
                    722:        hardware_release();
                    723: }
                    724: 
                    725: #ifdef USE_THREAD
                    726: DWORD WINAPI vram_thread(LPVOID)
                    727: {
                    728:        while(!m_halted) {
                    729:                EnterCriticalSection(&vram_crit_sect);
                    730:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                    731:                        vram_flush_char();
                    732:                }
                    733:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                    734:                        vram_flush_attr();
                    735:                }
                    736:                vram_last_length_char = vram_length_char;
                    737:                vram_last_length_attr = vram_length_attr;
                    738:                LeaveCriticalSection(&vram_crit_sect);
                    739:                // this is about half the maximum keyboard repeat rate - any
                    740:                // lower tends to be jerky, any higher misses updates
                    741:                Sleep(15);
                    742:        }
                    743:        return 0;
                    744: }
                    745: #endif
                    746: 
                    747: long get_section_in_exec_file(FILE *fp, char *name)
                    748: {
                    749:        UINT8 header[0x400];
                    750:        
                    751:        long position = ftell(fp);
                    752:        fseek(fp, 0, SEEK_SET);
                    753:        fread(header, sizeof(header), 1, fp);
                    754:        fseek(fp, position, SEEK_SET);
                    755:        
                    756:        try {
                    757:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                    758:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                    759:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                    760:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                    761:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                    762:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                    763:                
                    764:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                    765:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                    766:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                    767:                                return(sectionHeader->PointerToRawData);
                    768:                        }
                    769:                }
                    770:        } catch(...) {
                    771:        }
                    772:        return(0);
                    773: }
                    774: 
1.1.1.10  root      775: bool is_started_from_command_prompt()
                    776: {
1.1.1.18  root      777:        bool ret = false;
                    778:        
                    779:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                    780:        if(hLibrary) {
                    781:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                    782:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                    783:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                    784:                if(lpfnGetConsoleProcessList) {
                    785:                        DWORD pl;
                    786:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                    787:                        FreeLibrary(hLibrary);
                    788:                        return(ret);
                    789:                }
                    790:                FreeLibrary(hLibrary);
                    791:        }
                    792:        
                    793:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                    794:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                    795:                DWORD dwParentProcessID = 0;
                    796:                PROCESSENTRY32 pe32;
                    797:                pe32.dwSize = sizeof(PROCESSENTRY32);
                    798:                if(Process32First(hSnapshot, &pe32)) {
                    799:                        do {
                    800:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                    801:                                        dwParentProcessID = pe32.th32ParentProcessID;
                    802:                                        break;
                    803:                                }
                    804:                        } while(Process32Next(hSnapshot, &pe32));
                    805:                }
                    806:                CloseHandle(hSnapshot);
                    807:                if(dwParentProcessID != 0) {
                    808:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                    809:                        if(hProcess != NULL) {
                    810:                                HMODULE hMod;
                    811:                                DWORD cbNeeded;
                    812:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                    813:                                        char module_name[MAX_PATH];
                    814:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                    815:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                    816:                                        }
                    817:                                }
                    818:                                CloseHandle(hProcess);
                    819:                        }
                    820:                }
                    821:        }
                    822:        return(ret);
1.1.1.14  root      823: }
                    824: 
                    825: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                    826: {
1.1.1.24  root      827:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root      828:        OSVERSIONINFOEX osvi;
                    829:        DWORDLONG dwlConditionMask = 0;
                    830:        int op = VER_GREATER_EQUAL;
                    831:        
                    832:        // Initialize the OSVERSIONINFOEX structure.
                    833:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                    834:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                    835:        osvi.dwMajorVersion = dwMajorVersion;
                    836:        osvi.dwMinorVersion = dwMinorVersion;
                    837:        osvi.wServicePackMajor = wServicePackMajor;
                    838:        osvi.wServicePackMinor = wServicePackMinor;
                    839:        
                    840:         // Initialize the condition mask.
                    841:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                    842:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                    843:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                    844:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                    845:        
                    846:        // Perform the test.
                    847:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                    848: }
                    849: 
1.1.1.27  root      850: void get_sio_port_numbers()
                    851: {
                    852:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                    853:        HDEVINFO hDevInfo = 0;
                    854:        HKEY hKey = 0;
                    855:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                    856:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                    857:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                    858:                                char chData[256];
                    859:                                DWORD dwType = 0;
                    860:                                DWORD dwSize = sizeof(chData);
                    861:                                int port_number = 0;
                    862:                                
                    863:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                    864:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                    865:                                                port_number = atoi(chData + 3);
                    866:                                        }
                    867:                                }
                    868:                                RegCloseKey(hKey);
                    869:                                
1.1.1.29  root      870:                                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      871:                                        continue;
                    872:                                }
                    873:                                if(sio_port_number[0] == 0) {
                    874:                                        sio_port_number[0] = port_number;
                    875:                                } else if(sio_port_number[1] == 0) {
                    876:                                        sio_port_number[1] = port_number;
1.1.1.29  root      877:                                } else if(sio_port_number[2] == 0) {
                    878:                                        sio_port_number[2] = port_number;
                    879:                                } else if(sio_port_number[3] == 0) {
                    880:                                        sio_port_number[3] = port_number;
1.1.1.27  root      881:                                }
1.1.1.29  root      882:                                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      883:                                        break;
                    884:                                }
                    885:                        }
                    886:                }
                    887:        }
                    888: }
                    889: 
1.1.1.28  root      890: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                    891: 
1.1       root      892: int main(int argc, char *argv[], char *envp[])
                    893: {
1.1.1.9   root      894:        int arg_offset = 0;
                    895:        int standard_env = 0;
1.1.1.14  root      896:        int buf_width = 0, buf_height = 0;
1.1.1.28  root      897:        bool get_console_info_success = false;
                    898:        bool screen_size_changed = false;
                    899:        
                    900:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                    901:        GetModuleFileName(NULL, path, MAX_PATH);
                    902:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root      903:        
1.1.1.27  root      904:        char dummy_argv_0[] = "msdos.exe";
                    905:        char dummy_argv_1[MAX_PATH];
                    906:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                    907:        char new_exec_file[MAX_PATH];
                    908:        bool convert_cmd_file = false;
1.1.1.28  root      909:        unsigned int code_page = 0;
1.1.1.27  root      910:        
                    911:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root      912:                // check if command file is embedded to this execution file
                    913:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root      914:                FILE* fp = fopen(full, "rb");
1.1.1.28  root      915:                long offset = get_section_in_exec_file(fp, ".msdos");
                    916:                if(offset != 0) {
1.1.1.30  root      917:                        UINT8 buffer[16];
1.1.1.28  root      918:                        fseek(fp, offset, SEEK_SET);
                    919:                        fread(buffer, sizeof(buffer), 1, fp);
                    920:                        
                    921:                        // restore flags
                    922:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                    923:                        no_windows          = ((buffer[0] & 0x02) != 0);
                    924:                        standard_env        = ((buffer[0] & 0x04) != 0);
                    925:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                    926:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                    927:                        if((buffer[0] & 0x20) != 0) {
                    928:                                get_sio_port_numbers();
                    929:                        }
                    930:                        if((buffer[0] & 0x40) != 0) {
                    931:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                    932:                                support_ems = true;
1.1.1.30  root      933:                        }
1.1.1.27  root      934: #ifdef SUPPORT_XMS
1.1.1.30  root      935:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root      936:                                support_xms = true;
                    937:                        }
1.1.1.30  root      938: #endif
1.1.1.28  root      939:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                    940:                                buf_width  = buffer[1] | (buffer[2] << 8);
                    941:                                buf_height = buffer[3] | (buffer[4] << 8);
                    942:                        }
                    943:                        if(buffer[5] != 0) {
1.1.1.30  root      944:                                dos_major_version = buffer[5];
                    945:                                dos_minor_version = buffer[6];
                    946:                        }
                    947:                        if(buffer[7] != 0) {
                    948:                                win_major_version = buffer[7];
                    949:                                win_minor_version = buffer[8];
1.1.1.28  root      950:                        }
1.1.1.30  root      951:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root      952:                                SetConsoleCP(code_page);
                    953:                                SetConsoleOutputCP(code_page);
                    954:                        }
1.1.1.30  root      955:                        int name_len = buffer[11];
                    956:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root      957:                        
                    958:                        // restore command file name
                    959:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                    960:                        fread(dummy_argv_1, name_len, 1, fp);
                    961:                        
                    962:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                    963:                                // if original command file exists, create a temporary file name
                    964:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                    965:                                        // create a temporary command file in the current director
                    966:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root      967:                                } else {
1.1.1.28  root      968:                                        // create a temporary command file in the temporary folder
                    969:                                        GetTempPath(MAX_PATH, path);
                    970:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                    971:                                                DeleteFile(dummy_argv_1);
                    972:                                        } else {
                    973:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                    974:                                        }
1.1.1.27  root      975:                                }
1.1.1.28  root      976:                                // check the command file type
                    977:                                fread(buffer, 2, 1, fp);
                    978:                                fseek(fp, -2, SEEK_CUR);
                    979:                                if(memcmp(buffer, "MZ", 2) != 0) {
                    980:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                    981:                                } else {
                    982:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root      983:                                }
                    984:                        }
1.1.1.28  root      985:                        
                    986:                        // restore command file
                    987:                        FILE* fo = fopen(dummy_argv_1, "wb");
                    988:                        for(int i = 0; i < file_len; i++) {
                    989:                                fputc(fgetc(fp), fo);
                    990:                        }
                    991:                        fclose(fo);
                    992:                        
                    993:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                    994:                        temp_file_created = true;
                    995:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                    996:                        
                    997:                        // adjust argc/argv
                    998:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                    999:                                dummy_argv[i + 1] = argv[i];
                   1000:                        }
                   1001:                        argc++;
                   1002:                        argv = dummy_argv;
1.1.1.27  root     1003:                }
                   1004:                fclose(fp);
                   1005:        }
1.1.1.9   root     1006:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     1007:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   1008:                        stay_busy = true;
                   1009:                        arg_offset++;
1.1.1.27  root     1010:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   1011:                        if(argv[i][2] != '\0') {
                   1012:                                strcpy(new_exec_file, &argv[i][2]);
                   1013:                        } else {
                   1014:                                strcpy(new_exec_file, "new_exec_file.exe");
                   1015:                        }
                   1016:                        convert_cmd_file = true;
                   1017:                        arg_offset++;
1.1.1.28  root     1018:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   1019:                        if(IS_NUMERIC(argv[i][2])) {
                   1020:                                code_page = atoi(&argv[i][2]);
                   1021:                        } else {
                   1022:                                code_page = GetConsoleCP();
                   1023:                        }
                   1024:                        arg_offset++;
1.1.1.25  root     1025:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   1026:                        no_windows = true;
                   1027:                        arg_offset++;
                   1028:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     1029:                        standard_env = 1;
                   1030:                        arg_offset++;
1.1.1.14  root     1031:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   1032:                        ignore_illegal_insn = true;
                   1033:                        arg_offset++;
                   1034:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   1035:                        limit_max_memory = true;
                   1036:                        arg_offset++;
                   1037:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     1038:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   1039:                                buf_width = buf_height = 0;
                   1040:                        }
                   1041:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   1042:                                buf_width = 80;
                   1043:                        }
                   1044:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   1045:                                buf_height = 25;
                   1046:                        }
1.1.1.14  root     1047:                        arg_offset++;
1.1.1.25  root     1048:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     1049:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     1050:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   1051:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   1052:                                        sio_port_number[1] = atoi(p1 + 1);
                   1053:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   1054:                                                sio_port_number[2] = atoi(p2 + 1);
                   1055:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   1056:                                                        sio_port_number[3] = atoi(p3 + 1);
                   1057:                                                }
                   1058:                                        }
1.1.1.25  root     1059:                                }
1.1.1.29  root     1060:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     1061:                        }
1.1.1.29  root     1062:                        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     1063:                                get_sio_port_numbers();
1.1.1.25  root     1064:                        }
                   1065:                        arg_offset++;
1.1.1.9   root     1066:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     1067:                        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     1068:                                dos_major_version = argv[i][2] - '0';
                   1069:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   1070:                        }
                   1071:                        arg_offset++;
                   1072:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   1073:                        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]))) {
                   1074:                                win_major_version = argv[i][2] - '0';
                   1075:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     1076:                        }
                   1077:                        arg_offset++;
1.1.1.25  root     1078:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   1079:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   1080:                        support_ems = true;
                   1081: #ifdef SUPPORT_XMS
                   1082:                        support_xms = true;
                   1083: #endif
                   1084:                        arg_offset++;
1.1.1.9   root     1085:                } else {
                   1086:                        break;
                   1087:                }
                   1088:        }
                   1089:        if(argc < 2 + arg_offset) {
1.1       root     1090: #ifdef _WIN64
1.1.1.14  root     1091:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     1092: #else
1.1.1.14  root     1093:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     1094: #endif
1.1.1.25  root     1095:                fprintf(stderr,
1.1.1.28  root     1096:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     1097:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     1098:                        "\n"
                   1099:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     1100: #ifdef _WIN64
1.1.1.27  root     1101:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     1102: #else
1.1.1.27  root     1103:                        "\t-c\tconvert command file to 32bit execution file\n"
                   1104: #endif
1.1.1.28  root     1105:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     1106:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   1107:                        "\t-e\tuse a reduced environment block\n"
                   1108:                        "\t-i\tignore invalid instructions\n"
                   1109:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   1110:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   1111:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   1112:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     1113:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     1114: #ifdef SUPPORT_XMS
1.1.1.28  root     1115:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     1116: #else
1.1.1.28  root     1117:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     1118: #endif
                   1119:                );
1.1.1.10  root     1120:                
                   1121:                if(!is_started_from_command_prompt()) {
                   1122:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   1123:                        while(!_kbhit()) {
                   1124:                                Sleep(10);
                   1125:                        }
                   1126:                }
1.1.1.20  root     1127: #ifdef _DEBUG
                   1128:                _CrtDumpMemoryLeaks();
                   1129: #endif
1.1       root     1130:                return(EXIT_FAILURE);
                   1131:        }
1.1.1.27  root     1132:        if(convert_cmd_file) {
                   1133:                retval = EXIT_FAILURE;
1.1.1.28  root     1134:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     1135:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     1136:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     1137:                        
1.1.1.28  root     1138:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   1139:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   1140:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   1141:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     1142:                        } else {
1.1.1.28  root     1143:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   1144:                                if(offset != 0) {
                   1145:                                        UINT8 buffer[14];
                   1146:                                        fseek(fp, offset, SEEK_SET);
                   1147:                                        fread(buffer, sizeof(buffer), 1, fp);
                   1148:                                        memset(path, 0, sizeof(path));
                   1149:                                        fread(path, buffer[9], 1, fp);
                   1150:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   1151:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   1152:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   1153:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   1154:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   1155:                                } else {
                   1156:                                        // read pe header of msdos.exe
                   1157:                                        UINT8 header[0x400];
                   1158:                                        fseek(fp, 0, SEEK_SET);
                   1159:                                        fread(header, sizeof(header), 1, fp);
                   1160:                                        
                   1161:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   1162:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   1163:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   1164:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   1165:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   1166:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   1167:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   1168:                                        
                   1169:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   1170:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   1171:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   1172:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   1173:                                        if(dwExtraLastSectionBytes != 0) {
                   1174:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   1175:                                                dwLastSectionSize += dwRemain;
                   1176:                                        }
                   1177:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   1178:                                        
                   1179:                                        // store msdos.exe
                   1180:                                        fseek(fp, 0, SEEK_SET);
                   1181:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   1182:                                                if((data = fgetc(fp)) != EOF) {
                   1183:                                                        fputc(data, fo);
                   1184:                                                } else {
                   1185:                                                        // we should not reach here :-(
                   1186:                                                        fputc(0, fo);
                   1187:                                                }
                   1188:                                        }
                   1189:                                        
                   1190:                                        // store options
                   1191:                                        UINT8 flags = 0;
                   1192:                                        if(stay_busy) {
                   1193:                                                flags |= 0x01;
                   1194:                                        }
                   1195:                                        if(no_windows) {
                   1196:                                                flags |= 0x02;
                   1197:                                        }
                   1198:                                        if(standard_env) {
                   1199:                                                flags |= 0x04;
                   1200:                                        }
                   1201:                                        if(ignore_illegal_insn) {
                   1202:                                                flags |= 0x08;
                   1203:                                        }
                   1204:                                        if(limit_max_memory) {
                   1205:                                                flags |= 0x10;
                   1206:                                        }
1.1.1.29  root     1207:                                        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     1208:                                                flags |= 0x20;
                   1209:                                        }
                   1210:                                        if(support_ems) {
                   1211:                                                flags |= 0x40;
                   1212:                                        }
1.1.1.30  root     1213: #ifdef SUPPORT_XMS
                   1214:                                        if(support_xms) {
                   1215:                                                flags |= 0x80;
                   1216:                                        }
                   1217: #endif
1.1.1.28  root     1218:                                        
                   1219:                                        fputc(flags, fo);
                   1220:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   1221:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   1222:                                        fputc((buf_height >> 0) & 0xff, fo);
                   1223:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     1224:                                        fputc(dos_major_version, fo);
                   1225:                                        fputc(dos_minor_version, fo);
                   1226:                                        fputc(win_major_version, fo);
                   1227:                                        fputc(win_minor_version, fo);
1.1.1.28  root     1228:                                        fputc((code_page >> 0) & 0xff, fo);
                   1229:                                        fputc((code_page >> 8) & 0xff, fo);
                   1230:                                        
                   1231:                                        // store command file info
                   1232:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   1233:                                        int name_len = strlen(name);
                   1234:                                        fseek(fs, 0, SEEK_END);
                   1235:                                        long file_size = ftell(fs);
                   1236:                                        
                   1237:                                        fputc(name_len, fo);
                   1238:                                        fputc((file_size >>  0) & 0xff, fo);
                   1239:                                        fputc((file_size >>  8) & 0xff, fo);
                   1240:                                        fputc((file_size >> 16) & 0xff, fo);
                   1241:                                        fputc((file_size >> 24) & 0xff, fo);
                   1242:                                        fwrite(name, name_len, 1, fo);
                   1243:                                        
                   1244:                                        // store command file
                   1245:                                        fseek(fs, 0, SEEK_SET);
                   1246:                                        for(int i = 0; i < file_size; i++) {
                   1247:                                                if((data = fgetc(fs)) != EOF) {
                   1248:                                                        fputc(data, fo);
                   1249:                                                } else {
                   1250:                                                        // we should not reach here :-(
                   1251:                                                        fputc(0, fo);
                   1252:                                                }
                   1253:                                        }
                   1254:                                        
                   1255:                                        // store padding data and update pe header
1.1.1.29  root     1256:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   1257:                                        coffHeader->NumberOfSections++;
                   1258:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   1259:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   1260:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   1261:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   1262:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     1263:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   1264:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   1265:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     1266:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     1267:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   1268:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     1269:                                                        if(i < 2) {
                   1270:                                                                fputc(padding[i & 15], fo);
                   1271:                                                        } else {
                   1272:                                                                fputc(padding[(i - 2) & 15], fo);
                   1273:                                                        }
1.1.1.28  root     1274:                                                }
                   1275:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   1276:                                        }
                   1277:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   1278:                                        
                   1279:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   1280:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   1281:                                        if(dwExtraNewSectionBytes != 0) {
                   1282:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   1283:                                                dwNewSectionSize += dwRemain;
                   1284:                                        }
                   1285:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   1286:                                        
                   1287:                                        fseek(fo, 0, SEEK_SET);
                   1288:                                        fwrite(header, sizeof(header), 1, fo);
                   1289:                                        
                   1290:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   1291:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     1292:                                }
                   1293:                        }
                   1294:                        if(fp != NULL) {
                   1295:                                fclose(fp);
                   1296:                        }
                   1297:                        if(fs != NULL) {
                   1298:                                fclose(fs);
                   1299:                        }
                   1300:                        if(fo != NULL) {
                   1301:                                fclose(fo);
                   1302:                        }
                   1303:                }
                   1304: #ifdef _DEBUG
                   1305:                _CrtDumpMemoryLeaks();
                   1306: #endif
                   1307:                return(retval);
                   1308:        }
1.1       root     1309:        
1.1.1.14  root     1310:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   1311:        
1.1.1.23  root     1312:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     1313:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     1314:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     1315:        
1.1.1.28  root     1316:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     1317:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     1318:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     1319:        
1.1.1.14  root     1320:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   1321:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   1322:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   1323:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     1324:                }
                   1325:        }
1.1.1.28  root     1326:        if(get_console_info_success) {
1.1.1.12  root     1327:                scr_width = csbi.dwSize.X;
1.1.1.14  root     1328:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   1329:                
1.1.1.28  root     1330:                // v-text shadow buffer size must be lesser than 0x7fd0
                   1331:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     1332:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   1333:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   1334:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     1335:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     1336:                                scr_width = 80;
                   1337:                                scr_height = 25;
                   1338:                        }
1.1.1.28  root     1339:                        screen_size_changed = true;
1.1.1.14  root     1340:                }
1.1.1.12  root     1341:        } else {
                   1342:                // for a proof (not a console)
                   1343:                scr_width = 80;
                   1344:                scr_height = 25;
                   1345:        }
1.1.1.14  root     1346:        scr_buf_size.X = scr_width;
                   1347:        scr_buf_size.Y = scr_height;
                   1348:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   1349:        scr_top = csbi.srWindow.Top;
1.1       root     1350:        cursor_moved = false;
                   1351:        
1.1.1.25  root     1352:        key_buf_char = new FIFO(256);
                   1353:        key_buf_scan = new FIFO(256);
1.1       root     1354:        
                   1355:        hardware_init();
                   1356:        
1.1.1.9   root     1357:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     1358:                retval = EXIT_FAILURE;
                   1359:        } else {
1.1.1.27  root     1360: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     1361:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   1362: #endif
                   1363:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   1364:                
1.1.1.28  root     1365:                if(screen_size_changed) {
1.1.1.24  root     1366:                        change_console_size(scr_width, scr_height);
                   1367:                }
1.1.1.8   root     1368:                TIMECAPS caps;
                   1369:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   1370:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.14  root     1371: #ifdef USE_THREAD
                   1372:                InitializeCriticalSection(&vram_crit_sect);
                   1373:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   1374: #endif
1.1       root     1375:                hardware_run();
1.1.1.14  root     1376: #ifdef USE_THREAD
                   1377:                vram_flush();
                   1378:                DeleteCriticalSection(&vram_crit_sect);
                   1379: #endif
1.1.1.24  root     1380:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     1381:                
1.1.1.24  root     1382:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     1383:                if(get_console_info_success) {
1.1.1.23  root     1384:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     1385:                        if(restore_console_on_exit) {
1.1.1.14  root     1386:                                // window can't be bigger than buffer,
                   1387:                                // buffer can't be smaller than window,
                   1388:                                // so make a tiny window,
                   1389:                                // set the required buffer,
                   1390:                                // then set the required window
                   1391:                                SMALL_RECT rect;
                   1392:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   1393:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     1394:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     1395:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     1396:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   1397:                        }
1.1.1.14  root     1398:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   1399:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     1400:                }
1.1.1.24  root     1401:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   1402:                
1.1       root     1403:                msdos_finish();
1.1.1.14  root     1404:                
                   1405:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     1406:        }
                   1407:        
1.1.1.10  root     1408:        hardware_finish();
                   1409:        
1.1.1.28  root     1410:        if(key_buf_char != NULL) {
                   1411:                key_buf_char->release();
                   1412:                delete key_buf_char;
                   1413:                key_buf_char = NULL;
                   1414:        }
                   1415:        if(key_buf_scan != NULL) {
                   1416:                key_buf_scan->release();
                   1417:                delete key_buf_scan;
                   1418:                key_buf_scan = NULL;
                   1419:        }
                   1420:        if(temp_file_created) {
                   1421:                DeleteFile(temp_file_path);
                   1422:                temp_file_created = false;
                   1423:        }
                   1424: //     if(argv == dummy_argv) {
                   1425: //             if(!is_started_from_command_prompt()) {
                   1426: //                     fprintf(stderr, "\nHit any key to quit...");
                   1427: //                     while(!_kbhit()) {
                   1428: //                             Sleep(10);
                   1429: //                     }
                   1430: //             }
                   1431: //     }
1.1.1.20  root     1432: #ifdef _DEBUG
                   1433:        _CrtDumpMemoryLeaks();
                   1434: #endif
1.1       root     1435:        return(retval);
                   1436: }
                   1437: 
1.1.1.20  root     1438: /* ----------------------------------------------------------------------------
                   1439:        console
                   1440: ---------------------------------------------------------------------------- */
                   1441: 
1.1.1.14  root     1442: void change_console_size(int width, int height)
1.1.1.12  root     1443: {
1.1.1.23  root     1444:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     1445:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   1446:        SMALL_RECT rect;
                   1447:        COORD co;
                   1448:        
                   1449:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     1450:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   1451:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   1452:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   1453:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1454:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1455:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   1456:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   1457:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1458:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1459:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     1460:                }
                   1461:        }
1.1.1.14  root     1462:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     1463:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     1464:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     1465:                SetConsoleCursorPosition(hStdout, co);
                   1466:                cursor_moved = true;
                   1467:        }
1.1.1.14  root     1468:        
                   1469:        // window can't be bigger than buffer,
                   1470:        // buffer can't be smaller than window,
                   1471:        // so make a tiny window,
                   1472:        // set the required buffer,
                   1473:        // then set the required window
                   1474:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     1475:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     1476:        co.X = width;
                   1477:        co.Y = height;
1.1.1.12  root     1478:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     1479:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1480:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   1481:        
                   1482:        scr_width = scr_buf_size.X = width;
                   1483:        scr_height = scr_buf_size.Y = height;
                   1484:        scr_top = 0;
                   1485:        
                   1486:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   1487:        
                   1488:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     1489:        text_vram_end_address = text_vram_top_address + regen;
                   1490:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   1491:        
1.1.1.14  root     1492:        if(regen > 0x4000) {
                   1493:                regen = 0x8000;
                   1494:                vram_pages = 1;
                   1495:        } else if(regen > 0x2000) {
                   1496:                regen = 0x4000;
                   1497:                vram_pages = 2;
                   1498:        } else if(regen > 0x1000) {
                   1499:                regen = 0x2000;
                   1500:                vram_pages = 4;
                   1501:        } else {
                   1502:                regen = 0x1000;
                   1503:                vram_pages = 8;
                   1504:        }
1.1.1.15  root     1505:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   1506:        *(UINT16 *)(mem + 0x44c) = regen;
                   1507:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   1508:        
1.1.1.24  root     1509:        mouse.min_position.x = 0;
                   1510:        mouse.min_position.y = 0;
                   1511:        mouse.max_position.x = 8 * scr_width  - 1;
                   1512:        mouse.max_position.y = 8 * scr_height - 1;
                   1513:        
1.1.1.15  root     1514:        restore_console_on_exit = true;
1.1.1.14  root     1515: }
                   1516: 
                   1517: void clear_scr_buffer(WORD attr)
                   1518: {
                   1519:        for(int y = 0; y < scr_height; y++) {
                   1520:                for(int x = 0; x < scr_width; x++) {
                   1521:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   1522:                        SCR_BUF(y,x).Attributes = attr;
                   1523:                }
                   1524:        }
1.1.1.12  root     1525: }
                   1526: 
1.1.1.24  root     1527: bool update_console_input()
1.1       root     1528: {
1.1.1.23  root     1529:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     1530:        DWORD dwNumberOfEvents = 0;
1.1       root     1531:        DWORD dwRead;
                   1532:        INPUT_RECORD ir[16];
1.1.1.24  root     1533:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   1534:        bool result = false;
1.1       root     1535:        
1.1.1.8   root     1536:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   1537:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   1538:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     1539:                                if(ir[i].EventType & MOUSE_EVENT) {
                   1540:                                        if(mouse.active) {
                   1541:                                                if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   1542:                                                        for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   1543:                                                                static const DWORD bits[] = {
                   1544:                                                                        FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   1545:                                                                        RIGHTMOST_BUTTON_PRESSED,       // right
                   1546:                                                                        FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
1.1.1.14  root     1547:                                                                };
1.1.1.24  root     1548:                                                                bool prev_status = mouse.buttons[i].status;
                   1549:                                                                mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   1550:                                                                
                   1551:                                                                if(!prev_status && mouse.buttons[i].status) {
                   1552:                                                                        mouse.buttons[i].pressed_times++;
                   1553:                                                                        mouse.buttons[i].pressed_position.x = mouse.position.x;
                   1554:                                                                        mouse.buttons[i].pressed_position.y = mouse.position.y;
                   1555:                                                                        mouse.status |= 2 << (i * 2);
                   1556:                                                                } else if(prev_status && !mouse.buttons[i].status) {
                   1557:                                                                        mouse.buttons[i].released_times++;
                   1558:                                                                        mouse.buttons[i].released_position.x = mouse.position.x;
                   1559:                                                                        mouse.buttons[i].released_position.y = mouse.position.y;
                   1560:                                                                        mouse.status |= 4 << (i * 2);
                   1561:                                                                }
1.1.1.14  root     1562:                                                        }
1.1.1.24  root     1563:                                                } else if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   1564:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   1565:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   1566:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.14  root     1567:                                                        }
1.1.1.28  root     1568:                                                        // FIXME: character size is always 8x8 ???
                   1569:                                                        int x = 3 + 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   1570:                                                        int y = 4 + 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
1.1.1.24  root     1571:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   1572:                                                                mouse.position.x = x;
                   1573:                                                                mouse.position.y = y;
                   1574:                                                                mouse.status |= 1;
1.1.1.14  root     1575:                                                        }
                   1576:                                                }
                   1577:                                        }
1.1.1.24  root     1578:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.28  root     1579:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     1580:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.14  root     1581:                                        if(!ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     1582:                                                // break
1.1.1.14  root     1583:                                                kbd_data |= 0x80;
1.1.1.24  root     1584:                                        } else {
1.1.1.28  root     1585:                                                // make
1.1.1.24  root     1586:                                                kbd_data &= 0x7f;
                   1587:                                                
                   1588:                                                // update dos key buffer
                   1589:                                                UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   1590:                                                UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   1591:                                                
                   1592:                                                if(chr == 0) {
                   1593:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1594:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1595:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   1596:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1597:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   1598:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   1599:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   1600:                                                                } else if(scn == 0x35) {
                   1601:                                                                        scn = 0xa4;             // keypad /
                   1602:                                                                }
                   1603:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   1604:                                                                if(scn == 0x07) {
                   1605:                                                                        chr = 0x1e;     // Ctrl+^
                   1606:                                                                } else if(scn == 0x0c) {
                   1607:                                                                        chr = 0x1f;     // Ctrl+_
                   1608:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   1609:                                                                        static const UINT8 ctrl_map[] = {
                   1610:                                                                                0x95,   // keypad /
                   1611:                                                                                0,
                   1612:                                                                                0x96,   // keypad *
                   1613:                                                                                0, 0, 0,
                   1614:                                                                                0x5e,   // F1
                   1615:                                                                                0x5f,   // F2
                   1616:                                                                                0x60,   // F3
                   1617:                                                                                0x61,   // F4
                   1618:                                                                                0x62,   // F5
                   1619:                                                                                0x63,   // F6
                   1620:                                                                                0x64,   // F7
                   1621:                                                                                0x65,   // F8
                   1622:                                                                                0x66,   // F9
                   1623:                                                                                0x67,   // F10
                   1624:                                                                                0,
                   1625:                                                                                0,
                   1626:                                                                                0x77,   // Home
                   1627:                                                                                0x8d,   // Up
                   1628:                                                                                0x84,   // PgUp
                   1629:                                                                                0x8e,   // keypad -
                   1630:                                                                                0x73,   // Left
                   1631:                                                                                0x8f,   // keypad center
                   1632:                                                                                0x74,   // Right
                   1633:                                                                                0x90,   // keyapd +
                   1634:                                                                                0x75,   // End
                   1635:                                                                                0x91,   // Down
                   1636:                                                                                0x76,   // PgDn
                   1637:                                                                                0x92,   // Insert
                   1638:                                                                                0x93,   // Delete
                   1639:                                                                                0, 0, 0,
                   1640:                                                                                0x89,   // F11
                   1641:                                                                                0x8a,   // F12
                   1642:                                                                        };
                   1643:                                                                        scn = ctrl_map[scn - 0x35];
                   1644:                                                                }
                   1645:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   1646:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1647:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   1648:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1649:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   1650:                                                                }
                   1651:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   1652:                                                                scn += 0x85 - 0x57;
                   1653:                                                        }
                   1654:                                                        // ignore shift, ctrl, alt, win and menu keys
                   1655:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
                   1656:                                                                if(chr == 0) {
                   1657:                                                                        key_buf_char->write(0x00);
                   1658:                                                                        key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   1659:                                                                }
                   1660:                                                                key_buf_char->write(chr);
                   1661:                                                                key_buf_scan->write(scn);
                   1662:                                                        }
                   1663:                                                } else {
                   1664:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1665:                                                                chr = 0;
                   1666:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   1667:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   1668:                                                                }
                   1669:                                                        }
                   1670:                                                        key_buf_char->write(chr);
                   1671:                                                        key_buf_scan->write(scn);
                   1672:                                                }
1.1       root     1673:                                        }
1.1.1.24  root     1674:                                        result = key_changed = true;
1.1       root     1675:                                }
                   1676:                        }
                   1677:                }
                   1678:        }
1.1.1.24  root     1679:        return(result);
1.1.1.8   root     1680: }
                   1681: 
1.1.1.14  root     1682: bool update_key_buffer()
1.1.1.8   root     1683: {
1.1.1.24  root     1684:        return(update_console_input() || key_buf_char->count() != 0);
1.1.1.8   root     1685: }
                   1686: 
1.1.1.20  root     1687: /* ----------------------------------------------------------------------------
                   1688:        MS-DOS virtual machine
                   1689: ---------------------------------------------------------------------------- */
                   1690: 
                   1691: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   1692: int msdos_psp_get_file_table(int fd, int psp_seg);
                   1693: void msdos_putch(UINT8 data);
                   1694: 
1.1       root     1695: // process info
                   1696: 
                   1697: process_t *msdos_process_info_create(UINT16 psp_seg)
                   1698: {
                   1699:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1700:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   1701:                        memset(&process[i], 0, sizeof(process_t));
                   1702:                        process[i].psp = psp_seg;
                   1703:                        return(&process[i]);
                   1704:                }
                   1705:        }
                   1706:        fatalerror("too many processes\n");
                   1707:        return(NULL);
                   1708: }
                   1709: 
                   1710: process_t *msdos_process_info_get(UINT16 psp_seg)
                   1711: {
                   1712:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1713:                if(process[i].psp == psp_seg) {
                   1714:                        return(&process[i]);
                   1715:                }
                   1716:        }
                   1717:        fatalerror("invalid psp address\n");
                   1718:        return(NULL);
                   1719: }
                   1720: 
1.1.1.23  root     1721: void msdos_sda_update(int psp_seg)
                   1722: {
                   1723:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   1724:        
                   1725:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1726:                if(process[i].psp == psp_seg) {
                   1727:                        sda->switchar = process[i].switchar;
                   1728:                        sda->current_dta.w.l = process[i].dta.w.l;
                   1729:                        sda->current_dta.w.h = process[i].dta.w.h;
                   1730:                        sda->current_psp = process[i].psp;
                   1731:                        break;
                   1732:                }
                   1733:        }
                   1734:        sda->malloc_strategy = malloc_strategy;
                   1735:        sda->return_code = retval;
                   1736:        sda->current_drive = _getdrive();
                   1737: }
                   1738: 
1.1.1.13  root     1739: // dta info
                   1740: 
                   1741: void msdos_dta_info_init()
                   1742: {
1.1.1.14  root     1743:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     1744:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1745:        }
                   1746: }
                   1747: 
                   1748: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   1749: {
                   1750:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     1751:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1752:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   1753:                        if(free_dta == NULL) {
1.1.1.13  root     1754:                                free_dta = &dtalist[i];
                   1755:                        }
1.1.1.14  root     1756:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     1757:                        return(&dtalist[i]);
                   1758:                }
                   1759:        }
1.1.1.14  root     1760:        if(free_dta) {
1.1.1.13  root     1761:                free_dta->psp = psp_seg;
                   1762:                free_dta->dta = dta_laddr;
                   1763:                return(free_dta);
                   1764:        }
                   1765:        fatalerror("too many dta\n");
                   1766:        return(NULL);
                   1767: }
                   1768: 
                   1769: void msdos_dta_info_free(UINT16 psp_seg)
                   1770: {
1.1.1.14  root     1771:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1772:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     1773:                        FindClose(dtalist[i].find_handle);
                   1774:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1775:                }
                   1776:        }
                   1777: }
                   1778: 
1.1       root     1779: void msdos_cds_update(int drv)
                   1780: {
                   1781:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                   1782:        
                   1783:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                   1784:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   1785:        cds->drive_attrib = 0x4000;     // physical drive
                   1786:        cds->physical_drive_number = drv;
                   1787: }
                   1788: 
1.1.1.17  root     1789: // nls information tables
                   1790: 
                   1791: // uppercase table (func 6502h)
                   1792: void msdos_upper_table_update()
                   1793: {
                   1794:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1795:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1796:                UINT8 c[4];
                   1797:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1798:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1799:                c[0] = 0x80 + i;
                   1800:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   1801:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1802:        }
                   1803: }
                   1804: 
1.1.1.23  root     1805: // lowercase table (func 6503h)
                   1806: void msdos_lower_table_update()
                   1807: {
                   1808:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   1809:        for(unsigned i = 0; i < 0x80; ++i) {
                   1810:                UINT8 c[4];
                   1811:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1812:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1813:                c[0] = 0x80 + i;
                   1814:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   1815:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1816:        }
                   1817: }
                   1818: 
1.1.1.17  root     1819: // filename uppercase table (func 6504h)
                   1820: void msdos_filename_upper_table_init()
                   1821: {
                   1822:        // depended on (file)system, not on active codepage
                   1823:        // temporary solution: just filling data
                   1824:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1825:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1826:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   1827:        }
                   1828: }
                   1829: 
                   1830: // filaname terminator table (func 6505h)
                   1831: void msdos_filename_terminator_table_init()
                   1832: {
                   1833:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   1834:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   1835:        
                   1836:        data[2] = 1;            // marker? (permissible character value)
                   1837:        data[3] = 0x00;         // 00h...FFh
                   1838:        data[4] = 0xff;
                   1839:        data[5] = 0;            // marker? (excluded character)
                   1840:        data[6] = 0x00;         // 00h...20h
                   1841:        data[7] = 0x20;
                   1842:        data[8] = 2;            // marker? (illegal characters for filename)
                   1843:        data[9] = (UINT8)strlen(illegal_chars);
                   1844:        memcpy(data + 10, illegal_chars, data[9]);
                   1845:        
                   1846:        // total length
                   1847:        *(UINT16 *)data = (10 - 2) + data[9];
                   1848: }
                   1849: 
                   1850: // collating table (func 6506h)
                   1851: void msdos_collating_table_update()
                   1852: {
                   1853:        // temporary solution: just filling data
                   1854:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     1855:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     1856:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   1857:        }
                   1858: }
                   1859: 
1.1       root     1860: // dbcs
                   1861: 
                   1862: void msdos_dbcs_table_update()
                   1863: {
                   1864:        UINT8 dbcs_data[DBCS_SIZE];
                   1865:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   1866:        
                   1867:        CPINFO info;
                   1868:        GetCPInfo(active_code_page, &info);
                   1869:        
                   1870:        if(info.MaxCharSize != 1) {
                   1871:                for(int i = 0;; i += 2) {
                   1872:                        UINT8 lo = info.LeadByte[i + 0];
                   1873:                        UINT8 hi = info.LeadByte[i + 1];
                   1874:                        dbcs_data[2 + i + 0] = lo;
                   1875:                        dbcs_data[2 + i + 1] = hi;
                   1876:                        if(lo == 0 && hi == 0) {
                   1877:                                dbcs_data[0] = i + 2;
                   1878:                                break;
                   1879:                        }
                   1880:                }
                   1881:        } else {
                   1882:                dbcs_data[0] = 2;       // ???
                   1883:        }
                   1884:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   1885: }
                   1886: 
1.1.1.17  root     1887: void msdos_dbcs_table_finish()
                   1888: {
                   1889:        if(active_code_page != system_code_page) {
                   1890:                _setmbcp(system_code_page);
                   1891:        }
                   1892: }
                   1893: 
                   1894: void msdos_nls_tables_init()
1.1       root     1895: {
                   1896:        system_code_page = active_code_page = _getmbcp();
1.1.1.17  root     1897:        msdos_upper_table_update();
1.1.1.23  root     1898:        msdos_lower_table_update();
1.1.1.17  root     1899:        msdos_filename_terminator_table_init();
                   1900:        msdos_filename_upper_table_init();
                   1901:        msdos_collating_table_update();
1.1       root     1902:        msdos_dbcs_table_update();
                   1903: }
                   1904: 
1.1.1.17  root     1905: void msdos_nls_tables_update()
1.1       root     1906: {
1.1.1.17  root     1907:        msdos_dbcs_table_update();
                   1908:        msdos_upper_table_update();
1.1.1.23  root     1909:        msdos_lower_table_update();
                   1910: //     msdos_collating_table_update();
1.1       root     1911: }
                   1912: 
                   1913: int msdos_lead_byte_check(UINT8 code)
                   1914: {
                   1915:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   1916:        
                   1917:        for(int i = 0;; i += 2) {
                   1918:                UINT8 lo = dbcs_table[i + 0];
                   1919:                UINT8 hi = dbcs_table[i + 1];
                   1920:                if(lo == 0 && hi == 0) {
                   1921:                        break;
                   1922:                }
                   1923:                if(lo <= code && code <= hi) {
                   1924:                        return(1);
                   1925:                }
                   1926:        }
                   1927:        return(0);
                   1928: }
                   1929: 
1.1.1.20  root     1930: int msdos_ctrl_code_check(UINT8 code)
                   1931: {
1.1.1.22  root     1932:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     1933: }
                   1934: 
1.1       root     1935: // file control
                   1936: 
1.1.1.14  root     1937: char *msdos_remove_double_quote(char *path)
                   1938: {
                   1939:        static char tmp[MAX_PATH];
                   1940:        
                   1941:        memset(tmp, 0, sizeof(tmp));
                   1942:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   1943:                memcpy(tmp, path + 1, strlen(path) - 2);
                   1944:        } else {
                   1945:                strcpy(tmp, path);
                   1946:        }
                   1947:        return(tmp);
                   1948: }
                   1949: 
                   1950: char *msdos_combine_path(char *dir, const char *file)
                   1951: {
                   1952:        static char tmp[MAX_PATH];
                   1953:        char *tmp_dir = msdos_remove_double_quote(dir);
                   1954:        
                   1955:        if(strlen(tmp_dir) == 0) {
                   1956:                strcpy(tmp, file);
                   1957:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   1958:                sprintf(tmp, "%s%s", tmp_dir, file);
                   1959:        } else {
                   1960:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   1961:        }
                   1962:        return(tmp);
                   1963: }
                   1964: 
1.1       root     1965: char *msdos_trimmed_path(char *path, int lfn)
                   1966: {
                   1967:        static char tmp[MAX_PATH];
                   1968:        
                   1969:        if(lfn) {
                   1970:                strcpy(tmp, path);
                   1971:        } else {
                   1972:                // remove space in the path
                   1973:                char *src = path, *dst = tmp;
                   1974:                
                   1975:                while(*src != '\0') {
                   1976:                        if(msdos_lead_byte_check(*src)) {
                   1977:                                *dst++ = *src++;
                   1978:                                *dst++ = *src++;
                   1979:                        } else if(*src != ' ') {
                   1980:                                *dst++ = *src++;
                   1981:                        } else {
                   1982:                                src++;  // skip space
                   1983:                        }
                   1984:                }
                   1985:                *dst = '\0';
                   1986:        }
1.1.1.14  root     1987:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   1988:                // redirect C:\COMMAND.COM to comspec_path
                   1989:                strcpy(tmp, comspec_path);
                   1990:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   1991:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   1992:                static int root_drive_protected = -1;
                   1993:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   1994:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   1995:                
                   1996:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   1997:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   1998:                        strcpy(name, name_temp);
                   1999:                        name_temp[0] = '\0';
                   2000:                        
                   2001:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   2002:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   2003:                                if(root_drive_protected == -1) {
                   2004:                                        FILE *fp = NULL;
                   2005:                                        
                   2006:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   2007:                                        root_drive_protected = 1;
                   2008:                                        try {
                   2009:                                                if((fp = fopen(temp, "w")) != NULL) {
                   2010:                                                        if(fprintf(fp, "TEST") == 4) {
                   2011:                                                                root_drive_protected = 0;
                   2012:                                                        }
                   2013:                                                }
                   2014:                                        } catch(...) {
                   2015:                                        }
                   2016:                                        if(fp != NULL) {
                   2017:                                                fclose(fp);
                   2018:                                        }
                   2019:                                        if(_access(temp, 0) == 0) {
                   2020:                                                remove(temp);
                   2021:                                        }
                   2022:                                }
                   2023:                                if(root_drive_protected == 1) {
                   2024:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   2025:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   2026:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   2027:                                        }
                   2028:                                }
                   2029:                        }
                   2030:                }
                   2031:        }
1.1       root     2032:        return(tmp);
                   2033: }
                   2034: 
1.1.1.28  root     2035: char *msdos_get_multiple_short_path(char *src)
                   2036: {
                   2037:        // LONGPATH;LONGPATH;LONGPATH to SHORTPATH;SHORTPATH;SHORTPATH
                   2038:        static char env_path[ENV_SIZE];
                   2039:        char tmp[ENV_SIZE], *token;
                   2040:        
                   2041:        memset(env_path, 0, sizeof(env_path));
                   2042:        strcpy(tmp, src);
                   2043:        token = my_strtok(tmp, ";");
                   2044:        
                   2045:        while(token != NULL) {
                   2046:                if(token[0] != '\0') {
                   2047:                        char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
                   2048:                        if(strlen(path) != 0) {
                   2049:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
                   2050:                                        strcat(env_path, path);
                   2051:                                } else {
                   2052:                                        my_strupr(short_path);
                   2053:                                        strcat(env_path, short_path);
                   2054:                                }
                   2055:                                strcat(env_path, ";");
                   2056:                        }
                   2057:                }
                   2058:                token = my_strtok(NULL, ";");
                   2059:        }
                   2060:        int len = strlen(env_path);
                   2061:        if(len != 0 && env_path[len - 1] == ';') {
                   2062:                env_path[len - 1] = '\0';
                   2063:        }
                   2064:        return(env_path);
                   2065: }
                   2066: 
1.1       root     2067: bool match(char *text, char *pattern)
                   2068: {
1.1.1.24  root     2069:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     2070:        switch(*pattern) {
1.1       root     2071:        case '\0':
                   2072:                return !*text;
                   2073:        case '*':
1.1.1.14  root     2074:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     2075:        case '?':
                   2076:                return *text && match(text + 1, pattern + 1);
                   2077:        default:
                   2078:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   2079:        }
                   2080: }
                   2081: 
                   2082: bool msdos_match_volume_label(char *path, char *volume)
                   2083: {
                   2084:        char *p;
                   2085:        
1.1.1.14  root     2086:        if(!*volume) {
                   2087:                return false;
                   2088:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     2089:                return msdos_match_volume_label(p + 1, volume);
                   2090:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   2091:                return msdos_match_volume_label(p + 1, volume);
                   2092:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     2093:                char tmp[MAX_PATH];
                   2094:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   2095:                return match(volume, tmp);
1.1       root     2096:        } else {
                   2097:                return match(volume, path);
                   2098:        }
                   2099: }
                   2100: 
                   2101: char *msdos_fcb_path(fcb_t *fcb)
                   2102: {
                   2103:        static char tmp[MAX_PATH];
                   2104:        char name[9], ext[4];
                   2105:        
                   2106:        memset(name, 0, sizeof(name));
                   2107:        memcpy(name, fcb->file_name, 8);
                   2108:        strcpy(name, msdos_trimmed_path(name, 0));
                   2109:        
                   2110:        memset(ext, 0, sizeof(ext));
                   2111:        memcpy(ext, fcb->file_name + 8, 3);
                   2112:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   2113:        
                   2114:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   2115:                strcpy(name, "*");
                   2116:        }
                   2117:        if(ext[0] == '\0') {
                   2118:                strcpy(tmp, name);
                   2119:        } else {
                   2120:                if(strcmp(ext, "???") == 0) {
                   2121:                        strcpy(ext, "*");
                   2122:                }
                   2123:                sprintf(tmp, "%s.%s", name, ext);
                   2124:        }
                   2125:        return(tmp);
                   2126: }
                   2127: 
                   2128: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   2129: {
                   2130:        char *ext = my_strchr(path, '.');
                   2131:        
                   2132:        memset(fcb->file_name, 0x20, 8 + 3);
                   2133:        if(ext != NULL && path[0] != '.') {
                   2134:                *ext = '\0';
                   2135:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   2136:        }
                   2137:        memcpy(fcb->file_name, path, strlen(path));
                   2138: }
                   2139: 
                   2140: char *msdos_short_path(char *path)
                   2141: {
                   2142:        static char tmp[MAX_PATH];
                   2143:        
1.1.1.24  root     2144:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   2145:                strcpy(tmp, path);
                   2146:        }
1.1       root     2147:        my_strupr(tmp);
                   2148:        return(tmp);
                   2149: }
                   2150: 
1.1.1.13  root     2151: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   2152: {
                   2153:        static char tmp[MAX_PATH];
                   2154: 
1.1.1.14  root     2155:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     2156:                strcpy(tmp, fd->cAlternateFileName);
                   2157:        } else {
                   2158:                strcpy(tmp, fd->cFileName);
                   2159:        }
                   2160:        my_strupr(tmp);
                   2161:        return(tmp);
                   2162: }
                   2163: 
1.1       root     2164: char *msdos_short_full_path(char *path)
                   2165: {
                   2166:        static char tmp[MAX_PATH];
                   2167:        char full[MAX_PATH], *name;
                   2168:        
1.1.1.14  root     2169:        // Full works with non-existent files, but Short does not
1.1       root     2170:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     2171:        *tmp = '\0';
                   2172:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   2173:                name[-1] = '\0';
                   2174:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   2175:                if(len == 0) {
                   2176:                        strcpy(tmp, full);
                   2177:                } else {
                   2178:                        tmp[len++] = '\\';
                   2179:                        strcpy(tmp + len, name);
                   2180:                }
                   2181:        }
1.1       root     2182:        my_strupr(tmp);
                   2183:        return(tmp);
                   2184: }
                   2185: 
                   2186: char *msdos_short_full_dir(char *path)
                   2187: {
                   2188:        static char tmp[MAX_PATH];
                   2189:        char full[MAX_PATH], *name;
                   2190:        
                   2191:        GetFullPathName(path, MAX_PATH, full, &name);
                   2192:        name[-1] = '\0';
1.1.1.24  root     2193:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   2194:                strcpy(tmp, full);
                   2195:        }
1.1       root     2196:        my_strupr(tmp);
                   2197:        return(tmp);
                   2198: }
                   2199: 
                   2200: char *msdos_local_file_path(char *path, int lfn)
                   2201: {
                   2202:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     2203: #if 0
                   2204:        // I have forgotten the reason of this routine... :-(
1.1       root     2205:        if(_access(trimmed, 0) != 0) {
                   2206:                process_t *process = msdos_process_info_get(current_psp);
                   2207:                static char tmp[MAX_PATH];
                   2208:                
                   2209:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   2210:                if(_access(tmp, 0) == 0) {
                   2211:                        return(tmp);
                   2212:                }
                   2213:        }
1.1.1.14  root     2214: #endif
1.1       root     2215:        return(trimmed);
                   2216: }
                   2217: 
1.1.1.29  root     2218: bool msdos_is_device_path(char *path)
1.1.1.11  root     2219: {
                   2220:        char full[MAX_PATH], *name;
                   2221:        
1.1.1.24  root     2222:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     2223:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   2224:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   2225:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   2226:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   2227:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   2228:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   2229:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   2230:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   2231:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   2232:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   2233:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   2234:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   2235:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   2236:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   2237:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   2238:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   2239:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   2240:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   2241:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   2242:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   2243:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   2244:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   2245:                        return(true);
                   2246:                } else if(name != NULL) {
                   2247:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   2248:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     2249:                           _stricmp(name, "EMMXXXX0") == 0 ||
                   2250:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     2251:                                return(true);
                   2252:                        }
                   2253:                }
1.1.1.24  root     2254:        }
                   2255:        return(false);
1.1.1.11  root     2256: }
                   2257: 
1.1.1.29  root     2258: bool msdos_is_con_path(char *path)
1.1.1.8   root     2259: {
1.1.1.14  root     2260:        char full[MAX_PATH], *name;
1.1.1.8   root     2261:        
1.1.1.24  root     2262:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     2263:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     2264:        }
                   2265:        return(false);
                   2266: }
                   2267: 
1.1.1.29  root     2268: int msdos_is_comm_path(char *path)
1.1.1.24  root     2269: {
                   2270:        char full[MAX_PATH], *name;
                   2271:        
                   2272:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     2273:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   2274:                        return(1);
                   2275:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   2276:                        return(2);
                   2277:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   2278:                        return(3);
                   2279:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   2280:                        return(4);
1.1.1.24  root     2281:                }
                   2282:        }
1.1.1.29  root     2283:        return(0);
                   2284: }
                   2285: 
1.1.1.30  root     2286: int msdos_is_prn_path(char *path)
                   2287: {
                   2288:        char full[MAX_PATH], *name;
                   2289:        
                   2290:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2291:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   2292:                        return(1);
                   2293:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   2294:                        return(1);
                   2295:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   2296:                        return(2);
                   2297:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   2298:                        return(3);
                   2299:                }
                   2300:        }
                   2301:        return(0);
                   2302: }
                   2303: 
1.1.1.29  root     2304: char *msdos_create_comm_path(char *path, int port)
                   2305: {
                   2306:        static char tmp[MAX_PATH];
                   2307:        char *p = NULL;
                   2308:        
                   2309:        sprintf(tmp, "COM%d", port);
                   2310:        if((p = strchr(path, ':')) != NULL) {
                   2311:                strcat(tmp, p);
                   2312:        }
                   2313:        return(tmp);
1.1.1.24  root     2314: }
                   2315: 
                   2316: bool msdos_is_existing_file(char *path)
                   2317: {
                   2318:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   2319:        WIN32_FIND_DATA FindData;
                   2320:        HANDLE hFind;
                   2321:        
                   2322:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   2323:                FindClose(hFind);
                   2324:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   2325:        }
                   2326:        return(false);
1.1.1.8   root     2327: }
                   2328: 
1.1.1.9   root     2329: char *msdos_search_command_com(char *command_path, char *env_path)
                   2330: {
                   2331:        static char tmp[MAX_PATH];
1.1.1.28  root     2332:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     2333:        
1.1.1.28  root     2334:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     2335:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   2336:                sprintf(file_name, "COMMAND.COM");
                   2337:                if(_access(tmp, 0) == 0) {
                   2338:                        return(tmp);
                   2339:                }
                   2340:        }
1.1.1.28  root     2341:        
                   2342:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     2343:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   2344:                sprintf(file_name, "COMMAND.COM");
                   2345:                if(_access(tmp, 0) == 0) {
                   2346:                        return(tmp);
                   2347:                }
                   2348:        }
1.1.1.28  root     2349:        
                   2350:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     2351:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   2352:                if(_access(tmp, 0) == 0) {
                   2353:                        return(tmp);
                   2354:                }
                   2355:        }
1.1.1.28  root     2356:        
                   2357:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   2358:        strcpy(path, env_path);
                   2359:        char *token = my_strtok(path, ";");
1.1.1.9   root     2360:        while(token != NULL) {
1.1.1.14  root     2361:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     2362:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   2363:                        if(_access(tmp, 0) == 0) {
                   2364:                                return(tmp);
                   2365:                        }
                   2366:                }
                   2367:                token = my_strtok(NULL, ";");
                   2368:        }
                   2369:        return(NULL);
                   2370: }
                   2371: 
1.1.1.14  root     2372: int msdos_drive_number(const char *path)
1.1       root     2373: {
                   2374:        char tmp[MAX_PATH], *name;
                   2375:        
                   2376:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   2377:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   2378:                return(tmp[0] - 'a');
                   2379:        } else {
                   2380:                return(tmp[0] - 'A');
                   2381:        }
                   2382: }
                   2383: 
                   2384: char *msdos_volume_label(char *path)
                   2385: {
                   2386:        static char tmp[MAX_PATH];
                   2387:        char volume[] = "A:\\";
                   2388:        
                   2389:        if(path[1] == ':') {
                   2390:                volume[0] = path[0];
                   2391:        } else {
                   2392:                volume[0] = 'A' + _getdrive() - 1;
                   2393:        }
                   2394:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   2395:                memset(tmp, 0, sizeof(tmp));
                   2396:        }
                   2397:        return(tmp);
                   2398: }
                   2399: 
                   2400: char *msdos_short_volume_label(char *label)
                   2401: {
                   2402:        static char tmp[(8 + 1 + 3) + 1];
                   2403:        char *src = label;
                   2404:        int remain = strlen(label);
                   2405:        char *dst_n = tmp;
                   2406:        char *dst_e = tmp + 9;
                   2407:        
                   2408:        strcpy(tmp, "        .   ");
                   2409:        for(int i = 0; i < 8 && remain > 0; i++) {
                   2410:                if(msdos_lead_byte_check(*src)) {
                   2411:                        if(++i == 8) {
                   2412:                                break;
                   2413:                        }
                   2414:                        *dst_n++ = *src++;
                   2415:                        remain--;
                   2416:                }
                   2417:                *dst_n++ = *src++;
                   2418:                remain--;
                   2419:        }
                   2420:        if(remain > 0) {
                   2421:                for(int i = 0; i < 3 && remain > 0; i++) {
                   2422:                        if(msdos_lead_byte_check(*src)) {
                   2423:                                if(++i == 3) {
                   2424:                                        break;
                   2425:                                }
                   2426:                                *dst_e++ = *src++;
                   2427:                                remain--;
                   2428:                        }
                   2429:                        *dst_e++ = *src++;
                   2430:                        remain--;
                   2431:                }
                   2432:                *dst_e = '\0';
                   2433:        } else {
                   2434:                *dst_n = '\0';
                   2435:        }
                   2436:        my_strupr(tmp);
                   2437:        return(tmp);
                   2438: }
                   2439: 
1.1.1.13  root     2440: errno_t msdos_maperr(unsigned long oserrno)
                   2441: {
                   2442:        _doserrno = oserrno;
1.1.1.14  root     2443:        switch(oserrno) {
1.1.1.13  root     2444:        case ERROR_FILE_NOT_FOUND:         // 2
                   2445:        case ERROR_PATH_NOT_FOUND:         // 3
                   2446:        case ERROR_INVALID_DRIVE:          // 15
                   2447:        case ERROR_NO_MORE_FILES:          // 18
                   2448:        case ERROR_BAD_NETPATH:            // 53
                   2449:        case ERROR_BAD_NET_NAME:           // 67
                   2450:        case ERROR_BAD_PATHNAME:           // 161
                   2451:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   2452:                return ENOENT;
                   2453:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   2454:                return EMFILE;
                   2455:        case ERROR_ACCESS_DENIED:          // 5
                   2456:        case ERROR_CURRENT_DIRECTORY:      // 16
                   2457:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   2458:        case ERROR_CANNOT_MAKE:            // 82
                   2459:        case ERROR_FAIL_I24:               // 83
                   2460:        case ERROR_DRIVE_LOCKED:           // 108
                   2461:        case ERROR_SEEK_ON_DEVICE:         // 132
                   2462:        case ERROR_NOT_LOCKED:             // 158
                   2463:        case ERROR_LOCK_FAILED:            // 167
                   2464:                return EACCES;
                   2465:        case ERROR_INVALID_HANDLE:         // 6
                   2466:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   2467:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   2468:                return EBADF;
                   2469:        case ERROR_ARENA_TRASHED:          // 7
                   2470:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   2471:        case ERROR_INVALID_BLOCK:          // 9
                   2472:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   2473:                return ENOMEM;
                   2474:        case ERROR_BAD_ENVIRONMENT:        // 10
                   2475:                return E2BIG;
                   2476:        case ERROR_BAD_FORMAT:             // 11
                   2477:                return ENOEXEC;
                   2478:        case ERROR_NOT_SAME_DEVICE:        // 17
                   2479:                return EXDEV;
                   2480:        case ERROR_FILE_EXISTS:            // 80
                   2481:        case ERROR_ALREADY_EXISTS:         // 183
                   2482:                return EEXIST;
                   2483:        case ERROR_NO_PROC_SLOTS:          // 89
                   2484:        case ERROR_MAX_THRDS_REACHED:      // 164
                   2485:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   2486:                return EAGAIN;
                   2487:        case ERROR_BROKEN_PIPE:            // 109
                   2488:                return EPIPE;
                   2489:        case ERROR_DISK_FULL:              // 112
                   2490:                return ENOSPC;
                   2491:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   2492:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   2493:                return ECHILD;
                   2494:        case ERROR_DIR_NOT_EMPTY:          // 145
                   2495:                return ENOTEMPTY;
                   2496:        }
1.1.1.14  root     2497:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     2498:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   2499:                return EACCES;
                   2500:        }
1.1.1.14  root     2501:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     2502:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   2503:                return ENOEXEC;
                   2504:        }
                   2505:        return EINVAL;
                   2506: }
                   2507: 
                   2508: int msdos_open(const char *filename, int oflag)
                   2509: {
1.1.1.14  root     2510:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     2511:                return _open(filename, oflag);
                   2512:        }
1.1.1.14  root     2513:        
                   2514:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     2515:        DWORD disposition;
1.1.1.14  root     2516:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   2517:        default:
1.1.1.13  root     2518:        case _O_EXCL:
                   2519:                disposition = OPEN_EXISTING;
                   2520:                break;
                   2521:        case _O_CREAT:
                   2522:                disposition = OPEN_ALWAYS;
                   2523:                break;
                   2524:        case _O_CREAT | _O_EXCL:
                   2525:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   2526:                disposition = CREATE_NEW;
                   2527:                break;
                   2528:        case _O_TRUNC:
                   2529:        case _O_TRUNC | _O_EXCL:
                   2530:                disposition = TRUNCATE_EXISTING;
                   2531:                break;
                   2532:        case _O_CREAT | _O_TRUNC:
                   2533:                disposition = CREATE_ALWAYS;
                   2534:                break;
                   2535:        }
1.1.1.14  root     2536:        
1.1.1.13  root     2537:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   2538:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2539:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2540:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2541:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   2542:                // Retry without FILE_WRITE_ATTRIBUTES.
                   2543:                h = CreateFile(filename, GENERIC_READ,
                   2544:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2545:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2546:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2547:                        errno = msdos_maperr(GetLastError());
                   2548:                        return -1;
                   2549:                }
                   2550:        }
1.1.1.14  root     2551:        
1.1.1.13  root     2552:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     2553:        if(fd == -1) {
1.1.1.13  root     2554:                CloseHandle(h);
                   2555:        }
                   2556:        return fd;
                   2557: }
                   2558: 
1.1.1.14  root     2559: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
1.1       root     2560: {
                   2561:        static int id = 0;
                   2562:        char full[MAX_PATH], *name;
                   2563:        
                   2564:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2565:                strcpy(file_handler[fd].path, full);
                   2566:        } else {
                   2567:                strcpy(file_handler[fd].path, path);
                   2568:        }
1.1.1.14  root     2569:        // isatty makes no distinction between CON & NUL
                   2570:        // GetFileSize fails on CON, succeeds on NUL
                   2571:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   2572:                info = 0x8084;
                   2573:                atty = 0;
                   2574:        } else if(!atty && info == 0x80d3) {
                   2575:                info = msdos_drive_number(".");
                   2576:        }
1.1       root     2577:        file_handler[fd].valid = 1;
                   2578:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
                   2579:        file_handler[fd].atty = atty;
                   2580:        file_handler[fd].mode = mode;
                   2581:        file_handler[fd].info = info;
                   2582:        file_handler[fd].psp = psp_seg;
1.1.1.21  root     2583:        
                   2584:        // init system file table
                   2585:        if(fd < 20) {
                   2586:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   2587:                
                   2588:                memset(sft, 0, 0x3b);
                   2589:                
                   2590:                *(UINT16 *)(sft + 0x00) = 1;
                   2591:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   2592:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   2593:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   2594:                
                   2595:                if(!(file_handler[fd].info & 0x80)) {
                   2596:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   2597:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   2598:                        
                   2599:                        FILETIME time, local;
                   2600:                        HANDLE hHandle;
                   2601:                        WORD dos_date = 0, dos_time = 0;
                   2602:                        DWORD file_size = 0;
                   2603:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   2604:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   2605:                                        FileTimeToLocalFileTime(&time, &local);
                   2606:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   2607:                                }
                   2608:                                file_size = GetFileSize(hHandle, NULL);
                   2609:                        }
                   2610:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   2611:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   2612:                        *(UINT32 *)(sft + 0x11) = file_size;
                   2613:                }
                   2614:                
                   2615:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   2616:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   2617:                my_strupr(fname);
                   2618:                my_strupr(ext);
                   2619:                memset(sft + 0x20, 0x20, 11);
                   2620:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   2621:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   2622:                
                   2623:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   2624:        }
1.1       root     2625: }
                   2626: 
                   2627: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   2628: {
                   2629:        strcpy(file_handler[dst].path, file_handler[src].path);
                   2630:        file_handler[dst].valid = 1;
                   2631:        file_handler[dst].id = file_handler[src].id;
                   2632:        file_handler[dst].atty = file_handler[src].atty;
                   2633:        file_handler[dst].mode = file_handler[src].mode;
                   2634:        file_handler[dst].info = file_handler[src].info;
                   2635:        file_handler[dst].psp = psp_seg;
                   2636: }
                   2637: 
1.1.1.20  root     2638: void msdos_file_handler_close(int fd)
1.1       root     2639: {
                   2640:        file_handler[fd].valid = 0;
1.1.1.21  root     2641:        
                   2642:        if(fd < 20) {
                   2643:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   2644:        }
1.1       root     2645: }
                   2646: 
1.1.1.14  root     2647: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     2648: {
1.1.1.14  root     2649:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   2650:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   2651:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     2652: }
                   2653: 
                   2654: // find file
                   2655: 
                   2656: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   2657: {
                   2658:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2659:                return(0);      // search directory only !!!
                   2660:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   2661:                return(0);
                   2662:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   2663:                return(0);
                   2664:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2665:                return(0);
                   2666:        } else if((attribute & required_mask) != required_mask) {
                   2667:                return(0);
                   2668:        } else {
                   2669:                return(1);
                   2670:        }
                   2671: }
                   2672: 
1.1.1.13  root     2673: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   2674: {
1.1.1.14  root     2675:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     2676:                return 1;
                   2677:        }
                   2678:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     2679:        if(len > 12) {
1.1.1.13  root     2680:                return 0;
                   2681:        }
                   2682:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     2683:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.13  root     2684:                return 0;
                   2685:        }
                   2686:        return 1;
                   2687: }
                   2688: 
1.1       root     2689: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   2690: {
                   2691:        FILETIME local;
                   2692:        
                   2693:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   2694:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   2695:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   2696:        
                   2697:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   2698:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   2699:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   2700:        
                   2701:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   2702:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   2703:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   2704: }
                   2705: 
                   2706: // i/o
                   2707: 
                   2708: void msdos_stdio_reopen()
                   2709: {
                   2710:        if(!file_handler[0].valid) {
                   2711:                _dup2(DUP_STDIN, 0);
                   2712:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   2713:        }
                   2714:        if(!file_handler[1].valid) {
                   2715:                _dup2(DUP_STDOUT, 1);
                   2716:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   2717:        }
                   2718:        if(!file_handler[2].valid) {
                   2719:                _dup2(DUP_STDERR, 2);
                   2720:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   2721:        }
1.1.1.21  root     2722:        if(!file_handler[3].valid) {
                   2723:                _dup2(DUP_STDAUX, 3);
                   2724:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   2725:        }
                   2726:        if(!file_handler[4].valid) {
                   2727:                _dup2(DUP_STDPRN, 4);
                   2728:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   2729:        }
                   2730:        for(int i = 0; i < 5; i++) {
                   2731:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   2732:                        msdos_psp_set_file_table(i, i, current_psp);
                   2733:                }
                   2734:        }
1.1       root     2735: }
                   2736: 
                   2737: int msdos_kbhit()
                   2738: {
                   2739:        msdos_stdio_reopen();
                   2740:        
1.1.1.20  root     2741:        process_t *process = msdos_process_info_get(current_psp);
                   2742:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2743:        
                   2744:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2745:                // stdin is redirected to file
1.1.1.20  root     2746:                return(eof(fd) == 0);
1.1       root     2747:        }
                   2748:        
                   2749:        // check keyboard status
1.1.1.5   root     2750:        if(key_buf_char->count() != 0 || key_code != 0) {
1.1       root     2751:                return(1);
                   2752:        } else {
                   2753:                return(_kbhit());
                   2754:        }
                   2755: }
                   2756: 
                   2757: int msdos_getch_ex(int echo)
                   2758: {
                   2759:        static char prev = 0;
                   2760:        
                   2761:        msdos_stdio_reopen();
                   2762:        
1.1.1.20  root     2763:        process_t *process = msdos_process_info_get(current_psp);
                   2764:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2765:        
                   2766:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2767:                // stdin is redirected to file
                   2768: retry:
                   2769:                char data;
1.1.1.20  root     2770:                if(_read(fd, &data, 1) == 1) {
1.1       root     2771:                        char tmp = data;
                   2772:                        if(data == 0x0a) {
                   2773:                                if(prev == 0x0d) {
                   2774:                                        goto retry; // CRLF -> skip LF
                   2775:                                } else {
                   2776:                                        data = 0x0d; // LF only -> CR
                   2777:                                }
                   2778:                        }
                   2779:                        prev = tmp;
                   2780:                        return(data);
                   2781:                }
                   2782:                return(EOF);
                   2783:        }
                   2784:        
                   2785:        // input from console
1.1.1.5   root     2786:        int key_char, key_scan;
                   2787:        if(key_code != 0) {
                   2788:                key_char = (key_code >> 0) & 0xff;
                   2789:                key_scan = (key_code >> 8) & 0xff;
                   2790:                key_code >>= 16;
                   2791:        } else {
1.1.1.26  root     2792:                while(key_buf_char->count() == 0 && !m_halted && !ctrl_c_pressed) {
1.1.1.23  root     2793:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   2794:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   2795:                                if(_kbhit()) {
                   2796:                                        key_buf_char->write(_getch());
                   2797:                                        key_buf_scan->write(0);
                   2798:                                } else {
                   2799:                                        Sleep(10);
                   2800:                                }
                   2801:                        } else {
                   2802:                                if(!update_key_buffer()) {
                   2803:                                        Sleep(10);
                   2804:                                }
1.1.1.14  root     2805:                        }
                   2806:                }
                   2807:                if(m_halted) {
1.1.1.26  root     2808:                        // ctrl-break pressed - insert CR to terminate input loops
1.1.1.14  root     2809:                        key_char = 0x0d;
                   2810:                        key_scan = 0;
1.1.1.26  root     2811:                } else if(ctrl_c_pressed) {
                   2812:                        // ctrl-c pressed
                   2813:                        key_char = 0x03;
                   2814:                        key_scan = 0;
1.1.1.14  root     2815:                } else {
                   2816:                        key_char = key_buf_char->read();
                   2817:                        key_scan = key_buf_scan->read();
1.1.1.5   root     2818:                }
1.1       root     2819:        }
                   2820:        if(echo && key_char) {
                   2821:                msdos_putch(key_char);
                   2822:        }
                   2823:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   2824: }
                   2825: 
                   2826: inline int msdos_getch()
                   2827: {
                   2828:        return(msdos_getch_ex(0));
                   2829: }
                   2830: 
                   2831: inline int msdos_getche()
                   2832: {
                   2833:        return(msdos_getch_ex(1));
                   2834: }
                   2835: 
                   2836: int msdos_write(int fd, const void *buffer, unsigned int count)
                   2837: {
                   2838:        static int is_cr = 0;
                   2839:        
                   2840:        if(fd == 1 && !file_handler[1].atty) {
                   2841:                // CR+LF -> LF
                   2842:                UINT8 *buf = (UINT8 *)buffer;
                   2843:                for(unsigned int i = 0; i < count; i++) {
                   2844:                        UINT8 data = buf[i];
                   2845:                        if(is_cr) {
                   2846:                                if(data != 0x0a) {
                   2847:                                        UINT8 tmp = 0x0d;
                   2848:                                        _write(1, &tmp, 1);
                   2849:                                }
                   2850:                                _write(1, &data, 1);
                   2851:                                is_cr = 0;
                   2852:                        } else if(data == 0x0d) {
                   2853:                                is_cr = 1;
                   2854:                        } else {
                   2855:                                _write(1, &data, 1);
                   2856:                        }
                   2857:                }
                   2858:                return(count);
                   2859:        }
1.1.1.14  root     2860:        vram_flush();
1.1       root     2861:        return(_write(fd, buffer, count));
                   2862: }
                   2863: 
                   2864: void msdos_putch(UINT8 data)
                   2865: {
                   2866:        static int p = 0;
                   2867:        static int is_kanji = 0;
                   2868:        static int is_esc = 0;
                   2869:        static int stored_x;
                   2870:        static int stored_y;
                   2871:        static WORD stored_a;
1.1.1.20  root     2872:        static char tmp[64], out[64];
1.1       root     2873:        
                   2874:        msdos_stdio_reopen();
                   2875:        
1.1.1.20  root     2876:        process_t *process = msdos_process_info_get(current_psp);
                   2877:        int fd = msdos_psp_get_file_table(1, current_psp);
                   2878:        
                   2879:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2880:                // stdout is redirected to file
1.1.1.20  root     2881:                msdos_write(fd, &data, 1);
1.1       root     2882:                return;
                   2883:        }
1.1.1.23  root     2884:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2885:        
                   2886:        // output to console
                   2887:        tmp[p++] = data;
                   2888:        
1.1.1.14  root     2889:        vram_flush();
                   2890:        
1.1       root     2891:        if(is_kanji) {
                   2892:                // kanji character
                   2893:                is_kanji = 0;
                   2894:        } else if(is_esc) {
                   2895:                // escape sequense
                   2896:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   2897:                        p = is_esc = 0;
                   2898:                } else if(tmp[1] == '=' && p == 4) {
                   2899:                        COORD co;
                   2900:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     2901:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     2902:                        SetConsoleCursorPosition(hStdout, co);
                   2903:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     2904:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     2905:                        cursor_moved = false;
                   2906:                        p = is_esc = 0;
                   2907:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   2908:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2909:                        COORD co;
                   2910:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2911:                        co.X = csbi.dwCursorPosition.X;
                   2912:                        co.Y = csbi.dwCursorPosition.Y;
                   2913:                        WORD wAttributes = csbi.wAttributes;
                   2914:                        
                   2915:                        if(tmp[1] == 'D') {
                   2916:                                co.Y++;
                   2917:                        } else if(tmp[1] == 'E') {
                   2918:                                co.X = 0;
                   2919:                                co.Y++;
                   2920:                        } else if(tmp[1] == 'M') {
                   2921:                                co.Y--;
                   2922:                        } else if(tmp[1] == '*') {
                   2923:                                SMALL_RECT rect;
1.1.1.14  root     2924:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2925:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2926:                                co.X = 0;
                   2927:                                co.Y = csbi.srWindow.Top;
1.1       root     2928:                        } else if(tmp[1] == '[') {
                   2929:                                int param[256], params = 0;
                   2930:                                memset(param, 0, sizeof(param));
                   2931:                                for(int i = 2; i < p; i++) {
                   2932:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   2933:                                                param[params] *= 10;
                   2934:                                                param[params] += tmp[i] - '0';
                   2935:                                        } else {
                   2936:                                                params++;
                   2937:                                        }
                   2938:                                }
                   2939:                                if(data == 'A') {
1.1.1.14  root     2940:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     2941:                                } else if(data == 'B') {
1.1.1.14  root     2942:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     2943:                                } else if(data == 'C') {
1.1.1.14  root     2944:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     2945:                                } else if(data == 'D') {
1.1.1.14  root     2946:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     2947:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     2948:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   2949:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     2950:                                } else if(data == 'J') {
                   2951:                                        SMALL_RECT rect;
1.1.1.14  root     2952:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2953:                                        if(param[0] == 0) {
                   2954:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2955:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2956:                                                if(co.Y < csbi.srWindow.Bottom) {
                   2957:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2958:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2959:                                                }
                   2960:                                        } else if(param[0] == 1) {
1.1.1.14  root     2961:                                                if(co.Y > csbi.srWindow.Top) {
                   2962:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   2963:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2964:                                                }
                   2965:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2966:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2967:                                        } else if(param[0] == 2) {
1.1.1.14  root     2968:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2969:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2970:                                                co.X = co.Y = 0;
                   2971:                                        }
                   2972:                                } else if(data == 'K') {
                   2973:                                        SMALL_RECT rect;
1.1.1.14  root     2974:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2975:                                        if(param[0] == 0) {
                   2976:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2977:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2978:                                        } else if(param[0] == 1) {
                   2979:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2980:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2981:                                        } else if(param[0] == 2) {
                   2982:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2983:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2984:                                        }
                   2985:                                } else if(data == 'L') {
                   2986:                                        SMALL_RECT rect;
1.1.1.14  root     2987:                                        if(params == 0) {
                   2988:                                                param[0] = 1;
1.1       root     2989:                                        }
1.1.1.14  root     2990:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2991:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2992:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2993:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2994:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2995:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     2996:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2997:                                        co.X = 0;
                   2998:                                } else if(data == 'M') {
                   2999:                                        SMALL_RECT rect;
1.1.1.14  root     3000:                                        if(params == 0) {
                   3001:                                                param[0] = 1;
                   3002:                                        }
                   3003:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   3004:                                                clear_scr_buffer(csbi.wAttributes);
                   3005:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   3006:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     3007:                                        } else {
1.1.1.14  root     3008:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   3009:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3010:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   3011:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3012:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     3013:                                        }
                   3014:                                        co.X = 0;
                   3015:                                } else if(data == 'h') {
                   3016:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   3017:                                                CONSOLE_CURSOR_INFO cur;
                   3018:                                                GetConsoleCursorInfo(hStdout, &cur);
                   3019:                                                if(cur.bVisible) {
                   3020:                                                        cur.bVisible = FALSE;
1.1.1.14  root     3021: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     3022:                                                }
                   3023:                                        }
                   3024:                                } else if(data == 'l') {
                   3025:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   3026:                                                CONSOLE_CURSOR_INFO cur;
                   3027:                                                GetConsoleCursorInfo(hStdout, &cur);
                   3028:                                                if(!cur.bVisible) {
                   3029:                                                        cur.bVisible = TRUE;
1.1.1.14  root     3030: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     3031:                                                }
                   3032:                                        }
                   3033:                                } else if(data == 'm') {
                   3034:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   3035:                                        int reverse = 0, hidden = 0;
                   3036:                                        for(int i = 0; i < params; i++) {
                   3037:                                                if(param[i] == 1) {
                   3038:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   3039:                                                } else if(param[i] == 4) {
                   3040:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   3041:                                                } else if(param[i] == 7) {
                   3042:                                                        reverse = 1;
                   3043:                                                } else if(param[i] == 8 || param[i] == 16) {
                   3044:                                                        hidden = 1;
                   3045:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   3046:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3047:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   3048:                                                                param[i] -= 16;
                   3049:                                                        } else {
                   3050:                                                                param[i] -= 30;
                   3051:                                                        }
                   3052:                                                        if(param[i] & 1) {
                   3053:                                                                wAttributes |= FOREGROUND_RED;
                   3054:                                                        }
                   3055:                                                        if(param[i] & 2) {
                   3056:                                                                wAttributes |= FOREGROUND_GREEN;
                   3057:                                                        }
                   3058:                                                        if(param[i] & 4) {
                   3059:                                                                wAttributes |= FOREGROUND_BLUE;
                   3060:                                                        }
                   3061:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   3062:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   3063:                                                        if((param[i] - 40) & 1) {
                   3064:                                                                wAttributes |= BACKGROUND_RED;
                   3065:                                                        }
                   3066:                                                        if((param[i] - 40) & 2) {
                   3067:                                                                wAttributes |= BACKGROUND_GREEN;
                   3068:                                                        }
                   3069:                                                        if((param[i] - 40) & 4) {
                   3070:                                                                wAttributes |= BACKGROUND_BLUE;
                   3071:                                                        }
                   3072:                                                }
                   3073:                                        }
                   3074:                                        if(reverse) {
                   3075:                                                wAttributes &= ~0xff;
                   3076:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   3077:                                        }
                   3078:                                        if(hidden) {
                   3079:                                                wAttributes &= ~0x0f;
                   3080:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   3081:                                        }
                   3082:                                } else if(data == 'n') {
                   3083:                                        if(param[0] == 6) {
                   3084:                                                char tmp[16];
                   3085:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   3086:                                                int len = strlen(tmp);
                   3087:                                                for(int i = 0; i < len; i++) {
                   3088:                                                        key_buf_char->write(tmp[i]);
                   3089:                                                        key_buf_scan->write(0x00);
                   3090:                                                }
                   3091:                                        }
                   3092:                                } else if(data == 's') {
                   3093:                                        stored_x = co.X;
                   3094:                                        stored_y = co.Y;
                   3095:                                        stored_a = wAttributes;
                   3096:                                } else if(data == 'u') {
                   3097:                                        co.X = stored_x;
                   3098:                                        co.Y = stored_y;
                   3099:                                        wAttributes = stored_a;
                   3100:                                }
                   3101:                        }
                   3102:                        if(co.X < 0) {
                   3103:                                co.X = 0;
                   3104:                        } else if(co.X >= csbi.dwSize.X) {
                   3105:                                co.X = csbi.dwSize.X - 1;
                   3106:                        }
1.1.1.14  root     3107:                        if(co.Y < csbi.srWindow.Top) {
                   3108:                                co.Y = csbi.srWindow.Top;
                   3109:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   3110:                                co.Y = csbi.srWindow.Bottom;
1.1       root     3111:                        }
                   3112:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   3113:                                SetConsoleCursorPosition(hStdout, co);
                   3114:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     3115:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     3116:                                cursor_moved = false;
                   3117:                        }
                   3118:                        if(wAttributes != csbi.wAttributes) {
                   3119:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   3120:                        }
                   3121:                        p = is_esc = 0;
                   3122:                }
                   3123:                return;
                   3124:        } else {
                   3125:                if(msdos_lead_byte_check(data)) {
                   3126:                        is_kanji = 1;
                   3127:                        return;
                   3128:                } else if(data == 0x1b) {
                   3129:                        is_esc = 1;
                   3130:                        return;
                   3131:                }
                   3132:        }
1.1.1.20  root     3133:        
                   3134:        DWORD q = 0, num;
                   3135:        is_kanji = 0;
                   3136:        for(int i = 0; i < p; i++) {
                   3137:                UINT8 c = tmp[i];
                   3138:                if(is_kanji) {
                   3139:                        is_kanji = 0;
                   3140:                } else if(msdos_lead_byte_check(data)) {
                   3141:                        is_kanji = 1;
                   3142:                } else if(msdos_ctrl_code_check(data)) {
                   3143:                        out[q++] = '^';
                   3144:                        c += 'A' - 1;
                   3145:                }
                   3146:                out[q++] = c;
                   3147:        }
                   3148:        WriteConsole(hStdout, out, q, &num, NULL);
1.1       root     3149:        p = 0;
1.1.1.14  root     3150:        
1.1.1.15  root     3151:        if(!restore_console_on_exit) {
                   3152:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3153:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   3154:                scr_top = csbi.srWindow.Top;
                   3155:        }
1.1       root     3156:        cursor_moved = true;
                   3157: }
                   3158: 
                   3159: int msdos_aux_in()
                   3160: {
1.1.1.21  root     3161:        msdos_stdio_reopen();
                   3162:        
1.1.1.20  root     3163:        process_t *process = msdos_process_info_get(current_psp);
                   3164:        int fd = msdos_psp_get_file_table(3, current_psp);
                   3165:        
                   3166:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     3167:                char data = 0;
1.1.1.20  root     3168:                _read(fd, &data, 1);
1.1       root     3169:                return(data);
                   3170:        } else {
                   3171:                return(EOF);
                   3172:        }
                   3173: }
                   3174: 
                   3175: void msdos_aux_out(char data)
                   3176: {
1.1.1.21  root     3177:        msdos_stdio_reopen();
                   3178:        
1.1.1.20  root     3179:        process_t *process = msdos_process_info_get(current_psp);
                   3180:        int fd = msdos_psp_get_file_table(3, current_psp);
                   3181:        
                   3182:        if(fd < process->max_files && file_handler[fd].valid) {
                   3183:                msdos_write(fd, &data, 1);
1.1       root     3184:        }
                   3185: }
                   3186: 
                   3187: void msdos_prn_out(char data)
                   3188: {
1.1.1.21  root     3189:        msdos_stdio_reopen();
                   3190:        
1.1.1.20  root     3191:        process_t *process = msdos_process_info_get(current_psp);
                   3192:        int fd = msdos_psp_get_file_table(4, current_psp);
                   3193:        
                   3194:        if(fd < process->max_files && file_handler[fd].valid) {
                   3195:                msdos_write(fd, &data, 1);
1.1       root     3196:        }
                   3197: }
                   3198: 
                   3199: // memory control
                   3200: 
1.1.1.19  root     3201: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     3202: {
                   3203:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3204:        
                   3205:        mcb->mz = mz;
                   3206:        mcb->psp = psp;
1.1.1.30  root     3207:        mcb->paragraphs = paragraphs;
1.1       root     3208:        return(mcb);
                   3209: }
                   3210: 
                   3211: void msdos_mcb_check(mcb_t *mcb)
                   3212: {
                   3213:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     3214:                #if 0
                   3215:                        // shutdown now !!!
                   3216:                        fatalerror("broken memory control block\n");
                   3217:                #else
                   3218:                        // return error code and continue
                   3219:                        throw(0x07); // broken memory control block
                   3220:                #endif
1.1       root     3221:        }
                   3222: }
                   3223: 
                   3224: int msdos_mem_split(int seg, int paragraphs)
                   3225: {
                   3226:        int mcb_seg = seg - 1;
                   3227:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3228:        msdos_mcb_check(mcb);
                   3229:        
1.1.1.30  root     3230:        if(mcb->paragraphs > paragraphs) {
1.1       root     3231:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     3232:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     3233:                
                   3234:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   3235:                mcb->mz = 'M';
1.1.1.30  root     3236:                mcb->paragraphs = paragraphs;
1.1       root     3237:                return(0);
                   3238:        }
                   3239:        return(-1);
                   3240: }
                   3241: 
                   3242: void msdos_mem_merge(int seg)
                   3243: {
                   3244:        int mcb_seg = seg - 1;
                   3245:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3246:        msdos_mcb_check(mcb);
                   3247:        
                   3248:        while(1) {
                   3249:                if(mcb->mz == 'Z') {
                   3250:                        break;
                   3251:                }
1.1.1.30  root     3252:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     3253:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   3254:                msdos_mcb_check(next_mcb);
                   3255:                
                   3256:                if(next_mcb->psp != 0) {
                   3257:                        break;
                   3258:                }
                   3259:                mcb->mz = next_mcb->mz;
1.1.1.30  root     3260:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     3261:        }
                   3262: }
                   3263: 
1.1.1.8   root     3264: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     3265: {
                   3266:        while(1) {
                   3267:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3268:                
1.1.1.14  root     3269:                if(mcb->psp == 0) {
                   3270:                        msdos_mem_merge(mcb_seg + 1);
                   3271:                } else {
                   3272:                        msdos_mcb_check(mcb);
                   3273:                }
1.1.1.8   root     3274:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.30  root     3275:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     3276:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   3277:                                mcb->psp = current_psp;
                   3278:                                return(mcb_seg + 1);
                   3279:                        }
                   3280:                }
                   3281:                if(mcb->mz == 'Z') {
                   3282:                        break;
                   3283:                }
1.1.1.30  root     3284:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     3285:        }
                   3286:        return(-1);
                   3287: }
                   3288: 
                   3289: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   3290: {
                   3291:        int mcb_seg = seg - 1;
                   3292:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3293:        msdos_mcb_check(mcb);
1.1.1.30  root     3294:        int current_paragraphs = mcb->paragraphs;
1.1       root     3295:        
                   3296:        msdos_mem_merge(seg);
1.1.1.30  root     3297:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     3298:                if(max_paragraphs) {
1.1.1.30  root     3299:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     3300:                }
1.1       root     3301:                msdos_mem_split(seg, current_paragraphs);
                   3302:                return(-1);
                   3303:        }
                   3304:        msdos_mem_split(seg, paragraphs);
                   3305:        return(0);
                   3306: }
                   3307: 
                   3308: void msdos_mem_free(int seg)
                   3309: {
                   3310:        int mcb_seg = seg - 1;
                   3311:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3312:        msdos_mcb_check(mcb);
                   3313:        
                   3314:        mcb->psp = 0;
                   3315:        msdos_mem_merge(seg);
                   3316: }
                   3317: 
1.1.1.8   root     3318: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     3319: {
                   3320:        int max_paragraphs = 0;
                   3321:        
                   3322:        while(1) {
                   3323:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3324:                msdos_mcb_check(mcb);
                   3325:                
1.1.1.8   root     3326:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.30  root     3327:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   3328:                                max_paragraphs = mcb->paragraphs;
1.1       root     3329:                        }
                   3330:                }
                   3331:                if(mcb->mz == 'Z') {
                   3332:                        break;
                   3333:                }
1.1.1.30  root     3334:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     3335:        }
1.1.1.14  root     3336:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     3337: }
                   3338: 
1.1.1.8   root     3339: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   3340: {
                   3341:        int last_seg = -1;
                   3342:        
                   3343:        while(1) {
                   3344:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3345:                msdos_mcb_check(mcb);
                   3346:                
1.1.1.14  root     3347:                if(mcb->psp == psp) {
1.1.1.8   root     3348:                        last_seg = mcb_seg;
                   3349:                }
1.1.1.14  root     3350:                if(mcb->mz == 'Z') {
                   3351:                        break;
                   3352:                }
1.1.1.30  root     3353:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     3354:        }
                   3355:        return(last_seg);
                   3356: }
                   3357: 
1.1.1.19  root     3358: int msdos_mem_get_umb_linked()
                   3359: {
                   3360:        int mcb_seg = first_mcb;
                   3361:        
                   3362:        while(1) {
                   3363:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3364:                msdos_mcb_check(mcb);
                   3365:                
                   3366:                if(mcb->mz == 'Z') {
                   3367:                        if(mcb_seg >= (UMB_TOP >> 4)) {
                   3368:                                return(-1);
                   3369:                        }
                   3370:                        break;
                   3371:                }
1.1.1.30  root     3372:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.19  root     3373:        }
                   3374:        return(0);
                   3375: }
                   3376: 
                   3377: int msdos_mem_link_umb()
                   3378: {
                   3379:        int mcb_seg = first_mcb;
                   3380:        
                   3381:        while(1) {
                   3382:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3383:                msdos_mcb_check(mcb);
1.1.1.30  root     3384:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.19  root     3385:                
                   3386:                if(mcb->mz == 'Z') {
                   3387:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   3388:                                mcb->mz = 'M';
1.1.1.20  root     3389:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     3390:                                return(-1);
                   3391:                        }
                   3392:                        break;
                   3393:                }
                   3394:        }
                   3395:        return(0);
                   3396: }
                   3397: 
                   3398: int msdos_mem_unlink_umb()
                   3399: {
                   3400:        int mcb_seg = first_mcb;
                   3401:        
                   3402:        while(1) {
                   3403:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3404:                msdos_mcb_check(mcb);
1.1.1.30  root     3405:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.19  root     3406:                
                   3407:                if(mcb->mz == 'Z') {
                   3408:                        break;
                   3409:                } else {
                   3410:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   3411:                                mcb->mz = 'Z';
1.1.1.20  root     3412:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     3413:                                return(-1);
                   3414:                        }
                   3415:                }
                   3416:        }
                   3417:        return(0);
                   3418: }
                   3419: 
1.1.1.29  root     3420: #ifdef SUPPORT_HMA
                   3421: 
                   3422: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   3423: {
                   3424:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3425:        
                   3426:        mcb->ms[0] = 'M';
                   3427:        mcb->ms[1] = 'S';
                   3428:        mcb->owner = owner;
                   3429:        mcb->size = size;
                   3430:        mcb->next = next;
                   3431:        return(mcb);
                   3432: }
                   3433: 
                   3434: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   3435: {
                   3436:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   3437: }
                   3438: 
                   3439: int msdos_hma_mem_split(int offset, int size)
                   3440: {
                   3441:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3442:        
                   3443:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   3444:                return(-1);
                   3445:        }
                   3446:        if(mcb->size >= size + 0x10) {
                   3447:                int new_offset = offset + 0x10 + size;
                   3448:                int new_size = mcb->size - 0x10 - size;
                   3449:                
                   3450:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   3451:                mcb->size = size;
                   3452:                mcb->next = new_offset;
                   3453:                return(0);
                   3454:        }
                   3455:        return(-1);
                   3456: }
                   3457: 
                   3458: void msdos_hma_mem_merge(int offset)
                   3459: {
                   3460:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3461:        
                   3462:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   3463:                return;
                   3464:        }
                   3465:        while(1) {
                   3466:                if(mcb->next == 0) {
                   3467:                        break;
                   3468:                }
                   3469:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   3470:                
                   3471:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   3472:                        return;
                   3473:                }
                   3474:                if(next_mcb->owner != 0) {
                   3475:                        break;
                   3476:                }
                   3477:                mcb->size += 0x10 + next_mcb->size;
                   3478:                mcb->next = next_mcb->next;
                   3479:        }
                   3480: }
                   3481: 
                   3482: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   3483: {
                   3484:        int offset = 0x10; // first mcb in HMA
                   3485:        
                   3486:        while(1) {
                   3487:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3488:                
                   3489:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   3490:                        return(-1);
                   3491:                }
                   3492:                if(mcb->owner == 0) {
                   3493:                        msdos_hma_mem_merge(offset);
                   3494:                }
                   3495:                if(mcb->owner == 0 && mcb->size >= size) {
                   3496:                        msdos_hma_mem_split(offset, size);
                   3497:                        mcb->owner = owner;
                   3498:                        return(offset);
                   3499:                }
                   3500:                if(mcb->next == 0) {
                   3501:                        break;
                   3502:                }
                   3503:                offset = mcb->next;
                   3504:        }
                   3505:        return(-1);
                   3506: }
                   3507: 
                   3508: int msdos_hma_mem_realloc(int offset, int size)
                   3509: {
                   3510:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3511:        
                   3512:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   3513:                return(-1);
                   3514:        }
                   3515:        if(mcb->size < size) {
                   3516:                return(-1);
                   3517:        }
                   3518:        msdos_hma_mem_split(offset, size);
                   3519:        return(0);
                   3520: }
                   3521: 
                   3522: void msdos_hma_mem_free(int offset)
                   3523: {
                   3524:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3525:        
                   3526:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   3527:                return;
                   3528:        }
                   3529:        mcb->owner = 0;
                   3530:        msdos_hma_mem_merge(offset);
                   3531: }
                   3532: 
                   3533: int msdos_hma_mem_get_free(int *available_offset)
                   3534: {
                   3535:        int offset = 0x10; // first mcb in HMA
                   3536:        int size = 0;
                   3537:        
                   3538:        while(1) {
                   3539:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   3540:                
                   3541:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   3542:                        return(0);
                   3543:                }
                   3544:                if(mcb->owner == 0 && size < mcb->size) {
                   3545:                        if(available_offset != NULL) {
                   3546:                                *available_offset = offset;
                   3547:                        }
                   3548:                        size = mcb->size;
                   3549:                }
                   3550:                if(mcb->next == 0) {
                   3551:                        break;
                   3552:                }
                   3553:                offset = mcb->next;
                   3554:        }
                   3555:        return(size);
                   3556: }
                   3557: 
                   3558: #endif
                   3559: 
1.1       root     3560: // environment
                   3561: 
                   3562: void msdos_env_set_argv(int env_seg, char *argv)
                   3563: {
                   3564:        char *dst = (char *)(mem + (env_seg << 4));
                   3565:        
                   3566:        while(1) {
                   3567:                if(dst[0] == 0) {
                   3568:                        break;
                   3569:                }
                   3570:                dst += strlen(dst) + 1;
                   3571:        }
                   3572:        *dst++ = 0; // end of environment
                   3573:        *dst++ = 1; // top of argv[0]
                   3574:        *dst++ = 0;
                   3575:        memcpy(dst, argv, strlen(argv));
                   3576:        dst += strlen(argv);
                   3577:        *dst++ = 0;
                   3578:        *dst++ = 0;
                   3579: }
                   3580: 
                   3581: char *msdos_env_get_argv(int env_seg)
                   3582: {
                   3583:        static char env[ENV_SIZE];
                   3584:        char *src = env;
                   3585:        
                   3586:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   3587:        while(1) {
                   3588:                if(src[0] == 0) {
                   3589:                        if(src[1] == 1) {
                   3590:                                return(src + 3);
                   3591:                        }
                   3592:                        break;
                   3593:                }
                   3594:                src += strlen(src) + 1;
                   3595:        }
                   3596:        return(NULL);
                   3597: }
                   3598: 
                   3599: char *msdos_env_get(int env_seg, const char *name)
                   3600: {
                   3601:        static char env[ENV_SIZE];
                   3602:        char *src = env;
                   3603:        
                   3604:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   3605:        while(1) {
                   3606:                if(src[0] == 0) {
                   3607:                        break;
                   3608:                }
                   3609:                int len = strlen(src);
                   3610:                char *n = my_strtok(src, "=");
                   3611:                char *v = src + strlen(n) + 1;
                   3612:                
                   3613:                if(_stricmp(name, n) == 0) {
                   3614:                        return(v);
                   3615:                }
                   3616:                src += len + 1;
                   3617:        }
                   3618:        return(NULL);
                   3619: }
                   3620: 
                   3621: void msdos_env_set(int env_seg, char *name, char *value)
                   3622: {
                   3623:        char env[ENV_SIZE];
                   3624:        char *src = env;
                   3625:        char *dst = (char *)(mem + (env_seg << 4));
                   3626:        char *argv = msdos_env_get_argv(env_seg);
                   3627:        int done = 0;
                   3628:        
                   3629:        memcpy(src, dst, ENV_SIZE);
                   3630:        memset(dst, 0, ENV_SIZE);
                   3631:        while(1) {
                   3632:                if(src[0] == 0) {
                   3633:                        break;
                   3634:                }
                   3635:                int len = strlen(src);
                   3636:                char *n = my_strtok(src, "=");
                   3637:                char *v = src + strlen(n) + 1;
                   3638:                char tmp[1024];
                   3639:                
                   3640:                if(_stricmp(name, n) == 0) {
                   3641:                        sprintf(tmp, "%s=%s", n, value);
                   3642:                        done = 1;
                   3643:                } else {
                   3644:                        sprintf(tmp, "%s=%s", n, v);
                   3645:                }
                   3646:                memcpy(dst, tmp, strlen(tmp));
                   3647:                dst += strlen(tmp) + 1;
                   3648:                src += len + 1;
                   3649:        }
                   3650:        if(!done) {
                   3651:                char tmp[1024];
                   3652:                
                   3653:                sprintf(tmp, "%s=%s", name, value);
                   3654:                memcpy(dst, tmp, strlen(tmp));
                   3655:                dst += strlen(tmp) + 1;
                   3656:        }
                   3657:        if(argv) {
                   3658:                *dst++ = 0; // end of environment
                   3659:                *dst++ = 1; // top of argv[0]
                   3660:                *dst++ = 0;
                   3661:                memcpy(dst, argv, strlen(argv));
                   3662:                dst += strlen(argv);
                   3663:                *dst++ = 0;
                   3664:                *dst++ = 0;
                   3665:        }
                   3666: }
                   3667: 
                   3668: // process
                   3669: 
1.1.1.8   root     3670: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     3671: {
                   3672:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3673:        
                   3674:        memset(psp, 0, PSP_SIZE);
                   3675:        psp->exit[0] = 0xcd;
                   3676:        psp->exit[1] = 0x20;
1.1.1.8   root     3677:        psp->first_mcb = mcb_seg;
1.1       root     3678:        psp->far_call = 0xea;
                   3679:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   3680:        psp->cpm_entry.w.h = 0xf000;
                   3681:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   3682:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   3683:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   3684:        psp->parent_psp = parent_psp;
1.1.1.20  root     3685:        if(parent_psp == (UINT16)-1) {
                   3686:                for(int i = 0; i < 20; i++) {
                   3687:                        if(file_handler[i].valid) {
                   3688:                                psp->file_table[i] = i;
                   3689:                        } else {
                   3690:                                psp->file_table[i] = 0xff;
                   3691:                        }
1.1       root     3692:                }
1.1.1.20  root     3693:        } else {
                   3694:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     3695:        }
                   3696:        psp->env_seg = env_seg;
                   3697:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     3698:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     3699:        psp->file_table_size = 20;
                   3700:        psp->file_table_ptr.w.l = 0x18;
                   3701:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     3702:        psp->service[0] = 0xcd;
                   3703:        psp->service[1] = 0x21;
                   3704:        psp->service[2] = 0xcb;
                   3705:        return(psp);
                   3706: }
                   3707: 
1.1.1.20  root     3708: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   3709: {
                   3710:        if(psp_seg && fd < 20) {
                   3711:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3712:                psp->file_table[fd] = value;
                   3713:        }
                   3714: }
                   3715: 
                   3716: int msdos_psp_get_file_table(int fd, int psp_seg)
                   3717: {
                   3718:        if(psp_seg && fd < 20) {
                   3719:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3720:                fd = psp->file_table[fd];
                   3721:        }
                   3722:        return fd;
                   3723: }
                   3724: 
1.1       root     3725: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   3726: {
                   3727:        // load command file
                   3728:        int fd = -1;
                   3729:        int dos_command = 0;
1.1.1.24  root     3730:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1       root     3731:        
                   3732:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3733:        int opt_len = mem[opt_ofs];
                   3734:        memset(opt, 0, sizeof(opt));
                   3735:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3736:        
1.1.1.14  root     3737:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   3738:                // this is a batch file, run command.com
                   3739:                char tmp[MAX_PATH];
                   3740:                if(opt_len != 0) {
                   3741:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   3742:                } else {
                   3743:                        sprintf(tmp, "/C %s", cmd);
                   3744:                }
                   3745:                strcpy(opt, tmp);
                   3746:                opt_len = strlen(opt);
                   3747:                mem[opt_ofs] = opt_len;
                   3748:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3749:                strcpy(command, comspec_path);
                   3750:                strcpy(name_tmp, "COMMAND.COM");
                   3751:        } else {
                   3752:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   3753:                        // redirect C:\COMMAND.COM to comspec_path
                   3754:                        strcpy(command, comspec_path);
                   3755:                } else {
                   3756:                        strcpy(command, cmd);
                   3757:                }
1.1.1.24  root     3758:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   3759:                        return(-1);
                   3760:                }
1.1.1.14  root     3761:                memset(name_tmp, 0, sizeof(name_tmp));
                   3762:                strcpy(name_tmp, name);
                   3763:                
                   3764:                // check command.com
                   3765:                if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) {
                   3766:                        if(opt_len == 0) {
                   3767: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   3768:                                process_t *current_process = NULL;
                   3769:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   3770:                                        if(process[i].psp == current_psp) {
                   3771:                                                current_process = &process[i];
                   3772:                                                break;
                   3773:                                        }
                   3774:                                }
                   3775:                                if(current_process != NULL) {
                   3776:                                        param->cmd_line.dw = current_process->dta.dw;
                   3777:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3778:                                        opt_len = mem[opt_ofs];
                   3779:                                        memset(opt, 0, sizeof(opt));
                   3780:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3781:                                }
                   3782:                        }
                   3783:                        for(int i = 0; i < opt_len; i++) {
                   3784:                                if(opt[i] == ' ') {
                   3785:                                        continue;
                   3786:                                }
                   3787:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   3788:                                        for(int j = i + 3; j < opt_len; j++) {
                   3789:                                                if(opt[j] == ' ') {
                   3790:                                                        continue;
                   3791:                                                }
                   3792:                                                char *token = my_strtok(opt + j, " ");
                   3793:                                                
                   3794:                                                if(strlen(token) >= 5 && _stricmp(&token[strlen(token) - 4], ".BAT") == 0) {
                   3795:                                                        // this is a batch file, okay to run command.com
                   3796:                                                } else {
                   3797:                                                        // run program directly without command.com
                   3798:                                                        strcpy(command, token);
                   3799:                                                        char tmp[MAX_PATH];
                   3800:                                                        strcpy(tmp, token + strlen(token) + 1);
                   3801:                                                        strcpy(opt, tmp);
                   3802:                                                        opt_len = strlen(opt);
                   3803:                                                        mem[opt_ofs] = opt_len;
                   3804:                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3805:                                                        dos_command = 1;
                   3806:                                                }
                   3807:                                                break;
1.1       root     3808:                                        }
                   3809:                                }
1.1.1.14  root     3810:                                break;
1.1       root     3811:                        }
                   3812:                }
                   3813:        }
                   3814:        
                   3815:        // load command file
                   3816:        strcpy(path, command);
                   3817:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3818:                sprintf(path, "%s.COM", command);
                   3819:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3820:                        sprintf(path, "%s.EXE", command);
                   3821:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     3822:                                sprintf(path, "%s.BAT", command);
                   3823:                                if(_access(path, 0) == 0) {
                   3824:                                        // this is a batch file, run command.com
                   3825:                                        char tmp[MAX_PATH];
                   3826:                                        if(opt_len != 0) {
                   3827:                                                sprintf(tmp, "/C %s %s", path, opt);
                   3828:                                        } else {
                   3829:                                                sprintf(tmp, "/C %s", path);
                   3830:                                        }
                   3831:                                        strcpy(opt, tmp);
                   3832:                                        opt_len = strlen(opt);
                   3833:                                        mem[opt_ofs] = opt_len;
                   3834:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3835:                                        strcpy(path, comspec_path);
                   3836:                                        strcpy(name_tmp, "COMMAND.COM");
                   3837:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3838:                                } else {
                   3839:                                        // search path in parent environments
                   3840:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3841:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   3842:                                        if(env != NULL) {
                   3843:                                                char env_path[4096];
                   3844:                                                strcpy(env_path, env);
                   3845:                                                char *token = my_strtok(env_path, ";");
                   3846:                                                
                   3847:                                                while(token != NULL) {
                   3848:                                                        if(strlen(token) != 0) {
                   3849:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   3850:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3851:                                                                        break;
                   3852:                                                                }
                   3853:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   3854:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3855:                                                                        break;
                   3856:                                                                }
                   3857:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   3858:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3859:                                                                        break;
                   3860:                                                                }
                   3861:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   3862:                                                                if(_access(path, 0) == 0) {
                   3863:                                                                        // this is a batch file, run command.com
                   3864:                                                                        char tmp[MAX_PATH];
                   3865:                                                                        if(opt_len != 0) {
                   3866:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   3867:                                                                        } else {
                   3868:                                                                                sprintf(tmp, "/C %s", path);
                   3869:                                                                        }
                   3870:                                                                        strcpy(opt, tmp);
                   3871:                                                                        opt_len = strlen(opt);
                   3872:                                                                        mem[opt_ofs] = opt_len;
                   3873:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3874:                                                                        strcpy(path, comspec_path);
                   3875:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   3876:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3877:                                                                        break;
                   3878:                                                                }
1.1.1.8   root     3879:                                                        }
1.1.1.14  root     3880:                                                        token = my_strtok(NULL, ";");
1.1       root     3881:                                                }
                   3882:                                        }
                   3883:                                }
                   3884:                        }
                   3885:                }
                   3886:        }
                   3887:        if(fd == -1) {
                   3888:                if(dos_command) {
                   3889:                        // may be dos command
                   3890:                        char tmp[MAX_PATH];
                   3891:                        sprintf(tmp, "%s %s", command, opt);
                   3892:                        system(tmp);
                   3893:                        return(0);
                   3894:                } else {
                   3895:                        return(-1);
                   3896:                }
                   3897:        }
                   3898:        _read(fd, file_buffer, sizeof(file_buffer));
                   3899:        _close(fd);
                   3900:        
                   3901:        // copy environment
1.1.1.29  root     3902:        int umb_linked, env_seg, psp_seg;
1.1       root     3903:        
1.1.1.29  root     3904:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   3905:                msdos_mem_unlink_umb();
                   3906:        }
1.1.1.8   root     3907:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     3908:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   3909:                        if(umb_linked != 0) {
                   3910:                                msdos_mem_link_umb();
                   3911:                        }
                   3912:                        return(-1);
                   3913:                }
1.1       root     3914:        }
                   3915:        if(param->env_seg == 0) {
                   3916:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3917:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   3918:        } else {
                   3919:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   3920:        }
                   3921:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   3922:        
                   3923:        // check exe header
                   3924:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     3925:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     3926:        UINT16 cs, ss, ip, sp;
                   3927:        
                   3928:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   3929:                // memory allocation
                   3930:                int header_size = header->header_size * 16;
                   3931:                int load_size = header->pages * 512 - header_size;
                   3932:                if(header_size + load_size < 512) {
                   3933:                        load_size = 512 - header_size;
                   3934:                }
                   3935:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   3936:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   3937:                        msdos_mem_free(env_seg);
                   3938:                        return(-1);
                   3939:                }
                   3940:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   3941:                if(paragraphs > free_paragraphs) {
                   3942:                        paragraphs = free_paragraphs;
                   3943:                }
1.1.1.8   root     3944:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     3945:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   3946:                                if(umb_linked != 0) {
                   3947:                                        msdos_mem_link_umb();
                   3948:                                }
                   3949:                                msdos_mem_free(env_seg);
                   3950:                                return(-1);
                   3951:                        }
1.1       root     3952:                }
                   3953:                // relocation
                   3954:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3955:                for(int i = 0; i < header->relocations; i++) {
                   3956:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   3957:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   3958:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   3959:                }
                   3960:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   3961:                // segments
                   3962:                cs = header->init_cs + start_seg;
                   3963:                ss = header->init_ss + start_seg;
                   3964:                ip = header->init_ip;
                   3965:                sp = header->init_sp - 2; // for symdeb
                   3966:        } else {
                   3967:                // memory allocation
                   3968:                paragraphs = free_paragraphs;
1.1.1.8   root     3969:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     3970:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   3971:                                if(umb_linked != 0) {
                   3972:                                        msdos_mem_link_umb();
                   3973:                                }
                   3974:                                msdos_mem_free(env_seg);
                   3975:                                return(-1);
                   3976:                        }
1.1       root     3977:                }
                   3978:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3979:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   3980:                // segments
                   3981:                cs = ss = psp_seg;
                   3982:                ip = 0x100;
                   3983:                sp = 0xfffe;
                   3984:        }
1.1.1.29  root     3985:        if(umb_linked != 0) {
                   3986:                msdos_mem_link_umb();
                   3987:        }
1.1       root     3988:        
                   3989:        // create psp
1.1.1.3   root     3990: #if defined(HAS_I386)
                   3991:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   3992: #else
                   3993:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_pc - SREG_BASE(CS);
                   3994: #endif
                   3995:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     3996:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   3997:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   3998:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   3999:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   4000:        
                   4001:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   4002:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   4003:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   4004:        
1.1.1.4   root     4005:        for(int i = 0; i < 8; i++) {
                   4006:                if(name_tmp[i] == '.') {
                   4007:                        mcb_psp->prog_name[i] = '\0';
                   4008:                        break;
                   4009:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   4010:                        mcb_psp->prog_name[i] = name_tmp[i];
                   4011:                        i++;
                   4012:                        mcb_psp->prog_name[i] = name_tmp[i];
                   4013:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   4014:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   4015:                } else {
                   4016:                        mcb_psp->prog_name[i] = name_tmp[i];
                   4017:                }
                   4018:        }
                   4019:        
1.1       root     4020:        // process info
                   4021:        process_t *process = msdos_process_info_create(psp_seg);
                   4022:        strcpy(process->module_dir, msdos_short_full_dir(path));
                   4023:        process->dta.w.l = 0x80;
                   4024:        process->dta.w.h = psp_seg;
                   4025:        process->switchar = '/';
                   4026:        process->max_files = 20;
                   4027:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   4028:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     4029:        process->parent_ds = SREG(DS);
1.1.1.31! root     4030:        process->parent_es = SREG(ES);
1.1       root     4031:        
                   4032:        current_psp = psp_seg;
1.1.1.23  root     4033:        msdos_sda_update(current_psp);
1.1       root     4034:        
                   4035:        if(al == 0x00) {
                   4036:                int_10h_feh_called = int_10h_ffh_called = false;
                   4037:                
                   4038:                // registers and segments
                   4039:                REG16(AX) = REG16(BX) = 0x00;
                   4040:                REG16(CX) = 0xff;
                   4041:                REG16(DX) = psp_seg;
                   4042:                REG16(SI) = ip;
                   4043:                REG16(DI) = sp;
                   4044:                REG16(SP) = sp;
1.1.1.3   root     4045:                SREG(DS) = SREG(ES) = psp_seg;
                   4046:                SREG(SS) = ss;
                   4047:                i386_load_segment_descriptor(DS);
                   4048:                i386_load_segment_descriptor(ES);
                   4049:                i386_load_segment_descriptor(SS);
1.1       root     4050:                
                   4051:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   4052:                i386_jmp_far(cs, ip);
                   4053:        } else if(al == 0x01) {
                   4054:                // copy ss:sp and cs:ip to param block
                   4055:                param->sp = sp;
                   4056:                param->ss = ss;
                   4057:                param->ip = ip;
                   4058:                param->cs = cs;
1.1.1.31! root     4059:                
        !          4060:                // the AX value to be passed to the child program is put on top of the child's stack
        !          4061:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     4062:        }
                   4063:        return(0);
                   4064: }
                   4065: 
                   4066: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   4067: {
                   4068:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   4069:        
                   4070:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   4071:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   4072:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   4073:        
1.1.1.3   root     4074:        SREG(SS) = psp->stack.w.h;
                   4075:        i386_load_segment_descriptor(SS);
1.1       root     4076:        REG16(SP) = psp->stack.w.l;
                   4077:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   4078:        
1.1.1.28  root     4079: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   4080:        process_t *current_process = NULL;
                   4081:        for(int i = 0; i < MAX_PROCESS; i++) {
                   4082:                if(process[i].psp == psp_seg) {
                   4083:                        current_process = &process[i];
                   4084:                        break;
                   4085:                }
                   4086:        }
                   4087:        if(current_process == NULL) {
                   4088:                throw(0x1f); // general failure
                   4089:        }
                   4090:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   4091:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   4092:        if(current_process->called_by_int2eh) {
                   4093:                REG16(AX) = ret;
                   4094:        }
                   4095:        SREG(DS) = current_process->parent_ds;
1.1.1.31! root     4096:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     4097:        i386_load_segment_descriptor(DS);
1.1.1.31! root     4098:        i386_load_segment_descriptor(ES);
1.1       root     4099:        
                   4100:        if(mem_free) {
1.1.1.8   root     4101:                int mcb_seg;
                   4102:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   4103:                        msdos_mem_free(mcb_seg + 1);
                   4104:                }
                   4105:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   4106:                        msdos_mem_free(mcb_seg + 1);
                   4107:                }
1.1       root     4108:                
                   4109:                for(int i = 0; i < MAX_FILES; i++) {
                   4110:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   4111:                                _close(i);
1.1.1.20  root     4112:                                msdos_file_handler_close(i);
                   4113:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     4114:                        }
                   4115:                }
1.1.1.13  root     4116:                msdos_dta_info_free(psp_seg);
1.1       root     4117:        }
1.1.1.14  root     4118:        msdos_stdio_reopen();
1.1       root     4119:        
1.1.1.28  root     4120:        memset(current_process, 0, sizeof(process_t));
1.1       root     4121:        
                   4122:        current_psp = psp->parent_psp;
                   4123:        retval = ret;
1.1.1.23  root     4124:        msdos_sda_update(current_psp);
1.1       root     4125: }
                   4126: 
                   4127: // drive
                   4128: 
                   4129: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   4130: {
                   4131:        *seg = DPB_TOP >> 4;
                   4132:        *ofs = sizeof(dpb_t) * drive_num;
                   4133:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   4134:        
                   4135:        if(!force_update && dpb->free_clusters != 0) {
                   4136:                return(dpb->bytes_per_sector ? 1 : 0);
                   4137:        }
                   4138:        memset(dpb, 0, sizeof(dpb_t));
                   4139:        
                   4140:        int res = 0;
                   4141:        char dev[64];
                   4142:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   4143:        
1.1.1.17  root     4144:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     4145:        if(hFile != INVALID_HANDLE_VALUE) {
                   4146:                DISK_GEOMETRY geo;
                   4147:                DWORD dwSize;
                   4148:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   4149:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   4150:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   4151:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     4152:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     4153:                        switch(geo.MediaType) {
                   4154:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   4155:                                dpb->media_type = 0xff;
                   4156:                                break;
                   4157:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   4158:                                dpb->media_type = 0xfe;
                   4159:                                break;
                   4160:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   4161:                                dpb->media_type = 0xfd;
                   4162:                                break;
                   4163:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   4164:                                dpb->media_type = 0xfc;
                   4165:                                break;
                   4166:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   4167:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   4168:                                dpb->media_type = 0xf9;
                   4169:                                break;
                   4170:                        case FixedMedia:        // hard disk
                   4171:                        case RemovableMedia:
1.1.1.19  root     4172:                        case Unknown:
1.1       root     4173:                                dpb->media_type = 0xf8;
                   4174:                                break;
                   4175:                        default:
                   4176:                                dpb->media_type = 0xf0;
                   4177:                                break;
                   4178:                        }
                   4179:                        res = 1;
                   4180:                }
                   4181:                dpb->drive_num = drive_num;
                   4182:                dpb->unit_num = drive_num;
                   4183:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   4184:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     4185:                dpb->info_sector = 0xffff;
                   4186:                dpb->backup_boot_sector = 0xffff;
1.1       root     4187:                dpb->free_clusters = 0xffff;
1.1.1.14  root     4188:                dpb->free_search_cluster = 0xffffffff;
1.1       root     4189:                CloseHandle(hFile);
                   4190:        }
                   4191:        return(res);
                   4192: }
                   4193: 
                   4194: // pc bios
                   4195: 
1.1.1.19  root     4196: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   4197: {
                   4198:        static unsigned __int64 start_msec_since_midnight = 0;
                   4199:        static unsigned __int64 start_msec_since_hostboot = 0;
                   4200:        
                   4201:        if(start_msec_since_midnight == 0) {
                   4202:                SYSTEMTIME time;
                   4203:                GetLocalTime(&time);
                   4204:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   4205:                start_msec_since_hostboot = cur_msec;
                   4206:        }
                   4207:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   4208:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   4209:        return (UINT32)tick;
                   4210: }
                   4211: 
                   4212: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   4213: {
                   4214:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   4215:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   4216:        
                   4217:        if(prev_tick > next_tick) {
                   4218:                mem[0x470] = 1;
                   4219:        }
                   4220:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   4221: }
                   4222: 
1.1.1.14  root     4223: inline void pcbios_irq0()
                   4224: {
                   4225:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     4226:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     4227: }
                   4228: 
1.1.1.16  root     4229: int pcbios_get_text_vram_address(int page)
1.1       root     4230: {
                   4231:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4232:                return TEXT_VRAM_TOP;
1.1       root     4233:        } else {
1.1.1.14  root     4234:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     4235:        }
                   4236: }
                   4237: 
1.1.1.16  root     4238: int pcbios_get_shadow_buffer_address(int page)
1.1       root     4239: {
1.1.1.14  root     4240:        if(!int_10h_feh_called) {
1.1.1.16  root     4241:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     4242:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4243:                return SHADOW_BUF_TOP;
                   4244:        } else {
1.1.1.14  root     4245:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     4246:        }
                   4247: }
                   4248: 
1.1.1.16  root     4249: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     4250: {
1.1.1.16  root     4251:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     4252: }
                   4253: 
1.1.1.16  root     4254: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     4255: {
1.1.1.14  root     4256:        // clear the existing screen, not just the new one
                   4257:        int clr_height = max(height, scr_height);
                   4258:        
1.1.1.16  root     4259:        if(scr_width != width || scr_height != height) {
                   4260:                change_console_size(width, height);
1.1.1.14  root     4261:        }
                   4262:        mem[0x462] = 0;
                   4263:        *(UINT16 *)(mem + 0x44e) = 0;
                   4264:        
1.1.1.16  root     4265:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   4266:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   4267:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   4268:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     4269:        
1.1.1.23  root     4270:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     4271:        if(clr_screen) {
1.1.1.14  root     4272:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   4273:                        mem[ofs++] = 0x20;
                   4274:                        mem[ofs++] = 0x07;
                   4275:                }
                   4276:                
                   4277:                EnterCriticalSection(&vram_crit_sect);
                   4278:                for(int y = 0; y < clr_height; y++) {
                   4279:                        for(int x = 0; x < scr_width; x++) {
                   4280:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4281:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     4282:                        }
                   4283:                }
                   4284:                SMALL_RECT rect;
1.1.1.14  root     4285:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   4286:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4287:                vram_length_char = vram_last_length_char = 0;
                   4288:                vram_length_attr = vram_last_length_attr = 0;
                   4289:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4290:        }
1.1.1.14  root     4291:        COORD co;
                   4292:        co.X = 0;
                   4293:        co.Y = scr_top;
                   4294:        SetConsoleCursorPosition(hStdout, co);
                   4295:        cursor_moved = true;
                   4296:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     4297: }
                   4298: 
1.1.1.16  root     4299: inline void pcbios_int_10h_00h()
                   4300: {
                   4301:        switch(REG8(AL) & 0x7f) {
                   4302:        case 0x70: // v-text mode
                   4303:        case 0x71: // extended cga v-text mode
                   4304:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   4305:                break;
                   4306:        default:
                   4307:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   4308:                break;
                   4309:        }
                   4310:        if(REG8(AL) & 0x80) {
                   4311:                mem[0x487] |= 0x80;
                   4312:        } else {
                   4313:                mem[0x487] &= ~0x80;
                   4314:        }
                   4315:        mem[0x449] = REG8(AL) & 0x7f;
                   4316: }
                   4317: 
1.1       root     4318: inline void pcbios_int_10h_01h()
                   4319: {
1.1.1.13  root     4320:        mem[0x460] = REG8(CL);
                   4321:        mem[0x461] = REG8(CH);
1.1.1.14  root     4322:        
1.1.1.23  root     4323:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4324:        CONSOLE_CURSOR_INFO ci;
                   4325:        GetConsoleCursorInfo(hStdout, &ci);
                   4326: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   4327: //     if(ci.bVisible) {
                   4328:                int lines = max(8, REG8(CL) + 1);
                   4329:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   4330: //     }
                   4331:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     4332: }
                   4333: 
                   4334: inline void pcbios_int_10h_02h()
                   4335: {
1.1.1.14  root     4336:        // continuously setting the cursor effectively stops it blinking
                   4337:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     4338:                COORD co;
                   4339:                co.X = REG8(DL);
1.1.1.14  root     4340:                co.Y = REG8(DH) + scr_top;
                   4341:                
                   4342:                // some programs hide the cursor by moving it off screen
                   4343:                static bool hidden = false;
1.1.1.23  root     4344:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4345:                CONSOLE_CURSOR_INFO ci;
                   4346:                GetConsoleCursorInfo(hStdout, &ci);
                   4347:                
                   4348:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   4349:                        if(ci.bVisible) {
                   4350:                                ci.bVisible = FALSE;
                   4351: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4352:                                hidden = true;
                   4353:                        }
                   4354:                } else if(hidden) {
                   4355:                        if(!ci.bVisible) {
                   4356:                                ci.bVisible = TRUE;
                   4357: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4358:                        }
                   4359:                        hidden = false;
                   4360:                }
1.1       root     4361:        }
1.1.1.14  root     4362:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   4363:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     4364: }
                   4365: 
                   4366: inline void pcbios_int_10h_03h()
                   4367: {
1.1.1.14  root     4368:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4369:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4370:        REG8(CL) = mem[0x460];
                   4371:        REG8(CH) = mem[0x461];
                   4372: }
                   4373: 
                   4374: inline void pcbios_int_10h_05h()
                   4375: {
1.1.1.14  root     4376:        if(REG8(AL) >= vram_pages) {
                   4377:                return;
                   4378:        }
                   4379:        if(mem[0x462] != REG8(AL)) {
                   4380:                vram_flush();
                   4381:                
1.1.1.23  root     4382:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4383:                SMALL_RECT rect;
1.1.1.14  root     4384:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4385:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4386:                
1.1.1.16  root     4387:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     4388:                        for(int x = 0; x < scr_width; x++) {
                   4389:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4390:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4391:                        }
                   4392:                }
1.1.1.16  root     4393:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     4394:                        for(int x = 0; x < scr_width; x++) {
                   4395:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   4396:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     4397:                        }
                   4398:                }
1.1.1.14  root     4399:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4400:                
                   4401:                COORD co;
1.1.1.14  root     4402:                co.X = mem[0x450 + REG8(AL) * 2];
                   4403:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   4404:                if(co.Y < scr_top + scr_height) {
                   4405:                        SetConsoleCursorPosition(hStdout, co);
                   4406:                }
1.1       root     4407:        }
1.1.1.14  root     4408:        mem[0x462] = REG8(AL);
                   4409:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   4410:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     4411:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     4412:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     4413:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     4414:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     4415: }
                   4416: 
                   4417: inline void pcbios_int_10h_06h()
                   4418: {
1.1.1.14  root     4419:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4420:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4421:                return;
                   4422:        }
                   4423:        vram_flush();
                   4424:        
1.1.1.23  root     4425:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4426:        SMALL_RECT rect;
1.1.1.14  root     4427:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4428:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4429:        
                   4430:        int right = min(REG8(DL), scr_width - 1);
                   4431:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4432:        
                   4433:        if(REG8(AL) == 0) {
1.1.1.14  root     4434:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4435:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4436:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4437:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4438:                        }
                   4439:                }
                   4440:        } else {
1.1.1.14  root     4441:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     4442:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4443:                                if(y2 <= bottom) {
                   4444:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4445:                                } else {
1.1.1.14  root     4446:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4447:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4448:                                }
1.1.1.14  root     4449:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4450:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4451:                        }
                   4452:                }
                   4453:        }
1.1.1.14  root     4454:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4455: }
                   4456: 
                   4457: inline void pcbios_int_10h_07h()
                   4458: {
1.1.1.14  root     4459:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4460:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4461:                return;
                   4462:        }
                   4463:        vram_flush();
                   4464:        
1.1.1.23  root     4465:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4466:        SMALL_RECT rect;
1.1.1.14  root     4467:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4468:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4469:        
                   4470:        int right = min(REG8(DL), scr_width - 1);
                   4471:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4472:        
                   4473:        if(REG8(AL) == 0) {
1.1.1.14  root     4474:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4475:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4476:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4477:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4478:                        }
                   4479:                }
                   4480:        } else {
1.1.1.14  root     4481:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     4482:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4483:                                if(y2 >= REG8(CH)) {
                   4484:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4485:                                } else {
1.1.1.14  root     4486:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4487:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4488:                                }
1.1.1.14  root     4489:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4490:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4491:                        }
                   4492:                }
                   4493:        }
1.1.1.14  root     4494:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4495: }
                   4496: 
                   4497: inline void pcbios_int_10h_08h()
                   4498: {
                   4499:        COORD co;
                   4500:        DWORD num;
                   4501:        
1.1.1.14  root     4502:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4503:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4504:        
                   4505:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4506:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4507:                co.Y += scr_top;
                   4508:                vram_flush();
1.1       root     4509:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   4510:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   4511:                REG8(AL) = scr_char[0];
                   4512:                REG8(AH) = scr_attr[0];
                   4513:        } else {
1.1.1.16  root     4514:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     4515:        }
                   4516: }
                   4517: 
                   4518: inline void pcbios_int_10h_09h()
                   4519: {
                   4520:        COORD co;
                   4521:        
1.1.1.14  root     4522:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4523:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4524:        
1.1.1.16  root     4525:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4526:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4527:        
                   4528:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4529:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4530:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4531:                while(dest < end) {
                   4532:                        write_text_vram_char(dest - vram, REG8(AL));
                   4533:                        mem[dest++] = REG8(AL);
                   4534:                        write_text_vram_attr(dest - vram, REG8(BL));
                   4535:                        mem[dest++] = REG8(BL);
1.1       root     4536:                }
1.1.1.14  root     4537:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4538:        } else {
1.1.1.14  root     4539:                while(dest < end) {
1.1       root     4540:                        mem[dest++] = REG8(AL);
                   4541:                        mem[dest++] = REG8(BL);
                   4542:                }
                   4543:        }
                   4544: }
                   4545: 
                   4546: inline void pcbios_int_10h_0ah()
                   4547: {
                   4548:        COORD co;
                   4549:        
1.1.1.14  root     4550:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4551:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4552:        
1.1.1.16  root     4553:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4554:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4555:        
                   4556:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4557:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4558:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4559:                while(dest < end) {
                   4560:                        write_text_vram_char(dest - vram, REG8(AL));
                   4561:                        mem[dest++] = REG8(AL);
                   4562:                        dest++;
1.1       root     4563:                }
1.1.1.14  root     4564:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4565:        } else {
1.1.1.14  root     4566:                while(dest < end) {
1.1       root     4567:                        mem[dest++] = REG8(AL);
                   4568:                        dest++;
                   4569:                }
                   4570:        }
                   4571: }
                   4572: 
                   4573: inline void pcbios_int_10h_0eh()
                   4574: {
1.1.1.14  root     4575:        DWORD num;
                   4576:        COORD co;
                   4577:        
                   4578:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4579:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4580:        
                   4581:        if(REG8(AL) == 7) {
                   4582:                //MessageBeep(-1);
                   4583:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   4584:                if(REG8(AL) == 10) {
                   4585:                        vram_flush();
                   4586:                }
1.1.1.23  root     4587:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     4588:                cursor_moved = true;
                   4589:        } else {
1.1.1.16  root     4590:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     4591:                if(mem[0x462] == REG8(BH)) {
                   4592:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4593:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4594:                        write_text_vram_char(dest - vram, REG8(AL));
                   4595:                        LeaveCriticalSection(&vram_crit_sect);
                   4596:                        
1.1.1.23  root     4597:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4598:                        if(++co.X == scr_width) {
                   4599:                                co.X = 0;
                   4600:                                if(++co.Y == scr_height) {
                   4601:                                        vram_flush();
                   4602:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   4603:                                        cursor_moved = true;
                   4604:                                }
                   4605:                        }
                   4606:                        if(!cursor_moved) {
                   4607:                                co.Y += scr_top;
                   4608:                                SetConsoleCursorPosition(hStdout, co);
                   4609:                                cursor_moved = true;
                   4610:                        }
                   4611:                }
                   4612:                mem[dest] = REG8(AL);
                   4613:        }
1.1       root     4614: }
                   4615: 
                   4616: inline void pcbios_int_10h_0fh()
                   4617: {
                   4618:        REG8(AL) = mem[0x449];
                   4619:        REG8(AH) = mem[0x44a];
                   4620:        REG8(BH) = mem[0x462];
                   4621: }
                   4622: 
1.1.1.14  root     4623: inline void pcbios_int_10h_11h()
                   4624: {
                   4625:        switch(REG8(AL)) {
1.1.1.16  root     4626:        case 0x01:
1.1.1.14  root     4627:        case 0x11:
1.1.1.16  root     4628:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     4629:                break;
1.1.1.16  root     4630:        case 0x02:
1.1.1.14  root     4631:        case 0x12:
1.1.1.16  root     4632:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4633:                break;
1.1.1.16  root     4634:        case 0x04:
1.1.1.14  root     4635:        case 0x14:
1.1.1.16  root     4636:                pcbios_set_console_size(80, 25, true);
                   4637:                break;
                   4638:        case 0x18:
                   4639:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4640:                break;
                   4641:        case 0x30:
                   4642:                SREG(ES) = 0;
                   4643:                i386_load_segment_descriptor(ES);
                   4644:                REG16(BP) = 0;
                   4645:                REG16(CX) = mem[0x485];
                   4646:                REG8(DL) = mem[0x484];
                   4647:                break;
                   4648:        }
                   4649: }
                   4650: 
                   4651: inline void pcbios_int_10h_12h()
                   4652: {
1.1.1.16  root     4653:        switch(REG8(BL)) {
                   4654:        case 0x10:
1.1.1.14  root     4655:                REG16(BX) = 0x0003;
                   4656:                REG16(CX) = 0x0009;
1.1.1.16  root     4657:                break;
1.1.1.14  root     4658:        }
                   4659: }
                   4660: 
1.1       root     4661: inline void pcbios_int_10h_13h()
                   4662: {
1.1.1.3   root     4663:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     4664:        COORD co;
                   4665:        DWORD num;
                   4666:        
                   4667:        co.X = REG8(DL);
1.1.1.14  root     4668:        co.Y = REG8(DH) + scr_top;
                   4669:        
                   4670:        vram_flush();
1.1       root     4671:        
                   4672:        switch(REG8(AL)) {
                   4673:        case 0x00:
                   4674:        case 0x01:
                   4675:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4676:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4677:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4678:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4679:                        SetConsoleCursorPosition(hStdout, co);
                   4680:                        
                   4681:                        if(csbi.wAttributes != REG8(BL)) {
                   4682:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   4683:                        }
1.1.1.14  root     4684:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   4685:                        
1.1       root     4686:                        if(csbi.wAttributes != REG8(BL)) {
                   4687:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4688:                        }
                   4689:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     4690:                                if(!restore_console_on_exit) {
                   4691:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4692:                                        scr_top = csbi.srWindow.Top;
                   4693:                                }
1.1.1.14  root     4694:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4695:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4696:                                SetConsoleCursorPosition(hStdout, co);
                   4697:                        } else {
                   4698:                                cursor_moved = true;
                   4699:                        }
                   4700:                } else {
1.1.1.3   root     4701:                        m_CF = 1;
1.1       root     4702:                }
                   4703:                break;
                   4704:        case 0x02:
                   4705:        case 0x03:
                   4706:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4707:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4708:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4709:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4710:                        SetConsoleCursorPosition(hStdout, co);
                   4711:                        
                   4712:                        WORD wAttributes = csbi.wAttributes;
                   4713:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   4714:                                if(wAttributes != mem[ofs + 1]) {
                   4715:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   4716:                                        wAttributes = mem[ofs + 1];
                   4717:                                }
1.1.1.14  root     4718:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     4719:                        }
                   4720:                        if(csbi.wAttributes != wAttributes) {
                   4721:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4722:                        }
                   4723:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     4724:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4725:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4726:                                SetConsoleCursorPosition(hStdout, co);
                   4727:                        } else {
                   4728:                                cursor_moved = true;
                   4729:                        }
                   4730:                } else {
1.1.1.3   root     4731:                        m_CF = 1;
1.1       root     4732:                }
                   4733:                break;
                   4734:        case 0x10:
                   4735:        case 0x11:
                   4736:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4737:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4738:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   4739:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   4740:                        for(int i = 0; i < num; i++) {
                   4741:                                mem[ofs++] = scr_char[i];
                   4742:                                mem[ofs++] = scr_attr[i];
                   4743:                                if(REG8(AL) == 0x11) {
                   4744:                                        mem[ofs++] = 0;
                   4745:                                        mem[ofs++] = 0;
                   4746:                                }
                   4747:                        }
                   4748:                } else {
1.1.1.16  root     4749:                        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     4750:                                mem[ofs++] = mem[src++];
                   4751:                                mem[ofs++] = mem[src++];
                   4752:                                if(REG8(AL) == 0x11) {
                   4753:                                        mem[ofs++] = 0;
                   4754:                                        mem[ofs++] = 0;
                   4755:                                }
1.1.1.14  root     4756:                                if(++co.X == scr_width) {
                   4757:                                        if(++co.Y == scr_height) {
1.1       root     4758:                                                break;
                   4759:                                        }
                   4760:                                        co.X = 0;
                   4761:                                }
                   4762:                        }
                   4763:                }
                   4764:                break;
                   4765:        case 0x20:
                   4766:        case 0x21:
                   4767:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4768:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4769:                        int len = min(REG16(CX), scr_width * scr_height);
                   4770:                        for(int i = 0; i < len; i++) {
1.1       root     4771:                                scr_char[i] = mem[ofs++];
                   4772:                                scr_attr[i] = mem[ofs++];
                   4773:                                if(REG8(AL) == 0x21) {
                   4774:                                        ofs += 2;
                   4775:                                }
                   4776:                        }
1.1.1.14  root     4777:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4778:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4779:                } else {
1.1.1.16  root     4780:                        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     4781:                                mem[dest++] = mem[ofs++];
                   4782:                                mem[dest++] = mem[ofs++];
                   4783:                                if(REG8(AL) == 0x21) {
                   4784:                                        ofs += 2;
                   4785:                                }
1.1.1.14  root     4786:                                if(++co.X == scr_width) {
                   4787:                                        if(++co.Y == scr_height) {
1.1       root     4788:                                                break;
                   4789:                                        }
                   4790:                                        co.X = 0;
                   4791:                                }
                   4792:                        }
                   4793:                }
                   4794:                break;
                   4795:        default:
1.1.1.22  root     4796:                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     4797:                m_CF = 1;
1.1       root     4798:                break;
                   4799:        }
                   4800: }
                   4801: 
1.1.1.30  root     4802: inline void pcbios_int_10h_18h()
                   4803: {
                   4804:        switch(REG8(AL)) {
                   4805:        case 0x00:
                   4806:        case 0x01:
                   4807: //             REG8(AL) = 0x86;
                   4808:                REG8(AL) = 0x00;
                   4809:                break;
                   4810:        default:
                   4811:                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));
                   4812:                m_CF = 1;
                   4813:                break;
                   4814:        }
                   4815: }
                   4816: 
1.1.1.14  root     4817: inline void pcbios_int_10h_1ah()
                   4818: {
                   4819:        switch(REG8(AL)) {
                   4820:        case 0x00:
                   4821:                REG8(AL) = 0x1a;
                   4822:                REG8(BL) = 0x08;
                   4823:                REG8(BH) = 0x00;
                   4824:                break;
                   4825:        default:
1.1.1.22  root     4826:                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     4827:                m_CF = 1;
                   4828:                break;
                   4829:        }
                   4830: }
                   4831: 
1.1       root     4832: inline void pcbios_int_10h_1dh()
                   4833: {
                   4834:        switch(REG8(AL)) {
                   4835:        case 0x01:
                   4836:                break;
                   4837:        case 0x02:
                   4838:                REG16(BX) = 0;
                   4839:                break;
                   4840:        default:
1.1.1.22  root     4841:                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));
                   4842:                m_CF = 1;
                   4843:                break;
                   4844:        }
                   4845: }
                   4846: 
                   4847: inline void pcbios_int_10h_4fh()
                   4848: {
                   4849:        switch(REG8(AL)) {
                   4850:        case 0x00:
                   4851:                REG8(AH) = 0x02; // not supported
                   4852:                break;
                   4853:        case 0x01:
                   4854:        case 0x02:
                   4855:        case 0x03:
                   4856:        case 0x04:
                   4857:        case 0x05:
                   4858:        case 0x06:
                   4859:        case 0x07:
                   4860:        case 0x08:
                   4861:        case 0x09:
                   4862:        case 0x0a:
                   4863:        case 0x0b:
                   4864:        case 0x0c:
                   4865:                REG8(AH) = 0x01; // failed
                   4866:                break;
                   4867:        default:
                   4868:                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     4869:                m_CF = 1;
1.1       root     4870:                break;
                   4871:        }
                   4872: }
                   4873: 
                   4874: inline void pcbios_int_10h_82h()
                   4875: {
                   4876:        static UINT8 mode = 0;
                   4877:        
                   4878:        switch(REG8(AL)) {
1.1.1.22  root     4879:        case 0x00:
1.1       root     4880:                if(REG8(BL) != 0xff) {
                   4881:                        mode = REG8(BL);
                   4882:                }
                   4883:                REG8(AL) = mode;
                   4884:                break;
                   4885:        default:
1.1.1.22  root     4886:                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     4887:                m_CF = 1;
1.1       root     4888:                break;
                   4889:        }
                   4890: }
                   4891: 
1.1.1.22  root     4892: inline void pcbios_int_10h_83h()
                   4893: {
                   4894:        static UINT8 mode = 0;
                   4895:        
                   4896:        switch(REG8(AL)) {
                   4897:        case 0x00:
                   4898:                REG16(AX) = 0; // offset???
                   4899:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   4900:                i386_load_segment_descriptor(ES);
                   4901:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   4902:                break;
                   4903:        default:
                   4904:                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));
                   4905:                m_CF = 1;
                   4906:                break;
                   4907:        }
                   4908: }
                   4909: 
                   4910: inline void pcbios_int_10h_90h()
                   4911: {
                   4912:        REG8(AL) = mem[0x449];
                   4913: }
                   4914: 
                   4915: inline void pcbios_int_10h_91h()
                   4916: {
                   4917:        REG8(AL) = 0x04; // VGA
                   4918: }
                   4919: 
                   4920: inline void pcbios_int_10h_efh()
                   4921: {
                   4922:        REG16(DX) = 0xffff;
                   4923: }
                   4924: 
1.1       root     4925: inline void pcbios_int_10h_feh()
                   4926: {
                   4927:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4928:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     4929:                i386_load_segment_descriptor(ES);
1.1.1.8   root     4930:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     4931:        }
                   4932:        int_10h_feh_called = true;
                   4933: }
                   4934: 
                   4935: inline void pcbios_int_10h_ffh()
                   4936: {
                   4937:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     4938:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4939:                COORD co;
                   4940:                DWORD num;
                   4941:                
1.1.1.14  root     4942:                vram_flush();
                   4943:                
                   4944:                co.X = (REG16(DI) >> 1) % scr_width;
                   4945:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     4946:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   4947:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     4948:                int len;
                   4949:                for(len = 0; ofs < end; len++) {
                   4950:                        scr_char[len] = mem[ofs++];
                   4951:                        scr_attr[len] = mem[ofs++];
                   4952:                }
                   4953:                co.Y += scr_top;
                   4954:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4955:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4956:        }
                   4957:        int_10h_ffh_called = true;
                   4958: }
                   4959: 
1.1.1.25  root     4960: inline void pcbios_int_14h_00h()
                   4961: {
1.1.1.29  root     4962:        if(REG16(DX) < 4) {
1.1.1.25  root     4963:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   4964:                UINT8 selector = sio_read(REG16(DX), 3);
                   4965:                selector &= ~0x3f;
                   4966:                selector |= REG8(AL) & 0x1f;
                   4967:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   4968:                sio_write(REG16(DX), 3, selector | 0x80);
                   4969:                sio_write(REG16(DX), 0, divisor & 0xff);
                   4970:                sio_write(REG16(DX), 1, divisor >> 8);
                   4971:                sio_write(REG16(DX), 3, selector);
                   4972:                REG8(AH) = sio_read(REG16(DX), 5);
                   4973:                REG8(AL) = sio_read(REG16(DX), 6);
                   4974:        } else {
                   4975:                REG8(AH) = 0x80;
                   4976:        }
                   4977: }
                   4978: 
                   4979: inline void pcbios_int_14h_01h()
                   4980: {
1.1.1.29  root     4981:        if(REG16(DX) < 4) {
1.1.1.25  root     4982:                UINT8 selector = sio_read(REG16(DX), 3);
                   4983:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4984:                sio_write(REG16(DX), 0, REG8(AL));
                   4985:                sio_write(REG16(DX), 3, selector);
                   4986:                REG8(AH) = sio_read(REG16(DX), 5);
                   4987:        } else {
                   4988:                REG8(AH) = 0x80;
                   4989:        }
                   4990: }
                   4991: 
                   4992: inline void pcbios_int_14h_02h()
                   4993: {
1.1.1.29  root     4994:        if(REG16(DX) < 4) {
1.1.1.25  root     4995:                UINT8 selector = sio_read(REG16(DX), 3);
                   4996:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4997:                REG8(AL) = sio_read(REG16(DX), 0);
                   4998:                sio_write(REG16(DX), 3, selector);
                   4999:                REG8(AH) = sio_read(REG16(DX), 5);
                   5000:        } else {
                   5001:                REG8(AH) = 0x80;
                   5002:        }
                   5003: }
                   5004: 
                   5005: inline void pcbios_int_14h_03h()
                   5006: {
1.1.1.29  root     5007:        if(REG16(DX) < 4) {
1.1.1.25  root     5008:                REG8(AH) = sio_read(REG16(DX), 5);
                   5009:                REG8(AL) = sio_read(REG16(DX), 6);
                   5010:        } else {
                   5011:                REG8(AH) = 0x80;
                   5012:        }
                   5013: }
                   5014: 
                   5015: inline void pcbios_int_14h_04h()
                   5016: {
1.1.1.29  root     5017:        if(REG16(DX) < 4) {
1.1.1.25  root     5018:                UINT8 selector = sio_read(REG16(DX), 3);
                   5019:                if(REG8(CH) <= 0x03) {
                   5020:                        selector = (selector & ~0x03) | REG8(CH);
                   5021:                }
                   5022:                if(REG8(BL) == 0x00) {
                   5023:                        selector &= ~0x04;
                   5024:                } else if(REG8(BL) == 0x01) {
                   5025:                        selector |= 0x04;
                   5026:                }
                   5027:                if(REG8(BH) == 0x00) {
                   5028:                        selector = (selector & ~0x38) | 0x00;
                   5029:                } else if(REG8(BH) == 0x01) {
                   5030:                        selector = (selector & ~0x38) | 0x08;
                   5031:                } else if(REG8(BH) == 0x02) {
                   5032:                        selector = (selector & ~0x38) | 0x18;
                   5033:                } else if(REG8(BH) == 0x03) {
                   5034:                        selector = (selector & ~0x38) | 0x28;
                   5035:                } else if(REG8(BH) == 0x04) {
                   5036:                        selector = (selector & ~0x38) | 0x38;
                   5037:                }
                   5038:                if(REG8(AL) == 0x00) {
                   5039:                        selector |= 0x40;
                   5040:                } else if(REG8(AL) == 0x01) {
                   5041:                        selector &= ~0x40;
                   5042:                }
                   5043:                if(REG8(CL) <= 0x0b) {
                   5044:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   5045:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   5046:                        sio_write(REG16(DX), 3, selector | 0x80);
                   5047:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   5048:                        sio_write(REG16(DX), 1, divisor >> 8);
                   5049:                }
                   5050:                sio_write(REG16(DX), 3, selector);
                   5051:                REG8(AH) = sio_read(REG16(DX), 5);
                   5052:                REG8(AL) = sio_read(REG16(DX), 6);
                   5053:        } else {
                   5054:                REG8(AH) = 0x80;
                   5055:        }
                   5056: }
                   5057: 
                   5058: inline void pcbios_int_14h_05h()
                   5059: {
1.1.1.29  root     5060:        if(REG16(DX) < 4) {
1.1.1.25  root     5061:                if(REG8(AL) == 0x00) {
                   5062:                        REG8(BL) = sio_read(REG16(DX), 4);
                   5063:                        REG8(AH) = sio_read(REG16(DX), 5);
                   5064:                        REG8(AL) = sio_read(REG16(DX), 6);
                   5065:                } else if(REG8(AL) == 0x01) {
                   5066:                        sio_write(REG16(DX), 4, REG8(BL));
                   5067:                        REG8(AH) = sio_read(REG16(DX), 5);
                   5068:                        REG8(AL) = sio_read(REG16(DX), 6);
                   5069:                } else {
                   5070:                        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));
                   5071:                }
                   5072:        } else {
                   5073:                REG8(AH) = 0x80;
                   5074:        }
                   5075: }
                   5076: 
1.1.1.14  root     5077: inline void pcbios_int_15h_10h()
                   5078: {
1.1.1.22  root     5079:        switch(REG8(AL)) {
                   5080:        case 0x00:
1.1.1.14  root     5081:                Sleep(10);
                   5082:                hardware_update();
1.1.1.22  root     5083:                break;
                   5084:        default:
                   5085:                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     5086:                REG8(AH) = 0x86;
                   5087:                m_CF = 1;
                   5088:        }
                   5089: }
                   5090: 
1.1       root     5091: inline void pcbios_int_15h_23h()
                   5092: {
                   5093:        switch(REG8(AL)) {
1.1.1.22  root     5094:        case 0x00:
1.1.1.8   root     5095:                REG8(CL) = cmos_read(0x2d);
                   5096:                REG8(CH) = cmos_read(0x2e);
1.1       root     5097:                break;
1.1.1.22  root     5098:        case 0x01:
1.1.1.8   root     5099:                cmos_write(0x2d, REG8(CL));
                   5100:                cmos_write(0x2e, REG8(CH));
1.1       root     5101:                break;
                   5102:        default:
1.1.1.22  root     5103:                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     5104:                REG8(AH) = 0x86;
1.1.1.3   root     5105:                m_CF = 1;
1.1       root     5106:                break;
                   5107:        }
                   5108: }
                   5109: 
                   5110: inline void pcbios_int_15h_24h()
                   5111: {
                   5112:        switch(REG8(AL)) {
1.1.1.22  root     5113:        case 0x00:
1.1.1.3   root     5114:                i386_set_a20_line(0);
1.1       root     5115:                REG8(AH) = 0;
                   5116:                break;
1.1.1.22  root     5117:        case 0x01:
1.1.1.3   root     5118:                i386_set_a20_line(1);
1.1       root     5119:                REG8(AH) = 0;
                   5120:                break;
1.1.1.22  root     5121:        case 0x02:
1.1       root     5122:                REG8(AH) = 0;
1.1.1.3   root     5123:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     5124:                REG16(CX) = 0;
                   5125:                break;
1.1.1.22  root     5126:        case 0x03:
1.1       root     5127:                REG16(AX) = 0;
                   5128:                REG16(BX) = 0;
                   5129:                break;
1.1.1.22  root     5130:        default:
                   5131:                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));
                   5132:                REG8(AH) = 0x86;
                   5133:                m_CF = 1;
                   5134:                break;
1.1       root     5135:        }
                   5136: }
                   5137: 
                   5138: inline void pcbios_int_15h_49h()
                   5139: {
1.1.1.27  root     5140:        REG8(AH) = 0x00;
                   5141:        REG8(BL) = 0x00; // DOS/V
1.1       root     5142: }
                   5143: 
1.1.1.22  root     5144: inline void pcbios_int_15h_50h()
                   5145: {
                   5146:        switch(REG8(AL)) {
                   5147:        case 0x00:
                   5148:        case 0x01:
                   5149:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   5150:                        REG8(AH) = 0x01; // invalid font type in bh
                   5151:                        m_CF = 1;
1.1.1.27  root     5152:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     5153:                        REG8(AH) = 0x02; // bl not zero
                   5154:                        m_CF = 1;
                   5155:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   5156:                        REG8(AH) = 0x04; // invalid code page
                   5157:                        m_CF = 1;
1.1.1.27  root     5158:                } else if(REG8(AL) == 0x01) {
                   5159:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     5160:                        m_CF = 1;
1.1.1.27  root     5161:                } else {
                   5162:                        // dummy font read routine is at fffd:000d
                   5163:                        SREG(ES) = 0xfffd;
                   5164:                        i386_load_segment_descriptor(ES);
                   5165:                        REG16(BX) = 0x0d;
                   5166:                        REG8(AH) = 0x00; // success
1.1.1.22  root     5167:                }
                   5168:                break;
                   5169:        default:
                   5170:                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));
                   5171:                REG8(AH) = 0x86;
                   5172:                m_CF = 1;
                   5173:                break;
                   5174:        }
                   5175: }
                   5176: 
1.1.1.30  root     5177: inline void pcbios_int_15h_53h()
                   5178: {
                   5179:        switch(REG8(AL)) {
                   5180:        case 0x00:
                   5181:                // APM is not installed
                   5182:                REG8(AH) = 0x86;
                   5183:                m_CF = 1;
                   5184:                break;
                   5185:        default:
                   5186:                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));
                   5187:                REG8(AH) = 0x86;
                   5188:                m_CF = 1;
                   5189:                break;
                   5190:        }
                   5191: }
                   5192: 
1.1       root     5193: inline void pcbios_int_15h_86h()
                   5194: {
                   5195:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     5196:        UINT32 msec = usec / 1000;
                   5197:        
                   5198:        while(msec) {
                   5199:                UINT32 tmp = min(msec, 100);
                   5200:                if(msec - tmp < 10) {
                   5201:                        tmp = msec;
                   5202:                }
                   5203:                Sleep(tmp);
                   5204:                
                   5205:                if(m_halted) {
                   5206:                        return;
                   5207:                }
                   5208:                msec -= tmp;
                   5209:        }
1.1       root     5210: }
                   5211: 
                   5212: inline void pcbios_int_15h_87h()
                   5213: {
                   5214:        // copy extended memory (from DOSBox)
                   5215:        int len = REG16(CX) * 2;
1.1.1.3   root     5216:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     5217:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   5218:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   5219:        memcpy(mem + dst, mem + src, len);
                   5220:        REG16(AX) = 0x00;
                   5221: }
                   5222: 
                   5223: inline void pcbios_int_15h_88h()
                   5224: {
1.1.1.17  root     5225:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     5226: }
                   5227: 
                   5228: inline void pcbios_int_15h_89h()
                   5229: {
1.1.1.21  root     5230: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     5231:        // switch to protected mode (from DOSBox)
                   5232:        write_io_byte(0x20, 0x10);
                   5233:        write_io_byte(0x21, REG8(BH));
                   5234:        write_io_byte(0x21, 0x00);
                   5235:        write_io_byte(0xa0, 0x10);
                   5236:        write_io_byte(0xa1, REG8(BL));
                   5237:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     5238:        i386_set_a20_line(1);
                   5239:        int ofs = SREG_BASE(ES) + REG16(SI);
                   5240:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   5241:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   5242:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   5243:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   5244: #if defined(HAS_I386)
                   5245:        m_cr[0] |= 1;
                   5246: #else
                   5247:        m_msw |= 1;
                   5248: #endif
                   5249:        SREG(DS) = 0x18;
                   5250:        SREG(ES) = 0x20;
                   5251:        SREG(SS) = 0x28;
                   5252:        i386_load_segment_descriptor(DS);
                   5253:        i386_load_segment_descriptor(ES);
                   5254:        i386_load_segment_descriptor(SS);
1.1.1.21  root     5255:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     5256:        REG16(SP) += 6;
1.1.1.3   root     5257: #if defined(HAS_I386)
1.1.1.21  root     5258:        UINT32 flags = get_flags();
                   5259:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   5260:        set_flags(flags);
1.1.1.3   root     5261: #else
1.1.1.21  root     5262:        UINT32 flags = CompressFlags();
                   5263:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   5264:        ExpandFlags(flags);
1.1.1.3   root     5265: #endif
1.1       root     5266:        REG16(AX) = 0x00;
1.1.1.21  root     5267:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     5268: #else
1.1.1.21  root     5269:        // i86/i186/v30: protected mode is not supported
1.1       root     5270:        REG8(AH) = 0x86;
1.1.1.3   root     5271:        m_CF = 1;
1.1       root     5272: #endif
                   5273: }
                   5274: 
1.1.1.21  root     5275: inline void pcbios_int_15h_8ah()
                   5276: {
                   5277:        UINT32 size = MAX_MEM - 0x100000;
                   5278:        REG16(AX) = size & 0xffff;
                   5279:        REG16(DX) = size >> 16;
                   5280: }
                   5281: 
1.1.1.3   root     5282: #if defined(HAS_I386)
1.1       root     5283: inline void pcbios_int_15h_c9h()
                   5284: {
                   5285:        REG8(AH) = 0x00;
                   5286:        REG8(CH) = cpu_type;
                   5287:        REG8(CL) = cpu_step;
                   5288: }
1.1.1.3   root     5289: #endif
1.1       root     5290: 
                   5291: inline void pcbios_int_15h_cah()
                   5292: {
                   5293:        switch(REG8(AL)) {
1.1.1.22  root     5294:        case 0x00:
1.1       root     5295:                if(REG8(BL) > 0x3f) {
                   5296:                        REG8(AH) = 0x03;
1.1.1.3   root     5297:                        m_CF = 1;
1.1       root     5298:                } else if(REG8(BL) < 0x0e) {
                   5299:                        REG8(AH) = 0x04;
1.1.1.3   root     5300:                        m_CF = 1;
1.1       root     5301:                } else {
1.1.1.8   root     5302:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     5303:                }
                   5304:                break;
1.1.1.22  root     5305:        case 0x01:
1.1       root     5306:                if(REG8(BL) > 0x3f) {
                   5307:                        REG8(AH) = 0x03;
1.1.1.3   root     5308:                        m_CF = 1;
1.1       root     5309:                } else if(REG8(BL) < 0x0e) {
                   5310:                        REG8(AH) = 0x04;
1.1.1.3   root     5311:                        m_CF = 1;
1.1       root     5312:                } else {
1.1.1.8   root     5313:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     5314:                }
                   5315:                break;
                   5316:        default:
1.1.1.22  root     5317:                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     5318:                REG8(AH) = 0x86;
1.1.1.3   root     5319:                m_CF = 1;
1.1       root     5320:                break;
                   5321:        }
                   5322: }
                   5323: 
1.1.1.22  root     5324: inline void pcbios_int_15h_e8h()
1.1.1.17  root     5325: {
1.1.1.22  root     5326:        switch(REG8(AL)) {
                   5327: #if defined(HAS_I386)
                   5328:        case 0x01:
                   5329:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   5330:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   5331:                break;
1.1.1.17  root     5332: #endif
1.1.1.22  root     5333:        default:
                   5334:                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));
                   5335:                REG8(AH) = 0x86;
                   5336:                m_CF = 1;
                   5337:                break;
                   5338:        }
                   5339: }
1.1.1.17  root     5340: 
1.1.1.16  root     5341: UINT32 pcbios_get_key_code(bool clear_buffer)
1.1       root     5342: {
                   5343:        UINT32 code = 0;
                   5344:        
                   5345:        if(key_buf_char->count() == 0) {
1.1.1.14  root     5346:                if(!update_key_buffer()) {
                   5347:                        if(clear_buffer) {
                   5348:                                Sleep(10);
                   5349:                        } else {
                   5350:                                maybe_idle();
                   5351:                        }
                   5352:                }
1.1       root     5353:        }
                   5354:        if(!clear_buffer) {
                   5355:                key_buf_char->store_buffer();
                   5356:                key_buf_scan->store_buffer();
                   5357:        }
                   5358:        if(key_buf_char->count() != 0) {
                   5359:                code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   5360:        }
                   5361:        if(key_buf_char->count() != 0) {
                   5362:                code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   5363:        }
                   5364:        if(!clear_buffer) {
                   5365:                key_buf_char->restore_buffer();
                   5366:                key_buf_scan->restore_buffer();
                   5367:        }
                   5368:        return code;
                   5369: }
                   5370: 
                   5371: inline void pcbios_int_16h_00h()
                   5372: {
1.1.1.14  root     5373:        while(key_code == 0 && !m_halted) {
1.1.1.16  root     5374:                key_code = pcbios_get_key_code(true);
1.1       root     5375:        }
                   5376:        if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   5377:                if(REG8(AH) == 0x10) {
                   5378:                        key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   5379:                } else {
                   5380:                        key_code = ((key_code >> 16) & 0xff00);
                   5381:                }
                   5382:        }
                   5383:        REG16(AX) = key_code & 0xffff;
                   5384:        key_code >>= 16;
                   5385: }
                   5386: 
                   5387: inline void pcbios_int_16h_01h()
                   5388: {
1.1.1.5   root     5389:        UINT32 key_code_tmp = key_code;
1.1       root     5390:        
1.1.1.5   root     5391:        if(key_code_tmp == 0) {
1.1.1.16  root     5392:                key_code_tmp = pcbios_get_key_code(false);
1.1.1.5   root     5393:        }
1.1.1.14  root     5394:        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   5395:                if(REG8(AH) == 0x11) {
                   5396:                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   5397:                } else {
                   5398:                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1       root     5399:                }
                   5400:        }
1.1.1.5   root     5401:        if(key_code_tmp != 0) {
                   5402:                REG16(AX) = key_code_tmp & 0xffff;
1.1       root     5403:        }
1.1.1.3   root     5404: #if defined(HAS_I386)
1.1.1.5   root     5405:        m_ZF = (key_code_tmp == 0);
1.1.1.3   root     5406: #else
1.1.1.5   root     5407:        m_ZeroVal = (key_code_tmp != 0);
1.1.1.3   root     5408: #endif
1.1       root     5409: }
                   5410: 
                   5411: inline void pcbios_int_16h_02h()
                   5412: {
                   5413:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   5414:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   5415:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   5416:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   5417:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   5418:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   5419:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   5420:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   5421: }
                   5422: 
                   5423: inline void pcbios_int_16h_03h()
                   5424: {
                   5425:        static UINT16 status = 0;
                   5426:        
                   5427:        switch(REG8(AL)) {
                   5428:        case 0x05:
                   5429:                status = REG16(BX);
                   5430:                break;
                   5431:        case 0x06:
                   5432:                REG16(BX) = status;
                   5433:                break;
                   5434:        default:
1.1.1.3   root     5435:                m_CF = 1;
1.1       root     5436:                break;
                   5437:        }
                   5438: }
                   5439: 
                   5440: inline void pcbios_int_16h_05h()
                   5441: {
1.1.1.14  root     5442:        key_buf_char->write(REG8(CL));
                   5443:        key_buf_scan->write(REG8(CH));
1.1       root     5444:        REG8(AL) = 0x00;
                   5445: }
                   5446: 
                   5447: inline void pcbios_int_16h_12h()
                   5448: {
                   5449:        pcbios_int_16h_02h();
                   5450:        
                   5451:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   5452:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   5453:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   5454:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   5455:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   5456:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   5457:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   5458:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   5459: }
                   5460: 
                   5461: inline void pcbios_int_16h_13h()
                   5462: {
                   5463:        static UINT16 status = 0;
                   5464:        
                   5465:        switch(REG8(AL)) {
                   5466:        case 0x00:
                   5467:                status = REG16(DX);
                   5468:                break;
                   5469:        case 0x01:
                   5470:                REG16(DX) = status;
                   5471:                break;
                   5472:        default:
1.1.1.22  root     5473:                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     5474:                m_CF = 1;
1.1       root     5475:                break;
                   5476:        }
                   5477: }
                   5478: 
                   5479: inline void pcbios_int_16h_14h()
                   5480: {
                   5481:        static UINT8 status = 0;
                   5482:        
                   5483:        switch(REG8(AL)) {
                   5484:        case 0x00:
                   5485:        case 0x01:
                   5486:                status = REG8(AL);
                   5487:                break;
                   5488:        case 0x02:
                   5489:                REG8(AL) = status;
                   5490:                break;
                   5491:        default:
1.1.1.22  root     5492:                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     5493:                m_CF = 1;
1.1       root     5494:                break;
                   5495:        }
                   5496: }
                   5497: 
1.1.1.24  root     5498: inline void pcbios_int_16h_55h()
                   5499: {
                   5500:        switch(REG8(AL)) {
                   5501:        case 0x00:
                   5502:                // keyboard tsr is not present
                   5503:                break;
                   5504:        case 0xfe:
                   5505:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   5506:                break;
                   5507:        case 0xff:
                   5508:                break;
                   5509:        default:
                   5510:                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));
                   5511:                m_CF = 1;
                   5512:                break;
                   5513:        }
                   5514: }
                   5515: 
1.1.1.30  root     5516: inline void pcbios_int_16h_6fh()
                   5517: {
                   5518:        switch(REG8(AL)) {
                   5519:        case 0x00:
                   5520:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   5521:                break;
                   5522:        default:
                   5523:                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));
                   5524:                m_CF = 1;
                   5525:                break;
                   5526:        }
                   5527: }
                   5528: 
1.1       root     5529: inline void pcbios_int_1ah_00h()
                   5530: {
1.1.1.19  root     5531:        pcbios_update_daily_timer_counter(timeGetTime());
                   5532:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   5533:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   5534:        REG8(AL) = mem[0x470];
                   5535:        mem[0x470] = 0;
1.1       root     5536: }
                   5537: 
                   5538: inline int to_bcd(int t)
                   5539: {
                   5540:        int u = (t % 100) / 10;
                   5541:        return (u << 4) | (t % 10);
                   5542: }
                   5543: 
                   5544: inline void pcbios_int_1ah_02h()
                   5545: {
                   5546:        SYSTEMTIME time;
                   5547:        
                   5548:        GetLocalTime(&time);
                   5549:        REG8(CH) = to_bcd(time.wHour);
                   5550:        REG8(CL) = to_bcd(time.wMinute);
                   5551:        REG8(DH) = to_bcd(time.wSecond);
                   5552:        REG8(DL) = 0x00;
                   5553: }
                   5554: 
                   5555: inline void pcbios_int_1ah_04h()
                   5556: {
                   5557:        SYSTEMTIME time;
                   5558:        
                   5559:        GetLocalTime(&time);
                   5560:        REG8(CH) = to_bcd(time.wYear / 100);
                   5561:        REG8(CL) = to_bcd(time.wYear);
                   5562:        REG8(DH) = to_bcd(time.wMonth);
                   5563:        REG8(DL) = to_bcd(time.wDay);
                   5564: }
                   5565: 
                   5566: inline void pcbios_int_1ah_0ah()
                   5567: {
                   5568:        SYSTEMTIME time;
                   5569:        FILETIME file_time;
                   5570:        WORD dos_date, dos_time;
                   5571:        
                   5572:        GetLocalTime(&time);
                   5573:        SystemTimeToFileTime(&time, &file_time);
                   5574:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   5575:        REG16(CX) = dos_date;
                   5576: }
                   5577: 
                   5578: // msdos system call
                   5579: 
                   5580: inline void msdos_int_21h_00h()
                   5581: {
1.1.1.3   root     5582:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     5583: }
                   5584: 
                   5585: inline void msdos_int_21h_01h()
                   5586: {
                   5587:        REG8(AL) = msdos_getche();
1.1.1.26  root     5588:        ctrl_c_detected = ctrl_c_pressed;
                   5589:        
1.1.1.8   root     5590:        // some seconds may be passed in console
1.1       root     5591:        hardware_update();
                   5592: }
                   5593: 
                   5594: inline void msdos_int_21h_02h()
                   5595: {
                   5596:        msdos_putch(REG8(DL));
1.1.1.26  root     5597:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5598: }
                   5599: 
                   5600: inline void msdos_int_21h_03h()
                   5601: {
                   5602:        REG8(AL) = msdos_aux_in();
                   5603: }
                   5604: 
                   5605: inline void msdos_int_21h_04h()
                   5606: {
                   5607:        msdos_aux_out(REG8(DL));
                   5608: }
                   5609: 
                   5610: inline void msdos_int_21h_05h()
                   5611: {
                   5612:        msdos_prn_out(REG8(DL));
                   5613: }
                   5614: 
                   5615: inline void msdos_int_21h_06h()
                   5616: {
                   5617:        if(REG8(DL) == 0xff) {
                   5618:                if(msdos_kbhit()) {
                   5619:                        REG8(AL) = msdos_getch();
1.1.1.3   root     5620: #if defined(HAS_I386)
                   5621:                        m_ZF = 0;
                   5622: #else
                   5623:                        m_ZeroVal = 1;
                   5624: #endif
1.1       root     5625:                } else {
                   5626:                        REG8(AL) = 0;
1.1.1.3   root     5627: #if defined(HAS_I386)
                   5628:                        m_ZF = 1;
                   5629: #else
                   5630:                        m_ZeroVal = 0;
                   5631: #endif
1.1.1.14  root     5632:                        maybe_idle();
1.1       root     5633:                }
                   5634:        } else {
                   5635:                msdos_putch(REG8(DL));
                   5636:        }
                   5637: }
                   5638: 
                   5639: inline void msdos_int_21h_07h()
                   5640: {
                   5641:        REG8(AL) = msdos_getch();
1.1.1.26  root     5642:        
1.1.1.8   root     5643:        // some seconds may be passed in console
1.1       root     5644:        hardware_update();
                   5645: }
                   5646: 
                   5647: inline void msdos_int_21h_08h()
                   5648: {
                   5649:        REG8(AL) = msdos_getch();
1.1.1.26  root     5650:        ctrl_c_detected = ctrl_c_pressed;
                   5651:        
1.1.1.8   root     5652:        // some seconds may be passed in console
1.1       root     5653:        hardware_update();
                   5654: }
                   5655: 
                   5656: inline void msdos_int_21h_09h()
                   5657: {
1.1.1.21  root     5658:        msdos_stdio_reopen();
                   5659:        
1.1.1.20  root     5660:        process_t *process = msdos_process_info_get(current_psp);
                   5661:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5662:        
1.1.1.14  root     5663:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   5664:        int len = 0;
1.1       root     5665:        
1.1.1.14  root     5666:        while(str[len] != '$' && len < 0x10000) {
                   5667:                len++;
                   5668:        }
1.1.1.20  root     5669:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5670:                // stdout is redirected to file
1.1.1.20  root     5671:                msdos_write(fd, str, len);
1.1       root     5672:        } else {
                   5673:                for(int i = 0; i < len; i++) {
1.1.1.14  root     5674:                        msdos_putch(str[i]);
1.1       root     5675:                }
                   5676:        }
1.1.1.26  root     5677:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5678: }
                   5679: 
                   5680: inline void msdos_int_21h_0ah()
                   5681: {
1.1.1.3   root     5682:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     5683:        int max = mem[ofs] - 1;
                   5684:        UINT8 *buf = mem + ofs + 2;
                   5685:        int chr, p = 0;
                   5686:        
                   5687:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.26  root     5688:                if(ctrl_c_pressed) {
                   5689:                        p = 0;
                   5690:                        msdos_putch(chr);
                   5691:                        break;
                   5692:                } else if(chr == 0x00) {
1.1       root     5693:                        // skip 2nd byte
                   5694:                        msdos_getch();
                   5695:                } else if(chr == 0x08) {
                   5696:                        // back space
                   5697:                        if(p > 0) {
                   5698:                                p--;
1.1.1.20  root     5699:                                if(msdos_ctrl_code_check(buf[p])) {
                   5700:                                        msdos_putch(chr);
                   5701:                                        msdos_putch(chr);
                   5702:                                        msdos_putch(' ');
                   5703:                                        msdos_putch(' ');
                   5704:                                        msdos_putch(chr);
                   5705:                                        msdos_putch(chr);
                   5706:                                } else {
                   5707:                                        msdos_putch(chr);
                   5708:                                        msdos_putch(' ');
                   5709:                                        msdos_putch(chr);
                   5710:                                }
1.1       root     5711:                        }
                   5712:                } else if(p < max) {
                   5713:                        buf[p++] = chr;
                   5714:                        msdos_putch(chr);
                   5715:                }
                   5716:        }
                   5717:        buf[p] = 0x0d;
                   5718:        mem[ofs + 1] = p;
1.1.1.26  root     5719:        ctrl_c_detected = ctrl_c_pressed;
                   5720:        
1.1.1.8   root     5721:        // some seconds may be passed in console
1.1       root     5722:        hardware_update();
                   5723: }
                   5724: 
                   5725: inline void msdos_int_21h_0bh()
                   5726: {
                   5727:        if(msdos_kbhit()) {
                   5728:                REG8(AL) = 0xff;
                   5729:        } else {
                   5730:                REG8(AL) = 0x00;
1.1.1.14  root     5731:                maybe_idle();
1.1       root     5732:        }
1.1.1.26  root     5733:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5734: }
                   5735: 
                   5736: inline void msdos_int_21h_0ch()
                   5737: {
                   5738:        // clear key buffer
1.1.1.21  root     5739:        msdos_stdio_reopen();
                   5740:        
1.1.1.20  root     5741:        process_t *process = msdos_process_info_get(current_psp);
                   5742:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5743:        
                   5744:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5745:                // stdin is redirected to file
                   5746:        } else {
                   5747:                while(msdos_kbhit()) {
                   5748:                        msdos_getch();
                   5749:                }
                   5750:        }
                   5751:        
                   5752:        switch(REG8(AL)) {
                   5753:        case 0x01:
                   5754:                msdos_int_21h_01h();
                   5755:                break;
                   5756:        case 0x06:
                   5757:                msdos_int_21h_06h();
                   5758:                break;
                   5759:        case 0x07:
                   5760:                msdos_int_21h_07h();
                   5761:                break;
                   5762:        case 0x08:
                   5763:                msdos_int_21h_08h();
                   5764:                break;
                   5765:        case 0x0a:
                   5766:                msdos_int_21h_0ah();
                   5767:                break;
                   5768:        default:
1.1.1.22  root     5769: //             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));
                   5770: //             REG16(AX) = 0x01;
                   5771: //             m_CF = 1;
1.1       root     5772:                break;
                   5773:        }
                   5774: }
                   5775: 
                   5776: inline void msdos_int_21h_0dh()
                   5777: {
                   5778: }
                   5779: 
                   5780: inline void msdos_int_21h_0eh()
                   5781: {
                   5782:        if(REG8(DL) < 26) {
                   5783:                _chdrive(REG8(DL) + 1);
                   5784:                msdos_cds_update(REG8(DL));
1.1.1.23  root     5785:                msdos_sda_update(current_psp);
1.1       root     5786:        }
                   5787:        REG8(AL) = 26; // zdrive
                   5788: }
                   5789: 
1.1.1.14  root     5790: inline void msdos_int_21h_0fh()
                   5791: {
                   5792:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5793:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5794:        char *path = msdos_fcb_path(fcb);
                   5795:        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     5796:        
1.1.1.14  root     5797:        if(hFile == INVALID_HANDLE_VALUE) {
                   5798:                REG8(AL) = 0xff;
                   5799:        } else {
                   5800:                REG8(AL) = 0;
                   5801:                fcb->current_block = 0;
                   5802:                fcb->record_size = 128;
                   5803:                fcb->file_size = GetFileSize(hFile, NULL);
                   5804:                fcb->handle = hFile;
                   5805:                fcb->cur_record = 0;
                   5806:        }
                   5807: }
                   5808: 
                   5809: inline void msdos_int_21h_10h()
                   5810: {
                   5811:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5812:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5813:        
                   5814:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   5815: }
                   5816: 
1.1       root     5817: inline void msdos_int_21h_11h()
                   5818: {
1.1.1.3   root     5819:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5820:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5821:        
                   5822:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5823:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5824:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5825:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5826:        char *path = msdos_fcb_path(fcb);
                   5827:        WIN32_FIND_DATA fd;
                   5828:        
1.1.1.13  root     5829:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5830:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5831:                FindClose(dtainfo->find_handle);
                   5832:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5833:        }
                   5834:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     5835:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   5836:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     5837:        
1.1.1.14  root     5838:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   5839:                dtainfo->allowable_mask &= ~8;
1.1       root     5840:        }
1.1.1.14  root     5841:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   5842:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5843:                      !msdos_find_file_has_8dot3name(&fd)) {
                   5844:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5845:                                FindClose(dtainfo->find_handle);
                   5846:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5847:                                break;
                   5848:                        }
                   5849:                }
                   5850:        }
1.1.1.13  root     5851:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5852:                if(ext_fcb->flag == 0xff) {
                   5853:                        ext_find->flag = 0xff;
                   5854:                        memset(ext_find->reserved, 0, 5);
                   5855:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5856:                }
                   5857:                find->drive = _getdrive();
1.1.1.13  root     5858:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5859:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5860:                find->nt_res = 0;
                   5861:                msdos_find_file_conv_local_time(&fd);
                   5862:                find->create_time_ms = 0;
                   5863:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5864:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5865:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5866:                find->cluster_hi = find->cluster_lo = 0;
                   5867:                find->file_size = fd.nFileSizeLow;
                   5868:                REG8(AL) = 0x00;
1.1.1.14  root     5869:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5870:                if(ext_fcb->flag == 0xff) {
                   5871:                        ext_find->flag = 0xff;
                   5872:                        memset(ext_find->reserved, 0, 5);
                   5873:                        ext_find->attribute = 8;
                   5874:                }
                   5875:                find->drive = _getdrive();
                   5876:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5877:                find->attribute = 8;
                   5878:                find->nt_res = 0;
                   5879:                msdos_find_file_conv_local_time(&fd);
                   5880:                find->create_time_ms = 0;
                   5881:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5882:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5883:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5884:                find->cluster_hi = find->cluster_lo = 0;
                   5885:                find->file_size = 0;
1.1.1.14  root     5886:                dtainfo->allowable_mask &= ~8;
1.1       root     5887:                REG8(AL) = 0x00;
                   5888:        } else {
                   5889:                REG8(AL) = 0xff;
                   5890:        }
                   5891: }
                   5892: 
                   5893: inline void msdos_int_21h_12h()
                   5894: {
1.1.1.3   root     5895:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     5896: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5897:        
                   5898:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5899:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5900:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5901:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5902:        WIN32_FIND_DATA fd;
                   5903:        
1.1.1.13  root     5904:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5905:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5906:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     5907:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5908:                              !msdos_find_file_has_8dot3name(&fd)) {
                   5909:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5910:                                        FindClose(dtainfo->find_handle);
                   5911:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5912:                                        break;
                   5913:                                }
                   5914:                        }
                   5915:                } else {
1.1.1.13  root     5916:                        FindClose(dtainfo->find_handle);
                   5917:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5918:                }
                   5919:        }
1.1.1.13  root     5920:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5921:                if(ext_fcb->flag == 0xff) {
                   5922:                        ext_find->flag = 0xff;
                   5923:                        memset(ext_find->reserved, 0, 5);
                   5924:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5925:                }
                   5926:                find->drive = _getdrive();
1.1.1.13  root     5927:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5928:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5929:                find->nt_res = 0;
                   5930:                msdos_find_file_conv_local_time(&fd);
                   5931:                find->create_time_ms = 0;
                   5932:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5933:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5934:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5935:                find->cluster_hi = find->cluster_lo = 0;
                   5936:                find->file_size = fd.nFileSizeLow;
                   5937:                REG8(AL) = 0x00;
1.1.1.14  root     5938:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5939:                if(ext_fcb->flag == 0xff) {
                   5940:                        ext_find->flag = 0xff;
                   5941:                        memset(ext_find->reserved, 0, 5);
                   5942:                        ext_find->attribute = 8;
                   5943:                }
                   5944:                find->drive = _getdrive();
                   5945:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5946:                find->attribute = 8;
                   5947:                find->nt_res = 0;
                   5948:                msdos_find_file_conv_local_time(&fd);
                   5949:                find->create_time_ms = 0;
                   5950:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5951:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5952:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5953:                find->cluster_hi = find->cluster_lo = 0;
                   5954:                find->file_size = 0;
1.1.1.14  root     5955:                dtainfo->allowable_mask &= ~8;
1.1       root     5956:                REG8(AL) = 0x00;
                   5957:        } else {
                   5958:                REG8(AL) = 0xff;
                   5959:        }
                   5960: }
                   5961: 
                   5962: inline void msdos_int_21h_13h()
                   5963: {
1.1.1.3   root     5964:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     5965:                REG8(AL) = 0xff;
                   5966:        } else {
                   5967:                REG8(AL) = 0x00;
                   5968:        }
                   5969: }
                   5970: 
1.1.1.16  root     5971: inline void msdos_int_21h_14h()
                   5972: {
                   5973:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5974:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5975:        process_t *process = msdos_process_info_get(current_psp);
                   5976:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5977:        DWORD num = 0;
                   5978:        
                   5979:        memset(mem + dta_laddr, 0, fcb->record_size);
                   5980:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5981:                REG8(AL) = 1;
                   5982:        } else {
                   5983:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5984:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5985:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5986:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5987:        }
                   5988: }
                   5989: 
                   5990: inline void msdos_int_21h_15h()
1.1.1.14  root     5991: {
                   5992:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5993:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     5994:        process_t *process = msdos_process_info_get(current_psp);
                   5995:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5996:        DWORD num = 0;
1.1.1.14  root     5997:        
1.1.1.16  root     5998:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5999:                REG8(AL) = 1;
                   6000:        } else {
                   6001:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   6002:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   6003:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   6004:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   6005:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   6006:        }
                   6007: }
                   6008: 
                   6009: inline void msdos_int_21h_16h()
                   6010: {
                   6011:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6012:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     6013:        char *path = msdos_fcb_path(fcb);
                   6014:        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     6015:        
1.1.1.14  root     6016:        if(hFile == INVALID_HANDLE_VALUE) {
                   6017:                REG8(AL) = 0xff;
                   6018:        } else {
                   6019:                REG8(AL) = 0;
                   6020:                fcb->current_block = 0;
                   6021:                fcb->record_size = 128;
                   6022:                fcb->file_size = 0;
                   6023:                fcb->handle = hFile;
                   6024:                fcb->cur_record = 0;
                   6025:        }
                   6026: }
                   6027: 
1.1.1.16  root     6028: inline void msdos_int_21h_17h()
                   6029: {
                   6030:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6031:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   6032:        char *path_src = msdos_fcb_path(fcb_src);
                   6033:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   6034:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   6035:        char *path_dst = msdos_fcb_path(fcb_dst);
                   6036:        
                   6037:        if(rename(path_src, path_dst)) {
                   6038:                REG8(AL) = 0xff;
                   6039:        } else {
                   6040:                REG8(AL) = 0;
                   6041:        }
                   6042: }
                   6043: 
1.1       root     6044: inline void msdos_int_21h_18h()
                   6045: {
                   6046:        REG8(AL) = 0x00;
                   6047: }
                   6048: 
                   6049: inline void msdos_int_21h_19h()
                   6050: {
                   6051:        REG8(AL) = _getdrive() - 1;
                   6052: }
                   6053: 
                   6054: inline void msdos_int_21h_1ah()
                   6055: {
                   6056:        process_t *process = msdos_process_info_get(current_psp);
                   6057:        
                   6058:        process->dta.w.l = REG16(DX);
1.1.1.3   root     6059:        process->dta.w.h = SREG(DS);
1.1.1.23  root     6060:        msdos_sda_update(current_psp);
1.1       root     6061: }
                   6062: 
                   6063: inline void msdos_int_21h_1bh()
                   6064: {
                   6065:        int drive_num = _getdrive() - 1;
                   6066:        UINT16 seg, ofs;
                   6067:        
                   6068:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6069:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   6070:                REG8(AL) = dpb->highest_sector_num + 1;
                   6071:                REG16(CX) = dpb->bytes_per_sector;
                   6072:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     6073:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     6074:        } else {
                   6075:                REG8(AL) = 0xff;
1.1.1.3   root     6076:                m_CF = 1;
1.1       root     6077:        }
                   6078: 
                   6079: }
                   6080: 
                   6081: inline void msdos_int_21h_1ch()
                   6082: {
                   6083:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   6084:        UINT16 seg, ofs;
                   6085:        
                   6086:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6087:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   6088:                REG8(AL) = dpb->highest_sector_num + 1;
                   6089:                REG16(CX) = dpb->bytes_per_sector;
                   6090:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     6091:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     6092:        } else {
                   6093:                REG8(AL) = 0xff;
1.1.1.3   root     6094:                m_CF = 1;
1.1       root     6095:        }
                   6096: 
                   6097: }
                   6098: 
                   6099: inline void msdos_int_21h_1dh()
                   6100: {
                   6101:        REG8(AL) = 0;
                   6102: }
                   6103: 
                   6104: inline void msdos_int_21h_1eh()
                   6105: {
                   6106:        REG8(AL) = 0;
                   6107: }
                   6108: 
                   6109: inline void msdos_int_21h_1fh()
                   6110: {
                   6111:        int drive_num = _getdrive() - 1;
                   6112:        UINT16 seg, ofs;
                   6113:        
                   6114:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6115:                REG8(AL) = 0;
1.1.1.3   root     6116:                SREG(DS) = seg;
                   6117:                i386_load_segment_descriptor(DS);
1.1       root     6118:                REG16(BX) = ofs;
                   6119:        } else {
                   6120:                REG8(AL) = 0xff;
1.1.1.3   root     6121:                m_CF = 1;
1.1       root     6122:        }
                   6123: }
                   6124: 
                   6125: inline void msdos_int_21h_20h()
                   6126: {
                   6127:        REG8(AL) = 0;
                   6128: }
                   6129: 
1.1.1.14  root     6130: inline void msdos_int_21h_21h()
                   6131: {
                   6132:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6133:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6134:        
                   6135:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6136:                REG8(AL) = 1;
                   6137:        } else {
                   6138:                process_t *process = msdos_process_info_get(current_psp);
                   6139:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6140:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     6141:                DWORD num = 0;
1.1.1.14  root     6142:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   6143:                        REG8(AL) = 1;
                   6144:                } else {
                   6145:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6146:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     6147:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     6148:                }
                   6149:        }
                   6150: }
                   6151: 
                   6152: inline void msdos_int_21h_22h()
                   6153: {
                   6154:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6155:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6156:        
                   6157:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6158:                REG8(AL) = 0xff;
                   6159:        } else {
                   6160:                process_t *process = msdos_process_info_get(current_psp);
                   6161:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     6162:                DWORD num = 0;
1.1.1.14  root     6163:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   6164:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   6165:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6166:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     6167:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     6168:        }
                   6169: }
                   6170: 
1.1.1.16  root     6171: inline void msdos_int_21h_23h()
                   6172: {
                   6173:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6174:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6175:        char *path = msdos_fcb_path(fcb);
                   6176:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6177:        
                   6178:        if(hFile == INVALID_HANDLE_VALUE) {
                   6179:                REG8(AL) = 0xff;
                   6180:        } else {
                   6181:                UINT32 size = GetFileSize(hFile, NULL);
                   6182:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   6183:                REG8(AL) = 0;
                   6184:        }
                   6185: }
                   6186: 
                   6187: inline void msdos_int_21h_24h()
                   6188: {
                   6189:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6190:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6191:        
                   6192:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   6193: }
                   6194: 
1.1       root     6195: inline void msdos_int_21h_25h()
                   6196: {
                   6197:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     6198:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     6199: }
                   6200: 
                   6201: inline void msdos_int_21h_26h()
                   6202: {
                   6203:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   6204:        
                   6205:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   6206:        psp->first_mcb = REG16(DX) + 16;
                   6207:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6208:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6209:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6210:        psp->parent_psp = 0;
                   6211: }
                   6212: 
1.1.1.16  root     6213: inline void msdos_int_21h_27h()
                   6214: {
                   6215:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6216:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6217:        
                   6218:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6219:                REG8(AL) = 1;
                   6220:        } else {
                   6221:                process_t *process = msdos_process_info_get(current_psp);
                   6222:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6223:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   6224:                DWORD num = 0;
                   6225:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   6226:                        REG8(AL) = 1;
                   6227:                } else {
                   6228:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6229:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   6230:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   6231:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   6232:                }
                   6233:        }
                   6234: }
                   6235: 
                   6236: inline void msdos_int_21h_28h()
                   6237: {
                   6238:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6239:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6240:        
                   6241:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6242:                REG8(AL) = 0xff;
                   6243:        } else {
                   6244:                process_t *process = msdos_process_info_get(current_psp);
                   6245:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6246:                DWORD num = 0;
                   6247:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   6248:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   6249:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6250:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   6251:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   6252:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   6253:        }
                   6254: }
                   6255: 
1.1       root     6256: inline void msdos_int_21h_29h()
                   6257: {
1.1.1.20  root     6258:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   6259:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     6260:        UINT8 drv = 0;
                   6261:        char sep_chars[] = ":.;,=+";
                   6262:        char end_chars[] = "\\<>|/\"[]";
                   6263:        char spc_chars[] = " \t";
                   6264:        
1.1.1.20  root     6265:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   6266:        buffer[1023] = 0;
                   6267:        memset(name, 0x20, sizeof(name));
                   6268:        memset(ext, 0x20, sizeof(ext));
                   6269:        
1.1       root     6270:        if(REG8(AL) & 1) {
1.1.1.20  root     6271:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   6272:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     6273:                        ofs++;
                   6274:                }
                   6275:        }
1.1.1.20  root     6276:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     6277:        
1.1.1.24  root     6278:        if(buffer[ofs + 1] == ':') {
                   6279:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   6280:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     6281:                        ofs += 2;
1.1.1.24  root     6282:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   6283:                                ofs++;
                   6284:                        }
                   6285:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   6286:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     6287:                        ofs += 2;
1.1.1.24  root     6288:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   6289:                                ofs++;
                   6290:                        }
1.1       root     6291:                }
                   6292:        }
1.1.1.20  root     6293:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   6294:                UINT8 c = buffer[ofs];
                   6295:                if(is_kanji) {
                   6296:                        is_kanji = 0;
                   6297:                } else if(msdos_lead_byte_check(c)) {
                   6298:                        is_kanji = 1;
                   6299:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     6300:                        break;
                   6301:                } else if(c >= 'a' && c <= 'z') {
                   6302:                        c -= 0x20;
                   6303:                }
                   6304:                ofs++;
                   6305:                name[i] = c;
                   6306:        }
1.1.1.20  root     6307:        if(buffer[ofs] == '.') {
1.1       root     6308:                ofs++;
1.1.1.20  root     6309:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   6310:                        UINT8 c = buffer[ofs];
                   6311:                        if(is_kanji) {
                   6312:                                is_kanji = 0;
                   6313:                        } else if(msdos_lead_byte_check(c)) {
                   6314:                                is_kanji = 1;
                   6315:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     6316:                                break;
                   6317:                        } else if(c >= 'a' && c <= 'z') {
                   6318:                                c -= 0x20;
                   6319:                        }
                   6320:                        ofs++;
                   6321:                        ext[i] = c;
                   6322:                }
                   6323:        }
1.1.1.20  root     6324:        int si = REG16(SI) + ofs;
1.1.1.3   root     6325:        int ds = SREG(DS);
1.1       root     6326:        while(si > 0xffff) {
                   6327:                si -= 0x10;
                   6328:                ds++;
                   6329:        }
                   6330:        REG16(SI) = si;
1.1.1.3   root     6331:        SREG(DS) = ds;
                   6332:        i386_load_segment_descriptor(DS);
1.1       root     6333:        
1.1.1.3   root     6334:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     6335:        if(!(REG8(AL) & 2) || drv != 0) {
                   6336:                fcb[0] = drv;
                   6337:        }
                   6338:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   6339:                memcpy(fcb + 1, name, 8);
                   6340:        }
                   6341:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   6342:                memcpy(fcb + 9, ext, 3);
                   6343:        }
                   6344:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     6345:                if(fcb[i] == '*') {
                   6346:                        found_star = 1;
                   6347:                }
                   6348:                if(found_star) {
                   6349:                        fcb[i] = '?';
                   6350:                }
                   6351:        }
1.1.1.20  root     6352:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     6353:                if(fcb[i] == '*') {
                   6354:                        found_star = 1;
                   6355:                }
                   6356:                if(found_star) {
                   6357:                        fcb[i] = '?';
                   6358:                }
                   6359:        }
                   6360:        
                   6361:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   6362:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   6363:                        REG8(AL) = 0x01;
1.1.1.20  root     6364:                } else {
                   6365:                        REG8(AL) = 0x00;
1.1       root     6366:                }
                   6367:        } else {
                   6368:                REG8(AL) = 0xff;
                   6369:        }
                   6370: }
                   6371: 
                   6372: inline void msdos_int_21h_2ah()
                   6373: {
                   6374:        SYSTEMTIME sTime;
                   6375:        
                   6376:        GetLocalTime(&sTime);
                   6377:        REG16(CX) = sTime.wYear;
                   6378:        REG8(DH) = (UINT8)sTime.wMonth;
                   6379:        REG8(DL) = (UINT8)sTime.wDay;
                   6380:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   6381: }
                   6382: 
                   6383: inline void msdos_int_21h_2bh()
                   6384: {
1.1.1.14  root     6385:        REG8(AL) = 0xff;
1.1       root     6386: }
                   6387: 
                   6388: inline void msdos_int_21h_2ch()
                   6389: {
                   6390:        SYSTEMTIME sTime;
                   6391:        
                   6392:        GetLocalTime(&sTime);
                   6393:        REG8(CH) = (UINT8)sTime.wHour;
                   6394:        REG8(CL) = (UINT8)sTime.wMinute;
                   6395:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     6396:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     6397: }
                   6398: 
                   6399: inline void msdos_int_21h_2dh()
                   6400: {
                   6401:        REG8(AL) = 0x00;
                   6402: }
                   6403: 
                   6404: inline void msdos_int_21h_2eh()
                   6405: {
                   6406:        process_t *process = msdos_process_info_get(current_psp);
                   6407:        
                   6408:        process->verify = REG8(AL);
                   6409: }
                   6410: 
                   6411: inline void msdos_int_21h_2fh()
                   6412: {
                   6413:        process_t *process = msdos_process_info_get(current_psp);
                   6414:        
                   6415:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     6416:        SREG(ES) = process->dta.w.h;
                   6417:        i386_load_segment_descriptor(ES);
1.1       root     6418: }
                   6419: 
                   6420: inline void msdos_int_21h_30h()
                   6421: {
                   6422:        // Version Flag / OEM
1.1.1.27  root     6423:        if(REG8(AL) == 0x01) {
1.1.1.29  root     6424: #ifdef SUPPORT_HMA
                   6425:                REG16(BX) = 0x0000;
                   6426: #else
                   6427:                REG16(BX) = 0x1000; // DOS is in HMA
                   6428: #endif
1.1       root     6429:        } else {
1.1.1.27  root     6430:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   6431:                // but this is not correct on Windows 98 SE
                   6432: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   6433:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     6434:        }
1.1.1.27  root     6435:        REG16(CX) = 0x0000;
1.1.1.30  root     6436:        REG8(AL) = dos_major_version;   // 7
                   6437:        REG8(AH) = dos_minor_version;   // 10
1.1       root     6438: }
                   6439: 
                   6440: inline void msdos_int_21h_31h()
                   6441: {
1.1.1.29  root     6442:        try {
                   6443:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   6444:        } catch(...) {
                   6445:                // recover the broken mcb
                   6446:                int mcb_seg = current_psp - 1;
                   6447:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6448:                if(mcb_seg < (MEMORY_END >> 4)) {
                   6449:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
                   6450:                                mcb->mz = 'M';
                   6451:                        } else {
                   6452:                                mcb->mz = 'Z';
                   6453:                        }
1.1.1.30  root     6454:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
1.1.1.29  root     6455:                        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   6456:                } else {
                   6457:                        mcb->mz = 'Z';
1.1.1.30  root     6458:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     6459:                }
                   6460:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   6461:        }
1.1       root     6462:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   6463: }
                   6464: 
                   6465: inline void msdos_int_21h_32h()
                   6466: {
                   6467:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   6468:        UINT16 seg, ofs;
                   6469:        
                   6470:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6471:                REG8(AL) = 0;
1.1.1.3   root     6472:                SREG(DS) = seg;
                   6473:                i386_load_segment_descriptor(DS);
1.1       root     6474:                REG16(BX) = ofs;
                   6475:        } else {
                   6476:                REG8(AL) = 0xff;
1.1.1.3   root     6477:                m_CF = 1;
1.1       root     6478:        }
                   6479: }
                   6480: 
                   6481: inline void msdos_int_21h_33h()
                   6482: {
                   6483:        char path[MAX_PATH];
                   6484:        
                   6485:        switch(REG8(AL)) {
                   6486:        case 0x00:
1.1.1.26  root     6487:                REG8(DL) = ctrl_c_checking;
1.1       root     6488:                break;
                   6489:        case 0x01:
1.1.1.26  root     6490:                ctrl_c_checking = REG8(DL);
1.1       root     6491:                break;
                   6492:        case 0x05:
                   6493:                GetSystemDirectory(path, MAX_PATH);
                   6494:                if(path[0] >= 'a' && path[0] <= 'z') {
                   6495:                        REG8(DL) = path[0] - 'a' + 1;
                   6496:                } else {
                   6497:                        REG8(DL) = path[0] - 'A' + 1;
                   6498:                }
                   6499:                break;
                   6500:        case 0x06:
1.1.1.2   root     6501:                // MS-DOS version (7.10)
1.1       root     6502:                REG8(BL) = 7;
1.1.1.2   root     6503:                REG8(BH) = 10;
1.1       root     6504:                REG8(DL) = 0;
1.1.1.29  root     6505: #ifdef SUPPORT_HMA
                   6506:                REG8(DH) = 0x00;
                   6507: #else
                   6508:                REG8(DH) = 0x10; // DOS is in HMA
                   6509: #endif
1.1       root     6510:                break;
1.1.1.6   root     6511:        case 0x07:
                   6512:                if(REG8(DL) == 0) {
                   6513:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   6514:                } else if(REG8(DL) == 1) {
                   6515:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   6516:                }
                   6517:                break;
1.1       root     6518:        default:
1.1.1.22  root     6519:                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     6520:                REG16(AX) = 0x01;
1.1.1.3   root     6521:                m_CF = 1;
1.1       root     6522:                break;
                   6523:        }
                   6524: }
                   6525: 
1.1.1.23  root     6526: inline void msdos_int_21h_34h()
                   6527: {
                   6528:        SREG(ES) = SDA_TOP >> 4;
                   6529:        i386_load_segment_descriptor(ES);
                   6530:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   6531: }
                   6532: 
1.1       root     6533: inline void msdos_int_21h_35h()
                   6534: {
                   6535:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     6536:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   6537:        i386_load_segment_descriptor(ES);
1.1       root     6538: }
                   6539: 
                   6540: inline void msdos_int_21h_36h()
                   6541: {
                   6542:        struct _diskfree_t df = {0};
                   6543:        
                   6544:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   6545:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   6546:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     6547:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   6548:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     6549:        } else {
                   6550:                REG16(AX) = 0xffff;
                   6551:        }
                   6552: }
                   6553: 
                   6554: inline void msdos_int_21h_37h()
                   6555: {
1.1.1.22  root     6556:        static UINT8 dev_flag = 0xff;
1.1       root     6557:        
                   6558:        switch(REG8(AL)) {
                   6559:        case 0x00:
1.1.1.22  root     6560:                {
                   6561:                        process_t *process = msdos_process_info_get(current_psp);
                   6562:                        REG8(AL) = 0x00;
                   6563:                        REG8(DL) = process->switchar;
                   6564:                }
1.1       root     6565:                break;
                   6566:        case 0x01:
1.1.1.22  root     6567:                {
                   6568:                        process_t *process = msdos_process_info_get(current_psp);
                   6569:                        REG8(AL) = 0x00;
                   6570:                        process->switchar = REG8(DL);
1.1.1.23  root     6571:                        msdos_sda_update(current_psp);
1.1.1.22  root     6572:                }
                   6573:                break;
                   6574:        case 0x02:
                   6575:                REG8(DL) = dev_flag;
                   6576:                break;
                   6577:        case 0x03:
                   6578:                dev_flag = REG8(DL);
                   6579:                break;
                   6580:        case 0xd0:
                   6581:        case 0xd1:
                   6582:        case 0xd2:
                   6583:        case 0xd3:
                   6584:        case 0xd4:
                   6585:        case 0xd5:
                   6586:        case 0xd6:
                   6587:        case 0xd7:
                   6588:        case 0xdc:
                   6589:        case 0xdd:
                   6590:        case 0xde:
                   6591:        case 0xdf:
                   6592:                // diet ???
                   6593:                REG16(AX) = 1;
1.1       root     6594:                break;
                   6595:        default:
1.1.1.22  root     6596:                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     6597:                REG16(AX) = 1;
                   6598:                break;
                   6599:        }
                   6600: }
                   6601: 
1.1.1.19  root     6602: int get_country_info(country_info_t *ci)
1.1.1.17  root     6603: {
                   6604:        char LCdata[80];
                   6605:        
1.1.1.19  root     6606:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     6607:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   6608:        ci->currency_dec_digits = atoi(LCdata);
                   6609:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   6610:        ci->currency_format = *LCdata - '0';
                   6611:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   6612:        ci->date_format = *LCdata - '0';
                   6613:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   6614:        memcpy(&ci->currency_symbol, LCdata, 4);
                   6615:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   6616:        *ci->date_sep = *LCdata;
                   6617:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   6618:        *ci->dec_sep = *LCdata;
                   6619:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   6620:        *ci->list_sep = *LCdata;
                   6621:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   6622:        *ci->thou_sep = *LCdata;
                   6623:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   6624:        *ci->time_sep = *LCdata;
                   6625:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   6626:        if(strchr(LCdata, 'H') != NULL) {
                   6627:                ci->time_format = 1;
                   6628:        }
1.1.1.27  root     6629:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     6630:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     6631:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   6632:        return atoi(LCdata);
                   6633: }
                   6634: 
1.1.1.14  root     6635: inline void msdos_int_21h_38h()
                   6636: {
                   6637:        switch(REG8(AL)) {
                   6638:        case 0x00:
1.1.1.19  root     6639:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     6640:                break;
                   6641:        default:
1.1.1.22  root     6642:                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     6643:                REG16(AX) = 2;
                   6644:                m_CF = 1;
                   6645:                break;
                   6646:        }
                   6647: }
                   6648: 
1.1       root     6649: inline void msdos_int_21h_39h(int lfn)
                   6650: {
1.1.1.3   root     6651:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6652:                REG16(AX) = errno;
1.1.1.3   root     6653:                m_CF = 1;
1.1       root     6654:        }
                   6655: }
                   6656: 
                   6657: inline void msdos_int_21h_3ah(int lfn)
                   6658: {
1.1.1.3   root     6659:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6660:                REG16(AX) = errno;
1.1.1.3   root     6661:                m_CF = 1;
1.1       root     6662:        }
                   6663: }
                   6664: 
                   6665: inline void msdos_int_21h_3bh(int lfn)
                   6666: {
1.1.1.3   root     6667:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     6668:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     6669:                m_CF = 1;
1.1       root     6670:        }
                   6671: }
                   6672: 
                   6673: inline void msdos_int_21h_3ch()
                   6674: {
1.1.1.3   root     6675:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6676:        int attr = GetFileAttributes(path);
1.1.1.29  root     6677:        int fd = -1, c;
1.1.1.11  root     6678:        UINT16 info;
1.1       root     6679:        
1.1.1.11  root     6680:        if(msdos_is_con_path(path)) {
                   6681:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   6682:                info = 0x80d3;
1.1.1.29  root     6683:        } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   6684:                if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), _O_WRONLY | _O_BINARY)) == -1) {
                   6685:                        fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6686:                }
1.1.1.14  root     6687:                info = 0x80d3;
1.1.1.29  root     6688:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     6689:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6690:                info = 0x80d3;
1.1       root     6691:        } else {
                   6692:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     6693:                info = msdos_drive_number(path);
1.1       root     6694:        }
                   6695:        if(fd != -1) {
                   6696:                if(attr == -1) {
                   6697:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   6698:                }
                   6699:                SetFileAttributes(path, attr);
                   6700:                REG16(AX) = fd;
1.1.1.11  root     6701:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     6702:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6703:        } else {
                   6704:                REG16(AX) = errno;
1.1.1.3   root     6705:                m_CF = 1;
1.1       root     6706:        }
                   6707: }
                   6708: 
                   6709: inline void msdos_int_21h_3dh()
                   6710: {
1.1.1.3   root     6711:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6712:        int mode = REG8(AL) & 0x03;
1.1.1.29  root     6713:        int fd = -1, c;
1.1.1.11  root     6714:        UINT16 info;
1.1       root     6715:        
                   6716:        if(mode < 0x03) {
1.1.1.11  root     6717:                if(msdos_is_con_path(path)) {
1.1.1.13  root     6718:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     6719:                        info = 0x80d3;
1.1.1.29  root     6720:                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   6721:                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   6722:                                fd = msdos_open("NUL", file_mode[mode].mode);
                   6723:                        }
1.1.1.14  root     6724:                        info = 0x80d3;
1.1.1.29  root     6725:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     6726:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   6727:                        info = 0x80d3;
1.1.1.11  root     6728:                } else {
1.1.1.13  root     6729:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     6730:                        info = msdos_drive_number(path);
                   6731:                }
1.1       root     6732:                if(fd != -1) {
                   6733:                        REG16(AX) = fd;
1.1.1.11  root     6734:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     6735:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6736:                } else {
                   6737:                        REG16(AX) = errno;
1.1.1.3   root     6738:                        m_CF = 1;
1.1       root     6739:                }
                   6740:        } else {
                   6741:                REG16(AX) = 0x0c;
1.1.1.3   root     6742:                m_CF = 1;
1.1       root     6743:        }
                   6744: }
                   6745: 
                   6746: inline void msdos_int_21h_3eh()
                   6747: {
                   6748:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6749:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6750:        
1.1.1.20  root     6751:        if(fd < process->max_files && file_handler[fd].valid) {
                   6752:                _close(fd);
                   6753:                msdos_file_handler_close(fd);
                   6754:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     6755:        } else {
                   6756:                REG16(AX) = 0x06;
1.1.1.3   root     6757:                m_CF = 1;
1.1       root     6758:        }
                   6759: }
                   6760: 
                   6761: inline void msdos_int_21h_3fh()
                   6762: {
                   6763:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6764:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6765:        
1.1.1.20  root     6766:        if(fd < process->max_files && file_handler[fd].valid) {
                   6767:                if(file_mode[file_handler[fd].mode].in) {
                   6768:                        if(file_handler[fd].atty) {
1.1       root     6769:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     6770:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     6771:                                int max = REG16(CX);
                   6772:                                int p = 0;
                   6773:                                
                   6774:                                while(max > p) {
                   6775:                                        int chr = msdos_getch();
                   6776:                                        
1.1.1.26  root     6777:                                        if(ctrl_c_pressed) {
                   6778:                                                p = 0;
                   6779:                                                buf[p++] = 0x0d;
                   6780:                                                if(max > p) {
                   6781:                                                        buf[p++] = 0x0a;
                   6782:                                                }
                   6783:                                                msdos_putch(chr);
                   6784:                                                msdos_putch('\n');
                   6785:                                                break;
                   6786:                                        } else if(chr == 0x00) {
1.1       root     6787:                                                // skip 2nd byte
                   6788:                                                msdos_getch();
                   6789:                                        } else if(chr == 0x0d) {
                   6790:                                                // carriage return
                   6791:                                                buf[p++] = 0x0d;
                   6792:                                                if(max > p) {
                   6793:                                                        buf[p++] = 0x0a;
                   6794:                                                }
1.1.1.14  root     6795:                                                msdos_putch('\n');
1.1       root     6796:                                                break;
                   6797:                                        } else if(chr == 0x08) {
                   6798:                                                // back space
                   6799:                                                if(p > 0) {
                   6800:                                                        p--;
1.1.1.20  root     6801:                                                        if(msdos_ctrl_code_check(buf[p])) {
                   6802:                                                                msdos_putch(chr);
                   6803:                                                                msdos_putch(chr);
                   6804:                                                                msdos_putch(' ');
                   6805:                                                                msdos_putch(' ');
                   6806:                                                                msdos_putch(chr);
                   6807:                                                                msdos_putch(chr);
                   6808:                                                        } else {
                   6809:                                                                msdos_putch(chr);
                   6810:                                                                msdos_putch(' ');
                   6811:                                                                msdos_putch(chr);
                   6812:                                                        }
1.1       root     6813:                                                }
                   6814:                                        } else {
                   6815:                                                buf[p++] = chr;
                   6816:                                                msdos_putch(chr);
                   6817:                                        }
                   6818:                                }
                   6819:                                REG16(AX) = p;
1.1.1.26  root     6820:                                
1.1.1.8   root     6821:                                // some seconds may be passed in console
1.1       root     6822:                                hardware_update();
                   6823:                        } else {
1.1.1.20  root     6824:                                REG16(AX) = _read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6825:                        }
                   6826:                } else {
                   6827:                        REG16(AX) = 0x05;
1.1.1.3   root     6828:                        m_CF = 1;
1.1       root     6829:                }
                   6830:        } else {
                   6831:                REG16(AX) = 0x06;
1.1.1.3   root     6832:                m_CF = 1;
1.1       root     6833:        }
                   6834: }
                   6835: 
                   6836: inline void msdos_int_21h_40h()
                   6837: {
                   6838:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6839:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6840:        
1.1.1.20  root     6841:        if(fd < process->max_files && file_handler[fd].valid) {
                   6842:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     6843:                        if(REG16(CX)) {
1.1.1.20  root     6844:                                if(file_handler[fd].atty) {
1.1       root     6845:                                        // BX is stdout/stderr or is redirected to stdout
                   6846:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     6847:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     6848:                                        }
                   6849:                                        REG16(AX) = REG16(CX);
                   6850:                                } else {
1.1.1.20  root     6851:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6852:                                }
                   6853:                        } else {
1.1.1.20  root     6854:                                UINT32 pos = _tell(fd);
                   6855:                                _lseek(fd, 0, SEEK_END);
                   6856:                                UINT32 size = _tell(fd);
1.1.1.12  root     6857:                                if(pos < size) {
1.1.1.20  root     6858:                                        _lseek(fd, pos, SEEK_SET);
                   6859:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     6860:                                } else {
                   6861:                                        for(UINT32 i = size; i < pos; i++) {
                   6862:                                                UINT8 tmp = 0;
1.1.1.23  root     6863:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     6864:                                        }
1.1.1.20  root     6865:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     6866:                                }
1.1.1.23  root     6867:                                REG16(AX) = 0;
1.1       root     6868:                        }
                   6869:                } else {
                   6870:                        REG16(AX) = 0x05;
1.1.1.3   root     6871:                        m_CF = 1;
1.1       root     6872:                }
                   6873:        } else {
                   6874:                REG16(AX) = 0x06;
1.1.1.3   root     6875:                m_CF = 1;
1.1       root     6876:        }
                   6877: }
                   6878: 
                   6879: inline void msdos_int_21h_41h(int lfn)
                   6880: {
1.1.1.3   root     6881:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6882:                REG16(AX) = errno;
1.1.1.3   root     6883:                m_CF = 1;
1.1       root     6884:        }
                   6885: }
                   6886: 
                   6887: inline void msdos_int_21h_42h()
                   6888: {
                   6889:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6890:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6891:        
1.1.1.20  root     6892:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     6893:                if(REG8(AL) < 0x03) {
                   6894:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     6895:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   6896:                        UINT32 pos = _tell(fd);
1.1       root     6897:                        REG16(AX) = pos & 0xffff;
                   6898:                        REG16(DX) = (pos >> 16);
                   6899:                } else {
                   6900:                        REG16(AX) = 0x01;
1.1.1.3   root     6901:                        m_CF = 1;
1.1       root     6902:                }
                   6903:        } else {
                   6904:                REG16(AX) = 0x06;
1.1.1.3   root     6905:                m_CF = 1;
1.1       root     6906:        }
                   6907: }
                   6908: 
                   6909: inline void msdos_int_21h_43h(int lfn)
                   6910: {
1.1.1.3   root     6911:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     6912:        int attr;
                   6913:        
1.1.1.14  root     6914:        if(!lfn && REG8(AL) > 2) {
                   6915:                REG16(AX) = 0x01;
                   6916:                m_CF = 1;
                   6917:                return;
                   6918:        }
                   6919:        switch(REG8(lfn ? BL : AL)) {
1.1       root     6920:        case 0x00:
                   6921:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     6922:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   6923:                } else {
                   6924:                        REG16(AX) = (UINT16)GetLastError();
                   6925:                        m_CF = 1;
                   6926:                }
                   6927:                break;
                   6928:        case 0x01:
                   6929:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   6930:                        REG16(AX) = (UINT16)GetLastError();
                   6931:                        m_CF = 1;
                   6932:                }
                   6933:                break;
                   6934:        case 0x02:
                   6935:                {
                   6936:                        DWORD size = GetCompressedFileSize(path, NULL);
                   6937:                        if(size != INVALID_FILE_SIZE) {
                   6938:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   6939:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   6940:                                        // this isn't correct if the file is in the NTFS MFT
                   6941:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   6942:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   6943:                                        }
                   6944:                                }
                   6945:                                REG16(AX) = LOWORD(size);
                   6946:                                REG16(DX) = HIWORD(size);
                   6947:                        } else {
                   6948:                                REG16(AX) = (UINT16)GetLastError();
                   6949:                                m_CF = 1;
1.1       root     6950:                        }
1.1.1.14  root     6951:                }
                   6952:                break;
                   6953:        case 0x03:
                   6954:        case 0x05:
                   6955:        case 0x07:
                   6956:                {
                   6957:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6958:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6959:                                FILETIME local, time;
                   6960:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   6961:                                if(REG8(BL) == 7) {
                   6962:                                        ULARGE_INTEGER hund;
                   6963:                                        hund.LowPart = local.dwLowDateTime;
                   6964:                                        hund.HighPart = local.dwHighDateTime;
                   6965:                                        hund.QuadPart += REG16(SI) * 100000;
                   6966:                                        local.dwLowDateTime = hund.LowPart;
                   6967:                                        local.dwHighDateTime = hund.HighPart;
                   6968:                                }
                   6969:                                LocalFileTimeToFileTime(&local, &time);
                   6970:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   6971:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   6972:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   6973:                                        REG16(AX) = (UINT16)GetLastError();
                   6974:                                        m_CF = 1;
                   6975:                                }
                   6976:                                CloseHandle(hFile);
                   6977:                        } else {
                   6978:                                REG16(AX) = (UINT16)GetLastError();
                   6979:                                m_CF = 1;
1.1       root     6980:                        }
1.1.1.14  root     6981:                }
                   6982:                break;
                   6983:        case 0x04:
                   6984:        case 0x06:
                   6985:        case 0x08:
                   6986:                {
                   6987:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   6988:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   6989:                                FILETIME *time, local;
                   6990:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   6991:                                                   0x06 ? &fad.ftLastAccessTime :
                   6992:                                                          &fad.ftCreationTime;
                   6993:                                FileTimeToLocalFileTime(time, &local);
                   6994:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   6995:                                if(REG8(BL) == 0x08) {
                   6996:                                        ULARGE_INTEGER hund;
                   6997:                                        hund.LowPart = local.dwLowDateTime;
                   6998:                                        hund.HighPart = local.dwHighDateTime;
                   6999:                                        hund.QuadPart /= 100000;
                   7000:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   7001:                                }
                   7002:                        } else {
                   7003:                                REG16(AX) = (UINT16)GetLastError();
                   7004:                                m_CF = 1;
1.1       root     7005:                        }
1.1.1.14  root     7006:                }
                   7007:                break;
                   7008:        default:
1.1.1.22  root     7009:                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     7010:                REG16(AX) = 0x01;
                   7011:                m_CF = 1;
                   7012:                break;
                   7013:        }
                   7014: }
                   7015: 
                   7016: inline void msdos_int_21h_44h()
                   7017: {
1.1.1.22  root     7018:        static UINT16 iteration_count = 0;
                   7019:        
1.1.1.20  root     7020:        process_t *process = msdos_process_info_get(current_psp);
                   7021:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7022:        
1.1.1.14  root     7023:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   7024:        
                   7025:        switch(REG8(AL)) {
                   7026:        case 0x00:
                   7027:        case 0x01:
                   7028:        case 0x02:
                   7029:        case 0x03:
                   7030:        case 0x04:
                   7031:        case 0x05:
                   7032:        case 0x06:
                   7033:        case 0x07:
1.1.1.20  root     7034:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   7035:                        REG16(AX) = 0x06;
                   7036:                        m_CF = 1;
                   7037:                        return;
1.1.1.14  root     7038:                }
                   7039:                break;
                   7040:        case 0x08:
                   7041:        case 0x09:
                   7042:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   7043:                        // invalid drive number
                   7044:                        REG16(AX) = 0x0f;
                   7045:                        m_CF = 1;
                   7046:                        return;
                   7047:                } else {
                   7048:                        if(REG8(BL) == 0) {
                   7049:                                val = GetDriveType(NULL);
                   7050:                        } else {
                   7051:                                char tmp[8];
                   7052:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   7053:                                val = GetDriveType(tmp);
                   7054:                        }
                   7055:                        if(val == DRIVE_NO_ROOT_DIR) {
                   7056:                                // no drive
                   7057:                                REG16(AX) = 0x0f;
                   7058:                                m_CF = 1;
                   7059:                                return;
1.1       root     7060:                        }
                   7061:                }
                   7062:                break;
                   7063:        }
                   7064:        switch(REG8(AL)) {
                   7065:        case 0x00: // get ioctrl data
1.1.1.20  root     7066:                REG16(DX) = file_handler[fd].info;
1.1       root     7067:                break;
                   7068:        case 0x01: // set ioctrl data
1.1.1.20  root     7069:                file_handler[fd].info |= REG8(DL);
1.1       root     7070:                break;
                   7071:        case 0x02: // recv from character device
                   7072:        case 0x03: // send to character device
                   7073:        case 0x04: // recv from block device
                   7074:        case 0x05: // send to block device
                   7075:                REG16(AX) = 0x05;
1.1.1.3   root     7076:                m_CF = 1;
1.1       root     7077:                break;
                   7078:        case 0x06: // get read status
1.1.1.20  root     7079:                if(file_mode[file_handler[fd].mode].in) {
                   7080:                        if(file_handler[fd].atty) {
1.1.1.14  root     7081:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     7082:                        } else {
1.1.1.20  root     7083:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     7084:                        }
1.1.1.14  root     7085:                } else {
                   7086:                        REG8(AL) = 0x00;
1.1       root     7087:                }
                   7088:                break;
                   7089:        case 0x07: // get write status
1.1.1.20  root     7090:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     7091:                        REG8(AL) = 0xff;
                   7092:                } else {
                   7093:                        REG8(AL) = 0x00;
1.1       root     7094:                }
                   7095:                break;
                   7096:        case 0x08: // check removable drive
1.1.1.14  root     7097:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   7098:                        // removable drive
                   7099:                        REG16(AX) = 0x00;
1.1       root     7100:                } else {
1.1.1.14  root     7101:                        // fixed drive
                   7102:                        REG16(AX) = 0x01;
1.1       root     7103:                }
                   7104:                break;
                   7105:        case 0x09: // check remote drive
1.1.1.14  root     7106:                if(val == DRIVE_REMOTE) {
                   7107:                        // remote drive
                   7108:                        REG16(DX) = 0x1000;
1.1       root     7109:                } else {
1.1.1.14  root     7110:                        // local drive
                   7111:                        REG16(DX) = 0x00;
1.1       root     7112:                }
                   7113:                break;
1.1.1.21  root     7114:        case 0x0a: // check remote handle
                   7115:                REG16(DX) = 0x00; // FIXME
                   7116:                break;
1.1       root     7117:        case 0x0b: // set retry count
                   7118:                break;
1.1.1.22  root     7119:        case 0x0c: // generic character device request
                   7120:                if(REG8(CL) == 0x45) {
                   7121:                        // set iteration (retry) count
                   7122:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   7123:                } else if(REG8(CL) == 0x4a) {
                   7124:                        // select code page
                   7125:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   7126:                        msdos_nls_tables_update();
                   7127:                } else if(REG8(CL) == 0x65) {
                   7128:                        // get iteration (retry) count
                   7129:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   7130:                } else if(REG8(CL) == 0x6a) {
                   7131:                        // query selected code page
                   7132:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   7133:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   7134:                        
                   7135:                        CPINFO info;
                   7136:                        GetCPInfo(active_code_page, &info);
                   7137:                        
                   7138:                        if(info.MaxCharSize != 1) {
                   7139:                                for(int i = 0;; i++) {
                   7140:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   7141:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   7142:                                        
                   7143:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   7144:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   7145:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   7146:                                        
                   7147:                                        if(lo == 0 && hi == 0) {
                   7148:                                                break;
                   7149:                                        }
                   7150:                                }
                   7151:                        }
                   7152:                } else if(REG8(CL) == 0x7f) {
                   7153:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   7154:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   7155:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   7156:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   7157:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   7158:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   7159:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   7160:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   7161:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   7162:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   7163:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   7164:                } else {
                   7165:                        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));
                   7166:                        REG16(AX) = 0x01; // invalid function
                   7167:                        m_CF = 1;
                   7168:                }
                   7169:                break;
                   7170:        case 0x0d: // generic block device request
                   7171:                if(REG8(CL) == 0x40) {
                   7172:                        // set device parameters
                   7173:                } else if(REG8(CL) == 0x46) {
                   7174:                        // set volume serial number
                   7175:                } else if(REG8(CL) == 0x4a) {
                   7176:                        // lock logical volume
                   7177:                } else if(REG8(CL) == 0x4b) {
                   7178:                        // lock physical volume
                   7179:                } else if(REG8(CL) == 0x60) {
                   7180:                        // get device parameters
                   7181:                        char dev[] = "\\\\.\\A:";
                   7182:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   7183:                        
                   7184:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7185:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7186:                                DISK_GEOMETRY geo;
                   7187:                                DWORD dwSize;
                   7188:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   7189:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   7190:                                        switch(geo.MediaType) {
                   7191:                                        case F5_360_512:
                   7192:                                        case F5_320_512:
                   7193:                                        case F5_320_1024:
                   7194:                                        case F5_180_512:
                   7195:                                        case F5_160_512:
                   7196:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   7197:                                                break;
                   7198:                                        case F5_1Pt2_512:
                   7199:                                        case F3_1Pt2_512:
                   7200:                                        case F3_1Pt23_1024:
                   7201:                                        case F5_1Pt23_1024:
                   7202:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   7203:                                                break;
                   7204:                                        case F3_720_512:
                   7205:                                        case F3_640_512:
                   7206:                                        case F5_640_512:
                   7207:                                        case F5_720_512:
                   7208:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   7209:                                                break;
                   7210:                                        case F8_256_128:
                   7211:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   7212:                                                break;
                   7213:                                        case FixedMedia:
                   7214:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   7215:                                                break;
                   7216:                                        case F3_1Pt44_512:
                   7217:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   7218:                                                break;
                   7219:                                        case F3_2Pt88_512:
                   7220:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   7221:                                                break;
                   7222:                                        default:
                   7223:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   7224: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   7225:                                                break;
                   7226:                                        }
                   7227:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   7228:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   7229:                                        switch(geo.MediaType) {
                   7230:                                        case F5_360_512:
                   7231:                                        case F5_320_512:
                   7232:                                        case F5_320_1024:
                   7233:                                        case F5_180_512:
                   7234:                                        case F5_160_512:
                   7235:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   7236:                                                break;
                   7237:                                        default:
                   7238:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   7239:                                                break;
                   7240:                                        }
                   7241:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   7242:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   7243:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   7244:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   7245:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   7246:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   7247:                                        switch(geo.MediaType) {
                   7248:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   7249:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   7250:                                                break;
                   7251:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   7252:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   7253:                                                break;
                   7254:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   7255:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   7256:                                                break;
                   7257:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   7258:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   7259:                                                break;
                   7260:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   7261:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   7262:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   7263:                                                break;
                   7264:                                        case FixedMedia:        // hard disk
                   7265:                                        case RemovableMedia:
                   7266:                                        case Unknown:
                   7267:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   7268:                                                break;
                   7269:                                        default:
                   7270:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   7271:                                                break;
                   7272:                                        }
                   7273:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   7274:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   7275:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   7276:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   7277:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   7278:                                        // 21h  BYTE    device type
                   7279:                                        // 22h  WORD    device attributes (removable or not, etc)
                   7280:                                } else {
                   7281:                                        REG16(AX) = 0x0f; // invalid drive
                   7282:                                        m_CF = 1;
                   7283:                                }
                   7284:                                CloseHandle(hFile);
                   7285:                        } else {
                   7286:                                REG16(AX) = 0x0f; // invalid drive
                   7287:                                m_CF = 1;
                   7288:                        }
                   7289:                } else if(REG8(CL) == 0x66) {
                   7290:                        // get volume serial number
                   7291:                        char path[] = "A:\\";
                   7292:                        char volume_label[MAX_PATH];
                   7293:                        DWORD serial_number = 0;
                   7294:                        char file_system[MAX_PATH];
                   7295:                        
                   7296:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   7297:                        
                   7298:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   7299:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   7300:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   7301:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   7302:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   7303:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   7304:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   7305:                        } else {
                   7306:                                REG16(AX) = 0x0f; // invalid drive
                   7307:                                m_CF = 1;
                   7308:                        }
                   7309:                } else if(REG8(CL) == 0x67) {
                   7310:                        // get access flag
                   7311:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   7312:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   7313:                } else if(REG8(CL) == 0x68) {
                   7314:                        // sense media type
                   7315:                        char dev[64];
                   7316:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7317:                        
                   7318:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7319:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7320:                                DISK_GEOMETRY geo;
                   7321:                                DWORD dwSize;
                   7322:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   7323:                                        switch(geo.MediaType) {
                   7324:                                        case F3_720_512:
                   7325:                                        case F5_720_512:
                   7326:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7327:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   7328:                                                break;
                   7329:                                        case F3_1Pt44_512:
                   7330:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7331:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   7332:                                                break;
                   7333:                                        case F3_2Pt88_512:
                   7334:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7335:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   7336:                                                break;
                   7337:                                        default:
                   7338:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   7339:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   7340:                                                break;
                   7341:                                        }
                   7342:                                } else {
                   7343:                                        REG16(AX) = 0x0f; // invalid drive
                   7344:                                        m_CF = 1;
                   7345:                                }
                   7346:                                CloseHandle(hFile);
                   7347:                        } else {
                   7348:                                REG16(AX) = 0x0f; // invalid drive
                   7349:                                m_CF = 1;
                   7350:                        }
                   7351:                } else if(REG8(CL) == 0x6a) {
                   7352:                        // unlock logical volume
                   7353:                } else if(REG8(CL) == 0x6b) {
                   7354:                        // unlock physical volume
                   7355:                } else {
                   7356:                        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));
                   7357:                        REG16(AX) = 0x01; // invalid function
                   7358:                        m_CF = 1;
                   7359:                }
                   7360:                break;
                   7361:        case 0x0e: // get logical drive map
                   7362:                {
                   7363:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7364:                        if(!(GetLogicalDrives() & bits)) {
                   7365:                                REG16(AX) = 0x0f; // invalid drive
                   7366:                                m_CF = 1;
                   7367:                        } else {
                   7368:                                REG8(AL) = 0;
                   7369:                        }
                   7370:                }
                   7371:                break;
                   7372:        case 0x0f: // set logical drive map
                   7373:                {
                   7374:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7375:                        if(!(GetLogicalDrives() & bits)) {
                   7376:                                REG16(AX) = 0x0f; // invalid drive
                   7377:                                m_CF = 1;
                   7378:                        }
                   7379:                }
                   7380:                break;
                   7381:        case 0x10: // query generic ioctrl capability (handle)
                   7382:                switch(REG8(CL)) {
                   7383:                case 0x45:
                   7384:                case 0x4a:
                   7385:                case 0x65:
                   7386:                case 0x6a:
                   7387:                case 0x7f:
                   7388:                        REG16(AX) = 0x0000; // supported
                   7389:                        break;
                   7390:                default:
                   7391:                        REG8(AL) = 0x01; // ioctl capability not available
                   7392:                        m_CF = 1;
                   7393:                        break;
                   7394:                }
                   7395:                break;
                   7396:        case 0x11: // query generic ioctrl capability (drive)
                   7397:                switch(REG8(CL)) {
                   7398:                case 0x40:
                   7399:                case 0x46:
                   7400:                case 0x4a:
                   7401:                case 0x4b:
                   7402:                case 0x60:
                   7403:                case 0x66:
                   7404:                case 0x67:
                   7405:                case 0x68:
                   7406:                case 0x6a:
                   7407:                case 0x6b:
                   7408:                        REG16(AX) = 0x0000; // supported
                   7409:                        break;
                   7410:                default:
                   7411:                        REG8(AL) = 0x01; // ioctl capability not available
                   7412:                        m_CF = 1;
                   7413:                        break;
                   7414:                }
                   7415:                break;
                   7416:        case 0x12: // determine dos type
                   7417:        case 0x51: // concurrent dos v3.2+ - installation check
                   7418:        case 0x52: // determine dos type/get dr dos versuin
                   7419:                REG16(AX) = 0x01; // this  is not DR-DOS
                   7420:                m_CF = 1;
                   7421:                break;
1.1       root     7422:        default:
1.1.1.22  root     7423:                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     7424:                REG16(AX) = 0x01;
1.1.1.3   root     7425:                m_CF = 1;
1.1       root     7426:                break;
                   7427:        }
                   7428: }
                   7429: 
                   7430: inline void msdos_int_21h_45h()
                   7431: {
                   7432:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7433:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7434:        
1.1.1.20  root     7435:        if(fd < process->max_files && file_handler[fd].valid) {
                   7436:                int dup_fd = _dup(fd);
                   7437:                if(dup_fd != -1) {
                   7438:                        REG16(AX) = dup_fd;
                   7439:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7440: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7441:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7442:                } else {
                   7443:                        REG16(AX) = errno;
1.1.1.3   root     7444:                        m_CF = 1;
1.1       root     7445:                }
                   7446:        } else {
                   7447:                REG16(AX) = 0x06;
1.1.1.3   root     7448:                m_CF = 1;
1.1       root     7449:        }
                   7450: }
                   7451: 
                   7452: inline void msdos_int_21h_46h()
                   7453: {
                   7454:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7455:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7456:        int dup_fd = REG16(CX);
                   7457:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     7458:        
1.1.1.20  root     7459:        if(REG16(BX) == REG16(CX)) {
                   7460:                REG16(AX) = 0x06;
                   7461:                m_CF = 1;
                   7462:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   7463:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   7464:                        _close(tmp_fd);
                   7465:                        msdos_file_handler_close(tmp_fd);
                   7466:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   7467:                }
                   7468:                if(_dup2(fd, dup_fd) != -1) {
                   7469:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7470: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7471:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7472:                } else {
                   7473:                        REG16(AX) = errno;
1.1.1.3   root     7474:                        m_CF = 1;
1.1       root     7475:                }
                   7476:        } else {
                   7477:                REG16(AX) = 0x06;
1.1.1.3   root     7478:                m_CF = 1;
1.1       root     7479:        }
                   7480: }
                   7481: 
                   7482: inline void msdos_int_21h_47h(int lfn)
                   7483: {
                   7484:        char path[MAX_PATH];
                   7485:        
                   7486:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   7487:                if(path[1] == ':') {
                   7488:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     7489:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     7490:                } else {
1.1.1.3   root     7491:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     7492:                }
                   7493:        } else {
                   7494:                REG16(AX) = errno;
1.1.1.3   root     7495:                m_CF = 1;
1.1       root     7496:        }
                   7497: }
                   7498: 
                   7499: inline void msdos_int_21h_48h()
                   7500: {
1.1.1.19  root     7501:        int seg, umb_linked;
1.1       root     7502:        
1.1.1.8   root     7503:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     7504:                // unlink umb not to allocate memory in umb
                   7505:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   7506:                        msdos_mem_unlink_umb();
                   7507:                }
1.1.1.8   root     7508:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7509:                        REG16(AX) = seg;
                   7510:                } else {
                   7511:                        REG16(AX) = 0x08;
                   7512:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   7513:                        m_CF = 1;
                   7514:                }
1.1.1.19  root     7515:                if(umb_linked != 0) {
                   7516:                        msdos_mem_link_umb();
                   7517:                }
1.1.1.8   root     7518:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   7519:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7520:                        REG16(AX) = seg;
                   7521:                } else {
                   7522:                        REG16(AX) = 0x08;
                   7523:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   7524:                        m_CF = 1;
                   7525:                }
                   7526:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   7527:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7528:                        REG16(AX) = seg;
                   7529:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7530:                        REG16(AX) = seg;
                   7531:                } else {
                   7532:                        REG16(AX) = 0x08;
                   7533:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   7534:                        m_CF = 1;
                   7535:                }
1.1       root     7536:        }
                   7537: }
                   7538: 
                   7539: inline void msdos_int_21h_49h()
                   7540: {
1.1.1.14  root     7541:        int mcb_seg = SREG(ES) - 1;
                   7542:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   7543:        
                   7544:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7545:                msdos_mem_free(SREG(ES));
                   7546:        } else {
1.1.1.28  root     7547:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.14  root     7548:                m_CF = 1;
                   7549:        }
1.1       root     7550: }
                   7551: 
                   7552: inline void msdos_int_21h_4ah()
                   7553: {
1.1.1.14  root     7554:        int mcb_seg = SREG(ES) - 1;
                   7555:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     7556:        int max_paragraphs;
                   7557:        
1.1.1.14  root     7558:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7559:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   7560:                        REG16(AX) = 0x08;
                   7561:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   7562:                        m_CF = 1;
                   7563:                }
                   7564:        } else {
1.1.1.28  root     7565:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.3   root     7566:                m_CF = 1;
1.1       root     7567:        }
                   7568: }
                   7569: 
                   7570: inline void msdos_int_21h_4bh()
                   7571: {
1.1.1.3   root     7572:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7573:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     7574:        
                   7575:        switch(REG8(AL)) {
                   7576:        case 0x00:
                   7577:        case 0x01:
                   7578:                if(msdos_process_exec(command, param, REG8(AL))) {
                   7579:                        REG16(AX) = 0x02;
1.1.1.3   root     7580:                        m_CF = 1;
1.1       root     7581:                }
                   7582:                break;
1.1.1.14  root     7583:        case 0x03:
                   7584:                {
                   7585:                        int fd;
                   7586:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   7587:                                REG16(AX) = 0x02;
                   7588:                                m_CF = 1;
                   7589:                                break;
                   7590:                        }
                   7591:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   7592:                        _close(fd);
                   7593:                        
                   7594:                        UINT16 *overlay = (UINT16 *)param;
                   7595:                        
                   7596:                        // check exe header
                   7597:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   7598:                        int header_size = 0;
                   7599:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   7600:                                header_size = header->header_size * 16;
                   7601:                                // relocation
                   7602:                                int start_seg = overlay[1];
                   7603:                                for(int i = 0; i < header->relocations; i++) {
                   7604:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   7605:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   7606:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   7607:                                }
                   7608:                        }
                   7609:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   7610:                }
                   7611:                break;
1.1       root     7612:        default:
1.1.1.22  root     7613:                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     7614:                REG16(AX) = 0x01;
1.1.1.3   root     7615:                m_CF = 1;
1.1       root     7616:                break;
                   7617:        }
                   7618: }
                   7619: 
                   7620: inline void msdos_int_21h_4ch()
                   7621: {
                   7622:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   7623: }
                   7624: 
                   7625: inline void msdos_int_21h_4dh()
                   7626: {
                   7627:        REG16(AX) = retval;
                   7628: }
                   7629: 
                   7630: inline void msdos_int_21h_4eh()
                   7631: {
                   7632:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7633:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7634:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     7635:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7636:        WIN32_FIND_DATA fd;
                   7637:        
1.1.1.14  root     7638:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   7639:        find->find_magic = FIND_MAGIC;
                   7640:        find->dta_index = dtainfo - dtalist;
1.1       root     7641:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     7642:        dtainfo->allowable_mask = REG8(CL);
                   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:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7660:                msdos_find_file_conv_local_time(&fd);
                   7661:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7662:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7663:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7664:                REG16(AX) = 0;
1.1.1.14  root     7665:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7666:                find->attrib = 8;
                   7667:                find->size = 0;
                   7668:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7669:                dtainfo->allowable_mask &= ~8;
1.1       root     7670:                REG16(AX) = 0;
                   7671:        } else {
                   7672:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     7673:                m_CF = 1;
1.1       root     7674:        }
                   7675: }
                   7676: 
                   7677: inline void msdos_int_21h_4fh()
                   7678: {
                   7679:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7680:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7681:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     7682:        WIN32_FIND_DATA fd;
                   7683:        
1.1.1.14  root     7684:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   7685:                REG16(AX) = 0x12;
                   7686:                m_CF = 1;
                   7687:                return;
                   7688:        }
                   7689:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     7690:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7691:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     7692:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7693:                              !msdos_find_file_has_8dot3name(&fd)) {
                   7694:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7695:                                        FindClose(dtainfo->find_handle);
                   7696:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7697:                                        break;
                   7698:                                }
                   7699:                        }
                   7700:                } else {
1.1.1.13  root     7701:                        FindClose(dtainfo->find_handle);
                   7702:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7703:                }
                   7704:        }
1.1.1.13  root     7705:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7706:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7707:                msdos_find_file_conv_local_time(&fd);
                   7708:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7709:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7710:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7711:                REG16(AX) = 0;
1.1.1.14  root     7712:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7713:                find->attrib = 8;
                   7714:                find->size = 0;
                   7715:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7716:                dtainfo->allowable_mask &= ~8;
1.1       root     7717:                REG16(AX) = 0;
                   7718:        } else {
                   7719:                REG16(AX) = 0x12;
1.1.1.3   root     7720:                m_CF = 1;
1.1       root     7721:        }
                   7722: }
                   7723: 
                   7724: inline void msdos_int_21h_50h()
                   7725: {
1.1.1.8   root     7726:        if(current_psp != REG16(BX)) {
                   7727:                process_t *process = msdos_process_info_get(current_psp);
                   7728:                if(process != NULL) {
                   7729:                        process->psp = REG16(BX);
                   7730:                }
                   7731:                current_psp = REG16(BX);
1.1.1.23  root     7732:                msdos_sda_update(current_psp);
1.1.1.8   root     7733:        }
1.1       root     7734: }
                   7735: 
                   7736: inline void msdos_int_21h_51h()
                   7737: {
                   7738:        REG16(BX) = current_psp;
                   7739: }
                   7740: 
                   7741: inline void msdos_int_21h_52h()
                   7742: {
1.1.1.25  root     7743:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     7744:        i386_load_segment_descriptor(ES);
1.1.1.25  root     7745:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     7746: }
                   7747: 
                   7748: inline void msdos_int_21h_54h()
                   7749: {
                   7750:        process_t *process = msdos_process_info_get(current_psp);
                   7751:        
                   7752:        REG8(AL) = process->verify;
                   7753: }
                   7754: 
                   7755: inline void msdos_int_21h_55h()
                   7756: {
                   7757:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   7758:        
                   7759:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   7760:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   7761:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   7762:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   7763:        psp->parent_psp = current_psp;
                   7764: }
                   7765: 
                   7766: inline void msdos_int_21h_56h(int lfn)
                   7767: {
                   7768:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     7769:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   7770:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     7771:        
                   7772:        if(rename(src, dst)) {
                   7773:                REG16(AX) = errno;
1.1.1.3   root     7774:                m_CF = 1;
1.1       root     7775:        }
                   7776: }
                   7777: 
                   7778: inline void msdos_int_21h_57h()
                   7779: {
                   7780:        FILETIME time, local;
1.1.1.14  root     7781:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     7782:        HANDLE hHandle;
1.1       root     7783:        
1.1.1.21  root     7784:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     7785:                REG16(AX) = (UINT16)GetLastError();
                   7786:                m_CF = 1;
                   7787:                return;
                   7788:        }
                   7789:        ctime = atime = mtime = NULL;
                   7790:        
1.1       root     7791:        switch(REG8(AL)) {
                   7792:        case 0x00:
1.1.1.6   root     7793:        case 0x01:
1.1.1.14  root     7794:                mtime = &time;
1.1.1.6   root     7795:                break;
                   7796:        case 0x04:
                   7797:        case 0x05:
1.1.1.14  root     7798:                atime = &time;
1.1       root     7799:                break;
1.1.1.6   root     7800:        case 0x06:
                   7801:        case 0x07:
1.1.1.14  root     7802:                ctime = &time;
                   7803:                break;
                   7804:        default:
1.1.1.22  root     7805:                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     7806:                REG16(AX) = 0x01;
                   7807:                m_CF = 1;
                   7808:                return;
                   7809:        }
                   7810:        if(REG8(AL) & 1) {
1.1       root     7811:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   7812:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     7813:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     7814:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7815:                        m_CF = 1;
1.1       root     7816:                }
1.1.1.14  root     7817:        } else {
1.1.1.21  root     7818:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     7819:                        // assume a device and use the current time
                   7820:                        GetSystemTimeAsFileTime(&time);
                   7821:                }
                   7822:                FileTimeToLocalFileTime(&time, &local);
                   7823:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     7824:        }
                   7825: }
                   7826: 
                   7827: inline void msdos_int_21h_58h()
                   7828: {
                   7829:        switch(REG8(AL)) {
                   7830:        case 0x00:
1.1.1.7   root     7831:                REG16(AX) = malloc_strategy;
                   7832:                break;
                   7833:        case 0x01:
1.1.1.24  root     7834: //             switch(REG16(BX)) {
                   7835:                switch(REG8(BL)) {
1.1.1.7   root     7836:                case 0x0000:
                   7837:                case 0x0001:
                   7838:                case 0x0002:
                   7839:                case 0x0040:
                   7840:                case 0x0041:
                   7841:                case 0x0042:
                   7842:                case 0x0080:
                   7843:                case 0x0081:
                   7844:                case 0x0082:
                   7845:                        malloc_strategy = REG16(BX);
1.1.1.23  root     7846:                        msdos_sda_update(current_psp);
1.1.1.7   root     7847:                        break;
                   7848:                default:
1.1.1.22  root     7849:                        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     7850:                        REG16(AX) = 0x01;
                   7851:                        m_CF = 1;
                   7852:                        break;
                   7853:                }
                   7854:                break;
                   7855:        case 0x02:
1.1.1.19  root     7856:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     7857:                break;
                   7858:        case 0x03:
1.1.1.24  root     7859: //             switch(REG16(BX)) {
                   7860:                switch(REG8(BL)) {
1.1.1.7   root     7861:                case 0x0000:
1.1.1.19  root     7862:                        msdos_mem_unlink_umb();
                   7863:                        break;
1.1.1.7   root     7864:                case 0x0001:
1.1.1.19  root     7865:                        msdos_mem_link_umb();
1.1.1.7   root     7866:                        break;
                   7867:                default:
1.1.1.22  root     7868:                        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     7869:                        REG16(AX) = 0x01;
                   7870:                        m_CF = 1;
                   7871:                        break;
                   7872:                }
1.1       root     7873:                break;
                   7874:        default:
1.1.1.22  root     7875:                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     7876:                REG16(AX) = 0x01;
1.1.1.3   root     7877:                m_CF = 1;
1.1       root     7878:                break;
                   7879:        }
                   7880: }
                   7881: 
                   7882: inline void msdos_int_21h_59h()
                   7883: {
1.1.1.23  root     7884:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   7885:        
                   7886:        REG16(AX) = sda->extended_error_code;
                   7887:        REG8(BH) = sda->error_class;
                   7888:        REG8(BL) = sda->suggested_action;
                   7889:        REG8(CH) = sda->locus_of_last_error;
1.1       root     7890: }
                   7891: 
                   7892: inline void msdos_int_21h_5ah()
                   7893: {
1.1.1.3   root     7894:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7895:        int len = strlen(path);
                   7896:        char tmp[MAX_PATH];
                   7897:        
                   7898:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   7899:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7900:                
                   7901:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7902:                REG16(AX) = fd;
                   7903:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7904:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7905:                
                   7906:                strcpy(path, tmp);
                   7907:                int dx = REG16(DX) + len;
1.1.1.3   root     7908:                int ds = SREG(DS);
1.1       root     7909:                while(dx > 0xffff) {
                   7910:                        dx -= 0x10;
                   7911:                        ds++;
                   7912:                }
                   7913:                REG16(DX) = dx;
1.1.1.3   root     7914:                SREG(DS) = ds;
                   7915:                i386_load_segment_descriptor(DS);
1.1       root     7916:        } else {
                   7917:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7918:                m_CF = 1;
1.1       root     7919:        }
                   7920: }
                   7921: 
                   7922: inline void msdos_int_21h_5bh()
                   7923: {
1.1.1.3   root     7924:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7925:        
1.1.1.24  root     7926:        if(msdos_is_existing_file(path)) {
1.1       root     7927:                // already exists
                   7928:                REG16(AX) = 0x50;
1.1.1.3   root     7929:                m_CF = 1;
1.1       root     7930:        } else {
                   7931:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7932:                
                   7933:                if(fd != -1) {
                   7934:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7935:                        REG16(AX) = fd;
                   7936:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7937:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7938:                } else {
                   7939:                        REG16(AX) = errno;
1.1.1.3   root     7940:                        m_CF = 1;
1.1       root     7941:                }
                   7942:        }
                   7943: }
                   7944: 
                   7945: inline void msdos_int_21h_5ch()
                   7946: {
                   7947:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7948:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7949:        
1.1.1.20  root     7950:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     7951:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   7952:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     7953:                        UINT32 pos = _tell(fd);
                   7954:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   7955:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     7956:                                REG16(AX) = errno;
1.1.1.3   root     7957:                                m_CF = 1;
1.1       root     7958:                        }
1.1.1.20  root     7959:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     7960:                        
1.1       root     7961:                        // some seconds may be passed in _locking()
                   7962:                        hardware_update();
                   7963:                } else {
                   7964:                        REG16(AX) = 0x01;
1.1.1.3   root     7965:                        m_CF = 1;
1.1       root     7966:                }
                   7967:        } else {
                   7968:                REG16(AX) = 0x06;
1.1.1.3   root     7969:                m_CF = 1;
1.1       root     7970:        }
                   7971: }
                   7972: 
1.1.1.22  root     7973: inline void msdos_int_21h_5dh()
                   7974: {
                   7975:        switch(REG8(AL)) {
                   7976:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     7977:                SREG(DS) = (SDA_TOP >> 4);
                   7978:                i386_load_segment_descriptor(DS);
                   7979:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   7980:                REG16(CX) = 0x80;
                   7981:                REG16(DX) = 0x1a;
                   7982:                break;
                   7983:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     7984:                REG16(AX) = 0x01;
                   7985:                m_CF = 1;
                   7986:                break;
                   7987:        case 0x08: // set redirected printer mode
                   7988:        case 0x09: // flush redirected printer output
                   7989:        case 0x0a: // set extended error information
                   7990:                break;
                   7991:        default:
                   7992:                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));
                   7993:                REG16(AX) = 0x01;
                   7994:                m_CF = 1;
                   7995:                break;
                   7996:        }
                   7997: }
                   7998: 
1.1.1.30  root     7999: inline void msdos_int_21h_5fh()
                   8000: {
                   8001:        switch(REG8(AL)) {
                   8002:        case 0x02:
                   8003:                {
                   8004:                        DWORD drives = GetLogicalDrives();
                   8005:                        for(int i = 0, index = 0; i < 26; i++) {
                   8006:                                if(drives & (1 << i)) {
                   8007:                                        char volume[] = "A:\\";
                   8008:                                        volume[0] = 'A' + i;
                   8009:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   8010:                                                if(index == REG16(BX)) {
                   8011:                                                        DWORD dwSize = 128;
                   8012:                                                        volume[2] = '\0';
                   8013:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   8014:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   8015:                                                        REG8(BH) = 0x00; // valid
                   8016:                                                        REG8(BL) = 0x04; // disk drive
                   8017:                                                        REG16(CX) = 0x00;
                   8018:                                                        return;
                   8019:                                                }
                   8020:                                                index++;
                   8021:                                        }
                   8022:                                }
                   8023:                        }
                   8024:                }
                   8025:                REG16(AX) = 0x12; // no more files
                   8026:                m_CF = 1;
                   8027:                break;
                   8028:        default:
                   8029:                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));
                   8030:                REG16(AX) = 0x01;
                   8031:                m_CF = 1;
                   8032:                break;
                   8033:        }
                   8034: }
                   8035: 
1.1       root     8036: inline void msdos_int_21h_60h(int lfn)
                   8037: {
1.1.1.14  root     8038:        char full[MAX_PATH], *path;
                   8039:        
1.1       root     8040:        if(lfn) {
1.1.1.14  root     8041:                char *name;
                   8042:                *full = '\0';
1.1.1.3   root     8043:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     8044:                switch(REG8(CL)) {
                   8045:                case 1:
                   8046:                        GetShortPathName(full, full, MAX_PATH);
                   8047:                        my_strupr(full);
                   8048:                        break;
                   8049:                case 2:
                   8050:                        GetLongPathName(full, full, MAX_PATH);
                   8051:                        break;
                   8052:                }
                   8053:                path = full;
                   8054:        } else {
                   8055:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   8056:        }
                   8057:        if(*path != '\0') {
                   8058:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     8059:        } else {
1.1.1.14  root     8060:                REG16(AX) = (UINT16)GetLastError();
                   8061:                m_CF = 1;
1.1       root     8062:        }
                   8063: }
                   8064: 
                   8065: inline void msdos_int_21h_61h()
                   8066: {
                   8067:        REG8(AL) = 0;
                   8068: }
                   8069: 
                   8070: inline void msdos_int_21h_62h()
                   8071: {
                   8072:        REG16(BX) = current_psp;
                   8073: }
                   8074: 
                   8075: inline void msdos_int_21h_63h()
                   8076: {
                   8077:        switch(REG8(AL)) {
                   8078:        case 0x00:
1.1.1.3   root     8079:                SREG(DS) = (DBCS_TABLE >> 4);
                   8080:                i386_load_segment_descriptor(DS);
1.1       root     8081:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   8082:                REG8(AL) = 0x00;
                   8083:                break;
1.1.1.22  root     8084:        case 0x01: // set korean input mode
                   8085:        case 0x02: // get korean input mode
                   8086:                REG8(AL) = 0xff; // not supported
                   8087:                break;
1.1       root     8088:        default:
1.1.1.22  root     8089:                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     8090:                REG16(AX) = 0x01;
1.1.1.3   root     8091:                m_CF = 1;
1.1       root     8092:                break;
                   8093:        }
                   8094: }
                   8095: 
1.1.1.25  root     8096: UINT16 get_extended_country_info(UINT8 func)
1.1       root     8097: {
1.1.1.25  root     8098:        switch(func) {
1.1.1.17  root     8099:        case 0x01:
                   8100:                if(REG16(CX) >= 5) {
1.1.1.19  root     8101:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     8102:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   8103:                                REG16(CX) = sizeof(data);
                   8104:                        ZeroMemory(data, sizeof(data));
                   8105:                        data[0] = 0x01;
                   8106:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     8107:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     8108:                        *(UINT16 *)(data + 5) = active_code_page;
                   8109:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     8110: //                     REG16(AX) = active_code_page;
1.1.1.17  root     8111:                } else {
1.1.1.25  root     8112:                        return(0x08); // insufficient memory
1.1.1.17  root     8113:                }
                   8114:                break;
                   8115:        case 0x02:
                   8116:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   8117:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   8118:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     8119: //             REG16(AX) = active_code_page;
1.1.1.17  root     8120:                REG16(CX) = 0x05;
                   8121:                break;
1.1.1.23  root     8122:        case 0x03:
                   8123:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   8124:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   8125:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     8126: //             REG16(AX) = active_code_page;
1.1.1.23  root     8127:                REG16(CX) = 0x05;
                   8128:                break;
1.1.1.17  root     8129:        case 0x04:
                   8130:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   8131:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   8132:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     8133: //             REG16(AX) = active_code_page;
1.1.1.17  root     8134:                REG16(CX) = 0x05;
                   8135:                break;
                   8136:        case 0x05:
                   8137:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   8138:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   8139:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     8140: //             REG16(AX) = active_code_page;
1.1.1.17  root     8141:                REG16(CX) = 0x05;
                   8142:                break;
                   8143:        case 0x06:
                   8144:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   8145:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   8146:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     8147: //             REG16(AX) = active_code_page;
1.1.1.17  root     8148:                REG16(CX) = 0x05;
                   8149:                break;
1.1       root     8150:        case 0x07:
1.1.1.3   root     8151:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   8152:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   8153:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     8154: //             REG16(AX) = active_code_page;
1.1       root     8155:                REG16(CX) = 0x05;
                   8156:                break;
1.1.1.25  root     8157:        default:
                   8158:                return(0x01); // function number invalid
                   8159:        }
                   8160:        return(0x00);
                   8161: }
                   8162: 
                   8163: inline void msdos_int_21h_65h()
                   8164: {
                   8165:        char tmp[0x10000];
                   8166:        
                   8167:        switch(REG8(AL)) {
                   8168:        case 0x01:
                   8169:        case 0x02:
                   8170:        case 0x03:
                   8171:        case 0x04:
                   8172:        case 0x05:
                   8173:        case 0x06:
                   8174:        case 0x07:
                   8175:                {
                   8176:                        UINT16 result = get_extended_country_info(REG8(AL));
                   8177:                        if(result) {
                   8178:                                REG16(AX) = result;
                   8179:                                m_CF = 1;
                   8180:                        } else {
                   8181:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   8182:                        }
                   8183:                }
                   8184:                break;
1.1       root     8185:        case 0x20:
1.1.1.25  root     8186:        case 0xa0:
1.1.1.19  root     8187:                memset(tmp, 0, sizeof(tmp));
                   8188:                tmp[0] = REG8(DL);
1.1       root     8189:                my_strupr(tmp);
                   8190:                REG8(DL) = tmp[0];
                   8191:                break;
                   8192:        case 0x21:
1.1.1.25  root     8193:        case 0xa1:
1.1       root     8194:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8195:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     8196:                my_strupr(tmp);
1.1.1.3   root     8197:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     8198:                break;
                   8199:        case 0x22:
1.1.1.25  root     8200:        case 0xa2:
1.1.1.3   root     8201:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     8202:                break;
1.1.1.25  root     8203:        case 0x23:
                   8204:                // FIXME: need to check multi-byte (kanji) charactre?
                   8205:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   8206:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   8207:                        REG16(AX) = 0x00;
                   8208:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   8209:                        // 826dh/828eh: multi-byte (kanji) N and n
                   8210:                        REG16(AX) = 0x01;
                   8211:                } else {
                   8212:                        REG16(AX) = 0x02;
                   8213:                }
                   8214:                break;
1.1       root     8215:        default:
1.1.1.22  root     8216:                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     8217:                REG16(AX) = 0x01;
1.1.1.3   root     8218:                m_CF = 1;
1.1       root     8219:                break;
                   8220:        }
                   8221: }
                   8222: 
                   8223: inline void msdos_int_21h_66h()
                   8224: {
                   8225:        switch(REG8(AL)) {
                   8226:        case 0x01:
                   8227:                REG16(BX) = active_code_page;
                   8228:                REG16(DX) = system_code_page;
                   8229:                break;
                   8230:        case 0x02:
                   8231:                if(active_code_page == REG16(BX)) {
                   8232:                        REG16(AX) = 0xeb41;
                   8233:                } else if(_setmbcp(REG16(BX)) == 0) {
                   8234:                        active_code_page = REG16(BX);
1.1.1.17  root     8235:                        msdos_nls_tables_update();
1.1       root     8236:                        REG16(AX) = 0xeb41;
                   8237:                } else {
                   8238:                        REG16(AX) = 0x25;
1.1.1.3   root     8239:                        m_CF = 1;
1.1       root     8240:                }
                   8241:                break;
                   8242:        default:
1.1.1.22  root     8243:                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     8244:                REG16(AX) = 0x01;
1.1.1.3   root     8245:                m_CF = 1;
1.1       root     8246:                break;
                   8247:        }
                   8248: }
                   8249: 
                   8250: inline void msdos_int_21h_67h()
                   8251: {
                   8252:        process_t *process = msdos_process_info_get(current_psp);
                   8253:        
                   8254:        if(REG16(BX) <= MAX_FILES) {
                   8255:                process->max_files = max(REG16(BX), 20);
                   8256:        } else {
                   8257:                REG16(AX) = 0x08;
1.1.1.3   root     8258:                m_CF = 1;
1.1       root     8259:        }
                   8260: }
                   8261: 
                   8262: inline void msdos_int_21h_68h()
                   8263: {
                   8264:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8265:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8266:        
1.1.1.20  root     8267:        if(fd < process->max_files && file_handler[fd].valid) {
                   8268:                // fflush(_fdopen(fd, ""));
1.1       root     8269:        } else {
                   8270:                REG16(AX) = 0x06;
1.1.1.3   root     8271:                m_CF = 1;
1.1       root     8272:        }
                   8273: }
                   8274: 
                   8275: inline void msdos_int_21h_69h()
                   8276: {
1.1.1.3   root     8277:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8278:        char path[] = "A:\\";
                   8279:        char volume_label[MAX_PATH];
                   8280:        DWORD serial_number = 0;
                   8281:        char file_system[MAX_PATH];
                   8282:        
                   8283:        if(REG8(BL) == 0) {
                   8284:                path[0] = 'A' + _getdrive() - 1;
                   8285:        } else {
                   8286:                path[0] = 'A' + REG8(BL) - 1;
                   8287:        }
                   8288:        
                   8289:        switch(REG8(AL)) {
                   8290:        case 0x00:
                   8291:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   8292:                        info->info_level = 0;
                   8293:                        info->serial_number = serial_number;
                   8294:                        memset(info->volume_label, 0x20, 11);
                   8295:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   8296:                        memset(info->file_system, 0x20, 8);
                   8297:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   8298:                } else {
                   8299:                        REG16(AX) = errno;
1.1.1.3   root     8300:                        m_CF = 1;
1.1       root     8301:                }
                   8302:                break;
                   8303:        case 0x01:
                   8304:                REG16(AX) = 0x03;
1.1.1.3   root     8305:                m_CF = 1;
1.1       root     8306:        }
                   8307: }
                   8308: 
                   8309: inline void msdos_int_21h_6ah()
                   8310: {
                   8311:        REG8(AH) = 0x68;
                   8312:        msdos_int_21h_68h();
                   8313: }
                   8314: 
                   8315: inline void msdos_int_21h_6bh()
                   8316: {
                   8317:        REG8(AL) = 0;
                   8318: }
                   8319: 
                   8320: inline void msdos_int_21h_6ch(int lfn)
                   8321: {
1.1.1.3   root     8322:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     8323:        int mode = REG8(BL) & 0x03;
                   8324:        
                   8325:        if(mode < 0x03) {
1.1.1.29  root     8326:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     8327:                        // file exists
                   8328:                        if(REG8(DL) & 1) {
1.1.1.29  root     8329:                                int fd = -1, c;
1.1.1.11  root     8330:                                UINT16 info;
1.1       root     8331:                                
1.1.1.11  root     8332:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     8333:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     8334:                                        info = 0x80d3;
1.1.1.29  root     8335:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8336:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   8337:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   8338:                                        }
1.1.1.14  root     8339:                                        info = 0x80d3;
1.1.1.29  root     8340:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8341:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   8342:                                        info = 0x80d3;
1.1.1.11  root     8343:                                } else {
1.1.1.13  root     8344:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     8345:                                        info = msdos_drive_number(path);
                   8346:                                }
1.1       root     8347:                                if(fd != -1) {
                   8348:                                        REG16(AX) = fd;
                   8349:                                        REG16(CX) = 1;
1.1.1.11  root     8350:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     8351:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8352:                                } else {
                   8353:                                        REG16(AX) = errno;
1.1.1.3   root     8354:                                        m_CF = 1;
1.1       root     8355:                                }
                   8356:                        } else if(REG8(DL) & 2) {
                   8357:                                int attr = GetFileAttributes(path);
1.1.1.29  root     8358:                                int fd = -1, c;
1.1.1.11  root     8359:                                UINT16 info;
1.1       root     8360:                                
1.1.1.11  root     8361:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     8362:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     8363:                                        info = 0x80d3;
1.1.1.29  root     8364:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8365:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   8366:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   8367:                                        }
1.1.1.14  root     8368:                                        info = 0x80d3;
1.1.1.29  root     8369:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8370:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   8371:                                        info = 0x80d3;
1.1       root     8372:                                } else {
                   8373:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     8374:                                        info = msdos_drive_number(path);
1.1       root     8375:                                }
                   8376:                                if(fd != -1) {
                   8377:                                        if(attr == -1) {
                   8378:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   8379:                                        }
                   8380:                                        SetFileAttributes(path, attr);
                   8381:                                        REG16(AX) = fd;
                   8382:                                        REG16(CX) = 3;
1.1.1.11  root     8383:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     8384:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8385:                                } else {
                   8386:                                        REG16(AX) = errno;
1.1.1.3   root     8387:                                        m_CF = 1;
1.1       root     8388:                                }
                   8389:                        } else {
                   8390:                                REG16(AX) = 0x50;
1.1.1.3   root     8391:                                m_CF = 1;
1.1       root     8392:                        }
                   8393:                } else {
                   8394:                        // file not exists
                   8395:                        if(REG8(DL) & 0x10) {
                   8396:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   8397:                                
                   8398:                                if(fd != -1) {
                   8399:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   8400:                                        REG16(AX) = fd;
                   8401:                                        REG16(CX) = 2;
                   8402:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     8403:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8404:                                } else {
                   8405:                                        REG16(AX) = errno;
1.1.1.3   root     8406:                                        m_CF = 1;
1.1       root     8407:                                }
                   8408:                        } else {
                   8409:                                REG16(AX) = 0x02;
1.1.1.3   root     8410:                                m_CF = 1;
1.1       root     8411:                        }
                   8412:                }
                   8413:        } else {
                   8414:                REG16(AX) = 0x0c;
1.1.1.3   root     8415:                m_CF = 1;
1.1       root     8416:        }
                   8417: }
                   8418: 
                   8419: inline void msdos_int_21h_710dh()
                   8420: {
                   8421:        // reset drive
                   8422: }
                   8423: 
1.1.1.17  root     8424: inline void msdos_int_21h_7141h(int lfn)
                   8425: {
                   8426:        if(REG16(SI) == 0) {
                   8427:                msdos_int_21h_41h(lfn);
                   8428:                return;
                   8429:        }
                   8430:        if(REG16(SI) != 1) {
                   8431:                REG16(AX) = 5;
                   8432:                m_CF = 1;
                   8433:        }
                   8434:        /* wild card and matching attributes... */
                   8435:        char tmp[MAX_PATH * 2];
                   8436:        // copy search pathname (and quick check overrun)
                   8437:        ZeroMemory(tmp, sizeof(tmp));
                   8438:        tmp[MAX_PATH - 1] = '\0';
                   8439:        tmp[MAX_PATH] = 1;
                   8440:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   8441:        
                   8442:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   8443:                REG16(AX) = 1;
                   8444:                m_CF = 1;
                   8445:                return;
                   8446:        }
                   8447:        for(char *s = tmp; *s; ++s) {
                   8448:                if(*s == '/') {
                   8449:                        *s = '\\';
                   8450:                }
                   8451:        }
                   8452:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   8453:        if(tmp_name) {
                   8454:                ++tmp_name;
                   8455:        } else {
                   8456:                tmp_name = strchr(tmp, ':');
                   8457:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   8458:        }
                   8459:        
                   8460:        WIN32_FIND_DATAA fd;
                   8461:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   8462:        if(fh == INVALID_HANDLE_VALUE) {
                   8463:                REG16(AX) = 2;
                   8464:                m_CF = 1;
                   8465:                return;
                   8466:        }
                   8467:        do {
                   8468:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   8469:                        strcpy(tmp_name, fd.cFileName);
                   8470:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   8471:                                REG16(AX) = 5;
                   8472:                                m_CF = 1;
                   8473:                                break;
                   8474:                        }
                   8475:                }
                   8476:        } while(FindNextFileA(fh, &fd));
                   8477:        if(!m_CF) {
                   8478:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   8479:                        m_CF = 1;
                   8480:                        REG16(AX) = 2;
                   8481:                }
                   8482:        }
                   8483:        FindClose(fh);
                   8484: }
                   8485: 
1.1       root     8486: inline void msdos_int_21h_714eh()
                   8487: {
                   8488:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8489:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   8490:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8491:        WIN32_FIND_DATA fd;
                   8492:        
1.1.1.13  root     8493:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   8494:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8495:                FindClose(dtainfo->find_handle);
                   8496:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8497:        }
                   8498:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8499:        dtainfo->allowable_mask = REG8(CL);
                   8500:        dtainfo->required_mask = REG8(CH);
                   8501:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8502:        
1.1.1.14  root     8503:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8504:                dtainfo->allowable_mask &= ~8;
1.1       root     8505:        }
1.1.1.14  root     8506:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8507:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8508:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8509:                                FindClose(dtainfo->find_handle);
                   8510:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8511:                                break;
                   8512:                        }
                   8513:                }
                   8514:        }
1.1.1.13  root     8515:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8516:                find->attrib = fd.dwFileAttributes;
                   8517:                msdos_find_file_conv_local_time(&fd);
                   8518:                if(REG16(SI) == 0) {
                   8519:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8520:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8521:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8522:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8523:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8524:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8525:                } else {
                   8526:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8527:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8528:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8529:                }
                   8530:                find->size_hi = fd.nFileSizeHigh;
                   8531:                find->size_lo = fd.nFileSizeLow;
                   8532:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8533:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8534:                REG16(AX) = dtainfo - dtalist + 1;
                   8535:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8536:                // volume label
                   8537:                find->attrib = 8;
                   8538:                find->size_hi = find->size_lo = 0;
                   8539:                strcpy(find->full_name, process->volume_label);
                   8540:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8541:                dtainfo->allowable_mask &= ~8;
                   8542:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     8543:        } else {
                   8544:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     8545:                m_CF = 1;
1.1       root     8546:        }
                   8547: }
                   8548: 
                   8549: inline void msdos_int_21h_714fh()
                   8550: {
                   8551:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8552:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8553:        WIN32_FIND_DATA fd;
                   8554:        
1.1.1.14  root     8555:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8556:                REG16(AX) = 6;
1.1.1.13  root     8557:                m_CF = 1;
                   8558:                return;
                   8559:        }
1.1.1.14  root     8560:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8561:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8562:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8563:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8564:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8565:                                        FindClose(dtainfo->find_handle);
                   8566:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8567:                                        break;
                   8568:                                }
                   8569:                        }
                   8570:                } else {
1.1.1.13  root     8571:                        FindClose(dtainfo->find_handle);
                   8572:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8573:                }
                   8574:        }
1.1.1.13  root     8575:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8576:                find->attrib = fd.dwFileAttributes;
                   8577:                msdos_find_file_conv_local_time(&fd);
                   8578:                if(REG16(SI) == 0) {
                   8579:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8580:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8581:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8582:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8583:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8584:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8585:                } else {
                   8586:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8587:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8588:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8589:                }
                   8590:                find->size_hi = fd.nFileSizeHigh;
                   8591:                find->size_lo = fd.nFileSizeLow;
                   8592:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8593:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8594:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8595:                // volume label
                   8596:                find->attrib = 8;
                   8597:                find->size_hi = find->size_lo = 0;
                   8598:                strcpy(find->full_name, process->volume_label);
                   8599:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8600:                dtainfo->allowable_mask &= ~8;
1.1       root     8601:        } else {
                   8602:                REG16(AX) = 0x12;
1.1.1.3   root     8603:                m_CF = 1;
1.1       root     8604:        }
                   8605: }
                   8606: 
                   8607: inline void msdos_int_21h_71a0h()
                   8608: {
                   8609:        DWORD max_component_len, file_sys_flag;
                   8610:        
1.1.1.14  root     8611:        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))) {
                   8612:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   8613:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     8614:                REG16(CX) = (UINT16)max_component_len;          // 255
                   8615:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   8616:        } else {
                   8617:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8618:                m_CF = 1;
1.1       root     8619:        }
                   8620: }
                   8621: 
                   8622: inline void msdos_int_21h_71a1h()
                   8623: {
1.1.1.14  root     8624:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8625:                REG16(AX) = 6;
1.1.1.13  root     8626:                m_CF = 1;
                   8627:                return;
                   8628:        }
1.1.1.14  root     8629:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8630:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8631:                FindClose(dtainfo->find_handle);
                   8632:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8633:        }
                   8634: }
                   8635: 
                   8636: inline void msdos_int_21h_71a6h()
                   8637: {
                   8638:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8639:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8640:        
1.1.1.3   root     8641:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8642:        struct _stat64 status;
                   8643:        DWORD serial_number = 0;
                   8644:        
1.1.1.20  root     8645:        if(fd < process->max_files && file_handler[fd].valid) {
                   8646:                if(_fstat64(fd, &status) == 0) {
                   8647:                        if(file_handler[fd].path[1] == ':') {
1.1       root     8648:                                // NOTE: we need to consider the network file path "\\host\share\"
                   8649:                                char volume[] = "A:\\";
1.1.1.20  root     8650:                                volume[0] = file_handler[fd].path[1];
1.1       root     8651:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   8652:                        }
1.1.1.20  root     8653:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     8654:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   8655:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   8656:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   8657:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   8658:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   8659:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   8660:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   8661:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   8662:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   8663:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     8664:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     8665:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     8666:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     8667:                } else {
                   8668:                        REG16(AX) = errno;
1.1.1.3   root     8669:                        m_CF = 1;
1.1       root     8670:                }
                   8671:        } else {
                   8672:                REG16(AX) = 0x06;
1.1.1.3   root     8673:                m_CF = 1;
1.1       root     8674:        }
                   8675: }
                   8676: 
                   8677: inline void msdos_int_21h_71a7h()
                   8678: {
                   8679:        switch(REG8(BL)) {
                   8680:        case 0x00:
1.1.1.3   root     8681:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     8682:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8683:                        m_CF = 1;
1.1       root     8684:                }
                   8685:                break;
                   8686:        case 0x01:
                   8687:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     8688:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     8689:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8690:                        m_CF = 1;
1.1       root     8691:                }
                   8692:                break;
                   8693:        default:
1.1.1.22  root     8694:                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     8695:                REG16(AX) = 0x01;
1.1.1.3   root     8696:                m_CF = 1;
1.1       root     8697:                break;
                   8698:        }
                   8699: }
                   8700: 
                   8701: inline void msdos_int_21h_71a8h()
                   8702: {
                   8703:        if(REG8(DH) == 0) {
                   8704:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     8705:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8706:                memset(fcb, 0x20, sizeof(fcb));
                   8707:                int len = strlen(tmp);
1.1.1.21  root     8708:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     8709:                        if(tmp[i] == '.') {
                   8710:                                pos = 8;
                   8711:                        } else {
                   8712:                                if(msdos_lead_byte_check(tmp[i])) {
                   8713:                                        fcb[pos++] = tmp[i++];
                   8714:                                }
                   8715:                                fcb[pos++] = tmp[i];
                   8716:                        }
                   8717:                }
1.1.1.3   root     8718:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     8719:        } else {
1.1.1.3   root     8720:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8721:        }
                   8722: }
                   8723: 
1.1.1.22  root     8724: inline void msdos_int_21h_71aah()
                   8725: {
                   8726:        char drv[] = "A:", path[MAX_PATH];
                   8727:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8728:        
                   8729:        if(REG8(BL) == 0) {
                   8730:                drv[0] = 'A' + _getdrive() - 1;
                   8731:        } else {
                   8732:                drv[0] = 'A' + REG8(BL) - 1;
                   8733:        }
                   8734:        switch(REG8(BH)) {
                   8735:        case 0x00:
                   8736:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   8737:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   8738:                        if(GetLogicalDrives() & bits) {
                   8739:                                REG16(AX) = 0x0f; // invalid drive
                   8740:                        } else {
                   8741:                                REG16(AX) = 0x03; // path not found
                   8742:                        }
                   8743:                        m_CF = 1;
                   8744:                }
                   8745:                break;
                   8746:        case 0x01:
                   8747:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   8748:                        REG16(AX) = 0x0f; // invalid drive
                   8749:                        m_CF = 1;
                   8750:                }
                   8751:                break;
                   8752:        case 0x02:
                   8753:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   8754:                        REG16(AX) = 0x0f; // invalid drive
                   8755:                        m_CF = 1;
                   8756:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   8757:                        REG16(AX) = 0x0f; // invalid drive
                   8758:                        m_CF = 1;
                   8759:                } else {
                   8760:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   8761:                }
                   8762:                break;
                   8763:        default:
                   8764:                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));
                   8765:                REG16(AX) = 0x01;
                   8766:                m_CF = 1;
                   8767:                break;
                   8768:        }
                   8769: }
                   8770: 
1.1.1.14  root     8771: inline void msdos_int_21h_7300h()
                   8772: {
                   8773:        if(REG8(AL) == 0) {
                   8774:                REG8(AL) = REG8(CL);
                   8775:                REG8(AH) = 0;
                   8776:        } else {
                   8777:                REG16(AX) = 0x01;
                   8778:                m_CF = 1;
                   8779:        }
                   8780: }
                   8781: 
                   8782: inline void msdos_int_21h_7302h()
                   8783: {
                   8784:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   8785:        UINT16 seg, ofs;
                   8786:        
                   8787:        if(REG16(CX) < 0x3f) {
                   8788:                REG8(AL) = 0x18;
                   8789:                m_CF = 1;
                   8790:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8791:                REG8(AL) = 0xff;
                   8792:                m_CF = 1;
                   8793:        } else {
                   8794:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   8795:        }
                   8796: }
                   8797: 
1.1       root     8798: inline void msdos_int_21h_7303h()
                   8799: {
1.1.1.3   root     8800:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8801:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8802:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   8803:        
                   8804:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   8805:                info->size_of_structure = sizeof(ext_space_info_t);
                   8806:                info->structure_version = 0;
                   8807:                info->sectors_per_cluster = sectors_per_cluster;
                   8808:                info->bytes_per_sector = bytes_per_sector;
                   8809:                info->available_clusters_on_drive = free_clusters;
                   8810:                info->total_clusters_on_drive = total_clusters;
                   8811:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   8812:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   8813:                info->available_allocation_units = free_clusters;       // ???
                   8814:                info->total_allocation_units = total_clusters;          // ???
                   8815:        } else {
                   8816:                REG16(AX) = errno;
1.1.1.3   root     8817:                m_CF = 1;
1.1       root     8818:        }
                   8819: }
                   8820: 
1.1.1.30  root     8821: inline void msdos_int_21h_dbh()
                   8822: {
                   8823:        // Novell NetWare - Workstation - Get Number of Local Drives
                   8824:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   8825:        REG8(AL) = dos_info->last_drive;
                   8826: }
                   8827: 
                   8828: inline void msdos_int_21h_dch()
                   8829: {
                   8830:        // Novell NetWare - Connection Services - Get Connection Number
                   8831:        REG8(AL) = 0x00;
                   8832: }
                   8833: 
1.1       root     8834: inline void msdos_int_25h()
                   8835: {
                   8836:        UINT16 seg, ofs;
                   8837:        DWORD dwSize;
                   8838:        
1.1.1.3   root     8839: #if defined(HAS_I386)
                   8840:        I386OP(pushf)();
                   8841: #else
                   8842:        PREFIX86(_pushf());
                   8843: #endif
1.1       root     8844:        
                   8845:        if(!(REG8(AL) < 26)) {
                   8846:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8847:                m_CF = 1;
1.1       root     8848:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8849:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8850:                m_CF = 1;
1.1       root     8851:        } else {
                   8852:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8853:                char dev[64];
                   8854:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8855:                
                   8856:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8857:                if(hFile == INVALID_HANDLE_VALUE) {
                   8858:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8859:                        m_CF = 1;
1.1       root     8860:                } else {
1.1.1.19  root     8861:                        UINT32 top_sector  = REG16(DX);
                   8862:                        UINT16 sector_num  = REG16(CX);
                   8863:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8864:                        
                   8865:                        if(sector_num == 0xffff) {
                   8866:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8867:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8868:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8869:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8870:                                buffer_addr = (seg << 4) + ofs;
                   8871:                        }
                   8872: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8873: //                             REG8(AL) = 0x02; // drive not ready
                   8874: //                             m_CF = 1;
                   8875: //                     } else 
                   8876:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8877:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8878:                                m_CF = 1;
1.1.1.19  root     8879:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8880:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     8881:                                m_CF = 1;
1.1       root     8882:                        }
                   8883:                        CloseHandle(hFile);
                   8884:                }
                   8885:        }
                   8886: }
                   8887: 
                   8888: inline void msdos_int_26h()
                   8889: {
                   8890:        // this operation may cause serious damage for drives, so always returns error...
                   8891:        UINT16 seg, ofs;
                   8892:        DWORD dwSize;
                   8893:        
1.1.1.3   root     8894: #if defined(HAS_I386)
                   8895:        I386OP(pushf)();
                   8896: #else
                   8897:        PREFIX86(_pushf());
                   8898: #endif
1.1       root     8899:        
                   8900:        if(!(REG8(AL) < 26)) {
                   8901:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8902:                m_CF = 1;
1.1       root     8903:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8904:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8905:                m_CF = 1;
1.1       root     8906:        } else {
                   8907:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8908:                char dev[64];
                   8909:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8910:                
                   8911:                if(dpb->media_type == 0xf8) {
                   8912:                        // this drive is not a floppy
1.1.1.6   root     8913: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   8914: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   8915: //                     }
1.1       root     8916:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8917:                        m_CF = 1;
1.1       root     8918:                } else {
                   8919:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8920:                        if(hFile == INVALID_HANDLE_VALUE) {
                   8921:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8922:                                m_CF = 1;
1.1       root     8923:                        } else {
1.1.1.19  root     8924:                                UINT32 top_sector  = REG16(DX);
                   8925:                                UINT16 sector_num  = REG16(CX);
                   8926:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8927:                                
                   8928:                                if(sector_num == 0xffff) {
                   8929:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8930:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8931:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8932:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8933:                                        buffer_addr = (seg << 4) + ofs;
                   8934:                                }
1.1       root     8935:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8936:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8937:                                        m_CF = 1;
1.1.1.19  root     8938:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8939:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8940:                                        m_CF = 1;
1.1.1.19  root     8941:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8942:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     8943:                                        m_CF = 1;
1.1       root     8944:                                }
                   8945:                                CloseHandle(hFile);
                   8946:                        }
                   8947:                }
                   8948:        }
                   8949: }
                   8950: 
                   8951: inline void msdos_int_27h()
                   8952: {
1.1.1.29  root     8953:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   8954:        try {
                   8955:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   8956:        } catch(...) {
                   8957:                // recover the broken mcb
                   8958:                int mcb_seg = SREG(CS) - 1;
                   8959:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   8960:                if(mcb_seg < (MEMORY_END >> 4)) {
                   8961:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
                   8962:                                mcb->mz = 'M';
                   8963:                        } else {
                   8964:                                mcb->mz = 'Z';
                   8965:                        }
1.1.1.30  root     8966:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
1.1.1.29  root     8967:                        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   8968:                } else {
                   8969:                        mcb->mz = 'Z';
1.1.1.30  root     8970:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     8971:                }
                   8972:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   8973:        }
1.1.1.3   root     8974:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     8975: }
                   8976: 
                   8977: inline void msdos_int_29h()
                   8978: {
1.1.1.14  root     8979: #if 1
                   8980:        // need to check escape sequences
1.1       root     8981:        msdos_putch(REG8(AL));
1.1.1.14  root     8982: #else
                   8983:        DWORD num;
                   8984:        vram_flush();
1.1.1.23  root     8985:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     8986:        cursor_moved = true;
                   8987: #endif
1.1       root     8988: }
                   8989: 
                   8990: inline void msdos_int_2eh()
                   8991: {
                   8992:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   8993:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8994:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     8995:        char *token = my_strtok(tmp, " ");
                   8996:        strcpy(command, token);
                   8997:        strcpy(opt, token + strlen(token) + 1);
                   8998:        
                   8999:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   9000:        param->env_seg = 0;
                   9001:        param->cmd_line.w.l = 44;
                   9002:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   9003:        param->fcb1.w.l = 24;
                   9004:        param->fcb1.w.h = (WORK_TOP >> 4);
                   9005:        param->fcb2.w.l = 24;
                   9006:        param->fcb2.w.h = (WORK_TOP >> 4);
                   9007:        
                   9008:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   9009:        
                   9010:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   9011:        cmd_line->len = strlen(opt);
                   9012:        strcpy(cmd_line->cmd, opt);
                   9013:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   9014:        
1.1.1.28  root     9015:        try {
                   9016:                if(msdos_process_exec(command, param, 0)) {
                   9017:                        REG16(AX) = 0xffff; // error before processing command
                   9018:                } else {
                   9019:                        // set flag to set retval to ax when the started process is terminated
                   9020:                        process_t *process = msdos_process_info_get(current_psp);
                   9021:                        process->called_by_int2eh = true;
                   9022:                }
                   9023:        } catch(...) {
                   9024:                REG16(AX) = 0xffff; // error before processing command
                   9025:        }
1.1       root     9026: }
                   9027: 
1.1.1.29  root     9028: inline void msdos_int_2fh_05h()
                   9029: {
                   9030:        switch(REG8(AL)) {
                   9031:        case 0x00:
                   9032:                // Critical Error Handler is not installed
                   9033: //             REG8(AL) = 0x00;
                   9034:                break;
                   9035:        default:
                   9036:                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));
                   9037:                // error code can't be converted to string
                   9038: //             REG16(AX) = 0x01;
                   9039:                m_CF = 1;
                   9040:                break;
                   9041:        }
                   9042: }
                   9043: 
1.1.1.22  root     9044: inline void msdos_int_2fh_11h()
                   9045: {
                   9046:        switch(REG8(AL)) {
                   9047:        case 0x00:
1.1.1.29  root     9048:                if(i386_read_stack() == 0xdada) {
                   9049:                        // MSCDEX is not installed
                   9050: //                     REG8(AL) = 0x00;
                   9051:                } else {
                   9052:                        // Network Redirector is not installed
                   9053: //                     REG8(AL) = 0x00;
                   9054:                }
1.1.1.22  root     9055:                break;
                   9056:        default:
                   9057:                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     9058:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     9059:                m_CF = 1;
                   9060:                break;
                   9061:        }
                   9062: }
                   9063: 
1.1.1.21  root     9064: inline void msdos_int_2fh_12h()
                   9065: {
                   9066:        switch(REG8(AL)) {
1.1.1.22  root     9067:        case 0x00:
1.1.1.29  root     9068:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     9069:                REG8(AL) = 0xff;
                   9070:                break;
1.1.1.29  root     9071: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   9072:        case 0x02:
                   9073:                {
                   9074:                        UINT16 stack = i386_read_stack();
                   9075:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   9076:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   9077:                        i386_load_segment_descriptor(ES);
                   9078:                }
                   9079:                break;
1.1.1.30  root     9080:        case 0x03:
                   9081:                SREG(DS) = (DEVICE_TOP >> 4);
                   9082:                i386_load_segment_descriptor(DS);
                   9083:                break;
1.1.1.29  root     9084:        case 0x04:
                   9085:                {
                   9086:                        UINT16 stack = i386_read_stack();
                   9087:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   9088: #if defined(HAS_I386)
                   9089:                        m_ZF = (REG8(AL) == '\\');
                   9090: #else
                   9091:                        m_ZeroVal = (REG8(AL) != '\\');
                   9092: #endif
                   9093:                }
                   9094:                break;
                   9095:        case 0x05:
                   9096:                msdos_putch(i386_read_stack());
                   9097:                break;
                   9098: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   9099: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   9100: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   9101: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   9102: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   9103: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   9104: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   9105:        case 0x0d:
                   9106:                {
                   9107:                        SYSTEMTIME time;
                   9108:                        FILETIME file_time;
                   9109:                        WORD dos_date, dos_time;
                   9110:                        GetLocalTime(&time);
                   9111:                        SystemTimeToFileTime(&time, &file_time);
                   9112:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9113:                        REG16(AX) = dos_date;
                   9114:                        REG16(DX) = dos_time;
                   9115:                }
                   9116:                break;
                   9117: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   9118: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   9119: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   9120:        case 0x11:
                   9121:                {
                   9122:                        char path[MAX_PATH], *p;
                   9123:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   9124:                        my_strupr(path);
                   9125:                        while((p = my_strchr(path, '/')) != NULL) {
                   9126:                                *p = '\\';
                   9127:                        }
                   9128:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   9129:                }
                   9130:                break;
                   9131:        case 0x12:
                   9132:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   9133:                break;
                   9134:        case 0x13:
                   9135:                {
                   9136:                        char tmp[2] = {0};
                   9137:                        tmp[0] = i386_read_stack();
                   9138:                        my_strupr(tmp);
                   9139:                        REG8(AL) = tmp[0];
                   9140:                }
                   9141:                break;
                   9142:        case 0x14:
                   9143: #if defined(HAS_I386)
                   9144:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   9145: #else
                   9146:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   9147: #endif
                   9148:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   9149:                break;
                   9150: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     9151:        case 0x16:
                   9152:                if(REG16(BX) < 20) {
                   9153:                        SREG(ES) = SFT_TOP >> 4;
                   9154:                        i386_load_segment_descriptor(ES);
                   9155:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   9156:                        
                   9157:                        // update system file table
                   9158:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   9159:                        if(file_handler[REG16(BX)].valid) {
                   9160:                                int count = 0;
                   9161:                                for(int i = 0; i < 20; i++) {
                   9162:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   9163:                                                count++;
                   9164:                                        }
                   9165:                                }
                   9166:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   9167:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   9168:                                _lseek(REG16(BX), 0, SEEK_END);
                   9169:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   9170:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   9171:                        } else {
                   9172:                                memset(sft, 0, 0x3b);
                   9173:                        }
                   9174:                } else {
                   9175:                        REG16(AX) = 0x06;
                   9176:                        m_CF = 1;
                   9177:                }
                   9178:                break;
1.1.1.29  root     9179: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   9180: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   9181: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   9182:        case 0x1a:
                   9183:                {
                   9184:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   9185:                        if(path[1] == ':') {
                   9186:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   9187:                                        REG8(AL) = path[0] - 'a' + 1;
                   9188:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   9189:                                        REG8(AL) = path[0] - 'A' + 1;
                   9190:                                } else {
                   9191:                                        REG8(AL) = 0xff; // invalid
                   9192:                                }
                   9193:                                strcpy(full, path);
                   9194:                                strcpy(path, full + 2);
                   9195:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   9196:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   9197:                                        REG8(AL) = full[0] - 'a' + 1;
                   9198:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   9199:                                        REG8(AL) = full[0] - 'A' + 1;
                   9200:                                } else {
                   9201:                                        REG8(AL) = 0xff; // invalid
                   9202:                                }
                   9203:                        } else {
                   9204:                                REG8(AL) = 0x00; // default
                   9205:                        }
                   9206:                }
                   9207:                break;
                   9208:        case 0x1b:
                   9209:                {
                   9210:                        int year = REG16(CX) + 1980;
                   9211:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   9212:                }
                   9213:                break;
                   9214: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   9215: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   9216:        case 0x1e:
                   9217:                {
                   9218:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   9219:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   9220:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   9221: #if defined(HAS_I386)
                   9222:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   9223: #else
                   9224:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   9225: #endif
                   9226:                        } else {
                   9227: #if defined(HAS_I386)
                   9228:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   9229: #else
                   9230:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   9231: #endif
                   9232:                        }
                   9233:                }
                   9234:                break;
                   9235: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     9236:        case 0x20:
                   9237:                {
                   9238:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9239:                        
                   9240:                        if(fd < 20) {
                   9241:                                SREG(ES) = current_psp;
                   9242:                                i386_load_segment_descriptor(ES);
                   9243:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   9244:                        } else {
                   9245:                                REG16(AX) = 0x06;
                   9246:                                m_CF = 1;
                   9247:                        }
                   9248:                }
                   9249:                break;
1.1.1.29  root     9250:        case 0x21:
                   9251:                msdos_int_21h_60h(0);
                   9252:                break;
                   9253: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   9254: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   9255: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   9256:        case 0x25:
                   9257:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   9258:                break;
                   9259:        case 0x26:
                   9260:                REG8(AL) = REG8(CL);
                   9261:                msdos_int_21h_3dh();
                   9262:                break;
                   9263:        case 0x27:
                   9264:                msdos_int_21h_3eh();
                   9265:                break;
                   9266:        case 0x28:
                   9267:                REG16(AX) = REG16(BP);
                   9268:                msdos_int_21h_42h();
                   9269:                break;
                   9270:        case 0x29:
                   9271:                msdos_int_21h_3fh();
                   9272:                break;
                   9273: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   9274:        case 0x2b:
                   9275:                REG16(AX) = REG16(BP);
                   9276:                msdos_int_21h_44h();
                   9277:                break;
                   9278:        case 0x2c:
                   9279:                REG16(BX) = DEVICE_TOP >> 4;
                   9280:                REG16(AX) = 22;
                   9281:                break;
                   9282:        case 0x2d:
                   9283:                {
                   9284:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   9285:                        REG16(AX) = sda->extended_error_code;
                   9286:                }
                   9287:                break;
                   9288:        case 0x2e:
                   9289:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
                   9290:                        SREG(ES) = ERR_TABLE_TOP >> 4;
1.1.1.22  root     9291:                        i386_load_segment_descriptor(ES);
                   9292:                        REG16(DI) = 0;
                   9293:                }
                   9294:                break;
1.1.1.29  root     9295:        case 0x2f:
                   9296:                if(REG16(DX) != 0) {
1.1.1.30  root     9297:                        dos_major_version = REG8(DL);
                   9298:                        dos_minor_version = REG8(DH);
1.1.1.29  root     9299:                } else {
                   9300:                        REG8(DL) = 7;
                   9301:                        REG8(DH) = 10;
                   9302:                }
                   9303:                break;
                   9304: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   9305: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     9306:        default:
                   9307:                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));
                   9308:                REG16(AX) = 0x01;
                   9309:                m_CF = 1;
                   9310:                break;
                   9311:        }
                   9312: }
                   9313: 
1.1.1.30  root     9314: inline void msdos_int_2fh_13h()
                   9315: {
                   9316:        static UINT16 prevDS = 0, prevDX = 0;
                   9317:        static UINT16 prevES = 0, prevBX = 0;
                   9318:        UINT16 tmp;
                   9319:        
                   9320:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   9321:        i386_load_segment_descriptor(DS);
                   9322:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   9323:        
                   9324:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   9325:        i386_load_segment_descriptor(ES);
                   9326:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   9327: }
                   9328: 
1.1.1.22  root     9329: inline void msdos_int_2fh_14h()
                   9330: {
                   9331:        switch(REG8(AL)) {
                   9332:        case 0x00:
1.1.1.29  root     9333:                // NLSFUNC.COM is installed
                   9334:                REG8(AL) = 0xff;
1.1.1.25  root     9335:                break;
                   9336:        case 0x01:
                   9337:        case 0x03:
                   9338:                REG8(AL) = 0x00;
                   9339:                active_code_page = REG16(BX);
                   9340:                msdos_nls_tables_update();
                   9341:                break;
                   9342:        case 0x02:
                   9343:                REG8(AL) = get_extended_country_info(REG16(BP));
                   9344:                break;
                   9345:        case 0x04:
                   9346:                REG8(AL) = 0x00;
                   9347:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     9348:                break;
                   9349:        default:
                   9350:                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));
                   9351:                REG16(AX) = 0x01;
                   9352:                m_CF = 1;
                   9353:                break;
                   9354:        }
                   9355: }
                   9356: 
                   9357: inline void msdos_int_2fh_15h()
                   9358: {
                   9359:        switch(REG8(AL)) {
1.1.1.29  root     9360:        case 0x00: // CD-ROM - Installation Check
                   9361:                if(REG16(BX) == 0x0000) {
                   9362:                        // MSCDEX is not installed
                   9363: //                     REG8(AL) = 0x00;
                   9364:                } else {
                   9365:                        // GRAPHICS.COM is not installed
                   9366: //                     REG8(AL) = 0x00;
                   9367:                }
1.1.1.22  root     9368:                break;
                   9369:        case 0xff:
1.1.1.29  root     9370:                if(REG16(BX) == 0x0000) {
                   9371:                        // CORELCDX is not installed
                   9372:                } else {
                   9373:                        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));
                   9374:                        REG16(AX) = 0x01;
                   9375:                        m_CF = 1;
                   9376:                }
1.1.1.22  root     9377:                break;
1.1.1.21  root     9378:        default:
1.1.1.22  root     9379:                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     9380:                REG16(AX) = 0x01;
                   9381:                m_CF = 1;
                   9382:                break;
                   9383:        }
                   9384: }
                   9385: 
1.1       root     9386: inline void msdos_int_2fh_16h()
                   9387: {
                   9388:        switch(REG8(AL)) {
                   9389:        case 0x00:
1.1.1.14  root     9390:                if(no_windows) {
1.1.1.29  root     9391:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   9392: //                     REG8(AL) = 0x00;
1.1.1.14  root     9393:                } else {
1.1.1.30  root     9394:                        REG8(AL) = win_major_version;
                   9395:                        REG8(AH) = win_minor_version;
1.1       root     9396:                }
                   9397:                break;
1.1.1.30  root     9398:        case 0x05:
                   9399:                // from DOSBox
                   9400:                i386_set_a20_line(1);
                   9401:                break;
1.1.1.22  root     9402:        case 0x0a:
                   9403:                if(!no_windows) {
                   9404:                        REG16(AX) = 0x0000;
1.1.1.30  root     9405:                        REG8(BH) = win_major_version;
                   9406:                        REG8(BL) = win_minor_version;
1.1.1.22  root     9407:                        REG16(CX) = 0x0003; // enhanced
                   9408:                }
                   9409:                break;
1.1.1.30  root     9410:        case 0x0b:
                   9411:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     9412:        case 0x0e:
                   9413:        case 0x0f:
1.1.1.30  root     9414:        case 0x10:
1.1.1.22  root     9415:        case 0x11:
                   9416:        case 0x12:
                   9417:        case 0x13:
                   9418:        case 0x14:
1.1.1.30  root     9419:        case 0x15:
1.1.1.22  root     9420:        case 0x87:
1.1.1.30  root     9421:        case 0x89:
1.1.1.22  root     9422:                // function not supported, do not clear AX
                   9423:                break;
1.1.1.14  root     9424:        case 0x80:
                   9425:                Sleep(10);
                   9426:                hardware_update();
1.1.1.29  root     9427:                REG8(AL) = 0x00;
1.1.1.14  root     9428:                break;
1.1.1.22  root     9429:        case 0x8e:
                   9430:                REG16(AX) = 0x00; // failed
                   9431:                break;
1.1.1.20  root     9432:        case 0x8f:
                   9433:                switch(REG8(DH)) {
                   9434:                case 0x00:
                   9435:                case 0x02:
                   9436:                case 0x03:
                   9437:                        REG16(AX) = 0x00;
                   9438:                        break;
                   9439:                case 0x01:
                   9440:                        REG16(AX) = 0x168f;
                   9441:                        break;
                   9442:                }
                   9443:                break;
1.1       root     9444:        default:
1.1.1.22  root     9445:                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));
                   9446:                REG16(AX) = 0x01;
                   9447:                m_CF = 1;
                   9448:                break;
                   9449:        }
                   9450: }
                   9451: 
                   9452: inline void msdos_int_2fh_19h()
                   9453: {
                   9454:        switch(REG8(AL)) {
                   9455:        case 0x00:
1.1.1.29  root     9456:                // SHELLB.COM is not installed
                   9457: //             REG8(AL) = 0x00;
1.1.1.22  root     9458:                break;
                   9459:        case 0x01:
                   9460:        case 0x02:
                   9461:        case 0x03:
                   9462:        case 0x04:
                   9463:                REG16(AX) = 0x01;
                   9464:                m_CF = 1;
                   9465:                break;
1.1.1.29  root     9466:        case 0x80:
                   9467:                // IBM ROM-DOS v4.0 is not installed
                   9468: //             REG8(AL) = 0x00;
                   9469:                break;
1.1.1.22  root     9470:        default:
                   9471:                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     9472:                REG16(AX) = 0x01;
1.1.1.3   root     9473:                m_CF = 1;
1.1       root     9474:                break;
                   9475:        }
                   9476: }
                   9477: 
                   9478: inline void msdos_int_2fh_1ah()
                   9479: {
                   9480:        switch(REG8(AL)) {
                   9481:        case 0x00:
1.1.1.29  root     9482:                // ANSI.SYS is installed
1.1       root     9483:                REG8(AL) = 0xff;
                   9484:                break;
                   9485:        default:
1.1.1.22  root     9486:                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));
                   9487:                REG16(AX) = 0x01;
                   9488:                m_CF = 1;
                   9489:                break;
                   9490:        }
                   9491: }
                   9492: 
1.1.1.30  root     9493: inline void msdos_int_2fh_40h()
1.1.1.22  root     9494: {
                   9495:        switch(REG8(AL)) {
                   9496:        case 0x00:
1.1.1.30  root     9497:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   9498:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     9499:                break;
                   9500:        default:
                   9501:                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     9502:                REG16(AX) = 0x01;
1.1.1.3   root     9503:                m_CF = 1;
1.1       root     9504:                break;
                   9505:        }
                   9506: }
                   9507: 
                   9508: inline void msdos_int_2fh_43h()
                   9509: {
                   9510:        switch(REG8(AL)) {
                   9511:        case 0x00:
1.1.1.29  root     9512:                // XMS is installed ?
1.1.1.19  root     9513: #ifdef SUPPORT_XMS
                   9514:                if(support_xms) {
                   9515:                        REG8(AL) = 0x80;
                   9516:                } else
                   9517: #endif
                   9518:                REG8(AL) = 0x00;
                   9519:                break;
                   9520:        case 0x10:
                   9521:                SREG(ES) = XMS_TOP >> 4;
                   9522:                i386_load_segment_descriptor(ES);
1.1.1.26  root     9523:                REG16(BX) = 0x15;
1.1       root     9524:                break;
                   9525:        default:
1.1.1.22  root     9526:                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));
                   9527:                REG16(AX) = 0x01;
                   9528:                m_CF = 1;
                   9529:                break;
                   9530:        }
                   9531: }
                   9532: 
                   9533: inline void msdos_int_2fh_46h()
                   9534: {
                   9535:        switch(REG8(AL)) {
                   9536:        case 0x80:
1.1.1.29  root     9537:                // Windows v3.0 is not installed
                   9538: //             REG8(AL) = 0x00;
1.1.1.22  root     9539:                break;
                   9540:        default:
                   9541:                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));
                   9542:                REG16(AX) = 0x01;
                   9543:                m_CF = 1;
                   9544:                break;
                   9545:        }
                   9546: }
                   9547: 
                   9548: inline void msdos_int_2fh_48h()
                   9549: {
                   9550:        switch(REG8(AL)) {
                   9551:        case 0x00:
1.1.1.29  root     9552:                // DOSKEY is not installed
                   9553: //             REG8(AL) = 0x00;
1.1.1.22  root     9554:                break;
                   9555:        case 0x10:
                   9556:                msdos_int_21h_0ah();
                   9557:                REG16(AX) = 0x00;
                   9558:                break;
                   9559:        default:
                   9560:                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     9561:                REG16(AX) = 0x01;
1.1.1.3   root     9562:                m_CF = 1;
1.1       root     9563:                break;
                   9564:        }
                   9565: }
                   9566: 
                   9567: inline void msdos_int_2fh_4ah()
                   9568: {
                   9569:        switch(REG8(AL)) {
1.1.1.29  root     9570: #ifdef SUPPORT_HMA
                   9571:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   9572:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   9573:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9574:                                // restore first free mcb in high memory area
                   9575:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9576:                        }
                   9577:                        int offset = 0xffff;
                   9578:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   9579:                                REG16(DI) = offset + 0x10;
                   9580:                        } else {
                   9581:                                REG16(DI) = 0xffff;
                   9582:                        }
                   9583:                } else {
                   9584:                        // HMA is already used
                   9585:                        REG16(BX) = 0;
                   9586:                        REG16(DI) = 0xffff;
                   9587:                }
                   9588:                SREG(ES) = 0xffff;
                   9589:                i386_load_segment_descriptor(ES);
                   9590:                break;
                   9591:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   9592:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   9593:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9594:                                // restore first free mcb in high memory area
                   9595:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9596:                        }
                   9597:                        int size = REG16(BX), offset;
                   9598:                        if((size % 16) != 0) {
                   9599:                                size &= ~15;
                   9600:                                size += 16;
                   9601:                        }
                   9602:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   9603:                                REG16(BX) = size;
                   9604:                                REG16(DI) = offset + 0x10;
                   9605:                                is_hma_used_by_int_2fh = true;
                   9606:                        } else {
                   9607:                                REG16(BX) = 0;
                   9608:                                REG16(DI) = 0xffff;
                   9609:                        }
                   9610:                } else {
                   9611:                        // HMA is already used
                   9612:                        REG16(BX) = 0;
                   9613:                        REG16(DI) = 0xffff;
                   9614:                }
                   9615:                SREG(ES) = 0xffff;
                   9616:                i386_load_segment_descriptor(ES);
                   9617:                break;
                   9618:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   9619:                if(REG8(DL) == 0x00) {
                   9620:                        if(!is_hma_used_by_xms) {
                   9621:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9622:                                        // restore first free mcb in high memory area
                   9623:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9624:                                        is_hma_used_by_int_2fh = false;
                   9625:                                }
                   9626:                                int size = REG16(BX), offset;
                   9627:                                if((size % 16) != 0) {
                   9628:                                        size &= ~15;
                   9629:                                        size += 16;
                   9630:                                }
                   9631:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   9632: //                                     REG16(BX) = size;
                   9633:                                        SREG(ES) = 0xffff;
                   9634:                                        i386_load_segment_descriptor(ES);
                   9635:                                        REG16(DI) = offset + 0x10;
                   9636:                                        is_hma_used_by_int_2fh = true;
                   9637:                                } else {
                   9638:                                        REG16(DI) = 0xffff;
                   9639:                                }
                   9640:                        } else {
                   9641:                                REG16(DI) = 0xffff;
                   9642:                        }
                   9643:                } else if(REG8(DL) == 0x01) {
                   9644:                        if(!is_hma_used_by_xms) {
                   9645:                                int size = REG16(BX);
                   9646:                                if((size % 16) != 0) {
                   9647:                                        size &= ~15;
                   9648:                                        size += 16;
                   9649:                                }
                   9650:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   9651:                                        // memory block address is not changed
                   9652:                                } else {
                   9653:                                        REG16(DI) = 0xffff;
                   9654:                                }
                   9655:                        } else {
                   9656:                                REG16(DI) = 0xffff;
                   9657:                        }
                   9658:                } else if(REG8(DL) == 0x02) {
                   9659:                        if(!is_hma_used_by_xms) {
                   9660:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9661:                                        // restore first free mcb in high memory area
                   9662:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9663:                                        is_hma_used_by_int_2fh = false;
                   9664:                                } else {
                   9665:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   9666:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   9667:                                                is_hma_used_by_int_2fh = false;
                   9668:                                        }
                   9669:                                }
                   9670:                        }
                   9671:                } else {
                   9672:                        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));
                   9673:                        REG16(AX) = 0x01;
                   9674:                        m_CF = 1;
                   9675:                }
                   9676:                break;
                   9677:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   9678:                if(!is_hma_used_by_xms) {
                   9679:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9680:                                // restore first free mcb in high memory area
                   9681:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9682:                                is_hma_used_by_int_2fh = false;
                   9683:                        }
                   9684:                        REG16(AX) = 0x0000;
                   9685:                        SREG(ES) = 0xffff;
                   9686:                        i386_load_segment_descriptor(ES);
                   9687:                        REG16(DI) = 0x10;
                   9688:                }
                   9689:                break;
                   9690: #else
1.1       root     9691:        case 0x01:
                   9692:        case 0x02:
1.1.1.29  root     9693:                // HMA is already used
1.1.1.27  root     9694:                REG16(BX) = 0x0000;
1.1.1.3   root     9695:                SREG(ES) = 0xffff;
                   9696:                i386_load_segment_descriptor(ES);
1.1       root     9697:                REG16(DI) = 0xffff;
                   9698:                break;
1.1.1.19  root     9699:        case 0x03:
                   9700:                // unable to allocate
                   9701:                REG16(DI) = 0xffff;
                   9702:                break;
                   9703:        case 0x04:
                   9704:                // function not supported, do not clear AX
                   9705:                break;
1.1.1.29  root     9706: #endif
                   9707:        case 0x10:
                   9708:                if(REG16(BX) == 0x0000) {
                   9709:                        // SMARTDRV is not installed
                   9710:                } else {
                   9711:                        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));
                   9712:                        REG16(AX) = 0x01;
                   9713:                        m_CF = 1;
                   9714:                }
                   9715:                break;
                   9716:        case 0x11:
                   9717:                if(REG16(BX) == 0x0000) {
                   9718:                        // DBLSPACE.BIN is not installed
                   9719:                } else {
                   9720:                        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));
                   9721:                        REG16(AX) = 0x01;
                   9722:                        m_CF = 1;
                   9723:                }
1.1.1.22  root     9724:                break;
                   9725:        default:
                   9726:                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));
                   9727:                REG16(AX) = 0x01;
                   9728:                m_CF = 1;
                   9729:                break;
                   9730:        }
                   9731: }
                   9732: 
                   9733: inline void msdos_int_2fh_4bh()
                   9734: {
                   9735:        switch(REG8(AL)) {
1.1.1.24  root     9736:        case 0x01:
1.1.1.22  root     9737:        case 0x02:
1.1.1.29  root     9738:                // Task Switcher is not installed
1.1.1.24  root     9739:                break;
                   9740:        case 0x03:
                   9741:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   9742:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     9743:                break;
1.1.1.30  root     9744:        case 0x04:
                   9745:                REG16(BX) = 0x0000; // free switcher id successfully
                   9746:                break;
1.1       root     9747:        default:
1.1.1.22  root     9748:                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     9749:                REG16(AX) = 0x01;
1.1.1.3   root     9750:                m_CF = 1;
1.1       root     9751:                break;
                   9752:        }
                   9753: }
                   9754: 
                   9755: inline void msdos_int_2fh_4fh()
                   9756: {
                   9757:        switch(REG8(AL)) {
                   9758:        case 0x00:
1.1.1.29  root     9759:                // BILING is installed
1.1.1.27  root     9760:                REG16(AX) = 0x0000;
                   9761:                REG8(DL) = 0x01;        // major version
                   9762:                REG8(DH) = 0x00;        // minor version
1.1       root     9763:                break;
                   9764:        case 0x01:
1.1.1.27  root     9765:                REG16(AX) = 0x0000;
1.1       root     9766:                REG16(BX) = active_code_page;
                   9767:                break;
                   9768:        default:
1.1.1.22  root     9769:                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));
                   9770:                REG16(AX) = 0x01;
                   9771:                m_CF = 1;
                   9772:                break;
                   9773:        }
                   9774: }
                   9775: 
                   9776: inline void msdos_int_2fh_55h()
                   9777: {
                   9778:        switch(REG8(AL)) {
                   9779:        case 0x00:
                   9780:        case 0x01:
                   9781: //             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));
                   9782:                break;
                   9783:        default:
                   9784:                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     9785:                REG16(AX) = 0x01;
1.1.1.3   root     9786:                m_CF = 1;
1.1       root     9787:                break;
                   9788:        }
                   9789: }
                   9790: 
1.1.1.30  root     9791: inline void msdos_int_2fh_80h()
1.1.1.29  root     9792: {
                   9793:        switch(REG8(AL)) {
                   9794:        case 0x00:
1.1.1.30  root     9795:                if(REG16(DX) == 0x0000) {
                   9796:                        // FAX BIOS is not installed
                   9797: //                     REG8(AL) = 0x00;
                   9798:                } else {
                   9799:                        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));
                   9800:                        REG16(AX) = 0x01;
                   9801:                        m_CF = 1;
                   9802:                }
1.1.1.29  root     9803:                break;
                   9804:        default:
                   9805:                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));
                   9806:                REG16(AX) = 0x01;
                   9807:                m_CF = 1;
                   9808:                break;
                   9809:        }
                   9810: }
                   9811: 
1.1.1.24  root     9812: inline void msdos_int_2fh_adh()
                   9813: {
                   9814:        switch(REG8(AL)) {
                   9815:        case 0x00:
1.1.1.29  root     9816:                // DISPLAY.SYS is installed
1.1.1.24  root     9817:                REG8(AL) = 0xff;
                   9818:                REG16(BX) = 0x100; // ???
                   9819:                break;
                   9820:        case 0x01:
                   9821:                active_code_page = REG16(BX);
                   9822:                msdos_nls_tables_update();
                   9823:                REG16(AX) = 0x01;
                   9824:                break;
                   9825:        case 0x02:
                   9826:                REG16(BX) = active_code_page;
                   9827:                break;
                   9828:        case 0x03:
                   9829:                // FIXME
                   9830:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   9831:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   9832:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   9833:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   9834:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   9835:                break;
                   9836:        case 0x80:
                   9837:                break; // keyb.com is not installed
                   9838:        default:
                   9839:                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));
                   9840:                REG16(AX) = 0x01;
                   9841:                m_CF = 1;
                   9842:                break;
                   9843:        }
                   9844: }
                   9845: 
1.1       root     9846: inline void msdos_int_2fh_aeh()
                   9847: {
                   9848:        switch(REG8(AL)) {
                   9849:        case 0x00:
1.1.1.28  root     9850:                // FIXME: we need to check the given command line
                   9851:                REG8(AL) = 0x00; // the command should be executed as usual
                   9852: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     9853:                break;
                   9854:        case 0x01:
                   9855:                {
                   9856:                        char command[MAX_PATH];
                   9857:                        memset(command, 0, sizeof(command));
1.1.1.3   root     9858:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     9859:                        
                   9860:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   9861:                        param->env_seg = 0;
                   9862:                        param->cmd_line.w.l = 44;
                   9863:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   9864:                        param->fcb1.w.l = 24;
                   9865:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   9866:                        param->fcb2.w.l = 24;
                   9867:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   9868:                        
                   9869:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   9870:                        
                   9871:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     9872:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   9873:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     9874:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   9875:                        
1.1.1.28  root     9876:                        try {
                   9877:                                msdos_process_exec(command, param, 0);
                   9878:                        } catch(...) {
                   9879:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     9880:                        }
                   9881:                }
                   9882:                break;
                   9883:        default:
1.1.1.22  root     9884:                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     9885:                REG16(AX) = 0x01;
1.1.1.3   root     9886:                m_CF = 1;
1.1       root     9887:                break;
                   9888:        }
                   9889: }
                   9890: 
                   9891: inline void msdos_int_2fh_b7h()
                   9892: {
                   9893:        switch(REG8(AL)) {
                   9894:        case 0x00:
1.1.1.30  root     9895:                // APPEND is not installed
1.1.1.29  root     9896: //             REG8(AL) = 0x00;
1.1       root     9897:                break;
1.1.1.22  root     9898:        case 0x07:
                   9899:        case 0x11:
                   9900:                break;
1.1       root     9901:        default:
1.1.1.22  root     9902:                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     9903:                REG16(AX) = 0x01;
1.1.1.3   root     9904:                m_CF = 1;
1.1       root     9905:                break;
                   9906:        }
                   9907: }
                   9908: 
1.1.1.30  root     9909: inline void msdos_int_2fh_d7h()
1.1.1.29  root     9910: {
                   9911:        switch(REG8(AL)) {
1.1.1.30  root     9912:        case 0x01:
                   9913:                // Banyan VINES v4+ is not installed
1.1.1.29  root     9914:                break;
                   9915:        default:
                   9916:                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));
                   9917:                REG16(AX) = 0x01;
                   9918:                m_CF = 1;
                   9919:                break;
                   9920:        }
                   9921: }
                   9922: 
1.1.1.24  root     9923: inline void msdos_int_33h_0000h()
                   9924: {
                   9925:        REG16(AX) = 0xffff; // hardware/driver installed
                   9926:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   9927: }
                   9928: 
                   9929: inline void msdos_int_33h_0001h()
                   9930: {
                   9931:        if(!mouse.active) {
                   9932:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9933:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   9934:                }
                   9935:                mouse.active = true;
                   9936:                pic[1].imr &= ~0x10;    // enable irq12
                   9937:        }
                   9938: }
                   9939: 
                   9940: inline void msdos_int_33h_0002h()
                   9941: {
                   9942:        if(mouse.active) {
                   9943:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9944:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   9945:                }
                   9946:                mouse.active = false;
                   9947:                pic[1].imr |= 0x10;     // disable irq12
                   9948:        }
                   9949: }
                   9950: 
                   9951: inline void msdos_int_33h_0003h()
                   9952: {
                   9953:        REG16(BX) = mouse.get_buttons();
                   9954:        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   9955:        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   9956: }
                   9957: 
                   9958: inline void msdos_int_33h_0005h()
                   9959: {
                   9960:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9961:                int idx = REG16(BX);
                   9962:                REG16(BX) = mouse.buttons[idx].pressed_times;
                   9963:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].pressed_position.x));
                   9964:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].pressed_position.y));
                   9965:                mouse.buttons[idx].pressed_times = 0;
                   9966:        } else {
                   9967:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9968:        }
                   9969:        REG16(AX) = mouse.get_buttons();
                   9970: }
                   9971: 
                   9972: inline void msdos_int_33h_0006h()
                   9973: {
                   9974:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9975:                int idx = REG16(BX);
                   9976:                REG16(BX) = mouse.buttons[idx].released_times;
                   9977:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].released_position.x));
                   9978:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].released_position.y));
                   9979:                mouse.buttons[idx].released_times = 0;
                   9980:        } else {
                   9981:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9982:        }
                   9983:        REG16(AX) = mouse.get_buttons();
                   9984: }
                   9985: 
                   9986: inline void msdos_int_33h_0007h()
                   9987: {
                   9988:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   9989:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   9990: }
                   9991: 
                   9992: inline void msdos_int_33h_0008h()
                   9993: {
                   9994:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   9995:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   9996: }
                   9997: 
                   9998: inline void msdos_int_33h_0009h()
                   9999: {
                   10000:        mouse.hot_spot[0] = REG16(BX);
                   10001:        mouse.hot_spot[1] = REG16(CX);
                   10002: }
                   10003: 
                   10004: inline void msdos_int_33h_000bh()
                   10005: {
                   10006:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   10007:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   10008:        mouse.prev_position.x = mouse.position.x;
                   10009:        mouse.prev_position.y = mouse.position.y;
                   10010:        REG16(CX) = dx;
                   10011:        REG16(DX) = dy;
                   10012: }
                   10013: 
                   10014: inline void msdos_int_33h_000ch()
                   10015: {
                   10016:        mouse.call_mask = REG16(CX);
                   10017:        mouse.call_addr.w.l = REG16(DX);
                   10018:        mouse.call_addr.w.h = SREG(ES);
                   10019: }
                   10020: 
                   10021: inline void msdos_int_33h_000fh()
                   10022: {
                   10023:        mouse.mickey.x = REG16(CX);
                   10024:        mouse.mickey.y = REG16(DX);
                   10025: }
                   10026: 
                   10027: inline void msdos_int_33h_0011h()
                   10028: {
                   10029:        REG16(AX) = 0xffff;
                   10030:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   10031: }
                   10032: 
                   10033: inline void msdos_int_33h_0014h()
                   10034: {
                   10035:        UINT16 old_mask = mouse.call_mask;
                   10036:        UINT16 old_ofs = mouse.call_addr.w.l;
                   10037:        UINT16 old_seg = mouse.call_addr.w.h;
                   10038:        
                   10039:        mouse.call_mask = REG16(CX);
                   10040:        mouse.call_addr.w.l = REG16(DX);
                   10041:        mouse.call_addr.w.h = SREG(ES);
                   10042:        
                   10043:        REG16(CX) = old_mask;
                   10044:        REG16(DX) = old_ofs;
                   10045:        SREG(ES) = old_seg;
                   10046:        i386_load_segment_descriptor(ES);
                   10047: }
                   10048: 
                   10049: inline void msdos_int_33h_0015h()
                   10050: {
                   10051:        REG16(BX) = sizeof(mouse);
                   10052: }
                   10053: 
                   10054: inline void msdos_int_33h_0016h()
                   10055: {
                   10056:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   10057: }
                   10058: 
                   10059: inline void msdos_int_33h_0017h()
                   10060: {
                   10061:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   10062: }
                   10063: 
                   10064: inline void msdos_int_33h_001ah()
                   10065: {
                   10066:        mouse.sensitivity[0] = REG16(BX);
                   10067:        mouse.sensitivity[1] = REG16(CX);
                   10068:        mouse.sensitivity[2] = REG16(DX);
                   10069: }
                   10070: 
                   10071: inline void msdos_int_33h_001bh()
                   10072: {
                   10073:        REG16(BX) = mouse.sensitivity[0];
                   10074:        REG16(CX) = mouse.sensitivity[1];
                   10075:        REG16(DX) = mouse.sensitivity[2];
                   10076: }
                   10077: 
                   10078: inline void msdos_int_33h_001dh()
                   10079: {
                   10080:        mouse.display_page = REG16(BX);
                   10081: }
                   10082: 
                   10083: inline void msdos_int_33h_001eh()
                   10084: {
                   10085:        REG16(BX) = mouse.display_page;
                   10086: }
                   10087: 
                   10088: inline void msdos_int_33h_0021h()
                   10089: {
                   10090:        REG16(AX) = 0xffff;
                   10091:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   10092: }
                   10093: 
                   10094: inline void msdos_int_33h_0022h()
                   10095: {
                   10096:        mouse.language = REG16(BX);
                   10097: }
                   10098: 
                   10099: inline void msdos_int_33h_0023h()
                   10100: {
                   10101:        REG16(BX) = mouse.language;
                   10102: }
                   10103: 
                   10104: inline void msdos_int_33h_0024h()
                   10105: {
                   10106:        REG16(BX) = 0x0805; // V8.05
                   10107:        REG16(CX) = 0x0400; // PS/2
                   10108: }
                   10109: 
                   10110: inline void msdos_int_33h_0026h()
                   10111: {
                   10112:        REG16(BX) = 0x0000;
                   10113:        REG16(CX) = mouse.max_position.x;
                   10114:        REG16(DX) = mouse.max_position.y;
                   10115: }
                   10116: 
                   10117: inline void msdos_int_33h_002ah()
                   10118: {
                   10119:        REG16(AX) = mouse.active ? 0 : 0xffff;
                   10120:        REG16(BX) = mouse.hot_spot[0];
                   10121:        REG16(CX) = mouse.hot_spot[1];
                   10122:        REG16(DX) = 4; // PS/2
                   10123: }
                   10124: 
                   10125: inline void msdos_int_33h_0031h()
                   10126: {
                   10127:        REG16(AX) = mouse.min_position.x;
                   10128:        REG16(BX) = mouse.min_position.y;
                   10129:        REG16(CX) = mouse.max_position.x;
                   10130:        REG16(DX) = mouse.max_position.y;
                   10131: }
                   10132: 
                   10133: inline void msdos_int_33h_0032h()
                   10134: {
                   10135:        REG16(AX) = 0;
                   10136: //     REG16(AX) |= 0x8000; // 0025h
                   10137:        REG16(AX) |= 0x4000; // 0026h
                   10138: //     REG16(AX) |= 0x2000; // 0027h
                   10139: //     REG16(AX) |= 0x1000; // 0028h
                   10140: //     REG16(AX) |= 0x0800; // 0029h
                   10141:        REG16(AX) |= 0x0400; // 002ah
                   10142: //     REG16(AX) |= 0x0200; // 002bh
                   10143: //     REG16(AX) |= 0x0100; // 002ch
                   10144: //     REG16(AX) |= 0x0080; // 002dh
                   10145: //     REG16(AX) |= 0x0040; // 002eh
                   10146:        REG16(AX) |= 0x0020; // 002fh
                   10147: //     REG16(AX) |= 0x0010; // 0030h
                   10148:        REG16(AX) |= 0x0008; // 0031h
                   10149:        REG16(AX) |= 0x0004; // 0032h
                   10150: //     REG16(AX) |= 0x0002; // 0033h
                   10151: //     REG16(AX) |= 0x0001; // 0034h
                   10152: }
                   10153: 
1.1.1.19  root     10154: inline void msdos_int_67h_40h()
                   10155: {
                   10156:        if(!support_ems) {
                   10157:                REG8(AH) = 0x84;
                   10158:        } else {
                   10159:                REG8(AH) = 0x00;
                   10160:        }
                   10161: }
                   10162: 
                   10163: inline void msdos_int_67h_41h()
                   10164: {
                   10165:        if(!support_ems) {
                   10166:                REG8(AH) = 0x84;
                   10167:        } else {
                   10168:                REG8(AH) = 0x00;
                   10169:                REG16(BX) = EMS_TOP >> 4;
                   10170:        }
                   10171: }
                   10172: 
                   10173: inline void msdos_int_67h_42h()
                   10174: {
                   10175:        if(!support_ems) {
                   10176:                REG8(AH) = 0x84;
                   10177:        } else {
                   10178:                REG8(AH) = 0x00;
                   10179:                REG16(BX) = free_ems_pages;
                   10180:                REG16(DX) = MAX_EMS_PAGES;
                   10181:        }
                   10182: }
                   10183: 
                   10184: inline void msdos_int_67h_43h()
                   10185: {
                   10186:        if(!support_ems) {
                   10187:                REG8(AH) = 0x84;
                   10188:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10189:                REG8(AH) = 0x87;
                   10190:        } else if(REG16(BX) > free_ems_pages) {
                   10191:                REG8(AH) = 0x88;
                   10192:        } else if(REG16(BX) == 0) {
                   10193:                REG8(AH) = 0x89;
                   10194:        } else {
1.1.1.31! root     10195:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10196:                        if(!ems_handles[i].allocated) {
                   10197:                                ems_allocate_pages(i, REG16(BX));
                   10198:                                REG8(AH) = 0x00;
                   10199:                                REG16(DX) = i;
                   10200:                                return;
                   10201:                        }
                   10202:                }
                   10203:                REG8(AH) = 0x85;
                   10204:        }
                   10205: }
                   10206: 
                   10207: inline void msdos_int_67h_44h()
                   10208: {
                   10209:        if(!support_ems) {
                   10210:                REG8(AH) = 0x84;
1.1.1.31! root     10211:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10212:                REG8(AH) = 0x83;
                   10213:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   10214:                REG8(AH) = 0x8a;
                   10215: //     } else if(!(REG8(AL) < 4)) {
                   10216: //             REG8(AH) = 0x8b;
                   10217:        } else if(REG16(BX) == 0xffff) {
                   10218:                ems_unmap_page(REG8(AL) & 3);
                   10219:                REG8(AH) = 0x00;
                   10220:        } else {
                   10221:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   10222:                REG8(AH) = 0x00;
                   10223:        }
                   10224: }
                   10225: 
                   10226: inline void msdos_int_67h_45h()
                   10227: {
                   10228:        if(!support_ems) {
                   10229:                REG8(AH) = 0x84;
1.1.1.31! root     10230:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10231:                REG8(AH) = 0x83;
                   10232:        } else {
                   10233:                ems_release_pages(REG16(DX));
                   10234:                REG8(AH) = 0x00;
                   10235:        }
                   10236: }
                   10237: 
                   10238: inline void msdos_int_67h_46h()
                   10239: {
                   10240:        if(!support_ems) {
                   10241:                REG8(AH) = 0x84;
                   10242:        } else {
1.1.1.29  root     10243: //             REG16(AX) = 0x0032; // EMS 3.2
                   10244:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     10245:        }
                   10246: }
                   10247: 
                   10248: inline void msdos_int_67h_47h()
                   10249: {
                   10250:        // NOTE: the map data should be stored in the specified ems page, not process data
                   10251:        process_t *process = msdos_process_info_get(current_psp);
                   10252:        
                   10253:        if(!support_ems) {
                   10254:                REG8(AH) = 0x84;
1.1.1.31! root     10255: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10256: //             REG8(AH) = 0x83;
                   10257:        } else if(process->ems_pages_stored) {
                   10258:                REG8(AH) = 0x8d;
                   10259:        } else {
                   10260:                for(int i = 0; i < 4; i++) {
                   10261:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   10262:                        process->ems_pages[i].page   = ems_pages[i].page;
                   10263:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   10264:                }
                   10265:                process->ems_pages_stored = true;
                   10266:                REG8(AH) = 0x00;
                   10267:        }
                   10268: }
                   10269: 
                   10270: inline void msdos_int_67h_48h()
                   10271: {
                   10272:        // NOTE: the map data should be restored from the specified ems page, not process data
                   10273:        process_t *process = msdos_process_info_get(current_psp);
                   10274:        
                   10275:        if(!support_ems) {
                   10276:                REG8(AH) = 0x84;
1.1.1.31! root     10277: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10278: //             REG8(AH) = 0x83;
                   10279:        } else if(!process->ems_pages_stored) {
                   10280:                REG8(AH) = 0x8e;
                   10281:        } else {
                   10282:                for(int i = 0; i < 4; i++) {
                   10283:                        if(process->ems_pages[i].mapped) {
                   10284:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   10285:                        } else {
                   10286:                                ems_unmap_page(i);
                   10287:                        }
                   10288:                }
                   10289:                process->ems_pages_stored = false;
                   10290:                REG8(AH) = 0x00;
                   10291:        }
                   10292: }
                   10293: 
                   10294: inline void msdos_int_67h_4bh()
                   10295: {
                   10296:        if(!support_ems) {
                   10297:                REG8(AH) = 0x84;
                   10298:        } else {
                   10299:                REG8(AH) = 0x00;
                   10300:                REG16(BX) = 0;
1.1.1.31! root     10301:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10302:                        if(ems_handles[i].allocated) {
                   10303:                                REG16(BX)++;
                   10304:                        }
                   10305:                }
                   10306:        }
                   10307: }
                   10308: 
                   10309: inline void msdos_int_67h_4ch()
                   10310: {
                   10311:        if(!support_ems) {
                   10312:                REG8(AH) = 0x84;
1.1.1.31! root     10313:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10314:                REG8(AH) = 0x83;
                   10315:        } else {
                   10316:                REG8(AH) = 0x00;
                   10317:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   10318:        }
                   10319: }
                   10320: 
                   10321: inline void msdos_int_67h_4dh()
                   10322: {
                   10323:        if(!support_ems) {
                   10324:                REG8(AH) = 0x84;
                   10325:        } else {
                   10326:                REG8(AH) = 0x00;
                   10327:                REG16(BX) = 0;
1.1.1.31! root     10328:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10329:                        if(ems_handles[i].allocated) {
                   10330:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   10331:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   10332:                                REG16(BX)++;
                   10333:                        }
                   10334:                }
                   10335:        }
                   10336: }
                   10337: 
1.1.1.20  root     10338: inline void msdos_int_67h_4eh()
                   10339: {
                   10340:        if(!support_ems) {
                   10341:                REG8(AH) = 0x84;
                   10342:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   10343:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   10344:                        // save page map
                   10345:                        for(int i = 0; i < 4; i++) {
                   10346:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   10347:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   10348:                        }
                   10349:                }
                   10350:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   10351:                        // restore page map
                   10352:                        for(int i = 0; i < 4; i++) {
                   10353:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   10354:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   10355:                                
1.1.1.31! root     10356:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     10357:                                        ems_map_page(i, handle, page);
                   10358:                                } else {
                   10359:                                        ems_unmap_page(i);
                   10360:                                }
                   10361:                        }
                   10362:                }
                   10363:                REG8(AH) = 0x00;
                   10364:        } else if(REG8(AL) == 0x03) {
                   10365:                REG8(AH) = 0x00;
1.1.1.21  root     10366:                REG8(AL) = 4 * 4;
                   10367:        } else {
1.1.1.22  root     10368:                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     10369:                REG8(AH) = 0x8f;
                   10370:        }
                   10371: }
                   10372: 
                   10373: inline void msdos_int_67h_4fh()
                   10374: {
                   10375:        if(!support_ems) {
                   10376:                REG8(AH) = 0x84;
                   10377:        } else if(REG8(AL) == 0x00) {
                   10378:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   10379:                
                   10380:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   10381:                for(int i = 0; i < count; i++) {
                   10382:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   10383:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   10384:                        
                   10385: //                     if(!(physical < 4)) {
                   10386: //                             REG8(AH) = 0x8b;
                   10387: //                             return;
                   10388: //                     }
                   10389:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   10390:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   10391:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   10392:                }
                   10393:                REG8(AH) = 0x00;
                   10394:        } else if(REG8(AL) == 0x01) {
                   10395:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   10396:                
                   10397:                for(int i = 0; i < count; i++) {
                   10398:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   10399:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   10400:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   10401:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   10402:                        
                   10403: //                     if(!(physical < 4)) {
                   10404: //                             REG8(AH) = 0x8b;
                   10405: //                             return;
                   10406: //                     } else
1.1.1.31! root     10407:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     10408:                                REG8(AH) = 0x83;
                   10409:                                return;
                   10410:                        } else if(logical == 0xffff) {
                   10411:                                ems_unmap_page(physical & 3);
                   10412:                        } else if(logical < ems_handles[handle].pages) {
                   10413:                                ems_map_page(physical & 3, handle, logical);
                   10414:                        } else {
                   10415:                                REG8(AH) = 0x8a;
                   10416:                                return;
                   10417:                        }
                   10418:                }
                   10419:                REG8(AH) = 0x00;
                   10420:        } else if(REG8(AL) == 0x02) {
                   10421:                REG8(AH) = 0x00;
                   10422:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     10423:        } else {
1.1.1.22  root     10424:                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     10425:                REG8(AH) = 0x8f;
                   10426:        }
                   10427: }
                   10428: 
                   10429: inline void msdos_int_67h_50h()
                   10430: {
                   10431:        if(!support_ems) {
                   10432:                REG8(AH) = 0x84;
1.1.1.31! root     10433:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     10434:                REG8(AH) = 0x83;
                   10435:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   10436:                for(int i = 0; i < REG16(CX); i++) {
                   10437:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   10438:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   10439:                        
                   10440:                        if(REG8(AL) == 0x01) {
                   10441:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   10442:                        }
                   10443: //                     if(!(physical < 4)) {
                   10444: //                             REG8(AH) = 0x8b;
                   10445: //                             return;
                   10446: //                     } else
                   10447:                        if(logical == 0xffff) {
                   10448:                                ems_unmap_page(physical & 3);
                   10449:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   10450:                                ems_map_page(physical & 3, REG16(DX), logical);
                   10451:                        } else {
                   10452:                                REG8(AH) = 0x8a;
                   10453:                                return;
                   10454:                        }
                   10455:                }
                   10456:                REG8(AH) = 0x00;
                   10457:        } else {
1.1.1.22  root     10458:                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     10459:                REG8(AH) = 0x8f;
                   10460:        }
                   10461: }
                   10462: 
1.1.1.19  root     10463: inline void msdos_int_67h_51h()
                   10464: {
                   10465:        if(!support_ems) {
                   10466:                REG8(AH) = 0x84;
1.1.1.31! root     10467:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10468:                REG8(AH) = 0x83;
                   10469:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10470:                REG8(AH) = 0x87;
                   10471:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   10472:                REG8(AH) = 0x88;
                   10473:        } else {
                   10474:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   10475:                REG8(AH) = 0x00;
                   10476:        }
                   10477: }
                   10478: 
1.1.1.20  root     10479: inline void msdos_int_67h_52h()
                   10480: {
                   10481:        if(!support_ems) {
                   10482:                REG8(AH) = 0x84;
1.1.1.31! root     10483: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
        !          10484: //             REG8(AH) = 0x83;
1.1.1.20  root     10485:        } else if(REG8(AL) == 0x00) {
                   10486:                REG8(AL) = 0x00; // handle is volatile
                   10487:                REG8(AH) = 0x00;
                   10488:        } else if(REG8(AL) == 0x01) {
                   10489:                if(REG8(BL) == 0x00) {
                   10490:                        REG8(AH) = 0x00;
                   10491:                } else {
                   10492:                        REG8(AH) = 0x90; // undefined attribute type
                   10493:                }
                   10494:        } else if(REG8(AL) == 0x02) {
                   10495:                REG8(AL) = 0x00; // only volatile handles supported
                   10496:                REG8(AH) = 0x00;
                   10497:        } else {
1.1.1.22  root     10498:                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     10499:                REG8(AH) = 0x8f;
                   10500:        }
                   10501: }
                   10502: 
1.1.1.19  root     10503: inline void msdos_int_67h_53h()
                   10504: {
                   10505:        if(!support_ems) {
                   10506:                REG8(AH) = 0x84;
1.1.1.31! root     10507:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     10508:                REG8(AH) = 0x83;
                   10509:        } else if(REG8(AL) == 0x00) {
                   10510:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   10511:                REG8(AH) = 0x00;
                   10512:        } else if(REG8(AL) == 0x01) {
1.1.1.31! root     10513:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10514:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   10515:                                REG8(AH) = 0xa1;
                   10516:                                return;
                   10517:                        }
                   10518:                }
                   10519:                REG8(AH) = 0x00;
                   10520:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   10521:        } else {
1.1.1.22  root     10522:                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     10523:                REG8(AH) = 0x8f;
1.1.1.19  root     10524:        }
                   10525: }
                   10526: 
                   10527: inline void msdos_int_67h_54h()
                   10528: {
                   10529:        if(!support_ems) {
                   10530:                REG8(AH) = 0x84;
                   10531:        } else if(REG8(AL) == 0x00) {
1.1.1.31! root     10532:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10533:                        if(ems_handles[i].allocated) {
                   10534:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   10535:                        } else {
                   10536:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   10537:                        }
                   10538:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   10539:                }
                   10540:                REG8(AH) = 0x00;
                   10541:                REG8(AL) = MAX_EMS_HANDLES;
                   10542:        } else if(REG8(AL) == 0x01) {
                   10543:                REG8(AH) = 0xa0; // not found
1.1.1.31! root     10544:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     10545:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   10546:                                REG8(AH) = 0x00;
                   10547:                                REG16(DX) = i;
                   10548:                                break;
                   10549:                        }
                   10550:                }
                   10551:        } else if(REG8(AL) == 0x02) {
                   10552:                REG8(AH) = 0x00;
                   10553:                REG16(BX) = MAX_EMS_HANDLES;
                   10554:        } else {
1.1.1.22  root     10555:                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     10556:                REG8(AH) = 0x8f;
                   10557:        }
                   10558: }
                   10559: 
                   10560: inline void msdos_int_67h_57h_tmp()
                   10561: {
                   10562:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   10563:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   10564:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   10565:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   10566:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   10567:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   10568:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   10569:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   10570:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   10571:        
                   10572:        UINT8 *src_buffer, *dest_buffer;
                   10573:        UINT32 src_addr, dest_addr;
                   10574:        UINT32 src_addr_max, dest_addr_max;
                   10575:        
                   10576:        if(src_type == 0) {
                   10577:                src_buffer = mem;
                   10578:                src_addr = (src_seg << 4) + src_ofs;
                   10579:                src_addr_max = MAX_MEM;
                   10580:        } else {
1.1.1.31! root     10581:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     10582:                        REG8(AH) = 0x83;
                   10583:                        return;
                   10584:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   10585:                        REG8(AH) = 0x8a;
                   10586:                        return;
                   10587:                }
                   10588:                src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   10589:                src_addr = src_ofs;
                   10590:                src_addr_max = 0x4000;
                   10591:        }
                   10592:        if(dest_type == 0) {
                   10593:                dest_buffer = mem;
                   10594:                dest_addr = (dest_seg << 4) + dest_ofs;
                   10595:                dest_addr_max = MAX_MEM;
                   10596:        } else {
1.1.1.31! root     10597:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     10598:                        REG8(AH) = 0x83;
                   10599:                        return;
                   10600:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   10601:                        REG8(AH) = 0x8a;
                   10602:                        return;
                   10603:                }
                   10604:                dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   10605:                dest_addr = dest_ofs;
                   10606:                dest_addr_max = 0x4000;
                   10607:        }
                   10608:        for(int i = 0; i < copy_length; i++) {
                   10609:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   10610:                        if(REG8(AL) == 0x00) {
                   10611:                                dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   10612:                        } else if(REG8(AL) == 0x01) {
                   10613:                                UINT8 tmp = dest_buffer[dest_addr];
                   10614:                                dest_buffer[dest_addr++] = src_buffer[src_addr];
                   10615:                                src_buffer[src_addr++] = tmp;
                   10616:                        }
                   10617:                } else {
                   10618:                        REG8(AH) = 0x93;
                   10619:                        return;
                   10620:                }
                   10621:        }
                   10622:        REG8(AH) = 0x80;
                   10623: }
                   10624: 
                   10625: inline void msdos_int_67h_57h()
                   10626: {
                   10627:        if(!support_ems) {
                   10628:                REG8(AH) = 0x84;
                   10629:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   10630:                struct {
                   10631:                        UINT16 handle;
                   10632:                        UINT16 page;
                   10633:                        bool mapped;
                   10634:                } tmp_pages[4];
                   10635:                
                   10636:                // unmap pages to copy memory data to ems buffer
                   10637:                for(int i = 0; i < 4; i++) {
                   10638:                        tmp_pages[i].handle = ems_pages[i].handle;
                   10639:                        tmp_pages[i].page   = ems_pages[i].page;
                   10640:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   10641:                        ems_unmap_page(i);
                   10642:                }
                   10643:                
                   10644:                // run move/exchange operation
                   10645:                msdos_int_67h_57h_tmp();
                   10646:                
                   10647:                // restore unmapped pages
                   10648:                for(int i = 0; i < 4; i++) {
                   10649:                        if(tmp_pages[i].mapped) {
                   10650:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   10651:                        }
                   10652:                }
                   10653:        } else {
1.1.1.22  root     10654:                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     10655:                REG8(AH) = 0x8f;
                   10656:        }
                   10657: }
                   10658: 
                   10659: inline void msdos_int_67h_58h()
                   10660: {
                   10661:        if(!support_ems) {
                   10662:                REG8(AH) = 0x84;
                   10663:        } else if(REG8(AL) == 0x00) {
                   10664:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     10665:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   10666:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     10667:                }
                   10668:                REG8(AH) = 0x00;
                   10669:                REG16(CX) = 4;
                   10670:        } else if(REG8(AL) == 0x01) {
                   10671:                REG8(AH) = 0x00;
                   10672:                REG16(CX) = 4;
                   10673:        } else {
1.1.1.22  root     10674:                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     10675:                REG8(AH) = 0x8f;
                   10676:        }
                   10677: }
                   10678: 
                   10679: inline void msdos_int_67h_5ah()
                   10680: {
                   10681:        if(!support_ems) {
1.1.1.19  root     10682:                REG8(AH) = 0x84;
1.1.1.20  root     10683:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10684:                REG8(AH) = 0x87;
                   10685:        } else if(REG16(BX) > free_ems_pages) {
                   10686:                REG8(AH) = 0x88;
                   10687: //     } else if(REG16(BX) == 0) {
                   10688: //             REG8(AH) = 0x89;
                   10689:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31! root     10690:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     10691:                        if(!ems_handles[i].allocated) {
                   10692:                                ems_allocate_pages(i, REG16(BX));
                   10693:                                REG8(AH) = 0x00;
                   10694:                                REG16(DX) = i;
                   10695:                                return;
                   10696:                        }
                   10697:                }
                   10698:                REG8(AH) = 0x85;
                   10699:        } else {
1.1.1.22  root     10700:                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     10701:                REG8(AH) = 0x8f;
1.1.1.19  root     10702:        }
                   10703: }
                   10704: 
1.1.1.30  root     10705: inline void msdos_int_67h_deh()
                   10706: {
                   10707:        REG8(AH) = 0x84;
                   10708: }
                   10709: 
1.1.1.19  root     10710: #ifdef SUPPORT_XMS
                   10711: 
1.1.1.26  root     10712: inline void msdos_xms_init()
                   10713: {
1.1.1.30  root     10714:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   10715:        emb_handle_top->address = EMB_TOP;
                   10716:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     10717:        xms_a20_local_enb_count = 0;
                   10718: }
                   10719: 
1.1.1.30  root     10720: inline void msdos_xms_finish()
                   10721: {
                   10722:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   10723:                emb_handle_t *next_handle = emb_handle->next;
                   10724:                free(emb_handle);
                   10725:                emb_handle = next_handle;
                   10726:        }
                   10727: }
                   10728: 
                   10729: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   10730: {
                   10731:        if(handle != 0) {
                   10732:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   10733:                        if(emb_handle->handle == handle) {
                   10734:                                return(emb_handle);
                   10735:                        }
                   10736:                }
                   10737:        }
                   10738:        return(NULL);
                   10739: }
                   10740: 
                   10741: int msdos_xms_get_unused_emb_handle_id()
                   10742: {
                   10743:        for(int handle = 1;; handle++) {
                   10744:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   10745:                        return(handle);
                   10746:                }
                   10747:        }
                   10748:        return(0);
                   10749: }
                   10750: 
                   10751: int msdos_xms_get_unused_emb_handle_count()
                   10752: {
                   10753:        int count = 64; //255;
                   10754:        
                   10755:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   10756:                if(emb_handle->handle != 0) {
                   10757:                        if(--count == 1) {
                   10758:                                break;
                   10759:                        }
                   10760:                }
                   10761:        }
                   10762:        return(count);
                   10763: }
                   10764: 
                   10765: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   10766: {
                   10767:        if(emb_handle->size_kb > size_kb) {
                   10768:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   10769:                
                   10770:                new_handle->address = emb_handle->address + size_kb * 1024;
                   10771:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   10772:                emb_handle->size_kb = size_kb;
                   10773:                
                   10774:                new_handle->prev = emb_handle;
                   10775:                new_handle->next = emb_handle->next;
                   10776:                if(emb_handle->next != NULL) {
                   10777:                        emb_handle->next->prev = new_handle;
                   10778:                }
                   10779:                emb_handle->next = new_handle;
                   10780:        }
                   10781: }
                   10782: 
                   10783: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   10784: {
                   10785:        emb_handle_t *next_handle = emb_handle->next;
                   10786:        
                   10787:        if(next_handle != NULL) {
                   10788:                emb_handle->size_kb += next_handle->size_kb;
                   10789:                
                   10790:                if(next_handle->next != NULL) {
                   10791:                        next_handle->next->prev = emb_handle;
                   10792:                }
                   10793:                emb_handle->next = next_handle->next;
                   10794:                free(next_handle);
                   10795:        }
                   10796: }
                   10797: 
                   10798: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   10799: {
                   10800:        emb_handle_t *target_handle = NULL;
                   10801:        
                   10802:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   10803:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   10804:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   10805:                                target_handle = emb_handle;
                   10806:                        }
                   10807:                }
                   10808:        }
                   10809:        if(target_handle != NULL) {
                   10810:                if(target_handle->size_kb > size_kb) {
                   10811:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   10812:                }
                   10813: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   10814:                return(target_handle);
                   10815:        }
                   10816:        return(NULL);
                   10817: }
                   10818: 
                   10819: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   10820: {
                   10821:        emb_handle_t *prev_handle = emb_handle->prev;
                   10822:        emb_handle_t *next_handle = emb_handle->next;
                   10823:        
                   10824:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   10825:                msdos_xms_combine_emb_handles(prev_handle);
                   10826:                emb_handle = prev_handle;
                   10827:        }
                   10828:        if(next_handle != NULL && next_handle->handle == 0) {
                   10829:                msdos_xms_combine_emb_handles(emb_handle);
                   10830:        }
                   10831:        emb_handle->handle = 0;
                   10832: }
                   10833: 
1.1.1.19  root     10834: inline void msdos_call_xms_00h()
                   10835: {
1.1.1.29  root     10836: #if defined(HAS_I386)
                   10837:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   10838:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   10839: #else
                   10840:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   10841:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   10842: #endif
                   10843: //     REG16(DX) = 0x0000; // HMA does not exist
                   10844:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     10845: }
                   10846: 
                   10847: inline void msdos_call_xms_01h()
                   10848: {
1.1.1.29  root     10849:        if(REG8(AL) == 0x40) {
                   10850:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   10851:                // DX=KB free extended memory returned by last call of function 08h
                   10852:                REG16(AX) = 0x0000;
                   10853:                REG8(BL) = 0x91;
                   10854:                REG16(DX) = xms_dx_after_call_08h;
                   10855:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   10856:                REG16(AX) = 0x0000;
                   10857:                REG8(BL) = 0x81; // Vdisk was detected
                   10858: #ifdef SUPPORT_HMA
                   10859:        } else if(is_hma_used_by_int_2fh) {
                   10860:                REG16(AX) = 0x0000;
                   10861:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   10862:        } else if(is_hma_used_by_xms) {
                   10863:                REG16(AX) = 0x0000;
                   10864:                REG8(BL) = 0x91; // HMA is already in use
                   10865:        } else {
                   10866:                REG16(AX) = 0x0001;
                   10867:                is_hma_used_by_xms = true;
                   10868: #else
                   10869:        } else {
                   10870:                REG16(AX) = 0x0000;
                   10871:                REG8(BL) = 0x91; // HMA is already in use
                   10872: #endif
                   10873:        }
1.1.1.19  root     10874: }
                   10875: 
                   10876: inline void msdos_call_xms_02h()
                   10877: {
1.1.1.29  root     10878:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   10879:                REG16(AX) = 0x0000;
                   10880:                REG8(BL) = 0x81; // Vdisk was detected
                   10881: #ifdef SUPPORT_HMA
                   10882:        } else if(is_hma_used_by_int_2fh) {
                   10883:                REG16(AX) = 0x0000;
                   10884:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   10885:        } else if(!is_hma_used_by_xms) {
                   10886:                REG16(AX) = 0x0000;
                   10887:                REG8(BL) = 0x93; // HMA is not allocated
                   10888:        } else {
                   10889:                REG16(AX) = 0x0001;
                   10890:                is_hma_used_by_xms = false;
                   10891:                // restore first free mcb in high memory area
                   10892:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   10893: #else
                   10894:        } else {
                   10895:                REG16(AX) = 0x0000;
                   10896:                REG8(BL) = 0x91; // HMA is already in use
                   10897: #endif
                   10898:        }
1.1.1.19  root     10899: }
                   10900: 
                   10901: inline void msdos_call_xms_03h()
                   10902: {
                   10903:        i386_set_a20_line(1);
                   10904:        REG16(AX) = 0x0001;
                   10905:        REG8(BL) = 0x00;
                   10906: }
                   10907: 
                   10908: inline void msdos_call_xms_04h()
                   10909: {
1.1.1.21  root     10910:        i386_set_a20_line(0);
                   10911:        REG16(AX) = 0x0001;
                   10912:        REG8(BL) = 0x00;
1.1.1.19  root     10913: }
                   10914: 
                   10915: inline void msdos_call_xms_05h()
                   10916: {
                   10917:        i386_set_a20_line(1);
                   10918:        REG16(AX) = 0x0001;
                   10919:        REG8(BL) = 0x00;
1.1.1.21  root     10920:        xms_a20_local_enb_count++;
1.1.1.19  root     10921: }
                   10922: 
                   10923: void msdos_call_xms_06h()
                   10924: {
1.1.1.21  root     10925:        if(xms_a20_local_enb_count > 0) {
                   10926:                xms_a20_local_enb_count--;
                   10927:        }
                   10928:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     10929:                i386_set_a20_line(0);
1.1.1.21  root     10930:        }
                   10931:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     10932:                REG16(AX) = 0x0000;
                   10933:                REG8(BL) = 0x94;
1.1.1.21  root     10934:        } else {
                   10935:                REG16(AX) = 0x0001;
                   10936:                REG8(BL) = 0x00;
1.1.1.19  root     10937:        }
                   10938: }
                   10939: 
                   10940: inline void msdos_call_xms_07h()
                   10941: {
                   10942:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   10943:        REG8(BL) = 0x00;
                   10944: }
                   10945: 
                   10946: inline void msdos_call_xms_08h()
                   10947: {
                   10948:        REG16(AX) = REG16(DX) = 0x0000;
                   10949:        
1.1.1.30  root     10950:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   10951:                if(emb_handle->handle == 0) {
                   10952:                        if(REG16(AX) < emb_handle->size_kb) {
                   10953:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     10954:                        }
1.1.1.30  root     10955:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     10956:                }
                   10957:        }
                   10958:        
                   10959:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   10960:                REG8(BL) = 0xa0;
                   10961:        } else {
                   10962:                REG8(BL) = 0x00;
                   10963:        }
1.1.1.29  root     10964:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     10965: }
                   10966: 
1.1.1.30  root     10967: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     10968: {
1.1.1.30  root     10969:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   10970:        
                   10971:        if(emb_handle != NULL) {
                   10972:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   10973:                
                   10974:                REG16(AX) = 0x0001;
                   10975:                REG16(DX) = emb_handle->handle;
                   10976:                REG8(BL) = 0x00;
                   10977:        } else {
                   10978:                REG16(AX) = REG16(DX) = 0x0000;
                   10979:                REG8(BL) = 0xa0;
1.1.1.19  root     10980:        }
1.1.1.30  root     10981: }
                   10982: 
                   10983: inline void msdos_call_xms_09h()
                   10984: {
                   10985:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     10986: }
                   10987: 
                   10988: inline void msdos_call_xms_0ah()
                   10989: {
1.1.1.30  root     10990:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   10991:        
                   10992:        if(emb_handle == NULL) {
1.1.1.19  root     10993:                REG16(AX) = 0x0000;
                   10994:                REG8(BL) = 0xa2;
1.1.1.30  root     10995:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     10996:                REG16(AX) = 0x0000;
                   10997:                REG8(BL) = 0xab;
                   10998:        } else {
1.1.1.30  root     10999:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     11000:                
                   11001:                REG16(AX) = 0x0001;
                   11002:                REG8(BL) = 0x00;
                   11003:        }
                   11004: }
                   11005: 
                   11006: inline void msdos_call_xms_0bh()
                   11007: {
                   11008:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   11009:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   11010:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   11011:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   11012:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   11013:        
                   11014:        UINT8 *src_buffer, *dest_buffer;
                   11015:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     11016:        emb_handle_t *emb_handle;
1.1.1.19  root     11017:        
                   11018:        if(src_handle == 0) {
                   11019:                src_buffer = mem;
                   11020:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   11021:                src_addr_max = MAX_MEM;
1.1.1.30  root     11022: 
1.1.1.19  root     11023:        } else {
1.1.1.30  root     11024:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     11025:                        REG16(AX) = 0x0000;
                   11026:                        REG8(BL) = 0xa3;
                   11027:                        return;
                   11028:                }
1.1.1.30  root     11029:                src_buffer = mem + emb_handle->address;
                   11030:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     11031:        }
                   11032:        if(dest_handle == 0) {
                   11033:                dest_buffer = mem;
                   11034:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   11035:                dest_addr_max = MAX_MEM;
                   11036:        } else {
1.1.1.30  root     11037:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     11038:                        REG16(AX) = 0x0000;
                   11039:                        REG8(BL) = 0xa5;
                   11040:                        return;
                   11041:                }
1.1.1.30  root     11042:                dest_buffer = mem + emb_handle->address;
                   11043:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     11044:        }
                   11045:        for(int i = 0; i < copy_length; i++) {
                   11046:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   11047:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   11048:                } else {
                   11049:                        break;
                   11050:                }
                   11051:        }
                   11052:        REG16(AX) = 0x0001;
                   11053:        REG8(BL) = 0x00;
                   11054: }
                   11055: 
                   11056: inline void msdos_call_xms_0ch()
                   11057: {
1.1.1.30  root     11058:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   11059:        
                   11060:        if(emb_handle == NULL) {
1.1.1.19  root     11061:                REG16(AX) = 0x0000;
                   11062:                REG8(BL) = 0xa2;
                   11063:        } else {
1.1.1.30  root     11064:                emb_handle->lock++;
1.1.1.19  root     11065:                REG16(AX) = 0x0001;
                   11066:                REG8(BL) = 0x00;
1.1.1.30  root     11067:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   11068:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     11069:        }
                   11070: }
                   11071: 
                   11072: inline void msdos_call_xms_0dh()
                   11073: {
1.1.1.30  root     11074:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   11075:        
                   11076:        if(emb_handle == NULL) {
1.1.1.19  root     11077:                REG16(AX) = 0x0000;
                   11078:                REG8(BL) = 0xa2;
1.1.1.30  root     11079:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     11080:                REG16(AX) = 0x0000;
                   11081:                REG8(BL) = 0xaa;
                   11082:        } else {
1.1.1.30  root     11083:                emb_handle->lock--;
1.1.1.19  root     11084:                REG16(AX) = 0x0001;
                   11085:                REG8(BL) = 0x00;
                   11086:        }
                   11087: }
                   11088: 
                   11089: inline void msdos_call_xms_0eh()
                   11090: {
1.1.1.30  root     11091:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   11092:        
                   11093:        if(emb_handle == NULL) {
1.1.1.19  root     11094:                REG16(AX) = 0x0000;
                   11095:                REG8(BL) = 0xa2;
                   11096:        } else {
                   11097:                REG16(AX) = 0x0001;
1.1.1.30  root     11098:                REG8(BH) = emb_handle->lock;
                   11099:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   11100:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     11101:        }
                   11102: }
                   11103: 
1.1.1.30  root     11104: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     11105: {
1.1.1.30  root     11106:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   11107:        
                   11108:        if(emb_handle == NULL) {
1.1.1.19  root     11109:                REG16(AX) = 0x0000;
                   11110:                REG8(BL) = 0xa2;
1.1.1.30  root     11111:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     11112:                REG16(AX) = 0x0000;
                   11113:                REG8(BL) = 0xab;
                   11114:        } else {
1.1.1.30  root     11115:                if(emb_handle->size_kb < size_kb) {
                   11116:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   11117:                                msdos_xms_combine_emb_handles(emb_handle);
                   11118:                                if(emb_handle->size_kb > size_kb) {
                   11119:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   11120:                                }
                   11121:                        } else {
                   11122:                                int old_handle = emb_handle->handle;
                   11123:                                int old_size_kb = emb_handle->size_kb;
                   11124:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   11125:                                
                   11126:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   11127:                                msdos_xms_free_emb_handle(emb_handle);
                   11128:                                
                   11129:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   11130:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   11131:                                }
                   11132:                                emb_handle->handle = old_handle;
                   11133:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   11134:                                free(buffer);
                   11135:                        }
                   11136:                } else if(emb_handle->size_kb > size_kb) {
                   11137:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   11138:                }
                   11139:                if(emb_handle->size_kb != size_kb) {
                   11140:                        REG16(AX) = 0x0000;
                   11141:                        REG8(BL) = 0xa0;
                   11142:                } else {
                   11143:                        REG16(AX) = 0x0001;
                   11144:                        REG8(BL) = 0x00;
                   11145:                }
1.1.1.19  root     11146:        }
                   11147: }
                   11148: 
1.1.1.30  root     11149: inline void msdos_call_xms_0fh()
                   11150: {
                   11151:        msdos_call_xms_0fh(REG16(BX));
                   11152: }
                   11153: 
1.1.1.19  root     11154: inline void msdos_call_xms_10h()
                   11155: {
                   11156:        int seg;
                   11157:        
                   11158:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   11159:                REG16(AX) = 0x0001;
                   11160:                REG16(BX) = seg;
                   11161:        } else {
                   11162:                REG16(AX) = 0x0000;
                   11163:                REG8(BL) = 0xb0;
                   11164:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11165:        }
                   11166: }
                   11167: 
                   11168: inline void msdos_call_xms_11h()
                   11169: {
                   11170:        int mcb_seg = REG16(DX) - 1;
                   11171:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11172:        
                   11173:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11174:                msdos_mem_free(REG16(DX));
                   11175:                REG16(AX) = 0x0001;
                   11176:                REG8(BL) = 0x00;
                   11177:        } else {
                   11178:                REG16(AX) = 0x0000;
                   11179:                REG8(BL) = 0xb2;
                   11180:        }
                   11181: }
                   11182: 
                   11183: inline void msdos_call_xms_12h()
                   11184: {
                   11185:        int mcb_seg = REG16(DX) - 1;
                   11186:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11187:        int max_paragraphs;
                   11188:        
                   11189:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11190:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   11191:                        REG16(AX) = 0x0001;
                   11192:                        REG8(BL) = 0x00;
                   11193:                } else {
                   11194:                        REG16(AX) = 0x0000;
                   11195:                        REG8(BL) = 0xb0;
                   11196:                        REG16(DX) = max_paragraphs;
                   11197:                }
                   11198:        } else {
                   11199:                REG16(AX) = 0x0000;
                   11200:                REG8(BL) = 0xb2;
                   11201:        }
                   11202: }
                   11203: 
1.1.1.29  root     11204: #if defined(HAS_I386)
                   11205: 
                   11206: inline void msdos_call_xms_88h()
                   11207: {
                   11208:        REG32(EAX) = REG32(EDX) = 0x0000;
                   11209:        
1.1.1.30  root     11210:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   11211:                if(emb_handle->handle == 0) {
                   11212:                        if(REG32(EAX) < emb_handle->size_kb) {
                   11213:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     11214:                        }
1.1.1.30  root     11215:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     11216:                }
                   11217:        }
                   11218:        
                   11219:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   11220:                REG8(BL) = 0xa0;
                   11221:        } else {
                   11222:                REG8(BL) = 0x00;
                   11223:        }
                   11224:        REG32(ECX) = EMB_END - 1;
                   11225: }
                   11226: 
                   11227: inline void msdos_call_xms_89h()
                   11228: {
1.1.1.30  root     11229:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     11230: }
                   11231: 
                   11232: inline void msdos_call_xms_8eh()
                   11233: {
1.1.1.30  root     11234:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   11235:        
                   11236:        if(emb_handle == NULL) {
1.1.1.29  root     11237:                REG16(AX) = 0x0000;
                   11238:                REG8(BL) = 0xa2;
                   11239:        } else {
                   11240:                REG16(AX) = 0x0001;
1.1.1.30  root     11241:                REG8(BH) = emb_handle->lock;
                   11242:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   11243:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     11244:        }
                   11245: }
                   11246: 
                   11247: inline void msdos_call_xms_8fh()
                   11248: {
1.1.1.30  root     11249:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     11250: }
                   11251: 
                   11252: #endif
1.1.1.19  root     11253: #endif
                   11254: 
1.1.1.26  root     11255: UINT16 msdos_get_equipment()
                   11256: {
                   11257:        static UINT16 equip = 0;
                   11258:        
                   11259:        if(equip == 0) {
                   11260: #ifdef SUPPORT_FPU
                   11261:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   11262: #endif
                   11263:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   11264:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   11265: //             equip |= (1 << 8);      // 0 if DMA installed
                   11266:                equip |= (2 << 9);      // number of serial ports
                   11267:                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     11268:                
                   11269:                // check only A: and B: if it is floppy drive
                   11270:                DWORD dwDrives = GetLogicalDrives();
                   11271:                int n = 0;
                   11272:                for(int i = 0; i < 2; i++) {
                   11273:                        if(dwDrives & (1 << i)) {
                   11274:                                char volume[] = "A:\\";
                   11275:                                volume[0] = 'A' + i;
                   11276:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   11277:                                        n++;
                   11278:                                }
                   11279:                        }
                   11280:                }
                   11281:                if(n != 0) {
                   11282:                        equip |= (1 << 0);      // floppy disk(s) installed
                   11283:                        n--;
                   11284:                        equip |= (n << 6);      // number of floppies installed less 1
                   11285:                }
                   11286: //             if(joyGetNumDevs() != 0) {
                   11287: //                     equip |= (1 << 12);     // game port installed
                   11288: //             }
1.1.1.26  root     11289:        }
                   11290:        return(equip);
                   11291: }
                   11292: 
1.1       root     11293: void msdos_syscall(unsigned num)
                   11294: {
1.1.1.22  root     11295: #ifdef ENABLE_DEBUG_SYSCALL
                   11296:        if(num == 0x68) {
                   11297:                // dummy interrupt for EMS (int 67h)
                   11298:                fprintf(fdebug, "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));
                   11299:        } else if(num == 0x69) {
                   11300:                // dummy interrupt for XMS (call far)
                   11301:                fprintf(fdebug, "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));
                   11302:        } else if(num == 0x6a) {
                   11303:                // dummy interrupt for case map routine pointed in the country info
                   11304:        } else {
                   11305:                fprintf(fdebug, "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));
                   11306:        }
                   11307: #endif
1.1.1.26  root     11308:        ctrl_c_pressed = ctrl_c_detected = false;
1.1.1.22  root     11309:        
1.1       root     11310:        switch(num) {
                   11311:        case 0x00:
1.1.1.28  root     11312:                try {
                   11313:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11314:                        error("division by zero\n");
                   11315:                } catch(...) {
                   11316:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   11317:                }
1.1       root     11318:                break;
                   11319:        case 0x04:
1.1.1.28  root     11320:                try {
                   11321:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11322:                        error("overflow\n");
                   11323:                } catch(...) {
                   11324:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   11325:                }
1.1       root     11326:                break;
                   11327:        case 0x06:
                   11328:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     11329:                if(!ignore_illegal_insn) {
1.1.1.28  root     11330:                        try {
                   11331:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11332:                                error("illegal instruction\n");
                   11333:                        } catch(...) {
                   11334:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   11335:                        }
1.1.1.14  root     11336:                } else {
                   11337: #if defined(HAS_I386)
                   11338:                        m_eip++;
                   11339: #else
                   11340:                        m_pc++;
                   11341: #endif
                   11342:                }
1.1       root     11343:                break;
1.1.1.8   root     11344:        case 0x08:
1.1.1.14  root     11345: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     11346:        case 0x09:
                   11347:        case 0x0a:
                   11348:        case 0x0b:
                   11349:        case 0x0c:
                   11350:        case 0x0d:
                   11351:        case 0x0e:
                   11352:        case 0x0f:
                   11353:                // EOI
                   11354:                pic[0].isr &= ~(1 << (num - 0x08));
                   11355:                pic_update();
                   11356:                break;
1.1       root     11357:        case 0x10:
                   11358:                // PC BIOS - Video
1.1.1.14  root     11359:                if(!restore_console_on_exit) {
1.1.1.15  root     11360:                        change_console_size(scr_width, scr_height);
1.1       root     11361:                }
1.1.1.3   root     11362:                m_CF = 0;
1.1       root     11363:                switch(REG8(AH)) {
1.1.1.16  root     11364:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     11365:                case 0x01: pcbios_int_10h_01h(); break;
                   11366:                case 0x02: pcbios_int_10h_02h(); break;
                   11367:                case 0x03: pcbios_int_10h_03h(); break;
                   11368:                case 0x05: pcbios_int_10h_05h(); break;
                   11369:                case 0x06: pcbios_int_10h_06h(); break;
                   11370:                case 0x07: pcbios_int_10h_07h(); break;
                   11371:                case 0x08: pcbios_int_10h_08h(); break;
                   11372:                case 0x09: pcbios_int_10h_09h(); break;
                   11373:                case 0x0a: pcbios_int_10h_0ah(); break;
                   11374:                case 0x0b: break;
                   11375:                case 0x0c: break;
                   11376:                case 0x0d: break;
                   11377:                case 0x0e: pcbios_int_10h_0eh(); break;
                   11378:                case 0x0f: pcbios_int_10h_0fh(); break;
                   11379:                case 0x10: break;
1.1.1.14  root     11380:                case 0x11: pcbios_int_10h_11h(); break;
                   11381:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     11382:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     11383:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     11384:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     11385:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   11386:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     11387:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     11388:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   11389:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     11390:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     11391:                case 0x6f: break;
1.1.1.22  root     11392:                case 0x80: m_CF = 1; break; // unknown
                   11393:                case 0x81: m_CF = 1; break; // unknown
1.1       root     11394:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     11395:                case 0x83: pcbios_int_10h_83h(); break;
                   11396:                case 0x8b: break;
                   11397:                case 0x8c: m_CF = 1; break; // unknown
                   11398:                case 0x8d: m_CF = 1; break; // unknown
                   11399:                case 0x8e: m_CF = 1; break; // unknown
                   11400:                case 0x90: pcbios_int_10h_90h(); break;
                   11401:                case 0x91: pcbios_int_10h_91h(); break;
                   11402:                case 0x92: break;
                   11403:                case 0x93: break;
                   11404:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     11405:                case 0xfa: break; // ega register interface library is not installed
1.1       root     11406:                case 0xfe: pcbios_int_10h_feh(); break;
                   11407:                case 0xff: pcbios_int_10h_ffh(); break;
                   11408:                default:
1.1.1.22  root     11409:                        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));
                   11410:                        m_CF = 1;
1.1       root     11411:                        break;
                   11412:                }
                   11413:                break;
                   11414:        case 0x11:
                   11415:                // PC BIOS - Get Equipment List
1.1.1.26  root     11416:                REG16(AX) = msdos_get_equipment();
1.1       root     11417:                break;
                   11418:        case 0x12:
                   11419:                // PC BIOS - Get Memory Size
                   11420:                REG16(AX) = MEMORY_END / 1024;
                   11421:                break;
                   11422:        case 0x13:
                   11423:                // PC BIOS - Disk
1.1.1.22  root     11424: //             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     11425:                REG8(AH) = 0xff;
1.1.1.3   root     11426:                m_CF = 1;
1.1       root     11427:                break;
                   11428:        case 0x14:
                   11429:                // PC BIOS - Serial I/O
1.1.1.25  root     11430:                switch(REG8(AH)) {
                   11431:                case 0x00: pcbios_int_14h_00h(); break;
                   11432:                case 0x01: pcbios_int_14h_01h(); break;
                   11433:                case 0x02: pcbios_int_14h_02h(); break;
                   11434:                case 0x03: pcbios_int_14h_03h(); break;
                   11435:                case 0x04: pcbios_int_14h_04h(); break;
                   11436:                case 0x05: pcbios_int_14h_05h(); break;
                   11437:                default:
                   11438:                        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));
                   11439:                        break;
                   11440:                }
1.1       root     11441:                break;
                   11442:        case 0x15:
                   11443:                // PC BIOS
1.1.1.3   root     11444:                m_CF = 0;
1.1       root     11445:                switch(REG8(AH)) {
1.1.1.14  root     11446:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     11447:                case 0x23: pcbios_int_15h_23h(); break;
                   11448:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     11449:                case 0x41: break;
1.1       root     11450:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     11451:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     11452:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     11453:                case 0x86: pcbios_int_15h_86h(); break;
                   11454:                case 0x87: pcbios_int_15h_87h(); break;
                   11455:                case 0x88: pcbios_int_15h_88h(); break;
                   11456:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     11457:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     11458:                case 0xc0: // PS/2 ???
                   11459:                case 0xc1:
                   11460:                case 0xc2:
1.1.1.30  root     11461:                case 0xc3: // PS50+ ???
                   11462:                case 0xc4:
1.1.1.22  root     11463:                        REG8(AH) = 0x86;
                   11464:                        m_CF = 1;
                   11465:                        break;
1.1.1.3   root     11466: #if defined(HAS_I386)
1.1       root     11467:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     11468: #endif
1.1       root     11469:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     11470:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     11471:                default:
1.1.1.22  root     11472:                        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));
                   11473:                        REG8(AH) = 0x86;
1.1.1.3   root     11474:                        m_CF = 1;
1.1       root     11475:                        break;
                   11476:                }
                   11477:                break;
                   11478:        case 0x16:
                   11479:                // PC BIOS - Keyboard
1.1.1.3   root     11480:                m_CF = 0;
1.1       root     11481:                switch(REG8(AH)) {
                   11482:                case 0x00: pcbios_int_16h_00h(); break;
                   11483:                case 0x01: pcbios_int_16h_01h(); break;
                   11484:                case 0x02: pcbios_int_16h_02h(); break;
                   11485:                case 0x03: pcbios_int_16h_03h(); break;
                   11486:                case 0x05: pcbios_int_16h_05h(); break;
                   11487:                case 0x10: pcbios_int_16h_00h(); break;
                   11488:                case 0x11: pcbios_int_16h_01h(); break;
                   11489:                case 0x12: pcbios_int_16h_12h(); break;
                   11490:                case 0x13: pcbios_int_16h_13h(); break;
                   11491:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     11492:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     11493:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     11494:                case 0xda: break; // unknown
                   11495:                case 0xff: break; // unknown
1.1       root     11496:                default:
1.1.1.22  root     11497:                        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     11498:                        break;
                   11499:                }
                   11500:                break;
                   11501:        case 0x17:
                   11502:                // PC BIOS - Printer
1.1.1.22  root     11503: //             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     11504:                break;
                   11505:        case 0x1a:
                   11506:                // PC BIOS - Timer
1.1.1.3   root     11507:                m_CF = 0;
1.1       root     11508:                switch(REG8(AH)) {
                   11509:                case 0x00: pcbios_int_1ah_00h(); break;
                   11510:                case 0x01: break;
                   11511:                case 0x02: pcbios_int_1ah_02h(); break;
                   11512:                case 0x03: break;
                   11513:                case 0x04: pcbios_int_1ah_04h(); break;
                   11514:                case 0x05: break;
                   11515:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   11516:                case 0x0b: break;
1.1.1.14  root     11517:                case 0x35: break; // Word Perfect Third Party Interface?
                   11518:                case 0x36: break; // Word Perfect Third Party Interface
                   11519:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     11520:                default:
1.1.1.22  root     11521:                        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     11522:                        break;
                   11523:                }
                   11524:                break;
                   11525:        case 0x20:
1.1.1.28  root     11526:                try {
                   11527:                        msdos_process_terminate(SREG(CS), retval, 1);
                   11528:                } catch(...) {
                   11529:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   11530:                }
1.1       root     11531:                break;
                   11532:        case 0x21:
                   11533:                // MS-DOS System Call
1.1.1.3   root     11534:                m_CF = 0;
1.1.1.28  root     11535:                try {
                   11536:                        switch(REG8(AH)) {
                   11537:                        case 0x00: msdos_int_21h_00h(); break;
                   11538:                        case 0x01: msdos_int_21h_01h(); break;
                   11539:                        case 0x02: msdos_int_21h_02h(); break;
                   11540:                        case 0x03: msdos_int_21h_03h(); break;
                   11541:                        case 0x04: msdos_int_21h_04h(); break;
                   11542:                        case 0x05: msdos_int_21h_05h(); break;
                   11543:                        case 0x06: msdos_int_21h_06h(); break;
                   11544:                        case 0x07: msdos_int_21h_07h(); break;
                   11545:                        case 0x08: msdos_int_21h_08h(); break;
                   11546:                        case 0x09: msdos_int_21h_09h(); break;
                   11547:                        case 0x0a: msdos_int_21h_0ah(); break;
                   11548:                        case 0x0b: msdos_int_21h_0bh(); break;
                   11549:                        case 0x0c: msdos_int_21h_0ch(); break;
                   11550:                        case 0x0d: msdos_int_21h_0dh(); break;
                   11551:                        case 0x0e: msdos_int_21h_0eh(); break;
                   11552:                        case 0x0f: msdos_int_21h_0fh(); break;
                   11553:                        case 0x10: msdos_int_21h_10h(); break;
                   11554:                        case 0x11: msdos_int_21h_11h(); break;
                   11555:                        case 0x12: msdos_int_21h_12h(); break;
                   11556:                        case 0x13: msdos_int_21h_13h(); break;
                   11557:                        case 0x14: msdos_int_21h_14h(); break;
                   11558:                        case 0x15: msdos_int_21h_15h(); break;
                   11559:                        case 0x16: msdos_int_21h_16h(); break;
                   11560:                        case 0x17: msdos_int_21h_17h(); break;
                   11561:                        case 0x18: msdos_int_21h_18h(); break;
                   11562:                        case 0x19: msdos_int_21h_19h(); break;
                   11563:                        case 0x1a: msdos_int_21h_1ah(); break;
                   11564:                        case 0x1b: msdos_int_21h_1bh(); break;
                   11565:                        case 0x1c: msdos_int_21h_1ch(); break;
                   11566:                        case 0x1d: msdos_int_21h_1dh(); break;
                   11567:                        case 0x1e: msdos_int_21h_1eh(); break;
                   11568:                        case 0x1f: msdos_int_21h_1fh(); break;
                   11569:                        case 0x20: msdos_int_21h_20h(); break;
                   11570:                        case 0x21: msdos_int_21h_21h(); break;
                   11571:                        case 0x22: msdos_int_21h_22h(); break;
                   11572:                        case 0x23: msdos_int_21h_23h(); break;
                   11573:                        case 0x24: msdos_int_21h_24h(); break;
                   11574:                        case 0x25: msdos_int_21h_25h(); break;
                   11575:                        case 0x26: msdos_int_21h_26h(); break;
                   11576:                        case 0x27: msdos_int_21h_27h(); break;
                   11577:                        case 0x28: msdos_int_21h_28h(); break;
                   11578:                        case 0x29: msdos_int_21h_29h(); break;
                   11579:                        case 0x2a: msdos_int_21h_2ah(); break;
                   11580:                        case 0x2b: msdos_int_21h_2bh(); break;
                   11581:                        case 0x2c: msdos_int_21h_2ch(); break;
                   11582:                        case 0x2d: msdos_int_21h_2dh(); break;
                   11583:                        case 0x2e: msdos_int_21h_2eh(); break;
                   11584:                        case 0x2f: msdos_int_21h_2fh(); break;
                   11585:                        case 0x30: msdos_int_21h_30h(); break;
                   11586:                        case 0x31: msdos_int_21h_31h(); break;
                   11587:                        case 0x32: msdos_int_21h_32h(); break;
                   11588:                        case 0x33: msdos_int_21h_33h(); break;
                   11589:                        case 0x34: msdos_int_21h_34h(); break;
                   11590:                        case 0x35: msdos_int_21h_35h(); break;
                   11591:                        case 0x36: msdos_int_21h_36h(); break;
                   11592:                        case 0x37: msdos_int_21h_37h(); break;
                   11593:                        case 0x38: msdos_int_21h_38h(); break;
                   11594:                        case 0x39: msdos_int_21h_39h(0); break;
                   11595:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   11596:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   11597:                        case 0x3c: msdos_int_21h_3ch(); break;
                   11598:                        case 0x3d: msdos_int_21h_3dh(); break;
                   11599:                        case 0x3e: msdos_int_21h_3eh(); break;
                   11600:                        case 0x3f: msdos_int_21h_3fh(); break;
                   11601:                        case 0x40: msdos_int_21h_40h(); break;
                   11602:                        case 0x41: msdos_int_21h_41h(0); break;
                   11603:                        case 0x42: msdos_int_21h_42h(); break;
                   11604:                        case 0x43: msdos_int_21h_43h(0); break;
                   11605:                        case 0x44: msdos_int_21h_44h(); break;
                   11606:                        case 0x45: msdos_int_21h_45h(); break;
                   11607:                        case 0x46: msdos_int_21h_46h(); break;
                   11608:                        case 0x47: msdos_int_21h_47h(0); break;
                   11609:                        case 0x48: msdos_int_21h_48h(); break;
                   11610:                        case 0x49: msdos_int_21h_49h(); break;
                   11611:                        case 0x4a: msdos_int_21h_4ah(); break;
                   11612:                        case 0x4b: msdos_int_21h_4bh(); break;
                   11613:                        case 0x4c: msdos_int_21h_4ch(); break;
                   11614:                        case 0x4d: msdos_int_21h_4dh(); break;
                   11615:                        case 0x4e: msdos_int_21h_4eh(); break;
                   11616:                        case 0x4f: msdos_int_21h_4fh(); break;
                   11617:                        case 0x50: msdos_int_21h_50h(); break;
                   11618:                        case 0x51: msdos_int_21h_51h(); break;
                   11619:                        case 0x52: msdos_int_21h_52h(); break;
                   11620:                        // 0x53: translate bios parameter block to drive param bock
                   11621:                        case 0x54: msdos_int_21h_54h(); break;
                   11622:                        case 0x55: msdos_int_21h_55h(); break;
                   11623:                        case 0x56: msdos_int_21h_56h(0); break;
                   11624:                        case 0x57: msdos_int_21h_57h(); break;
                   11625:                        case 0x58: msdos_int_21h_58h(); break;
                   11626:                        case 0x59: msdos_int_21h_59h(); break;
                   11627:                        case 0x5a: msdos_int_21h_5ah(); break;
                   11628:                        case 0x5b: msdos_int_21h_5bh(); break;
                   11629:                        case 0x5c: msdos_int_21h_5ch(); break;
                   11630:                        case 0x5d: msdos_int_21h_5dh(); break;
                   11631:                        // 0x5e: ms-network
1.1.1.30  root     11632:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     11633:                        case 0x60: msdos_int_21h_60h(0); break;
                   11634:                        case 0x61: msdos_int_21h_61h(); break;
                   11635:                        case 0x62: msdos_int_21h_62h(); break;
                   11636:                        case 0x63: msdos_int_21h_63h(); break;
                   11637:                        // 0x64: set device driver lockahead flag
                   11638:                        case 0x65: msdos_int_21h_65h(); break;
                   11639:                        case 0x66: msdos_int_21h_66h(); break;
                   11640:                        case 0x67: msdos_int_21h_67h(); break;
                   11641:                        case 0x68: msdos_int_21h_68h(); break;
                   11642:                        case 0x69: msdos_int_21h_69h(); break;
                   11643:                        case 0x6a: msdos_int_21h_6ah(); break;
                   11644:                        case 0x6b: msdos_int_21h_6bh(); break;
                   11645:                        case 0x6c: msdos_int_21h_6ch(0); break;
                   11646:                        // 0x6d: find first rom program
                   11647:                        // 0x6e: find next rom program
                   11648:                        // 0x6f: get/set rom scan start address
                   11649:                        // 0x70: windows95 get/set internationalization information
                   11650:                        case 0x71:
                   11651:                                // windows95 long filename functions
                   11652:                                switch(REG8(AL)) {
                   11653:                                case 0x0d: msdos_int_21h_710dh(); break;
                   11654:                                case 0x39: msdos_int_21h_39h(1); break;
                   11655:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   11656:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   11657:                                case 0x41: msdos_int_21h_7141h(1); break;
                   11658:                                case 0x43: msdos_int_21h_43h(1); break;
                   11659:                                case 0x47: msdos_int_21h_47h(1); break;
                   11660:                                case 0x4e: msdos_int_21h_714eh(); break;
                   11661:                                case 0x4f: msdos_int_21h_714fh(); break;
                   11662:                                case 0x56: msdos_int_21h_56h(1); break;
                   11663:                                case 0x60: msdos_int_21h_60h(1); break;
                   11664:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   11665:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   11666:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   11667:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   11668:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   11669:                                case 0xa8: msdos_int_21h_71a8h(); break;
                   11670:                                // 0xa9: server create/open file
                   11671:                                case 0xaa: msdos_int_21h_71aah(); break;
                   11672:                                default:
                   11673:                                        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));
                   11674:                                        REG16(AX) = 0x7100;
                   11675:                                        m_CF = 1;
                   11676:                                        break;
                   11677:                                }
                   11678:                                break;
                   11679:                        // 0x72: Windows95 beta - LFN FindClose
                   11680:                        case 0x73:
                   11681:                                // windows95 fat32 functions
                   11682:                                switch(REG8(AL)) {
                   11683:                                case 0x00: msdos_int_21h_7300h(); break;
                   11684:                                // 0x01: set drive locking ???
                   11685:                                case 0x02: msdos_int_21h_7302h(); break;
                   11686:                                case 0x03: msdos_int_21h_7303h(); break;
                   11687:                                // 0x04: set dpb to use for formatting
                   11688:                                // 0x05: extended absolute disk read/write
                   11689:                                default:
                   11690:                                        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));
                   11691:                                        REG16(AX) = 0x7300;
                   11692:                                        m_CF = 1;
                   11693:                                        break;
                   11694:                                }
1.1       root     11695:                                break;
1.1.1.30  root     11696:                        case 0xdb: msdos_int_21h_dbh(); break;
                   11697:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     11698:                        default:
1.1.1.22  root     11699:                                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     11700:                                REG16(AX) = 0x01;
1.1.1.3   root     11701:                                m_CF = 1;
1.1       root     11702:                                break;
                   11703:                        }
1.1.1.28  root     11704:                } catch(int error) {
                   11705:                        REG16(AX) = error;
                   11706:                        m_CF = 1;
                   11707:                } catch(...) {
                   11708:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     11709:                        m_CF = 1;
1.1       root     11710:                }
1.1.1.3   root     11711:                if(m_CF) {
1.1.1.23  root     11712:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11713:                        sda->extended_error_code = REG16(AX);
                   11714:                        switch(sda->extended_error_code) {
                   11715:                        case  4: // Too many open files
                   11716:                        case  8: // Insufficient memory
                   11717:                                sda->error_class = 1; // Out of resource
                   11718:                                break;
                   11719:                        case  5: // Access denied
                   11720:                                sda->error_class = 3; // Authorization
                   11721:                                break;
                   11722:                        case  7: // Memory control block destroyed
                   11723:                                sda->error_class = 4; // Internal
                   11724:                                break;
                   11725:                        case  2: // File not found
                   11726:                        case  3: // Path not found
                   11727:                        case 15: // Invaid drive specified
                   11728:                        case 18: // No more files
                   11729:                                sda->error_class = 8; // Not found
                   11730:                                break;
                   11731:                        case 32: // Sharing violation
                   11732:                        case 33: // Lock violation
                   11733:                                sda->error_class = 10; // Locked
                   11734:                                break;
                   11735: //                     case 16: // Removal of current directory attempted
                   11736:                        case 19: // Attempted write on protected disk
                   11737:                        case 21: // Drive not ready
                   11738: //                     case 29: // Write failure
                   11739: //                     case 30: // Read failure
                   11740: //                     case 82: // Cannot create subdirectory
                   11741:                                sda->error_class = 11; // Media
                   11742:                                break;
                   11743:                        case 80: // File already exists
                   11744:                                sda->error_class = 12; // Already exist
                   11745:                                break;
                   11746:                        default:
                   11747:                                sda->error_class = 13; // Unknown
                   11748:                                break;
                   11749:                        }
                   11750:                        sda->suggested_action = 1; // Retry
                   11751:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     11752:                }
1.1.1.26  root     11753:                if(ctrl_c_checking && ctrl_c_detected) {
                   11754:                        // raise int 23h
                   11755: #if defined(HAS_I386)
                   11756:                        m_ext = 0; // not an external interrupt
                   11757:                        i386_trap(0x23, 1, 0);
                   11758:                        m_ext = 1;
                   11759: #else
                   11760:                        PREFIX86(_interrupt)(0x23);
                   11761: #endif
                   11762:                }
1.1       root     11763:                break;
                   11764:        case 0x22:
                   11765:                fatalerror("int 22h (terminate address)\n");
                   11766:        case 0x23:
1.1.1.28  root     11767:                try {
                   11768:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   11769:                } catch(...) {
                   11770:                        fatalerror("failed to terminate the current process by int 23h\n");
                   11771:                }
1.1       root     11772:                break;
                   11773:        case 0x24:
1.1.1.28  root     11774:                try {
                   11775:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11776:                } catch(...) {
                   11777:                        fatalerror("failed to terminate the current process by int 24h\n");
                   11778:                }
1.1       root     11779:                break;
                   11780:        case 0x25:
                   11781:                msdos_int_25h();
                   11782:                break;
                   11783:        case 0x26:
                   11784:                msdos_int_26h();
                   11785:                break;
                   11786:        case 0x27:
1.1.1.28  root     11787:                try {
                   11788:                        msdos_int_27h();
                   11789:                } catch(...) {
                   11790:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   11791:                }
1.1       root     11792:                break;
                   11793:        case 0x28:
                   11794:                Sleep(10);
                   11795:                break;
                   11796:        case 0x29:
                   11797:                msdos_int_29h();
                   11798:                break;
                   11799:        case 0x2e:
                   11800:                msdos_int_2eh();
                   11801:                break;
                   11802:        case 0x2f:
                   11803:                // multiplex interrupt
                   11804:                switch(REG8(AH)) {
1.1.1.22  root     11805:                case 0x05: msdos_int_2fh_05h(); break;
                   11806:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     11807:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     11808:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     11809:                case 0x14: msdos_int_2fh_14h(); break;
                   11810:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     11811:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     11812:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     11813:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     11814:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     11815:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     11816:                case 0x46: msdos_int_2fh_46h(); break;
                   11817:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     11818:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     11819:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     11820:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     11821:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.30  root     11822:                case 0x80: msdos_int_2fh_80h(); break;
1.1.1.24  root     11823:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     11824:                case 0xae: msdos_int_2fh_aeh(); break;
                   11825:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     11826:                case 0xd7: msdos_int_2fh_d7h(); break;
                   11827:                // Installation Check
                   11828:                case 0x01: // PRINT.COM
                   11829:                case 0x02: // PC LAN Program Redirector
                   11830:                case 0x06: // ASSIGN
                   11831:                case 0x08: // DRIVER.SYS
                   11832:                case 0x10: // SHARE
                   11833:                case 0x17: // Clibboard functions
                   11834:                case 0x1b: // XMA2EMS.SYS
                   11835:                case 0x23: // DR DOS 5.0 GRAFTABL
                   11836:                case 0x27: // DR-DOR 6.0 TaskMAX
                   11837:                case 0x2e: // Novell DOS 7 GRAFTABL
                   11838:                case 0x45: // PROF.COM
                   11839:                case 0x51: // ODIHELP.EXE
                   11840:                case 0x54: // POWER.EXE
                   11841:                case 0x56: // INTERLNK
                   11842:                case 0x70: // License Service API
                   11843:                case 0x7a: // Novell NetWare
                   11844:                case 0x94: // MICRO.EXE
                   11845:                case 0xac: // GRAPHICS.COM
                   11846:                case 0xb0: // GRAFTABLE.COM
                   11847:                case 0xb8: // NETWORK
                   11848:                case 0xb9: // RECEIVER.COM
                   11849:                case 0xbc: // EGA.SYS
                   11850:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   11851:                case 0xc0: // Novell LSL.COM
                   11852:                case 0xd2: // PCL-838.EXE
                   11853:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
                   11854:                        switch(REG8(AL)) {
                   11855:                        case 0x00:
                   11856:                                // This is not installed
                   11857: //                             REG8(AL) = 0x00;
                   11858:                                break;
                   11859:                        default:
                   11860:                                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));
                   11861:                                REG16(AX) = 0x01;
                   11862:                                m_CF = 1;
                   11863:                                break;
                   11864:                        }
                   11865:                        break;
1.1.1.22  root     11866:                default:
                   11867:                        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));
                   11868:                        break;
1.1       root     11869:                }
                   11870:                break;
1.1.1.24  root     11871:        case 0x33:
                   11872:                switch(REG8(AH)) {
                   11873:                case 0x00:
                   11874:                        // Mouse
                   11875:                        switch(REG8(AL)) {
                   11876:                        case 0x00: msdos_int_33h_0000h(); break;
                   11877:                        case 0x01: msdos_int_33h_0001h(); break;
                   11878:                        case 0x02: msdos_int_33h_0002h(); break;
                   11879:                        case 0x03: msdos_int_33h_0003h(); break;
                   11880:                        case 0x04: break; // position mouse cursor
                   11881:                        case 0x05: msdos_int_33h_0005h(); break;
                   11882:                        case 0x06: msdos_int_33h_0006h(); break;
                   11883:                        case 0x07: msdos_int_33h_0007h(); break;
                   11884:                        case 0x08: msdos_int_33h_0008h(); break;
                   11885:                        case 0x09: msdos_int_33h_0009h(); break;
                   11886:                        case 0x0a: break; // define text cursor
                   11887:                        case 0x0b: msdos_int_33h_000bh(); break;
                   11888:                        case 0x0c: msdos_int_33h_000ch(); break;
                   11889:                        case 0x0d: break; // light pen emulation on
                   11890:                        case 0x0e: break; // light pen emulation off
                   11891:                        case 0x0f: msdos_int_33h_000fh(); break;
                   11892:                        case 0x10: break; // define screen region for updating
                   11893:                        case 0x11: msdos_int_33h_0011h(); break;
                   11894:                        case 0x12: REG16(AX) = 0xffff; break; // set large graphics cursor block
                   11895:                        case 0x13: break; // define double-speed threshold
                   11896:                        case 0x14: msdos_int_33h_0014h(); break;
                   11897:                        case 0x15: msdos_int_33h_0015h(); break;
                   11898:                        case 0x16: msdos_int_33h_0016h(); break;
                   11899:                        case 0x17: msdos_int_33h_0017h(); break;
                   11900:                        case 0x1a: msdos_int_33h_001ah(); break;
                   11901:                        case 0x1b: msdos_int_33h_001bh(); break;
                   11902:                        case 0x1d: msdos_int_33h_001dh(); break;
                   11903:                        case 0x1e: msdos_int_33h_001eh(); break;
                   11904:                        case 0x21: msdos_int_33h_0021h(); break;
                   11905:                        case 0x22: msdos_int_33h_0022h(); break;
                   11906:                        case 0x23: msdos_int_33h_0023h(); break;
                   11907:                        case 0x24: msdos_int_33h_0024h(); break;
                   11908:                        case 0x26: msdos_int_33h_0026h(); break;
                   11909:                        case 0x2a: msdos_int_33h_002ah(); break;
                   11910:                        case 0x2f: break; // mouse hardware reset
                   11911:                        case 0x31: msdos_int_33h_0031h(); break;
                   11912:                        case 0x32: msdos_int_33h_0032h(); break;
                   11913:                        default:
                   11914:                                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));
                   11915:                                break;
                   11916:                        }
                   11917:                        break;
                   11918:                default:
                   11919:                        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));
                   11920:                        break;
                   11921:                }
                   11922:                break;
1.1.1.19  root     11923:        case 0x68:
                   11924:                // dummy interrupt for EMS (int 67h)
                   11925:                switch(REG8(AH)) {
                   11926:                case 0x40: msdos_int_67h_40h(); break;
                   11927:                case 0x41: msdos_int_67h_41h(); break;
                   11928:                case 0x42: msdos_int_67h_42h(); break;
                   11929:                case 0x43: msdos_int_67h_43h(); break;
                   11930:                case 0x44: msdos_int_67h_44h(); break;
                   11931:                case 0x45: msdos_int_67h_45h(); break;
                   11932:                case 0x46: msdos_int_67h_46h(); break;
                   11933:                case 0x47: msdos_int_67h_47h(); break;
                   11934:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     11935:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   11936:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     11937:                case 0x4b: msdos_int_67h_4bh(); break;
                   11938:                case 0x4c: msdos_int_67h_4ch(); break;
                   11939:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     11940:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     11941:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     11942:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     11943:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     11944:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     11945:                case 0x53: msdos_int_67h_53h(); break;
                   11946:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     11947:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   11948:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   11949:                case 0x57: msdos_int_67h_57h(); break;
                   11950:                case 0x58: msdos_int_67h_58h(); break;
                   11951:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   11952:                case 0x5a: msdos_int_67h_5ah(); break;
                   11953:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   11954:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   11955:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31! root     11956:                // 0x60: EEMS - Get Physical Window Array
        !          11957:                // 0x61: EEMS - Generic Accelerator Card Support
        !          11958:                // 0x68: EEMS - Get Address of All Pge Frames om System
        !          11959:                // 0x69: EEMS - Map Page into Frame
        !          11960:                // 0x6a: EEMS - Page Mapping
        !          11961:                // 0xde: VCPI
1.1.1.30  root     11962:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     11963:                default:
1.1.1.22  root     11964:                        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     11965:                        REG8(AH) = 0x84;
                   11966:                        break;
                   11967:                }
                   11968:                break;
                   11969: #ifdef SUPPORT_XMS
                   11970:        case 0x69:
                   11971:                // dummy interrupt for XMS (call far)
1.1.1.28  root     11972:                try {
                   11973:                        switch(REG8(AH)) {
                   11974:                        case 0x00: msdos_call_xms_00h(); break;
                   11975:                        case 0x01: msdos_call_xms_01h(); break;
                   11976:                        case 0x02: msdos_call_xms_02h(); break;
                   11977:                        case 0x03: msdos_call_xms_03h(); break;
                   11978:                        case 0x04: msdos_call_xms_04h(); break;
                   11979:                        case 0x05: msdos_call_xms_05h(); break;
                   11980:                        case 0x06: msdos_call_xms_06h(); break;
                   11981:                        case 0x07: msdos_call_xms_07h(); break;
                   11982:                        case 0x08: msdos_call_xms_08h(); break;
                   11983:                        case 0x09: msdos_call_xms_09h(); break;
                   11984:                        case 0x0a: msdos_call_xms_0ah(); break;
                   11985:                        case 0x0b: msdos_call_xms_0bh(); break;
                   11986:                        case 0x0c: msdos_call_xms_0ch(); break;
                   11987:                        case 0x0d: msdos_call_xms_0dh(); break;
                   11988:                        case 0x0e: msdos_call_xms_0eh(); break;
                   11989:                        case 0x0f: msdos_call_xms_0fh(); break;
                   11990:                        case 0x10: msdos_call_xms_10h(); break;
                   11991:                        case 0x11: msdos_call_xms_11h(); break;
                   11992:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     11993: #if defined(HAS_I386)
                   11994:                        case 0x88: msdos_call_xms_88h(); break;
                   11995:                        case 0x89: msdos_call_xms_89h(); break;
                   11996:                        case 0x8e: msdos_call_xms_8eh(); break;
                   11997:                        case 0x8f: msdos_call_xms_8fh(); break;
                   11998: #endif
1.1.1.28  root     11999:                        default:
                   12000:                                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));
                   12001:                                REG16(AX) = 0x0000;
                   12002:                                REG8(BL) = 0x80; // function not implemented
                   12003:                                break;
                   12004:                        }
                   12005:                } catch(...) {
1.1.1.19  root     12006:                        REG16(AX) = 0x0000;
1.1.1.28  root     12007:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     12008:                }
                   12009:                break;
                   12010: #endif
                   12011:        case 0x6a:
1.1.1.24  root     12012:                // irq12 (mouse)
                   12013:                mouse_push_ax = REG16(AX);
                   12014:                mouse_push_bx = REG16(BX);
                   12015:                mouse_push_cx = REG16(CX);
                   12016:                mouse_push_dx = REG16(DX);
                   12017:                mouse_push_si = REG16(SI);
                   12018:                mouse_push_di = REG16(DI);
                   12019:                
                   12020:                if(mouse.active && mouse.call_addr.dw != 0) {
                   12021:                        REG16(AX) = mouse.status_irq;
                   12022:                        REG16(BX) = mouse.get_buttons();
                   12023:                        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   12024:                        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   12025:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   12026:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   12027:                        
                   12028:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   12029:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   12030:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   12031:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   12032:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   12033:                } else {
                   12034:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   12035:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   12036:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   12037:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   12038:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   12039:                }
                   12040:                break;
                   12041:        case 0x6b:
                   12042:                // end of irq12 (mouse)
                   12043:                REG16(AX) = mouse_push_ax;
                   12044:                REG16(BX) = mouse_push_bx;
                   12045:                REG16(CX) = mouse_push_cx;
                   12046:                REG16(DX) = mouse_push_dx;
                   12047:                REG16(SI) = mouse_push_si;
                   12048:                REG16(DI) = mouse_push_di;
                   12049:                
                   12050:                // EOI
                   12051:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   12052:                        pic[0].isr &= ~(1 << 2); // master
                   12053:                }
                   12054:                pic_update();
                   12055:                break;
                   12056:        case 0x6c:
1.1.1.19  root     12057:                // dummy interrupt for case map routine pointed in the country info
                   12058:                if(REG8(AL) >= 0x80) {
                   12059:                        char tmp[2] = {0};
                   12060:                        tmp[0] = REG8(AL);
                   12061:                        my_strupr(tmp);
                   12062:                        REG8(AL) = tmp[0];
                   12063:                }
                   12064:                break;
1.1.1.27  root     12065:        case 0x6d:
                   12066:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   12067:                REG8(AL) = 0x86; // not supported
                   12068:                m_CF = 1;
                   12069:                break;
1.1.1.8   root     12070:        case 0x70:
                   12071:        case 0x71:
                   12072:        case 0x72:
                   12073:        case 0x73:
                   12074:        case 0x74:
                   12075:        case 0x75:
                   12076:        case 0x76:
                   12077:        case 0x77:
                   12078:                // EOI
                   12079:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   12080:                        pic[0].isr &= ~(1 << 2); // master
                   12081:                }
                   12082:                pic_update();
                   12083:                break;
1.1       root     12084:        default:
1.1.1.22  root     12085: //             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     12086:                break;
                   12087:        }
                   12088:        
                   12089:        // update cursor position
                   12090:        if(cursor_moved) {
                   12091:                CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.23  root     12092:                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.15  root     12093:                if(!restore_console_on_exit) {
                   12094:                        scr_top = csbi.srWindow.Top;
                   12095:                }
1.1       root     12096:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
1.1.1.14  root     12097:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
1.1       root     12098:                cursor_moved = false;
                   12099:        }
                   12100: }
                   12101: 
                   12102: // init
                   12103: 
                   12104: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   12105: {
                   12106:        // init file handler
                   12107:        memset(file_handler, 0, sizeof(file_handler));
                   12108:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   12109:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   12110:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     12111: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     12112:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     12113: #else
                   12114:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   12115: #endif
                   12116:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     12117:        }
1.1.1.21  root     12118: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     12119:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.21  root     12120: #else
                   12121:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1       root     12122: #endif
1.1.1.21  root     12123:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   12124:        }
1.1       root     12125:        _dup2(0, DUP_STDIN);
                   12126:        _dup2(1, DUP_STDOUT);
                   12127:        _dup2(2, DUP_STDERR);
1.1.1.21  root     12128:        _dup2(3, DUP_STDAUX);
                   12129:        _dup2(4, DUP_STDPRN);
1.1       root     12130:        
1.1.1.24  root     12131:        // init mouse
                   12132:        memset(&mouse, 0, sizeof(mouse));
                   12133:        mouse.max_position.x = 8 * scr_width  - 1;
                   12134:        mouse.max_position.y = 8 * scr_height - 1;
                   12135:        mouse.mickey.x = 8;
                   12136:        mouse.mickey.y = 16;
                   12137:        
1.1.1.26  root     12138: #ifdef SUPPORT_XMS
                   12139:        // init xms
                   12140:        msdos_xms_init();
                   12141: #endif
                   12142:        
1.1       root     12143:        // init process
                   12144:        memset(process, 0, sizeof(process));
                   12145:        
1.1.1.13  root     12146:        // init dtainfo
                   12147:        msdos_dta_info_init();
                   12148:        
1.1       root     12149:        // init memory
                   12150:        memset(mem, 0, sizeof(mem));
                   12151:        
                   12152:        // bios data area
1.1.1.23  root     12153:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     12154:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   12155:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     12156:        CONSOLE_FONT_INFO cfi;
                   12157:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   12158:        
                   12159:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   12160:        text_vram_top_address = TEXT_VRAM_TOP;
                   12161:        text_vram_end_address = text_vram_top_address + regen;
                   12162:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   12163:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   12164:        
                   12165:        if(regen > 0x4000) {
                   12166:                regen = 0x8000;
                   12167:                vram_pages = 1;
                   12168:        } else if(regen > 0x2000) {
                   12169:                regen = 0x4000;
                   12170:                vram_pages = 2;
                   12171:        } else if(regen > 0x1000) {
                   12172:                regen = 0x2000;
                   12173:                vram_pages = 4;
                   12174:        } else {
                   12175:                regen = 0x1000;
                   12176:                vram_pages = 8;
                   12177:        }
1.1       root     12178:        
1.1.1.25  root     12179:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   12180:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     12181:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   12182:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     12183:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.26  root     12184: //     *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
1.1.1.25  root     12185: //     *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
                   12186:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.26  root     12187:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1       root     12188:        *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
                   12189:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     12190:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   12191:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     12192:        *(UINT16 *)(mem + 0x44e) = 0;
                   12193:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     12194:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     12195:        *(UINT8  *)(mem + 0x460) = 7;
                   12196:        *(UINT8  *)(mem + 0x461) = 7;
                   12197:        *(UINT8  *)(mem + 0x462) = 0;
                   12198:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     12199:        *(UINT8  *)(mem + 0x465) = 0x09;
                   12200:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     12201:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   12202:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   12203:        *(UINT8  *)(mem + 0x487) = 0x60;
                   12204:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.25  root     12205:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.14  root     12206:        
                   12207:        // initial screen
                   12208:        SMALL_RECT rect;
                   12209:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   12210:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   12211:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   12212:                for(int x = 0; x < scr_width; x++) {
                   12213:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   12214:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   12215:                }
                   12216:        }
1.1       root     12217:        
1.1.1.19  root     12218:        // init mcb
1.1       root     12219:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     12220:        
                   12221:        // iret table
                   12222:        // note: int 2eh vector should address the routine in command.com,
                   12223:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   12224:        // so move iret table into allocated memory block
                   12225:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
                   12226:        msdos_mcb_create(seg++, 'M', -1, IRET_SIZE >> 4);
                   12227:        IRET_TOP = seg << 4;
                   12228:        seg += IRET_SIZE >> 4;
1.1.1.25  root     12229:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     12230:        
                   12231:        // dummy xms/ems device
                   12232:        msdos_mcb_create(seg++, 'M', -1, XMS_SIZE >> 4);
                   12233:        XMS_TOP = seg << 4;
                   12234:        seg += XMS_SIZE >> 4;
                   12235:        
                   12236:        // environment
1.1       root     12237:        msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
                   12238:        int env_seg = seg;
                   12239:        int ofs = 0;
1.1.1.28  root     12240:        char env_msdos_path[ENV_SIZE] = "", env_path[ENV_SIZE] = "", env_temp[ENV_SIZE] = "", *path;
1.1       root     12241:        
1.1.1.28  root     12242:        if((path = getenv("MSDOS_PATH")) != NULL) {
                   12243:                strcpy(env_msdos_path, msdos_get_multiple_short_path(path));
                   12244:                if(env_msdos_path[0] != '\0') {
                   12245:                        strcat(env_path, env_msdos_path);
1.1.1.14  root     12246:                }
                   12247:        }
1.1.1.28  root     12248:        if((path = getenv("PATH")) != NULL) {
                   12249:                if(env_path[0] != '\0') {
                   12250:                        strcat(env_path, ";");
1.1.1.9   root     12251:                }
1.1.1.28  root     12252:                strcat(env_path, msdos_get_multiple_short_path(path));
1.1.1.9   root     12253:        }
1.1.1.28  root     12254:        if((path = getenv("MSDOS_TEMP")) != NULL || (path = getenv("TEMP")) != NULL || (path = getenv("TMP")) != NULL) {
                   12255:                strcpy(env_temp, msdos_get_multiple_short_path(path));
1.1.1.15  root     12256:        }
1.1.1.28  root     12257:        if((path = getenv("MSDOS_COMSPEC")) != NULL || (path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   12258:                strcpy(comspec_path, msdos_get_multiple_short_path(path));
1.1.1.24  root     12259:        }
1.1.1.9   root     12260:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     12261:                // lower to upper
1.1.1.28  root     12262:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     12263:                strcpy(tmp, *p);
                   12264:                for(int i = 0;; i++) {
                   12265:                        if(tmp[i] == '=') {
                   12266:                                tmp[i] = '\0';
                   12267:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     12268:                                my_strupr(name);
1.1       root     12269:                                tmp[i] = '=';
                   12270:                                break;
                   12271:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     12272:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     12273:                        }
                   12274:                }
1.1.1.18  root     12275:                if(strncmp(tmp, "MSDOS_COMSPEC=", 14) == 0) {
                   12276:                        // ignore MSDOS_COMSPEC
1.1.1.24  root     12277:                } else if(strncmp(tmp, "MSDOS_TEMP=", 11) == 0) {
                   12278:                        // ignore MSDOS_TEMP
1.1.1.28  root     12279:                } else if(standard_env && strstr(";COMSPEC;INCLUDE;LIB;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     12280:                        // ignore non standard environments
                   12281:                } else {
1.1       root     12282:                        if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     12283:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.28  root     12284:                        } else if(strncmp(tmp, "MSDOS_PATH=",  11) == 0) {
                   12285:                                if(env_msdos_path[0] != '\0') {
                   12286:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   12287:                                } else {
                   12288:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   12289:                                }
                   12290:                        } else if(strncmp(tmp, "PATH=",  5) == 0) {
                   12291:                                if(env_path[0] != '\0') {
                   12292:                                        sprintf(tmp, "PATH=%s", env_path);
                   12293:                                } else {
                   12294:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   12295:                                }
                   12296:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   12297:                                if(env_temp[0] != '\0') {
                   12298:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   12299:                                } else {
                   12300:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   12301:                                }
                   12302:                        } else if(strncmp(tmp, "TMP=",  4) == 0) {
                   12303:                                if(env_temp[0] != '\0') {
                   12304:                                        sprintf(tmp, "TMP=%s", env_temp);
                   12305:                                } else {
                   12306:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     12307:                                }
                   12308:                        }
                   12309:                        int len = strlen(tmp);
1.1.1.14  root     12310:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     12311:                                fatalerror("too many environments\n");
                   12312:                        }
                   12313:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   12314:                        ofs += len + 1;
                   12315:                }
                   12316:        }
                   12317:        seg += (ENV_SIZE >> 4);
                   12318:        
                   12319:        // psp
                   12320:        msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
                   12321:        current_psp = seg;
1.1.1.14  root     12322:        msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
1.1       root     12323:        seg += (PSP_SIZE >> 4);
                   12324:        
1.1.1.19  root     12325:        // first free mcb in conventional memory
                   12326:        msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     12327:        first_mcb = seg;
                   12328:        
1.1.1.19  root     12329:        // dummy mcb to link to umb
                   12330:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   12331:        
                   12332:        // first free mcb in upper memory block
1.1.1.8   root     12333:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     12334:        
1.1.1.29  root     12335: #ifdef SUPPORT_HMA
                   12336:        // first free mcb in high memory area
                   12337:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12338: #endif
                   12339:        
1.1.1.26  root     12340:        // interrupt vector
                   12341:        for(int i = 0; i < 0x80; i++) {
                   12342:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   12343:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   12344:        }
                   12345:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0010;       // fffd:0010 irq0 (system timer)
                   12346:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   12347:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   12348:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   12349:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   12350:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   12351:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   12352:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   12353:        
1.1.1.29  root     12354:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     12355:        static const struct {
                   12356:                UINT16 attributes;
                   12357:                char *dev_name;
                   12358:        } dummy_devices[] = {
                   12359:                {0x8013, "CON     "},
                   12360:                {0x8000, "AUX     "},
                   12361:                {0xa0c0, "PRN     "},
                   12362:                {0x8008, "CLOCK$  "},
                   12363:                {0x8000, "COM1    "},
                   12364:                {0xa0c0, "LPT1    "},
                   12365:                {0xa0c0, "LPT2    "},
                   12366:                {0xa0c0, "LPT3    "},
                   12367:                {0x8000, "COM2    "},
                   12368:                {0x8000, "COM3    "},
                   12369:                {0x8000, "COM4    "},
1.1.1.30  root     12370: //             {0xc000, "CONFIG$ "},
                   12371:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     12372:        };
                   12373:        static const UINT8 dummy_device_routine[] = {
                   12374:                // from NUL device of Windows 98 SE
                   12375:                // or word ptr ES:[BX+03],0100
                   12376:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   12377:                // retf
                   12378:                0xcb,
                   12379:        };
1.1.1.29  root     12380:        device_t *last = NULL;
1.1.1.26  root     12381:        for(int i = 0; i < 12; i++) {
                   12382:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     12383:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   12384:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     12385:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     12386:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   12387:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     12388:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     12389:                last = device;
                   12390:        }
                   12391:        if(last != NULL) {
                   12392:                last->next_driver.w.l = 0;
                   12393:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     12394:        }
1.1.1.29  root     12395:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     12396:        
1.1.1.25  root     12397:        // dos info
                   12398:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12399:        dos_info->magic_word = 1;
                   12400:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   12401:        dos_info->first_dpb.w.l = 0;
                   12402:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   12403:        dos_info->first_sft.w.l = 0;
                   12404:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     12405:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     12406:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     12407:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     12408:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   12409:        dos_info->max_sector_len = 512;
                   12410:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   12411:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   12412:        dos_info->cds.w.l = 0;
                   12413:        dos_info->cds.w.h = CDS_TOP >> 4;
                   12414:        dos_info->fcb_table.w.l = 0;
                   12415:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   12416:        dos_info->last_drive = 'Z' - 'A' + 1;
                   12417:        dos_info->buffers_x = 20;
                   12418:        dos_info->buffers_y = 0;
                   12419:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     12420:        dos_info->nul_device.next_driver.w.l = 22;
                   12421:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     12422:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     12423:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   12424:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     12425:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   12426:        dos_info->disk_buf_heads.w.l = 0;
                   12427:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   12428:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   12429:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     12430:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     12431:        
                   12432:        char *env;
                   12433:        if((env = getenv("LASTDRIVE")) != NULL) {
                   12434:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   12435:                        dos_info->last_drive = env[0] - 'A' + 1;
                   12436:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   12437:                        dos_info->last_drive = env[0] - 'a' + 1;
                   12438:                }
                   12439:        }
                   12440:        if((env = getenv("windir")) != NULL) {
                   12441:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   12442:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   12443:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   12444:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   12445:                }
                   12446:        }
                   12447: #if defined(HAS_I386)
                   12448:        dos_info->i386_or_later = 1;
                   12449: #else
                   12450:        dos_info->i386_or_later = 0;
                   12451: #endif
                   12452:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   12453:        
1.1.1.27  root     12454:        // ems (int 67h) and xms
1.1.1.25  root     12455:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   12456:        xms_device->next_driver.w.l = 0xffff;
                   12457:        xms_device->next_driver.w.h = 0xffff;
                   12458:        xms_device->attributes = 0xc000;
1.1.1.29  root     12459:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   12460:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     12461:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   12462:        
1.1.1.26  root     12463:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   12464:        mem[XMS_TOP + 0x13] = 0x68;
                   12465:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     12466: #ifdef SUPPORT_XMS
                   12467:        if(support_xms) {
1.1.1.26  root     12468:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   12469:                mem[XMS_TOP + 0x16] = 0x69;
                   12470:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     12471:        } else
                   12472: #endif
1.1.1.26  root     12473:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     12474:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     12475:        
1.1.1.26  root     12476:        // irq12 routine (mouse)
1.1.1.24  root     12477:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   12478:        mem[0xfffd0 + 0x01] = 0x6a;
                   12479:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   12480:        mem[0xfffd0 + 0x03] = 0xff;
                   12481:        mem[0xfffd0 + 0x04] = 0xff;
                   12482:        mem[0xfffd0 + 0x05] = 0xff;
                   12483:        mem[0xfffd0 + 0x06] = 0xff;
                   12484:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   12485:        mem[0xfffd0 + 0x08] = 0x6b;
                   12486:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   12487:        
1.1.1.27  root     12488:        // case map routine
                   12489:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   12490:        mem[0xfffd0 + 0x0b] = 0x6c;
                   12491:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   12492:        
                   12493:        // font read routine
                   12494:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   12495:        mem[0xfffd0 + 0x0e] = 0x6d;
                   12496:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     12497:        
1.1.1.26  root     12498:        // irq0 routine (system time)
1.1.1.24  root     12499:        mem[0xfffd0 + 0x10] = 0xcd;     // int 1ch
                   12500:        mem[0xfffd0 + 0x11] = 0x1c;
                   12501:        mem[0xfffd0 + 0x12] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   12502:        mem[0xfffd0 + 0x13] = 0x08;
                   12503:        mem[0xfffd0 + 0x14] = 0x00;
                   12504:        mem[0xfffd0 + 0x15] = ((IRET_TOP >> 4)     ) & 0xff;
                   12505:        mem[0xfffd0 + 0x16] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     12506:        
1.1.1.26  root     12507:        // boot routine
1.1       root     12508:        mem[0xffff0] = 0xf4;    // halt
                   12509:        mem[0xffff1] = 0xcd;    // int 21h
                   12510:        mem[0xffff2] = 0x21;
                   12511:        mem[0xffff3] = 0xcb;    // retf
                   12512:        
1.1.1.24  root     12513:        mem[0xffff5] = '0';     // rom date
                   12514:        mem[0xffff6] = '2';
                   12515:        mem[0xffff7] = '/';
                   12516:        mem[0xffff8] = '2';
                   12517:        mem[0xffff9] = '2';
                   12518:        mem[0xffffa] = '/';
                   12519:        mem[0xffffb] = '0';
                   12520:        mem[0xffffc] = '6';
                   12521:        mem[0xffffe] = 0xfc;    // machine id
                   12522:        mem[0xfffff] = 0x00;
                   12523:        
1.1       root     12524:        // param block
                   12525:        // + 0: param block (22bytes)
                   12526:        // +24: fcb1/2 (20bytes)
                   12527:        // +44: command tail (128bytes)
                   12528:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12529:        param->env_seg = 0;
                   12530:        param->cmd_line.w.l = 44;
                   12531:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12532:        param->fcb1.w.l = 24;
                   12533:        param->fcb1.w.h = (WORK_TOP >> 4);
                   12534:        param->fcb2.w.l = 24;
                   12535:        param->fcb2.w.h = (WORK_TOP >> 4);
                   12536:        
                   12537:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12538:        
                   12539:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   12540:        if(argc > 1) {
                   12541:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   12542:                for(int i = 2; i < argc; i++) {
                   12543:                        char tmp[128];
                   12544:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   12545:                        strcpy(cmd_line->cmd, tmp);
                   12546:                }
                   12547:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   12548:        } else {
                   12549:                cmd_line->len = 0;
                   12550:        }
                   12551:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12552:        
                   12553:        // system file table
1.1.1.21  root     12554:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   12555:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     12556:        
1.1.1.19  root     12557:        // disk buffer header (from DOSBox)
                   12558:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   12559:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   12560:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   12561:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   12562:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   12563:        
1.1       root     12564:        // current directory structure
                   12565:        msdos_cds_update(_getdrive() - 1);
                   12566:        
                   12567:        // fcb table
                   12568:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     12569:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     12570:        
1.1.1.22  root     12571:        // error table
                   12572:        *(UINT8 *)(mem + ERR_TABLE_TOP + 0) = 0xff;
                   12573:        *(UINT8 *)(mem + ERR_TABLE_TOP + 1) = 0x04;
                   12574:        *(UINT8 *)(mem + ERR_TABLE_TOP + 2) = 0x00;
                   12575:        *(UINT8 *)(mem + ERR_TABLE_TOP + 3) = 0x00;
                   12576:        
1.1.1.17  root     12577:        // nls stuff
                   12578:        msdos_nls_tables_init();
1.1       root     12579:        
                   12580:        // execute command
1.1.1.28  root     12581:        try {
                   12582:                if(msdos_process_exec(argv[0], param, 0)) {
                   12583:                        fatalerror("'%s' not found\n", argv[0]);
                   12584:                }
                   12585:        } catch(...) {
                   12586:                // we should not reach here :-(
                   12587:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     12588:        }
                   12589:        retval = 0;
                   12590:        return(0);
                   12591: }
                   12592: 
                   12593: #define remove_std_file(path) { \
                   12594:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   12595:        if(fd != -1) { \
                   12596:                _lseek(fd, 0, SEEK_END); \
                   12597:                int size = _tell(fd); \
                   12598:                _close(fd); \
                   12599:                if(size == 0) { \
                   12600:                        remove(path); \
                   12601:                } \
                   12602:        } \
                   12603: }
                   12604: 
                   12605: void msdos_finish()
                   12606: {
                   12607:        for(int i = 0; i < MAX_FILES; i++) {
                   12608:                if(file_handler[i].valid) {
                   12609:                        _close(i);
                   12610:                }
                   12611:        }
1.1.1.21  root     12612: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     12613:        remove_std_file("stdaux.txt");
1.1.1.21  root     12614: #endif
                   12615: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     12616:        remove_std_file("stdprn.txt");
                   12617: #endif
1.1.1.30  root     12618: #ifdef SUPPORT_XMS
                   12619:        msdos_xms_finish();
                   12620: #endif
1.1       root     12621:        msdos_dbcs_table_finish();
                   12622: }
                   12623: 
                   12624: /* ----------------------------------------------------------------------------
                   12625:        PC/AT hardware emulation
                   12626: ---------------------------------------------------------------------------- */
                   12627: 
                   12628: void hardware_init()
                   12629: {
1.1.1.3   root     12630:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     12631:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     12632:        m_IF = 1;
1.1.1.3   root     12633: #if defined(HAS_I386)
1.1       root     12634:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   12635:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     12636: #endif
                   12637:        i386_set_a20_line(0);
1.1.1.14  root     12638:        
1.1.1.19  root     12639:        ems_init();
1.1.1.25  root     12640:        dma_init();
1.1       root     12641:        pic_init();
1.1.1.25  root     12642:        pio_init();
1.1.1.8   root     12643: #ifdef PIT_ALWAYS_RUNNING
                   12644:        pit_init();
                   12645: #else
1.1       root     12646:        pit_active = 0;
                   12647: #endif
1.1.1.25  root     12648:        sio_init();
1.1.1.8   root     12649:        cmos_init();
                   12650:        kbd_init();
1.1       root     12651: }
                   12652: 
1.1.1.10  root     12653: void hardware_finish()
                   12654: {
                   12655: #if defined(HAS_I386)
                   12656:        vtlb_free(m_vtlb);
                   12657: #endif
1.1.1.19  root     12658:        ems_finish();
1.1.1.25  root     12659:        sio_finish();
1.1.1.10  root     12660: }
                   12661: 
1.1.1.28  root     12662: void hardware_release()
                   12663: {
                   12664:        // release hardware resources when this program will be terminated abnormally
                   12665: #ifdef EXPORT_DEBUG_TO_FILE
                   12666:        if(fdebug != NULL) {
                   12667:                fclose(fdebug);
                   12668:                fdebug = NULL;
                   12669:        }
                   12670: #endif
                   12671: #if defined(HAS_I386)
                   12672:        vtlb_free(m_vtlb);
                   12673: #endif
                   12674:        ems_release();
                   12675:        sio_release();
                   12676: }
                   12677: 
1.1       root     12678: void hardware_run()
                   12679: {
                   12680:        int ops = 0;
                   12681:        
1.1.1.22  root     12682: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     12683:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.22  root     12684:        fdebug = fopen("debug.log", "w");
                   12685: #endif
1.1.1.3   root     12686:        while(!m_halted) {
1.1.1.22  root     12687: #ifdef ENABLE_DEBUG_DASM
                   12688:                if(dasm > 0) {
1.1       root     12689:                        char buffer[256];
1.1.1.3   root     12690: #if defined(HAS_I386)
1.1.1.22  root     12691:                        UINT32 flags = get_flags();
1.1.1.19  root     12692:                        UINT32 eip = m_eip;
1.1.1.3   root     12693: #else
1.1.1.22  root     12694:                        UINT32 flags = CompressFlags();
1.1.1.19  root     12695:                        UINT32 eip = m_pc - SREG_BASE(CS);
1.1.1.3   root     12696: #endif
                   12697:                        UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1       root     12698:                        
1.1.1.3   root     12699: #if defined(HAS_I386)
                   12700:                        if(m_operand_size) {
1.1       root     12701:                                CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3   root     12702:                        } else
                   12703: #endif
                   12704:                        CPU_DISASSEMBLE_CALL(x86_16);
1.1.1.22  root     12705:                        
                   12706:                        fprintf(fdebug, "AX=%04X  BX=%04X CX=%04X DX=%04X SP=%04X  BP=%04X  SI=%04X  DI=%04X\nDS=%04X  ES=%04X SS=%04X CS=%04X IP=%04X  FLAG=[%s %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   12707:                        REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), SREG(DS), SREG(ES), SREG(SS), SREG(CS), eip,
                   12708: #if defined(HAS_I386)
                   12709:                        PROTECTED_MODE ? "PE" : "--",
                   12710: #else
                   12711:                        "--",
                   12712: #endif
                   12713:                        (flags & 0x40000) ? 'A' : '-',
                   12714:                        (flags & 0x20000) ? 'V' : '-',
                   12715:                        (flags & 0x10000) ? 'R' : '-',
                   12716:                        (flags & 0x04000) ? 'N' : '-',
                   12717:                        (flags & 0x02000) ? '1' : '0',
                   12718:                        (flags & 0x01000) ? '1' : '0',
                   12719:                        (flags & 0x00800) ? 'O' : '-',
                   12720:                        (flags & 0x00400) ? 'D' : '-',
                   12721:                        (flags & 0x00200) ? 'I' : '-',
                   12722:                        (flags & 0x00100) ? 'T' : '-',
                   12723:                        (flags & 0x00080) ? 'S' : '-',
                   12724:                        (flags & 0x00040) ? 'Z' : '-',
                   12725:                        (flags & 0x00010) ? 'A' : '-',
                   12726:                        (flags & 0x00004) ? 'P' : '-',
                   12727:                        (flags & 0x00001) ? 'C' : '-');
                   12728:                        fprintf(fdebug, "%04X:%04X\t%s\n", SREG(CS), (unsigned)eip, buffer);
                   12729:                        dasm--;
1.1       root     12730:                }
                   12731: #endif
1.1.1.3   root     12732: #if defined(HAS_I386)
                   12733:                m_cycles = 1;
1.1       root     12734:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     12735: #else
                   12736:                CPU_EXECUTE_CALL(CPU_MODEL);
                   12737: #endif
1.1.1.14  root     12738: #if defined(HAS_I386)
                   12739:                if(m_eip != m_prev_eip) {
                   12740: #else
                   12741:                if(m_pc != m_prevpc) {
                   12742: #endif
                   12743:                        iops++;
                   12744:                }
1.1.1.8   root     12745:                if(++ops == 16384) {
1.1       root     12746:                        hardware_update();
                   12747:                        ops = 0;
                   12748:                }
                   12749:        }
1.1.1.22  root     12750: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     12751:        if(fdebug != NULL) {
                   12752:                fclose(fdebug);
                   12753:                fdebug = NULL;
                   12754:        }
1.1.1.22  root     12755: #endif
1.1       root     12756: }
                   12757: 
                   12758: void hardware_update()
                   12759: {
1.1.1.8   root     12760:        static UINT32 prev_time = 0;
                   12761:        UINT32 cur_time = timeGetTime();
                   12762:        
                   12763:        if(prev_time != cur_time) {
                   12764:                // update pit and raise irq0
                   12765: #ifndef PIT_ALWAYS_RUNNING
                   12766:                if(pit_active)
                   12767: #endif
                   12768:                {
                   12769:                        if(pit_run(0, cur_time)) {
                   12770:                                pic_req(0, 0, 1);
                   12771:                        }
                   12772:                        pit_run(1, cur_time);
                   12773:                        pit_run(2, cur_time);
                   12774:                }
1.1.1.24  root     12775:                
1.1.1.25  root     12776:                // update sio and raise irq4/3
1.1.1.29  root     12777:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     12778:                        sio_update(c);
                   12779:                }
                   12780:                
1.1.1.24  root     12781:                // update keyboard and mouse
1.1.1.14  root     12782:                static UINT32 prev_tick = 0;
                   12783:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     12784:                
1.1.1.14  root     12785:                if(prev_tick != cur_tick) {
                   12786:                        // update keyboard flags
                   12787:                        UINT8 state;
1.1.1.24  root     12788:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   12789:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   12790:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   12791:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   12792:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   12793:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   12794:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   12795:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     12796:                        mem[0x417] = state;
                   12797:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   12798:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   12799:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   12800:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     12801: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   12802: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     12803:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   12804:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   12805:                        mem[0x418] = state;
                   12806:                        
1.1.1.24  root     12807:                        // update console input if needed
                   12808:                        if(!key_changed || mouse.active) {
                   12809:                                update_console_input();
                   12810:                        }
                   12811:                        
                   12812:                        // raise irq1 if key is pressed/released
                   12813:                        if(key_changed) {
1.1.1.8   root     12814:                                pic_req(0, 1, 1);
1.1.1.24  root     12815:                                key_changed = false;
                   12816:                        }
                   12817:                        
                   12818:                        // raise irq12 if mouse status is changed
                   12819:                        if(mouse.status & mouse.call_mask) {
                   12820:                                if(mouse.active) {
                   12821:                                        pic_req(1, 4, 1);
                   12822:                                        mouse.status_irq = mouse.status & mouse.call_mask;
                   12823:                                }
                   12824:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     12825:                        }
1.1.1.24  root     12826:                        
1.1.1.14  root     12827:                        prev_tick = cur_tick;
1.1.1.8   root     12828:                }
1.1.1.24  root     12829:                
1.1.1.19  root     12830:                // update daily timer counter
                   12831:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     12832:                
1.1.1.8   root     12833:                prev_time = cur_time;
1.1       root     12834:        }
                   12835: }
                   12836: 
1.1.1.19  root     12837: // ems
                   12838: 
                   12839: void ems_init()
                   12840: {
                   12841:        memset(ems_handles, 0, sizeof(ems_handles));
                   12842:        memset(ems_pages, 0, sizeof(ems_pages));
                   12843:        free_ems_pages = MAX_EMS_PAGES;
                   12844: }
                   12845: 
                   12846: void ems_finish()
                   12847: {
1.1.1.28  root     12848:        ems_release();
                   12849: }
                   12850: 
                   12851: void ems_release()
                   12852: {
1.1.1.31! root     12853:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     12854:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     12855:                        free(ems_handles[i].buffer);
                   12856:                        ems_handles[i].buffer = NULL;
                   12857:                }
                   12858:        }
                   12859: }
                   12860: 
                   12861: void ems_allocate_pages(int handle, int pages)
                   12862: {
                   12863:        if(pages > 0) {
                   12864:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   12865:        } else {
                   12866:                ems_handles[handle].buffer = NULL;
                   12867:        }
                   12868:        ems_handles[handle].pages = pages;
                   12869:        ems_handles[handle].allocated = true;
                   12870:        free_ems_pages -= pages;
                   12871: }
                   12872: 
                   12873: void ems_reallocate_pages(int handle, int pages)
                   12874: {
                   12875:        if(ems_handles[handle].allocated) {
                   12876:                if(ems_handles[handle].pages != pages) {
                   12877:                        UINT8 *new_buffer = NULL;
                   12878:                        
                   12879:                        if(pages > 0) {
                   12880:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   12881:                        }
                   12882:                        if(ems_handles[handle].buffer) {
                   12883:                                if(new_buffer) {
                   12884:                                        if(pages > ems_handles[handle].pages) {
                   12885:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   12886:                                        } else {
                   12887:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   12888:                                        }
                   12889:                                }
                   12890:                                free(ems_handles[handle].buffer);
                   12891:                                ems_handles[handle].buffer = NULL;
                   12892:                        }
                   12893:                        free_ems_pages += ems_handles[handle].pages;
                   12894:                        
                   12895:                        ems_handles[handle].buffer = new_buffer;
                   12896:                        ems_handles[handle].pages = pages;
                   12897:                        free_ems_pages -= pages;
                   12898:                }
                   12899:        } else {
                   12900:                ems_allocate_pages(handle, pages);
                   12901:        }
                   12902: }
                   12903: 
                   12904: void ems_release_pages(int handle)
                   12905: {
                   12906:        if(ems_handles[handle].allocated) {
                   12907:                if(ems_handles[handle].buffer) {
                   12908:                        free(ems_handles[handle].buffer);
                   12909:                        ems_handles[handle].buffer = NULL;
                   12910:                }
                   12911:                free_ems_pages += ems_handles[handle].pages;
                   12912:                ems_handles[handle].allocated = false;
                   12913:        }
                   12914: }
                   12915: 
                   12916: void ems_map_page(int physical, int handle, int logical)
                   12917: {
                   12918:        if(ems_pages[physical].mapped) {
                   12919:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   12920:                        return;
                   12921:                }
                   12922:                ems_unmap_page(physical);
                   12923:        }
                   12924:        if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   12925:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   12926:        }
                   12927:        ems_pages[physical].handle = handle;
                   12928:        ems_pages[physical].page = logical;
                   12929:        ems_pages[physical].mapped = true;
                   12930: }
                   12931: 
                   12932: void ems_unmap_page(int physical)
                   12933: {
                   12934:        if(ems_pages[physical].mapped) {
                   12935:                int handle = ems_pages[physical].handle;
                   12936:                int logical = ems_pages[physical].page;
                   12937:                
                   12938:                if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   12939:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   12940:                }
                   12941:                ems_pages[physical].mapped = false;
                   12942:        }
                   12943: }
                   12944: 
1.1.1.25  root     12945: // dma
1.1       root     12946: 
1.1.1.25  root     12947: void dma_init()
1.1       root     12948: {
1.1.1.26  root     12949:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     12950:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     12951: //             for(int ch = 0; ch < 4; ch++) {
                   12952: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   12953: //             }
1.1.1.25  root     12954:                dma_reset(c);
                   12955:        }
1.1       root     12956: }
                   12957: 
1.1.1.25  root     12958: void dma_reset(int c)
1.1       root     12959: {
1.1.1.25  root     12960:        dma[c].low_high = false;
                   12961:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   12962:        dma[c].mask = 0xff;
                   12963: }
                   12964: 
                   12965: void dma_write(int c, UINT32 addr, UINT8 data)
                   12966: {
                   12967:        int ch = (addr >> 1) & 3;
                   12968:        UINT8 bit = 1 << (data & 3);
                   12969:        
                   12970:        switch(addr & 0x0f) {
                   12971:        case 0x00: case 0x02: case 0x04: case 0x06:
                   12972:                if(dma[c].low_high) {
                   12973:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     12974:                } else {
1.1.1.25  root     12975:                        dma[c].ch[ch].bareg.b.l = data;
                   12976:                }
                   12977:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   12978:                dma[c].low_high = !dma[c].low_high;
                   12979:                break;
                   12980:        case 0x01: case 0x03: case 0x05: case 0x07:
                   12981:                if(dma[c].low_high) {
                   12982:                        dma[c].ch[ch].bcreg.b.h = data;
                   12983:                } else {
                   12984:                        dma[c].ch[ch].bcreg.b.l = data;
                   12985:                }
                   12986:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   12987:                dma[c].low_high = !dma[c].low_high;
                   12988:                break;
                   12989:        case 0x08:
                   12990:                // command register
                   12991:                dma[c].cmd = data;
                   12992:                break;
                   12993:        case 0x09:
                   12994:                // dma[c].request register
                   12995:                if(data & 4) {
                   12996:                        if(!(dma[c].req & bit)) {
                   12997:                                dma[c].req |= bit;
                   12998: //                             dma_run(c, ch);
                   12999:                        }
                   13000:                } else {
                   13001:                        dma[c].req &= ~bit;
                   13002:                }
                   13003:                break;
                   13004:        case 0x0a:
                   13005:                // single mask register
                   13006:                if(data & 4) {
                   13007:                        dma[c].mask |= bit;
                   13008:                } else {
                   13009:                        dma[c].mask &= ~bit;
                   13010:                }
                   13011:                break;
                   13012:        case 0x0b:
                   13013:                // mode register
                   13014:                dma[c].ch[data & 3].mode = data;
                   13015:                break;
                   13016:        case 0x0c:
                   13017:                dma[c].low_high = false;
                   13018:                break;
                   13019:        case 0x0d:
                   13020:                // clear master
                   13021:                dma_reset(c);
                   13022:                break;
                   13023:        case 0x0e:
                   13024:                // clear mask register
                   13025:                dma[c].mask = 0;
                   13026:                break;
                   13027:        case 0x0f:
                   13028:                // all mask register
                   13029:                dma[c].mask = data & 0x0f;
                   13030:                break;
                   13031:        }
                   13032: }
                   13033: 
                   13034: UINT8 dma_read(int c, UINT32 addr)
                   13035: {
                   13036:        int ch = (addr >> 1) & 3;
                   13037:        UINT8 val = 0xff;
                   13038:        
                   13039:        switch(addr & 0x0f) {
                   13040:        case 0x00: case 0x02: case 0x04: case 0x06:
                   13041:                if(dma[c].low_high) {
                   13042:                        val = dma[c].ch[ch].areg.b.h;
                   13043:                } else {
                   13044:                        val = dma[c].ch[ch].areg.b.l;
                   13045:                }
                   13046:                dma[c].low_high = !dma[c].low_high;
                   13047:                return(val);
                   13048:        case 0x01: case 0x03: case 0x05: case 0x07:
                   13049:                if(dma[c].low_high) {
                   13050:                        val = dma[c].ch[ch].creg.b.h;
                   13051:                } else {
                   13052:                        val = dma[c].ch[ch].creg.b.l;
                   13053:                }
                   13054:                dma[c].low_high = !dma[c].low_high;
                   13055:                return(val);
                   13056:        case 0x08:
                   13057:                // status register
                   13058:                val = (dma[c].req << 4) | dma[c].tc;
                   13059:                dma[c].tc = 0;
                   13060:                return(val);
                   13061:        case 0x0d:
1.1.1.26  root     13062:                // temporary register (intel 82374 does not support)
1.1.1.25  root     13063:                return(dma[c].tmp & 0xff);
1.1.1.26  root     13064:        case 0x0f:
                   13065:                // mask register (intel 82374 does support)
                   13066:                return(dma[c].mask);
1.1.1.25  root     13067:        }
                   13068:        return(0xff);
                   13069: }
                   13070: 
                   13071: void dma_page_write(int c, int ch, UINT8 data)
                   13072: {
                   13073:        dma[c].ch[ch].pagereg = data;
                   13074: }
                   13075: 
                   13076: UINT8 dma_page_read(int c, int ch)
                   13077: {
                   13078:        return(dma[c].ch[ch].pagereg);
                   13079: }
                   13080: 
                   13081: void dma_run(int c, int ch)
                   13082: {
                   13083:        UINT8 bit = 1 << ch;
                   13084:        
                   13085:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   13086:                // execute dma
                   13087:                while(dma[c].req & bit) {
                   13088:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   13089:                                // memory -> memory
                   13090:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   13091:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   13092:                                
                   13093:                                if(c == 0) {
                   13094:                                        dma[c].tmp = read_byte(saddr);
                   13095:                                        write_byte(daddr, dma[c].tmp);
                   13096:                                } else {
                   13097:                                        dma[c].tmp = read_word(saddr << 1);
                   13098:                                        write_word(daddr << 1, dma[c].tmp);
                   13099:                                }
                   13100:                                if(!(dma[c].cmd & 0x02)) {
                   13101:                                        if(dma[c].ch[0].mode & 0x20) {
                   13102:                                                dma[c].ch[0].areg.w--;
                   13103:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   13104:                                                        dma[c].ch[0].pagereg--;
                   13105:                                                }
                   13106:                                        } else {
                   13107:                                                dma[c].ch[0].areg.w++;
                   13108:                                                if(dma[c].ch[0].areg.w == 0) {
                   13109:                                                        dma[c].ch[0].pagereg++;
                   13110:                                                }
                   13111:                                        }
                   13112:                                }
                   13113:                                if(dma[c].ch[1].mode & 0x20) {
                   13114:                                        dma[c].ch[1].areg.w--;
                   13115:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   13116:                                                dma[c].ch[1].pagereg--;
                   13117:                                        }
                   13118:                                } else {
                   13119:                                        dma[c].ch[1].areg.w++;
                   13120:                                        if(dma[c].ch[1].areg.w == 0) {
                   13121:                                                dma[c].ch[1].pagereg++;
                   13122:                                        }
                   13123:                                }
                   13124:                                
                   13125:                                // check dma condition
                   13126:                                if(dma[c].ch[0].creg.w-- == 0) {
                   13127:                                        if(dma[c].ch[0].mode & 0x10) {
                   13128:                                                // self initialize
                   13129:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   13130:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   13131:                                        } else {
                   13132: //                                             dma[c].mask |= bit;
                   13133:                                        }
                   13134:                                }
                   13135:                                if(dma[c].ch[1].creg.w-- == 0) {
                   13136:                                        // terminal count
                   13137:                                        if(dma[c].ch[1].mode & 0x10) {
                   13138:                                                // self initialize
                   13139:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   13140:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   13141:                                        } else {
                   13142:                                                dma[c].mask |= bit;
                   13143:                                        }
                   13144:                                        dma[c].req &= ~bit;
                   13145:                                        dma[c].tc |= bit;
                   13146:                                }
                   13147:                        } else {
                   13148:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   13149:                                
                   13150:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   13151:                                        // verify
                   13152:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   13153:                                        // io -> memory
                   13154:                                        if(c == 0) {
                   13155:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   13156:                                                write_byte(addr, dma[c].tmp);
                   13157:                                        } else {
                   13158:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   13159:                                                write_word(addr << 1, dma[c].tmp);
                   13160:                                        }
                   13161:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   13162:                                        // memory -> io
                   13163:                                        if(c == 0) {
                   13164:                                                dma[c].tmp = read_byte(addr);
                   13165:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   13166:                                        } else {
                   13167:                                                dma[c].tmp = read_word(addr << 1);
                   13168:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   13169:                                        }
                   13170:                                }
                   13171:                                if(dma[c].ch[ch].mode & 0x20) {
                   13172:                                        dma[c].ch[ch].areg.w--;
                   13173:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   13174:                                                dma[c].ch[ch].pagereg--;
                   13175:                                        }
                   13176:                                } else {
                   13177:                                        dma[c].ch[ch].areg.w++;
                   13178:                                        if(dma[c].ch[ch].areg.w == 0) {
                   13179:                                                dma[c].ch[ch].pagereg++;
                   13180:                                        }
                   13181:                                }
                   13182:                                
                   13183:                                // check dma condition
                   13184:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   13185:                                        // terminal count
                   13186:                                        if(dma[c].ch[ch].mode & 0x10) {
                   13187:                                                // self initialize
                   13188:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   13189:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   13190:                                        } else {
                   13191:                                                dma[c].mask |= bit;
                   13192:                                        }
                   13193:                                        dma[c].req &= ~bit;
                   13194:                                        dma[c].tc |= bit;
                   13195:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   13196:                                        // single mode
                   13197:                                        break;
                   13198:                                }
                   13199:                        }
                   13200:                }
                   13201:        }
                   13202: }
                   13203: 
                   13204: // pic
                   13205: 
                   13206: void pic_init()
                   13207: {
                   13208:        memset(pic, 0, sizeof(pic));
                   13209:        pic[0].imr = pic[1].imr = 0xff;
                   13210:        
                   13211:        // from bochs bios
                   13212:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   13213:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   13214:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   13215:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   13216:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   13217:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   13218:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   13219:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   13220:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   13221: }
                   13222: 
                   13223: void pic_write(int c, UINT32 addr, UINT8 data)
                   13224: {
                   13225:        if(addr & 1) {
                   13226:                if(pic[c].icw2_r) {
                   13227:                        // icw2
                   13228:                        pic[c].icw2 = data;
                   13229:                        pic[c].icw2_r = 0;
                   13230:                } else if(pic[c].icw3_r) {
                   13231:                        // icw3
                   13232:                        pic[c].icw3 = data;
                   13233:                        pic[c].icw3_r = 0;
                   13234:                } else if(pic[c].icw4_r) {
                   13235:                        // icw4
                   13236:                        pic[c].icw4 = data;
                   13237:                        pic[c].icw4_r = 0;
                   13238:                } else {
                   13239:                        // ocw1
1.1       root     13240:                        pic[c].imr = data;
                   13241:                }
                   13242:        } else {
                   13243:                if(data & 0x10) {
                   13244:                        // icw1
                   13245:                        pic[c].icw1 = data;
                   13246:                        pic[c].icw2_r = 1;
                   13247:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   13248:                        pic[c].icw4_r = data & 1;
                   13249:                        pic[c].irr = 0;
                   13250:                        pic[c].isr = 0;
                   13251:                        pic[c].imr = 0;
                   13252:                        pic[c].prio = 0;
                   13253:                        if(!(pic[c].icw1 & 1)) {
                   13254:                                pic[c].icw4 = 0;
                   13255:                        }
                   13256:                        pic[c].ocw3 = 0;
                   13257:                } else if(data & 8) {
                   13258:                        // ocw3
                   13259:                        if(!(data & 2)) {
                   13260:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   13261:                        }
                   13262:                        if(!(data & 0x40)) {
                   13263:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   13264:                        }
                   13265:                        pic[c].ocw3 = data;
                   13266:                } else {
                   13267:                        // ocw2
                   13268:                        int level = 0;
                   13269:                        if(data & 0x40) {
                   13270:                                level = data & 7;
                   13271:                        } else {
                   13272:                                if(!pic[c].isr) {
                   13273:                                        return;
                   13274:                                }
                   13275:                                level = pic[c].prio;
                   13276:                                while(!(pic[c].isr & (1 << level))) {
                   13277:                                        level = (level + 1) & 7;
                   13278:                                }
                   13279:                        }
                   13280:                        if(data & 0x80) {
                   13281:                                pic[c].prio = (level + 1) & 7;
                   13282:                        }
                   13283:                        if(data & 0x20) {
                   13284:                                pic[c].isr &= ~(1 << level);
                   13285:                        }
                   13286:                }
                   13287:        }
                   13288:        pic_update();
                   13289: }
                   13290: 
                   13291: UINT8 pic_read(int c, UINT32 addr)
                   13292: {
                   13293:        if(addr & 1) {
                   13294:                return(pic[c].imr);
                   13295:        } else {
                   13296:                // polling mode is not supported...
                   13297:                //if(pic[c].ocw3 & 4) {
                   13298:                //      return ???;
                   13299:                //}
                   13300:                if(pic[c].ocw3 & 1) {
                   13301:                        return(pic[c].isr);
                   13302:                } else {
                   13303:                        return(pic[c].irr);
                   13304:                }
                   13305:        }
                   13306: }
                   13307: 
                   13308: void pic_req(int c, int level, int signal)
                   13309: {
                   13310:        if(signal) {
                   13311:                pic[c].irr |= (1 << level);
                   13312:        } else {
                   13313:                pic[c].irr &= ~(1 << level);
                   13314:        }
                   13315:        pic_update();
                   13316: }
                   13317: 
                   13318: int pic_ack()
                   13319: {
                   13320:        // ack (INTA=L)
                   13321:        pic[pic_req_chip].isr |= pic_req_bit;
                   13322:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   13323:        if(pic_req_chip > 0) {
                   13324:                // update isr and irr of master
                   13325:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   13326:                pic[pic_req_chip - 1].isr |= slave;
                   13327:                pic[pic_req_chip - 1].irr &= ~slave;
                   13328:        }
                   13329:        //if(pic[pic_req_chip].icw4 & 1) {
                   13330:                // 8086 mode
                   13331:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   13332:        //} else {
                   13333:        //      // 8080 mode
                   13334:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   13335:        //      if(pic[pic_req_chip].icw1 & 4) {
                   13336:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   13337:        //      } else {
                   13338:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   13339:        //      }
                   13340:        //      vector = 0xcd | (addr << 8);
                   13341:        //}
                   13342:        if(pic[pic_req_chip].icw4 & 2) {
                   13343:                // auto eoi
                   13344:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   13345:        }
                   13346:        return(vector);
                   13347: }
                   13348: 
                   13349: void pic_update()
                   13350: {
                   13351:        for(int c = 0; c < 2; c++) {
                   13352:                UINT8 irr = pic[c].irr;
                   13353:                if(c + 1 < 2) {
                   13354:                        // this is master
                   13355:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   13356:                                // request from slave
                   13357:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   13358:                        }
                   13359:                }
                   13360:                irr &= (~pic[c].imr);
                   13361:                if(!irr) {
                   13362:                        break;
                   13363:                }
                   13364:                if(!(pic[c].ocw3 & 0x20)) {
                   13365:                        irr |= pic[c].isr;
                   13366:                }
                   13367:                int level = pic[c].prio;
                   13368:                UINT8 bit = 1 << level;
                   13369:                while(!(irr & bit)) {
                   13370:                        level = (level + 1) & 7;
                   13371:                        bit = 1 << level;
                   13372:                }
                   13373:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   13374:                        // check slave
                   13375:                        continue;
                   13376:                }
                   13377:                if(pic[c].isr & bit) {
                   13378:                        break;
                   13379:                }
                   13380:                // interrupt request
                   13381:                pic_req_chip = c;
                   13382:                pic_req_level = level;
                   13383:                pic_req_bit = bit;
1.1.1.3   root     13384:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     13385:                return;
                   13386:        }
1.1.1.3   root     13387:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     13388: }
1.1       root     13389: 
1.1.1.25  root     13390: // pio
                   13391: 
                   13392: void pio_init()
                   13393: {
1.1.1.26  root     13394:        memset(pio, 0, sizeof(pio));
1.1.1.25  root     13395:        for(int c = 0; c < 2; c++) {
                   13396:                pio[c].stat = 0xde;
                   13397:                pio[c].ctrl = 0x0c;
                   13398:        }
                   13399: }
                   13400: 
                   13401: void pio_write(int c, UINT32 addr, UINT8 data)
                   13402: {
                   13403:        switch(addr & 3) {
                   13404:        case 0:
                   13405:                pio[c].data = data;
                   13406:                break;
                   13407:        case 2:
                   13408:                pio[c].ctrl = data;
                   13409:                break;
                   13410:        }
                   13411: }
                   13412: 
                   13413: UINT8 pio_read(int c, UINT32 addr)
                   13414: {
                   13415:        switch(addr & 3) {
                   13416:        case 0:
                   13417:                return(pio[c].data);
                   13418:        case 1:
                   13419:                return(pio[c].stat);
                   13420:        case 2:
                   13421:                return(pio[c].ctrl);
                   13422:        }
                   13423:        return(0xff);
                   13424: }
                   13425: 
1.1       root     13426: // pit
                   13427: 
1.1.1.22  root     13428: #define PIT_FREQ 1193182ULL
1.1       root     13429: #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)
                   13430: 
                   13431: void pit_init()
                   13432: {
1.1.1.8   root     13433:        memset(pit, 0, sizeof(pit));
1.1       root     13434:        for(int ch = 0; ch < 3; ch++) {
                   13435:                pit[ch].count = 0x10000;
                   13436:                pit[ch].ctrl_reg = 0x34;
                   13437:                pit[ch].mode = 3;
                   13438:        }
                   13439:        
                   13440:        // from bochs bios
                   13441:        pit_write(3, 0x34);
                   13442:        pit_write(0, 0x00);
                   13443:        pit_write(0, 0x00);
                   13444: }
                   13445: 
                   13446: void pit_write(int ch, UINT8 val)
                   13447: {
1.1.1.8   root     13448: #ifndef PIT_ALWAYS_RUNNING
1.1       root     13449:        if(!pit_active) {
                   13450:                pit_active = 1;
                   13451:                pit_init();
                   13452:        }
1.1.1.8   root     13453: #endif
1.1       root     13454:        switch(ch) {
                   13455:        case 0:
                   13456:        case 1:
                   13457:        case 2:
                   13458:                // write count register
                   13459:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   13460:                        if(pit[ch].ctrl_reg & 0x10) {
                   13461:                                pit[ch].low_write = 1;
                   13462:                        }
                   13463:                        if(pit[ch].ctrl_reg & 0x20) {
                   13464:                                pit[ch].high_write = 1;
                   13465:                        }
                   13466:                }
                   13467:                if(pit[ch].low_write) {
                   13468:                        pit[ch].count_reg = val;
                   13469:                        pit[ch].low_write = 0;
                   13470:                } else if(pit[ch].high_write) {
                   13471:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   13472:                                pit[ch].count_reg = val << 8;
                   13473:                        } else {
                   13474:                                pit[ch].count_reg |= val << 8;
                   13475:                        }
                   13476:                        pit[ch].high_write = 0;
                   13477:                }
                   13478:                // start count
1.1.1.8   root     13479:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   13480:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   13481:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   13482:                                pit[ch].prev_time = timeGetTime();
                   13483:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     13484:                        }
                   13485:                }
                   13486:                break;
                   13487:        case 3: // ctrl reg
                   13488:                if((val & 0xc0) == 0xc0) {
                   13489:                        // i8254 read-back command
                   13490:                        for(ch = 0; ch < 3; ch++) {
                   13491:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   13492:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   13493:                                        pit[ch].status_latched = 1;
                   13494:                                }
                   13495:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   13496:                                        pit_latch_count(ch);
                   13497:                                }
                   13498:                        }
                   13499:                        break;
                   13500:                }
                   13501:                ch = (val >> 6) & 3;
                   13502:                if(val & 0x30) {
                   13503:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   13504:                        pit[ch].mode = modes[(val >> 1) & 7];
                   13505:                        pit[ch].count_latched = 0;
                   13506:                        pit[ch].low_read = pit[ch].high_read = 0;
                   13507:                        pit[ch].low_write = pit[ch].high_write = 0;
                   13508:                        pit[ch].ctrl_reg = val;
                   13509:                        // stop count
1.1.1.8   root     13510:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     13511:                        pit[ch].count_reg = 0;
                   13512:                } else if(!pit[ch].count_latched) {
                   13513:                        pit_latch_count(ch);
                   13514:                }
                   13515:                break;
                   13516:        }
                   13517: }
                   13518: 
                   13519: UINT8 pit_read(int ch)
                   13520: {
1.1.1.8   root     13521: #ifndef PIT_ALWAYS_RUNNING
1.1       root     13522:        if(!pit_active) {
                   13523:                pit_active = 1;
                   13524:                pit_init();
                   13525:        }
1.1.1.8   root     13526: #endif
1.1       root     13527:        switch(ch) {
                   13528:        case 0:
                   13529:        case 1:
                   13530:        case 2:
                   13531:                if(pit[ch].status_latched) {
                   13532:                        pit[ch].status_latched = 0;
                   13533:                        return(pit[ch].status);
                   13534:                }
                   13535:                // if not latched, through current count
                   13536:                if(!pit[ch].count_latched) {
                   13537:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   13538:                                pit_latch_count(ch);
                   13539:                        }
                   13540:                }
                   13541:                // return latched count
                   13542:                if(pit[ch].low_read) {
                   13543:                        pit[ch].low_read = 0;
                   13544:                        if(!pit[ch].high_read) {
                   13545:                                pit[ch].count_latched = 0;
                   13546:                        }
                   13547:                        return(pit[ch].latch & 0xff);
                   13548:                } else if(pit[ch].high_read) {
                   13549:                        pit[ch].high_read = 0;
                   13550:                        pit[ch].count_latched = 0;
                   13551:                        return((pit[ch].latch >> 8) & 0xff);
                   13552:                }
                   13553:        }
                   13554:        return(0xff);
                   13555: }
                   13556: 
1.1.1.8   root     13557: int pit_run(int ch, UINT32 cur_time)
1.1       root     13558: {
1.1.1.8   root     13559:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     13560:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     13561:                pit[ch].prev_time = pit[ch].expired_time;
                   13562:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   13563:                if(cur_time >= pit[ch].expired_time) {
                   13564:                        pit[ch].prev_time = cur_time;
                   13565:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     13566:                }
1.1.1.8   root     13567:                return(1);
1.1       root     13568:        }
1.1.1.8   root     13569:        return(0);
1.1       root     13570: }
                   13571: 
                   13572: void pit_latch_count(int ch)
                   13573: {
1.1.1.8   root     13574:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     13575:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     13576:                pit_run(ch, cur_time);
                   13577:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     13578:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   13579:                
                   13580:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   13581:                        // decrement counter in 1msec period
                   13582:                        if(pit[ch].next_latch == 0) {
                   13583:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   13584:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   13585:                        }
                   13586:                        if(pit[ch].latch > pit[ch].next_latch) {
                   13587:                                pit[ch].latch--;
                   13588:                        }
                   13589:                } else {
                   13590:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   13591:                        pit[ch].next_latch = 0;
                   13592:                }
1.1.1.8   root     13593:        } else {
                   13594:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     13595:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     13596:        }
                   13597:        pit[ch].count_latched = 1;
                   13598:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   13599:                // lower byte
                   13600:                pit[ch].low_read = 1;
                   13601:                pit[ch].high_read = 0;
                   13602:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   13603:                // upper byte
                   13604:                pit[ch].low_read = 0;
                   13605:                pit[ch].high_read = 1;
                   13606:        } else {
                   13607:                // lower -> upper
1.1.1.14  root     13608:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     13609:        }
                   13610: }
                   13611: 
1.1.1.8   root     13612: int pit_get_expired_time(int ch)
1.1       root     13613: {
1.1.1.22  root     13614:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   13615:        UINT64 val = pit[ch].accum >> 10;
                   13616:        pit[ch].accum -= val << 10;
                   13617:        return((val != 0) ? val : 1);
1.1.1.8   root     13618: }
                   13619: 
1.1.1.25  root     13620: // sio
                   13621: 
                   13622: void sio_init()
                   13623: {
1.1.1.26  root     13624:        memset(sio, 0, sizeof(sio));
                   13625:        memset(sio_mt, 0, sizeof(sio_mt));
                   13626:        
1.1.1.29  root     13627:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     13628:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   13629:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   13630:                
                   13631:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   13632:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     13633:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   13634:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     13635:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   13636:                sio[c].irq_identify = 0x01;     // no pending irq
                   13637:                
                   13638:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   13639:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   13640:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   13641:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   13642:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   13643:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   13644:                
1.1.1.26  root     13645:                if(sio_port_number[c] != 0) {
1.1.1.25  root     13646:                        sio[c].channel = c;
                   13647:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   13648:                }
                   13649:        }
                   13650: }
                   13651: 
                   13652: void sio_finish()
                   13653: {
1.1.1.29  root     13654:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     13655:                if(sio_mt[c].hThread != NULL) {
                   13656:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   13657:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     13658:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     13659:                }
                   13660:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   13661:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   13662:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   13663:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   13664:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   13665:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     13666:        }
                   13667:        sio_release();
                   13668: }
                   13669: 
                   13670: void sio_release()
                   13671: {
1.1.1.29  root     13672:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     13673:                // sio_thread() may access the resources :-(
                   13674:                if(sio_mt[c].hThread == NULL) {
                   13675:                        if(sio[c].send_buffer != NULL) {
                   13676:                                sio[c].send_buffer->release();
                   13677:                                delete sio[c].send_buffer;
                   13678:                                sio[c].send_buffer = NULL;
                   13679:                        }
                   13680:                        if(sio[c].recv_buffer != NULL) {
                   13681:                                sio[c].recv_buffer->release();
                   13682:                                delete sio[c].recv_buffer;
                   13683:                                sio[c].recv_buffer = NULL;
                   13684:                        }
                   13685:                }
1.1.1.25  root     13686:        }
                   13687: }
                   13688: 
                   13689: void sio_write(int c, UINT32 addr, UINT8 data)
                   13690: {
                   13691:        switch(addr & 7) {
                   13692:        case 0:
                   13693:                if(sio[c].selector & 0x80) {
                   13694:                        if(sio[c].divisor.b.l != data) {
                   13695:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13696:                                sio[c].divisor.b.l = data;
                   13697:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13698:                        }
                   13699:                } else {
                   13700:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   13701:                        sio[c].send_buffer->write(data);
                   13702:                        // transmitter holding/shift registers are not empty
                   13703:                        sio[c].line_stat_buf &= ~0x60;
                   13704:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   13705:                        
                   13706:                        if(sio[c].irq_enable & 0x02) {
                   13707:                                sio_update_irq(c);
                   13708:                        }
                   13709:                }
                   13710:                break;
                   13711:        case 1:
                   13712:                if(sio[c].selector & 0x80) {
                   13713:                        if(sio[c].divisor.b.h != data) {
                   13714:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13715:                                sio[c].divisor.b.h = data;
                   13716:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13717:                        }
                   13718:                } else {
                   13719:                        if(sio[c].irq_enable != data) {
                   13720:                                sio[c].irq_enable = data;
                   13721:                                sio_update_irq(c);
                   13722:                        }
                   13723:                }
                   13724:                break;
                   13725:        case 3:
                   13726:                {
                   13727:                        UINT8 line_ctrl = data & 0x3f;
                   13728:                        bool set_brk = ((data & 0x40) != 0);
                   13729:                        
                   13730:                        if(sio[c].line_ctrl != line_ctrl) {
                   13731:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13732:                                sio[c].line_ctrl = line_ctrl;
                   13733:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13734:                        }
                   13735:                        if(sio[c].set_brk != set_brk) {
                   13736:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   13737:                                sio[c].set_brk = set_brk;
                   13738:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   13739:                        }
                   13740:                }
                   13741:                sio[c].selector = data;
                   13742:                break;
                   13743:        case 4:
                   13744:                {
                   13745:                        bool set_dtr = ((data & 0x01) != 0);
                   13746:                        bool set_rts = ((data & 0x02) != 0);
                   13747:                        
                   13748:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     13749: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     13750:                                sio[c].set_dtr = set_dtr;
                   13751:                                sio[c].set_rts = set_rts;
1.1.1.26  root     13752: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   13753:                                
                   13754:                                bool state_changed = false;
                   13755:                                
                   13756:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13757:                                if(set_dtr) {
                   13758:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   13759:                                } else {
                   13760:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   13761:                                }
                   13762:                                if(set_rts) {
                   13763:                                        sio[c].modem_stat |= 0x10;      // cts on
                   13764:                                } else {
                   13765:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   13766:                                }
                   13767:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   13768:                                        if(!(sio[c].modem_stat & 0x02)) {
                   13769:                                                if(sio[c].irq_enable & 0x08) {
                   13770:                                                        state_changed = true;
                   13771:                                                }
                   13772:                                                sio[c].modem_stat |= 0x02;
                   13773:                                        }
                   13774:                                }
                   13775:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   13776:                                        if(!(sio[c].modem_stat & 0x01)) {
                   13777:                                                if(sio[c].irq_enable & 0x08) {
                   13778:                                                        state_changed = true;
                   13779:                                                }
                   13780:                                                sio[c].modem_stat |= 0x01;
                   13781:                                        }
                   13782:                                }
                   13783:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   13784:                                
                   13785:                                if(state_changed) {
                   13786:                                        sio_update_irq(c);
                   13787:                                }
1.1.1.25  root     13788:                        }
                   13789:                }
                   13790:                sio[c].modem_ctrl = data;
                   13791:                break;
                   13792:        case 7:
                   13793:                sio[c].scratch = data;
                   13794:                break;
                   13795:        }
                   13796: }
                   13797: 
                   13798: UINT8 sio_read(int c, UINT32 addr)
                   13799: {
                   13800:        switch(addr & 7) {
                   13801:        case 0:
                   13802:                if(sio[c].selector & 0x80) {
                   13803:                        return(sio[c].divisor.b.l);
                   13804:                } else {
                   13805:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   13806:                        UINT8 data = sio[c].recv_buffer->read();
                   13807:                        // data is not ready
                   13808:                        sio[c].line_stat_buf &= ~0x01;
                   13809:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13810:                        
                   13811:                        if(sio[c].irq_enable & 0x01) {
                   13812:                                sio_update_irq(c);
                   13813:                        }
                   13814:                        return(data);
                   13815:                }
                   13816:        case 1:
                   13817:                if(sio[c].selector & 0x80) {
                   13818:                        return(sio[c].divisor.b.h);
                   13819:                } else {
                   13820:                        return(sio[c].irq_enable);
                   13821:                }
                   13822:        case 2:
                   13823:                return(sio[c].irq_identify);
                   13824:        case 3:
                   13825:                return(sio[c].selector);
                   13826:        case 4:
                   13827:                return(sio[c].modem_ctrl);
                   13828:        case 5:
                   13829:                {
                   13830:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   13831:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   13832:                        sio[c].line_stat_err = 0x00;
                   13833:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   13834:                        
                   13835:                        bool state_changed = false;
                   13836:                        
                   13837:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   13838:                                EnterCriticalSection(&sio_mt[c].csSendData);
                   13839:                                if(!sio[c].send_buffer->full()) {
                   13840:                                        // transmitter holding register will be empty first
                   13841:                                        if(sio[c].irq_enable & 0x02) {
                   13842:                                                state_changed = true;
                   13843:                                        }
                   13844:                                        sio[c].line_stat_buf |= 0x20;
                   13845:                                }
                   13846:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   13847:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   13848:                                // transmitter shift register will be empty later
                   13849:                                sio[c].line_stat_buf |= 0x40;
                   13850:                        }
                   13851:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   13852:                                EnterCriticalSection(&sio_mt[c].csRecvData);
                   13853:                                if(!sio[c].recv_buffer->empty()) {
                   13854:                                        // data is ready
                   13855:                                        if(sio[c].irq_enable & 0x01) {
                   13856:                                                state_changed = true;
                   13857:                                        }
                   13858:                                        sio[c].line_stat_buf |= 0x01;
                   13859:                                }
                   13860:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13861:                        }
                   13862:                        if(state_changed) {
                   13863:                                sio_update_irq(c);
                   13864:                        }
                   13865:                        return(val);
                   13866:                }
                   13867:        case 6:
                   13868:                {
                   13869:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   13870:                        UINT8 val = sio[c].modem_stat;
                   13871:                        sio[c].modem_stat &= 0xf0;
                   13872:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   13873:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   13874:                        
                   13875:                        if(sio[c].modem_ctrl & 0x10) {
                   13876:                                // loop-back
                   13877:                                val &= 0x0f;
                   13878:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   13879:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   13880:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   13881:                        }
                   13882:                        return(val);
                   13883:                }
                   13884:        case 7:
                   13885:                return(sio[c].scratch);
                   13886:        }
                   13887:        return(0xff);
                   13888: }
                   13889: 
                   13890: void sio_update(int c)
                   13891: {
                   13892:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   13893:                EnterCriticalSection(&sio_mt[c].csSendData);
                   13894:                if(!sio[c].send_buffer->full()) {
                   13895:                        // transmitter holding/shift registers will be empty
                   13896:                        sio[c].line_stat_buf |= 0x60;
                   13897:                }
                   13898:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   13899:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   13900:                // transmitter shift register will be empty
                   13901:                sio[c].line_stat_buf |= 0x40;
                   13902:        }
                   13903:        if(!(sio[c].line_stat_buf & 0x01)) {
                   13904:                EnterCriticalSection(&sio_mt[c].csRecvData);
                   13905:                if(!sio[c].recv_buffer->empty()) {
                   13906:                        // data is ready
                   13907:                        sio[c].line_stat_buf |= 0x01;
                   13908:                }
                   13909:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13910:        }
                   13911:        sio_update_irq(c);
                   13912: }
                   13913: 
                   13914: void sio_update_irq(int c)
                   13915: {
                   13916:        int level = -1;
                   13917:        
                   13918:        if(sio[c].irq_enable & 0x08) {
                   13919:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13920:                if((sio[c].modem_stat & 0x0f) != 0) {
                   13921:                        level = 0;
                   13922:                }
                   13923:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13924:        }
                   13925:        if(sio[c].irq_enable & 0x02) {
                   13926:                if(sio[c].line_stat_buf & 0x20) {
                   13927:                        level = 1;
                   13928:                }
                   13929:        }
                   13930:        if(sio[c].irq_enable & 0x01) {
                   13931:                if(sio[c].line_stat_buf & 0x01) {
                   13932:                        level = 2;
                   13933:                }
                   13934:        }
                   13935:        if(sio[c].irq_enable & 0x04) {
                   13936:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   13937:                if(sio[c].line_stat_err != 0) {
                   13938:                        level = 3;
                   13939:                }
                   13940:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   13941:        }
1.1.1.29  root     13942:        
                   13943:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     13944:        if(level != -1) {
                   13945:                sio[c].irq_identify = level << 1;
1.1.1.29  root     13946:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     13947:        } else {
                   13948:                sio[c].irq_identify = 1;
1.1.1.29  root     13949:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     13950:        }
                   13951: }
                   13952: 
                   13953: DWORD WINAPI sio_thread(void *lpx)
                   13954: {
                   13955:        volatile sio_t *p = (sio_t *)lpx;
                   13956:        sio_mt_t *q = &sio_mt[p->channel];
                   13957:        
                   13958:        char name[] = "COM1";
1.1.1.26  root     13959:        name[3] = '0' + sio_port_number[p->channel];
                   13960:        HANDLE hComm = NULL;
                   13961:        COMMPROP commProp;
                   13962:        DCB dcb;
                   13963:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   13964:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   13965:        
                   13966:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   13967:                if(GetCommProperties(hComm, &commProp)) {
                   13968:                        dwSettableBaud = commProp.dwSettableBaud;
                   13969:                }
1.1.1.25  root     13970:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     13971: //             EscapeCommFunction(hComm, SETRTS);
                   13972: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     13973:                
                   13974:                while(!m_halted) {
                   13975:                        // setup comm port
                   13976:                        bool comm_state_changed = false;
                   13977:                        
                   13978:                        EnterCriticalSection(&q->csLineCtrl);
                   13979:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   13980:                                p->prev_divisor = p->divisor.w;
                   13981:                                p->prev_line_ctrl = p->line_ctrl;
                   13982:                                comm_state_changed = true;
                   13983:                        }
                   13984:                        LeaveCriticalSection(&q->csLineCtrl);
                   13985:                        
                   13986:                        if(comm_state_changed) {
1.1.1.26  root     13987:                                if(GetCommState(hComm, &dcb)) {
                   13988: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   13989:                                        DWORD baud = 115200 / p->prev_divisor;
                   13990:                                        dcb.BaudRate = 9600; // default
                   13991:                                        
                   13992:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   13993:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   13994:                                        // 134.5bps is not supported ???
                   13995: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   13996:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   13997:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   13998:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   13999:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   14000:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   14001:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   14002:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   14003:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   14004:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   14005: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   14006: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   14007: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   14008:                                        
                   14009:                                        switch(p->prev_line_ctrl & 0x03) {
                   14010:                                        case 0x00: dcb.ByteSize = 5; break;
                   14011:                                        case 0x01: dcb.ByteSize = 6; break;
                   14012:                                        case 0x02: dcb.ByteSize = 7; break;
                   14013:                                        case 0x03: dcb.ByteSize = 8; break;
                   14014:                                        }
                   14015:                                        switch(p->prev_line_ctrl & 0x04) {
                   14016:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   14017:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   14018:                                        }
                   14019:                                        switch(p->prev_line_ctrl & 0x38) {
                   14020:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   14021:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   14022:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   14023:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   14024:                                        default:   dcb.Parity = NOPARITY;    break;
                   14025:                                        }
                   14026:                                        dcb.fBinary = TRUE;
                   14027:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   14028:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   14029:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   14030:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   14031:                                        dcb.fTXContinueOnXoff = TRUE;
                   14032:                                        dcb.fOutX = dcb.fInX = FALSE;
                   14033:                                        dcb.fErrorChar = FALSE;
                   14034:                                        dcb.fNull = FALSE;
                   14035:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   14036:                                        dcb.fAbortOnError = FALSE;
                   14037:                                        
                   14038:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     14039:                                }
                   14040:                                
                   14041:                                // check again to apply all comm state changes
                   14042:                                Sleep(10);
                   14043:                                continue;
                   14044:                        }
                   14045:                        
                   14046:                        // set comm pins
                   14047:                        bool change_brk = false;
1.1.1.26  root     14048: //                     bool change_rts = false;
                   14049: //                     bool change_dtr = false;
1.1.1.25  root     14050:                        
                   14051:                        EnterCriticalSection(&q->csModemCtrl);
                   14052:                        if(p->prev_set_brk != p->set_brk) {
                   14053:                                p->prev_set_brk = p->set_brk;
                   14054:                                change_brk = true;
                   14055:                        }
1.1.1.26  root     14056: //                     if(p->prev_set_rts != p->set_rts) {
                   14057: //                             p->prev_set_rts = p->set_rts;
                   14058: //                             change_rts = true;
                   14059: //                     }
                   14060: //                     if(p->prev_set_dtr != p->set_dtr) {
                   14061: //                             p->prev_set_dtr = p->set_dtr;
                   14062: //                             change_dtr = true;
                   14063: //                     }
1.1.1.25  root     14064:                        LeaveCriticalSection(&q->csModemCtrl);
                   14065:                        
                   14066:                        if(change_brk) {
1.1.1.26  root     14067:                                static UINT32 clear_time = 0;
                   14068:                                if(p->prev_set_brk) {
                   14069:                                        EscapeCommFunction(hComm, SETBREAK);
                   14070:                                        clear_time = timeGetTime() + 200;
                   14071:                                } else {
                   14072:                                        // keep break for at least 200msec
                   14073:                                        UINT32 cur_time = timeGetTime();
                   14074:                                        if(clear_time > cur_time) {
                   14075:                                                Sleep(clear_time - cur_time);
                   14076:                                        }
                   14077:                                        EscapeCommFunction(hComm, CLRBREAK);
                   14078:                                }
1.1.1.25  root     14079:                        }
1.1.1.26  root     14080: //                     if(change_rts) {
                   14081: //                             if(p->prev_set_rts) {
                   14082: //                                     EscapeCommFunction(hComm, SETRTS);
                   14083: //                             } else {
                   14084: //                                     EscapeCommFunction(hComm, CLRRTS);
                   14085: //                             }
                   14086: //                     }
                   14087: //                     if(change_dtr) {
                   14088: //                             if(p->prev_set_dtr) {
                   14089: //                                     EscapeCommFunction(hComm, SETDTR);
                   14090: //                             } else {
                   14091: //                                     EscapeCommFunction(hComm, CLRDTR);
                   14092: //                             }
                   14093: //                     }
1.1.1.25  root     14094:                        
                   14095:                        // get comm pins
                   14096:                        DWORD dwModemStat = 0;
                   14097:                        
                   14098:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   14099:                                EnterCriticalSection(&q->csModemStat);
                   14100:                                if(dwModemStat & MS_RLSD_ON) {
                   14101:                                        p->modem_stat |= 0x80;
                   14102:                                } else {
                   14103:                                        p->modem_stat &= ~0x80;
                   14104:                                }
                   14105:                                if(dwModemStat & MS_RING_ON) {
                   14106:                                        p->modem_stat |= 0x40;
                   14107:                                } else {
                   14108:                                        p->modem_stat &= ~0x40;
                   14109:                                }
1.1.1.26  root     14110: //                             if(dwModemStat & MS_DSR_ON) {
                   14111: //                                     p->modem_stat |= 0x20;
                   14112: //                             } else {
                   14113: //                                     p->modem_stat &= ~0x20;
                   14114: //                             }
                   14115: //                             if(dwModemStat & MS_CTS_ON) {
                   14116: //                                     p->modem_stat |= 0x10;
                   14117: //                             } else {
                   14118: //                                     p->modem_stat &= ~0x10;
                   14119: //                             }
1.1.1.25  root     14120:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   14121:                                        p->modem_stat |= 0x08;
                   14122:                                }
                   14123:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   14124:                                        p->modem_stat |= 0x04;
                   14125:                                }
1.1.1.26  root     14126: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   14127: //                                     p->modem_stat |= 0x02;
                   14128: //                             }
                   14129: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   14130: //                                     p->modem_stat |= 0x01;
                   14131: //                             }
1.1.1.25  root     14132:                                LeaveCriticalSection(&q->csModemStat);
                   14133:                        }
                   14134:                        
                   14135:                        // send data
                   14136:                        DWORD dwSend = 0;
                   14137:                        
                   14138:                        EnterCriticalSection(&q->csSendData);
                   14139:                        while(!p->send_buffer->empty()) {
                   14140:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   14141:                        }
                   14142:                        LeaveCriticalSection(&q->csSendData);
                   14143:                        
                   14144:                        if(dwSend != 0) {
                   14145:                                DWORD dwWritten = 0;
                   14146:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   14147:                        }
                   14148:                        
                   14149:                        // get line status and recv data
                   14150:                        DWORD dwLineStat = 0;
                   14151:                        COMSTAT comStat;
                   14152:                        
                   14153:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   14154:                                EnterCriticalSection(&q->csLineStat);
                   14155:                                if(dwLineStat & CE_BREAK) {
                   14156:                                        p->line_stat_err |= 0x10;
                   14157:                                }
                   14158:                                if(dwLineStat & CE_FRAME) {
                   14159:                                        p->line_stat_err |= 0x08;
                   14160:                                }
                   14161:                                if(dwLineStat & CE_RXPARITY) {
                   14162:                                        p->line_stat_err |= 0x04;
                   14163:                                }
                   14164:                                if(dwLineStat & CE_OVERRUN) {
                   14165:                                        p->line_stat_err |= 0x02;
                   14166:                                }
                   14167:                                LeaveCriticalSection(&q->csLineStat);
                   14168:                                
                   14169:                                if(comStat.cbInQue != 0) {
                   14170:                                        EnterCriticalSection(&q->csRecvData);
                   14171:                                        DWORD dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   14172:                                        LeaveCriticalSection(&q->csRecvData);
                   14173:                                        
                   14174:                                        if(dwRecv != 0) {
                   14175:                                                DWORD dwRead = 0;
                   14176:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   14177:                                                        EnterCriticalSection(&q->csRecvData);
                   14178:                                                        for(int i = 0; i < dwRead; i++) {
                   14179:                                                                p->recv_buffer->write(bytBuffer[i]);
                   14180:                                                        }
                   14181:                                                        LeaveCriticalSection(&q->csRecvData);
                   14182:                                                }
                   14183:                                        }
                   14184:                                }
                   14185:                        }
                   14186:                        Sleep(10);
                   14187:                }
                   14188:                CloseHandle(hComm);
                   14189:        }
                   14190:        return 0;
                   14191: }
                   14192: 
1.1.1.8   root     14193: // cmos
                   14194: 
                   14195: void cmos_init()
                   14196: {
                   14197:        memset(cmos, 0, sizeof(cmos));
                   14198:        cmos_addr = 0;
1.1       root     14199:        
1.1.1.8   root     14200:        // from DOSBox
                   14201:        cmos_write(0x0a, 0x26);
                   14202:        cmos_write(0x0b, 0x02);
                   14203:        cmos_write(0x0d, 0x80);
1.1       root     14204: }
                   14205: 
1.1.1.8   root     14206: void cmos_write(int addr, UINT8 val)
1.1       root     14207: {
1.1.1.8   root     14208:        cmos[addr & 0x7f] = val;
                   14209: }
                   14210: 
                   14211: #define CMOS_GET_TIME() { \
                   14212:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   14213:        if(prev_sec != cur_sec) { \
                   14214:                GetLocalTime(&time); \
                   14215:                prev_sec = cur_sec; \
                   14216:        } \
1.1       root     14217: }
1.1.1.8   root     14218: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     14219: 
1.1.1.8   root     14220: UINT8 cmos_read(int addr)
1.1       root     14221: {
1.1.1.8   root     14222:        static SYSTEMTIME time;
                   14223:        static UINT32 prev_sec = 0;
1.1       root     14224:        
1.1.1.8   root     14225:        switch(addr & 0x7f) {
                   14226:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   14227:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   14228:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   14229:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   14230:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   14231:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   14232:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   14233: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   14234:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   14235:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   14236:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   14237:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   14238:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   14239:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   14240:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   14241:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     14242:        }
1.1.1.8   root     14243:        return(cmos[addr & 0x7f]);
1.1       root     14244: }
                   14245: 
1.1.1.7   root     14246: // kbd (a20)
                   14247: 
                   14248: void kbd_init()
                   14249: {
1.1.1.8   root     14250:        kbd_data = kbd_command = 0;
1.1.1.7   root     14251:        kbd_status = 0x18;
                   14252: }
                   14253: 
                   14254: UINT8 kbd_read_data()
                   14255: {
1.1.1.8   root     14256:        kbd_status &= ~1;
1.1.1.7   root     14257:        return(kbd_data);
                   14258: }
                   14259: 
                   14260: void kbd_write_data(UINT8 val)
                   14261: {
                   14262:        switch(kbd_command) {
                   14263:        case 0xd1:
                   14264:                i386_set_a20_line((val >> 1) & 1);
                   14265:                break;
                   14266:        }
                   14267:        kbd_command = 0;
1.1.1.8   root     14268:        kbd_status &= ~8;
1.1.1.7   root     14269: }
                   14270: 
                   14271: UINT8 kbd_read_status()
                   14272: {
                   14273:        return(kbd_status);
                   14274: }
                   14275: 
                   14276: void kbd_write_command(UINT8 val)
                   14277: {
                   14278:        switch(val) {
                   14279:        case 0xd0:
                   14280:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     14281:                kbd_status |= 1;
1.1.1.7   root     14282:                break;
                   14283:        case 0xdd:
                   14284:                i386_set_a20_line(0);
                   14285:                break;
                   14286:        case 0xdf:
                   14287:                i386_set_a20_line(1);
                   14288:                break;
1.1.1.26  root     14289:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   14290:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     14291:                if(!(val & 1)) {
1.1.1.8   root     14292:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     14293:                                // reset pic
                   14294:                                pic_init();
                   14295:                                pic[0].irr = pic[1].irr = 0x00;
                   14296:                                pic[0].imr = pic[1].imr = 0xff;
                   14297:                        }
                   14298:                        CPU_RESET_CALL(CPU_MODEL);
                   14299:                        i386_jmp_far(0x40, 0x67);
                   14300:                }
                   14301:                i386_set_a20_line((val >> 1) & 1);
                   14302:                break;
                   14303:        }
                   14304:        kbd_command = val;
1.1.1.8   root     14305:        kbd_status |= 8;
1.1.1.7   root     14306: }
                   14307: 
1.1.1.9   root     14308: // vga
                   14309: 
                   14310: UINT8 vga_read_status()
                   14311: {
                   14312:        // 60hz
                   14313:        static const int period[3] = {16, 17, 17};
                   14314:        static int index = 0;
                   14315:        UINT32 time = timeGetTime() % period[index];
                   14316:        
                   14317:        index = (index + 1) % 3;
1.1.1.14  root     14318:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     14319: }
                   14320: 
1.1       root     14321: // i/o bus
                   14322: 
1.1.1.29  root     14323: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   14324: //#define SW1US_PATCH
                   14325: 
1.1.1.25  root     14326: #ifdef ENABLE_DEBUG_IOPORT
                   14327: UINT8 read_io_byte_debug(offs_t addr);
                   14328: 
                   14329: UINT8 read_io_byte(offs_t addr)
                   14330: {
                   14331:        UINT8 val = read_io_byte_debug(addr);
                   14332:        if(fdebug != NULL) {
                   14333:                fprintf(fdebug, "inb %04X, %02X\n", addr, val);
                   14334:        }
                   14335:        return(val);
                   14336: }
                   14337: 
                   14338: UINT8 read_io_byte_debug(offs_t addr)
                   14339: #else
1.1       root     14340: UINT8 read_io_byte(offs_t addr)
1.1.1.25  root     14341: #endif
1.1       root     14342: {
                   14343:        switch(addr) {
1.1.1.29  root     14344: #ifdef SW1US_PATCH
                   14345:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   14346:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   14347:                return(sio_read(0, addr - 1));
                   14348: #else
1.1.1.25  root     14349:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   14350:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   14351:                return(dma_read(0, addr));
1.1.1.29  root     14352: #endif
1.1.1.25  root     14353:        case 0x20: case 0x21:
1.1       root     14354:                return(pic_read(0, addr));
1.1.1.25  root     14355:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     14356:                return(pit_read(addr & 0x03));
1.1.1.7   root     14357:        case 0x60:
                   14358:                return(kbd_read_data());
1.1.1.9   root     14359:        case 0x61:
                   14360:                return(system_port);
1.1.1.7   root     14361:        case 0x64:
                   14362:                return(kbd_read_status());
1.1       root     14363:        case 0x71:
1.1.1.8   root     14364:                return(cmos_read(cmos_addr));
1.1.1.25  root     14365:        case 0x81:
                   14366:                return(dma_page_read(0, 2));
                   14367:        case 0x82:
                   14368:                return(dma_page_read(0, 3));
                   14369:        case 0x83:
                   14370:                return(dma_page_read(0, 1));
                   14371:        case 0x87:
                   14372:                return(dma_page_read(0, 0));
                   14373:        case 0x89:
                   14374:                return(dma_page_read(1, 2));
                   14375:        case 0x8a:
                   14376:                return(dma_page_read(1, 3));
                   14377:        case 0x8b:
                   14378:                return(dma_page_read(1, 1));
                   14379:        case 0x8f:
                   14380:                return(dma_page_read(1, 0));
1.1       root     14381:        case 0x92:
1.1.1.3   root     14382:                return((m_a20_mask >> 19) & 2);
1.1.1.25  root     14383:        case 0xa0: case 0xa1:
1.1       root     14384:                return(pic_read(1, addr));
1.1.1.25  root     14385:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   14386:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     14387:                return(dma_read(1, (addr - 0xc0) >> 1));
                   14388: //     case 0x278: case 0x279: case 0x27a:
                   14389: //             return(pio_read(1, addr));
1.1.1.29  root     14390:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   14391:                return(sio_read(3, addr));
1.1.1.25  root     14392:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   14393:                return(sio_read(1, addr));
                   14394:        case 0x378: case 0x379: case 0x37a:
                   14395:                return(pio_read(0, addr));
                   14396:        case 0x3ba: case 0x3da:
1.1.1.9   root     14397:                return(vga_read_status());
1.1.1.29  root     14398:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   14399:                return(sio_read(2, addr));
1.1.1.25  root     14400:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   14401:                return(sio_read(0, addr));
1.1       root     14402:        default:
                   14403: //             error("inb %4x\n", addr);
                   14404:                break;
                   14405:        }
                   14406:        return(0xff);
                   14407: }
                   14408: 
                   14409: UINT16 read_io_word(offs_t addr)
                   14410: {
                   14411:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   14412: }
                   14413: 
                   14414: UINT32 read_io_dword(offs_t addr)
                   14415: {
                   14416:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   14417: }
                   14418: 
                   14419: void write_io_byte(offs_t addr, UINT8 val)
                   14420: {
1.1.1.25  root     14421: #ifdef ENABLE_DEBUG_IOPORT
                   14422:        if(fdebug != NULL) {
                   14423:                fprintf(fdebug, "outb %04X, %02X\n", addr, val);
                   14424:        }
                   14425: #endif
1.1       root     14426:        switch(addr) {
1.1.1.29  root     14427: #ifdef SW1US_PATCH
                   14428:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   14429:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   14430:                sio_write(0, addr - 1, val);
                   14431:                break;
                   14432: #else
1.1.1.25  root     14433:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   14434:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   14435:                dma_write(0, addr, val);
                   14436:                break;
1.1.1.29  root     14437: #endif
1.1.1.25  root     14438:        case 0x20: case 0x21:
1.1       root     14439:                pic_write(0, addr, val);
                   14440:                break;
1.1.1.25  root     14441:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     14442:                pit_write(addr & 0x03, val);
                   14443:                break;
1.1.1.7   root     14444:        case 0x60:
                   14445:                kbd_write_data(val);
                   14446:                break;
1.1.1.9   root     14447:        case 0x61:
                   14448:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   14449:                        // beep on
                   14450: //                     MessageBeep(-1);
                   14451:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   14452:                        // beep off
                   14453:                }
                   14454:                system_port = val;
                   14455:                break;
1.1       root     14456:        case 0x64:
1.1.1.7   root     14457:                kbd_write_command(val);
1.1       root     14458:                break;
                   14459:        case 0x70:
                   14460:                cmos_addr = val;
                   14461:                break;
                   14462:        case 0x71:
1.1.1.8   root     14463:                cmos_write(cmos_addr, val);
1.1       root     14464:                break;
1.1.1.25  root     14465:        case 0x81:
                   14466:                dma_page_write(0, 2, val);
                   14467:        case 0x82:
                   14468:                dma_page_write(0, 3, val);
                   14469:        case 0x83:
                   14470:                dma_page_write(0, 1, val);
                   14471:        case 0x87:
                   14472:                dma_page_write(0, 0, val);
                   14473:        case 0x89:
                   14474:                dma_page_write(1, 2, val);
                   14475:        case 0x8a:
                   14476:                dma_page_write(1, 3, val);
                   14477:        case 0x8b:
                   14478:                dma_page_write(1, 1, val);
                   14479:        case 0x8f:
                   14480:                dma_page_write(1, 0, val);
1.1       root     14481:        case 0x92:
1.1.1.7   root     14482:                i386_set_a20_line((val >> 1) & 1);
1.1       root     14483:                break;
1.1.1.25  root     14484:        case 0xa0: case 0xa1:
1.1       root     14485:                pic_write(1, addr, val);
                   14486:                break;
1.1.1.25  root     14487:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   14488:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     14489:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     14490:                break;
1.1.1.26  root     14491: //     case 0x278: case 0x279: case 0x27a:
                   14492: //             pio_write(1, addr, val);
                   14493: //             break;
1.1.1.29  root     14494:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   14495:                sio_write(3, addr, val);
                   14496:                break;
1.1.1.25  root     14497:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   14498:                sio_write(1, addr, val);
                   14499:                break;
                   14500:        case 0x378: case 0x379: case 0x37a:
                   14501:                pio_write(0, addr, val);
                   14502:                break;
1.1.1.29  root     14503:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   14504:                sio_write(2, addr, val);
                   14505:                break;
1.1.1.25  root     14506:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   14507:                sio_write(0, addr, val);
                   14508:                break;
1.1       root     14509:        default:
                   14510: //             error("outb %4x,%2x\n", addr, val);
                   14511:                break;
                   14512:        }
                   14513: }
                   14514: 
                   14515: void write_io_word(offs_t addr, UINT16 val)
                   14516: {
                   14517:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   14518:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   14519: }
                   14520: 
                   14521: void write_io_dword(offs_t addr, UINT32 val)
                   14522: {
                   14523:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   14524:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   14525:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   14526:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   14527: }

unix.superglobalmegacorp.com

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