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

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       root     4030:        
                   4031:        current_psp = psp_seg;
1.1.1.23  root     4032:        msdos_sda_update(current_psp);
1.1       root     4033:        
                   4034:        if(al == 0x00) {
                   4035:                int_10h_feh_called = int_10h_ffh_called = false;
                   4036:                
                   4037:                // registers and segments
                   4038:                REG16(AX) = REG16(BX) = 0x00;
                   4039:                REG16(CX) = 0xff;
                   4040:                REG16(DX) = psp_seg;
                   4041:                REG16(SI) = ip;
                   4042:                REG16(DI) = sp;
                   4043:                REG16(SP) = sp;
1.1.1.3   root     4044:                SREG(DS) = SREG(ES) = psp_seg;
                   4045:                SREG(SS) = ss;
                   4046:                i386_load_segment_descriptor(DS);
                   4047:                i386_load_segment_descriptor(ES);
                   4048:                i386_load_segment_descriptor(SS);
1.1       root     4049:                
                   4050:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   4051:                i386_jmp_far(cs, ip);
                   4052:        } else if(al == 0x01) {
                   4053:                // copy ss:sp and cs:ip to param block
                   4054:                param->sp = sp;
                   4055:                param->ss = ss;
                   4056:                param->ip = ip;
                   4057:                param->cs = cs;
                   4058:        }
                   4059:        return(0);
                   4060: }
                   4061: 
                   4062: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   4063: {
                   4064:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   4065:        
                   4066:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   4067:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   4068:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   4069:        
1.1.1.3   root     4070:        SREG(SS) = psp->stack.w.h;
                   4071:        i386_load_segment_descriptor(SS);
1.1       root     4072:        REG16(SP) = psp->stack.w.l;
                   4073:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   4074:        
1.1.1.28  root     4075: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   4076:        process_t *current_process = NULL;
                   4077:        for(int i = 0; i < MAX_PROCESS; i++) {
                   4078:                if(process[i].psp == psp_seg) {
                   4079:                        current_process = &process[i];
                   4080:                        break;
                   4081:                }
                   4082:        }
                   4083:        if(current_process == NULL) {
                   4084:                throw(0x1f); // general failure
                   4085:        }
                   4086:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   4087:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   4088:        if(current_process->called_by_int2eh) {
                   4089:                REG16(AX) = ret;
                   4090:        }
                   4091:        SREG(DS) = current_process->parent_ds;
1.1.1.14  root     4092:        i386_load_segment_descriptor(DS);
1.1       root     4093:        
                   4094:        if(mem_free) {
1.1.1.8   root     4095:                int mcb_seg;
                   4096:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   4097:                        msdos_mem_free(mcb_seg + 1);
                   4098:                }
                   4099:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   4100:                        msdos_mem_free(mcb_seg + 1);
                   4101:                }
1.1       root     4102:                
                   4103:                for(int i = 0; i < MAX_FILES; i++) {
                   4104:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   4105:                                _close(i);
1.1.1.20  root     4106:                                msdos_file_handler_close(i);
                   4107:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     4108:                        }
                   4109:                }
1.1.1.13  root     4110:                msdos_dta_info_free(psp_seg);
1.1       root     4111:        }
1.1.1.14  root     4112:        msdos_stdio_reopen();
1.1       root     4113:        
1.1.1.28  root     4114:        memset(current_process, 0, sizeof(process_t));
1.1       root     4115:        
                   4116:        current_psp = psp->parent_psp;
                   4117:        retval = ret;
1.1.1.23  root     4118:        msdos_sda_update(current_psp);
1.1       root     4119: }
                   4120: 
                   4121: // drive
                   4122: 
                   4123: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   4124: {
                   4125:        *seg = DPB_TOP >> 4;
                   4126:        *ofs = sizeof(dpb_t) * drive_num;
                   4127:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   4128:        
                   4129:        if(!force_update && dpb->free_clusters != 0) {
                   4130:                return(dpb->bytes_per_sector ? 1 : 0);
                   4131:        }
                   4132:        memset(dpb, 0, sizeof(dpb_t));
                   4133:        
                   4134:        int res = 0;
                   4135:        char dev[64];
                   4136:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   4137:        
1.1.1.17  root     4138:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     4139:        if(hFile != INVALID_HANDLE_VALUE) {
                   4140:                DISK_GEOMETRY geo;
                   4141:                DWORD dwSize;
                   4142:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   4143:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   4144:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   4145:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     4146:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     4147:                        switch(geo.MediaType) {
                   4148:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   4149:                                dpb->media_type = 0xff;
                   4150:                                break;
                   4151:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   4152:                                dpb->media_type = 0xfe;
                   4153:                                break;
                   4154:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   4155:                                dpb->media_type = 0xfd;
                   4156:                                break;
                   4157:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   4158:                                dpb->media_type = 0xfc;
                   4159:                                break;
                   4160:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   4161:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   4162:                                dpb->media_type = 0xf9;
                   4163:                                break;
                   4164:                        case FixedMedia:        // hard disk
                   4165:                        case RemovableMedia:
1.1.1.19  root     4166:                        case Unknown:
1.1       root     4167:                                dpb->media_type = 0xf8;
                   4168:                                break;
                   4169:                        default:
                   4170:                                dpb->media_type = 0xf0;
                   4171:                                break;
                   4172:                        }
                   4173:                        res = 1;
                   4174:                }
                   4175:                dpb->drive_num = drive_num;
                   4176:                dpb->unit_num = drive_num;
                   4177:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   4178:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     4179:                dpb->info_sector = 0xffff;
                   4180:                dpb->backup_boot_sector = 0xffff;
1.1       root     4181:                dpb->free_clusters = 0xffff;
1.1.1.14  root     4182:                dpb->free_search_cluster = 0xffffffff;
1.1       root     4183:                CloseHandle(hFile);
                   4184:        }
                   4185:        return(res);
                   4186: }
                   4187: 
                   4188: // pc bios
                   4189: 
1.1.1.19  root     4190: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   4191: {
                   4192:        static unsigned __int64 start_msec_since_midnight = 0;
                   4193:        static unsigned __int64 start_msec_since_hostboot = 0;
                   4194:        
                   4195:        if(start_msec_since_midnight == 0) {
                   4196:                SYSTEMTIME time;
                   4197:                GetLocalTime(&time);
                   4198:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   4199:                start_msec_since_hostboot = cur_msec;
                   4200:        }
                   4201:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   4202:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   4203:        return (UINT32)tick;
                   4204: }
                   4205: 
                   4206: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   4207: {
                   4208:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   4209:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   4210:        
                   4211:        if(prev_tick > next_tick) {
                   4212:                mem[0x470] = 1;
                   4213:        }
                   4214:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   4215: }
                   4216: 
1.1.1.14  root     4217: inline void pcbios_irq0()
                   4218: {
                   4219:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     4220:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     4221: }
                   4222: 
1.1.1.16  root     4223: int pcbios_get_text_vram_address(int page)
1.1       root     4224: {
                   4225:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4226:                return TEXT_VRAM_TOP;
1.1       root     4227:        } else {
1.1.1.14  root     4228:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     4229:        }
                   4230: }
                   4231: 
1.1.1.16  root     4232: int pcbios_get_shadow_buffer_address(int page)
1.1       root     4233: {
1.1.1.14  root     4234:        if(!int_10h_feh_called) {
1.1.1.16  root     4235:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     4236:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4237:                return SHADOW_BUF_TOP;
                   4238:        } else {
1.1.1.14  root     4239:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     4240:        }
                   4241: }
                   4242: 
1.1.1.16  root     4243: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     4244: {
1.1.1.16  root     4245:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     4246: }
                   4247: 
1.1.1.16  root     4248: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     4249: {
1.1.1.14  root     4250:        // clear the existing screen, not just the new one
                   4251:        int clr_height = max(height, scr_height);
                   4252:        
1.1.1.16  root     4253:        if(scr_width != width || scr_height != height) {
                   4254:                change_console_size(width, height);
1.1.1.14  root     4255:        }
                   4256:        mem[0x462] = 0;
                   4257:        *(UINT16 *)(mem + 0x44e) = 0;
                   4258:        
1.1.1.16  root     4259:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   4260:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   4261:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   4262:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     4263:        
1.1.1.23  root     4264:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     4265:        if(clr_screen) {
1.1.1.14  root     4266:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   4267:                        mem[ofs++] = 0x20;
                   4268:                        mem[ofs++] = 0x07;
                   4269:                }
                   4270:                
                   4271:                EnterCriticalSection(&vram_crit_sect);
                   4272:                for(int y = 0; y < clr_height; y++) {
                   4273:                        for(int x = 0; x < scr_width; x++) {
                   4274:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4275:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     4276:                        }
                   4277:                }
                   4278:                SMALL_RECT rect;
1.1.1.14  root     4279:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   4280:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4281:                vram_length_char = vram_last_length_char = 0;
                   4282:                vram_length_attr = vram_last_length_attr = 0;
                   4283:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4284:        }
1.1.1.14  root     4285:        COORD co;
                   4286:        co.X = 0;
                   4287:        co.Y = scr_top;
                   4288:        SetConsoleCursorPosition(hStdout, co);
                   4289:        cursor_moved = true;
                   4290:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     4291: }
                   4292: 
1.1.1.16  root     4293: inline void pcbios_int_10h_00h()
                   4294: {
                   4295:        switch(REG8(AL) & 0x7f) {
                   4296:        case 0x70: // v-text mode
                   4297:        case 0x71: // extended cga v-text mode
                   4298:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   4299:                break;
                   4300:        default:
                   4301:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   4302:                break;
                   4303:        }
                   4304:        if(REG8(AL) & 0x80) {
                   4305:                mem[0x487] |= 0x80;
                   4306:        } else {
                   4307:                mem[0x487] &= ~0x80;
                   4308:        }
                   4309:        mem[0x449] = REG8(AL) & 0x7f;
                   4310: }
                   4311: 
1.1       root     4312: inline void pcbios_int_10h_01h()
                   4313: {
1.1.1.13  root     4314:        mem[0x460] = REG8(CL);
                   4315:        mem[0x461] = REG8(CH);
1.1.1.14  root     4316:        
1.1.1.23  root     4317:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4318:        CONSOLE_CURSOR_INFO ci;
                   4319:        GetConsoleCursorInfo(hStdout, &ci);
                   4320: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   4321: //     if(ci.bVisible) {
                   4322:                int lines = max(8, REG8(CL) + 1);
                   4323:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   4324: //     }
                   4325:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     4326: }
                   4327: 
                   4328: inline void pcbios_int_10h_02h()
                   4329: {
1.1.1.14  root     4330:        // continuously setting the cursor effectively stops it blinking
                   4331:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     4332:                COORD co;
                   4333:                co.X = REG8(DL);
1.1.1.14  root     4334:                co.Y = REG8(DH) + scr_top;
                   4335:                
                   4336:                // some programs hide the cursor by moving it off screen
                   4337:                static bool hidden = false;
1.1.1.23  root     4338:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4339:                CONSOLE_CURSOR_INFO ci;
                   4340:                GetConsoleCursorInfo(hStdout, &ci);
                   4341:                
                   4342:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   4343:                        if(ci.bVisible) {
                   4344:                                ci.bVisible = FALSE;
                   4345: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4346:                                hidden = true;
                   4347:                        }
                   4348:                } else if(hidden) {
                   4349:                        if(!ci.bVisible) {
                   4350:                                ci.bVisible = TRUE;
                   4351: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4352:                        }
                   4353:                        hidden = false;
                   4354:                }
1.1       root     4355:        }
1.1.1.14  root     4356:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   4357:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     4358: }
                   4359: 
                   4360: inline void pcbios_int_10h_03h()
                   4361: {
1.1.1.14  root     4362:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4363:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4364:        REG8(CL) = mem[0x460];
                   4365:        REG8(CH) = mem[0x461];
                   4366: }
                   4367: 
                   4368: inline void pcbios_int_10h_05h()
                   4369: {
1.1.1.14  root     4370:        if(REG8(AL) >= vram_pages) {
                   4371:                return;
                   4372:        }
                   4373:        if(mem[0x462] != REG8(AL)) {
                   4374:                vram_flush();
                   4375:                
1.1.1.23  root     4376:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4377:                SMALL_RECT rect;
1.1.1.14  root     4378:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4379:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4380:                
1.1.1.16  root     4381:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     4382:                        for(int x = 0; x < scr_width; x++) {
                   4383:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4384:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4385:                        }
                   4386:                }
1.1.1.16  root     4387:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     4388:                        for(int x = 0; x < scr_width; x++) {
                   4389:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   4390:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     4391:                        }
                   4392:                }
1.1.1.14  root     4393:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4394:                
                   4395:                COORD co;
1.1.1.14  root     4396:                co.X = mem[0x450 + REG8(AL) * 2];
                   4397:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   4398:                if(co.Y < scr_top + scr_height) {
                   4399:                        SetConsoleCursorPosition(hStdout, co);
                   4400:                }
1.1       root     4401:        }
1.1.1.14  root     4402:        mem[0x462] = REG8(AL);
                   4403:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   4404:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     4405:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     4406:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     4407:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     4408:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     4409: }
                   4410: 
                   4411: inline void pcbios_int_10h_06h()
                   4412: {
1.1.1.14  root     4413:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4414:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4415:                return;
                   4416:        }
                   4417:        vram_flush();
                   4418:        
1.1.1.23  root     4419:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4420:        SMALL_RECT rect;
1.1.1.14  root     4421:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4422:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4423:        
                   4424:        int right = min(REG8(DL), scr_width - 1);
                   4425:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4426:        
                   4427:        if(REG8(AL) == 0) {
1.1.1.14  root     4428:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4429:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4430:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4431:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4432:                        }
                   4433:                }
                   4434:        } else {
1.1.1.14  root     4435:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     4436:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4437:                                if(y2 <= bottom) {
                   4438:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4439:                                } else {
1.1.1.14  root     4440:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4441:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4442:                                }
1.1.1.14  root     4443:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4444:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4445:                        }
                   4446:                }
                   4447:        }
1.1.1.14  root     4448:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4449: }
                   4450: 
                   4451: inline void pcbios_int_10h_07h()
                   4452: {
1.1.1.14  root     4453:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4454:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4455:                return;
                   4456:        }
                   4457:        vram_flush();
                   4458:        
1.1.1.23  root     4459:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4460:        SMALL_RECT rect;
1.1.1.14  root     4461:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4462:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4463:        
                   4464:        int right = min(REG8(DL), scr_width - 1);
                   4465:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4466:        
                   4467:        if(REG8(AL) == 0) {
1.1.1.14  root     4468:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4469:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4470:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4471:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4472:                        }
                   4473:                }
                   4474:        } else {
1.1.1.14  root     4475:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     4476:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4477:                                if(y2 >= REG8(CH)) {
                   4478:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4479:                                } else {
1.1.1.14  root     4480:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4481:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4482:                                }
1.1.1.14  root     4483:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4484:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4485:                        }
                   4486:                }
                   4487:        }
1.1.1.14  root     4488:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4489: }
                   4490: 
                   4491: inline void pcbios_int_10h_08h()
                   4492: {
                   4493:        COORD co;
                   4494:        DWORD num;
                   4495:        
1.1.1.14  root     4496:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4497:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4498:        
                   4499:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4500:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4501:                co.Y += scr_top;
                   4502:                vram_flush();
1.1       root     4503:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   4504:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   4505:                REG8(AL) = scr_char[0];
                   4506:                REG8(AH) = scr_attr[0];
                   4507:        } else {
1.1.1.16  root     4508:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     4509:        }
                   4510: }
                   4511: 
                   4512: inline void pcbios_int_10h_09h()
                   4513: {
                   4514:        COORD co;
                   4515:        
1.1.1.14  root     4516:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4517:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4518:        
1.1.1.16  root     4519:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4520:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4521:        
                   4522:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4523:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4524:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4525:                while(dest < end) {
                   4526:                        write_text_vram_char(dest - vram, REG8(AL));
                   4527:                        mem[dest++] = REG8(AL);
                   4528:                        write_text_vram_attr(dest - vram, REG8(BL));
                   4529:                        mem[dest++] = REG8(BL);
1.1       root     4530:                }
1.1.1.14  root     4531:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4532:        } else {
1.1.1.14  root     4533:                while(dest < end) {
1.1       root     4534:                        mem[dest++] = REG8(AL);
                   4535:                        mem[dest++] = REG8(BL);
                   4536:                }
                   4537:        }
                   4538: }
                   4539: 
                   4540: inline void pcbios_int_10h_0ah()
                   4541: {
                   4542:        COORD co;
                   4543:        
1.1.1.14  root     4544:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4545:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4546:        
1.1.1.16  root     4547:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4548:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4549:        
                   4550:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4551:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4552:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4553:                while(dest < end) {
                   4554:                        write_text_vram_char(dest - vram, REG8(AL));
                   4555:                        mem[dest++] = REG8(AL);
                   4556:                        dest++;
1.1       root     4557:                }
1.1.1.14  root     4558:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4559:        } else {
1.1.1.14  root     4560:                while(dest < end) {
1.1       root     4561:                        mem[dest++] = REG8(AL);
                   4562:                        dest++;
                   4563:                }
                   4564:        }
                   4565: }
                   4566: 
                   4567: inline void pcbios_int_10h_0eh()
                   4568: {
1.1.1.14  root     4569:        DWORD num;
                   4570:        COORD co;
                   4571:        
                   4572:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4573:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4574:        
                   4575:        if(REG8(AL) == 7) {
                   4576:                //MessageBeep(-1);
                   4577:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   4578:                if(REG8(AL) == 10) {
                   4579:                        vram_flush();
                   4580:                }
1.1.1.23  root     4581:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     4582:                cursor_moved = true;
                   4583:        } else {
1.1.1.16  root     4584:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     4585:                if(mem[0x462] == REG8(BH)) {
                   4586:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4587:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4588:                        write_text_vram_char(dest - vram, REG8(AL));
                   4589:                        LeaveCriticalSection(&vram_crit_sect);
                   4590:                        
1.1.1.23  root     4591:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4592:                        if(++co.X == scr_width) {
                   4593:                                co.X = 0;
                   4594:                                if(++co.Y == scr_height) {
                   4595:                                        vram_flush();
                   4596:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   4597:                                        cursor_moved = true;
                   4598:                                }
                   4599:                        }
                   4600:                        if(!cursor_moved) {
                   4601:                                co.Y += scr_top;
                   4602:                                SetConsoleCursorPosition(hStdout, co);
                   4603:                                cursor_moved = true;
                   4604:                        }
                   4605:                }
                   4606:                mem[dest] = REG8(AL);
                   4607:        }
1.1       root     4608: }
                   4609: 
                   4610: inline void pcbios_int_10h_0fh()
                   4611: {
                   4612:        REG8(AL) = mem[0x449];
                   4613:        REG8(AH) = mem[0x44a];
                   4614:        REG8(BH) = mem[0x462];
                   4615: }
                   4616: 
1.1.1.14  root     4617: inline void pcbios_int_10h_11h()
                   4618: {
                   4619:        switch(REG8(AL)) {
1.1.1.16  root     4620:        case 0x01:
1.1.1.14  root     4621:        case 0x11:
1.1.1.16  root     4622:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     4623:                break;
1.1.1.16  root     4624:        case 0x02:
1.1.1.14  root     4625:        case 0x12:
1.1.1.16  root     4626:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4627:                break;
1.1.1.16  root     4628:        case 0x04:
1.1.1.14  root     4629:        case 0x14:
1.1.1.16  root     4630:                pcbios_set_console_size(80, 25, true);
                   4631:                break;
                   4632:        case 0x18:
                   4633:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4634:                break;
                   4635:        case 0x30:
                   4636:                SREG(ES) = 0;
                   4637:                i386_load_segment_descriptor(ES);
                   4638:                REG16(BP) = 0;
                   4639:                REG16(CX) = mem[0x485];
                   4640:                REG8(DL) = mem[0x484];
                   4641:                break;
                   4642:        }
                   4643: }
                   4644: 
                   4645: inline void pcbios_int_10h_12h()
                   4646: {
1.1.1.16  root     4647:        switch(REG8(BL)) {
                   4648:        case 0x10:
1.1.1.14  root     4649:                REG16(BX) = 0x0003;
                   4650:                REG16(CX) = 0x0009;
1.1.1.16  root     4651:                break;
1.1.1.14  root     4652:        }
                   4653: }
                   4654: 
1.1       root     4655: inline void pcbios_int_10h_13h()
                   4656: {
1.1.1.3   root     4657:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     4658:        COORD co;
                   4659:        DWORD num;
                   4660:        
                   4661:        co.X = REG8(DL);
1.1.1.14  root     4662:        co.Y = REG8(DH) + scr_top;
                   4663:        
                   4664:        vram_flush();
1.1       root     4665:        
                   4666:        switch(REG8(AL)) {
                   4667:        case 0x00:
                   4668:        case 0x01:
                   4669:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4670:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4671:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4672:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4673:                        SetConsoleCursorPosition(hStdout, co);
                   4674:                        
                   4675:                        if(csbi.wAttributes != REG8(BL)) {
                   4676:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   4677:                        }
1.1.1.14  root     4678:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   4679:                        
1.1       root     4680:                        if(csbi.wAttributes != REG8(BL)) {
                   4681:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4682:                        }
                   4683:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     4684:                                if(!restore_console_on_exit) {
                   4685:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4686:                                        scr_top = csbi.srWindow.Top;
                   4687:                                }
1.1.1.14  root     4688:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4689:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4690:                                SetConsoleCursorPosition(hStdout, co);
                   4691:                        } else {
                   4692:                                cursor_moved = true;
                   4693:                        }
                   4694:                } else {
1.1.1.3   root     4695:                        m_CF = 1;
1.1       root     4696:                }
                   4697:                break;
                   4698:        case 0x02:
                   4699:        case 0x03:
                   4700:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4701:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4702:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4703:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4704:                        SetConsoleCursorPosition(hStdout, co);
                   4705:                        
                   4706:                        WORD wAttributes = csbi.wAttributes;
                   4707:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   4708:                                if(wAttributes != mem[ofs + 1]) {
                   4709:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   4710:                                        wAttributes = mem[ofs + 1];
                   4711:                                }
1.1.1.14  root     4712:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     4713:                        }
                   4714:                        if(csbi.wAttributes != wAttributes) {
                   4715:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4716:                        }
                   4717:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     4718:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4719:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4720:                                SetConsoleCursorPosition(hStdout, co);
                   4721:                        } else {
                   4722:                                cursor_moved = true;
                   4723:                        }
                   4724:                } else {
1.1.1.3   root     4725:                        m_CF = 1;
1.1       root     4726:                }
                   4727:                break;
                   4728:        case 0x10:
                   4729:        case 0x11:
                   4730:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4731:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4732:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   4733:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   4734:                        for(int i = 0; i < num; i++) {
                   4735:                                mem[ofs++] = scr_char[i];
                   4736:                                mem[ofs++] = scr_attr[i];
                   4737:                                if(REG8(AL) == 0x11) {
                   4738:                                        mem[ofs++] = 0;
                   4739:                                        mem[ofs++] = 0;
                   4740:                                }
                   4741:                        }
                   4742:                } else {
1.1.1.16  root     4743:                        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     4744:                                mem[ofs++] = mem[src++];
                   4745:                                mem[ofs++] = mem[src++];
                   4746:                                if(REG8(AL) == 0x11) {
                   4747:                                        mem[ofs++] = 0;
                   4748:                                        mem[ofs++] = 0;
                   4749:                                }
1.1.1.14  root     4750:                                if(++co.X == scr_width) {
                   4751:                                        if(++co.Y == scr_height) {
1.1       root     4752:                                                break;
                   4753:                                        }
                   4754:                                        co.X = 0;
                   4755:                                }
                   4756:                        }
                   4757:                }
                   4758:                break;
                   4759:        case 0x20:
                   4760:        case 0x21:
                   4761:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4762:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4763:                        int len = min(REG16(CX), scr_width * scr_height);
                   4764:                        for(int i = 0; i < len; i++) {
1.1       root     4765:                                scr_char[i] = mem[ofs++];
                   4766:                                scr_attr[i] = mem[ofs++];
                   4767:                                if(REG8(AL) == 0x21) {
                   4768:                                        ofs += 2;
                   4769:                                }
                   4770:                        }
1.1.1.14  root     4771:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4772:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4773:                } else {
1.1.1.16  root     4774:                        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     4775:                                mem[dest++] = mem[ofs++];
                   4776:                                mem[dest++] = mem[ofs++];
                   4777:                                if(REG8(AL) == 0x21) {
                   4778:                                        ofs += 2;
                   4779:                                }
1.1.1.14  root     4780:                                if(++co.X == scr_width) {
                   4781:                                        if(++co.Y == scr_height) {
1.1       root     4782:                                                break;
                   4783:                                        }
                   4784:                                        co.X = 0;
                   4785:                                }
                   4786:                        }
                   4787:                }
                   4788:                break;
                   4789:        default:
1.1.1.22  root     4790:                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     4791:                m_CF = 1;
1.1       root     4792:                break;
                   4793:        }
                   4794: }
                   4795: 
1.1.1.30! root     4796: inline void pcbios_int_10h_18h()
        !          4797: {
        !          4798:        switch(REG8(AL)) {
        !          4799:        case 0x00:
        !          4800:        case 0x01:
        !          4801: //             REG8(AL) = 0x86;
        !          4802:                REG8(AL) = 0x00;
        !          4803:                break;
        !          4804:        default:
        !          4805:                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));
        !          4806:                m_CF = 1;
        !          4807:                break;
        !          4808:        }
        !          4809: }
        !          4810: 
1.1.1.14  root     4811: inline void pcbios_int_10h_1ah()
                   4812: {
                   4813:        switch(REG8(AL)) {
                   4814:        case 0x00:
                   4815:                REG8(AL) = 0x1a;
                   4816:                REG8(BL) = 0x08;
                   4817:                REG8(BH) = 0x00;
                   4818:                break;
                   4819:        default:
1.1.1.22  root     4820:                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     4821:                m_CF = 1;
                   4822:                break;
                   4823:        }
                   4824: }
                   4825: 
1.1       root     4826: inline void pcbios_int_10h_1dh()
                   4827: {
                   4828:        switch(REG8(AL)) {
                   4829:        case 0x01:
                   4830:                break;
                   4831:        case 0x02:
                   4832:                REG16(BX) = 0;
                   4833:                break;
                   4834:        default:
1.1.1.22  root     4835:                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));
                   4836:                m_CF = 1;
                   4837:                break;
                   4838:        }
                   4839: }
                   4840: 
                   4841: inline void pcbios_int_10h_4fh()
                   4842: {
                   4843:        switch(REG8(AL)) {
                   4844:        case 0x00:
                   4845:                REG8(AH) = 0x02; // not supported
                   4846:                break;
                   4847:        case 0x01:
                   4848:        case 0x02:
                   4849:        case 0x03:
                   4850:        case 0x04:
                   4851:        case 0x05:
                   4852:        case 0x06:
                   4853:        case 0x07:
                   4854:        case 0x08:
                   4855:        case 0x09:
                   4856:        case 0x0a:
                   4857:        case 0x0b:
                   4858:        case 0x0c:
                   4859:                REG8(AH) = 0x01; // failed
                   4860:                break;
                   4861:        default:
                   4862:                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     4863:                m_CF = 1;
1.1       root     4864:                break;
                   4865:        }
                   4866: }
                   4867: 
                   4868: inline void pcbios_int_10h_82h()
                   4869: {
                   4870:        static UINT8 mode = 0;
                   4871:        
                   4872:        switch(REG8(AL)) {
1.1.1.22  root     4873:        case 0x00:
1.1       root     4874:                if(REG8(BL) != 0xff) {
                   4875:                        mode = REG8(BL);
                   4876:                }
                   4877:                REG8(AL) = mode;
                   4878:                break;
                   4879:        default:
1.1.1.22  root     4880:                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     4881:                m_CF = 1;
1.1       root     4882:                break;
                   4883:        }
                   4884: }
                   4885: 
1.1.1.22  root     4886: inline void pcbios_int_10h_83h()
                   4887: {
                   4888:        static UINT8 mode = 0;
                   4889:        
                   4890:        switch(REG8(AL)) {
                   4891:        case 0x00:
                   4892:                REG16(AX) = 0; // offset???
                   4893:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   4894:                i386_load_segment_descriptor(ES);
                   4895:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   4896:                break;
                   4897:        default:
                   4898:                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));
                   4899:                m_CF = 1;
                   4900:                break;
                   4901:        }
                   4902: }
                   4903: 
                   4904: inline void pcbios_int_10h_90h()
                   4905: {
                   4906:        REG8(AL) = mem[0x449];
                   4907: }
                   4908: 
                   4909: inline void pcbios_int_10h_91h()
                   4910: {
                   4911:        REG8(AL) = 0x04; // VGA
                   4912: }
                   4913: 
                   4914: inline void pcbios_int_10h_efh()
                   4915: {
                   4916:        REG16(DX) = 0xffff;
                   4917: }
                   4918: 
1.1       root     4919: inline void pcbios_int_10h_feh()
                   4920: {
                   4921:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4922:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     4923:                i386_load_segment_descriptor(ES);
1.1.1.8   root     4924:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     4925:        }
                   4926:        int_10h_feh_called = true;
                   4927: }
                   4928: 
                   4929: inline void pcbios_int_10h_ffh()
                   4930: {
                   4931:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     4932:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4933:                COORD co;
                   4934:                DWORD num;
                   4935:                
1.1.1.14  root     4936:                vram_flush();
                   4937:                
                   4938:                co.X = (REG16(DI) >> 1) % scr_width;
                   4939:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     4940:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   4941:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     4942:                int len;
                   4943:                for(len = 0; ofs < end; len++) {
                   4944:                        scr_char[len] = mem[ofs++];
                   4945:                        scr_attr[len] = mem[ofs++];
                   4946:                }
                   4947:                co.Y += scr_top;
                   4948:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4949:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4950:        }
                   4951:        int_10h_ffh_called = true;
                   4952: }
                   4953: 
1.1.1.25  root     4954: inline void pcbios_int_14h_00h()
                   4955: {
1.1.1.29  root     4956:        if(REG16(DX) < 4) {
1.1.1.25  root     4957:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   4958:                UINT8 selector = sio_read(REG16(DX), 3);
                   4959:                selector &= ~0x3f;
                   4960:                selector |= REG8(AL) & 0x1f;
                   4961:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   4962:                sio_write(REG16(DX), 3, selector | 0x80);
                   4963:                sio_write(REG16(DX), 0, divisor & 0xff);
                   4964:                sio_write(REG16(DX), 1, divisor >> 8);
                   4965:                sio_write(REG16(DX), 3, selector);
                   4966:                REG8(AH) = sio_read(REG16(DX), 5);
                   4967:                REG8(AL) = sio_read(REG16(DX), 6);
                   4968:        } else {
                   4969:                REG8(AH) = 0x80;
                   4970:        }
                   4971: }
                   4972: 
                   4973: inline void pcbios_int_14h_01h()
                   4974: {
1.1.1.29  root     4975:        if(REG16(DX) < 4) {
1.1.1.25  root     4976:                UINT8 selector = sio_read(REG16(DX), 3);
                   4977:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4978:                sio_write(REG16(DX), 0, REG8(AL));
                   4979:                sio_write(REG16(DX), 3, selector);
                   4980:                REG8(AH) = sio_read(REG16(DX), 5);
                   4981:        } else {
                   4982:                REG8(AH) = 0x80;
                   4983:        }
                   4984: }
                   4985: 
                   4986: inline void pcbios_int_14h_02h()
                   4987: {
1.1.1.29  root     4988:        if(REG16(DX) < 4) {
1.1.1.25  root     4989:                UINT8 selector = sio_read(REG16(DX), 3);
                   4990:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4991:                REG8(AL) = sio_read(REG16(DX), 0);
                   4992:                sio_write(REG16(DX), 3, selector);
                   4993:                REG8(AH) = sio_read(REG16(DX), 5);
                   4994:        } else {
                   4995:                REG8(AH) = 0x80;
                   4996:        }
                   4997: }
                   4998: 
                   4999: inline void pcbios_int_14h_03h()
                   5000: {
1.1.1.29  root     5001:        if(REG16(DX) < 4) {
1.1.1.25  root     5002:                REG8(AH) = sio_read(REG16(DX), 5);
                   5003:                REG8(AL) = sio_read(REG16(DX), 6);
                   5004:        } else {
                   5005:                REG8(AH) = 0x80;
                   5006:        }
                   5007: }
                   5008: 
                   5009: inline void pcbios_int_14h_04h()
                   5010: {
1.1.1.29  root     5011:        if(REG16(DX) < 4) {
1.1.1.25  root     5012:                UINT8 selector = sio_read(REG16(DX), 3);
                   5013:                if(REG8(CH) <= 0x03) {
                   5014:                        selector = (selector & ~0x03) | REG8(CH);
                   5015:                }
                   5016:                if(REG8(BL) == 0x00) {
                   5017:                        selector &= ~0x04;
                   5018:                } else if(REG8(BL) == 0x01) {
                   5019:                        selector |= 0x04;
                   5020:                }
                   5021:                if(REG8(BH) == 0x00) {
                   5022:                        selector = (selector & ~0x38) | 0x00;
                   5023:                } else if(REG8(BH) == 0x01) {
                   5024:                        selector = (selector & ~0x38) | 0x08;
                   5025:                } else if(REG8(BH) == 0x02) {
                   5026:                        selector = (selector & ~0x38) | 0x18;
                   5027:                } else if(REG8(BH) == 0x03) {
                   5028:                        selector = (selector & ~0x38) | 0x28;
                   5029:                } else if(REG8(BH) == 0x04) {
                   5030:                        selector = (selector & ~0x38) | 0x38;
                   5031:                }
                   5032:                if(REG8(AL) == 0x00) {
                   5033:                        selector |= 0x40;
                   5034:                } else if(REG8(AL) == 0x01) {
                   5035:                        selector &= ~0x40;
                   5036:                }
                   5037:                if(REG8(CL) <= 0x0b) {
                   5038:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   5039:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   5040:                        sio_write(REG16(DX), 3, selector | 0x80);
                   5041:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   5042:                        sio_write(REG16(DX), 1, divisor >> 8);
                   5043:                }
                   5044:                sio_write(REG16(DX), 3, selector);
                   5045:                REG8(AH) = sio_read(REG16(DX), 5);
                   5046:                REG8(AL) = sio_read(REG16(DX), 6);
                   5047:        } else {
                   5048:                REG8(AH) = 0x80;
                   5049:        }
                   5050: }
                   5051: 
                   5052: inline void pcbios_int_14h_05h()
                   5053: {
1.1.1.29  root     5054:        if(REG16(DX) < 4) {
1.1.1.25  root     5055:                if(REG8(AL) == 0x00) {
                   5056:                        REG8(BL) = sio_read(REG16(DX), 4);
                   5057:                        REG8(AH) = sio_read(REG16(DX), 5);
                   5058:                        REG8(AL) = sio_read(REG16(DX), 6);
                   5059:                } else if(REG8(AL) == 0x01) {
                   5060:                        sio_write(REG16(DX), 4, REG8(BL));
                   5061:                        REG8(AH) = sio_read(REG16(DX), 5);
                   5062:                        REG8(AL) = sio_read(REG16(DX), 6);
                   5063:                } else {
                   5064:                        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));
                   5065:                }
                   5066:        } else {
                   5067:                REG8(AH) = 0x80;
                   5068:        }
                   5069: }
                   5070: 
1.1.1.14  root     5071: inline void pcbios_int_15h_10h()
                   5072: {
1.1.1.22  root     5073:        switch(REG8(AL)) {
                   5074:        case 0x00:
1.1.1.14  root     5075:                Sleep(10);
                   5076:                hardware_update();
1.1.1.22  root     5077:                break;
                   5078:        default:
                   5079:                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     5080:                REG8(AH) = 0x86;
                   5081:                m_CF = 1;
                   5082:        }
                   5083: }
                   5084: 
1.1       root     5085: inline void pcbios_int_15h_23h()
                   5086: {
                   5087:        switch(REG8(AL)) {
1.1.1.22  root     5088:        case 0x00:
1.1.1.8   root     5089:                REG8(CL) = cmos_read(0x2d);
                   5090:                REG8(CH) = cmos_read(0x2e);
1.1       root     5091:                break;
1.1.1.22  root     5092:        case 0x01:
1.1.1.8   root     5093:                cmos_write(0x2d, REG8(CL));
                   5094:                cmos_write(0x2e, REG8(CH));
1.1       root     5095:                break;
                   5096:        default:
1.1.1.22  root     5097:                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     5098:                REG8(AH) = 0x86;
1.1.1.3   root     5099:                m_CF = 1;
1.1       root     5100:                break;
                   5101:        }
                   5102: }
                   5103: 
                   5104: inline void pcbios_int_15h_24h()
                   5105: {
                   5106:        switch(REG8(AL)) {
1.1.1.22  root     5107:        case 0x00:
1.1.1.3   root     5108:                i386_set_a20_line(0);
1.1       root     5109:                REG8(AH) = 0;
                   5110:                break;
1.1.1.22  root     5111:        case 0x01:
1.1.1.3   root     5112:                i386_set_a20_line(1);
1.1       root     5113:                REG8(AH) = 0;
                   5114:                break;
1.1.1.22  root     5115:        case 0x02:
1.1       root     5116:                REG8(AH) = 0;
1.1.1.3   root     5117:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     5118:                REG16(CX) = 0;
                   5119:                break;
1.1.1.22  root     5120:        case 0x03:
1.1       root     5121:                REG16(AX) = 0;
                   5122:                REG16(BX) = 0;
                   5123:                break;
1.1.1.22  root     5124:        default:
                   5125:                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));
                   5126:                REG8(AH) = 0x86;
                   5127:                m_CF = 1;
                   5128:                break;
1.1       root     5129:        }
                   5130: }
                   5131: 
                   5132: inline void pcbios_int_15h_49h()
                   5133: {
1.1.1.27  root     5134:        REG8(AH) = 0x00;
                   5135:        REG8(BL) = 0x00; // DOS/V
1.1       root     5136: }
                   5137: 
1.1.1.22  root     5138: inline void pcbios_int_15h_50h()
                   5139: {
                   5140:        switch(REG8(AL)) {
                   5141:        case 0x00:
                   5142:        case 0x01:
                   5143:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   5144:                        REG8(AH) = 0x01; // invalid font type in bh
                   5145:                        m_CF = 1;
1.1.1.27  root     5146:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     5147:                        REG8(AH) = 0x02; // bl not zero
                   5148:                        m_CF = 1;
                   5149:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   5150:                        REG8(AH) = 0x04; // invalid code page
                   5151:                        m_CF = 1;
1.1.1.27  root     5152:                } else if(REG8(AL) == 0x01) {
                   5153:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     5154:                        m_CF = 1;
1.1.1.27  root     5155:                } else {
                   5156:                        // dummy font read routine is at fffd:000d
                   5157:                        SREG(ES) = 0xfffd;
                   5158:                        i386_load_segment_descriptor(ES);
                   5159:                        REG16(BX) = 0x0d;
                   5160:                        REG8(AH) = 0x00; // success
1.1.1.22  root     5161:                }
                   5162:                break;
                   5163:        default:
                   5164:                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));
                   5165:                REG8(AH) = 0x86;
                   5166:                m_CF = 1;
                   5167:                break;
                   5168:        }
                   5169: }
                   5170: 
1.1.1.30! root     5171: inline void pcbios_int_15h_53h()
        !          5172: {
        !          5173:        switch(REG8(AL)) {
        !          5174:        case 0x00:
        !          5175:                // APM is not installed
        !          5176:                REG8(AH) = 0x86;
        !          5177:                m_CF = 1;
        !          5178:                break;
        !          5179:        default:
        !          5180:                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));
        !          5181:                REG8(AH) = 0x86;
        !          5182:                m_CF = 1;
        !          5183:                break;
        !          5184:        }
        !          5185: }
        !          5186: 
1.1       root     5187: inline void pcbios_int_15h_86h()
                   5188: {
                   5189:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     5190:        UINT32 msec = usec / 1000;
                   5191:        
                   5192:        while(msec) {
                   5193:                UINT32 tmp = min(msec, 100);
                   5194:                if(msec - tmp < 10) {
                   5195:                        tmp = msec;
                   5196:                }
                   5197:                Sleep(tmp);
                   5198:                
                   5199:                if(m_halted) {
                   5200:                        return;
                   5201:                }
                   5202:                msec -= tmp;
                   5203:        }
1.1       root     5204: }
                   5205: 
                   5206: inline void pcbios_int_15h_87h()
                   5207: {
                   5208:        // copy extended memory (from DOSBox)
                   5209:        int len = REG16(CX) * 2;
1.1.1.3   root     5210:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     5211:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   5212:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   5213:        memcpy(mem + dst, mem + src, len);
                   5214:        REG16(AX) = 0x00;
                   5215: }
                   5216: 
                   5217: inline void pcbios_int_15h_88h()
                   5218: {
1.1.1.17  root     5219:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     5220: }
                   5221: 
                   5222: inline void pcbios_int_15h_89h()
                   5223: {
1.1.1.21  root     5224: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     5225:        // switch to protected mode (from DOSBox)
                   5226:        write_io_byte(0x20, 0x10);
                   5227:        write_io_byte(0x21, REG8(BH));
                   5228:        write_io_byte(0x21, 0x00);
                   5229:        write_io_byte(0xa0, 0x10);
                   5230:        write_io_byte(0xa1, REG8(BL));
                   5231:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     5232:        i386_set_a20_line(1);
                   5233:        int ofs = SREG_BASE(ES) + REG16(SI);
                   5234:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   5235:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   5236:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   5237:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   5238: #if defined(HAS_I386)
                   5239:        m_cr[0] |= 1;
                   5240: #else
                   5241:        m_msw |= 1;
                   5242: #endif
                   5243:        SREG(DS) = 0x18;
                   5244:        SREG(ES) = 0x20;
                   5245:        SREG(SS) = 0x28;
                   5246:        i386_load_segment_descriptor(DS);
                   5247:        i386_load_segment_descriptor(ES);
                   5248:        i386_load_segment_descriptor(SS);
1.1.1.21  root     5249:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     5250:        REG16(SP) += 6;
1.1.1.3   root     5251: #if defined(HAS_I386)
1.1.1.21  root     5252:        UINT32 flags = get_flags();
                   5253:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   5254:        set_flags(flags);
1.1.1.3   root     5255: #else
1.1.1.21  root     5256:        UINT32 flags = CompressFlags();
                   5257:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   5258:        ExpandFlags(flags);
1.1.1.3   root     5259: #endif
1.1       root     5260:        REG16(AX) = 0x00;
1.1.1.21  root     5261:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     5262: #else
1.1.1.21  root     5263:        // i86/i186/v30: protected mode is not supported
1.1       root     5264:        REG8(AH) = 0x86;
1.1.1.3   root     5265:        m_CF = 1;
1.1       root     5266: #endif
                   5267: }
                   5268: 
1.1.1.21  root     5269: inline void pcbios_int_15h_8ah()
                   5270: {
                   5271:        UINT32 size = MAX_MEM - 0x100000;
                   5272:        REG16(AX) = size & 0xffff;
                   5273:        REG16(DX) = size >> 16;
                   5274: }
                   5275: 
1.1.1.3   root     5276: #if defined(HAS_I386)
1.1       root     5277: inline void pcbios_int_15h_c9h()
                   5278: {
                   5279:        REG8(AH) = 0x00;
                   5280:        REG8(CH) = cpu_type;
                   5281:        REG8(CL) = cpu_step;
                   5282: }
1.1.1.3   root     5283: #endif
1.1       root     5284: 
                   5285: inline void pcbios_int_15h_cah()
                   5286: {
                   5287:        switch(REG8(AL)) {
1.1.1.22  root     5288:        case 0x00:
1.1       root     5289:                if(REG8(BL) > 0x3f) {
                   5290:                        REG8(AH) = 0x03;
1.1.1.3   root     5291:                        m_CF = 1;
1.1       root     5292:                } else if(REG8(BL) < 0x0e) {
                   5293:                        REG8(AH) = 0x04;
1.1.1.3   root     5294:                        m_CF = 1;
1.1       root     5295:                } else {
1.1.1.8   root     5296:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     5297:                }
                   5298:                break;
1.1.1.22  root     5299:        case 0x01:
1.1       root     5300:                if(REG8(BL) > 0x3f) {
                   5301:                        REG8(AH) = 0x03;
1.1.1.3   root     5302:                        m_CF = 1;
1.1       root     5303:                } else if(REG8(BL) < 0x0e) {
                   5304:                        REG8(AH) = 0x04;
1.1.1.3   root     5305:                        m_CF = 1;
1.1       root     5306:                } else {
1.1.1.8   root     5307:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     5308:                }
                   5309:                break;
                   5310:        default:
1.1.1.22  root     5311:                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     5312:                REG8(AH) = 0x86;
1.1.1.3   root     5313:                m_CF = 1;
1.1       root     5314:                break;
                   5315:        }
                   5316: }
                   5317: 
1.1.1.22  root     5318: inline void pcbios_int_15h_e8h()
1.1.1.17  root     5319: {
1.1.1.22  root     5320:        switch(REG8(AL)) {
                   5321: #if defined(HAS_I386)
                   5322:        case 0x01:
                   5323:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   5324:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   5325:                break;
1.1.1.17  root     5326: #endif
1.1.1.22  root     5327:        default:
                   5328:                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));
                   5329:                REG8(AH) = 0x86;
                   5330:                m_CF = 1;
                   5331:                break;
                   5332:        }
                   5333: }
1.1.1.17  root     5334: 
1.1.1.16  root     5335: UINT32 pcbios_get_key_code(bool clear_buffer)
1.1       root     5336: {
                   5337:        UINT32 code = 0;
                   5338:        
                   5339:        if(key_buf_char->count() == 0) {
1.1.1.14  root     5340:                if(!update_key_buffer()) {
                   5341:                        if(clear_buffer) {
                   5342:                                Sleep(10);
                   5343:                        } else {
                   5344:                                maybe_idle();
                   5345:                        }
                   5346:                }
1.1       root     5347:        }
                   5348:        if(!clear_buffer) {
                   5349:                key_buf_char->store_buffer();
                   5350:                key_buf_scan->store_buffer();
                   5351:        }
                   5352:        if(key_buf_char->count() != 0) {
                   5353:                code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   5354:        }
                   5355:        if(key_buf_char->count() != 0) {
                   5356:                code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   5357:        }
                   5358:        if(!clear_buffer) {
                   5359:                key_buf_char->restore_buffer();
                   5360:                key_buf_scan->restore_buffer();
                   5361:        }
                   5362:        return code;
                   5363: }
                   5364: 
                   5365: inline void pcbios_int_16h_00h()
                   5366: {
1.1.1.14  root     5367:        while(key_code == 0 && !m_halted) {
1.1.1.16  root     5368:                key_code = pcbios_get_key_code(true);
1.1       root     5369:        }
                   5370:        if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   5371:                if(REG8(AH) == 0x10) {
                   5372:                        key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   5373:                } else {
                   5374:                        key_code = ((key_code >> 16) & 0xff00);
                   5375:                }
                   5376:        }
                   5377:        REG16(AX) = key_code & 0xffff;
                   5378:        key_code >>= 16;
                   5379: }
                   5380: 
                   5381: inline void pcbios_int_16h_01h()
                   5382: {
1.1.1.5   root     5383:        UINT32 key_code_tmp = key_code;
1.1       root     5384:        
1.1.1.5   root     5385:        if(key_code_tmp == 0) {
1.1.1.16  root     5386:                key_code_tmp = pcbios_get_key_code(false);
1.1.1.5   root     5387:        }
1.1.1.14  root     5388:        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   5389:                if(REG8(AH) == 0x11) {
                   5390:                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   5391:                } else {
                   5392:                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1       root     5393:                }
                   5394:        }
1.1.1.5   root     5395:        if(key_code_tmp != 0) {
                   5396:                REG16(AX) = key_code_tmp & 0xffff;
1.1       root     5397:        }
1.1.1.3   root     5398: #if defined(HAS_I386)
1.1.1.5   root     5399:        m_ZF = (key_code_tmp == 0);
1.1.1.3   root     5400: #else
1.1.1.5   root     5401:        m_ZeroVal = (key_code_tmp != 0);
1.1.1.3   root     5402: #endif
1.1       root     5403: }
                   5404: 
                   5405: inline void pcbios_int_16h_02h()
                   5406: {
                   5407:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   5408:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   5409:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   5410:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   5411:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   5412:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   5413:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   5414:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   5415: }
                   5416: 
                   5417: inline void pcbios_int_16h_03h()
                   5418: {
                   5419:        static UINT16 status = 0;
                   5420:        
                   5421:        switch(REG8(AL)) {
                   5422:        case 0x05:
                   5423:                status = REG16(BX);
                   5424:                break;
                   5425:        case 0x06:
                   5426:                REG16(BX) = status;
                   5427:                break;
                   5428:        default:
1.1.1.3   root     5429:                m_CF = 1;
1.1       root     5430:                break;
                   5431:        }
                   5432: }
                   5433: 
                   5434: inline void pcbios_int_16h_05h()
                   5435: {
1.1.1.14  root     5436:        key_buf_char->write(REG8(CL));
                   5437:        key_buf_scan->write(REG8(CH));
1.1       root     5438:        REG8(AL) = 0x00;
                   5439: }
                   5440: 
                   5441: inline void pcbios_int_16h_12h()
                   5442: {
                   5443:        pcbios_int_16h_02h();
                   5444:        
                   5445:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   5446:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   5447:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   5448:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   5449:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   5450:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   5451:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   5452:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   5453: }
                   5454: 
                   5455: inline void pcbios_int_16h_13h()
                   5456: {
                   5457:        static UINT16 status = 0;
                   5458:        
                   5459:        switch(REG8(AL)) {
                   5460:        case 0x00:
                   5461:                status = REG16(DX);
                   5462:                break;
                   5463:        case 0x01:
                   5464:                REG16(DX) = status;
                   5465:                break;
                   5466:        default:
1.1.1.22  root     5467:                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     5468:                m_CF = 1;
1.1       root     5469:                break;
                   5470:        }
                   5471: }
                   5472: 
                   5473: inline void pcbios_int_16h_14h()
                   5474: {
                   5475:        static UINT8 status = 0;
                   5476:        
                   5477:        switch(REG8(AL)) {
                   5478:        case 0x00:
                   5479:        case 0x01:
                   5480:                status = REG8(AL);
                   5481:                break;
                   5482:        case 0x02:
                   5483:                REG8(AL) = status;
                   5484:                break;
                   5485:        default:
1.1.1.22  root     5486:                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     5487:                m_CF = 1;
1.1       root     5488:                break;
                   5489:        }
                   5490: }
                   5491: 
1.1.1.24  root     5492: inline void pcbios_int_16h_55h()
                   5493: {
                   5494:        switch(REG8(AL)) {
                   5495:        case 0x00:
                   5496:                // keyboard tsr is not present
                   5497:                break;
                   5498:        case 0xfe:
                   5499:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   5500:                break;
                   5501:        case 0xff:
                   5502:                break;
                   5503:        default:
                   5504:                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));
                   5505:                m_CF = 1;
                   5506:                break;
                   5507:        }
                   5508: }
                   5509: 
1.1.1.30! root     5510: inline void pcbios_int_16h_6fh()
        !          5511: {
        !          5512:        switch(REG8(AL)) {
        !          5513:        case 0x00:
        !          5514:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
        !          5515:                break;
        !          5516:        default:
        !          5517:                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));
        !          5518:                m_CF = 1;
        !          5519:                break;
        !          5520:        }
        !          5521: }
        !          5522: 
1.1       root     5523: inline void pcbios_int_1ah_00h()
                   5524: {
1.1.1.19  root     5525:        pcbios_update_daily_timer_counter(timeGetTime());
                   5526:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   5527:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   5528:        REG8(AL) = mem[0x470];
                   5529:        mem[0x470] = 0;
1.1       root     5530: }
                   5531: 
                   5532: inline int to_bcd(int t)
                   5533: {
                   5534:        int u = (t % 100) / 10;
                   5535:        return (u << 4) | (t % 10);
                   5536: }
                   5537: 
                   5538: inline void pcbios_int_1ah_02h()
                   5539: {
                   5540:        SYSTEMTIME time;
                   5541:        
                   5542:        GetLocalTime(&time);
                   5543:        REG8(CH) = to_bcd(time.wHour);
                   5544:        REG8(CL) = to_bcd(time.wMinute);
                   5545:        REG8(DH) = to_bcd(time.wSecond);
                   5546:        REG8(DL) = 0x00;
                   5547: }
                   5548: 
                   5549: inline void pcbios_int_1ah_04h()
                   5550: {
                   5551:        SYSTEMTIME time;
                   5552:        
                   5553:        GetLocalTime(&time);
                   5554:        REG8(CH) = to_bcd(time.wYear / 100);
                   5555:        REG8(CL) = to_bcd(time.wYear);
                   5556:        REG8(DH) = to_bcd(time.wMonth);
                   5557:        REG8(DL) = to_bcd(time.wDay);
                   5558: }
                   5559: 
                   5560: inline void pcbios_int_1ah_0ah()
                   5561: {
                   5562:        SYSTEMTIME time;
                   5563:        FILETIME file_time;
                   5564:        WORD dos_date, dos_time;
                   5565:        
                   5566:        GetLocalTime(&time);
                   5567:        SystemTimeToFileTime(&time, &file_time);
                   5568:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   5569:        REG16(CX) = dos_date;
                   5570: }
                   5571: 
                   5572: // msdos system call
                   5573: 
                   5574: inline void msdos_int_21h_00h()
                   5575: {
1.1.1.3   root     5576:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     5577: }
                   5578: 
                   5579: inline void msdos_int_21h_01h()
                   5580: {
                   5581:        REG8(AL) = msdos_getche();
1.1.1.26  root     5582:        ctrl_c_detected = ctrl_c_pressed;
                   5583:        
1.1.1.8   root     5584:        // some seconds may be passed in console
1.1       root     5585:        hardware_update();
                   5586: }
                   5587: 
                   5588: inline void msdos_int_21h_02h()
                   5589: {
                   5590:        msdos_putch(REG8(DL));
1.1.1.26  root     5591:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5592: }
                   5593: 
                   5594: inline void msdos_int_21h_03h()
                   5595: {
                   5596:        REG8(AL) = msdos_aux_in();
                   5597: }
                   5598: 
                   5599: inline void msdos_int_21h_04h()
                   5600: {
                   5601:        msdos_aux_out(REG8(DL));
                   5602: }
                   5603: 
                   5604: inline void msdos_int_21h_05h()
                   5605: {
                   5606:        msdos_prn_out(REG8(DL));
                   5607: }
                   5608: 
                   5609: inline void msdos_int_21h_06h()
                   5610: {
                   5611:        if(REG8(DL) == 0xff) {
                   5612:                if(msdos_kbhit()) {
                   5613:                        REG8(AL) = msdos_getch();
1.1.1.3   root     5614: #if defined(HAS_I386)
                   5615:                        m_ZF = 0;
                   5616: #else
                   5617:                        m_ZeroVal = 1;
                   5618: #endif
1.1       root     5619:                } else {
                   5620:                        REG8(AL) = 0;
1.1.1.3   root     5621: #if defined(HAS_I386)
                   5622:                        m_ZF = 1;
                   5623: #else
                   5624:                        m_ZeroVal = 0;
                   5625: #endif
1.1.1.14  root     5626:                        maybe_idle();
1.1       root     5627:                }
                   5628:        } else {
                   5629:                msdos_putch(REG8(DL));
                   5630:        }
                   5631: }
                   5632: 
                   5633: inline void msdos_int_21h_07h()
                   5634: {
                   5635:        REG8(AL) = msdos_getch();
1.1.1.26  root     5636:        
1.1.1.8   root     5637:        // some seconds may be passed in console
1.1       root     5638:        hardware_update();
                   5639: }
                   5640: 
                   5641: inline void msdos_int_21h_08h()
                   5642: {
                   5643:        REG8(AL) = msdos_getch();
1.1.1.26  root     5644:        ctrl_c_detected = ctrl_c_pressed;
                   5645:        
1.1.1.8   root     5646:        // some seconds may be passed in console
1.1       root     5647:        hardware_update();
                   5648: }
                   5649: 
                   5650: inline void msdos_int_21h_09h()
                   5651: {
1.1.1.21  root     5652:        msdos_stdio_reopen();
                   5653:        
1.1.1.20  root     5654:        process_t *process = msdos_process_info_get(current_psp);
                   5655:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5656:        
1.1.1.14  root     5657:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   5658:        int len = 0;
1.1       root     5659:        
1.1.1.14  root     5660:        while(str[len] != '$' && len < 0x10000) {
                   5661:                len++;
                   5662:        }
1.1.1.20  root     5663:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5664:                // stdout is redirected to file
1.1.1.20  root     5665:                msdos_write(fd, str, len);
1.1       root     5666:        } else {
                   5667:                for(int i = 0; i < len; i++) {
1.1.1.14  root     5668:                        msdos_putch(str[i]);
1.1       root     5669:                }
                   5670:        }
1.1.1.26  root     5671:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5672: }
                   5673: 
                   5674: inline void msdos_int_21h_0ah()
                   5675: {
1.1.1.3   root     5676:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     5677:        int max = mem[ofs] - 1;
                   5678:        UINT8 *buf = mem + ofs + 2;
                   5679:        int chr, p = 0;
                   5680:        
                   5681:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.26  root     5682:                if(ctrl_c_pressed) {
                   5683:                        p = 0;
                   5684:                        msdos_putch(chr);
                   5685:                        break;
                   5686:                } else if(chr == 0x00) {
1.1       root     5687:                        // skip 2nd byte
                   5688:                        msdos_getch();
                   5689:                } else if(chr == 0x08) {
                   5690:                        // back space
                   5691:                        if(p > 0) {
                   5692:                                p--;
1.1.1.20  root     5693:                                if(msdos_ctrl_code_check(buf[p])) {
                   5694:                                        msdos_putch(chr);
                   5695:                                        msdos_putch(chr);
                   5696:                                        msdos_putch(' ');
                   5697:                                        msdos_putch(' ');
                   5698:                                        msdos_putch(chr);
                   5699:                                        msdos_putch(chr);
                   5700:                                } else {
                   5701:                                        msdos_putch(chr);
                   5702:                                        msdos_putch(' ');
                   5703:                                        msdos_putch(chr);
                   5704:                                }
1.1       root     5705:                        }
                   5706:                } else if(p < max) {
                   5707:                        buf[p++] = chr;
                   5708:                        msdos_putch(chr);
                   5709:                }
                   5710:        }
                   5711:        buf[p] = 0x0d;
                   5712:        mem[ofs + 1] = p;
1.1.1.26  root     5713:        ctrl_c_detected = ctrl_c_pressed;
                   5714:        
1.1.1.8   root     5715:        // some seconds may be passed in console
1.1       root     5716:        hardware_update();
                   5717: }
                   5718: 
                   5719: inline void msdos_int_21h_0bh()
                   5720: {
                   5721:        if(msdos_kbhit()) {
                   5722:                REG8(AL) = 0xff;
                   5723:        } else {
                   5724:                REG8(AL) = 0x00;
1.1.1.14  root     5725:                maybe_idle();
1.1       root     5726:        }
1.1.1.26  root     5727:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5728: }
                   5729: 
                   5730: inline void msdos_int_21h_0ch()
                   5731: {
                   5732:        // clear key buffer
1.1.1.21  root     5733:        msdos_stdio_reopen();
                   5734:        
1.1.1.20  root     5735:        process_t *process = msdos_process_info_get(current_psp);
                   5736:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5737:        
                   5738:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5739:                // stdin is redirected to file
                   5740:        } else {
                   5741:                while(msdos_kbhit()) {
                   5742:                        msdos_getch();
                   5743:                }
                   5744:        }
                   5745:        
                   5746:        switch(REG8(AL)) {
                   5747:        case 0x01:
                   5748:                msdos_int_21h_01h();
                   5749:                break;
                   5750:        case 0x06:
                   5751:                msdos_int_21h_06h();
                   5752:                break;
                   5753:        case 0x07:
                   5754:                msdos_int_21h_07h();
                   5755:                break;
                   5756:        case 0x08:
                   5757:                msdos_int_21h_08h();
                   5758:                break;
                   5759:        case 0x0a:
                   5760:                msdos_int_21h_0ah();
                   5761:                break;
                   5762:        default:
1.1.1.22  root     5763: //             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));
                   5764: //             REG16(AX) = 0x01;
                   5765: //             m_CF = 1;
1.1       root     5766:                break;
                   5767:        }
                   5768: }
                   5769: 
                   5770: inline void msdos_int_21h_0dh()
                   5771: {
                   5772: }
                   5773: 
                   5774: inline void msdos_int_21h_0eh()
                   5775: {
                   5776:        if(REG8(DL) < 26) {
                   5777:                _chdrive(REG8(DL) + 1);
                   5778:                msdos_cds_update(REG8(DL));
1.1.1.23  root     5779:                msdos_sda_update(current_psp);
1.1       root     5780:        }
                   5781:        REG8(AL) = 26; // zdrive
                   5782: }
                   5783: 
1.1.1.14  root     5784: inline void msdos_int_21h_0fh()
                   5785: {
                   5786:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5787:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5788:        char *path = msdos_fcb_path(fcb);
                   5789:        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     5790:        
1.1.1.14  root     5791:        if(hFile == INVALID_HANDLE_VALUE) {
                   5792:                REG8(AL) = 0xff;
                   5793:        } else {
                   5794:                REG8(AL) = 0;
                   5795:                fcb->current_block = 0;
                   5796:                fcb->record_size = 128;
                   5797:                fcb->file_size = GetFileSize(hFile, NULL);
                   5798:                fcb->handle = hFile;
                   5799:                fcb->cur_record = 0;
                   5800:        }
                   5801: }
                   5802: 
                   5803: inline void msdos_int_21h_10h()
                   5804: {
                   5805:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5806:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5807:        
                   5808:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   5809: }
                   5810: 
1.1       root     5811: inline void msdos_int_21h_11h()
                   5812: {
1.1.1.3   root     5813:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5814:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5815:        
                   5816:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5817:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5818:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5819:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5820:        char *path = msdos_fcb_path(fcb);
                   5821:        WIN32_FIND_DATA fd;
                   5822:        
1.1.1.13  root     5823:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5824:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5825:                FindClose(dtainfo->find_handle);
                   5826:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5827:        }
                   5828:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     5829:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   5830:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     5831:        
1.1.1.14  root     5832:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   5833:                dtainfo->allowable_mask &= ~8;
1.1       root     5834:        }
1.1.1.14  root     5835:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   5836:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5837:                      !msdos_find_file_has_8dot3name(&fd)) {
                   5838:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5839:                                FindClose(dtainfo->find_handle);
                   5840:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5841:                                break;
                   5842:                        }
                   5843:                }
                   5844:        }
1.1.1.13  root     5845:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5846:                if(ext_fcb->flag == 0xff) {
                   5847:                        ext_find->flag = 0xff;
                   5848:                        memset(ext_find->reserved, 0, 5);
                   5849:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5850:                }
                   5851:                find->drive = _getdrive();
1.1.1.13  root     5852:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5853:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5854:                find->nt_res = 0;
                   5855:                msdos_find_file_conv_local_time(&fd);
                   5856:                find->create_time_ms = 0;
                   5857:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5858:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5859:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5860:                find->cluster_hi = find->cluster_lo = 0;
                   5861:                find->file_size = fd.nFileSizeLow;
                   5862:                REG8(AL) = 0x00;
1.1.1.14  root     5863:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5864:                if(ext_fcb->flag == 0xff) {
                   5865:                        ext_find->flag = 0xff;
                   5866:                        memset(ext_find->reserved, 0, 5);
                   5867:                        ext_find->attribute = 8;
                   5868:                }
                   5869:                find->drive = _getdrive();
                   5870:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5871:                find->attribute = 8;
                   5872:                find->nt_res = 0;
                   5873:                msdos_find_file_conv_local_time(&fd);
                   5874:                find->create_time_ms = 0;
                   5875:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5876:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5877:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5878:                find->cluster_hi = find->cluster_lo = 0;
                   5879:                find->file_size = 0;
1.1.1.14  root     5880:                dtainfo->allowable_mask &= ~8;
1.1       root     5881:                REG8(AL) = 0x00;
                   5882:        } else {
                   5883:                REG8(AL) = 0xff;
                   5884:        }
                   5885: }
                   5886: 
                   5887: inline void msdos_int_21h_12h()
                   5888: {
1.1.1.3   root     5889:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     5890: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5891:        
                   5892:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5893:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5894:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5895:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5896:        WIN32_FIND_DATA fd;
                   5897:        
1.1.1.13  root     5898:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5899:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5900:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     5901:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5902:                              !msdos_find_file_has_8dot3name(&fd)) {
                   5903:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5904:                                        FindClose(dtainfo->find_handle);
                   5905:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5906:                                        break;
                   5907:                                }
                   5908:                        }
                   5909:                } else {
1.1.1.13  root     5910:                        FindClose(dtainfo->find_handle);
                   5911:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5912:                }
                   5913:        }
1.1.1.13  root     5914:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5915:                if(ext_fcb->flag == 0xff) {
                   5916:                        ext_find->flag = 0xff;
                   5917:                        memset(ext_find->reserved, 0, 5);
                   5918:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5919:                }
                   5920:                find->drive = _getdrive();
1.1.1.13  root     5921:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5922:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5923:                find->nt_res = 0;
                   5924:                msdos_find_file_conv_local_time(&fd);
                   5925:                find->create_time_ms = 0;
                   5926:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5927:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5928:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5929:                find->cluster_hi = find->cluster_lo = 0;
                   5930:                find->file_size = fd.nFileSizeLow;
                   5931:                REG8(AL) = 0x00;
1.1.1.14  root     5932:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5933:                if(ext_fcb->flag == 0xff) {
                   5934:                        ext_find->flag = 0xff;
                   5935:                        memset(ext_find->reserved, 0, 5);
                   5936:                        ext_find->attribute = 8;
                   5937:                }
                   5938:                find->drive = _getdrive();
                   5939:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5940:                find->attribute = 8;
                   5941:                find->nt_res = 0;
                   5942:                msdos_find_file_conv_local_time(&fd);
                   5943:                find->create_time_ms = 0;
                   5944:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5945:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5946:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5947:                find->cluster_hi = find->cluster_lo = 0;
                   5948:                find->file_size = 0;
1.1.1.14  root     5949:                dtainfo->allowable_mask &= ~8;
1.1       root     5950:                REG8(AL) = 0x00;
                   5951:        } else {
                   5952:                REG8(AL) = 0xff;
                   5953:        }
                   5954: }
                   5955: 
                   5956: inline void msdos_int_21h_13h()
                   5957: {
1.1.1.3   root     5958:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     5959:                REG8(AL) = 0xff;
                   5960:        } else {
                   5961:                REG8(AL) = 0x00;
                   5962:        }
                   5963: }
                   5964: 
1.1.1.16  root     5965: inline void msdos_int_21h_14h()
                   5966: {
                   5967:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5968:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5969:        process_t *process = msdos_process_info_get(current_psp);
                   5970:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5971:        DWORD num = 0;
                   5972:        
                   5973:        memset(mem + dta_laddr, 0, fcb->record_size);
                   5974:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5975:                REG8(AL) = 1;
                   5976:        } else {
                   5977:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5978:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5979:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5980:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5981:        }
                   5982: }
                   5983: 
                   5984: inline void msdos_int_21h_15h()
1.1.1.14  root     5985: {
                   5986:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5987:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     5988:        process_t *process = msdos_process_info_get(current_psp);
                   5989:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5990:        DWORD num = 0;
1.1.1.14  root     5991:        
1.1.1.16  root     5992:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5993:                REG8(AL) = 1;
                   5994:        } else {
                   5995:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5996:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5997:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5998:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5999:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   6000:        }
                   6001: }
                   6002: 
                   6003: inline void msdos_int_21h_16h()
                   6004: {
                   6005:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6006:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     6007:        char *path = msdos_fcb_path(fcb);
                   6008:        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     6009:        
1.1.1.14  root     6010:        if(hFile == INVALID_HANDLE_VALUE) {
                   6011:                REG8(AL) = 0xff;
                   6012:        } else {
                   6013:                REG8(AL) = 0;
                   6014:                fcb->current_block = 0;
                   6015:                fcb->record_size = 128;
                   6016:                fcb->file_size = 0;
                   6017:                fcb->handle = hFile;
                   6018:                fcb->cur_record = 0;
                   6019:        }
                   6020: }
                   6021: 
1.1.1.16  root     6022: inline void msdos_int_21h_17h()
                   6023: {
                   6024:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6025:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   6026:        char *path_src = msdos_fcb_path(fcb_src);
                   6027:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   6028:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   6029:        char *path_dst = msdos_fcb_path(fcb_dst);
                   6030:        
                   6031:        if(rename(path_src, path_dst)) {
                   6032:                REG8(AL) = 0xff;
                   6033:        } else {
                   6034:                REG8(AL) = 0;
                   6035:        }
                   6036: }
                   6037: 
1.1       root     6038: inline void msdos_int_21h_18h()
                   6039: {
                   6040:        REG8(AL) = 0x00;
                   6041: }
                   6042: 
                   6043: inline void msdos_int_21h_19h()
                   6044: {
                   6045:        REG8(AL) = _getdrive() - 1;
                   6046: }
                   6047: 
                   6048: inline void msdos_int_21h_1ah()
                   6049: {
                   6050:        process_t *process = msdos_process_info_get(current_psp);
                   6051:        
                   6052:        process->dta.w.l = REG16(DX);
1.1.1.3   root     6053:        process->dta.w.h = SREG(DS);
1.1.1.23  root     6054:        msdos_sda_update(current_psp);
1.1       root     6055: }
                   6056: 
                   6057: inline void msdos_int_21h_1bh()
                   6058: {
                   6059:        int drive_num = _getdrive() - 1;
                   6060:        UINT16 seg, ofs;
                   6061:        
                   6062:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6063:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   6064:                REG8(AL) = dpb->highest_sector_num + 1;
                   6065:                REG16(CX) = dpb->bytes_per_sector;
                   6066:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     6067:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     6068:        } else {
                   6069:                REG8(AL) = 0xff;
1.1.1.3   root     6070:                m_CF = 1;
1.1       root     6071:        }
                   6072: 
                   6073: }
                   6074: 
                   6075: inline void msdos_int_21h_1ch()
                   6076: {
                   6077:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   6078:        UINT16 seg, ofs;
                   6079:        
                   6080:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6081:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   6082:                REG8(AL) = dpb->highest_sector_num + 1;
                   6083:                REG16(CX) = dpb->bytes_per_sector;
                   6084:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     6085:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     6086:        } else {
                   6087:                REG8(AL) = 0xff;
1.1.1.3   root     6088:                m_CF = 1;
1.1       root     6089:        }
                   6090: 
                   6091: }
                   6092: 
                   6093: inline void msdos_int_21h_1dh()
                   6094: {
                   6095:        REG8(AL) = 0;
                   6096: }
                   6097: 
                   6098: inline void msdos_int_21h_1eh()
                   6099: {
                   6100:        REG8(AL) = 0;
                   6101: }
                   6102: 
                   6103: inline void msdos_int_21h_1fh()
                   6104: {
                   6105:        int drive_num = _getdrive() - 1;
                   6106:        UINT16 seg, ofs;
                   6107:        
                   6108:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6109:                REG8(AL) = 0;
1.1.1.3   root     6110:                SREG(DS) = seg;
                   6111:                i386_load_segment_descriptor(DS);
1.1       root     6112:                REG16(BX) = ofs;
                   6113:        } else {
                   6114:                REG8(AL) = 0xff;
1.1.1.3   root     6115:                m_CF = 1;
1.1       root     6116:        }
                   6117: }
                   6118: 
                   6119: inline void msdos_int_21h_20h()
                   6120: {
                   6121:        REG8(AL) = 0;
                   6122: }
                   6123: 
1.1.1.14  root     6124: inline void msdos_int_21h_21h()
                   6125: {
                   6126:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6127:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6128:        
                   6129:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6130:                REG8(AL) = 1;
                   6131:        } else {
                   6132:                process_t *process = msdos_process_info_get(current_psp);
                   6133:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6134:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     6135:                DWORD num = 0;
1.1.1.14  root     6136:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   6137:                        REG8(AL) = 1;
                   6138:                } else {
                   6139:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6140:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     6141:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     6142:                }
                   6143:        }
                   6144: }
                   6145: 
                   6146: inline void msdos_int_21h_22h()
                   6147: {
                   6148:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6149:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6150:        
                   6151:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6152:                REG8(AL) = 0xff;
                   6153:        } else {
                   6154:                process_t *process = msdos_process_info_get(current_psp);
                   6155:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     6156:                DWORD num = 0;
1.1.1.14  root     6157:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   6158:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   6159:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6160:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     6161:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     6162:        }
                   6163: }
                   6164: 
1.1.1.16  root     6165: inline void msdos_int_21h_23h()
                   6166: {
                   6167:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6168:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6169:        char *path = msdos_fcb_path(fcb);
                   6170:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6171:        
                   6172:        if(hFile == INVALID_HANDLE_VALUE) {
                   6173:                REG8(AL) = 0xff;
                   6174:        } else {
                   6175:                UINT32 size = GetFileSize(hFile, NULL);
                   6176:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   6177:                REG8(AL) = 0;
                   6178:        }
                   6179: }
                   6180: 
                   6181: inline void msdos_int_21h_24h()
                   6182: {
                   6183:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6184:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6185:        
                   6186:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   6187: }
                   6188: 
1.1       root     6189: inline void msdos_int_21h_25h()
                   6190: {
                   6191:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     6192:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     6193: }
                   6194: 
                   6195: inline void msdos_int_21h_26h()
                   6196: {
                   6197:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   6198:        
                   6199:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   6200:        psp->first_mcb = REG16(DX) + 16;
                   6201:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6202:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6203:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6204:        psp->parent_psp = 0;
                   6205: }
                   6206: 
1.1.1.16  root     6207: inline void msdos_int_21h_27h()
                   6208: {
                   6209:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6210:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6211:        
                   6212:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6213:                REG8(AL) = 1;
                   6214:        } else {
                   6215:                process_t *process = msdos_process_info_get(current_psp);
                   6216:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6217:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   6218:                DWORD num = 0;
                   6219:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   6220:                        REG8(AL) = 1;
                   6221:                } else {
                   6222:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6223:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   6224:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   6225:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   6226:                }
                   6227:        }
                   6228: }
                   6229: 
                   6230: inline void msdos_int_21h_28h()
                   6231: {
                   6232:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   6233:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   6234:        
                   6235:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   6236:                REG8(AL) = 0xff;
                   6237:        } else {
                   6238:                process_t *process = msdos_process_info_get(current_psp);
                   6239:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6240:                DWORD num = 0;
                   6241:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   6242:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   6243:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   6244:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   6245:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   6246:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   6247:        }
                   6248: }
                   6249: 
1.1       root     6250: inline void msdos_int_21h_29h()
                   6251: {
1.1.1.20  root     6252:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   6253:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     6254:        UINT8 drv = 0;
                   6255:        char sep_chars[] = ":.;,=+";
                   6256:        char end_chars[] = "\\<>|/\"[]";
                   6257:        char spc_chars[] = " \t";
                   6258:        
1.1.1.20  root     6259:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   6260:        buffer[1023] = 0;
                   6261:        memset(name, 0x20, sizeof(name));
                   6262:        memset(ext, 0x20, sizeof(ext));
                   6263:        
1.1       root     6264:        if(REG8(AL) & 1) {
1.1.1.20  root     6265:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   6266:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     6267:                        ofs++;
                   6268:                }
                   6269:        }
1.1.1.20  root     6270:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     6271:        
1.1.1.24  root     6272:        if(buffer[ofs + 1] == ':') {
                   6273:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   6274:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     6275:                        ofs += 2;
1.1.1.24  root     6276:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   6277:                                ofs++;
                   6278:                        }
                   6279:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   6280:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     6281:                        ofs += 2;
1.1.1.24  root     6282:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   6283:                                ofs++;
                   6284:                        }
1.1       root     6285:                }
                   6286:        }
1.1.1.20  root     6287:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   6288:                UINT8 c = buffer[ofs];
                   6289:                if(is_kanji) {
                   6290:                        is_kanji = 0;
                   6291:                } else if(msdos_lead_byte_check(c)) {
                   6292:                        is_kanji = 1;
                   6293:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     6294:                        break;
                   6295:                } else if(c >= 'a' && c <= 'z') {
                   6296:                        c -= 0x20;
                   6297:                }
                   6298:                ofs++;
                   6299:                name[i] = c;
                   6300:        }
1.1.1.20  root     6301:        if(buffer[ofs] == '.') {
1.1       root     6302:                ofs++;
1.1.1.20  root     6303:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   6304:                        UINT8 c = buffer[ofs];
                   6305:                        if(is_kanji) {
                   6306:                                is_kanji = 0;
                   6307:                        } else if(msdos_lead_byte_check(c)) {
                   6308:                                is_kanji = 1;
                   6309:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     6310:                                break;
                   6311:                        } else if(c >= 'a' && c <= 'z') {
                   6312:                                c -= 0x20;
                   6313:                        }
                   6314:                        ofs++;
                   6315:                        ext[i] = c;
                   6316:                }
                   6317:        }
1.1.1.20  root     6318:        int si = REG16(SI) + ofs;
1.1.1.3   root     6319:        int ds = SREG(DS);
1.1       root     6320:        while(si > 0xffff) {
                   6321:                si -= 0x10;
                   6322:                ds++;
                   6323:        }
                   6324:        REG16(SI) = si;
1.1.1.3   root     6325:        SREG(DS) = ds;
                   6326:        i386_load_segment_descriptor(DS);
1.1       root     6327:        
1.1.1.3   root     6328:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     6329:        if(!(REG8(AL) & 2) || drv != 0) {
                   6330:                fcb[0] = drv;
                   6331:        }
                   6332:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   6333:                memcpy(fcb + 1, name, 8);
                   6334:        }
                   6335:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   6336:                memcpy(fcb + 9, ext, 3);
                   6337:        }
                   6338:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     6339:                if(fcb[i] == '*') {
                   6340:                        found_star = 1;
                   6341:                }
                   6342:                if(found_star) {
                   6343:                        fcb[i] = '?';
                   6344:                }
                   6345:        }
1.1.1.20  root     6346:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     6347:                if(fcb[i] == '*') {
                   6348:                        found_star = 1;
                   6349:                }
                   6350:                if(found_star) {
                   6351:                        fcb[i] = '?';
                   6352:                }
                   6353:        }
                   6354:        
                   6355:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   6356:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   6357:                        REG8(AL) = 0x01;
1.1.1.20  root     6358:                } else {
                   6359:                        REG8(AL) = 0x00;
1.1       root     6360:                }
                   6361:        } else {
                   6362:                REG8(AL) = 0xff;
                   6363:        }
                   6364: }
                   6365: 
                   6366: inline void msdos_int_21h_2ah()
                   6367: {
                   6368:        SYSTEMTIME sTime;
                   6369:        
                   6370:        GetLocalTime(&sTime);
                   6371:        REG16(CX) = sTime.wYear;
                   6372:        REG8(DH) = (UINT8)sTime.wMonth;
                   6373:        REG8(DL) = (UINT8)sTime.wDay;
                   6374:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   6375: }
                   6376: 
                   6377: inline void msdos_int_21h_2bh()
                   6378: {
1.1.1.14  root     6379:        REG8(AL) = 0xff;
1.1       root     6380: }
                   6381: 
                   6382: inline void msdos_int_21h_2ch()
                   6383: {
                   6384:        SYSTEMTIME sTime;
                   6385:        
                   6386:        GetLocalTime(&sTime);
                   6387:        REG8(CH) = (UINT8)sTime.wHour;
                   6388:        REG8(CL) = (UINT8)sTime.wMinute;
                   6389:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     6390:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     6391: }
                   6392: 
                   6393: inline void msdos_int_21h_2dh()
                   6394: {
                   6395:        REG8(AL) = 0x00;
                   6396: }
                   6397: 
                   6398: inline void msdos_int_21h_2eh()
                   6399: {
                   6400:        process_t *process = msdos_process_info_get(current_psp);
                   6401:        
                   6402:        process->verify = REG8(AL);
                   6403: }
                   6404: 
                   6405: inline void msdos_int_21h_2fh()
                   6406: {
                   6407:        process_t *process = msdos_process_info_get(current_psp);
                   6408:        
                   6409:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     6410:        SREG(ES) = process->dta.w.h;
                   6411:        i386_load_segment_descriptor(ES);
1.1       root     6412: }
                   6413: 
                   6414: inline void msdos_int_21h_30h()
                   6415: {
                   6416:        // Version Flag / OEM
1.1.1.27  root     6417:        if(REG8(AL) == 0x01) {
1.1.1.29  root     6418: #ifdef SUPPORT_HMA
                   6419:                REG16(BX) = 0x0000;
                   6420: #else
                   6421:                REG16(BX) = 0x1000; // DOS is in HMA
                   6422: #endif
1.1       root     6423:        } else {
1.1.1.27  root     6424:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   6425:                // but this is not correct on Windows 98 SE
                   6426: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   6427:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     6428:        }
1.1.1.27  root     6429:        REG16(CX) = 0x0000;
1.1.1.30! root     6430:        REG8(AL) = dos_major_version;   // 7
        !          6431:        REG8(AH) = dos_minor_version;   // 10
1.1       root     6432: }
                   6433: 
                   6434: inline void msdos_int_21h_31h()
                   6435: {
1.1.1.29  root     6436:        try {
                   6437:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   6438:        } catch(...) {
                   6439:                // recover the broken mcb
                   6440:                int mcb_seg = current_psp - 1;
                   6441:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6442:                if(mcb_seg < (MEMORY_END >> 4)) {
                   6443:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
                   6444:                                mcb->mz = 'M';
                   6445:                        } else {
                   6446:                                mcb->mz = 'Z';
                   6447:                        }
1.1.1.30! root     6448:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
1.1.1.29  root     6449:                        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   6450:                } else {
                   6451:                        mcb->mz = 'Z';
1.1.1.30! root     6452:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     6453:                }
                   6454:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   6455:        }
1.1       root     6456:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   6457: }
                   6458: 
                   6459: inline void msdos_int_21h_32h()
                   6460: {
                   6461:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   6462:        UINT16 seg, ofs;
                   6463:        
                   6464:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6465:                REG8(AL) = 0;
1.1.1.3   root     6466:                SREG(DS) = seg;
                   6467:                i386_load_segment_descriptor(DS);
1.1       root     6468:                REG16(BX) = ofs;
                   6469:        } else {
                   6470:                REG8(AL) = 0xff;
1.1.1.3   root     6471:                m_CF = 1;
1.1       root     6472:        }
                   6473: }
                   6474: 
                   6475: inline void msdos_int_21h_33h()
                   6476: {
                   6477:        char path[MAX_PATH];
                   6478:        
                   6479:        switch(REG8(AL)) {
                   6480:        case 0x00:
1.1.1.26  root     6481:                REG8(DL) = ctrl_c_checking;
1.1       root     6482:                break;
                   6483:        case 0x01:
1.1.1.26  root     6484:                ctrl_c_checking = REG8(DL);
1.1       root     6485:                break;
                   6486:        case 0x05:
                   6487:                GetSystemDirectory(path, MAX_PATH);
                   6488:                if(path[0] >= 'a' && path[0] <= 'z') {
                   6489:                        REG8(DL) = path[0] - 'a' + 1;
                   6490:                } else {
                   6491:                        REG8(DL) = path[0] - 'A' + 1;
                   6492:                }
                   6493:                break;
                   6494:        case 0x06:
1.1.1.2   root     6495:                // MS-DOS version (7.10)
1.1       root     6496:                REG8(BL) = 7;
1.1.1.2   root     6497:                REG8(BH) = 10;
1.1       root     6498:                REG8(DL) = 0;
1.1.1.29  root     6499: #ifdef SUPPORT_HMA
                   6500:                REG8(DH) = 0x00;
                   6501: #else
                   6502:                REG8(DH) = 0x10; // DOS is in HMA
                   6503: #endif
1.1       root     6504:                break;
1.1.1.6   root     6505:        case 0x07:
                   6506:                if(REG8(DL) == 0) {
                   6507:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   6508:                } else if(REG8(DL) == 1) {
                   6509:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   6510:                }
                   6511:                break;
1.1       root     6512:        default:
1.1.1.22  root     6513:                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     6514:                REG16(AX) = 0x01;
1.1.1.3   root     6515:                m_CF = 1;
1.1       root     6516:                break;
                   6517:        }
                   6518: }
                   6519: 
1.1.1.23  root     6520: inline void msdos_int_21h_34h()
                   6521: {
                   6522:        SREG(ES) = SDA_TOP >> 4;
                   6523:        i386_load_segment_descriptor(ES);
                   6524:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   6525: }
                   6526: 
1.1       root     6527: inline void msdos_int_21h_35h()
                   6528: {
                   6529:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     6530:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   6531:        i386_load_segment_descriptor(ES);
1.1       root     6532: }
                   6533: 
                   6534: inline void msdos_int_21h_36h()
                   6535: {
                   6536:        struct _diskfree_t df = {0};
                   6537:        
                   6538:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   6539:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   6540:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     6541:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   6542:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     6543:        } else {
                   6544:                REG16(AX) = 0xffff;
                   6545:        }
                   6546: }
                   6547: 
                   6548: inline void msdos_int_21h_37h()
                   6549: {
1.1.1.22  root     6550:        static UINT8 dev_flag = 0xff;
1.1       root     6551:        
                   6552:        switch(REG8(AL)) {
                   6553:        case 0x00:
1.1.1.22  root     6554:                {
                   6555:                        process_t *process = msdos_process_info_get(current_psp);
                   6556:                        REG8(AL) = 0x00;
                   6557:                        REG8(DL) = process->switchar;
                   6558:                }
1.1       root     6559:                break;
                   6560:        case 0x01:
1.1.1.22  root     6561:                {
                   6562:                        process_t *process = msdos_process_info_get(current_psp);
                   6563:                        REG8(AL) = 0x00;
                   6564:                        process->switchar = REG8(DL);
1.1.1.23  root     6565:                        msdos_sda_update(current_psp);
1.1.1.22  root     6566:                }
                   6567:                break;
                   6568:        case 0x02:
                   6569:                REG8(DL) = dev_flag;
                   6570:                break;
                   6571:        case 0x03:
                   6572:                dev_flag = REG8(DL);
                   6573:                break;
                   6574:        case 0xd0:
                   6575:        case 0xd1:
                   6576:        case 0xd2:
                   6577:        case 0xd3:
                   6578:        case 0xd4:
                   6579:        case 0xd5:
                   6580:        case 0xd6:
                   6581:        case 0xd7:
                   6582:        case 0xdc:
                   6583:        case 0xdd:
                   6584:        case 0xde:
                   6585:        case 0xdf:
                   6586:                // diet ???
                   6587:                REG16(AX) = 1;
1.1       root     6588:                break;
                   6589:        default:
1.1.1.22  root     6590:                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     6591:                REG16(AX) = 1;
                   6592:                break;
                   6593:        }
                   6594: }
                   6595: 
1.1.1.19  root     6596: int get_country_info(country_info_t *ci)
1.1.1.17  root     6597: {
                   6598:        char LCdata[80];
                   6599:        
1.1.1.19  root     6600:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     6601:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   6602:        ci->currency_dec_digits = atoi(LCdata);
                   6603:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   6604:        ci->currency_format = *LCdata - '0';
                   6605:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   6606:        ci->date_format = *LCdata - '0';
                   6607:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   6608:        memcpy(&ci->currency_symbol, LCdata, 4);
                   6609:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   6610:        *ci->date_sep = *LCdata;
                   6611:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   6612:        *ci->dec_sep = *LCdata;
                   6613:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   6614:        *ci->list_sep = *LCdata;
                   6615:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   6616:        *ci->thou_sep = *LCdata;
                   6617:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   6618:        *ci->time_sep = *LCdata;
                   6619:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   6620:        if(strchr(LCdata, 'H') != NULL) {
                   6621:                ci->time_format = 1;
                   6622:        }
1.1.1.27  root     6623:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     6624:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     6625:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   6626:        return atoi(LCdata);
                   6627: }
                   6628: 
1.1.1.14  root     6629: inline void msdos_int_21h_38h()
                   6630: {
                   6631:        switch(REG8(AL)) {
                   6632:        case 0x00:
1.1.1.19  root     6633:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     6634:                break;
                   6635:        default:
1.1.1.22  root     6636:                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     6637:                REG16(AX) = 2;
                   6638:                m_CF = 1;
                   6639:                break;
                   6640:        }
                   6641: }
                   6642: 
1.1       root     6643: inline void msdos_int_21h_39h(int lfn)
                   6644: {
1.1.1.3   root     6645:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6646:                REG16(AX) = errno;
1.1.1.3   root     6647:                m_CF = 1;
1.1       root     6648:        }
                   6649: }
                   6650: 
                   6651: inline void msdos_int_21h_3ah(int lfn)
                   6652: {
1.1.1.3   root     6653:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6654:                REG16(AX) = errno;
1.1.1.3   root     6655:                m_CF = 1;
1.1       root     6656:        }
                   6657: }
                   6658: 
                   6659: inline void msdos_int_21h_3bh(int lfn)
                   6660: {
1.1.1.3   root     6661:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     6662:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     6663:                m_CF = 1;
1.1       root     6664:        }
                   6665: }
                   6666: 
                   6667: inline void msdos_int_21h_3ch()
                   6668: {
1.1.1.3   root     6669:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6670:        int attr = GetFileAttributes(path);
1.1.1.29  root     6671:        int fd = -1, c;
1.1.1.11  root     6672:        UINT16 info;
1.1       root     6673:        
1.1.1.11  root     6674:        if(msdos_is_con_path(path)) {
                   6675:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   6676:                info = 0x80d3;
1.1.1.29  root     6677:        } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   6678:                if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), _O_WRONLY | _O_BINARY)) == -1) {
                   6679:                        fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6680:                }
1.1.1.14  root     6681:                info = 0x80d3;
1.1.1.29  root     6682:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     6683:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6684:                info = 0x80d3;
1.1       root     6685:        } else {
                   6686:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     6687:                info = msdos_drive_number(path);
1.1       root     6688:        }
                   6689:        if(fd != -1) {
                   6690:                if(attr == -1) {
                   6691:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   6692:                }
                   6693:                SetFileAttributes(path, attr);
                   6694:                REG16(AX) = fd;
1.1.1.11  root     6695:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     6696:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6697:        } else {
                   6698:                REG16(AX) = errno;
1.1.1.3   root     6699:                m_CF = 1;
1.1       root     6700:        }
                   6701: }
                   6702: 
                   6703: inline void msdos_int_21h_3dh()
                   6704: {
1.1.1.3   root     6705:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6706:        int mode = REG8(AL) & 0x03;
1.1.1.29  root     6707:        int fd = -1, c;
1.1.1.11  root     6708:        UINT16 info;
1.1       root     6709:        
                   6710:        if(mode < 0x03) {
1.1.1.11  root     6711:                if(msdos_is_con_path(path)) {
1.1.1.13  root     6712:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     6713:                        info = 0x80d3;
1.1.1.29  root     6714:                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   6715:                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   6716:                                fd = msdos_open("NUL", file_mode[mode].mode);
                   6717:                        }
1.1.1.14  root     6718:                        info = 0x80d3;
1.1.1.29  root     6719:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     6720:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   6721:                        info = 0x80d3;
1.1.1.11  root     6722:                } else {
1.1.1.13  root     6723:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     6724:                        info = msdos_drive_number(path);
                   6725:                }
1.1       root     6726:                if(fd != -1) {
                   6727:                        REG16(AX) = fd;
1.1.1.11  root     6728:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     6729:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6730:                } else {
                   6731:                        REG16(AX) = errno;
1.1.1.3   root     6732:                        m_CF = 1;
1.1       root     6733:                }
                   6734:        } else {
                   6735:                REG16(AX) = 0x0c;
1.1.1.3   root     6736:                m_CF = 1;
1.1       root     6737:        }
                   6738: }
                   6739: 
                   6740: inline void msdos_int_21h_3eh()
                   6741: {
                   6742:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6743:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6744:        
1.1.1.20  root     6745:        if(fd < process->max_files && file_handler[fd].valid) {
                   6746:                _close(fd);
                   6747:                msdos_file_handler_close(fd);
                   6748:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     6749:        } else {
                   6750:                REG16(AX) = 0x06;
1.1.1.3   root     6751:                m_CF = 1;
1.1       root     6752:        }
                   6753: }
                   6754: 
                   6755: inline void msdos_int_21h_3fh()
                   6756: {
                   6757:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6758:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6759:        
1.1.1.20  root     6760:        if(fd < process->max_files && file_handler[fd].valid) {
                   6761:                if(file_mode[file_handler[fd].mode].in) {
                   6762:                        if(file_handler[fd].atty) {
1.1       root     6763:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     6764:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     6765:                                int max = REG16(CX);
                   6766:                                int p = 0;
                   6767:                                
                   6768:                                while(max > p) {
                   6769:                                        int chr = msdos_getch();
                   6770:                                        
1.1.1.26  root     6771:                                        if(ctrl_c_pressed) {
                   6772:                                                p = 0;
                   6773:                                                buf[p++] = 0x0d;
                   6774:                                                if(max > p) {
                   6775:                                                        buf[p++] = 0x0a;
                   6776:                                                }
                   6777:                                                msdos_putch(chr);
                   6778:                                                msdos_putch('\n');
                   6779:                                                break;
                   6780:                                        } else if(chr == 0x00) {
1.1       root     6781:                                                // skip 2nd byte
                   6782:                                                msdos_getch();
                   6783:                                        } else if(chr == 0x0d) {
                   6784:                                                // carriage return
                   6785:                                                buf[p++] = 0x0d;
                   6786:                                                if(max > p) {
                   6787:                                                        buf[p++] = 0x0a;
                   6788:                                                }
1.1.1.14  root     6789:                                                msdos_putch('\n');
1.1       root     6790:                                                break;
                   6791:                                        } else if(chr == 0x08) {
                   6792:                                                // back space
                   6793:                                                if(p > 0) {
                   6794:                                                        p--;
1.1.1.20  root     6795:                                                        if(msdos_ctrl_code_check(buf[p])) {
                   6796:                                                                msdos_putch(chr);
                   6797:                                                                msdos_putch(chr);
                   6798:                                                                msdos_putch(' ');
                   6799:                                                                msdos_putch(' ');
                   6800:                                                                msdos_putch(chr);
                   6801:                                                                msdos_putch(chr);
                   6802:                                                        } else {
                   6803:                                                                msdos_putch(chr);
                   6804:                                                                msdos_putch(' ');
                   6805:                                                                msdos_putch(chr);
                   6806:                                                        }
1.1       root     6807:                                                }
                   6808:                                        } else {
                   6809:                                                buf[p++] = chr;
                   6810:                                                msdos_putch(chr);
                   6811:                                        }
                   6812:                                }
                   6813:                                REG16(AX) = p;
1.1.1.26  root     6814:                                
1.1.1.8   root     6815:                                // some seconds may be passed in console
1.1       root     6816:                                hardware_update();
                   6817:                        } else {
1.1.1.20  root     6818:                                REG16(AX) = _read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6819:                        }
                   6820:                } else {
                   6821:                        REG16(AX) = 0x05;
1.1.1.3   root     6822:                        m_CF = 1;
1.1       root     6823:                }
                   6824:        } else {
                   6825:                REG16(AX) = 0x06;
1.1.1.3   root     6826:                m_CF = 1;
1.1       root     6827:        }
                   6828: }
                   6829: 
                   6830: inline void msdos_int_21h_40h()
                   6831: {
                   6832:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6833:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6834:        
1.1.1.20  root     6835:        if(fd < process->max_files && file_handler[fd].valid) {
                   6836:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     6837:                        if(REG16(CX)) {
1.1.1.20  root     6838:                                if(file_handler[fd].atty) {
1.1       root     6839:                                        // BX is stdout/stderr or is redirected to stdout
                   6840:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     6841:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     6842:                                        }
                   6843:                                        REG16(AX) = REG16(CX);
                   6844:                                } else {
1.1.1.20  root     6845:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6846:                                }
                   6847:                        } else {
1.1.1.20  root     6848:                                UINT32 pos = _tell(fd);
                   6849:                                _lseek(fd, 0, SEEK_END);
                   6850:                                UINT32 size = _tell(fd);
1.1.1.12  root     6851:                                if(pos < size) {
1.1.1.20  root     6852:                                        _lseek(fd, pos, SEEK_SET);
                   6853:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     6854:                                } else {
                   6855:                                        for(UINT32 i = size; i < pos; i++) {
                   6856:                                                UINT8 tmp = 0;
1.1.1.23  root     6857:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     6858:                                        }
1.1.1.20  root     6859:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     6860:                                }
1.1.1.23  root     6861:                                REG16(AX) = 0;
1.1       root     6862:                        }
                   6863:                } else {
                   6864:                        REG16(AX) = 0x05;
1.1.1.3   root     6865:                        m_CF = 1;
1.1       root     6866:                }
                   6867:        } else {
                   6868:                REG16(AX) = 0x06;
1.1.1.3   root     6869:                m_CF = 1;
1.1       root     6870:        }
                   6871: }
                   6872: 
                   6873: inline void msdos_int_21h_41h(int lfn)
                   6874: {
1.1.1.3   root     6875:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6876:                REG16(AX) = errno;
1.1.1.3   root     6877:                m_CF = 1;
1.1       root     6878:        }
                   6879: }
                   6880: 
                   6881: inline void msdos_int_21h_42h()
                   6882: {
                   6883:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6884:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6885:        
1.1.1.20  root     6886:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     6887:                if(REG8(AL) < 0x03) {
                   6888:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     6889:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   6890:                        UINT32 pos = _tell(fd);
1.1       root     6891:                        REG16(AX) = pos & 0xffff;
                   6892:                        REG16(DX) = (pos >> 16);
                   6893:                } else {
                   6894:                        REG16(AX) = 0x01;
1.1.1.3   root     6895:                        m_CF = 1;
1.1       root     6896:                }
                   6897:        } else {
                   6898:                REG16(AX) = 0x06;
1.1.1.3   root     6899:                m_CF = 1;
1.1       root     6900:        }
                   6901: }
                   6902: 
                   6903: inline void msdos_int_21h_43h(int lfn)
                   6904: {
1.1.1.3   root     6905:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     6906:        int attr;
                   6907:        
1.1.1.14  root     6908:        if(!lfn && REG8(AL) > 2) {
                   6909:                REG16(AX) = 0x01;
                   6910:                m_CF = 1;
                   6911:                return;
                   6912:        }
                   6913:        switch(REG8(lfn ? BL : AL)) {
1.1       root     6914:        case 0x00:
                   6915:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     6916:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   6917:                } else {
                   6918:                        REG16(AX) = (UINT16)GetLastError();
                   6919:                        m_CF = 1;
                   6920:                }
                   6921:                break;
                   6922:        case 0x01:
                   6923:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   6924:                        REG16(AX) = (UINT16)GetLastError();
                   6925:                        m_CF = 1;
                   6926:                }
                   6927:                break;
                   6928:        case 0x02:
                   6929:                {
                   6930:                        DWORD size = GetCompressedFileSize(path, NULL);
                   6931:                        if(size != INVALID_FILE_SIZE) {
                   6932:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   6933:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   6934:                                        // this isn't correct if the file is in the NTFS MFT
                   6935:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   6936:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   6937:                                        }
                   6938:                                }
                   6939:                                REG16(AX) = LOWORD(size);
                   6940:                                REG16(DX) = HIWORD(size);
                   6941:                        } else {
                   6942:                                REG16(AX) = (UINT16)GetLastError();
                   6943:                                m_CF = 1;
1.1       root     6944:                        }
1.1.1.14  root     6945:                }
                   6946:                break;
                   6947:        case 0x03:
                   6948:        case 0x05:
                   6949:        case 0x07:
                   6950:                {
                   6951:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6952:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6953:                                FILETIME local, time;
                   6954:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   6955:                                if(REG8(BL) == 7) {
                   6956:                                        ULARGE_INTEGER hund;
                   6957:                                        hund.LowPart = local.dwLowDateTime;
                   6958:                                        hund.HighPart = local.dwHighDateTime;
                   6959:                                        hund.QuadPart += REG16(SI) * 100000;
                   6960:                                        local.dwLowDateTime = hund.LowPart;
                   6961:                                        local.dwHighDateTime = hund.HighPart;
                   6962:                                }
                   6963:                                LocalFileTimeToFileTime(&local, &time);
                   6964:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   6965:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   6966:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   6967:                                        REG16(AX) = (UINT16)GetLastError();
                   6968:                                        m_CF = 1;
                   6969:                                }
                   6970:                                CloseHandle(hFile);
                   6971:                        } else {
                   6972:                                REG16(AX) = (UINT16)GetLastError();
                   6973:                                m_CF = 1;
1.1       root     6974:                        }
1.1.1.14  root     6975:                }
                   6976:                break;
                   6977:        case 0x04:
                   6978:        case 0x06:
                   6979:        case 0x08:
                   6980:                {
                   6981:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   6982:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   6983:                                FILETIME *time, local;
                   6984:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   6985:                                                   0x06 ? &fad.ftLastAccessTime :
                   6986:                                                          &fad.ftCreationTime;
                   6987:                                FileTimeToLocalFileTime(time, &local);
                   6988:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   6989:                                if(REG8(BL) == 0x08) {
                   6990:                                        ULARGE_INTEGER hund;
                   6991:                                        hund.LowPart = local.dwLowDateTime;
                   6992:                                        hund.HighPart = local.dwHighDateTime;
                   6993:                                        hund.QuadPart /= 100000;
                   6994:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   6995:                                }
                   6996:                        } else {
                   6997:                                REG16(AX) = (UINT16)GetLastError();
                   6998:                                m_CF = 1;
1.1       root     6999:                        }
1.1.1.14  root     7000:                }
                   7001:                break;
                   7002:        default:
1.1.1.22  root     7003:                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     7004:                REG16(AX) = 0x01;
                   7005:                m_CF = 1;
                   7006:                break;
                   7007:        }
                   7008: }
                   7009: 
                   7010: inline void msdos_int_21h_44h()
                   7011: {
1.1.1.22  root     7012:        static UINT16 iteration_count = 0;
                   7013:        
1.1.1.20  root     7014:        process_t *process = msdos_process_info_get(current_psp);
                   7015:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7016:        
1.1.1.14  root     7017:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   7018:        
                   7019:        switch(REG8(AL)) {
                   7020:        case 0x00:
                   7021:        case 0x01:
                   7022:        case 0x02:
                   7023:        case 0x03:
                   7024:        case 0x04:
                   7025:        case 0x05:
                   7026:        case 0x06:
                   7027:        case 0x07:
1.1.1.20  root     7028:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   7029:                        REG16(AX) = 0x06;
                   7030:                        m_CF = 1;
                   7031:                        return;
1.1.1.14  root     7032:                }
                   7033:                break;
                   7034:        case 0x08:
                   7035:        case 0x09:
                   7036:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   7037:                        // invalid drive number
                   7038:                        REG16(AX) = 0x0f;
                   7039:                        m_CF = 1;
                   7040:                        return;
                   7041:                } else {
                   7042:                        if(REG8(BL) == 0) {
                   7043:                                val = GetDriveType(NULL);
                   7044:                        } else {
                   7045:                                char tmp[8];
                   7046:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   7047:                                val = GetDriveType(tmp);
                   7048:                        }
                   7049:                        if(val == DRIVE_NO_ROOT_DIR) {
                   7050:                                // no drive
                   7051:                                REG16(AX) = 0x0f;
                   7052:                                m_CF = 1;
                   7053:                                return;
1.1       root     7054:                        }
                   7055:                }
                   7056:                break;
                   7057:        }
                   7058:        switch(REG8(AL)) {
                   7059:        case 0x00: // get ioctrl data
1.1.1.20  root     7060:                REG16(DX) = file_handler[fd].info;
1.1       root     7061:                break;
                   7062:        case 0x01: // set ioctrl data
1.1.1.20  root     7063:                file_handler[fd].info |= REG8(DL);
1.1       root     7064:                break;
                   7065:        case 0x02: // recv from character device
                   7066:        case 0x03: // send to character device
                   7067:        case 0x04: // recv from block device
                   7068:        case 0x05: // send to block device
                   7069:                REG16(AX) = 0x05;
1.1.1.3   root     7070:                m_CF = 1;
1.1       root     7071:                break;
                   7072:        case 0x06: // get read status
1.1.1.20  root     7073:                if(file_mode[file_handler[fd].mode].in) {
                   7074:                        if(file_handler[fd].atty) {
1.1.1.14  root     7075:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     7076:                        } else {
1.1.1.20  root     7077:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     7078:                        }
1.1.1.14  root     7079:                } else {
                   7080:                        REG8(AL) = 0x00;
1.1       root     7081:                }
                   7082:                break;
                   7083:        case 0x07: // get write status
1.1.1.20  root     7084:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     7085:                        REG8(AL) = 0xff;
                   7086:                } else {
                   7087:                        REG8(AL) = 0x00;
1.1       root     7088:                }
                   7089:                break;
                   7090:        case 0x08: // check removable drive
1.1.1.14  root     7091:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   7092:                        // removable drive
                   7093:                        REG16(AX) = 0x00;
1.1       root     7094:                } else {
1.1.1.14  root     7095:                        // fixed drive
                   7096:                        REG16(AX) = 0x01;
1.1       root     7097:                }
                   7098:                break;
                   7099:        case 0x09: // check remote drive
1.1.1.14  root     7100:                if(val == DRIVE_REMOTE) {
                   7101:                        // remote drive
                   7102:                        REG16(DX) = 0x1000;
1.1       root     7103:                } else {
1.1.1.14  root     7104:                        // local drive
                   7105:                        REG16(DX) = 0x00;
1.1       root     7106:                }
                   7107:                break;
1.1.1.21  root     7108:        case 0x0a: // check remote handle
                   7109:                REG16(DX) = 0x00; // FIXME
                   7110:                break;
1.1       root     7111:        case 0x0b: // set retry count
                   7112:                break;
1.1.1.22  root     7113:        case 0x0c: // generic character device request
                   7114:                if(REG8(CL) == 0x45) {
                   7115:                        // set iteration (retry) count
                   7116:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   7117:                } else if(REG8(CL) == 0x4a) {
                   7118:                        // select code page
                   7119:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   7120:                        msdos_nls_tables_update();
                   7121:                } else if(REG8(CL) == 0x65) {
                   7122:                        // get iteration (retry) count
                   7123:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   7124:                } else if(REG8(CL) == 0x6a) {
                   7125:                        // query selected code page
                   7126:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   7127:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   7128:                        
                   7129:                        CPINFO info;
                   7130:                        GetCPInfo(active_code_page, &info);
                   7131:                        
                   7132:                        if(info.MaxCharSize != 1) {
                   7133:                                for(int i = 0;; i++) {
                   7134:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   7135:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   7136:                                        
                   7137:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   7138:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   7139:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   7140:                                        
                   7141:                                        if(lo == 0 && hi == 0) {
                   7142:                                                break;
                   7143:                                        }
                   7144:                                }
                   7145:                        }
                   7146:                } else if(REG8(CL) == 0x7f) {
                   7147:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   7148:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   7149:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   7150:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   7151:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   7152:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   7153:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   7154:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   7155:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   7156:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   7157:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   7158:                } else {
                   7159:                        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));
                   7160:                        REG16(AX) = 0x01; // invalid function
                   7161:                        m_CF = 1;
                   7162:                }
                   7163:                break;
                   7164:        case 0x0d: // generic block device request
                   7165:                if(REG8(CL) == 0x40) {
                   7166:                        // set device parameters
                   7167:                } else if(REG8(CL) == 0x46) {
                   7168:                        // set volume serial number
                   7169:                } else if(REG8(CL) == 0x4a) {
                   7170:                        // lock logical volume
                   7171:                } else if(REG8(CL) == 0x4b) {
                   7172:                        // lock physical volume
                   7173:                } else if(REG8(CL) == 0x60) {
                   7174:                        // get device parameters
                   7175:                        char dev[] = "\\\\.\\A:";
                   7176:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   7177:                        
                   7178:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7179:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7180:                                DISK_GEOMETRY geo;
                   7181:                                DWORD dwSize;
                   7182:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   7183:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   7184:                                        switch(geo.MediaType) {
                   7185:                                        case F5_360_512:
                   7186:                                        case F5_320_512:
                   7187:                                        case F5_320_1024:
                   7188:                                        case F5_180_512:
                   7189:                                        case F5_160_512:
                   7190:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   7191:                                                break;
                   7192:                                        case F5_1Pt2_512:
                   7193:                                        case F3_1Pt2_512:
                   7194:                                        case F3_1Pt23_1024:
                   7195:                                        case F5_1Pt23_1024:
                   7196:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   7197:                                                break;
                   7198:                                        case F3_720_512:
                   7199:                                        case F3_640_512:
                   7200:                                        case F5_640_512:
                   7201:                                        case F5_720_512:
                   7202:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   7203:                                                break;
                   7204:                                        case F8_256_128:
                   7205:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   7206:                                                break;
                   7207:                                        case FixedMedia:
                   7208:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   7209:                                                break;
                   7210:                                        case F3_1Pt44_512:
                   7211:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   7212:                                                break;
                   7213:                                        case F3_2Pt88_512:
                   7214:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   7215:                                                break;
                   7216:                                        default:
                   7217:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   7218: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   7219:                                                break;
                   7220:                                        }
                   7221:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   7222:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   7223:                                        switch(geo.MediaType) {
                   7224:                                        case F5_360_512:
                   7225:                                        case F5_320_512:
                   7226:                                        case F5_320_1024:
                   7227:                                        case F5_180_512:
                   7228:                                        case F5_160_512:
                   7229:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   7230:                                                break;
                   7231:                                        default:
                   7232:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   7233:                                                break;
                   7234:                                        }
                   7235:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   7236:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   7237:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   7238:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   7239:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   7240:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   7241:                                        switch(geo.MediaType) {
                   7242:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   7243:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   7244:                                                break;
                   7245:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   7246:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   7247:                                                break;
                   7248:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   7249:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   7250:                                                break;
                   7251:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   7252:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   7253:                                                break;
                   7254:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   7255:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   7256:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   7257:                                                break;
                   7258:                                        case FixedMedia:        // hard disk
                   7259:                                        case RemovableMedia:
                   7260:                                        case Unknown:
                   7261:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   7262:                                                break;
                   7263:                                        default:
                   7264:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   7265:                                                break;
                   7266:                                        }
                   7267:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   7268:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   7269:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   7270:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   7271:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   7272:                                        // 21h  BYTE    device type
                   7273:                                        // 22h  WORD    device attributes (removable or not, etc)
                   7274:                                } else {
                   7275:                                        REG16(AX) = 0x0f; // invalid drive
                   7276:                                        m_CF = 1;
                   7277:                                }
                   7278:                                CloseHandle(hFile);
                   7279:                        } else {
                   7280:                                REG16(AX) = 0x0f; // invalid drive
                   7281:                                m_CF = 1;
                   7282:                        }
                   7283:                } else if(REG8(CL) == 0x66) {
                   7284:                        // get volume serial number
                   7285:                        char path[] = "A:\\";
                   7286:                        char volume_label[MAX_PATH];
                   7287:                        DWORD serial_number = 0;
                   7288:                        char file_system[MAX_PATH];
                   7289:                        
                   7290:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   7291:                        
                   7292:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   7293:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   7294:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   7295:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   7296:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   7297:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   7298:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   7299:                        } else {
                   7300:                                REG16(AX) = 0x0f; // invalid drive
                   7301:                                m_CF = 1;
                   7302:                        }
                   7303:                } else if(REG8(CL) == 0x67) {
                   7304:                        // get access flag
                   7305:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   7306:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   7307:                } else if(REG8(CL) == 0x68) {
                   7308:                        // sense media type
                   7309:                        char dev[64];
                   7310:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7311:                        
                   7312:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7313:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7314:                                DISK_GEOMETRY geo;
                   7315:                                DWORD dwSize;
                   7316:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   7317:                                        switch(geo.MediaType) {
                   7318:                                        case F3_720_512:
                   7319:                                        case F5_720_512:
                   7320:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7321:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   7322:                                                break;
                   7323:                                        case F3_1Pt44_512:
                   7324:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7325:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   7326:                                                break;
                   7327:                                        case F3_2Pt88_512:
                   7328:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   7329:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   7330:                                                break;
                   7331:                                        default:
                   7332:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   7333:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   7334:                                                break;
                   7335:                                        }
                   7336:                                } else {
                   7337:                                        REG16(AX) = 0x0f; // invalid drive
                   7338:                                        m_CF = 1;
                   7339:                                }
                   7340:                                CloseHandle(hFile);
                   7341:                        } else {
                   7342:                                REG16(AX) = 0x0f; // invalid drive
                   7343:                                m_CF = 1;
                   7344:                        }
                   7345:                } else if(REG8(CL) == 0x6a) {
                   7346:                        // unlock logical volume
                   7347:                } else if(REG8(CL) == 0x6b) {
                   7348:                        // unlock physical volume
                   7349:                } else {
                   7350:                        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));
                   7351:                        REG16(AX) = 0x01; // invalid function
                   7352:                        m_CF = 1;
                   7353:                }
                   7354:                break;
                   7355:        case 0x0e: // get logical drive map
                   7356:                {
                   7357:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7358:                        if(!(GetLogicalDrives() & bits)) {
                   7359:                                REG16(AX) = 0x0f; // invalid drive
                   7360:                                m_CF = 1;
                   7361:                        } else {
                   7362:                                REG8(AL) = 0;
                   7363:                        }
                   7364:                }
                   7365:                break;
                   7366:        case 0x0f: // set logical drive map
                   7367:                {
                   7368:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7369:                        if(!(GetLogicalDrives() & bits)) {
                   7370:                                REG16(AX) = 0x0f; // invalid drive
                   7371:                                m_CF = 1;
                   7372:                        }
                   7373:                }
                   7374:                break;
                   7375:        case 0x10: // query generic ioctrl capability (handle)
                   7376:                switch(REG8(CL)) {
                   7377:                case 0x45:
                   7378:                case 0x4a:
                   7379:                case 0x65:
                   7380:                case 0x6a:
                   7381:                case 0x7f:
                   7382:                        REG16(AX) = 0x0000; // supported
                   7383:                        break;
                   7384:                default:
                   7385:                        REG8(AL) = 0x01; // ioctl capability not available
                   7386:                        m_CF = 1;
                   7387:                        break;
                   7388:                }
                   7389:                break;
                   7390:        case 0x11: // query generic ioctrl capability (drive)
                   7391:                switch(REG8(CL)) {
                   7392:                case 0x40:
                   7393:                case 0x46:
                   7394:                case 0x4a:
                   7395:                case 0x4b:
                   7396:                case 0x60:
                   7397:                case 0x66:
                   7398:                case 0x67:
                   7399:                case 0x68:
                   7400:                case 0x6a:
                   7401:                case 0x6b:
                   7402:                        REG16(AX) = 0x0000; // supported
                   7403:                        break;
                   7404:                default:
                   7405:                        REG8(AL) = 0x01; // ioctl capability not available
                   7406:                        m_CF = 1;
                   7407:                        break;
                   7408:                }
                   7409:                break;
                   7410:        case 0x12: // determine dos type
                   7411:        case 0x51: // concurrent dos v3.2+ - installation check
                   7412:        case 0x52: // determine dos type/get dr dos versuin
                   7413:                REG16(AX) = 0x01; // this  is not DR-DOS
                   7414:                m_CF = 1;
                   7415:                break;
1.1       root     7416:        default:
1.1.1.22  root     7417:                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     7418:                REG16(AX) = 0x01;
1.1.1.3   root     7419:                m_CF = 1;
1.1       root     7420:                break;
                   7421:        }
                   7422: }
                   7423: 
                   7424: inline void msdos_int_21h_45h()
                   7425: {
                   7426:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7427:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7428:        
1.1.1.20  root     7429:        if(fd < process->max_files && file_handler[fd].valid) {
                   7430:                int dup_fd = _dup(fd);
                   7431:                if(dup_fd != -1) {
                   7432:                        REG16(AX) = dup_fd;
                   7433:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7434: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7435:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7436:                } else {
                   7437:                        REG16(AX) = errno;
1.1.1.3   root     7438:                        m_CF = 1;
1.1       root     7439:                }
                   7440:        } else {
                   7441:                REG16(AX) = 0x06;
1.1.1.3   root     7442:                m_CF = 1;
1.1       root     7443:        }
                   7444: }
                   7445: 
                   7446: inline void msdos_int_21h_46h()
                   7447: {
                   7448:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7449:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7450:        int dup_fd = REG16(CX);
                   7451:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     7452:        
1.1.1.20  root     7453:        if(REG16(BX) == REG16(CX)) {
                   7454:                REG16(AX) = 0x06;
                   7455:                m_CF = 1;
                   7456:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   7457:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   7458:                        _close(tmp_fd);
                   7459:                        msdos_file_handler_close(tmp_fd);
                   7460:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   7461:                }
                   7462:                if(_dup2(fd, dup_fd) != -1) {
                   7463:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7464: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7465:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7466:                } else {
                   7467:                        REG16(AX) = errno;
1.1.1.3   root     7468:                        m_CF = 1;
1.1       root     7469:                }
                   7470:        } else {
                   7471:                REG16(AX) = 0x06;
1.1.1.3   root     7472:                m_CF = 1;
1.1       root     7473:        }
                   7474: }
                   7475: 
                   7476: inline void msdos_int_21h_47h(int lfn)
                   7477: {
                   7478:        char path[MAX_PATH];
                   7479:        
                   7480:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   7481:                if(path[1] == ':') {
                   7482:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     7483:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     7484:                } else {
1.1.1.3   root     7485:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     7486:                }
                   7487:        } else {
                   7488:                REG16(AX) = errno;
1.1.1.3   root     7489:                m_CF = 1;
1.1       root     7490:        }
                   7491: }
                   7492: 
                   7493: inline void msdos_int_21h_48h()
                   7494: {
1.1.1.19  root     7495:        int seg, umb_linked;
1.1       root     7496:        
1.1.1.8   root     7497:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     7498:                // unlink umb not to allocate memory in umb
                   7499:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   7500:                        msdos_mem_unlink_umb();
                   7501:                }
1.1.1.8   root     7502:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7503:                        REG16(AX) = seg;
                   7504:                } else {
                   7505:                        REG16(AX) = 0x08;
                   7506:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   7507:                        m_CF = 1;
                   7508:                }
1.1.1.19  root     7509:                if(umb_linked != 0) {
                   7510:                        msdos_mem_link_umb();
                   7511:                }
1.1.1.8   root     7512:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   7513:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7514:                        REG16(AX) = seg;
                   7515:                } else {
                   7516:                        REG16(AX) = 0x08;
                   7517:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   7518:                        m_CF = 1;
                   7519:                }
                   7520:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   7521:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7522:                        REG16(AX) = seg;
                   7523:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7524:                        REG16(AX) = seg;
                   7525:                } else {
                   7526:                        REG16(AX) = 0x08;
                   7527:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   7528:                        m_CF = 1;
                   7529:                }
1.1       root     7530:        }
                   7531: }
                   7532: 
                   7533: inline void msdos_int_21h_49h()
                   7534: {
1.1.1.14  root     7535:        int mcb_seg = SREG(ES) - 1;
                   7536:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   7537:        
                   7538:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7539:                msdos_mem_free(SREG(ES));
                   7540:        } else {
1.1.1.28  root     7541:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.14  root     7542:                m_CF = 1;
                   7543:        }
1.1       root     7544: }
                   7545: 
                   7546: inline void msdos_int_21h_4ah()
                   7547: {
1.1.1.14  root     7548:        int mcb_seg = SREG(ES) - 1;
                   7549:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     7550:        int max_paragraphs;
                   7551:        
1.1.1.14  root     7552:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7553:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   7554:                        REG16(AX) = 0x08;
                   7555:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   7556:                        m_CF = 1;
                   7557:                }
                   7558:        } else {
1.1.1.28  root     7559:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.3   root     7560:                m_CF = 1;
1.1       root     7561:        }
                   7562: }
                   7563: 
                   7564: inline void msdos_int_21h_4bh()
                   7565: {
1.1.1.3   root     7566:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7567:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     7568:        
                   7569:        switch(REG8(AL)) {
                   7570:        case 0x00:
                   7571:        case 0x01:
                   7572:                if(msdos_process_exec(command, param, REG8(AL))) {
                   7573:                        REG16(AX) = 0x02;
1.1.1.3   root     7574:                        m_CF = 1;
1.1       root     7575:                }
                   7576:                break;
1.1.1.14  root     7577:        case 0x03:
                   7578:                {
                   7579:                        int fd;
                   7580:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   7581:                                REG16(AX) = 0x02;
                   7582:                                m_CF = 1;
                   7583:                                break;
                   7584:                        }
                   7585:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   7586:                        _close(fd);
                   7587:                        
                   7588:                        UINT16 *overlay = (UINT16 *)param;
                   7589:                        
                   7590:                        // check exe header
                   7591:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   7592:                        int header_size = 0;
                   7593:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   7594:                                header_size = header->header_size * 16;
                   7595:                                // relocation
                   7596:                                int start_seg = overlay[1];
                   7597:                                for(int i = 0; i < header->relocations; i++) {
                   7598:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   7599:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   7600:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   7601:                                }
                   7602:                        }
                   7603:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   7604:                }
                   7605:                break;
1.1       root     7606:        default:
1.1.1.22  root     7607:                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     7608:                REG16(AX) = 0x01;
1.1.1.3   root     7609:                m_CF = 1;
1.1       root     7610:                break;
                   7611:        }
                   7612: }
                   7613: 
                   7614: inline void msdos_int_21h_4ch()
                   7615: {
                   7616:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   7617: }
                   7618: 
                   7619: inline void msdos_int_21h_4dh()
                   7620: {
                   7621:        REG16(AX) = retval;
                   7622: }
                   7623: 
                   7624: inline void msdos_int_21h_4eh()
                   7625: {
                   7626:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7627:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7628:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     7629:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7630:        WIN32_FIND_DATA fd;
                   7631:        
1.1.1.14  root     7632:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   7633:        find->find_magic = FIND_MAGIC;
                   7634:        find->dta_index = dtainfo - dtalist;
1.1       root     7635:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     7636:        dtainfo->allowable_mask = REG8(CL);
                   7637:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     7638:        
1.1.1.14  root     7639:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   7640:                dtainfo->allowable_mask &= ~8;
1.1       root     7641:        }
1.1.1.14  root     7642:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   7643:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7644:                      !msdos_find_file_has_8dot3name(&fd)) {
                   7645:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7646:                                FindClose(dtainfo->find_handle);
                   7647:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7648:                                break;
                   7649:                        }
                   7650:                }
                   7651:        }
1.1.1.13  root     7652:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7653:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7654:                msdos_find_file_conv_local_time(&fd);
                   7655:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7656:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7657:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7658:                REG16(AX) = 0;
1.1.1.14  root     7659:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7660:                find->attrib = 8;
                   7661:                find->size = 0;
                   7662:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7663:                dtainfo->allowable_mask &= ~8;
1.1       root     7664:                REG16(AX) = 0;
                   7665:        } else {
                   7666:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     7667:                m_CF = 1;
1.1       root     7668:        }
                   7669: }
                   7670: 
                   7671: inline void msdos_int_21h_4fh()
                   7672: {
                   7673:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7674:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7675:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     7676:        WIN32_FIND_DATA fd;
                   7677:        
1.1.1.14  root     7678:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   7679:                REG16(AX) = 0x12;
                   7680:                m_CF = 1;
                   7681:                return;
                   7682:        }
                   7683:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     7684:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7685:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     7686:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7687:                              !msdos_find_file_has_8dot3name(&fd)) {
                   7688:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7689:                                        FindClose(dtainfo->find_handle);
                   7690:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7691:                                        break;
                   7692:                                }
                   7693:                        }
                   7694:                } else {
1.1.1.13  root     7695:                        FindClose(dtainfo->find_handle);
                   7696:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7697:                }
                   7698:        }
1.1.1.13  root     7699:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7700:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7701:                msdos_find_file_conv_local_time(&fd);
                   7702:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7703:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7704:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7705:                REG16(AX) = 0;
1.1.1.14  root     7706:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7707:                find->attrib = 8;
                   7708:                find->size = 0;
                   7709:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7710:                dtainfo->allowable_mask &= ~8;
1.1       root     7711:                REG16(AX) = 0;
                   7712:        } else {
                   7713:                REG16(AX) = 0x12;
1.1.1.3   root     7714:                m_CF = 1;
1.1       root     7715:        }
                   7716: }
                   7717: 
                   7718: inline void msdos_int_21h_50h()
                   7719: {
1.1.1.8   root     7720:        if(current_psp != REG16(BX)) {
                   7721:                process_t *process = msdos_process_info_get(current_psp);
                   7722:                if(process != NULL) {
                   7723:                        process->psp = REG16(BX);
                   7724:                }
                   7725:                current_psp = REG16(BX);
1.1.1.23  root     7726:                msdos_sda_update(current_psp);
1.1.1.8   root     7727:        }
1.1       root     7728: }
                   7729: 
                   7730: inline void msdos_int_21h_51h()
                   7731: {
                   7732:        REG16(BX) = current_psp;
                   7733: }
                   7734: 
                   7735: inline void msdos_int_21h_52h()
                   7736: {
1.1.1.25  root     7737:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     7738:        i386_load_segment_descriptor(ES);
1.1.1.25  root     7739:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     7740: }
                   7741: 
                   7742: inline void msdos_int_21h_54h()
                   7743: {
                   7744:        process_t *process = msdos_process_info_get(current_psp);
                   7745:        
                   7746:        REG8(AL) = process->verify;
                   7747: }
                   7748: 
                   7749: inline void msdos_int_21h_55h()
                   7750: {
                   7751:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   7752:        
                   7753:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   7754:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   7755:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   7756:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   7757:        psp->parent_psp = current_psp;
                   7758: }
                   7759: 
                   7760: inline void msdos_int_21h_56h(int lfn)
                   7761: {
                   7762:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     7763:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   7764:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     7765:        
                   7766:        if(rename(src, dst)) {
                   7767:                REG16(AX) = errno;
1.1.1.3   root     7768:                m_CF = 1;
1.1       root     7769:        }
                   7770: }
                   7771: 
                   7772: inline void msdos_int_21h_57h()
                   7773: {
                   7774:        FILETIME time, local;
1.1.1.14  root     7775:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     7776:        HANDLE hHandle;
1.1       root     7777:        
1.1.1.21  root     7778:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     7779:                REG16(AX) = (UINT16)GetLastError();
                   7780:                m_CF = 1;
                   7781:                return;
                   7782:        }
                   7783:        ctime = atime = mtime = NULL;
                   7784:        
1.1       root     7785:        switch(REG8(AL)) {
                   7786:        case 0x00:
1.1.1.6   root     7787:        case 0x01:
1.1.1.14  root     7788:                mtime = &time;
1.1.1.6   root     7789:                break;
                   7790:        case 0x04:
                   7791:        case 0x05:
1.1.1.14  root     7792:                atime = &time;
1.1       root     7793:                break;
1.1.1.6   root     7794:        case 0x06:
                   7795:        case 0x07:
1.1.1.14  root     7796:                ctime = &time;
                   7797:                break;
                   7798:        default:
1.1.1.22  root     7799:                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     7800:                REG16(AX) = 0x01;
                   7801:                m_CF = 1;
                   7802:                return;
                   7803:        }
                   7804:        if(REG8(AL) & 1) {
1.1       root     7805:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   7806:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     7807:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     7808:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7809:                        m_CF = 1;
1.1       root     7810:                }
1.1.1.14  root     7811:        } else {
1.1.1.21  root     7812:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     7813:                        // assume a device and use the current time
                   7814:                        GetSystemTimeAsFileTime(&time);
                   7815:                }
                   7816:                FileTimeToLocalFileTime(&time, &local);
                   7817:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     7818:        }
                   7819: }
                   7820: 
                   7821: inline void msdos_int_21h_58h()
                   7822: {
                   7823:        switch(REG8(AL)) {
                   7824:        case 0x00:
1.1.1.7   root     7825:                REG16(AX) = malloc_strategy;
                   7826:                break;
                   7827:        case 0x01:
1.1.1.24  root     7828: //             switch(REG16(BX)) {
                   7829:                switch(REG8(BL)) {
1.1.1.7   root     7830:                case 0x0000:
                   7831:                case 0x0001:
                   7832:                case 0x0002:
                   7833:                case 0x0040:
                   7834:                case 0x0041:
                   7835:                case 0x0042:
                   7836:                case 0x0080:
                   7837:                case 0x0081:
                   7838:                case 0x0082:
                   7839:                        malloc_strategy = REG16(BX);
1.1.1.23  root     7840:                        msdos_sda_update(current_psp);
1.1.1.7   root     7841:                        break;
                   7842:                default:
1.1.1.22  root     7843:                        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     7844:                        REG16(AX) = 0x01;
                   7845:                        m_CF = 1;
                   7846:                        break;
                   7847:                }
                   7848:                break;
                   7849:        case 0x02:
1.1.1.19  root     7850:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     7851:                break;
                   7852:        case 0x03:
1.1.1.24  root     7853: //             switch(REG16(BX)) {
                   7854:                switch(REG8(BL)) {
1.1.1.7   root     7855:                case 0x0000:
1.1.1.19  root     7856:                        msdos_mem_unlink_umb();
                   7857:                        break;
1.1.1.7   root     7858:                case 0x0001:
1.1.1.19  root     7859:                        msdos_mem_link_umb();
1.1.1.7   root     7860:                        break;
                   7861:                default:
1.1.1.22  root     7862:                        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     7863:                        REG16(AX) = 0x01;
                   7864:                        m_CF = 1;
                   7865:                        break;
                   7866:                }
1.1       root     7867:                break;
                   7868:        default:
1.1.1.22  root     7869:                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     7870:                REG16(AX) = 0x01;
1.1.1.3   root     7871:                m_CF = 1;
1.1       root     7872:                break;
                   7873:        }
                   7874: }
                   7875: 
                   7876: inline void msdos_int_21h_59h()
                   7877: {
1.1.1.23  root     7878:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   7879:        
                   7880:        REG16(AX) = sda->extended_error_code;
                   7881:        REG8(BH) = sda->error_class;
                   7882:        REG8(BL) = sda->suggested_action;
                   7883:        REG8(CH) = sda->locus_of_last_error;
1.1       root     7884: }
                   7885: 
                   7886: inline void msdos_int_21h_5ah()
                   7887: {
1.1.1.3   root     7888:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7889:        int len = strlen(path);
                   7890:        char tmp[MAX_PATH];
                   7891:        
                   7892:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   7893:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7894:                
                   7895:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7896:                REG16(AX) = fd;
                   7897:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7898:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7899:                
                   7900:                strcpy(path, tmp);
                   7901:                int dx = REG16(DX) + len;
1.1.1.3   root     7902:                int ds = SREG(DS);
1.1       root     7903:                while(dx > 0xffff) {
                   7904:                        dx -= 0x10;
                   7905:                        ds++;
                   7906:                }
                   7907:                REG16(DX) = dx;
1.1.1.3   root     7908:                SREG(DS) = ds;
                   7909:                i386_load_segment_descriptor(DS);
1.1       root     7910:        } else {
                   7911:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7912:                m_CF = 1;
1.1       root     7913:        }
                   7914: }
                   7915: 
                   7916: inline void msdos_int_21h_5bh()
                   7917: {
1.1.1.3   root     7918:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7919:        
1.1.1.24  root     7920:        if(msdos_is_existing_file(path)) {
1.1       root     7921:                // already exists
                   7922:                REG16(AX) = 0x50;
1.1.1.3   root     7923:                m_CF = 1;
1.1       root     7924:        } else {
                   7925:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7926:                
                   7927:                if(fd != -1) {
                   7928:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7929:                        REG16(AX) = fd;
                   7930:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7931:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7932:                } else {
                   7933:                        REG16(AX) = errno;
1.1.1.3   root     7934:                        m_CF = 1;
1.1       root     7935:                }
                   7936:        }
                   7937: }
                   7938: 
                   7939: inline void msdos_int_21h_5ch()
                   7940: {
                   7941:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7942:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7943:        
1.1.1.20  root     7944:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     7945:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   7946:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     7947:                        UINT32 pos = _tell(fd);
                   7948:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   7949:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     7950:                                REG16(AX) = errno;
1.1.1.3   root     7951:                                m_CF = 1;
1.1       root     7952:                        }
1.1.1.20  root     7953:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     7954:                        
1.1       root     7955:                        // some seconds may be passed in _locking()
                   7956:                        hardware_update();
                   7957:                } else {
                   7958:                        REG16(AX) = 0x01;
1.1.1.3   root     7959:                        m_CF = 1;
1.1       root     7960:                }
                   7961:        } else {
                   7962:                REG16(AX) = 0x06;
1.1.1.3   root     7963:                m_CF = 1;
1.1       root     7964:        }
                   7965: }
                   7966: 
1.1.1.22  root     7967: inline void msdos_int_21h_5dh()
                   7968: {
                   7969:        switch(REG8(AL)) {
                   7970:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     7971:                SREG(DS) = (SDA_TOP >> 4);
                   7972:                i386_load_segment_descriptor(DS);
                   7973:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   7974:                REG16(CX) = 0x80;
                   7975:                REG16(DX) = 0x1a;
                   7976:                break;
                   7977:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     7978:                REG16(AX) = 0x01;
                   7979:                m_CF = 1;
                   7980:                break;
                   7981:        case 0x08: // set redirected printer mode
                   7982:        case 0x09: // flush redirected printer output
                   7983:        case 0x0a: // set extended error information
                   7984:                break;
                   7985:        default:
                   7986:                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));
                   7987:                REG16(AX) = 0x01;
                   7988:                m_CF = 1;
                   7989:                break;
                   7990:        }
                   7991: }
                   7992: 
1.1.1.30! root     7993: inline void msdos_int_21h_5fh()
        !          7994: {
        !          7995:        switch(REG8(AL)) {
        !          7996:        case 0x02:
        !          7997:                {
        !          7998:                        DWORD drives = GetLogicalDrives();
        !          7999:                        for(int i = 0, index = 0; i < 26; i++) {
        !          8000:                                if(drives & (1 << i)) {
        !          8001:                                        char volume[] = "A:\\";
        !          8002:                                        volume[0] = 'A' + i;
        !          8003:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
        !          8004:                                                if(index == REG16(BX)) {
        !          8005:                                                        DWORD dwSize = 128;
        !          8006:                                                        volume[2] = '\0';
        !          8007:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
        !          8008:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
        !          8009:                                                        REG8(BH) = 0x00; // valid
        !          8010:                                                        REG8(BL) = 0x04; // disk drive
        !          8011:                                                        REG16(CX) = 0x00;
        !          8012:                                                        return;
        !          8013:                                                }
        !          8014:                                                index++;
        !          8015:                                        }
        !          8016:                                }
        !          8017:                        }
        !          8018:                }
        !          8019:                REG16(AX) = 0x12; // no more files
        !          8020:                m_CF = 1;
        !          8021:                break;
        !          8022:        default:
        !          8023:                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));
        !          8024:                REG16(AX) = 0x01;
        !          8025:                m_CF = 1;
        !          8026:                break;
        !          8027:        }
        !          8028: }
        !          8029: 
1.1       root     8030: inline void msdos_int_21h_60h(int lfn)
                   8031: {
1.1.1.14  root     8032:        char full[MAX_PATH], *path;
                   8033:        
1.1       root     8034:        if(lfn) {
1.1.1.14  root     8035:                char *name;
                   8036:                *full = '\0';
1.1.1.3   root     8037:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     8038:                switch(REG8(CL)) {
                   8039:                case 1:
                   8040:                        GetShortPathName(full, full, MAX_PATH);
                   8041:                        my_strupr(full);
                   8042:                        break;
                   8043:                case 2:
                   8044:                        GetLongPathName(full, full, MAX_PATH);
                   8045:                        break;
                   8046:                }
                   8047:                path = full;
                   8048:        } else {
                   8049:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   8050:        }
                   8051:        if(*path != '\0') {
                   8052:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     8053:        } else {
1.1.1.14  root     8054:                REG16(AX) = (UINT16)GetLastError();
                   8055:                m_CF = 1;
1.1       root     8056:        }
                   8057: }
                   8058: 
                   8059: inline void msdos_int_21h_61h()
                   8060: {
                   8061:        REG8(AL) = 0;
                   8062: }
                   8063: 
                   8064: inline void msdos_int_21h_62h()
                   8065: {
                   8066:        REG16(BX) = current_psp;
                   8067: }
                   8068: 
                   8069: inline void msdos_int_21h_63h()
                   8070: {
                   8071:        switch(REG8(AL)) {
                   8072:        case 0x00:
1.1.1.3   root     8073:                SREG(DS) = (DBCS_TABLE >> 4);
                   8074:                i386_load_segment_descriptor(DS);
1.1       root     8075:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   8076:                REG8(AL) = 0x00;
                   8077:                break;
1.1.1.22  root     8078:        case 0x01: // set korean input mode
                   8079:        case 0x02: // get korean input mode
                   8080:                REG8(AL) = 0xff; // not supported
                   8081:                break;
1.1       root     8082:        default:
1.1.1.22  root     8083:                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     8084:                REG16(AX) = 0x01;
1.1.1.3   root     8085:                m_CF = 1;
1.1       root     8086:                break;
                   8087:        }
                   8088: }
                   8089: 
1.1.1.25  root     8090: UINT16 get_extended_country_info(UINT8 func)
1.1       root     8091: {
1.1.1.25  root     8092:        switch(func) {
1.1.1.17  root     8093:        case 0x01:
                   8094:                if(REG16(CX) >= 5) {
1.1.1.19  root     8095:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     8096:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   8097:                                REG16(CX) = sizeof(data);
                   8098:                        ZeroMemory(data, sizeof(data));
                   8099:                        data[0] = 0x01;
                   8100:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     8101:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     8102:                        *(UINT16 *)(data + 5) = active_code_page;
                   8103:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     8104: //                     REG16(AX) = active_code_page;
1.1.1.17  root     8105:                } else {
1.1.1.25  root     8106:                        return(0x08); // insufficient memory
1.1.1.17  root     8107:                }
                   8108:                break;
                   8109:        case 0x02:
                   8110:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   8111:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   8112:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     8113: //             REG16(AX) = active_code_page;
1.1.1.17  root     8114:                REG16(CX) = 0x05;
                   8115:                break;
1.1.1.23  root     8116:        case 0x03:
                   8117:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   8118:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   8119:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     8120: //             REG16(AX) = active_code_page;
1.1.1.23  root     8121:                REG16(CX) = 0x05;
                   8122:                break;
1.1.1.17  root     8123:        case 0x04:
                   8124:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   8125:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   8126:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     8127: //             REG16(AX) = active_code_page;
1.1.1.17  root     8128:                REG16(CX) = 0x05;
                   8129:                break;
                   8130:        case 0x05:
                   8131:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   8132:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   8133:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     8134: //             REG16(AX) = active_code_page;
1.1.1.17  root     8135:                REG16(CX) = 0x05;
                   8136:                break;
                   8137:        case 0x06:
                   8138:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   8139:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   8140:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     8141: //             REG16(AX) = active_code_page;
1.1.1.17  root     8142:                REG16(CX) = 0x05;
                   8143:                break;
1.1       root     8144:        case 0x07:
1.1.1.3   root     8145:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   8146:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   8147:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     8148: //             REG16(AX) = active_code_page;
1.1       root     8149:                REG16(CX) = 0x05;
                   8150:                break;
1.1.1.25  root     8151:        default:
                   8152:                return(0x01); // function number invalid
                   8153:        }
                   8154:        return(0x00);
                   8155: }
                   8156: 
                   8157: inline void msdos_int_21h_65h()
                   8158: {
                   8159:        char tmp[0x10000];
                   8160:        
                   8161:        switch(REG8(AL)) {
                   8162:        case 0x01:
                   8163:        case 0x02:
                   8164:        case 0x03:
                   8165:        case 0x04:
                   8166:        case 0x05:
                   8167:        case 0x06:
                   8168:        case 0x07:
                   8169:                {
                   8170:                        UINT16 result = get_extended_country_info(REG8(AL));
                   8171:                        if(result) {
                   8172:                                REG16(AX) = result;
                   8173:                                m_CF = 1;
                   8174:                        } else {
                   8175:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   8176:                        }
                   8177:                }
                   8178:                break;
1.1       root     8179:        case 0x20:
1.1.1.25  root     8180:        case 0xa0:
1.1.1.19  root     8181:                memset(tmp, 0, sizeof(tmp));
                   8182:                tmp[0] = REG8(DL);
1.1       root     8183:                my_strupr(tmp);
                   8184:                REG8(DL) = tmp[0];
                   8185:                break;
                   8186:        case 0x21:
1.1.1.25  root     8187:        case 0xa1:
1.1       root     8188:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8189:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     8190:                my_strupr(tmp);
1.1.1.3   root     8191:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     8192:                break;
                   8193:        case 0x22:
1.1.1.25  root     8194:        case 0xa2:
1.1.1.3   root     8195:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     8196:                break;
1.1.1.25  root     8197:        case 0x23:
                   8198:                // FIXME: need to check multi-byte (kanji) charactre?
                   8199:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   8200:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   8201:                        REG16(AX) = 0x00;
                   8202:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   8203:                        // 826dh/828eh: multi-byte (kanji) N and n
                   8204:                        REG16(AX) = 0x01;
                   8205:                } else {
                   8206:                        REG16(AX) = 0x02;
                   8207:                }
                   8208:                break;
1.1       root     8209:        default:
1.1.1.22  root     8210:                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     8211:                REG16(AX) = 0x01;
1.1.1.3   root     8212:                m_CF = 1;
1.1       root     8213:                break;
                   8214:        }
                   8215: }
                   8216: 
                   8217: inline void msdos_int_21h_66h()
                   8218: {
                   8219:        switch(REG8(AL)) {
                   8220:        case 0x01:
                   8221:                REG16(BX) = active_code_page;
                   8222:                REG16(DX) = system_code_page;
                   8223:                break;
                   8224:        case 0x02:
                   8225:                if(active_code_page == REG16(BX)) {
                   8226:                        REG16(AX) = 0xeb41;
                   8227:                } else if(_setmbcp(REG16(BX)) == 0) {
                   8228:                        active_code_page = REG16(BX);
1.1.1.17  root     8229:                        msdos_nls_tables_update();
1.1       root     8230:                        REG16(AX) = 0xeb41;
                   8231:                } else {
                   8232:                        REG16(AX) = 0x25;
1.1.1.3   root     8233:                        m_CF = 1;
1.1       root     8234:                }
                   8235:                break;
                   8236:        default:
1.1.1.22  root     8237:                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     8238:                REG16(AX) = 0x01;
1.1.1.3   root     8239:                m_CF = 1;
1.1       root     8240:                break;
                   8241:        }
                   8242: }
                   8243: 
                   8244: inline void msdos_int_21h_67h()
                   8245: {
                   8246:        process_t *process = msdos_process_info_get(current_psp);
                   8247:        
                   8248:        if(REG16(BX) <= MAX_FILES) {
                   8249:                process->max_files = max(REG16(BX), 20);
                   8250:        } else {
                   8251:                REG16(AX) = 0x08;
1.1.1.3   root     8252:                m_CF = 1;
1.1       root     8253:        }
                   8254: }
                   8255: 
                   8256: inline void msdos_int_21h_68h()
                   8257: {
                   8258:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8259:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8260:        
1.1.1.20  root     8261:        if(fd < process->max_files && file_handler[fd].valid) {
                   8262:                // fflush(_fdopen(fd, ""));
1.1       root     8263:        } else {
                   8264:                REG16(AX) = 0x06;
1.1.1.3   root     8265:                m_CF = 1;
1.1       root     8266:        }
                   8267: }
                   8268: 
                   8269: inline void msdos_int_21h_69h()
                   8270: {
1.1.1.3   root     8271:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8272:        char path[] = "A:\\";
                   8273:        char volume_label[MAX_PATH];
                   8274:        DWORD serial_number = 0;
                   8275:        char file_system[MAX_PATH];
                   8276:        
                   8277:        if(REG8(BL) == 0) {
                   8278:                path[0] = 'A' + _getdrive() - 1;
                   8279:        } else {
                   8280:                path[0] = 'A' + REG8(BL) - 1;
                   8281:        }
                   8282:        
                   8283:        switch(REG8(AL)) {
                   8284:        case 0x00:
                   8285:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   8286:                        info->info_level = 0;
                   8287:                        info->serial_number = serial_number;
                   8288:                        memset(info->volume_label, 0x20, 11);
                   8289:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   8290:                        memset(info->file_system, 0x20, 8);
                   8291:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   8292:                } else {
                   8293:                        REG16(AX) = errno;
1.1.1.3   root     8294:                        m_CF = 1;
1.1       root     8295:                }
                   8296:                break;
                   8297:        case 0x01:
                   8298:                REG16(AX) = 0x03;
1.1.1.3   root     8299:                m_CF = 1;
1.1       root     8300:        }
                   8301: }
                   8302: 
                   8303: inline void msdos_int_21h_6ah()
                   8304: {
                   8305:        REG8(AH) = 0x68;
                   8306:        msdos_int_21h_68h();
                   8307: }
                   8308: 
                   8309: inline void msdos_int_21h_6bh()
                   8310: {
                   8311:        REG8(AL) = 0;
                   8312: }
                   8313: 
                   8314: inline void msdos_int_21h_6ch(int lfn)
                   8315: {
1.1.1.3   root     8316:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     8317:        int mode = REG8(BL) & 0x03;
                   8318:        
                   8319:        if(mode < 0x03) {
1.1.1.29  root     8320:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     8321:                        // file exists
                   8322:                        if(REG8(DL) & 1) {
1.1.1.29  root     8323:                                int fd = -1, c;
1.1.1.11  root     8324:                                UINT16 info;
1.1       root     8325:                                
1.1.1.11  root     8326:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     8327:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     8328:                                        info = 0x80d3;
1.1.1.29  root     8329:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8330:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   8331:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   8332:                                        }
1.1.1.14  root     8333:                                        info = 0x80d3;
1.1.1.29  root     8334:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8335:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   8336:                                        info = 0x80d3;
1.1.1.11  root     8337:                                } else {
1.1.1.13  root     8338:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     8339:                                        info = msdos_drive_number(path);
                   8340:                                }
1.1       root     8341:                                if(fd != -1) {
                   8342:                                        REG16(AX) = fd;
                   8343:                                        REG16(CX) = 1;
1.1.1.11  root     8344:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     8345:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8346:                                } else {
                   8347:                                        REG16(AX) = errno;
1.1.1.3   root     8348:                                        m_CF = 1;
1.1       root     8349:                                }
                   8350:                        } else if(REG8(DL) & 2) {
                   8351:                                int attr = GetFileAttributes(path);
1.1.1.29  root     8352:                                int fd = -1, c;
1.1.1.11  root     8353:                                UINT16 info;
1.1       root     8354:                                
1.1.1.11  root     8355:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     8356:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     8357:                                        info = 0x80d3;
1.1.1.29  root     8358:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8359:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   8360:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   8361:                                        }
1.1.1.14  root     8362:                                        info = 0x80d3;
1.1.1.29  root     8363:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8364:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   8365:                                        info = 0x80d3;
1.1       root     8366:                                } else {
                   8367:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     8368:                                        info = msdos_drive_number(path);
1.1       root     8369:                                }
                   8370:                                if(fd != -1) {
                   8371:                                        if(attr == -1) {
                   8372:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   8373:                                        }
                   8374:                                        SetFileAttributes(path, attr);
                   8375:                                        REG16(AX) = fd;
                   8376:                                        REG16(CX) = 3;
1.1.1.11  root     8377:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     8378:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8379:                                } else {
                   8380:                                        REG16(AX) = errno;
1.1.1.3   root     8381:                                        m_CF = 1;
1.1       root     8382:                                }
                   8383:                        } else {
                   8384:                                REG16(AX) = 0x50;
1.1.1.3   root     8385:                                m_CF = 1;
1.1       root     8386:                        }
                   8387:                } else {
                   8388:                        // file not exists
                   8389:                        if(REG8(DL) & 0x10) {
                   8390:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   8391:                                
                   8392:                                if(fd != -1) {
                   8393:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   8394:                                        REG16(AX) = fd;
                   8395:                                        REG16(CX) = 2;
                   8396:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     8397:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8398:                                } else {
                   8399:                                        REG16(AX) = errno;
1.1.1.3   root     8400:                                        m_CF = 1;
1.1       root     8401:                                }
                   8402:                        } else {
                   8403:                                REG16(AX) = 0x02;
1.1.1.3   root     8404:                                m_CF = 1;
1.1       root     8405:                        }
                   8406:                }
                   8407:        } else {
                   8408:                REG16(AX) = 0x0c;
1.1.1.3   root     8409:                m_CF = 1;
1.1       root     8410:        }
                   8411: }
                   8412: 
                   8413: inline void msdos_int_21h_710dh()
                   8414: {
                   8415:        // reset drive
                   8416: }
                   8417: 
1.1.1.17  root     8418: inline void msdos_int_21h_7141h(int lfn)
                   8419: {
                   8420:        if(REG16(SI) == 0) {
                   8421:                msdos_int_21h_41h(lfn);
                   8422:                return;
                   8423:        }
                   8424:        if(REG16(SI) != 1) {
                   8425:                REG16(AX) = 5;
                   8426:                m_CF = 1;
                   8427:        }
                   8428:        /* wild card and matching attributes... */
                   8429:        char tmp[MAX_PATH * 2];
                   8430:        // copy search pathname (and quick check overrun)
                   8431:        ZeroMemory(tmp, sizeof(tmp));
                   8432:        tmp[MAX_PATH - 1] = '\0';
                   8433:        tmp[MAX_PATH] = 1;
                   8434:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   8435:        
                   8436:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   8437:                REG16(AX) = 1;
                   8438:                m_CF = 1;
                   8439:                return;
                   8440:        }
                   8441:        for(char *s = tmp; *s; ++s) {
                   8442:                if(*s == '/') {
                   8443:                        *s = '\\';
                   8444:                }
                   8445:        }
                   8446:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   8447:        if(tmp_name) {
                   8448:                ++tmp_name;
                   8449:        } else {
                   8450:                tmp_name = strchr(tmp, ':');
                   8451:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   8452:        }
                   8453:        
                   8454:        WIN32_FIND_DATAA fd;
                   8455:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   8456:        if(fh == INVALID_HANDLE_VALUE) {
                   8457:                REG16(AX) = 2;
                   8458:                m_CF = 1;
                   8459:                return;
                   8460:        }
                   8461:        do {
                   8462:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   8463:                        strcpy(tmp_name, fd.cFileName);
                   8464:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   8465:                                REG16(AX) = 5;
                   8466:                                m_CF = 1;
                   8467:                                break;
                   8468:                        }
                   8469:                }
                   8470:        } while(FindNextFileA(fh, &fd));
                   8471:        if(!m_CF) {
                   8472:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   8473:                        m_CF = 1;
                   8474:                        REG16(AX) = 2;
                   8475:                }
                   8476:        }
                   8477:        FindClose(fh);
                   8478: }
                   8479: 
1.1       root     8480: inline void msdos_int_21h_714eh()
                   8481: {
                   8482:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8483:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   8484:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8485:        WIN32_FIND_DATA fd;
                   8486:        
1.1.1.13  root     8487:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   8488:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8489:                FindClose(dtainfo->find_handle);
                   8490:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8491:        }
                   8492:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8493:        dtainfo->allowable_mask = REG8(CL);
                   8494:        dtainfo->required_mask = REG8(CH);
                   8495:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8496:        
1.1.1.14  root     8497:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8498:                dtainfo->allowable_mask &= ~8;
1.1       root     8499:        }
1.1.1.14  root     8500:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8501:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8502:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8503:                                FindClose(dtainfo->find_handle);
                   8504:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8505:                                break;
                   8506:                        }
                   8507:                }
                   8508:        }
1.1.1.13  root     8509:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8510:                find->attrib = fd.dwFileAttributes;
                   8511:                msdos_find_file_conv_local_time(&fd);
                   8512:                if(REG16(SI) == 0) {
                   8513:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8514:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8515:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8516:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8517:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8518:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8519:                } else {
                   8520:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8521:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8522:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8523:                }
                   8524:                find->size_hi = fd.nFileSizeHigh;
                   8525:                find->size_lo = fd.nFileSizeLow;
                   8526:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8527:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8528:                REG16(AX) = dtainfo - dtalist + 1;
                   8529:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8530:                // volume label
                   8531:                find->attrib = 8;
                   8532:                find->size_hi = find->size_lo = 0;
                   8533:                strcpy(find->full_name, process->volume_label);
                   8534:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8535:                dtainfo->allowable_mask &= ~8;
                   8536:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     8537:        } else {
                   8538:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     8539:                m_CF = 1;
1.1       root     8540:        }
                   8541: }
                   8542: 
                   8543: inline void msdos_int_21h_714fh()
                   8544: {
                   8545:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8546:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8547:        WIN32_FIND_DATA fd;
                   8548:        
1.1.1.14  root     8549:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8550:                REG16(AX) = 6;
1.1.1.13  root     8551:                m_CF = 1;
                   8552:                return;
                   8553:        }
1.1.1.14  root     8554:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8555:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8556:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8557:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8558:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8559:                                        FindClose(dtainfo->find_handle);
                   8560:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8561:                                        break;
                   8562:                                }
                   8563:                        }
                   8564:                } else {
1.1.1.13  root     8565:                        FindClose(dtainfo->find_handle);
                   8566:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8567:                }
                   8568:        }
1.1.1.13  root     8569:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8570:                find->attrib = fd.dwFileAttributes;
                   8571:                msdos_find_file_conv_local_time(&fd);
                   8572:                if(REG16(SI) == 0) {
                   8573:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8574:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8575:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8576:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8577:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8578:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8579:                } else {
                   8580:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8581:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8582:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8583:                }
                   8584:                find->size_hi = fd.nFileSizeHigh;
                   8585:                find->size_lo = fd.nFileSizeLow;
                   8586:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8587:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8588:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8589:                // volume label
                   8590:                find->attrib = 8;
                   8591:                find->size_hi = find->size_lo = 0;
                   8592:                strcpy(find->full_name, process->volume_label);
                   8593:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8594:                dtainfo->allowable_mask &= ~8;
1.1       root     8595:        } else {
                   8596:                REG16(AX) = 0x12;
1.1.1.3   root     8597:                m_CF = 1;
1.1       root     8598:        }
                   8599: }
                   8600: 
                   8601: inline void msdos_int_21h_71a0h()
                   8602: {
                   8603:        DWORD max_component_len, file_sys_flag;
                   8604:        
1.1.1.14  root     8605:        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))) {
                   8606:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   8607:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     8608:                REG16(CX) = (UINT16)max_component_len;          // 255
                   8609:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   8610:        } else {
                   8611:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8612:                m_CF = 1;
1.1       root     8613:        }
                   8614: }
                   8615: 
                   8616: inline void msdos_int_21h_71a1h()
                   8617: {
1.1.1.14  root     8618:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8619:                REG16(AX) = 6;
1.1.1.13  root     8620:                m_CF = 1;
                   8621:                return;
                   8622:        }
1.1.1.14  root     8623:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8624:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8625:                FindClose(dtainfo->find_handle);
                   8626:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8627:        }
                   8628: }
                   8629: 
                   8630: inline void msdos_int_21h_71a6h()
                   8631: {
                   8632:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8633:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8634:        
1.1.1.3   root     8635:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8636:        struct _stat64 status;
                   8637:        DWORD serial_number = 0;
                   8638:        
1.1.1.20  root     8639:        if(fd < process->max_files && file_handler[fd].valid) {
                   8640:                if(_fstat64(fd, &status) == 0) {
                   8641:                        if(file_handler[fd].path[1] == ':') {
1.1       root     8642:                                // NOTE: we need to consider the network file path "\\host\share\"
                   8643:                                char volume[] = "A:\\";
1.1.1.20  root     8644:                                volume[0] = file_handler[fd].path[1];
1.1       root     8645:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   8646:                        }
1.1.1.20  root     8647:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     8648:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   8649:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   8650:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   8651:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   8652:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   8653:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   8654:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   8655:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   8656:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   8657:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     8658:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     8659:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     8660:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     8661:                } else {
                   8662:                        REG16(AX) = errno;
1.1.1.3   root     8663:                        m_CF = 1;
1.1       root     8664:                }
                   8665:        } else {
                   8666:                REG16(AX) = 0x06;
1.1.1.3   root     8667:                m_CF = 1;
1.1       root     8668:        }
                   8669: }
                   8670: 
                   8671: inline void msdos_int_21h_71a7h()
                   8672: {
                   8673:        switch(REG8(BL)) {
                   8674:        case 0x00:
1.1.1.3   root     8675:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     8676:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8677:                        m_CF = 1;
1.1       root     8678:                }
                   8679:                break;
                   8680:        case 0x01:
                   8681:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     8682:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     8683:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8684:                        m_CF = 1;
1.1       root     8685:                }
                   8686:                break;
                   8687:        default:
1.1.1.22  root     8688:                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     8689:                REG16(AX) = 0x01;
1.1.1.3   root     8690:                m_CF = 1;
1.1       root     8691:                break;
                   8692:        }
                   8693: }
                   8694: 
                   8695: inline void msdos_int_21h_71a8h()
                   8696: {
                   8697:        if(REG8(DH) == 0) {
                   8698:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     8699:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8700:                memset(fcb, 0x20, sizeof(fcb));
                   8701:                int len = strlen(tmp);
1.1.1.21  root     8702:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     8703:                        if(tmp[i] == '.') {
                   8704:                                pos = 8;
                   8705:                        } else {
                   8706:                                if(msdos_lead_byte_check(tmp[i])) {
                   8707:                                        fcb[pos++] = tmp[i++];
                   8708:                                }
                   8709:                                fcb[pos++] = tmp[i];
                   8710:                        }
                   8711:                }
1.1.1.3   root     8712:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     8713:        } else {
1.1.1.3   root     8714:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8715:        }
                   8716: }
                   8717: 
1.1.1.22  root     8718: inline void msdos_int_21h_71aah()
                   8719: {
                   8720:        char drv[] = "A:", path[MAX_PATH];
                   8721:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8722:        
                   8723:        if(REG8(BL) == 0) {
                   8724:                drv[0] = 'A' + _getdrive() - 1;
                   8725:        } else {
                   8726:                drv[0] = 'A' + REG8(BL) - 1;
                   8727:        }
                   8728:        switch(REG8(BH)) {
                   8729:        case 0x00:
                   8730:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   8731:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   8732:                        if(GetLogicalDrives() & bits) {
                   8733:                                REG16(AX) = 0x0f; // invalid drive
                   8734:                        } else {
                   8735:                                REG16(AX) = 0x03; // path not found
                   8736:                        }
                   8737:                        m_CF = 1;
                   8738:                }
                   8739:                break;
                   8740:        case 0x01:
                   8741:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   8742:                        REG16(AX) = 0x0f; // invalid drive
                   8743:                        m_CF = 1;
                   8744:                }
                   8745:                break;
                   8746:        case 0x02:
                   8747:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   8748:                        REG16(AX) = 0x0f; // invalid drive
                   8749:                        m_CF = 1;
                   8750:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   8751:                        REG16(AX) = 0x0f; // invalid drive
                   8752:                        m_CF = 1;
                   8753:                } else {
                   8754:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   8755:                }
                   8756:                break;
                   8757:        default:
                   8758:                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));
                   8759:                REG16(AX) = 0x01;
                   8760:                m_CF = 1;
                   8761:                break;
                   8762:        }
                   8763: }
                   8764: 
1.1.1.14  root     8765: inline void msdos_int_21h_7300h()
                   8766: {
                   8767:        if(REG8(AL) == 0) {
                   8768:                REG8(AL) = REG8(CL);
                   8769:                REG8(AH) = 0;
                   8770:        } else {
                   8771:                REG16(AX) = 0x01;
                   8772:                m_CF = 1;
                   8773:        }
                   8774: }
                   8775: 
                   8776: inline void msdos_int_21h_7302h()
                   8777: {
                   8778:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   8779:        UINT16 seg, ofs;
                   8780:        
                   8781:        if(REG16(CX) < 0x3f) {
                   8782:                REG8(AL) = 0x18;
                   8783:                m_CF = 1;
                   8784:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8785:                REG8(AL) = 0xff;
                   8786:                m_CF = 1;
                   8787:        } else {
                   8788:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   8789:        }
                   8790: }
                   8791: 
1.1       root     8792: inline void msdos_int_21h_7303h()
                   8793: {
1.1.1.3   root     8794:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8795:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8796:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   8797:        
                   8798:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   8799:                info->size_of_structure = sizeof(ext_space_info_t);
                   8800:                info->structure_version = 0;
                   8801:                info->sectors_per_cluster = sectors_per_cluster;
                   8802:                info->bytes_per_sector = bytes_per_sector;
                   8803:                info->available_clusters_on_drive = free_clusters;
                   8804:                info->total_clusters_on_drive = total_clusters;
                   8805:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   8806:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   8807:                info->available_allocation_units = free_clusters;       // ???
                   8808:                info->total_allocation_units = total_clusters;          // ???
                   8809:        } else {
                   8810:                REG16(AX) = errno;
1.1.1.3   root     8811:                m_CF = 1;
1.1       root     8812:        }
                   8813: }
                   8814: 
1.1.1.30! root     8815: inline void msdos_int_21h_dbh()
        !          8816: {
        !          8817:        // Novell NetWare - Workstation - Get Number of Local Drives
        !          8818:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
        !          8819:        REG8(AL) = dos_info->last_drive;
        !          8820: }
        !          8821: 
        !          8822: inline void msdos_int_21h_dch()
        !          8823: {
        !          8824:        // Novell NetWare - Connection Services - Get Connection Number
        !          8825:        REG8(AL) = 0x00;
        !          8826: }
        !          8827: 
1.1       root     8828: inline void msdos_int_25h()
                   8829: {
                   8830:        UINT16 seg, ofs;
                   8831:        DWORD dwSize;
                   8832:        
1.1.1.3   root     8833: #if defined(HAS_I386)
                   8834:        I386OP(pushf)();
                   8835: #else
                   8836:        PREFIX86(_pushf());
                   8837: #endif
1.1       root     8838:        
                   8839:        if(!(REG8(AL) < 26)) {
                   8840:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8841:                m_CF = 1;
1.1       root     8842:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8843:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8844:                m_CF = 1;
1.1       root     8845:        } else {
                   8846:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8847:                char dev[64];
                   8848:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8849:                
                   8850:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8851:                if(hFile == INVALID_HANDLE_VALUE) {
                   8852:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8853:                        m_CF = 1;
1.1       root     8854:                } else {
1.1.1.19  root     8855:                        UINT32 top_sector  = REG16(DX);
                   8856:                        UINT16 sector_num  = REG16(CX);
                   8857:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8858:                        
                   8859:                        if(sector_num == 0xffff) {
                   8860:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8861:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8862:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8863:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8864:                                buffer_addr = (seg << 4) + ofs;
                   8865:                        }
                   8866: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8867: //                             REG8(AL) = 0x02; // drive not ready
                   8868: //                             m_CF = 1;
                   8869: //                     } else 
                   8870:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8871:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8872:                                m_CF = 1;
1.1.1.19  root     8873:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8874:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     8875:                                m_CF = 1;
1.1       root     8876:                        }
                   8877:                        CloseHandle(hFile);
                   8878:                }
                   8879:        }
                   8880: }
                   8881: 
                   8882: inline void msdos_int_26h()
                   8883: {
                   8884:        // this operation may cause serious damage for drives, so always returns error...
                   8885:        UINT16 seg, ofs;
                   8886:        DWORD dwSize;
                   8887:        
1.1.1.3   root     8888: #if defined(HAS_I386)
                   8889:        I386OP(pushf)();
                   8890: #else
                   8891:        PREFIX86(_pushf());
                   8892: #endif
1.1       root     8893:        
                   8894:        if(!(REG8(AL) < 26)) {
                   8895:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8896:                m_CF = 1;
1.1       root     8897:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8898:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8899:                m_CF = 1;
1.1       root     8900:        } else {
                   8901:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8902:                char dev[64];
                   8903:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8904:                
                   8905:                if(dpb->media_type == 0xf8) {
                   8906:                        // this drive is not a floppy
1.1.1.6   root     8907: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   8908: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   8909: //                     }
1.1       root     8910:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8911:                        m_CF = 1;
1.1       root     8912:                } else {
                   8913:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8914:                        if(hFile == INVALID_HANDLE_VALUE) {
                   8915:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8916:                                m_CF = 1;
1.1       root     8917:                        } else {
1.1.1.19  root     8918:                                UINT32 top_sector  = REG16(DX);
                   8919:                                UINT16 sector_num  = REG16(CX);
                   8920:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8921:                                
                   8922:                                if(sector_num == 0xffff) {
                   8923:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8924:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8925:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8926:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8927:                                        buffer_addr = (seg << 4) + ofs;
                   8928:                                }
1.1       root     8929:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8930:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8931:                                        m_CF = 1;
1.1.1.19  root     8932:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8933:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8934:                                        m_CF = 1;
1.1.1.19  root     8935:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8936:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     8937:                                        m_CF = 1;
1.1       root     8938:                                }
                   8939:                                CloseHandle(hFile);
                   8940:                        }
                   8941:                }
                   8942:        }
                   8943: }
                   8944: 
                   8945: inline void msdos_int_27h()
                   8946: {
1.1.1.29  root     8947:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   8948:        try {
                   8949:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   8950:        } catch(...) {
                   8951:                // recover the broken mcb
                   8952:                int mcb_seg = SREG(CS) - 1;
                   8953:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   8954:                if(mcb_seg < (MEMORY_END >> 4)) {
                   8955:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
                   8956:                                mcb->mz = 'M';
                   8957:                        } else {
                   8958:                                mcb->mz = 'Z';
                   8959:                        }
1.1.1.30! root     8960:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
1.1.1.29  root     8961:                        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   8962:                } else {
                   8963:                        mcb->mz = 'Z';
1.1.1.30! root     8964:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     8965:                }
                   8966:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   8967:        }
1.1.1.3   root     8968:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     8969: }
                   8970: 
                   8971: inline void msdos_int_29h()
                   8972: {
1.1.1.14  root     8973: #if 1
                   8974:        // need to check escape sequences
1.1       root     8975:        msdos_putch(REG8(AL));
1.1.1.14  root     8976: #else
                   8977:        DWORD num;
                   8978:        vram_flush();
1.1.1.23  root     8979:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     8980:        cursor_moved = true;
                   8981: #endif
1.1       root     8982: }
                   8983: 
                   8984: inline void msdos_int_2eh()
                   8985: {
                   8986:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   8987:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8988:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     8989:        char *token = my_strtok(tmp, " ");
                   8990:        strcpy(command, token);
                   8991:        strcpy(opt, token + strlen(token) + 1);
                   8992:        
                   8993:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   8994:        param->env_seg = 0;
                   8995:        param->cmd_line.w.l = 44;
                   8996:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   8997:        param->fcb1.w.l = 24;
                   8998:        param->fcb1.w.h = (WORK_TOP >> 4);
                   8999:        param->fcb2.w.l = 24;
                   9000:        param->fcb2.w.h = (WORK_TOP >> 4);
                   9001:        
                   9002:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   9003:        
                   9004:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   9005:        cmd_line->len = strlen(opt);
                   9006:        strcpy(cmd_line->cmd, opt);
                   9007:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   9008:        
1.1.1.28  root     9009:        try {
                   9010:                if(msdos_process_exec(command, param, 0)) {
                   9011:                        REG16(AX) = 0xffff; // error before processing command
                   9012:                } else {
                   9013:                        // set flag to set retval to ax when the started process is terminated
                   9014:                        process_t *process = msdos_process_info_get(current_psp);
                   9015:                        process->called_by_int2eh = true;
                   9016:                }
                   9017:        } catch(...) {
                   9018:                REG16(AX) = 0xffff; // error before processing command
                   9019:        }
1.1       root     9020: }
                   9021: 
1.1.1.29  root     9022: inline void msdos_int_2fh_05h()
                   9023: {
                   9024:        switch(REG8(AL)) {
                   9025:        case 0x00:
                   9026:                // Critical Error Handler is not installed
                   9027: //             REG8(AL) = 0x00;
                   9028:                break;
                   9029:        default:
                   9030:                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));
                   9031:                // error code can't be converted to string
                   9032: //             REG16(AX) = 0x01;
                   9033:                m_CF = 1;
                   9034:                break;
                   9035:        }
                   9036: }
                   9037: 
1.1.1.22  root     9038: inline void msdos_int_2fh_11h()
                   9039: {
                   9040:        switch(REG8(AL)) {
                   9041:        case 0x00:
1.1.1.29  root     9042:                if(i386_read_stack() == 0xdada) {
                   9043:                        // MSCDEX is not installed
                   9044: //                     REG8(AL) = 0x00;
                   9045:                } else {
                   9046:                        // Network Redirector is not installed
                   9047: //                     REG8(AL) = 0x00;
                   9048:                }
1.1.1.22  root     9049:                break;
                   9050:        default:
                   9051:                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     9052:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     9053:                m_CF = 1;
                   9054:                break;
                   9055:        }
                   9056: }
                   9057: 
1.1.1.21  root     9058: inline void msdos_int_2fh_12h()
                   9059: {
                   9060:        switch(REG8(AL)) {
1.1.1.22  root     9061:        case 0x00:
1.1.1.29  root     9062:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     9063:                REG8(AL) = 0xff;
                   9064:                break;
1.1.1.29  root     9065: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   9066:        case 0x02:
                   9067:                {
                   9068:                        UINT16 stack = i386_read_stack();
                   9069:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   9070:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   9071:                        i386_load_segment_descriptor(ES);
                   9072:                }
                   9073:                break;
1.1.1.30! root     9074:        case 0x03:
        !          9075:                SREG(DS) = (DEVICE_TOP >> 4);
        !          9076:                i386_load_segment_descriptor(DS);
        !          9077:                break;
1.1.1.29  root     9078:        case 0x04:
                   9079:                {
                   9080:                        UINT16 stack = i386_read_stack();
                   9081:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   9082: #if defined(HAS_I386)
                   9083:                        m_ZF = (REG8(AL) == '\\');
                   9084: #else
                   9085:                        m_ZeroVal = (REG8(AL) != '\\');
                   9086: #endif
                   9087:                }
                   9088:                break;
                   9089:        case 0x05:
                   9090:                msdos_putch(i386_read_stack());
                   9091:                break;
                   9092: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   9093: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   9094: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   9095: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   9096: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   9097: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   9098: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   9099:        case 0x0d:
                   9100:                {
                   9101:                        SYSTEMTIME time;
                   9102:                        FILETIME file_time;
                   9103:                        WORD dos_date, dos_time;
                   9104:                        GetLocalTime(&time);
                   9105:                        SystemTimeToFileTime(&time, &file_time);
                   9106:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9107:                        REG16(AX) = dos_date;
                   9108:                        REG16(DX) = dos_time;
                   9109:                }
                   9110:                break;
                   9111: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   9112: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   9113: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   9114:        case 0x11:
                   9115:                {
                   9116:                        char path[MAX_PATH], *p;
                   9117:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   9118:                        my_strupr(path);
                   9119:                        while((p = my_strchr(path, '/')) != NULL) {
                   9120:                                *p = '\\';
                   9121:                        }
                   9122:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   9123:                }
                   9124:                break;
                   9125:        case 0x12:
                   9126:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   9127:                break;
                   9128:        case 0x13:
                   9129:                {
                   9130:                        char tmp[2] = {0};
                   9131:                        tmp[0] = i386_read_stack();
                   9132:                        my_strupr(tmp);
                   9133:                        REG8(AL) = tmp[0];
                   9134:                }
                   9135:                break;
                   9136:        case 0x14:
                   9137: #if defined(HAS_I386)
                   9138:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   9139: #else
                   9140:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   9141: #endif
                   9142:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   9143:                break;
                   9144: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     9145:        case 0x16:
                   9146:                if(REG16(BX) < 20) {
                   9147:                        SREG(ES) = SFT_TOP >> 4;
                   9148:                        i386_load_segment_descriptor(ES);
                   9149:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   9150:                        
                   9151:                        // update system file table
                   9152:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   9153:                        if(file_handler[REG16(BX)].valid) {
                   9154:                                int count = 0;
                   9155:                                for(int i = 0; i < 20; i++) {
                   9156:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   9157:                                                count++;
                   9158:                                        }
                   9159:                                }
                   9160:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   9161:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   9162:                                _lseek(REG16(BX), 0, SEEK_END);
                   9163:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   9164:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   9165:                        } else {
                   9166:                                memset(sft, 0, 0x3b);
                   9167:                        }
                   9168:                } else {
                   9169:                        REG16(AX) = 0x06;
                   9170:                        m_CF = 1;
                   9171:                }
                   9172:                break;
1.1.1.29  root     9173: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   9174: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   9175: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   9176:        case 0x1a:
                   9177:                {
                   9178:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   9179:                        if(path[1] == ':') {
                   9180:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   9181:                                        REG8(AL) = path[0] - 'a' + 1;
                   9182:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   9183:                                        REG8(AL) = path[0] - 'A' + 1;
                   9184:                                } else {
                   9185:                                        REG8(AL) = 0xff; // invalid
                   9186:                                }
                   9187:                                strcpy(full, path);
                   9188:                                strcpy(path, full + 2);
                   9189:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   9190:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   9191:                                        REG8(AL) = full[0] - 'a' + 1;
                   9192:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   9193:                                        REG8(AL) = full[0] - 'A' + 1;
                   9194:                                } else {
                   9195:                                        REG8(AL) = 0xff; // invalid
                   9196:                                }
                   9197:                        } else {
                   9198:                                REG8(AL) = 0x00; // default
                   9199:                        }
                   9200:                }
                   9201:                break;
                   9202:        case 0x1b:
                   9203:                {
                   9204:                        int year = REG16(CX) + 1980;
                   9205:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   9206:                }
                   9207:                break;
                   9208: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   9209: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   9210:        case 0x1e:
                   9211:                {
                   9212:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   9213:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   9214:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   9215: #if defined(HAS_I386)
                   9216:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   9217: #else
                   9218:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   9219: #endif
                   9220:                        } else {
                   9221: #if defined(HAS_I386)
                   9222:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   9223: #else
                   9224:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   9225: #endif
                   9226:                        }
                   9227:                }
                   9228:                break;
                   9229: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     9230:        case 0x20:
                   9231:                {
                   9232:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9233:                        
                   9234:                        if(fd < 20) {
                   9235:                                SREG(ES) = current_psp;
                   9236:                                i386_load_segment_descriptor(ES);
                   9237:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   9238:                        } else {
                   9239:                                REG16(AX) = 0x06;
                   9240:                                m_CF = 1;
                   9241:                        }
                   9242:                }
                   9243:                break;
1.1.1.29  root     9244:        case 0x21:
                   9245:                msdos_int_21h_60h(0);
                   9246:                break;
                   9247: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   9248: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   9249: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   9250:        case 0x25:
                   9251:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   9252:                break;
                   9253:        case 0x26:
                   9254:                REG8(AL) = REG8(CL);
                   9255:                msdos_int_21h_3dh();
                   9256:                break;
                   9257:        case 0x27:
                   9258:                msdos_int_21h_3eh();
                   9259:                break;
                   9260:        case 0x28:
                   9261:                REG16(AX) = REG16(BP);
                   9262:                msdos_int_21h_42h();
                   9263:                break;
                   9264:        case 0x29:
                   9265:                msdos_int_21h_3fh();
                   9266:                break;
                   9267: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   9268:        case 0x2b:
                   9269:                REG16(AX) = REG16(BP);
                   9270:                msdos_int_21h_44h();
                   9271:                break;
                   9272:        case 0x2c:
                   9273:                REG16(BX) = DEVICE_TOP >> 4;
                   9274:                REG16(AX) = 22;
                   9275:                break;
                   9276:        case 0x2d:
                   9277:                {
                   9278:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   9279:                        REG16(AX) = sda->extended_error_code;
                   9280:                }
                   9281:                break;
                   9282:        case 0x2e:
                   9283:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
                   9284:                        SREG(ES) = ERR_TABLE_TOP >> 4;
1.1.1.22  root     9285:                        i386_load_segment_descriptor(ES);
                   9286:                        REG16(DI) = 0;
                   9287:                }
                   9288:                break;
1.1.1.29  root     9289:        case 0x2f:
                   9290:                if(REG16(DX) != 0) {
1.1.1.30! root     9291:                        dos_major_version = REG8(DL);
        !          9292:                        dos_minor_version = REG8(DH);
1.1.1.29  root     9293:                } else {
                   9294:                        REG8(DL) = 7;
                   9295:                        REG8(DH) = 10;
                   9296:                }
                   9297:                break;
                   9298: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   9299: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     9300:        default:
                   9301:                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));
                   9302:                REG16(AX) = 0x01;
                   9303:                m_CF = 1;
                   9304:                break;
                   9305:        }
                   9306: }
                   9307: 
1.1.1.30! root     9308: inline void msdos_int_2fh_13h()
        !          9309: {
        !          9310:        static UINT16 prevDS = 0, prevDX = 0;
        !          9311:        static UINT16 prevES = 0, prevBX = 0;
        !          9312:        UINT16 tmp;
        !          9313:        
        !          9314:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
        !          9315:        i386_load_segment_descriptor(DS);
        !          9316:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
        !          9317:        
        !          9318:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
        !          9319:        i386_load_segment_descriptor(ES);
        !          9320:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
        !          9321: }
        !          9322: 
1.1.1.22  root     9323: inline void msdos_int_2fh_14h()
                   9324: {
                   9325:        switch(REG8(AL)) {
                   9326:        case 0x00:
1.1.1.29  root     9327:                // NLSFUNC.COM is installed
                   9328:                REG8(AL) = 0xff;
1.1.1.25  root     9329:                break;
                   9330:        case 0x01:
                   9331:        case 0x03:
                   9332:                REG8(AL) = 0x00;
                   9333:                active_code_page = REG16(BX);
                   9334:                msdos_nls_tables_update();
                   9335:                break;
                   9336:        case 0x02:
                   9337:                REG8(AL) = get_extended_country_info(REG16(BP));
                   9338:                break;
                   9339:        case 0x04:
                   9340:                REG8(AL) = 0x00;
                   9341:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     9342:                break;
                   9343:        default:
                   9344:                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));
                   9345:                REG16(AX) = 0x01;
                   9346:                m_CF = 1;
                   9347:                break;
                   9348:        }
                   9349: }
                   9350: 
                   9351: inline void msdos_int_2fh_15h()
                   9352: {
                   9353:        switch(REG8(AL)) {
1.1.1.29  root     9354:        case 0x00: // CD-ROM - Installation Check
                   9355:                if(REG16(BX) == 0x0000) {
                   9356:                        // MSCDEX is not installed
                   9357: //                     REG8(AL) = 0x00;
                   9358:                } else {
                   9359:                        // GRAPHICS.COM is not installed
                   9360: //                     REG8(AL) = 0x00;
                   9361:                }
1.1.1.22  root     9362:                break;
                   9363:        case 0xff:
1.1.1.29  root     9364:                if(REG16(BX) == 0x0000) {
                   9365:                        // CORELCDX is not installed
                   9366:                } else {
                   9367:                        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));
                   9368:                        REG16(AX) = 0x01;
                   9369:                        m_CF = 1;
                   9370:                }
1.1.1.22  root     9371:                break;
1.1.1.21  root     9372:        default:
1.1.1.22  root     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));
1.1.1.21  root     9374:                REG16(AX) = 0x01;
                   9375:                m_CF = 1;
                   9376:                break;
                   9377:        }
                   9378: }
                   9379: 
1.1       root     9380: inline void msdos_int_2fh_16h()
                   9381: {
                   9382:        switch(REG8(AL)) {
                   9383:        case 0x00:
1.1.1.14  root     9384:                if(no_windows) {
1.1.1.29  root     9385:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   9386: //                     REG8(AL) = 0x00;
1.1.1.14  root     9387:                } else {
1.1.1.30! root     9388:                        REG8(AL) = win_major_version;
        !          9389:                        REG8(AH) = win_minor_version;
1.1       root     9390:                }
                   9391:                break;
1.1.1.30! root     9392:        case 0x05:
        !          9393:                // from DOSBox
        !          9394:                i386_set_a20_line(1);
        !          9395:                break;
1.1.1.22  root     9396:        case 0x0a:
                   9397:                if(!no_windows) {
                   9398:                        REG16(AX) = 0x0000;
1.1.1.30! root     9399:                        REG8(BH) = win_major_version;
        !          9400:                        REG8(BL) = win_minor_version;
1.1.1.22  root     9401:                        REG16(CX) = 0x0003; // enhanced
                   9402:                }
                   9403:                break;
1.1.1.30! root     9404:        case 0x0b:
        !          9405:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     9406:        case 0x0e:
                   9407:        case 0x0f:
1.1.1.30! root     9408:        case 0x10:
1.1.1.22  root     9409:        case 0x11:
                   9410:        case 0x12:
                   9411:        case 0x13:
                   9412:        case 0x14:
1.1.1.30! root     9413:        case 0x15:
1.1.1.22  root     9414:        case 0x87:
1.1.1.30! root     9415:        case 0x89:
1.1.1.22  root     9416:                // function not supported, do not clear AX
                   9417:                break;
1.1.1.14  root     9418:        case 0x80:
                   9419:                Sleep(10);
                   9420:                hardware_update();
1.1.1.29  root     9421:                REG8(AL) = 0x00;
1.1.1.14  root     9422:                break;
1.1.1.22  root     9423:        case 0x8e:
                   9424:                REG16(AX) = 0x00; // failed
                   9425:                break;
1.1.1.20  root     9426:        case 0x8f:
                   9427:                switch(REG8(DH)) {
                   9428:                case 0x00:
                   9429:                case 0x02:
                   9430:                case 0x03:
                   9431:                        REG16(AX) = 0x00;
                   9432:                        break;
                   9433:                case 0x01:
                   9434:                        REG16(AX) = 0x168f;
                   9435:                        break;
                   9436:                }
                   9437:                break;
1.1       root     9438:        default:
1.1.1.22  root     9439:                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));
                   9440:                REG16(AX) = 0x01;
                   9441:                m_CF = 1;
                   9442:                break;
                   9443:        }
                   9444: }
                   9445: 
                   9446: inline void msdos_int_2fh_19h()
                   9447: {
                   9448:        switch(REG8(AL)) {
                   9449:        case 0x00:
1.1.1.29  root     9450:                // SHELLB.COM is not installed
                   9451: //             REG8(AL) = 0x00;
1.1.1.22  root     9452:                break;
                   9453:        case 0x01:
                   9454:        case 0x02:
                   9455:        case 0x03:
                   9456:        case 0x04:
                   9457:                REG16(AX) = 0x01;
                   9458:                m_CF = 1;
                   9459:                break;
1.1.1.29  root     9460:        case 0x80:
                   9461:                // IBM ROM-DOS v4.0 is not installed
                   9462: //             REG8(AL) = 0x00;
                   9463:                break;
1.1.1.22  root     9464:        default:
                   9465:                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     9466:                REG16(AX) = 0x01;
1.1.1.3   root     9467:                m_CF = 1;
1.1       root     9468:                break;
                   9469:        }
                   9470: }
                   9471: 
                   9472: inline void msdos_int_2fh_1ah()
                   9473: {
                   9474:        switch(REG8(AL)) {
                   9475:        case 0x00:
1.1.1.29  root     9476:                // ANSI.SYS is installed
1.1       root     9477:                REG8(AL) = 0xff;
                   9478:                break;
                   9479:        default:
1.1.1.22  root     9480:                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));
                   9481:                REG16(AX) = 0x01;
                   9482:                m_CF = 1;
                   9483:                break;
                   9484:        }
                   9485: }
                   9486: 
1.1.1.30! root     9487: inline void msdos_int_2fh_40h()
1.1.1.22  root     9488: {
                   9489:        switch(REG8(AL)) {
                   9490:        case 0x00:
1.1.1.30! root     9491:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
        !          9492:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     9493:                break;
                   9494:        default:
                   9495:                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     9496:                REG16(AX) = 0x01;
1.1.1.3   root     9497:                m_CF = 1;
1.1       root     9498:                break;
                   9499:        }
                   9500: }
                   9501: 
                   9502: inline void msdos_int_2fh_43h()
                   9503: {
                   9504:        switch(REG8(AL)) {
                   9505:        case 0x00:
1.1.1.29  root     9506:                // XMS is installed ?
1.1.1.19  root     9507: #ifdef SUPPORT_XMS
                   9508:                if(support_xms) {
                   9509:                        REG8(AL) = 0x80;
                   9510:                } else
                   9511: #endif
                   9512:                REG8(AL) = 0x00;
                   9513:                break;
                   9514:        case 0x10:
                   9515:                SREG(ES) = XMS_TOP >> 4;
                   9516:                i386_load_segment_descriptor(ES);
1.1.1.26  root     9517:                REG16(BX) = 0x15;
1.1       root     9518:                break;
                   9519:        default:
1.1.1.22  root     9520:                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));
                   9521:                REG16(AX) = 0x01;
                   9522:                m_CF = 1;
                   9523:                break;
                   9524:        }
                   9525: }
                   9526: 
                   9527: inline void msdos_int_2fh_46h()
                   9528: {
                   9529:        switch(REG8(AL)) {
                   9530:        case 0x80:
1.1.1.29  root     9531:                // Windows v3.0 is not installed
                   9532: //             REG8(AL) = 0x00;
1.1.1.22  root     9533:                break;
                   9534:        default:
                   9535:                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));
                   9536:                REG16(AX) = 0x01;
                   9537:                m_CF = 1;
                   9538:                break;
                   9539:        }
                   9540: }
                   9541: 
                   9542: inline void msdos_int_2fh_48h()
                   9543: {
                   9544:        switch(REG8(AL)) {
                   9545:        case 0x00:
1.1.1.29  root     9546:                // DOSKEY is not installed
                   9547: //             REG8(AL) = 0x00;
1.1.1.22  root     9548:                break;
                   9549:        case 0x10:
                   9550:                msdos_int_21h_0ah();
                   9551:                REG16(AX) = 0x00;
                   9552:                break;
                   9553:        default:
                   9554:                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     9555:                REG16(AX) = 0x01;
1.1.1.3   root     9556:                m_CF = 1;
1.1       root     9557:                break;
                   9558:        }
                   9559: }
                   9560: 
                   9561: inline void msdos_int_2fh_4ah()
                   9562: {
                   9563:        switch(REG8(AL)) {
1.1.1.29  root     9564: #ifdef SUPPORT_HMA
                   9565:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   9566:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   9567:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9568:                                // restore first free mcb in high memory area
                   9569:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9570:                        }
                   9571:                        int offset = 0xffff;
                   9572:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   9573:                                REG16(DI) = offset + 0x10;
                   9574:                        } else {
                   9575:                                REG16(DI) = 0xffff;
                   9576:                        }
                   9577:                } else {
                   9578:                        // HMA is already used
                   9579:                        REG16(BX) = 0;
                   9580:                        REG16(DI) = 0xffff;
                   9581:                }
                   9582:                SREG(ES) = 0xffff;
                   9583:                i386_load_segment_descriptor(ES);
                   9584:                break;
                   9585:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   9586:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   9587:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9588:                                // restore first free mcb in high memory area
                   9589:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9590:                        }
                   9591:                        int size = REG16(BX), offset;
                   9592:                        if((size % 16) != 0) {
                   9593:                                size &= ~15;
                   9594:                                size += 16;
                   9595:                        }
                   9596:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   9597:                                REG16(BX) = size;
                   9598:                                REG16(DI) = offset + 0x10;
                   9599:                                is_hma_used_by_int_2fh = true;
                   9600:                        } else {
                   9601:                                REG16(BX) = 0;
                   9602:                                REG16(DI) = 0xffff;
                   9603:                        }
                   9604:                } else {
                   9605:                        // HMA is already used
                   9606:                        REG16(BX) = 0;
                   9607:                        REG16(DI) = 0xffff;
                   9608:                }
                   9609:                SREG(ES) = 0xffff;
                   9610:                i386_load_segment_descriptor(ES);
                   9611:                break;
                   9612:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   9613:                if(REG8(DL) == 0x00) {
                   9614:                        if(!is_hma_used_by_xms) {
                   9615:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9616:                                        // restore first free mcb in high memory area
                   9617:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9618:                                        is_hma_used_by_int_2fh = false;
                   9619:                                }
                   9620:                                int size = REG16(BX), offset;
                   9621:                                if((size % 16) != 0) {
                   9622:                                        size &= ~15;
                   9623:                                        size += 16;
                   9624:                                }
                   9625:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   9626: //                                     REG16(BX) = size;
                   9627:                                        SREG(ES) = 0xffff;
                   9628:                                        i386_load_segment_descriptor(ES);
                   9629:                                        REG16(DI) = offset + 0x10;
                   9630:                                        is_hma_used_by_int_2fh = true;
                   9631:                                } else {
                   9632:                                        REG16(DI) = 0xffff;
                   9633:                                }
                   9634:                        } else {
                   9635:                                REG16(DI) = 0xffff;
                   9636:                        }
                   9637:                } else if(REG8(DL) == 0x01) {
                   9638:                        if(!is_hma_used_by_xms) {
                   9639:                                int size = REG16(BX);
                   9640:                                if((size % 16) != 0) {
                   9641:                                        size &= ~15;
                   9642:                                        size += 16;
                   9643:                                }
                   9644:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   9645:                                        // memory block address is not changed
                   9646:                                } else {
                   9647:                                        REG16(DI) = 0xffff;
                   9648:                                }
                   9649:                        } else {
                   9650:                                REG16(DI) = 0xffff;
                   9651:                        }
                   9652:                } else if(REG8(DL) == 0x02) {
                   9653:                        if(!is_hma_used_by_xms) {
                   9654:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9655:                                        // restore first free mcb in high memory area
                   9656:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9657:                                        is_hma_used_by_int_2fh = false;
                   9658:                                } else {
                   9659:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   9660:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   9661:                                                is_hma_used_by_int_2fh = false;
                   9662:                                        }
                   9663:                                }
                   9664:                        }
                   9665:                } else {
                   9666:                        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));
                   9667:                        REG16(AX) = 0x01;
                   9668:                        m_CF = 1;
                   9669:                }
                   9670:                break;
                   9671:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   9672:                if(!is_hma_used_by_xms) {
                   9673:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   9674:                                // restore first free mcb in high memory area
                   9675:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   9676:                                is_hma_used_by_int_2fh = false;
                   9677:                        }
                   9678:                        REG16(AX) = 0x0000;
                   9679:                        SREG(ES) = 0xffff;
                   9680:                        i386_load_segment_descriptor(ES);
                   9681:                        REG16(DI) = 0x10;
                   9682:                }
                   9683:                break;
                   9684: #else
1.1       root     9685:        case 0x01:
                   9686:        case 0x02:
1.1.1.29  root     9687:                // HMA is already used
1.1.1.27  root     9688:                REG16(BX) = 0x0000;
1.1.1.3   root     9689:                SREG(ES) = 0xffff;
                   9690:                i386_load_segment_descriptor(ES);
1.1       root     9691:                REG16(DI) = 0xffff;
                   9692:                break;
1.1.1.19  root     9693:        case 0x03:
                   9694:                // unable to allocate
                   9695:                REG16(DI) = 0xffff;
                   9696:                break;
                   9697:        case 0x04:
                   9698:                // function not supported, do not clear AX
                   9699:                break;
1.1.1.29  root     9700: #endif
                   9701:        case 0x10:
                   9702:                if(REG16(BX) == 0x0000) {
                   9703:                        // SMARTDRV is not installed
                   9704:                } else {
                   9705:                        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));
                   9706:                        REG16(AX) = 0x01;
                   9707:                        m_CF = 1;
                   9708:                }
                   9709:                break;
                   9710:        case 0x11:
                   9711:                if(REG16(BX) == 0x0000) {
                   9712:                        // DBLSPACE.BIN is not installed
                   9713:                } else {
                   9714:                        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));
                   9715:                        REG16(AX) = 0x01;
                   9716:                        m_CF = 1;
                   9717:                }
1.1.1.22  root     9718:                break;
                   9719:        default:
                   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:                break;
                   9724:        }
                   9725: }
                   9726: 
                   9727: inline void msdos_int_2fh_4bh()
                   9728: {
                   9729:        switch(REG8(AL)) {
1.1.1.24  root     9730:        case 0x01:
1.1.1.22  root     9731:        case 0x02:
1.1.1.29  root     9732:                // Task Switcher is not installed
1.1.1.24  root     9733:                break;
                   9734:        case 0x03:
                   9735:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   9736:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     9737:                break;
1.1.1.30! root     9738:        case 0x04:
        !          9739:                REG16(BX) = 0x0000; // free switcher id successfully
        !          9740:                break;
1.1       root     9741:        default:
1.1.1.22  root     9742:                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     9743:                REG16(AX) = 0x01;
1.1.1.3   root     9744:                m_CF = 1;
1.1       root     9745:                break;
                   9746:        }
                   9747: }
                   9748: 
                   9749: inline void msdos_int_2fh_4fh()
                   9750: {
                   9751:        switch(REG8(AL)) {
                   9752:        case 0x00:
1.1.1.29  root     9753:                // BILING is installed
1.1.1.27  root     9754:                REG16(AX) = 0x0000;
                   9755:                REG8(DL) = 0x01;        // major version
                   9756:                REG8(DH) = 0x00;        // minor version
1.1       root     9757:                break;
                   9758:        case 0x01:
1.1.1.27  root     9759:                REG16(AX) = 0x0000;
1.1       root     9760:                REG16(BX) = active_code_page;
                   9761:                break;
                   9762:        default:
1.1.1.22  root     9763:                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));
                   9764:                REG16(AX) = 0x01;
                   9765:                m_CF = 1;
                   9766:                break;
                   9767:        }
                   9768: }
                   9769: 
                   9770: inline void msdos_int_2fh_55h()
                   9771: {
                   9772:        switch(REG8(AL)) {
                   9773:        case 0x00:
                   9774:        case 0x01:
                   9775: //             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));
                   9776:                break;
                   9777:        default:
                   9778:                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     9779:                REG16(AX) = 0x01;
1.1.1.3   root     9780:                m_CF = 1;
1.1       root     9781:                break;
                   9782:        }
                   9783: }
                   9784: 
1.1.1.30! root     9785: inline void msdos_int_2fh_80h()
1.1.1.29  root     9786: {
                   9787:        switch(REG8(AL)) {
                   9788:        case 0x00:
1.1.1.30! root     9789:                if(REG16(DX) == 0x0000) {
        !          9790:                        // FAX BIOS is not installed
        !          9791: //                     REG8(AL) = 0x00;
        !          9792:                } else {
        !          9793:                        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));
        !          9794:                        REG16(AX) = 0x01;
        !          9795:                        m_CF = 1;
        !          9796:                }
1.1.1.29  root     9797:                break;
                   9798:        default:
                   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:                break;
                   9803:        }
                   9804: }
                   9805: 
1.1.1.24  root     9806: inline void msdos_int_2fh_adh()
                   9807: {
                   9808:        switch(REG8(AL)) {
                   9809:        case 0x00:
1.1.1.29  root     9810:                // DISPLAY.SYS is installed
1.1.1.24  root     9811:                REG8(AL) = 0xff;
                   9812:                REG16(BX) = 0x100; // ???
                   9813:                break;
                   9814:        case 0x01:
                   9815:                active_code_page = REG16(BX);
                   9816:                msdos_nls_tables_update();
                   9817:                REG16(AX) = 0x01;
                   9818:                break;
                   9819:        case 0x02:
                   9820:                REG16(BX) = active_code_page;
                   9821:                break;
                   9822:        case 0x03:
                   9823:                // FIXME
                   9824:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   9825:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   9826:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   9827:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   9828:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   9829:                break;
                   9830:        case 0x80:
                   9831:                break; // keyb.com is not installed
                   9832:        default:
                   9833:                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));
                   9834:                REG16(AX) = 0x01;
                   9835:                m_CF = 1;
                   9836:                break;
                   9837:        }
                   9838: }
                   9839: 
1.1       root     9840: inline void msdos_int_2fh_aeh()
                   9841: {
                   9842:        switch(REG8(AL)) {
                   9843:        case 0x00:
1.1.1.28  root     9844:                // FIXME: we need to check the given command line
                   9845:                REG8(AL) = 0x00; // the command should be executed as usual
                   9846: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     9847:                break;
                   9848:        case 0x01:
                   9849:                {
                   9850:                        char command[MAX_PATH];
                   9851:                        memset(command, 0, sizeof(command));
1.1.1.3   root     9852:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     9853:                        
                   9854:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   9855:                        param->env_seg = 0;
                   9856:                        param->cmd_line.w.l = 44;
                   9857:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   9858:                        param->fcb1.w.l = 24;
                   9859:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   9860:                        param->fcb2.w.l = 24;
                   9861:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   9862:                        
                   9863:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   9864:                        
                   9865:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     9866:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   9867:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     9868:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   9869:                        
1.1.1.28  root     9870:                        try {
                   9871:                                msdos_process_exec(command, param, 0);
                   9872:                        } catch(...) {
                   9873:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     9874:                        }
                   9875:                }
                   9876:                break;
                   9877:        default:
1.1.1.22  root     9878:                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     9879:                REG16(AX) = 0x01;
1.1.1.3   root     9880:                m_CF = 1;
1.1       root     9881:                break;
                   9882:        }
                   9883: }
                   9884: 
                   9885: inline void msdos_int_2fh_b7h()
                   9886: {
                   9887:        switch(REG8(AL)) {
                   9888:        case 0x00:
1.1.1.30! root     9889:                // APPEND is not installed
1.1.1.29  root     9890: //             REG8(AL) = 0x00;
1.1       root     9891:                break;
1.1.1.22  root     9892:        case 0x07:
                   9893:        case 0x11:
                   9894:                break;
1.1       root     9895:        default:
1.1.1.22  root     9896:                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     9897:                REG16(AX) = 0x01;
1.1.1.3   root     9898:                m_CF = 1;
1.1       root     9899:                break;
                   9900:        }
                   9901: }
                   9902: 
1.1.1.30! root     9903: inline void msdos_int_2fh_d7h()
1.1.1.29  root     9904: {
                   9905:        switch(REG8(AL)) {
1.1.1.30! root     9906:        case 0x01:
        !          9907:                // Banyan VINES v4+ is not installed
1.1.1.29  root     9908:                break;
                   9909:        default:
                   9910:                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));
                   9911:                REG16(AX) = 0x01;
                   9912:                m_CF = 1;
                   9913:                break;
                   9914:        }
                   9915: }
                   9916: 
1.1.1.24  root     9917: inline void msdos_int_33h_0000h()
                   9918: {
                   9919:        REG16(AX) = 0xffff; // hardware/driver installed
                   9920:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   9921: }
                   9922: 
                   9923: inline void msdos_int_33h_0001h()
                   9924: {
                   9925:        if(!mouse.active) {
                   9926:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9927:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   9928:                }
                   9929:                mouse.active = true;
                   9930:                pic[1].imr &= ~0x10;    // enable irq12
                   9931:        }
                   9932: }
                   9933: 
                   9934: inline void msdos_int_33h_0002h()
                   9935: {
                   9936:        if(mouse.active) {
                   9937:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9938:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   9939:                }
                   9940:                mouse.active = false;
                   9941:                pic[1].imr |= 0x10;     // disable irq12
                   9942:        }
                   9943: }
                   9944: 
                   9945: inline void msdos_int_33h_0003h()
                   9946: {
                   9947:        REG16(BX) = mouse.get_buttons();
                   9948:        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   9949:        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   9950: }
                   9951: 
                   9952: inline void msdos_int_33h_0005h()
                   9953: {
                   9954:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9955:                int idx = REG16(BX);
                   9956:                REG16(BX) = mouse.buttons[idx].pressed_times;
                   9957:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].pressed_position.x));
                   9958:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].pressed_position.y));
                   9959:                mouse.buttons[idx].pressed_times = 0;
                   9960:        } else {
                   9961:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9962:        }
                   9963:        REG16(AX) = mouse.get_buttons();
                   9964: }
                   9965: 
                   9966: inline void msdos_int_33h_0006h()
                   9967: {
                   9968:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9969:                int idx = REG16(BX);
                   9970:                REG16(BX) = mouse.buttons[idx].released_times;
                   9971:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].released_position.x));
                   9972:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].released_position.y));
                   9973:                mouse.buttons[idx].released_times = 0;
                   9974:        } else {
                   9975:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9976:        }
                   9977:        REG16(AX) = mouse.get_buttons();
                   9978: }
                   9979: 
                   9980: inline void msdos_int_33h_0007h()
                   9981: {
                   9982:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   9983:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   9984: }
                   9985: 
                   9986: inline void msdos_int_33h_0008h()
                   9987: {
                   9988:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   9989:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   9990: }
                   9991: 
                   9992: inline void msdos_int_33h_0009h()
                   9993: {
                   9994:        mouse.hot_spot[0] = REG16(BX);
                   9995:        mouse.hot_spot[1] = REG16(CX);
                   9996: }
                   9997: 
                   9998: inline void msdos_int_33h_000bh()
                   9999: {
                   10000:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   10001:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   10002:        mouse.prev_position.x = mouse.position.x;
                   10003:        mouse.prev_position.y = mouse.position.y;
                   10004:        REG16(CX) = dx;
                   10005:        REG16(DX) = dy;
                   10006: }
                   10007: 
                   10008: inline void msdos_int_33h_000ch()
                   10009: {
                   10010:        mouse.call_mask = REG16(CX);
                   10011:        mouse.call_addr.w.l = REG16(DX);
                   10012:        mouse.call_addr.w.h = SREG(ES);
                   10013: }
                   10014: 
                   10015: inline void msdos_int_33h_000fh()
                   10016: {
                   10017:        mouse.mickey.x = REG16(CX);
                   10018:        mouse.mickey.y = REG16(DX);
                   10019: }
                   10020: 
                   10021: inline void msdos_int_33h_0011h()
                   10022: {
                   10023:        REG16(AX) = 0xffff;
                   10024:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   10025: }
                   10026: 
                   10027: inline void msdos_int_33h_0014h()
                   10028: {
                   10029:        UINT16 old_mask = mouse.call_mask;
                   10030:        UINT16 old_ofs = mouse.call_addr.w.l;
                   10031:        UINT16 old_seg = mouse.call_addr.w.h;
                   10032:        
                   10033:        mouse.call_mask = REG16(CX);
                   10034:        mouse.call_addr.w.l = REG16(DX);
                   10035:        mouse.call_addr.w.h = SREG(ES);
                   10036:        
                   10037:        REG16(CX) = old_mask;
                   10038:        REG16(DX) = old_ofs;
                   10039:        SREG(ES) = old_seg;
                   10040:        i386_load_segment_descriptor(ES);
                   10041: }
                   10042: 
                   10043: inline void msdos_int_33h_0015h()
                   10044: {
                   10045:        REG16(BX) = sizeof(mouse);
                   10046: }
                   10047: 
                   10048: inline void msdos_int_33h_0016h()
                   10049: {
                   10050:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   10051: }
                   10052: 
                   10053: inline void msdos_int_33h_0017h()
                   10054: {
                   10055:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   10056: }
                   10057: 
                   10058: inline void msdos_int_33h_001ah()
                   10059: {
                   10060:        mouse.sensitivity[0] = REG16(BX);
                   10061:        mouse.sensitivity[1] = REG16(CX);
                   10062:        mouse.sensitivity[2] = REG16(DX);
                   10063: }
                   10064: 
                   10065: inline void msdos_int_33h_001bh()
                   10066: {
                   10067:        REG16(BX) = mouse.sensitivity[0];
                   10068:        REG16(CX) = mouse.sensitivity[1];
                   10069:        REG16(DX) = mouse.sensitivity[2];
                   10070: }
                   10071: 
                   10072: inline void msdos_int_33h_001dh()
                   10073: {
                   10074:        mouse.display_page = REG16(BX);
                   10075: }
                   10076: 
                   10077: inline void msdos_int_33h_001eh()
                   10078: {
                   10079:        REG16(BX) = mouse.display_page;
                   10080: }
                   10081: 
                   10082: inline void msdos_int_33h_0021h()
                   10083: {
                   10084:        REG16(AX) = 0xffff;
                   10085:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   10086: }
                   10087: 
                   10088: inline void msdos_int_33h_0022h()
                   10089: {
                   10090:        mouse.language = REG16(BX);
                   10091: }
                   10092: 
                   10093: inline void msdos_int_33h_0023h()
                   10094: {
                   10095:        REG16(BX) = mouse.language;
                   10096: }
                   10097: 
                   10098: inline void msdos_int_33h_0024h()
                   10099: {
                   10100:        REG16(BX) = 0x0805; // V8.05
                   10101:        REG16(CX) = 0x0400; // PS/2
                   10102: }
                   10103: 
                   10104: inline void msdos_int_33h_0026h()
                   10105: {
                   10106:        REG16(BX) = 0x0000;
                   10107:        REG16(CX) = mouse.max_position.x;
                   10108:        REG16(DX) = mouse.max_position.y;
                   10109: }
                   10110: 
                   10111: inline void msdos_int_33h_002ah()
                   10112: {
                   10113:        REG16(AX) = mouse.active ? 0 : 0xffff;
                   10114:        REG16(BX) = mouse.hot_spot[0];
                   10115:        REG16(CX) = mouse.hot_spot[1];
                   10116:        REG16(DX) = 4; // PS/2
                   10117: }
                   10118: 
                   10119: inline void msdos_int_33h_0031h()
                   10120: {
                   10121:        REG16(AX) = mouse.min_position.x;
                   10122:        REG16(BX) = mouse.min_position.y;
                   10123:        REG16(CX) = mouse.max_position.x;
                   10124:        REG16(DX) = mouse.max_position.y;
                   10125: }
                   10126: 
                   10127: inline void msdos_int_33h_0032h()
                   10128: {
                   10129:        REG16(AX) = 0;
                   10130: //     REG16(AX) |= 0x8000; // 0025h
                   10131:        REG16(AX) |= 0x4000; // 0026h
                   10132: //     REG16(AX) |= 0x2000; // 0027h
                   10133: //     REG16(AX) |= 0x1000; // 0028h
                   10134: //     REG16(AX) |= 0x0800; // 0029h
                   10135:        REG16(AX) |= 0x0400; // 002ah
                   10136: //     REG16(AX) |= 0x0200; // 002bh
                   10137: //     REG16(AX) |= 0x0100; // 002ch
                   10138: //     REG16(AX) |= 0x0080; // 002dh
                   10139: //     REG16(AX) |= 0x0040; // 002eh
                   10140:        REG16(AX) |= 0x0020; // 002fh
                   10141: //     REG16(AX) |= 0x0010; // 0030h
                   10142:        REG16(AX) |= 0x0008; // 0031h
                   10143:        REG16(AX) |= 0x0004; // 0032h
                   10144: //     REG16(AX) |= 0x0002; // 0033h
                   10145: //     REG16(AX) |= 0x0001; // 0034h
                   10146: }
                   10147: 
1.1.1.19  root     10148: inline void msdos_int_67h_40h()
                   10149: {
                   10150:        if(!support_ems) {
                   10151:                REG8(AH) = 0x84;
                   10152:        } else {
                   10153:                REG8(AH) = 0x00;
                   10154:        }
                   10155: }
                   10156: 
                   10157: inline void msdos_int_67h_41h()
                   10158: {
                   10159:        if(!support_ems) {
                   10160:                REG8(AH) = 0x84;
                   10161:        } else {
                   10162:                REG8(AH) = 0x00;
                   10163:                REG16(BX) = EMS_TOP >> 4;
                   10164:        }
                   10165: }
                   10166: 
                   10167: inline void msdos_int_67h_42h()
                   10168: {
                   10169:        if(!support_ems) {
                   10170:                REG8(AH) = 0x84;
                   10171:        } else {
                   10172:                REG8(AH) = 0x00;
                   10173:                REG16(BX) = free_ems_pages;
                   10174:                REG16(DX) = MAX_EMS_PAGES;
                   10175:        }
                   10176: }
                   10177: 
                   10178: inline void msdos_int_67h_43h()
                   10179: {
                   10180:        if(!support_ems) {
                   10181:                REG8(AH) = 0x84;
                   10182:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10183:                REG8(AH) = 0x87;
                   10184:        } else if(REG16(BX) > free_ems_pages) {
                   10185:                REG8(AH) = 0x88;
                   10186:        } else if(REG16(BX) == 0) {
                   10187:                REG8(AH) = 0x89;
                   10188:        } else {
                   10189:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10190:                        if(!ems_handles[i].allocated) {
                   10191:                                ems_allocate_pages(i, REG16(BX));
                   10192:                                REG8(AH) = 0x00;
                   10193:                                REG16(DX) = i;
                   10194:                                return;
                   10195:                        }
                   10196:                }
                   10197:                REG8(AH) = 0x85;
                   10198:        }
                   10199: }
                   10200: 
                   10201: inline void msdos_int_67h_44h()
                   10202: {
                   10203:        if(!support_ems) {
                   10204:                REG8(AH) = 0x84;
                   10205:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10206:                REG8(AH) = 0x83;
                   10207:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   10208:                REG8(AH) = 0x8a;
                   10209: //     } else if(!(REG8(AL) < 4)) {
                   10210: //             REG8(AH) = 0x8b;
                   10211:        } else if(REG16(BX) == 0xffff) {
                   10212:                ems_unmap_page(REG8(AL) & 3);
                   10213:                REG8(AH) = 0x00;
                   10214:        } else {
                   10215:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   10216:                REG8(AH) = 0x00;
                   10217:        }
                   10218: }
                   10219: 
                   10220: inline void msdos_int_67h_45h()
                   10221: {
                   10222:        if(!support_ems) {
                   10223:                REG8(AH) = 0x84;
                   10224:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10225:                REG8(AH) = 0x83;
                   10226:        } else {
                   10227:                ems_release_pages(REG16(DX));
                   10228:                REG8(AH) = 0x00;
                   10229:        }
                   10230: }
                   10231: 
                   10232: inline void msdos_int_67h_46h()
                   10233: {
                   10234:        if(!support_ems) {
                   10235:                REG8(AH) = 0x84;
                   10236:        } else {
1.1.1.29  root     10237: //             REG16(AX) = 0x0032; // EMS 3.2
                   10238:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     10239:        }
                   10240: }
                   10241: 
                   10242: inline void msdos_int_67h_47h()
                   10243: {
                   10244:        // NOTE: the map data should be stored in the specified ems page, not process data
                   10245:        process_t *process = msdos_process_info_get(current_psp);
                   10246:        
                   10247:        if(!support_ems) {
                   10248:                REG8(AH) = 0x84;
                   10249: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10250: //             REG8(AH) = 0x83;
                   10251:        } else if(process->ems_pages_stored) {
                   10252:                REG8(AH) = 0x8d;
                   10253:        } else {
                   10254:                for(int i = 0; i < 4; i++) {
                   10255:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   10256:                        process->ems_pages[i].page   = ems_pages[i].page;
                   10257:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   10258:                }
                   10259:                process->ems_pages_stored = true;
                   10260:                REG8(AH) = 0x00;
                   10261:        }
                   10262: }
                   10263: 
                   10264: inline void msdos_int_67h_48h()
                   10265: {
                   10266:        // NOTE: the map data should be restored from the specified ems page, not process data
                   10267:        process_t *process = msdos_process_info_get(current_psp);
                   10268:        
                   10269:        if(!support_ems) {
                   10270:                REG8(AH) = 0x84;
                   10271: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10272: //             REG8(AH) = 0x83;
                   10273:        } else if(!process->ems_pages_stored) {
                   10274:                REG8(AH) = 0x8e;
                   10275:        } else {
                   10276:                for(int i = 0; i < 4; i++) {
                   10277:                        if(process->ems_pages[i].mapped) {
                   10278:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   10279:                        } else {
                   10280:                                ems_unmap_page(i);
                   10281:                        }
                   10282:                }
                   10283:                process->ems_pages_stored = false;
                   10284:                REG8(AH) = 0x00;
                   10285:        }
                   10286: }
                   10287: 
                   10288: inline void msdos_int_67h_4bh()
                   10289: {
                   10290:        if(!support_ems) {
                   10291:                REG8(AH) = 0x84;
                   10292:        } else {
                   10293:                REG8(AH) = 0x00;
                   10294:                REG16(BX) = 0;
                   10295:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10296:                        if(ems_handles[i].allocated) {
                   10297:                                REG16(BX)++;
                   10298:                        }
                   10299:                }
                   10300:        }
                   10301: }
                   10302: 
                   10303: inline void msdos_int_67h_4ch()
                   10304: {
                   10305:        if(!support_ems) {
                   10306:                REG8(AH) = 0x84;
                   10307:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10308:                REG8(AH) = 0x83;
                   10309:        } else {
                   10310:                REG8(AH) = 0x00;
                   10311:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   10312:        }
                   10313: }
                   10314: 
                   10315: inline void msdos_int_67h_4dh()
                   10316: {
                   10317:        if(!support_ems) {
                   10318:                REG8(AH) = 0x84;
                   10319:        } else {
                   10320:                REG8(AH) = 0x00;
                   10321:                REG16(BX) = 0;
                   10322:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10323:                        if(ems_handles[i].allocated) {
                   10324:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   10325:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   10326:                                REG16(BX)++;
                   10327:                        }
                   10328:                }
                   10329:        }
                   10330: }
                   10331: 
1.1.1.20  root     10332: inline void msdos_int_67h_4eh()
                   10333: {
                   10334:        if(!support_ems) {
                   10335:                REG8(AH) = 0x84;
                   10336:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   10337:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   10338:                        // save page map
                   10339:                        for(int i = 0; i < 4; i++) {
                   10340:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   10341:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   10342:                        }
                   10343:                }
                   10344:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   10345:                        // restore page map
                   10346:                        for(int i = 0; i < 4; i++) {
                   10347:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   10348:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   10349:                                
                   10350:                                if(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   10351:                                        ems_map_page(i, handle, page);
                   10352:                                } else {
                   10353:                                        ems_unmap_page(i);
                   10354:                                }
                   10355:                        }
                   10356:                }
                   10357:                REG8(AH) = 0x00;
                   10358:        } else if(REG8(AL) == 0x03) {
                   10359:                REG8(AH) = 0x00;
1.1.1.21  root     10360:                REG8(AL) = 4 * 4;
                   10361:        } else {
1.1.1.22  root     10362:                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     10363:                REG8(AH) = 0x8f;
                   10364:        }
                   10365: }
                   10366: 
                   10367: inline void msdos_int_67h_4fh()
                   10368: {
                   10369:        if(!support_ems) {
                   10370:                REG8(AH) = 0x84;
                   10371:        } else if(REG8(AL) == 0x00) {
                   10372:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   10373:                
                   10374:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   10375:                for(int i = 0; i < count; i++) {
                   10376:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   10377:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   10378:                        
                   10379: //                     if(!(physical < 4)) {
                   10380: //                             REG8(AH) = 0x8b;
                   10381: //                             return;
                   10382: //                     }
                   10383:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   10384:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   10385:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   10386:                }
                   10387:                REG8(AH) = 0x00;
                   10388:        } else if(REG8(AL) == 0x01) {
                   10389:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   10390:                
                   10391:                for(int i = 0; i < count; i++) {
                   10392:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   10393:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   10394:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   10395:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   10396:                        
                   10397: //                     if(!(physical < 4)) {
                   10398: //                             REG8(AH) = 0x8b;
                   10399: //                             return;
                   10400: //                     } else
                   10401:                        if(!(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
                   10402:                                REG8(AH) = 0x83;
                   10403:                                return;
                   10404:                        } else if(logical == 0xffff) {
                   10405:                                ems_unmap_page(physical & 3);
                   10406:                        } else if(logical < ems_handles[handle].pages) {
                   10407:                                ems_map_page(physical & 3, handle, logical);
                   10408:                        } else {
                   10409:                                REG8(AH) = 0x8a;
                   10410:                                return;
                   10411:                        }
                   10412:                }
                   10413:                REG8(AH) = 0x00;
                   10414:        } else if(REG8(AL) == 0x02) {
                   10415:                REG8(AH) = 0x00;
                   10416:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     10417:        } else {
1.1.1.22  root     10418:                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     10419:                REG8(AH) = 0x8f;
                   10420:        }
                   10421: }
                   10422: 
                   10423: inline void msdos_int_67h_50h()
                   10424: {
                   10425:        if(!support_ems) {
                   10426:                REG8(AH) = 0x84;
                   10427:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10428:                REG8(AH) = 0x83;
                   10429:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   10430:                for(int i = 0; i < REG16(CX); i++) {
                   10431:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   10432:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   10433:                        
                   10434:                        if(REG8(AL) == 0x01) {
                   10435:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   10436:                        }
                   10437: //                     if(!(physical < 4)) {
                   10438: //                             REG8(AH) = 0x8b;
                   10439: //                             return;
                   10440: //                     } else
                   10441:                        if(logical == 0xffff) {
                   10442:                                ems_unmap_page(physical & 3);
                   10443:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   10444:                                ems_map_page(physical & 3, REG16(DX), logical);
                   10445:                        } else {
                   10446:                                REG8(AH) = 0x8a;
                   10447:                                return;
                   10448:                        }
                   10449:                }
                   10450:                REG8(AH) = 0x00;
                   10451:        } else {
1.1.1.22  root     10452:                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     10453:                REG8(AH) = 0x8f;
                   10454:        }
                   10455: }
                   10456: 
1.1.1.19  root     10457: inline void msdos_int_67h_51h()
                   10458: {
                   10459:        if(!support_ems) {
                   10460:                REG8(AH) = 0x84;
                   10461:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10462:                REG8(AH) = 0x83;
                   10463:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10464:                REG8(AH) = 0x87;
                   10465:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   10466:                REG8(AH) = 0x88;
                   10467:        } else {
                   10468:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   10469:                REG8(AH) = 0x00;
                   10470:        }
                   10471: }
                   10472: 
1.1.1.20  root     10473: inline void msdos_int_67h_52h()
                   10474: {
                   10475:        if(!support_ems) {
                   10476:                REG8(AH) = 0x84;
                   10477:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10478:                REG8(AH) = 0x83;
                   10479:        } else if(REG8(AL) == 0x00) {
                   10480:                REG8(AL) = 0x00; // handle is volatile
                   10481:                REG8(AH) = 0x00;
                   10482:        } else if(REG8(AL) == 0x01) {
                   10483:                if(REG8(BL) == 0x00) {
                   10484:                        REG8(AH) = 0x00;
                   10485:                } else {
                   10486:                        REG8(AH) = 0x90; // undefined attribute type
                   10487:                }
                   10488:        } else if(REG8(AL) == 0x02) {
                   10489:                REG8(AL) = 0x00; // only volatile handles supported
                   10490:                REG8(AH) = 0x00;
                   10491:        } else {
1.1.1.22  root     10492:                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     10493:                REG8(AH) = 0x8f;
                   10494:        }
                   10495: }
                   10496: 
1.1.1.19  root     10497: inline void msdos_int_67h_53h()
                   10498: {
                   10499:        if(!support_ems) {
                   10500:                REG8(AH) = 0x84;
                   10501:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   10502:                REG8(AH) = 0x83;
                   10503:        } else if(REG8(AL) == 0x00) {
                   10504:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   10505:                REG8(AH) = 0x00;
                   10506:        } else if(REG8(AL) == 0x01) {
                   10507:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10508:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   10509:                                REG8(AH) = 0xa1;
                   10510:                                return;
                   10511:                        }
                   10512:                }
                   10513:                REG8(AH) = 0x00;
                   10514:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   10515:        } else {
1.1.1.22  root     10516:                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     10517:                REG8(AH) = 0x8f;
1.1.1.19  root     10518:        }
                   10519: }
                   10520: 
                   10521: inline void msdos_int_67h_54h()
                   10522: {
                   10523:        if(!support_ems) {
                   10524:                REG8(AH) = 0x84;
                   10525:        } else if(REG8(AL) == 0x00) {
                   10526:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10527:                        if(ems_handles[i].allocated) {
                   10528:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   10529:                        } else {
                   10530:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   10531:                        }
                   10532:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   10533:                }
                   10534:                REG8(AH) = 0x00;
                   10535:                REG8(AL) = MAX_EMS_HANDLES;
                   10536:        } else if(REG8(AL) == 0x01) {
                   10537:                REG8(AH) = 0xa0; // not found
                   10538:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10539:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   10540:                                REG8(AH) = 0x00;
                   10541:                                REG16(DX) = i;
                   10542:                                break;
                   10543:                        }
                   10544:                }
                   10545:        } else if(REG8(AL) == 0x02) {
                   10546:                REG8(AH) = 0x00;
                   10547:                REG16(BX) = MAX_EMS_HANDLES;
                   10548:        } else {
1.1.1.22  root     10549:                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     10550:                REG8(AH) = 0x8f;
                   10551:        }
                   10552: }
                   10553: 
                   10554: inline void msdos_int_67h_57h_tmp()
                   10555: {
                   10556:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   10557:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   10558:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   10559:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   10560:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   10561:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   10562:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   10563:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   10564:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   10565:        
                   10566:        UINT8 *src_buffer, *dest_buffer;
                   10567:        UINT32 src_addr, dest_addr;
                   10568:        UINT32 src_addr_max, dest_addr_max;
                   10569:        
                   10570:        if(src_type == 0) {
                   10571:                src_buffer = mem;
                   10572:                src_addr = (src_seg << 4) + src_ofs;
                   10573:                src_addr_max = MAX_MEM;
                   10574:        } else {
                   10575:                if(!(src_handle < MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
                   10576:                        REG8(AH) = 0x83;
                   10577:                        return;
                   10578:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   10579:                        REG8(AH) = 0x8a;
                   10580:                        return;
                   10581:                }
                   10582:                src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   10583:                src_addr = src_ofs;
                   10584:                src_addr_max = 0x4000;
                   10585:        }
                   10586:        if(dest_type == 0) {
                   10587:                dest_buffer = mem;
                   10588:                dest_addr = (dest_seg << 4) + dest_ofs;
                   10589:                dest_addr_max = MAX_MEM;
                   10590:        } else {
                   10591:                if(!(dest_handle < MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
                   10592:                        REG8(AH) = 0x83;
                   10593:                        return;
                   10594:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   10595:                        REG8(AH) = 0x8a;
                   10596:                        return;
                   10597:                }
                   10598:                dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   10599:                dest_addr = dest_ofs;
                   10600:                dest_addr_max = 0x4000;
                   10601:        }
                   10602:        for(int i = 0; i < copy_length; i++) {
                   10603:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   10604:                        if(REG8(AL) == 0x00) {
                   10605:                                dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   10606:                        } else if(REG8(AL) == 0x01) {
                   10607:                                UINT8 tmp = dest_buffer[dest_addr];
                   10608:                                dest_buffer[dest_addr++] = src_buffer[src_addr];
                   10609:                                src_buffer[src_addr++] = tmp;
                   10610:                        }
                   10611:                } else {
                   10612:                        REG8(AH) = 0x93;
                   10613:                        return;
                   10614:                }
                   10615:        }
                   10616:        REG8(AH) = 0x80;
                   10617: }
                   10618: 
                   10619: inline void msdos_int_67h_57h()
                   10620: {
                   10621:        if(!support_ems) {
                   10622:                REG8(AH) = 0x84;
                   10623:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   10624:                struct {
                   10625:                        UINT16 handle;
                   10626:                        UINT16 page;
                   10627:                        bool mapped;
                   10628:                } tmp_pages[4];
                   10629:                
                   10630:                // unmap pages to copy memory data to ems buffer
                   10631:                for(int i = 0; i < 4; i++) {
                   10632:                        tmp_pages[i].handle = ems_pages[i].handle;
                   10633:                        tmp_pages[i].page   = ems_pages[i].page;
                   10634:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   10635:                        ems_unmap_page(i);
                   10636:                }
                   10637:                
                   10638:                // run move/exchange operation
                   10639:                msdos_int_67h_57h_tmp();
                   10640:                
                   10641:                // restore unmapped pages
                   10642:                for(int i = 0; i < 4; i++) {
                   10643:                        if(tmp_pages[i].mapped) {
                   10644:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   10645:                        }
                   10646:                }
                   10647:        } else {
1.1.1.22  root     10648:                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     10649:                REG8(AH) = 0x8f;
                   10650:        }
                   10651: }
                   10652: 
                   10653: inline void msdos_int_67h_58h()
                   10654: {
                   10655:        if(!support_ems) {
                   10656:                REG8(AH) = 0x84;
                   10657:        } else if(REG8(AL) == 0x00) {
                   10658:                for(int i = 0; i < 4; i++) {
1.1.1.30! root     10659:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
        !          10660:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     10661:                }
                   10662:                REG8(AH) = 0x00;
                   10663:                REG16(CX) = 4;
                   10664:        } else if(REG8(AL) == 0x01) {
                   10665:                REG8(AH) = 0x00;
                   10666:                REG16(CX) = 4;
                   10667:        } else {
1.1.1.22  root     10668:                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     10669:                REG8(AH) = 0x8f;
                   10670:        }
                   10671: }
                   10672: 
                   10673: inline void msdos_int_67h_5ah()
                   10674: {
                   10675:        if(!support_ems) {
1.1.1.19  root     10676:                REG8(AH) = 0x84;
1.1.1.20  root     10677:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   10678:                REG8(AH) = 0x87;
                   10679:        } else if(REG16(BX) > free_ems_pages) {
                   10680:                REG8(AH) = 0x88;
                   10681: //     } else if(REG16(BX) == 0) {
                   10682: //             REG8(AH) = 0x89;
                   10683:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   10684:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   10685:                        if(!ems_handles[i].allocated) {
                   10686:                                ems_allocate_pages(i, REG16(BX));
                   10687:                                REG8(AH) = 0x00;
                   10688:                                REG16(DX) = i;
                   10689:                                return;
                   10690:                        }
                   10691:                }
                   10692:                REG8(AH) = 0x85;
                   10693:        } else {
1.1.1.22  root     10694:                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     10695:                REG8(AH) = 0x8f;
1.1.1.19  root     10696:        }
                   10697: }
                   10698: 
1.1.1.30! root     10699: inline void msdos_int_67h_deh()
        !          10700: {
        !          10701:        REG8(AH) = 0x84;
        !          10702: }
        !          10703: 
1.1.1.19  root     10704: #ifdef SUPPORT_XMS
                   10705: 
1.1.1.26  root     10706: inline void msdos_xms_init()
                   10707: {
1.1.1.30! root     10708:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
        !          10709:        emb_handle_top->address = EMB_TOP;
        !          10710:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     10711:        xms_a20_local_enb_count = 0;
                   10712: }
                   10713: 
1.1.1.30! root     10714: inline void msdos_xms_finish()
        !          10715: {
        !          10716:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
        !          10717:                emb_handle_t *next_handle = emb_handle->next;
        !          10718:                free(emb_handle);
        !          10719:                emb_handle = next_handle;
        !          10720:        }
        !          10721: }
        !          10722: 
        !          10723: emb_handle_t *msdos_xms_get_emb_handle(int handle)
        !          10724: {
        !          10725:        if(handle != 0) {
        !          10726:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          10727:                        if(emb_handle->handle == handle) {
        !          10728:                                return(emb_handle);
        !          10729:                        }
        !          10730:                }
        !          10731:        }
        !          10732:        return(NULL);
        !          10733: }
        !          10734: 
        !          10735: int msdos_xms_get_unused_emb_handle_id()
        !          10736: {
        !          10737:        for(int handle = 1;; handle++) {
        !          10738:                if(msdos_xms_get_emb_handle(handle) == NULL) {
        !          10739:                        return(handle);
        !          10740:                }
        !          10741:        }
        !          10742:        return(0);
        !          10743: }
        !          10744: 
        !          10745: int msdos_xms_get_unused_emb_handle_count()
        !          10746: {
        !          10747:        int count = 64; //255;
        !          10748:        
        !          10749:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          10750:                if(emb_handle->handle != 0) {
        !          10751:                        if(--count == 1) {
        !          10752:                                break;
        !          10753:                        }
        !          10754:                }
        !          10755:        }
        !          10756:        return(count);
        !          10757: }
        !          10758: 
        !          10759: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
        !          10760: {
        !          10761:        if(emb_handle->size_kb > size_kb) {
        !          10762:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
        !          10763:                
        !          10764:                new_handle->address = emb_handle->address + size_kb * 1024;
        !          10765:                new_handle->size_kb = emb_handle->size_kb - size_kb;
        !          10766:                emb_handle->size_kb = size_kb;
        !          10767:                
        !          10768:                new_handle->prev = emb_handle;
        !          10769:                new_handle->next = emb_handle->next;
        !          10770:                if(emb_handle->next != NULL) {
        !          10771:                        emb_handle->next->prev = new_handle;
        !          10772:                }
        !          10773:                emb_handle->next = new_handle;
        !          10774:        }
        !          10775: }
        !          10776: 
        !          10777: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
        !          10778: {
        !          10779:        emb_handle_t *next_handle = emb_handle->next;
        !          10780:        
        !          10781:        if(next_handle != NULL) {
        !          10782:                emb_handle->size_kb += next_handle->size_kb;
        !          10783:                
        !          10784:                if(next_handle->next != NULL) {
        !          10785:                        next_handle->next->prev = emb_handle;
        !          10786:                }
        !          10787:                emb_handle->next = next_handle->next;
        !          10788:                free(next_handle);
        !          10789:        }
        !          10790: }
        !          10791: 
        !          10792: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
        !          10793: {
        !          10794:        emb_handle_t *target_handle = NULL;
        !          10795:        
        !          10796:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          10797:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
        !          10798:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
        !          10799:                                target_handle = emb_handle;
        !          10800:                        }
        !          10801:                }
        !          10802:        }
        !          10803:        if(target_handle != NULL) {
        !          10804:                if(target_handle->size_kb > size_kb) {
        !          10805:                        msdos_xms_split_emb_handle(target_handle, size_kb);
        !          10806:                }
        !          10807: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
        !          10808:                return(target_handle);
        !          10809:        }
        !          10810:        return(NULL);
        !          10811: }
        !          10812: 
        !          10813: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
        !          10814: {
        !          10815:        emb_handle_t *prev_handle = emb_handle->prev;
        !          10816:        emb_handle_t *next_handle = emb_handle->next;
        !          10817:        
        !          10818:        if(prev_handle != NULL && prev_handle->handle == 0) {
        !          10819:                msdos_xms_combine_emb_handles(prev_handle);
        !          10820:                emb_handle = prev_handle;
        !          10821:        }
        !          10822:        if(next_handle != NULL && next_handle->handle == 0) {
        !          10823:                msdos_xms_combine_emb_handles(emb_handle);
        !          10824:        }
        !          10825:        emb_handle->handle = 0;
        !          10826: }
        !          10827: 
1.1.1.19  root     10828: inline void msdos_call_xms_00h()
                   10829: {
1.1.1.29  root     10830: #if defined(HAS_I386)
                   10831:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   10832:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   10833: #else
                   10834:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   10835:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   10836: #endif
                   10837: //     REG16(DX) = 0x0000; // HMA does not exist
                   10838:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     10839: }
                   10840: 
                   10841: inline void msdos_call_xms_01h()
                   10842: {
1.1.1.29  root     10843:        if(REG8(AL) == 0x40) {
                   10844:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   10845:                // DX=KB free extended memory returned by last call of function 08h
                   10846:                REG16(AX) = 0x0000;
                   10847:                REG8(BL) = 0x91;
                   10848:                REG16(DX) = xms_dx_after_call_08h;
                   10849:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   10850:                REG16(AX) = 0x0000;
                   10851:                REG8(BL) = 0x81; // Vdisk was detected
                   10852: #ifdef SUPPORT_HMA
                   10853:        } else if(is_hma_used_by_int_2fh) {
                   10854:                REG16(AX) = 0x0000;
                   10855:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   10856:        } else if(is_hma_used_by_xms) {
                   10857:                REG16(AX) = 0x0000;
                   10858:                REG8(BL) = 0x91; // HMA is already in use
                   10859:        } else {
                   10860:                REG16(AX) = 0x0001;
                   10861:                is_hma_used_by_xms = true;
                   10862: #else
                   10863:        } else {
                   10864:                REG16(AX) = 0x0000;
                   10865:                REG8(BL) = 0x91; // HMA is already in use
                   10866: #endif
                   10867:        }
1.1.1.19  root     10868: }
                   10869: 
                   10870: inline void msdos_call_xms_02h()
                   10871: {
1.1.1.29  root     10872:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   10873:                REG16(AX) = 0x0000;
                   10874:                REG8(BL) = 0x81; // Vdisk was detected
                   10875: #ifdef SUPPORT_HMA
                   10876:        } else if(is_hma_used_by_int_2fh) {
                   10877:                REG16(AX) = 0x0000;
                   10878:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   10879:        } else if(!is_hma_used_by_xms) {
                   10880:                REG16(AX) = 0x0000;
                   10881:                REG8(BL) = 0x93; // HMA is not allocated
                   10882:        } else {
                   10883:                REG16(AX) = 0x0001;
                   10884:                is_hma_used_by_xms = false;
                   10885:                // restore first free mcb in high memory area
                   10886:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   10887: #else
                   10888:        } else {
                   10889:                REG16(AX) = 0x0000;
                   10890:                REG8(BL) = 0x91; // HMA is already in use
                   10891: #endif
                   10892:        }
1.1.1.19  root     10893: }
                   10894: 
                   10895: inline void msdos_call_xms_03h()
                   10896: {
                   10897:        i386_set_a20_line(1);
                   10898:        REG16(AX) = 0x0001;
                   10899:        REG8(BL) = 0x00;
                   10900: }
                   10901: 
                   10902: inline void msdos_call_xms_04h()
                   10903: {
1.1.1.21  root     10904:        i386_set_a20_line(0);
                   10905:        REG16(AX) = 0x0001;
                   10906:        REG8(BL) = 0x00;
1.1.1.19  root     10907: }
                   10908: 
                   10909: inline void msdos_call_xms_05h()
                   10910: {
                   10911:        i386_set_a20_line(1);
                   10912:        REG16(AX) = 0x0001;
                   10913:        REG8(BL) = 0x00;
1.1.1.21  root     10914:        xms_a20_local_enb_count++;
1.1.1.19  root     10915: }
                   10916: 
                   10917: void msdos_call_xms_06h()
                   10918: {
1.1.1.21  root     10919:        if(xms_a20_local_enb_count > 0) {
                   10920:                xms_a20_local_enb_count--;
                   10921:        }
                   10922:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     10923:                i386_set_a20_line(0);
1.1.1.21  root     10924:        }
                   10925:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     10926:                REG16(AX) = 0x0000;
                   10927:                REG8(BL) = 0x94;
1.1.1.21  root     10928:        } else {
                   10929:                REG16(AX) = 0x0001;
                   10930:                REG8(BL) = 0x00;
1.1.1.19  root     10931:        }
                   10932: }
                   10933: 
                   10934: inline void msdos_call_xms_07h()
                   10935: {
                   10936:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   10937:        REG8(BL) = 0x00;
                   10938: }
                   10939: 
                   10940: inline void msdos_call_xms_08h()
                   10941: {
                   10942:        REG16(AX) = REG16(DX) = 0x0000;
                   10943:        
1.1.1.30! root     10944:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          10945:                if(emb_handle->handle == 0) {
        !          10946:                        if(REG16(AX) < emb_handle->size_kb) {
        !          10947:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     10948:                        }
1.1.1.30! root     10949:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     10950:                }
                   10951:        }
                   10952:        
                   10953:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   10954:                REG8(BL) = 0xa0;
                   10955:        } else {
                   10956:                REG8(BL) = 0x00;
                   10957:        }
1.1.1.29  root     10958:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     10959: }
                   10960: 
1.1.1.30! root     10961: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     10962: {
1.1.1.30! root     10963:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
        !          10964:        
        !          10965:        if(emb_handle != NULL) {
        !          10966:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
        !          10967:                
        !          10968:                REG16(AX) = 0x0001;
        !          10969:                REG16(DX) = emb_handle->handle;
        !          10970:                REG8(BL) = 0x00;
        !          10971:        } else {
        !          10972:                REG16(AX) = REG16(DX) = 0x0000;
        !          10973:                REG8(BL) = 0xa0;
1.1.1.19  root     10974:        }
1.1.1.30! root     10975: }
        !          10976: 
        !          10977: inline void msdos_call_xms_09h()
        !          10978: {
        !          10979:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     10980: }
                   10981: 
                   10982: inline void msdos_call_xms_0ah()
                   10983: {
1.1.1.30! root     10984:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          10985:        
        !          10986:        if(emb_handle == NULL) {
1.1.1.19  root     10987:                REG16(AX) = 0x0000;
                   10988:                REG8(BL) = 0xa2;
1.1.1.30! root     10989:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     10990:                REG16(AX) = 0x0000;
                   10991:                REG8(BL) = 0xab;
                   10992:        } else {
1.1.1.30! root     10993:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     10994:                
                   10995:                REG16(AX) = 0x0001;
                   10996:                REG8(BL) = 0x00;
                   10997:        }
                   10998: }
                   10999: 
                   11000: inline void msdos_call_xms_0bh()
                   11001: {
                   11002:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   11003:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   11004:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   11005:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   11006:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   11007:        
                   11008:        UINT8 *src_buffer, *dest_buffer;
                   11009:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30! root     11010:        emb_handle_t *emb_handle;
1.1.1.19  root     11011:        
                   11012:        if(src_handle == 0) {
                   11013:                src_buffer = mem;
                   11014:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   11015:                src_addr_max = MAX_MEM;
1.1.1.30! root     11016: 
1.1.1.19  root     11017:        } else {
1.1.1.30! root     11018:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     11019:                        REG16(AX) = 0x0000;
                   11020:                        REG8(BL) = 0xa3;
                   11021:                        return;
                   11022:                }
1.1.1.30! root     11023:                src_buffer = mem + emb_handle->address;
        !          11024:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     11025:        }
                   11026:        if(dest_handle == 0) {
                   11027:                dest_buffer = mem;
                   11028:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   11029:                dest_addr_max = MAX_MEM;
                   11030:        } else {
1.1.1.30! root     11031:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     11032:                        REG16(AX) = 0x0000;
                   11033:                        REG8(BL) = 0xa5;
                   11034:                        return;
                   11035:                }
1.1.1.30! root     11036:                dest_buffer = mem + emb_handle->address;
        !          11037:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     11038:        }
                   11039:        for(int i = 0; i < copy_length; i++) {
                   11040:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   11041:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   11042:                } else {
                   11043:                        break;
                   11044:                }
                   11045:        }
                   11046:        REG16(AX) = 0x0001;
                   11047:        REG8(BL) = 0x00;
                   11048: }
                   11049: 
                   11050: inline void msdos_call_xms_0ch()
                   11051: {
1.1.1.30! root     11052:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          11053:        
        !          11054:        if(emb_handle == NULL) {
1.1.1.19  root     11055:                REG16(AX) = 0x0000;
                   11056:                REG8(BL) = 0xa2;
                   11057:        } else {
1.1.1.30! root     11058:                emb_handle->lock++;
1.1.1.19  root     11059:                REG16(AX) = 0x0001;
                   11060:                REG8(BL) = 0x00;
1.1.1.30! root     11061:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
        !          11062:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     11063:        }
                   11064: }
                   11065: 
                   11066: inline void msdos_call_xms_0dh()
                   11067: {
1.1.1.30! root     11068:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          11069:        
        !          11070:        if(emb_handle == NULL) {
1.1.1.19  root     11071:                REG16(AX) = 0x0000;
                   11072:                REG8(BL) = 0xa2;
1.1.1.30! root     11073:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     11074:                REG16(AX) = 0x0000;
                   11075:                REG8(BL) = 0xaa;
                   11076:        } else {
1.1.1.30! root     11077:                emb_handle->lock--;
1.1.1.19  root     11078:                REG16(AX) = 0x0001;
                   11079:                REG8(BL) = 0x00;
                   11080:        }
                   11081: }
                   11082: 
                   11083: inline void msdos_call_xms_0eh()
                   11084: {
1.1.1.30! root     11085:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          11086:        
        !          11087:        if(emb_handle == NULL) {
1.1.1.19  root     11088:                REG16(AX) = 0x0000;
                   11089:                REG8(BL) = 0xa2;
                   11090:        } else {
                   11091:                REG16(AX) = 0x0001;
1.1.1.30! root     11092:                REG8(BH) = emb_handle->lock;
        !          11093:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
        !          11094:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     11095:        }
                   11096: }
                   11097: 
1.1.1.30! root     11098: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     11099: {
1.1.1.30! root     11100:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          11101:        
        !          11102:        if(emb_handle == NULL) {
1.1.1.19  root     11103:                REG16(AX) = 0x0000;
                   11104:                REG8(BL) = 0xa2;
1.1.1.30! root     11105:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     11106:                REG16(AX) = 0x0000;
                   11107:                REG8(BL) = 0xab;
                   11108:        } else {
1.1.1.30! root     11109:                if(emb_handle->size_kb < size_kb) {
        !          11110:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
        !          11111:                                msdos_xms_combine_emb_handles(emb_handle);
        !          11112:                                if(emb_handle->size_kb > size_kb) {
        !          11113:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
        !          11114:                                }
        !          11115:                        } else {
        !          11116:                                int old_handle = emb_handle->handle;
        !          11117:                                int old_size_kb = emb_handle->size_kb;
        !          11118:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
        !          11119:                                
        !          11120:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
        !          11121:                                msdos_xms_free_emb_handle(emb_handle);
        !          11122:                                
        !          11123:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
        !          11124:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
        !          11125:                                }
        !          11126:                                emb_handle->handle = old_handle;
        !          11127:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
        !          11128:                                free(buffer);
        !          11129:                        }
        !          11130:                } else if(emb_handle->size_kb > size_kb) {
        !          11131:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
        !          11132:                }
        !          11133:                if(emb_handle->size_kb != size_kb) {
        !          11134:                        REG16(AX) = 0x0000;
        !          11135:                        REG8(BL) = 0xa0;
        !          11136:                } else {
        !          11137:                        REG16(AX) = 0x0001;
        !          11138:                        REG8(BL) = 0x00;
        !          11139:                }
1.1.1.19  root     11140:        }
                   11141: }
                   11142: 
1.1.1.30! root     11143: inline void msdos_call_xms_0fh()
        !          11144: {
        !          11145:        msdos_call_xms_0fh(REG16(BX));
        !          11146: }
        !          11147: 
1.1.1.19  root     11148: inline void msdos_call_xms_10h()
                   11149: {
                   11150:        int seg;
                   11151:        
                   11152:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   11153:                REG16(AX) = 0x0001;
                   11154:                REG16(BX) = seg;
                   11155:        } else {
                   11156:                REG16(AX) = 0x0000;
                   11157:                REG8(BL) = 0xb0;
                   11158:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11159:        }
                   11160: }
                   11161: 
                   11162: inline void msdos_call_xms_11h()
                   11163: {
                   11164:        int mcb_seg = REG16(DX) - 1;
                   11165:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11166:        
                   11167:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11168:                msdos_mem_free(REG16(DX));
                   11169:                REG16(AX) = 0x0001;
                   11170:                REG8(BL) = 0x00;
                   11171:        } else {
                   11172:                REG16(AX) = 0x0000;
                   11173:                REG8(BL) = 0xb2;
                   11174:        }
                   11175: }
                   11176: 
                   11177: inline void msdos_call_xms_12h()
                   11178: {
                   11179:        int mcb_seg = REG16(DX) - 1;
                   11180:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11181:        int max_paragraphs;
                   11182:        
                   11183:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11184:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   11185:                        REG16(AX) = 0x0001;
                   11186:                        REG8(BL) = 0x00;
                   11187:                } else {
                   11188:                        REG16(AX) = 0x0000;
                   11189:                        REG8(BL) = 0xb0;
                   11190:                        REG16(DX) = max_paragraphs;
                   11191:                }
                   11192:        } else {
                   11193:                REG16(AX) = 0x0000;
                   11194:                REG8(BL) = 0xb2;
                   11195:        }
                   11196: }
                   11197: 
1.1.1.29  root     11198: #if defined(HAS_I386)
                   11199: 
                   11200: inline void msdos_call_xms_88h()
                   11201: {
                   11202:        REG32(EAX) = REG32(EDX) = 0x0000;
                   11203:        
1.1.1.30! root     11204:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          11205:                if(emb_handle->handle == 0) {
        !          11206:                        if(REG32(EAX) < emb_handle->size_kb) {
        !          11207:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     11208:                        }
1.1.1.30! root     11209:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     11210:                }
                   11211:        }
                   11212:        
                   11213:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   11214:                REG8(BL) = 0xa0;
                   11215:        } else {
                   11216:                REG8(BL) = 0x00;
                   11217:        }
                   11218:        REG32(ECX) = EMB_END - 1;
                   11219: }
                   11220: 
                   11221: inline void msdos_call_xms_89h()
                   11222: {
1.1.1.30! root     11223:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     11224: }
                   11225: 
                   11226: inline void msdos_call_xms_8eh()
                   11227: {
1.1.1.30! root     11228:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
        !          11229:        
        !          11230:        if(emb_handle == NULL) {
1.1.1.29  root     11231:                REG16(AX) = 0x0000;
                   11232:                REG8(BL) = 0xa2;
                   11233:        } else {
                   11234:                REG16(AX) = 0x0001;
1.1.1.30! root     11235:                REG8(BH) = emb_handle->lock;
        !          11236:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
        !          11237:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     11238:        }
                   11239: }
                   11240: 
                   11241: inline void msdos_call_xms_8fh()
                   11242: {
1.1.1.30! root     11243:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     11244: }
                   11245: 
                   11246: #endif
1.1.1.19  root     11247: #endif
                   11248: 
1.1.1.26  root     11249: UINT16 msdos_get_equipment()
                   11250: {
                   11251:        static UINT16 equip = 0;
                   11252:        
                   11253:        if(equip == 0) {
                   11254: #ifdef SUPPORT_FPU
                   11255:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   11256: #endif
                   11257:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   11258:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   11259: //             equip |= (1 << 8);      // 0 if DMA installed
                   11260:                equip |= (2 << 9);      // number of serial ports
                   11261:                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     11262:                
                   11263:                // check only A: and B: if it is floppy drive
                   11264:                DWORD dwDrives = GetLogicalDrives();
                   11265:                int n = 0;
                   11266:                for(int i = 0; i < 2; i++) {
                   11267:                        if(dwDrives & (1 << i)) {
                   11268:                                char volume[] = "A:\\";
                   11269:                                volume[0] = 'A' + i;
                   11270:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   11271:                                        n++;
                   11272:                                }
                   11273:                        }
                   11274:                }
                   11275:                if(n != 0) {
                   11276:                        equip |= (1 << 0);      // floppy disk(s) installed
                   11277:                        n--;
                   11278:                        equip |= (n << 6);      // number of floppies installed less 1
                   11279:                }
                   11280: //             if(joyGetNumDevs() != 0) {
                   11281: //                     equip |= (1 << 12);     // game port installed
                   11282: //             }
1.1.1.26  root     11283:        }
                   11284:        return(equip);
                   11285: }
                   11286: 
1.1       root     11287: void msdos_syscall(unsigned num)
                   11288: {
1.1.1.22  root     11289: #ifdef ENABLE_DEBUG_SYSCALL
                   11290:        if(num == 0x68) {
                   11291:                // dummy interrupt for EMS (int 67h)
                   11292:                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));
                   11293:        } else if(num == 0x69) {
                   11294:                // dummy interrupt for XMS (call far)
                   11295:                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));
                   11296:        } else if(num == 0x6a) {
                   11297:                // dummy interrupt for case map routine pointed in the country info
                   11298:        } else {
                   11299:                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));
                   11300:        }
                   11301: #endif
1.1.1.26  root     11302:        ctrl_c_pressed = ctrl_c_detected = false;
1.1.1.22  root     11303:        
1.1       root     11304:        switch(num) {
                   11305:        case 0x00:
1.1.1.28  root     11306:                try {
                   11307:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11308:                        error("division by zero\n");
                   11309:                } catch(...) {
                   11310:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   11311:                }
1.1       root     11312:                break;
                   11313:        case 0x04:
1.1.1.28  root     11314:                try {
                   11315:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11316:                        error("overflow\n");
                   11317:                } catch(...) {
                   11318:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   11319:                }
1.1       root     11320:                break;
                   11321:        case 0x06:
                   11322:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     11323:                if(!ignore_illegal_insn) {
1.1.1.28  root     11324:                        try {
                   11325:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11326:                                error("illegal instruction\n");
                   11327:                        } catch(...) {
                   11328:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   11329:                        }
1.1.1.14  root     11330:                } else {
                   11331: #if defined(HAS_I386)
                   11332:                        m_eip++;
                   11333: #else
                   11334:                        m_pc++;
                   11335: #endif
                   11336:                }
1.1       root     11337:                break;
1.1.1.8   root     11338:        case 0x08:
1.1.1.14  root     11339: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     11340:        case 0x09:
                   11341:        case 0x0a:
                   11342:        case 0x0b:
                   11343:        case 0x0c:
                   11344:        case 0x0d:
                   11345:        case 0x0e:
                   11346:        case 0x0f:
                   11347:                // EOI
                   11348:                pic[0].isr &= ~(1 << (num - 0x08));
                   11349:                pic_update();
                   11350:                break;
1.1       root     11351:        case 0x10:
                   11352:                // PC BIOS - Video
1.1.1.14  root     11353:                if(!restore_console_on_exit) {
1.1.1.15  root     11354:                        change_console_size(scr_width, scr_height);
1.1       root     11355:                }
1.1.1.3   root     11356:                m_CF = 0;
1.1       root     11357:                switch(REG8(AH)) {
1.1.1.16  root     11358:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     11359:                case 0x01: pcbios_int_10h_01h(); break;
                   11360:                case 0x02: pcbios_int_10h_02h(); break;
                   11361:                case 0x03: pcbios_int_10h_03h(); break;
                   11362:                case 0x05: pcbios_int_10h_05h(); break;
                   11363:                case 0x06: pcbios_int_10h_06h(); break;
                   11364:                case 0x07: pcbios_int_10h_07h(); break;
                   11365:                case 0x08: pcbios_int_10h_08h(); break;
                   11366:                case 0x09: pcbios_int_10h_09h(); break;
                   11367:                case 0x0a: pcbios_int_10h_0ah(); break;
                   11368:                case 0x0b: break;
                   11369:                case 0x0c: break;
                   11370:                case 0x0d: break;
                   11371:                case 0x0e: pcbios_int_10h_0eh(); break;
                   11372:                case 0x0f: pcbios_int_10h_0fh(); break;
                   11373:                case 0x10: break;
1.1.1.14  root     11374:                case 0x11: pcbios_int_10h_11h(); break;
                   11375:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     11376:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30! root     11377:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     11378:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     11379:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   11380:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     11381:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     11382:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   11383:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     11384:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30! root     11385:                case 0x6f: break;
1.1.1.22  root     11386:                case 0x80: m_CF = 1; break; // unknown
                   11387:                case 0x81: m_CF = 1; break; // unknown
1.1       root     11388:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     11389:                case 0x83: pcbios_int_10h_83h(); break;
                   11390:                case 0x8b: break;
                   11391:                case 0x8c: m_CF = 1; break; // unknown
                   11392:                case 0x8d: m_CF = 1; break; // unknown
                   11393:                case 0x8e: m_CF = 1; break; // unknown
                   11394:                case 0x90: pcbios_int_10h_90h(); break;
                   11395:                case 0x91: pcbios_int_10h_91h(); break;
                   11396:                case 0x92: break;
                   11397:                case 0x93: break;
                   11398:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     11399:                case 0xfa: break; // ega register interface library is not installed
1.1       root     11400:                case 0xfe: pcbios_int_10h_feh(); break;
                   11401:                case 0xff: pcbios_int_10h_ffh(); break;
                   11402:                default:
1.1.1.22  root     11403:                        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));
                   11404:                        m_CF = 1;
1.1       root     11405:                        break;
                   11406:                }
                   11407:                break;
                   11408:        case 0x11:
                   11409:                // PC BIOS - Get Equipment List
1.1.1.26  root     11410:                REG16(AX) = msdos_get_equipment();
1.1       root     11411:                break;
                   11412:        case 0x12:
                   11413:                // PC BIOS - Get Memory Size
                   11414:                REG16(AX) = MEMORY_END / 1024;
                   11415:                break;
                   11416:        case 0x13:
                   11417:                // PC BIOS - Disk
1.1.1.22  root     11418: //             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     11419:                REG8(AH) = 0xff;
1.1.1.3   root     11420:                m_CF = 1;
1.1       root     11421:                break;
                   11422:        case 0x14:
                   11423:                // PC BIOS - Serial I/O
1.1.1.25  root     11424:                switch(REG8(AH)) {
                   11425:                case 0x00: pcbios_int_14h_00h(); break;
                   11426:                case 0x01: pcbios_int_14h_01h(); break;
                   11427:                case 0x02: pcbios_int_14h_02h(); break;
                   11428:                case 0x03: pcbios_int_14h_03h(); break;
                   11429:                case 0x04: pcbios_int_14h_04h(); break;
                   11430:                case 0x05: pcbios_int_14h_05h(); break;
                   11431:                default:
                   11432:                        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));
                   11433:                        break;
                   11434:                }
1.1       root     11435:                break;
                   11436:        case 0x15:
                   11437:                // PC BIOS
1.1.1.3   root     11438:                m_CF = 0;
1.1       root     11439:                switch(REG8(AH)) {
1.1.1.14  root     11440:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     11441:                case 0x23: pcbios_int_15h_23h(); break;
                   11442:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     11443:                case 0x41: break;
1.1       root     11444:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     11445:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30! root     11446:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     11447:                case 0x86: pcbios_int_15h_86h(); break;
                   11448:                case 0x87: pcbios_int_15h_87h(); break;
                   11449:                case 0x88: pcbios_int_15h_88h(); break;
                   11450:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     11451:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     11452:                case 0xc0: // PS/2 ???
                   11453:                case 0xc1:
                   11454:                case 0xc2:
1.1.1.30! root     11455:                case 0xc3: // PS50+ ???
        !          11456:                case 0xc4:
1.1.1.22  root     11457:                        REG8(AH) = 0x86;
                   11458:                        m_CF = 1;
                   11459:                        break;
1.1.1.3   root     11460: #if defined(HAS_I386)
1.1       root     11461:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     11462: #endif
1.1       root     11463:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     11464:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     11465:                default:
1.1.1.22  root     11466:                        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));
                   11467:                        REG8(AH) = 0x86;
1.1.1.3   root     11468:                        m_CF = 1;
1.1       root     11469:                        break;
                   11470:                }
                   11471:                break;
                   11472:        case 0x16:
                   11473:                // PC BIOS - Keyboard
1.1.1.3   root     11474:                m_CF = 0;
1.1       root     11475:                switch(REG8(AH)) {
                   11476:                case 0x00: pcbios_int_16h_00h(); break;
                   11477:                case 0x01: pcbios_int_16h_01h(); break;
                   11478:                case 0x02: pcbios_int_16h_02h(); break;
                   11479:                case 0x03: pcbios_int_16h_03h(); break;
                   11480:                case 0x05: pcbios_int_16h_05h(); break;
                   11481:                case 0x10: pcbios_int_16h_00h(); break;
                   11482:                case 0x11: pcbios_int_16h_01h(); break;
                   11483:                case 0x12: pcbios_int_16h_12h(); break;
                   11484:                case 0x13: pcbios_int_16h_13h(); break;
                   11485:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     11486:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30! root     11487:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     11488:                case 0xda: break; // unknown
                   11489:                case 0xff: break; // unknown
1.1       root     11490:                default:
1.1.1.22  root     11491:                        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     11492:                        break;
                   11493:                }
                   11494:                break;
                   11495:        case 0x17:
                   11496:                // PC BIOS - Printer
1.1.1.22  root     11497: //             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     11498:                break;
                   11499:        case 0x1a:
                   11500:                // PC BIOS - Timer
1.1.1.3   root     11501:                m_CF = 0;
1.1       root     11502:                switch(REG8(AH)) {
                   11503:                case 0x00: pcbios_int_1ah_00h(); break;
                   11504:                case 0x01: break;
                   11505:                case 0x02: pcbios_int_1ah_02h(); break;
                   11506:                case 0x03: break;
                   11507:                case 0x04: pcbios_int_1ah_04h(); break;
                   11508:                case 0x05: break;
                   11509:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   11510:                case 0x0b: break;
1.1.1.14  root     11511:                case 0x35: break; // Word Perfect Third Party Interface?
                   11512:                case 0x36: break; // Word Perfect Third Party Interface
                   11513:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     11514:                default:
1.1.1.22  root     11515:                        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     11516:                        break;
                   11517:                }
                   11518:                break;
                   11519:        case 0x20:
1.1.1.28  root     11520:                try {
                   11521:                        msdos_process_terminate(SREG(CS), retval, 1);
                   11522:                } catch(...) {
                   11523:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   11524:                }
1.1       root     11525:                break;
                   11526:        case 0x21:
                   11527:                // MS-DOS System Call
1.1.1.3   root     11528:                m_CF = 0;
1.1.1.28  root     11529:                try {
                   11530:                        switch(REG8(AH)) {
                   11531:                        case 0x00: msdos_int_21h_00h(); break;
                   11532:                        case 0x01: msdos_int_21h_01h(); break;
                   11533:                        case 0x02: msdos_int_21h_02h(); break;
                   11534:                        case 0x03: msdos_int_21h_03h(); break;
                   11535:                        case 0x04: msdos_int_21h_04h(); break;
                   11536:                        case 0x05: msdos_int_21h_05h(); break;
                   11537:                        case 0x06: msdos_int_21h_06h(); break;
                   11538:                        case 0x07: msdos_int_21h_07h(); break;
                   11539:                        case 0x08: msdos_int_21h_08h(); break;
                   11540:                        case 0x09: msdos_int_21h_09h(); break;
                   11541:                        case 0x0a: msdos_int_21h_0ah(); break;
                   11542:                        case 0x0b: msdos_int_21h_0bh(); break;
                   11543:                        case 0x0c: msdos_int_21h_0ch(); break;
                   11544:                        case 0x0d: msdos_int_21h_0dh(); break;
                   11545:                        case 0x0e: msdos_int_21h_0eh(); break;
                   11546:                        case 0x0f: msdos_int_21h_0fh(); break;
                   11547:                        case 0x10: msdos_int_21h_10h(); break;
                   11548:                        case 0x11: msdos_int_21h_11h(); break;
                   11549:                        case 0x12: msdos_int_21h_12h(); break;
                   11550:                        case 0x13: msdos_int_21h_13h(); break;
                   11551:                        case 0x14: msdos_int_21h_14h(); break;
                   11552:                        case 0x15: msdos_int_21h_15h(); break;
                   11553:                        case 0x16: msdos_int_21h_16h(); break;
                   11554:                        case 0x17: msdos_int_21h_17h(); break;
                   11555:                        case 0x18: msdos_int_21h_18h(); break;
                   11556:                        case 0x19: msdos_int_21h_19h(); break;
                   11557:                        case 0x1a: msdos_int_21h_1ah(); break;
                   11558:                        case 0x1b: msdos_int_21h_1bh(); break;
                   11559:                        case 0x1c: msdos_int_21h_1ch(); break;
                   11560:                        case 0x1d: msdos_int_21h_1dh(); break;
                   11561:                        case 0x1e: msdos_int_21h_1eh(); break;
                   11562:                        case 0x1f: msdos_int_21h_1fh(); break;
                   11563:                        case 0x20: msdos_int_21h_20h(); break;
                   11564:                        case 0x21: msdos_int_21h_21h(); break;
                   11565:                        case 0x22: msdos_int_21h_22h(); break;
                   11566:                        case 0x23: msdos_int_21h_23h(); break;
                   11567:                        case 0x24: msdos_int_21h_24h(); break;
                   11568:                        case 0x25: msdos_int_21h_25h(); break;
                   11569:                        case 0x26: msdos_int_21h_26h(); break;
                   11570:                        case 0x27: msdos_int_21h_27h(); break;
                   11571:                        case 0x28: msdos_int_21h_28h(); break;
                   11572:                        case 0x29: msdos_int_21h_29h(); break;
                   11573:                        case 0x2a: msdos_int_21h_2ah(); break;
                   11574:                        case 0x2b: msdos_int_21h_2bh(); break;
                   11575:                        case 0x2c: msdos_int_21h_2ch(); break;
                   11576:                        case 0x2d: msdos_int_21h_2dh(); break;
                   11577:                        case 0x2e: msdos_int_21h_2eh(); break;
                   11578:                        case 0x2f: msdos_int_21h_2fh(); break;
                   11579:                        case 0x30: msdos_int_21h_30h(); break;
                   11580:                        case 0x31: msdos_int_21h_31h(); break;
                   11581:                        case 0x32: msdos_int_21h_32h(); break;
                   11582:                        case 0x33: msdos_int_21h_33h(); break;
                   11583:                        case 0x34: msdos_int_21h_34h(); break;
                   11584:                        case 0x35: msdos_int_21h_35h(); break;
                   11585:                        case 0x36: msdos_int_21h_36h(); break;
                   11586:                        case 0x37: msdos_int_21h_37h(); break;
                   11587:                        case 0x38: msdos_int_21h_38h(); break;
                   11588:                        case 0x39: msdos_int_21h_39h(0); break;
                   11589:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   11590:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   11591:                        case 0x3c: msdos_int_21h_3ch(); break;
                   11592:                        case 0x3d: msdos_int_21h_3dh(); break;
                   11593:                        case 0x3e: msdos_int_21h_3eh(); break;
                   11594:                        case 0x3f: msdos_int_21h_3fh(); break;
                   11595:                        case 0x40: msdos_int_21h_40h(); break;
                   11596:                        case 0x41: msdos_int_21h_41h(0); break;
                   11597:                        case 0x42: msdos_int_21h_42h(); break;
                   11598:                        case 0x43: msdos_int_21h_43h(0); break;
                   11599:                        case 0x44: msdos_int_21h_44h(); break;
                   11600:                        case 0x45: msdos_int_21h_45h(); break;
                   11601:                        case 0x46: msdos_int_21h_46h(); break;
                   11602:                        case 0x47: msdos_int_21h_47h(0); break;
                   11603:                        case 0x48: msdos_int_21h_48h(); break;
                   11604:                        case 0x49: msdos_int_21h_49h(); break;
                   11605:                        case 0x4a: msdos_int_21h_4ah(); break;
                   11606:                        case 0x4b: msdos_int_21h_4bh(); break;
                   11607:                        case 0x4c: msdos_int_21h_4ch(); break;
                   11608:                        case 0x4d: msdos_int_21h_4dh(); break;
                   11609:                        case 0x4e: msdos_int_21h_4eh(); break;
                   11610:                        case 0x4f: msdos_int_21h_4fh(); break;
                   11611:                        case 0x50: msdos_int_21h_50h(); break;
                   11612:                        case 0x51: msdos_int_21h_51h(); break;
                   11613:                        case 0x52: msdos_int_21h_52h(); break;
                   11614:                        // 0x53: translate bios parameter block to drive param bock
                   11615:                        case 0x54: msdos_int_21h_54h(); break;
                   11616:                        case 0x55: msdos_int_21h_55h(); break;
                   11617:                        case 0x56: msdos_int_21h_56h(0); break;
                   11618:                        case 0x57: msdos_int_21h_57h(); break;
                   11619:                        case 0x58: msdos_int_21h_58h(); break;
                   11620:                        case 0x59: msdos_int_21h_59h(); break;
                   11621:                        case 0x5a: msdos_int_21h_5ah(); break;
                   11622:                        case 0x5b: msdos_int_21h_5bh(); break;
                   11623:                        case 0x5c: msdos_int_21h_5ch(); break;
                   11624:                        case 0x5d: msdos_int_21h_5dh(); break;
                   11625:                        // 0x5e: ms-network
1.1.1.30! root     11626:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     11627:                        case 0x60: msdos_int_21h_60h(0); break;
                   11628:                        case 0x61: msdos_int_21h_61h(); break;
                   11629:                        case 0x62: msdos_int_21h_62h(); break;
                   11630:                        case 0x63: msdos_int_21h_63h(); break;
                   11631:                        // 0x64: set device driver lockahead flag
                   11632:                        case 0x65: msdos_int_21h_65h(); break;
                   11633:                        case 0x66: msdos_int_21h_66h(); break;
                   11634:                        case 0x67: msdos_int_21h_67h(); break;
                   11635:                        case 0x68: msdos_int_21h_68h(); break;
                   11636:                        case 0x69: msdos_int_21h_69h(); break;
                   11637:                        case 0x6a: msdos_int_21h_6ah(); break;
                   11638:                        case 0x6b: msdos_int_21h_6bh(); break;
                   11639:                        case 0x6c: msdos_int_21h_6ch(0); break;
                   11640:                        // 0x6d: find first rom program
                   11641:                        // 0x6e: find next rom program
                   11642:                        // 0x6f: get/set rom scan start address
                   11643:                        // 0x70: windows95 get/set internationalization information
                   11644:                        case 0x71:
                   11645:                                // windows95 long filename functions
                   11646:                                switch(REG8(AL)) {
                   11647:                                case 0x0d: msdos_int_21h_710dh(); break;
                   11648:                                case 0x39: msdos_int_21h_39h(1); break;
                   11649:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   11650:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   11651:                                case 0x41: msdos_int_21h_7141h(1); break;
                   11652:                                case 0x43: msdos_int_21h_43h(1); break;
                   11653:                                case 0x47: msdos_int_21h_47h(1); break;
                   11654:                                case 0x4e: msdos_int_21h_714eh(); break;
                   11655:                                case 0x4f: msdos_int_21h_714fh(); break;
                   11656:                                case 0x56: msdos_int_21h_56h(1); break;
                   11657:                                case 0x60: msdos_int_21h_60h(1); break;
                   11658:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   11659:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   11660:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   11661:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   11662:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   11663:                                case 0xa8: msdos_int_21h_71a8h(); break;
                   11664:                                // 0xa9: server create/open file
                   11665:                                case 0xaa: msdos_int_21h_71aah(); break;
                   11666:                                default:
                   11667:                                        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));
                   11668:                                        REG16(AX) = 0x7100;
                   11669:                                        m_CF = 1;
                   11670:                                        break;
                   11671:                                }
                   11672:                                break;
                   11673:                        // 0x72: Windows95 beta - LFN FindClose
                   11674:                        case 0x73:
                   11675:                                // windows95 fat32 functions
                   11676:                                switch(REG8(AL)) {
                   11677:                                case 0x00: msdos_int_21h_7300h(); break;
                   11678:                                // 0x01: set drive locking ???
                   11679:                                case 0x02: msdos_int_21h_7302h(); break;
                   11680:                                case 0x03: msdos_int_21h_7303h(); break;
                   11681:                                // 0x04: set dpb to use for formatting
                   11682:                                // 0x05: extended absolute disk read/write
                   11683:                                default:
                   11684:                                        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));
                   11685:                                        REG16(AX) = 0x7300;
                   11686:                                        m_CF = 1;
                   11687:                                        break;
                   11688:                                }
1.1       root     11689:                                break;
1.1.1.30! root     11690:                        case 0xdb: msdos_int_21h_dbh(); break;
        !          11691:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     11692:                        default:
1.1.1.22  root     11693:                                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     11694:                                REG16(AX) = 0x01;
1.1.1.3   root     11695:                                m_CF = 1;
1.1       root     11696:                                break;
                   11697:                        }
1.1.1.28  root     11698:                } catch(int error) {
                   11699:                        REG16(AX) = error;
                   11700:                        m_CF = 1;
                   11701:                } catch(...) {
                   11702:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     11703:                        m_CF = 1;
1.1       root     11704:                }
1.1.1.3   root     11705:                if(m_CF) {
1.1.1.23  root     11706:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11707:                        sda->extended_error_code = REG16(AX);
                   11708:                        switch(sda->extended_error_code) {
                   11709:                        case  4: // Too many open files
                   11710:                        case  8: // Insufficient memory
                   11711:                                sda->error_class = 1; // Out of resource
                   11712:                                break;
                   11713:                        case  5: // Access denied
                   11714:                                sda->error_class = 3; // Authorization
                   11715:                                break;
                   11716:                        case  7: // Memory control block destroyed
                   11717:                                sda->error_class = 4; // Internal
                   11718:                                break;
                   11719:                        case  2: // File not found
                   11720:                        case  3: // Path not found
                   11721:                        case 15: // Invaid drive specified
                   11722:                        case 18: // No more files
                   11723:                                sda->error_class = 8; // Not found
                   11724:                                break;
                   11725:                        case 32: // Sharing violation
                   11726:                        case 33: // Lock violation
                   11727:                                sda->error_class = 10; // Locked
                   11728:                                break;
                   11729: //                     case 16: // Removal of current directory attempted
                   11730:                        case 19: // Attempted write on protected disk
                   11731:                        case 21: // Drive not ready
                   11732: //                     case 29: // Write failure
                   11733: //                     case 30: // Read failure
                   11734: //                     case 82: // Cannot create subdirectory
                   11735:                                sda->error_class = 11; // Media
                   11736:                                break;
                   11737:                        case 80: // File already exists
                   11738:                                sda->error_class = 12; // Already exist
                   11739:                                break;
                   11740:                        default:
                   11741:                                sda->error_class = 13; // Unknown
                   11742:                                break;
                   11743:                        }
                   11744:                        sda->suggested_action = 1; // Retry
                   11745:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     11746:                }
1.1.1.26  root     11747:                if(ctrl_c_checking && ctrl_c_detected) {
                   11748:                        // raise int 23h
                   11749: #if defined(HAS_I386)
                   11750:                        m_ext = 0; // not an external interrupt
                   11751:                        i386_trap(0x23, 1, 0);
                   11752:                        m_ext = 1;
                   11753: #else
                   11754:                        PREFIX86(_interrupt)(0x23);
                   11755: #endif
                   11756:                }
1.1       root     11757:                break;
                   11758:        case 0x22:
                   11759:                fatalerror("int 22h (terminate address)\n");
                   11760:        case 0x23:
1.1.1.28  root     11761:                try {
                   11762:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   11763:                } catch(...) {
                   11764:                        fatalerror("failed to terminate the current process by int 23h\n");
                   11765:                }
1.1       root     11766:                break;
                   11767:        case 0x24:
1.1.1.28  root     11768:                try {
                   11769:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   11770:                } catch(...) {
                   11771:                        fatalerror("failed to terminate the current process by int 24h\n");
                   11772:                }
1.1       root     11773:                break;
                   11774:        case 0x25:
                   11775:                msdos_int_25h();
                   11776:                break;
                   11777:        case 0x26:
                   11778:                msdos_int_26h();
                   11779:                break;
                   11780:        case 0x27:
1.1.1.28  root     11781:                try {
                   11782:                        msdos_int_27h();
                   11783:                } catch(...) {
                   11784:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   11785:                }
1.1       root     11786:                break;
                   11787:        case 0x28:
                   11788:                Sleep(10);
                   11789:                break;
                   11790:        case 0x29:
                   11791:                msdos_int_29h();
                   11792:                break;
                   11793:        case 0x2e:
                   11794:                msdos_int_2eh();
                   11795:                break;
                   11796:        case 0x2f:
                   11797:                // multiplex interrupt
                   11798:                switch(REG8(AH)) {
1.1.1.22  root     11799:                case 0x05: msdos_int_2fh_05h(); break;
                   11800:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     11801:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30! root     11802:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     11803:                case 0x14: msdos_int_2fh_14h(); break;
                   11804:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     11805:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     11806:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     11807:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30! root     11808:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     11809:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     11810:                case 0x46: msdos_int_2fh_46h(); break;
                   11811:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     11812:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     11813:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     11814:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     11815:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.30! root     11816:                case 0x80: msdos_int_2fh_80h(); break;
1.1.1.24  root     11817:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     11818:                case 0xae: msdos_int_2fh_aeh(); break;
                   11819:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30! root     11820:                case 0xd7: msdos_int_2fh_d7h(); break;
        !          11821:                // Installation Check
        !          11822:                case 0x01: // PRINT.COM
        !          11823:                case 0x02: // PC LAN Program Redirector
        !          11824:                case 0x06: // ASSIGN
        !          11825:                case 0x08: // DRIVER.SYS
        !          11826:                case 0x10: // SHARE
        !          11827:                case 0x17: // Clibboard functions
        !          11828:                case 0x1b: // XMA2EMS.SYS
        !          11829:                case 0x23: // DR DOS 5.0 GRAFTABL
        !          11830:                case 0x27: // DR-DOR 6.0 TaskMAX
        !          11831:                case 0x2e: // Novell DOS 7 GRAFTABL
        !          11832:                case 0x45: // PROF.COM
        !          11833:                case 0x51: // ODIHELP.EXE
        !          11834:                case 0x54: // POWER.EXE
        !          11835:                case 0x56: // INTERLNK
        !          11836:                case 0x70: // License Service API
        !          11837:                case 0x7a: // Novell NetWare
        !          11838:                case 0x94: // MICRO.EXE
        !          11839:                case 0xac: // GRAPHICS.COM
        !          11840:                case 0xb0: // GRAFTABLE.COM
        !          11841:                case 0xb8: // NETWORK
        !          11842:                case 0xb9: // RECEIVER.COM
        !          11843:                case 0xbc: // EGA.SYS
        !          11844:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
        !          11845:                case 0xc0: // Novell LSL.COM
        !          11846:                case 0xd2: // PCL-838.EXE
        !          11847:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
        !          11848:                        switch(REG8(AL)) {
        !          11849:                        case 0x00:
        !          11850:                                // This is not installed
        !          11851: //                             REG8(AL) = 0x00;
        !          11852:                                break;
        !          11853:                        default:
        !          11854:                                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));
        !          11855:                                REG16(AX) = 0x01;
        !          11856:                                m_CF = 1;
        !          11857:                                break;
        !          11858:                        }
        !          11859:                        break;
1.1.1.22  root     11860:                default:
                   11861:                        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));
                   11862:                        break;
1.1       root     11863:                }
                   11864:                break;
1.1.1.24  root     11865:        case 0x33:
                   11866:                switch(REG8(AH)) {
                   11867:                case 0x00:
                   11868:                        // Mouse
                   11869:                        switch(REG8(AL)) {
                   11870:                        case 0x00: msdos_int_33h_0000h(); break;
                   11871:                        case 0x01: msdos_int_33h_0001h(); break;
                   11872:                        case 0x02: msdos_int_33h_0002h(); break;
                   11873:                        case 0x03: msdos_int_33h_0003h(); break;
                   11874:                        case 0x04: break; // position mouse cursor
                   11875:                        case 0x05: msdos_int_33h_0005h(); break;
                   11876:                        case 0x06: msdos_int_33h_0006h(); break;
                   11877:                        case 0x07: msdos_int_33h_0007h(); break;
                   11878:                        case 0x08: msdos_int_33h_0008h(); break;
                   11879:                        case 0x09: msdos_int_33h_0009h(); break;
                   11880:                        case 0x0a: break; // define text cursor
                   11881:                        case 0x0b: msdos_int_33h_000bh(); break;
                   11882:                        case 0x0c: msdos_int_33h_000ch(); break;
                   11883:                        case 0x0d: break; // light pen emulation on
                   11884:                        case 0x0e: break; // light pen emulation off
                   11885:                        case 0x0f: msdos_int_33h_000fh(); break;
                   11886:                        case 0x10: break; // define screen region for updating
                   11887:                        case 0x11: msdos_int_33h_0011h(); break;
                   11888:                        case 0x12: REG16(AX) = 0xffff; break; // set large graphics cursor block
                   11889:                        case 0x13: break; // define double-speed threshold
                   11890:                        case 0x14: msdos_int_33h_0014h(); break;
                   11891:                        case 0x15: msdos_int_33h_0015h(); break;
                   11892:                        case 0x16: msdos_int_33h_0016h(); break;
                   11893:                        case 0x17: msdos_int_33h_0017h(); break;
                   11894:                        case 0x1a: msdos_int_33h_001ah(); break;
                   11895:                        case 0x1b: msdos_int_33h_001bh(); break;
                   11896:                        case 0x1d: msdos_int_33h_001dh(); break;
                   11897:                        case 0x1e: msdos_int_33h_001eh(); break;
                   11898:                        case 0x21: msdos_int_33h_0021h(); break;
                   11899:                        case 0x22: msdos_int_33h_0022h(); break;
                   11900:                        case 0x23: msdos_int_33h_0023h(); break;
                   11901:                        case 0x24: msdos_int_33h_0024h(); break;
                   11902:                        case 0x26: msdos_int_33h_0026h(); break;
                   11903:                        case 0x2a: msdos_int_33h_002ah(); break;
                   11904:                        case 0x2f: break; // mouse hardware reset
                   11905:                        case 0x31: msdos_int_33h_0031h(); break;
                   11906:                        case 0x32: msdos_int_33h_0032h(); break;
                   11907:                        default:
                   11908:                                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));
                   11909:                                break;
                   11910:                        }
                   11911:                        break;
                   11912:                default:
                   11913:                        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));
                   11914:                        break;
                   11915:                }
                   11916:                break;
1.1.1.19  root     11917:        case 0x68:
                   11918:                // dummy interrupt for EMS (int 67h)
                   11919:                switch(REG8(AH)) {
                   11920:                case 0x40: msdos_int_67h_40h(); break;
                   11921:                case 0x41: msdos_int_67h_41h(); break;
                   11922:                case 0x42: msdos_int_67h_42h(); break;
                   11923:                case 0x43: msdos_int_67h_43h(); break;
                   11924:                case 0x44: msdos_int_67h_44h(); break;
                   11925:                case 0x45: msdos_int_67h_45h(); break;
                   11926:                case 0x46: msdos_int_67h_46h(); break;
                   11927:                case 0x47: msdos_int_67h_47h(); break;
                   11928:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     11929:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   11930:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     11931:                case 0x4b: msdos_int_67h_4bh(); break;
                   11932:                case 0x4c: msdos_int_67h_4ch(); break;
                   11933:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     11934:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     11935:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     11936:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     11937:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     11938:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     11939:                case 0x53: msdos_int_67h_53h(); break;
                   11940:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     11941:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   11942:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   11943:                case 0x57: msdos_int_67h_57h(); break;
                   11944:                case 0x58: msdos_int_67h_58h(); break;
                   11945:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   11946:                case 0x5a: msdos_int_67h_5ah(); break;
                   11947:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   11948:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   11949:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.30! root     11950:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     11951:                default:
1.1.1.22  root     11952:                        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     11953:                        REG8(AH) = 0x84;
                   11954:                        break;
                   11955:                }
                   11956:                break;
                   11957: #ifdef SUPPORT_XMS
                   11958:        case 0x69:
                   11959:                // dummy interrupt for XMS (call far)
1.1.1.28  root     11960:                try {
                   11961:                        switch(REG8(AH)) {
                   11962:                        case 0x00: msdos_call_xms_00h(); break;
                   11963:                        case 0x01: msdos_call_xms_01h(); break;
                   11964:                        case 0x02: msdos_call_xms_02h(); break;
                   11965:                        case 0x03: msdos_call_xms_03h(); break;
                   11966:                        case 0x04: msdos_call_xms_04h(); break;
                   11967:                        case 0x05: msdos_call_xms_05h(); break;
                   11968:                        case 0x06: msdos_call_xms_06h(); break;
                   11969:                        case 0x07: msdos_call_xms_07h(); break;
                   11970:                        case 0x08: msdos_call_xms_08h(); break;
                   11971:                        case 0x09: msdos_call_xms_09h(); break;
                   11972:                        case 0x0a: msdos_call_xms_0ah(); break;
                   11973:                        case 0x0b: msdos_call_xms_0bh(); break;
                   11974:                        case 0x0c: msdos_call_xms_0ch(); break;
                   11975:                        case 0x0d: msdos_call_xms_0dh(); break;
                   11976:                        case 0x0e: msdos_call_xms_0eh(); break;
                   11977:                        case 0x0f: msdos_call_xms_0fh(); break;
                   11978:                        case 0x10: msdos_call_xms_10h(); break;
                   11979:                        case 0x11: msdos_call_xms_11h(); break;
                   11980:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     11981: #if defined(HAS_I386)
                   11982:                        case 0x88: msdos_call_xms_88h(); break;
                   11983:                        case 0x89: msdos_call_xms_89h(); break;
                   11984:                        case 0x8e: msdos_call_xms_8eh(); break;
                   11985:                        case 0x8f: msdos_call_xms_8fh(); break;
                   11986: #endif
1.1.1.28  root     11987:                        default:
                   11988:                                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));
                   11989:                                REG16(AX) = 0x0000;
                   11990:                                REG8(BL) = 0x80; // function not implemented
                   11991:                                break;
                   11992:                        }
                   11993:                } catch(...) {
1.1.1.19  root     11994:                        REG16(AX) = 0x0000;
1.1.1.28  root     11995:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     11996:                }
                   11997:                break;
                   11998: #endif
                   11999:        case 0x6a:
1.1.1.24  root     12000:                // irq12 (mouse)
                   12001:                mouse_push_ax = REG16(AX);
                   12002:                mouse_push_bx = REG16(BX);
                   12003:                mouse_push_cx = REG16(CX);
                   12004:                mouse_push_dx = REG16(DX);
                   12005:                mouse_push_si = REG16(SI);
                   12006:                mouse_push_di = REG16(DI);
                   12007:                
                   12008:                if(mouse.active && mouse.call_addr.dw != 0) {
                   12009:                        REG16(AX) = mouse.status_irq;
                   12010:                        REG16(BX) = mouse.get_buttons();
                   12011:                        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   12012:                        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   12013:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   12014:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   12015:                        
                   12016:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   12017:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   12018:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   12019:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   12020:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   12021:                } else {
                   12022:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   12023:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   12024:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   12025:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   12026:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   12027:                }
                   12028:                break;
                   12029:        case 0x6b:
                   12030:                // end of irq12 (mouse)
                   12031:                REG16(AX) = mouse_push_ax;
                   12032:                REG16(BX) = mouse_push_bx;
                   12033:                REG16(CX) = mouse_push_cx;
                   12034:                REG16(DX) = mouse_push_dx;
                   12035:                REG16(SI) = mouse_push_si;
                   12036:                REG16(DI) = mouse_push_di;
                   12037:                
                   12038:                // EOI
                   12039:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   12040:                        pic[0].isr &= ~(1 << 2); // master
                   12041:                }
                   12042:                pic_update();
                   12043:                break;
                   12044:        case 0x6c:
1.1.1.19  root     12045:                // dummy interrupt for case map routine pointed in the country info
                   12046:                if(REG8(AL) >= 0x80) {
                   12047:                        char tmp[2] = {0};
                   12048:                        tmp[0] = REG8(AL);
                   12049:                        my_strupr(tmp);
                   12050:                        REG8(AL) = tmp[0];
                   12051:                }
                   12052:                break;
1.1.1.27  root     12053:        case 0x6d:
                   12054:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   12055:                REG8(AL) = 0x86; // not supported
                   12056:                m_CF = 1;
                   12057:                break;
1.1.1.8   root     12058:        case 0x70:
                   12059:        case 0x71:
                   12060:        case 0x72:
                   12061:        case 0x73:
                   12062:        case 0x74:
                   12063:        case 0x75:
                   12064:        case 0x76:
                   12065:        case 0x77:
                   12066:                // EOI
                   12067:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   12068:                        pic[0].isr &= ~(1 << 2); // master
                   12069:                }
                   12070:                pic_update();
                   12071:                break;
1.1       root     12072:        default:
1.1.1.22  root     12073: //             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     12074:                break;
                   12075:        }
                   12076:        
                   12077:        // update cursor position
                   12078:        if(cursor_moved) {
                   12079:                CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.23  root     12080:                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.15  root     12081:                if(!restore_console_on_exit) {
                   12082:                        scr_top = csbi.srWindow.Top;
                   12083:                }
1.1       root     12084:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
1.1.1.14  root     12085:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
1.1       root     12086:                cursor_moved = false;
                   12087:        }
                   12088: }
                   12089: 
                   12090: // init
                   12091: 
                   12092: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   12093: {
                   12094:        // init file handler
                   12095:        memset(file_handler, 0, sizeof(file_handler));
                   12096:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   12097:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   12098:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     12099: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     12100:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     12101: #else
                   12102:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   12103: #endif
                   12104:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     12105:        }
1.1.1.21  root     12106: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     12107:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.21  root     12108: #else
                   12109:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1       root     12110: #endif
1.1.1.21  root     12111:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   12112:        }
1.1       root     12113:        _dup2(0, DUP_STDIN);
                   12114:        _dup2(1, DUP_STDOUT);
                   12115:        _dup2(2, DUP_STDERR);
1.1.1.21  root     12116:        _dup2(3, DUP_STDAUX);
                   12117:        _dup2(4, DUP_STDPRN);
1.1       root     12118:        
1.1.1.24  root     12119:        // init mouse
                   12120:        memset(&mouse, 0, sizeof(mouse));
                   12121:        mouse.max_position.x = 8 * scr_width  - 1;
                   12122:        mouse.max_position.y = 8 * scr_height - 1;
                   12123:        mouse.mickey.x = 8;
                   12124:        mouse.mickey.y = 16;
                   12125:        
1.1.1.26  root     12126: #ifdef SUPPORT_XMS
                   12127:        // init xms
                   12128:        msdos_xms_init();
                   12129: #endif
                   12130:        
1.1       root     12131:        // init process
                   12132:        memset(process, 0, sizeof(process));
                   12133:        
1.1.1.13  root     12134:        // init dtainfo
                   12135:        msdos_dta_info_init();
                   12136:        
1.1       root     12137:        // init memory
                   12138:        memset(mem, 0, sizeof(mem));
                   12139:        
                   12140:        // bios data area
1.1.1.23  root     12141:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     12142:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   12143:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     12144:        CONSOLE_FONT_INFO cfi;
                   12145:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   12146:        
                   12147:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   12148:        text_vram_top_address = TEXT_VRAM_TOP;
                   12149:        text_vram_end_address = text_vram_top_address + regen;
                   12150:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   12151:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   12152:        
                   12153:        if(regen > 0x4000) {
                   12154:                regen = 0x8000;
                   12155:                vram_pages = 1;
                   12156:        } else if(regen > 0x2000) {
                   12157:                regen = 0x4000;
                   12158:                vram_pages = 2;
                   12159:        } else if(regen > 0x1000) {
                   12160:                regen = 0x2000;
                   12161:                vram_pages = 4;
                   12162:        } else {
                   12163:                regen = 0x1000;
                   12164:                vram_pages = 8;
                   12165:        }
1.1       root     12166:        
1.1.1.25  root     12167:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   12168:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     12169:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   12170:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     12171:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.26  root     12172: //     *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
1.1.1.25  root     12173: //     *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
                   12174:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.26  root     12175:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1       root     12176:        *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
                   12177:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     12178:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   12179:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     12180:        *(UINT16 *)(mem + 0x44e) = 0;
                   12181:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     12182:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     12183:        *(UINT8  *)(mem + 0x460) = 7;
                   12184:        *(UINT8  *)(mem + 0x461) = 7;
                   12185:        *(UINT8  *)(mem + 0x462) = 0;
                   12186:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     12187:        *(UINT8  *)(mem + 0x465) = 0x09;
                   12188:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     12189:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   12190:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   12191:        *(UINT8  *)(mem + 0x487) = 0x60;
                   12192:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.25  root     12193:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.14  root     12194:        
                   12195:        // initial screen
                   12196:        SMALL_RECT rect;
                   12197:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   12198:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   12199:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   12200:                for(int x = 0; x < scr_width; x++) {
                   12201:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   12202:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   12203:                }
                   12204:        }
1.1       root     12205:        
1.1.1.19  root     12206:        // init mcb
1.1       root     12207:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     12208:        
                   12209:        // iret table
                   12210:        // note: int 2eh vector should address the routine in command.com,
                   12211:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   12212:        // so move iret table into allocated memory block
                   12213:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
                   12214:        msdos_mcb_create(seg++, 'M', -1, IRET_SIZE >> 4);
                   12215:        IRET_TOP = seg << 4;
                   12216:        seg += IRET_SIZE >> 4;
1.1.1.25  root     12217:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     12218:        
                   12219:        // dummy xms/ems device
                   12220:        msdos_mcb_create(seg++, 'M', -1, XMS_SIZE >> 4);
                   12221:        XMS_TOP = seg << 4;
                   12222:        seg += XMS_SIZE >> 4;
                   12223:        
                   12224:        // environment
1.1       root     12225:        msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
                   12226:        int env_seg = seg;
                   12227:        int ofs = 0;
1.1.1.28  root     12228:        char env_msdos_path[ENV_SIZE] = "", env_path[ENV_SIZE] = "", env_temp[ENV_SIZE] = "", *path;
1.1       root     12229:        
1.1.1.28  root     12230:        if((path = getenv("MSDOS_PATH")) != NULL) {
                   12231:                strcpy(env_msdos_path, msdos_get_multiple_short_path(path));
                   12232:                if(env_msdos_path[0] != '\0') {
                   12233:                        strcat(env_path, env_msdos_path);
1.1.1.14  root     12234:                }
                   12235:        }
1.1.1.28  root     12236:        if((path = getenv("PATH")) != NULL) {
                   12237:                if(env_path[0] != '\0') {
                   12238:                        strcat(env_path, ";");
1.1.1.9   root     12239:                }
1.1.1.28  root     12240:                strcat(env_path, msdos_get_multiple_short_path(path));
1.1.1.9   root     12241:        }
1.1.1.28  root     12242:        if((path = getenv("MSDOS_TEMP")) != NULL || (path = getenv("TEMP")) != NULL || (path = getenv("TMP")) != NULL) {
                   12243:                strcpy(env_temp, msdos_get_multiple_short_path(path));
1.1.1.15  root     12244:        }
1.1.1.28  root     12245:        if((path = getenv("MSDOS_COMSPEC")) != NULL || (path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   12246:                strcpy(comspec_path, msdos_get_multiple_short_path(path));
1.1.1.24  root     12247:        }
1.1.1.9   root     12248:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     12249:                // lower to upper
1.1.1.28  root     12250:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     12251:                strcpy(tmp, *p);
                   12252:                for(int i = 0;; i++) {
                   12253:                        if(tmp[i] == '=') {
                   12254:                                tmp[i] = '\0';
                   12255:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     12256:                                my_strupr(name);
1.1       root     12257:                                tmp[i] = '=';
                   12258:                                break;
                   12259:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     12260:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     12261:                        }
                   12262:                }
1.1.1.18  root     12263:                if(strncmp(tmp, "MSDOS_COMSPEC=", 14) == 0) {
                   12264:                        // ignore MSDOS_COMSPEC
1.1.1.24  root     12265:                } else if(strncmp(tmp, "MSDOS_TEMP=", 11) == 0) {
                   12266:                        // ignore MSDOS_TEMP
1.1.1.28  root     12267:                } else if(standard_env && strstr(";COMSPEC;INCLUDE;LIB;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     12268:                        // ignore non standard environments
                   12269:                } else {
1.1       root     12270:                        if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     12271:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.28  root     12272:                        } else if(strncmp(tmp, "MSDOS_PATH=",  11) == 0) {
                   12273:                                if(env_msdos_path[0] != '\0') {
                   12274:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   12275:                                } else {
                   12276:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   12277:                                }
                   12278:                        } else if(strncmp(tmp, "PATH=",  5) == 0) {
                   12279:                                if(env_path[0] != '\0') {
                   12280:                                        sprintf(tmp, "PATH=%s", env_path);
                   12281:                                } else {
                   12282:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   12283:                                }
                   12284:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   12285:                                if(env_temp[0] != '\0') {
                   12286:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   12287:                                } else {
                   12288:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   12289:                                }
                   12290:                        } else if(strncmp(tmp, "TMP=",  4) == 0) {
                   12291:                                if(env_temp[0] != '\0') {
                   12292:                                        sprintf(tmp, "TMP=%s", env_temp);
                   12293:                                } else {
                   12294:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     12295:                                }
                   12296:                        }
                   12297:                        int len = strlen(tmp);
1.1.1.14  root     12298:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     12299:                                fatalerror("too many environments\n");
                   12300:                        }
                   12301:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   12302:                        ofs += len + 1;
                   12303:                }
                   12304:        }
                   12305:        seg += (ENV_SIZE >> 4);
                   12306:        
                   12307:        // psp
                   12308:        msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
                   12309:        current_psp = seg;
1.1.1.14  root     12310:        msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
1.1       root     12311:        seg += (PSP_SIZE >> 4);
                   12312:        
1.1.1.19  root     12313:        // first free mcb in conventional memory
                   12314:        msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     12315:        first_mcb = seg;
                   12316:        
1.1.1.19  root     12317:        // dummy mcb to link to umb
                   12318:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   12319:        
                   12320:        // first free mcb in upper memory block
1.1.1.8   root     12321:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     12322:        
1.1.1.29  root     12323: #ifdef SUPPORT_HMA
                   12324:        // first free mcb in high memory area
                   12325:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12326: #endif
                   12327:        
1.1.1.26  root     12328:        // interrupt vector
                   12329:        for(int i = 0; i < 0x80; i++) {
                   12330:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   12331:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   12332:        }
                   12333:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0010;       // fffd:0010 irq0 (system timer)
                   12334:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   12335:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   12336:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   12337:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   12338:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   12339:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   12340:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   12341:        
1.1.1.29  root     12342:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     12343:        static const struct {
                   12344:                UINT16 attributes;
                   12345:                char *dev_name;
                   12346:        } dummy_devices[] = {
                   12347:                {0x8013, "CON     "},
                   12348:                {0x8000, "AUX     "},
                   12349:                {0xa0c0, "PRN     "},
                   12350:                {0x8008, "CLOCK$  "},
                   12351:                {0x8000, "COM1    "},
                   12352:                {0xa0c0, "LPT1    "},
                   12353:                {0xa0c0, "LPT2    "},
                   12354:                {0xa0c0, "LPT3    "},
                   12355:                {0x8000, "COM2    "},
                   12356:                {0x8000, "COM3    "},
                   12357:                {0x8000, "COM4    "},
1.1.1.30! root     12358: //             {0xc000, "CONFIG$ "},
        !          12359:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     12360:        };
                   12361:        static const UINT8 dummy_device_routine[] = {
                   12362:                // from NUL device of Windows 98 SE
                   12363:                // or word ptr ES:[BX+03],0100
                   12364:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   12365:                // retf
                   12366:                0xcb,
                   12367:        };
1.1.1.29  root     12368:        device_t *last = NULL;
1.1.1.26  root     12369:        for(int i = 0; i < 12; i++) {
                   12370:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     12371:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   12372:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     12373:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     12374:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   12375:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     12376:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     12377:                last = device;
                   12378:        }
                   12379:        if(last != NULL) {
                   12380:                last->next_driver.w.l = 0;
                   12381:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     12382:        }
1.1.1.29  root     12383:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     12384:        
1.1.1.25  root     12385:        // dos info
                   12386:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12387:        dos_info->magic_word = 1;
                   12388:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   12389:        dos_info->first_dpb.w.l = 0;
                   12390:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   12391:        dos_info->first_sft.w.l = 0;
                   12392:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     12393:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     12394:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     12395:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     12396:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   12397:        dos_info->max_sector_len = 512;
                   12398:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   12399:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   12400:        dos_info->cds.w.l = 0;
                   12401:        dos_info->cds.w.h = CDS_TOP >> 4;
                   12402:        dos_info->fcb_table.w.l = 0;
                   12403:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   12404:        dos_info->last_drive = 'Z' - 'A' + 1;
                   12405:        dos_info->buffers_x = 20;
                   12406:        dos_info->buffers_y = 0;
                   12407:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     12408:        dos_info->nul_device.next_driver.w.l = 22;
                   12409:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     12410:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     12411:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   12412:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     12413:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   12414:        dos_info->disk_buf_heads.w.l = 0;
                   12415:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   12416:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   12417:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     12418:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     12419:        
                   12420:        char *env;
                   12421:        if((env = getenv("LASTDRIVE")) != NULL) {
                   12422:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   12423:                        dos_info->last_drive = env[0] - 'A' + 1;
                   12424:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   12425:                        dos_info->last_drive = env[0] - 'a' + 1;
                   12426:                }
                   12427:        }
                   12428:        if((env = getenv("windir")) != NULL) {
                   12429:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   12430:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   12431:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   12432:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   12433:                }
                   12434:        }
                   12435: #if defined(HAS_I386)
                   12436:        dos_info->i386_or_later = 1;
                   12437: #else
                   12438:        dos_info->i386_or_later = 0;
                   12439: #endif
                   12440:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   12441:        
1.1.1.27  root     12442:        // ems (int 67h) and xms
1.1.1.25  root     12443:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   12444:        xms_device->next_driver.w.l = 0xffff;
                   12445:        xms_device->next_driver.w.h = 0xffff;
                   12446:        xms_device->attributes = 0xc000;
1.1.1.29  root     12447:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   12448:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     12449:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   12450:        
1.1.1.26  root     12451:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   12452:        mem[XMS_TOP + 0x13] = 0x68;
                   12453:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     12454: #ifdef SUPPORT_XMS
                   12455:        if(support_xms) {
1.1.1.26  root     12456:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   12457:                mem[XMS_TOP + 0x16] = 0x69;
                   12458:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     12459:        } else
                   12460: #endif
1.1.1.26  root     12461:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     12462:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     12463:        
1.1.1.26  root     12464:        // irq12 routine (mouse)
1.1.1.24  root     12465:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   12466:        mem[0xfffd0 + 0x01] = 0x6a;
                   12467:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   12468:        mem[0xfffd0 + 0x03] = 0xff;
                   12469:        mem[0xfffd0 + 0x04] = 0xff;
                   12470:        mem[0xfffd0 + 0x05] = 0xff;
                   12471:        mem[0xfffd0 + 0x06] = 0xff;
                   12472:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   12473:        mem[0xfffd0 + 0x08] = 0x6b;
                   12474:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   12475:        
1.1.1.27  root     12476:        // case map routine
                   12477:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   12478:        mem[0xfffd0 + 0x0b] = 0x6c;
                   12479:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   12480:        
                   12481:        // font read routine
                   12482:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   12483:        mem[0xfffd0 + 0x0e] = 0x6d;
                   12484:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     12485:        
1.1.1.26  root     12486:        // irq0 routine (system time)
1.1.1.24  root     12487:        mem[0xfffd0 + 0x10] = 0xcd;     // int 1ch
                   12488:        mem[0xfffd0 + 0x11] = 0x1c;
                   12489:        mem[0xfffd0 + 0x12] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   12490:        mem[0xfffd0 + 0x13] = 0x08;
                   12491:        mem[0xfffd0 + 0x14] = 0x00;
                   12492:        mem[0xfffd0 + 0x15] = ((IRET_TOP >> 4)     ) & 0xff;
                   12493:        mem[0xfffd0 + 0x16] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     12494:        
1.1.1.26  root     12495:        // boot routine
1.1       root     12496:        mem[0xffff0] = 0xf4;    // halt
                   12497:        mem[0xffff1] = 0xcd;    // int 21h
                   12498:        mem[0xffff2] = 0x21;
                   12499:        mem[0xffff3] = 0xcb;    // retf
                   12500:        
1.1.1.24  root     12501:        mem[0xffff5] = '0';     // rom date
                   12502:        mem[0xffff6] = '2';
                   12503:        mem[0xffff7] = '/';
                   12504:        mem[0xffff8] = '2';
                   12505:        mem[0xffff9] = '2';
                   12506:        mem[0xffffa] = '/';
                   12507:        mem[0xffffb] = '0';
                   12508:        mem[0xffffc] = '6';
                   12509:        mem[0xffffe] = 0xfc;    // machine id
                   12510:        mem[0xfffff] = 0x00;
                   12511:        
1.1       root     12512:        // param block
                   12513:        // + 0: param block (22bytes)
                   12514:        // +24: fcb1/2 (20bytes)
                   12515:        // +44: command tail (128bytes)
                   12516:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12517:        param->env_seg = 0;
                   12518:        param->cmd_line.w.l = 44;
                   12519:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12520:        param->fcb1.w.l = 24;
                   12521:        param->fcb1.w.h = (WORK_TOP >> 4);
                   12522:        param->fcb2.w.l = 24;
                   12523:        param->fcb2.w.h = (WORK_TOP >> 4);
                   12524:        
                   12525:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12526:        
                   12527:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   12528:        if(argc > 1) {
                   12529:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   12530:                for(int i = 2; i < argc; i++) {
                   12531:                        char tmp[128];
                   12532:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   12533:                        strcpy(cmd_line->cmd, tmp);
                   12534:                }
                   12535:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   12536:        } else {
                   12537:                cmd_line->len = 0;
                   12538:        }
                   12539:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12540:        
                   12541:        // system file table
1.1.1.21  root     12542:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   12543:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     12544:        
1.1.1.19  root     12545:        // disk buffer header (from DOSBox)
                   12546:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   12547:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   12548:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   12549:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   12550:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   12551:        
1.1       root     12552:        // current directory structure
                   12553:        msdos_cds_update(_getdrive() - 1);
                   12554:        
                   12555:        // fcb table
                   12556:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     12557:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     12558:        
1.1.1.22  root     12559:        // error table
                   12560:        *(UINT8 *)(mem + ERR_TABLE_TOP + 0) = 0xff;
                   12561:        *(UINT8 *)(mem + ERR_TABLE_TOP + 1) = 0x04;
                   12562:        *(UINT8 *)(mem + ERR_TABLE_TOP + 2) = 0x00;
                   12563:        *(UINT8 *)(mem + ERR_TABLE_TOP + 3) = 0x00;
                   12564:        
1.1.1.17  root     12565:        // nls stuff
                   12566:        msdos_nls_tables_init();
1.1       root     12567:        
                   12568:        // execute command
1.1.1.28  root     12569:        try {
                   12570:                if(msdos_process_exec(argv[0], param, 0)) {
                   12571:                        fatalerror("'%s' not found\n", argv[0]);
                   12572:                }
                   12573:        } catch(...) {
                   12574:                // we should not reach here :-(
                   12575:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     12576:        }
                   12577:        retval = 0;
                   12578:        return(0);
                   12579: }
                   12580: 
                   12581: #define remove_std_file(path) { \
                   12582:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   12583:        if(fd != -1) { \
                   12584:                _lseek(fd, 0, SEEK_END); \
                   12585:                int size = _tell(fd); \
                   12586:                _close(fd); \
                   12587:                if(size == 0) { \
                   12588:                        remove(path); \
                   12589:                } \
                   12590:        } \
                   12591: }
                   12592: 
                   12593: void msdos_finish()
                   12594: {
                   12595:        for(int i = 0; i < MAX_FILES; i++) {
                   12596:                if(file_handler[i].valid) {
                   12597:                        _close(i);
                   12598:                }
                   12599:        }
1.1.1.21  root     12600: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     12601:        remove_std_file("stdaux.txt");
1.1.1.21  root     12602: #endif
                   12603: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     12604:        remove_std_file("stdprn.txt");
                   12605: #endif
1.1.1.30! root     12606: #ifdef SUPPORT_XMS
        !          12607:        msdos_xms_finish();
        !          12608: #endif
1.1       root     12609:        msdos_dbcs_table_finish();
                   12610: }
                   12611: 
                   12612: /* ----------------------------------------------------------------------------
                   12613:        PC/AT hardware emulation
                   12614: ---------------------------------------------------------------------------- */
                   12615: 
                   12616: void hardware_init()
                   12617: {
1.1.1.3   root     12618:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     12619:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     12620:        m_IF = 1;
1.1.1.3   root     12621: #if defined(HAS_I386)
1.1       root     12622:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   12623:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     12624: #endif
                   12625:        i386_set_a20_line(0);
1.1.1.14  root     12626:        
1.1.1.19  root     12627:        ems_init();
1.1.1.25  root     12628:        dma_init();
1.1       root     12629:        pic_init();
1.1.1.25  root     12630:        pio_init();
1.1.1.8   root     12631: #ifdef PIT_ALWAYS_RUNNING
                   12632:        pit_init();
                   12633: #else
1.1       root     12634:        pit_active = 0;
                   12635: #endif
1.1.1.25  root     12636:        sio_init();
1.1.1.8   root     12637:        cmos_init();
                   12638:        kbd_init();
1.1       root     12639: }
                   12640: 
1.1.1.10  root     12641: void hardware_finish()
                   12642: {
                   12643: #if defined(HAS_I386)
                   12644:        vtlb_free(m_vtlb);
                   12645: #endif
1.1.1.19  root     12646:        ems_finish();
1.1.1.25  root     12647:        sio_finish();
1.1.1.10  root     12648: }
                   12649: 
1.1.1.28  root     12650: void hardware_release()
                   12651: {
                   12652:        // release hardware resources when this program will be terminated abnormally
                   12653: #ifdef EXPORT_DEBUG_TO_FILE
                   12654:        if(fdebug != NULL) {
                   12655:                fclose(fdebug);
                   12656:                fdebug = NULL;
                   12657:        }
                   12658: #endif
                   12659: #if defined(HAS_I386)
                   12660:        vtlb_free(m_vtlb);
                   12661: #endif
                   12662:        ems_release();
                   12663:        sio_release();
                   12664: }
                   12665: 
1.1       root     12666: void hardware_run()
                   12667: {
                   12668:        int ops = 0;
                   12669:        
1.1.1.22  root     12670: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     12671:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.22  root     12672:        fdebug = fopen("debug.log", "w");
                   12673: #endif
1.1.1.3   root     12674:        while(!m_halted) {
1.1.1.22  root     12675: #ifdef ENABLE_DEBUG_DASM
                   12676:                if(dasm > 0) {
1.1       root     12677:                        char buffer[256];
1.1.1.3   root     12678: #if defined(HAS_I386)
1.1.1.22  root     12679:                        UINT32 flags = get_flags();
1.1.1.19  root     12680:                        UINT32 eip = m_eip;
1.1.1.3   root     12681: #else
1.1.1.22  root     12682:                        UINT32 flags = CompressFlags();
1.1.1.19  root     12683:                        UINT32 eip = m_pc - SREG_BASE(CS);
1.1.1.3   root     12684: #endif
                   12685:                        UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1       root     12686:                        
1.1.1.3   root     12687: #if defined(HAS_I386)
                   12688:                        if(m_operand_size) {
1.1       root     12689:                                CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3   root     12690:                        } else
                   12691: #endif
                   12692:                        CPU_DISASSEMBLE_CALL(x86_16);
1.1.1.22  root     12693:                        
                   12694:                        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",
                   12695:                        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,
                   12696: #if defined(HAS_I386)
                   12697:                        PROTECTED_MODE ? "PE" : "--",
                   12698: #else
                   12699:                        "--",
                   12700: #endif
                   12701:                        (flags & 0x40000) ? 'A' : '-',
                   12702:                        (flags & 0x20000) ? 'V' : '-',
                   12703:                        (flags & 0x10000) ? 'R' : '-',
                   12704:                        (flags & 0x04000) ? 'N' : '-',
                   12705:                        (flags & 0x02000) ? '1' : '0',
                   12706:                        (flags & 0x01000) ? '1' : '0',
                   12707:                        (flags & 0x00800) ? 'O' : '-',
                   12708:                        (flags & 0x00400) ? 'D' : '-',
                   12709:                        (flags & 0x00200) ? 'I' : '-',
                   12710:                        (flags & 0x00100) ? 'T' : '-',
                   12711:                        (flags & 0x00080) ? 'S' : '-',
                   12712:                        (flags & 0x00040) ? 'Z' : '-',
                   12713:                        (flags & 0x00010) ? 'A' : '-',
                   12714:                        (flags & 0x00004) ? 'P' : '-',
                   12715:                        (flags & 0x00001) ? 'C' : '-');
                   12716:                        fprintf(fdebug, "%04X:%04X\t%s\n", SREG(CS), (unsigned)eip, buffer);
                   12717:                        dasm--;
1.1       root     12718:                }
                   12719: #endif
1.1.1.3   root     12720: #if defined(HAS_I386)
                   12721:                m_cycles = 1;
1.1       root     12722:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     12723: #else
                   12724:                CPU_EXECUTE_CALL(CPU_MODEL);
                   12725: #endif
1.1.1.14  root     12726: #if defined(HAS_I386)
                   12727:                if(m_eip != m_prev_eip) {
                   12728: #else
                   12729:                if(m_pc != m_prevpc) {
                   12730: #endif
                   12731:                        iops++;
                   12732:                }
1.1.1.8   root     12733:                if(++ops == 16384) {
1.1       root     12734:                        hardware_update();
                   12735:                        ops = 0;
                   12736:                }
                   12737:        }
1.1.1.22  root     12738: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     12739:        if(fdebug != NULL) {
                   12740:                fclose(fdebug);
                   12741:                fdebug = NULL;
                   12742:        }
1.1.1.22  root     12743: #endif
1.1       root     12744: }
                   12745: 
                   12746: void hardware_update()
                   12747: {
1.1.1.8   root     12748:        static UINT32 prev_time = 0;
                   12749:        UINT32 cur_time = timeGetTime();
                   12750:        
                   12751:        if(prev_time != cur_time) {
                   12752:                // update pit and raise irq0
                   12753: #ifndef PIT_ALWAYS_RUNNING
                   12754:                if(pit_active)
                   12755: #endif
                   12756:                {
                   12757:                        if(pit_run(0, cur_time)) {
                   12758:                                pic_req(0, 0, 1);
                   12759:                        }
                   12760:                        pit_run(1, cur_time);
                   12761:                        pit_run(2, cur_time);
                   12762:                }
1.1.1.24  root     12763:                
1.1.1.25  root     12764:                // update sio and raise irq4/3
1.1.1.29  root     12765:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     12766:                        sio_update(c);
                   12767:                }
                   12768:                
1.1.1.24  root     12769:                // update keyboard and mouse
1.1.1.14  root     12770:                static UINT32 prev_tick = 0;
                   12771:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     12772:                
1.1.1.14  root     12773:                if(prev_tick != cur_tick) {
                   12774:                        // update keyboard flags
                   12775:                        UINT8 state;
1.1.1.24  root     12776:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   12777:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   12778:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   12779:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   12780:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   12781:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   12782:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   12783:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     12784:                        mem[0x417] = state;
                   12785:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   12786:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   12787:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   12788:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     12789: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   12790: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     12791:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   12792:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   12793:                        mem[0x418] = state;
                   12794:                        
1.1.1.24  root     12795:                        // update console input if needed
                   12796:                        if(!key_changed || mouse.active) {
                   12797:                                update_console_input();
                   12798:                        }
                   12799:                        
                   12800:                        // raise irq1 if key is pressed/released
                   12801:                        if(key_changed) {
1.1.1.8   root     12802:                                pic_req(0, 1, 1);
1.1.1.24  root     12803:                                key_changed = false;
                   12804:                        }
                   12805:                        
                   12806:                        // raise irq12 if mouse status is changed
                   12807:                        if(mouse.status & mouse.call_mask) {
                   12808:                                if(mouse.active) {
                   12809:                                        pic_req(1, 4, 1);
                   12810:                                        mouse.status_irq = mouse.status & mouse.call_mask;
                   12811:                                }
                   12812:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     12813:                        }
1.1.1.24  root     12814:                        
1.1.1.14  root     12815:                        prev_tick = cur_tick;
1.1.1.8   root     12816:                }
1.1.1.24  root     12817:                
1.1.1.19  root     12818:                // update daily timer counter
                   12819:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     12820:                
1.1.1.8   root     12821:                prev_time = cur_time;
1.1       root     12822:        }
                   12823: }
                   12824: 
1.1.1.19  root     12825: // ems
                   12826: 
                   12827: void ems_init()
                   12828: {
                   12829:        memset(ems_handles, 0, sizeof(ems_handles));
                   12830:        memset(ems_pages, 0, sizeof(ems_pages));
                   12831:        free_ems_pages = MAX_EMS_PAGES;
                   12832: }
                   12833: 
                   12834: void ems_finish()
                   12835: {
1.1.1.28  root     12836:        ems_release();
                   12837: }
                   12838: 
                   12839: void ems_release()
                   12840: {
1.1.1.19  root     12841:        for(int i = 0; i < MAX_EMS_HANDLES; i++) {
1.1.1.28  root     12842:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     12843:                        free(ems_handles[i].buffer);
                   12844:                        ems_handles[i].buffer = NULL;
                   12845:                }
                   12846:        }
                   12847: }
                   12848: 
                   12849: void ems_allocate_pages(int handle, int pages)
                   12850: {
                   12851:        if(pages > 0) {
                   12852:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   12853:        } else {
                   12854:                ems_handles[handle].buffer = NULL;
                   12855:        }
                   12856:        ems_handles[handle].pages = pages;
                   12857:        ems_handles[handle].allocated = true;
                   12858:        free_ems_pages -= pages;
                   12859: }
                   12860: 
                   12861: void ems_reallocate_pages(int handle, int pages)
                   12862: {
                   12863:        if(ems_handles[handle].allocated) {
                   12864:                if(ems_handles[handle].pages != pages) {
                   12865:                        UINT8 *new_buffer = NULL;
                   12866:                        
                   12867:                        if(pages > 0) {
                   12868:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   12869:                        }
                   12870:                        if(ems_handles[handle].buffer) {
                   12871:                                if(new_buffer) {
                   12872:                                        if(pages > ems_handles[handle].pages) {
                   12873:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   12874:                                        } else {
                   12875:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   12876:                                        }
                   12877:                                }
                   12878:                                free(ems_handles[handle].buffer);
                   12879:                                ems_handles[handle].buffer = NULL;
                   12880:                        }
                   12881:                        free_ems_pages += ems_handles[handle].pages;
                   12882:                        
                   12883:                        ems_handles[handle].buffer = new_buffer;
                   12884:                        ems_handles[handle].pages = pages;
                   12885:                        free_ems_pages -= pages;
                   12886:                }
                   12887:        } else {
                   12888:                ems_allocate_pages(handle, pages);
                   12889:        }
                   12890: }
                   12891: 
                   12892: void ems_release_pages(int handle)
                   12893: {
                   12894:        if(ems_handles[handle].allocated) {
                   12895:                if(ems_handles[handle].buffer) {
                   12896:                        free(ems_handles[handle].buffer);
                   12897:                        ems_handles[handle].buffer = NULL;
                   12898:                }
                   12899:                free_ems_pages += ems_handles[handle].pages;
                   12900:                ems_handles[handle].allocated = false;
                   12901:        }
                   12902: }
                   12903: 
                   12904: void ems_map_page(int physical, int handle, int logical)
                   12905: {
                   12906:        if(ems_pages[physical].mapped) {
                   12907:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   12908:                        return;
                   12909:                }
                   12910:                ems_unmap_page(physical);
                   12911:        }
                   12912:        if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   12913:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   12914:        }
                   12915:        ems_pages[physical].handle = handle;
                   12916:        ems_pages[physical].page = logical;
                   12917:        ems_pages[physical].mapped = true;
                   12918: }
                   12919: 
                   12920: void ems_unmap_page(int physical)
                   12921: {
                   12922:        if(ems_pages[physical].mapped) {
                   12923:                int handle = ems_pages[physical].handle;
                   12924:                int logical = ems_pages[physical].page;
                   12925:                
                   12926:                if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   12927:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   12928:                }
                   12929:                ems_pages[physical].mapped = false;
                   12930:        }
                   12931: }
                   12932: 
1.1.1.25  root     12933: // dma
1.1       root     12934: 
1.1.1.25  root     12935: void dma_init()
1.1       root     12936: {
1.1.1.26  root     12937:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     12938:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     12939: //             for(int ch = 0; ch < 4; ch++) {
                   12940: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   12941: //             }
1.1.1.25  root     12942:                dma_reset(c);
                   12943:        }
1.1       root     12944: }
                   12945: 
1.1.1.25  root     12946: void dma_reset(int c)
1.1       root     12947: {
1.1.1.25  root     12948:        dma[c].low_high = false;
                   12949:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   12950:        dma[c].mask = 0xff;
                   12951: }
                   12952: 
                   12953: void dma_write(int c, UINT32 addr, UINT8 data)
                   12954: {
                   12955:        int ch = (addr >> 1) & 3;
                   12956:        UINT8 bit = 1 << (data & 3);
                   12957:        
                   12958:        switch(addr & 0x0f) {
                   12959:        case 0x00: case 0x02: case 0x04: case 0x06:
                   12960:                if(dma[c].low_high) {
                   12961:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     12962:                } else {
1.1.1.25  root     12963:                        dma[c].ch[ch].bareg.b.l = data;
                   12964:                }
                   12965:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   12966:                dma[c].low_high = !dma[c].low_high;
                   12967:                break;
                   12968:        case 0x01: case 0x03: case 0x05: case 0x07:
                   12969:                if(dma[c].low_high) {
                   12970:                        dma[c].ch[ch].bcreg.b.h = data;
                   12971:                } else {
                   12972:                        dma[c].ch[ch].bcreg.b.l = data;
                   12973:                }
                   12974:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   12975:                dma[c].low_high = !dma[c].low_high;
                   12976:                break;
                   12977:        case 0x08:
                   12978:                // command register
                   12979:                dma[c].cmd = data;
                   12980:                break;
                   12981:        case 0x09:
                   12982:                // dma[c].request register
                   12983:                if(data & 4) {
                   12984:                        if(!(dma[c].req & bit)) {
                   12985:                                dma[c].req |= bit;
                   12986: //                             dma_run(c, ch);
                   12987:                        }
                   12988:                } else {
                   12989:                        dma[c].req &= ~bit;
                   12990:                }
                   12991:                break;
                   12992:        case 0x0a:
                   12993:                // single mask register
                   12994:                if(data & 4) {
                   12995:                        dma[c].mask |= bit;
                   12996:                } else {
                   12997:                        dma[c].mask &= ~bit;
                   12998:                }
                   12999:                break;
                   13000:        case 0x0b:
                   13001:                // mode register
                   13002:                dma[c].ch[data & 3].mode = data;
                   13003:                break;
                   13004:        case 0x0c:
                   13005:                dma[c].low_high = false;
                   13006:                break;
                   13007:        case 0x0d:
                   13008:                // clear master
                   13009:                dma_reset(c);
                   13010:                break;
                   13011:        case 0x0e:
                   13012:                // clear mask register
                   13013:                dma[c].mask = 0;
                   13014:                break;
                   13015:        case 0x0f:
                   13016:                // all mask register
                   13017:                dma[c].mask = data & 0x0f;
                   13018:                break;
                   13019:        }
                   13020: }
                   13021: 
                   13022: UINT8 dma_read(int c, UINT32 addr)
                   13023: {
                   13024:        int ch = (addr >> 1) & 3;
                   13025:        UINT8 val = 0xff;
                   13026:        
                   13027:        switch(addr & 0x0f) {
                   13028:        case 0x00: case 0x02: case 0x04: case 0x06:
                   13029:                if(dma[c].low_high) {
                   13030:                        val = dma[c].ch[ch].areg.b.h;
                   13031:                } else {
                   13032:                        val = dma[c].ch[ch].areg.b.l;
                   13033:                }
                   13034:                dma[c].low_high = !dma[c].low_high;
                   13035:                return(val);
                   13036:        case 0x01: case 0x03: case 0x05: case 0x07:
                   13037:                if(dma[c].low_high) {
                   13038:                        val = dma[c].ch[ch].creg.b.h;
                   13039:                } else {
                   13040:                        val = dma[c].ch[ch].creg.b.l;
                   13041:                }
                   13042:                dma[c].low_high = !dma[c].low_high;
                   13043:                return(val);
                   13044:        case 0x08:
                   13045:                // status register
                   13046:                val = (dma[c].req << 4) | dma[c].tc;
                   13047:                dma[c].tc = 0;
                   13048:                return(val);
                   13049:        case 0x0d:
1.1.1.26  root     13050:                // temporary register (intel 82374 does not support)
1.1.1.25  root     13051:                return(dma[c].tmp & 0xff);
1.1.1.26  root     13052:        case 0x0f:
                   13053:                // mask register (intel 82374 does support)
                   13054:                return(dma[c].mask);
1.1.1.25  root     13055:        }
                   13056:        return(0xff);
                   13057: }
                   13058: 
                   13059: void dma_page_write(int c, int ch, UINT8 data)
                   13060: {
                   13061:        dma[c].ch[ch].pagereg = data;
                   13062: }
                   13063: 
                   13064: UINT8 dma_page_read(int c, int ch)
                   13065: {
                   13066:        return(dma[c].ch[ch].pagereg);
                   13067: }
                   13068: 
                   13069: void dma_run(int c, int ch)
                   13070: {
                   13071:        UINT8 bit = 1 << ch;
                   13072:        
                   13073:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   13074:                // execute dma
                   13075:                while(dma[c].req & bit) {
                   13076:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   13077:                                // memory -> memory
                   13078:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   13079:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   13080:                                
                   13081:                                if(c == 0) {
                   13082:                                        dma[c].tmp = read_byte(saddr);
                   13083:                                        write_byte(daddr, dma[c].tmp);
                   13084:                                } else {
                   13085:                                        dma[c].tmp = read_word(saddr << 1);
                   13086:                                        write_word(daddr << 1, dma[c].tmp);
                   13087:                                }
                   13088:                                if(!(dma[c].cmd & 0x02)) {
                   13089:                                        if(dma[c].ch[0].mode & 0x20) {
                   13090:                                                dma[c].ch[0].areg.w--;
                   13091:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   13092:                                                        dma[c].ch[0].pagereg--;
                   13093:                                                }
                   13094:                                        } else {
                   13095:                                                dma[c].ch[0].areg.w++;
                   13096:                                                if(dma[c].ch[0].areg.w == 0) {
                   13097:                                                        dma[c].ch[0].pagereg++;
                   13098:                                                }
                   13099:                                        }
                   13100:                                }
                   13101:                                if(dma[c].ch[1].mode & 0x20) {
                   13102:                                        dma[c].ch[1].areg.w--;
                   13103:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   13104:                                                dma[c].ch[1].pagereg--;
                   13105:                                        }
                   13106:                                } else {
                   13107:                                        dma[c].ch[1].areg.w++;
                   13108:                                        if(dma[c].ch[1].areg.w == 0) {
                   13109:                                                dma[c].ch[1].pagereg++;
                   13110:                                        }
                   13111:                                }
                   13112:                                
                   13113:                                // check dma condition
                   13114:                                if(dma[c].ch[0].creg.w-- == 0) {
                   13115:                                        if(dma[c].ch[0].mode & 0x10) {
                   13116:                                                // self initialize
                   13117:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   13118:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   13119:                                        } else {
                   13120: //                                             dma[c].mask |= bit;
                   13121:                                        }
                   13122:                                }
                   13123:                                if(dma[c].ch[1].creg.w-- == 0) {
                   13124:                                        // terminal count
                   13125:                                        if(dma[c].ch[1].mode & 0x10) {
                   13126:                                                // self initialize
                   13127:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   13128:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   13129:                                        } else {
                   13130:                                                dma[c].mask |= bit;
                   13131:                                        }
                   13132:                                        dma[c].req &= ~bit;
                   13133:                                        dma[c].tc |= bit;
                   13134:                                }
                   13135:                        } else {
                   13136:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   13137:                                
                   13138:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   13139:                                        // verify
                   13140:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   13141:                                        // io -> memory
                   13142:                                        if(c == 0) {
                   13143:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   13144:                                                write_byte(addr, dma[c].tmp);
                   13145:                                        } else {
                   13146:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   13147:                                                write_word(addr << 1, dma[c].tmp);
                   13148:                                        }
                   13149:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   13150:                                        // memory -> io
                   13151:                                        if(c == 0) {
                   13152:                                                dma[c].tmp = read_byte(addr);
                   13153:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   13154:                                        } else {
                   13155:                                                dma[c].tmp = read_word(addr << 1);
                   13156:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   13157:                                        }
                   13158:                                }
                   13159:                                if(dma[c].ch[ch].mode & 0x20) {
                   13160:                                        dma[c].ch[ch].areg.w--;
                   13161:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   13162:                                                dma[c].ch[ch].pagereg--;
                   13163:                                        }
                   13164:                                } else {
                   13165:                                        dma[c].ch[ch].areg.w++;
                   13166:                                        if(dma[c].ch[ch].areg.w == 0) {
                   13167:                                                dma[c].ch[ch].pagereg++;
                   13168:                                        }
                   13169:                                }
                   13170:                                
                   13171:                                // check dma condition
                   13172:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   13173:                                        // terminal count
                   13174:                                        if(dma[c].ch[ch].mode & 0x10) {
                   13175:                                                // self initialize
                   13176:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   13177:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   13178:                                        } else {
                   13179:                                                dma[c].mask |= bit;
                   13180:                                        }
                   13181:                                        dma[c].req &= ~bit;
                   13182:                                        dma[c].tc |= bit;
                   13183:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   13184:                                        // single mode
                   13185:                                        break;
                   13186:                                }
                   13187:                        }
                   13188:                }
                   13189:        }
                   13190: }
                   13191: 
                   13192: // pic
                   13193: 
                   13194: void pic_init()
                   13195: {
                   13196:        memset(pic, 0, sizeof(pic));
                   13197:        pic[0].imr = pic[1].imr = 0xff;
                   13198:        
                   13199:        // from bochs bios
                   13200:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   13201:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   13202:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   13203:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   13204:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   13205:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   13206:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   13207:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   13208:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   13209: }
                   13210: 
                   13211: void pic_write(int c, UINT32 addr, UINT8 data)
                   13212: {
                   13213:        if(addr & 1) {
                   13214:                if(pic[c].icw2_r) {
                   13215:                        // icw2
                   13216:                        pic[c].icw2 = data;
                   13217:                        pic[c].icw2_r = 0;
                   13218:                } else if(pic[c].icw3_r) {
                   13219:                        // icw3
                   13220:                        pic[c].icw3 = data;
                   13221:                        pic[c].icw3_r = 0;
                   13222:                } else if(pic[c].icw4_r) {
                   13223:                        // icw4
                   13224:                        pic[c].icw4 = data;
                   13225:                        pic[c].icw4_r = 0;
                   13226:                } else {
                   13227:                        // ocw1
1.1       root     13228:                        pic[c].imr = data;
                   13229:                }
                   13230:        } else {
                   13231:                if(data & 0x10) {
                   13232:                        // icw1
                   13233:                        pic[c].icw1 = data;
                   13234:                        pic[c].icw2_r = 1;
                   13235:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   13236:                        pic[c].icw4_r = data & 1;
                   13237:                        pic[c].irr = 0;
                   13238:                        pic[c].isr = 0;
                   13239:                        pic[c].imr = 0;
                   13240:                        pic[c].prio = 0;
                   13241:                        if(!(pic[c].icw1 & 1)) {
                   13242:                                pic[c].icw4 = 0;
                   13243:                        }
                   13244:                        pic[c].ocw3 = 0;
                   13245:                } else if(data & 8) {
                   13246:                        // ocw3
                   13247:                        if(!(data & 2)) {
                   13248:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   13249:                        }
                   13250:                        if(!(data & 0x40)) {
                   13251:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   13252:                        }
                   13253:                        pic[c].ocw3 = data;
                   13254:                } else {
                   13255:                        // ocw2
                   13256:                        int level = 0;
                   13257:                        if(data & 0x40) {
                   13258:                                level = data & 7;
                   13259:                        } else {
                   13260:                                if(!pic[c].isr) {
                   13261:                                        return;
                   13262:                                }
                   13263:                                level = pic[c].prio;
                   13264:                                while(!(pic[c].isr & (1 << level))) {
                   13265:                                        level = (level + 1) & 7;
                   13266:                                }
                   13267:                        }
                   13268:                        if(data & 0x80) {
                   13269:                                pic[c].prio = (level + 1) & 7;
                   13270:                        }
                   13271:                        if(data & 0x20) {
                   13272:                                pic[c].isr &= ~(1 << level);
                   13273:                        }
                   13274:                }
                   13275:        }
                   13276:        pic_update();
                   13277: }
                   13278: 
                   13279: UINT8 pic_read(int c, UINT32 addr)
                   13280: {
                   13281:        if(addr & 1) {
                   13282:                return(pic[c].imr);
                   13283:        } else {
                   13284:                // polling mode is not supported...
                   13285:                //if(pic[c].ocw3 & 4) {
                   13286:                //      return ???;
                   13287:                //}
                   13288:                if(pic[c].ocw3 & 1) {
                   13289:                        return(pic[c].isr);
                   13290:                } else {
                   13291:                        return(pic[c].irr);
                   13292:                }
                   13293:        }
                   13294: }
                   13295: 
                   13296: void pic_req(int c, int level, int signal)
                   13297: {
                   13298:        if(signal) {
                   13299:                pic[c].irr |= (1 << level);
                   13300:        } else {
                   13301:                pic[c].irr &= ~(1 << level);
                   13302:        }
                   13303:        pic_update();
                   13304: }
                   13305: 
                   13306: int pic_ack()
                   13307: {
                   13308:        // ack (INTA=L)
                   13309:        pic[pic_req_chip].isr |= pic_req_bit;
                   13310:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   13311:        if(pic_req_chip > 0) {
                   13312:                // update isr and irr of master
                   13313:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   13314:                pic[pic_req_chip - 1].isr |= slave;
                   13315:                pic[pic_req_chip - 1].irr &= ~slave;
                   13316:        }
                   13317:        //if(pic[pic_req_chip].icw4 & 1) {
                   13318:                // 8086 mode
                   13319:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   13320:        //} else {
                   13321:        //      // 8080 mode
                   13322:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   13323:        //      if(pic[pic_req_chip].icw1 & 4) {
                   13324:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   13325:        //      } else {
                   13326:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   13327:        //      }
                   13328:        //      vector = 0xcd | (addr << 8);
                   13329:        //}
                   13330:        if(pic[pic_req_chip].icw4 & 2) {
                   13331:                // auto eoi
                   13332:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   13333:        }
                   13334:        return(vector);
                   13335: }
                   13336: 
                   13337: void pic_update()
                   13338: {
                   13339:        for(int c = 0; c < 2; c++) {
                   13340:                UINT8 irr = pic[c].irr;
                   13341:                if(c + 1 < 2) {
                   13342:                        // this is master
                   13343:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   13344:                                // request from slave
                   13345:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   13346:                        }
                   13347:                }
                   13348:                irr &= (~pic[c].imr);
                   13349:                if(!irr) {
                   13350:                        break;
                   13351:                }
                   13352:                if(!(pic[c].ocw3 & 0x20)) {
                   13353:                        irr |= pic[c].isr;
                   13354:                }
                   13355:                int level = pic[c].prio;
                   13356:                UINT8 bit = 1 << level;
                   13357:                while(!(irr & bit)) {
                   13358:                        level = (level + 1) & 7;
                   13359:                        bit = 1 << level;
                   13360:                }
                   13361:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   13362:                        // check slave
                   13363:                        continue;
                   13364:                }
                   13365:                if(pic[c].isr & bit) {
                   13366:                        break;
                   13367:                }
                   13368:                // interrupt request
                   13369:                pic_req_chip = c;
                   13370:                pic_req_level = level;
                   13371:                pic_req_bit = bit;
1.1.1.3   root     13372:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     13373:                return;
                   13374:        }
1.1.1.3   root     13375:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     13376: }
1.1       root     13377: 
1.1.1.25  root     13378: // pio
                   13379: 
                   13380: void pio_init()
                   13381: {
1.1.1.26  root     13382:        memset(pio, 0, sizeof(pio));
1.1.1.25  root     13383:        for(int c = 0; c < 2; c++) {
                   13384:                pio[c].stat = 0xde;
                   13385:                pio[c].ctrl = 0x0c;
                   13386:        }
                   13387: }
                   13388: 
                   13389: void pio_write(int c, UINT32 addr, UINT8 data)
                   13390: {
                   13391:        switch(addr & 3) {
                   13392:        case 0:
                   13393:                pio[c].data = data;
                   13394:                break;
                   13395:        case 2:
                   13396:                pio[c].ctrl = data;
                   13397:                break;
                   13398:        }
                   13399: }
                   13400: 
                   13401: UINT8 pio_read(int c, UINT32 addr)
                   13402: {
                   13403:        switch(addr & 3) {
                   13404:        case 0:
                   13405:                return(pio[c].data);
                   13406:        case 1:
                   13407:                return(pio[c].stat);
                   13408:        case 2:
                   13409:                return(pio[c].ctrl);
                   13410:        }
                   13411:        return(0xff);
                   13412: }
                   13413: 
1.1       root     13414: // pit
                   13415: 
1.1.1.22  root     13416: #define PIT_FREQ 1193182ULL
1.1       root     13417: #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)
                   13418: 
                   13419: void pit_init()
                   13420: {
1.1.1.8   root     13421:        memset(pit, 0, sizeof(pit));
1.1       root     13422:        for(int ch = 0; ch < 3; ch++) {
                   13423:                pit[ch].count = 0x10000;
                   13424:                pit[ch].ctrl_reg = 0x34;
                   13425:                pit[ch].mode = 3;
                   13426:        }
                   13427:        
                   13428:        // from bochs bios
                   13429:        pit_write(3, 0x34);
                   13430:        pit_write(0, 0x00);
                   13431:        pit_write(0, 0x00);
                   13432: }
                   13433: 
                   13434: void pit_write(int ch, UINT8 val)
                   13435: {
1.1.1.8   root     13436: #ifndef PIT_ALWAYS_RUNNING
1.1       root     13437:        if(!pit_active) {
                   13438:                pit_active = 1;
                   13439:                pit_init();
                   13440:        }
1.1.1.8   root     13441: #endif
1.1       root     13442:        switch(ch) {
                   13443:        case 0:
                   13444:        case 1:
                   13445:        case 2:
                   13446:                // write count register
                   13447:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   13448:                        if(pit[ch].ctrl_reg & 0x10) {
                   13449:                                pit[ch].low_write = 1;
                   13450:                        }
                   13451:                        if(pit[ch].ctrl_reg & 0x20) {
                   13452:                                pit[ch].high_write = 1;
                   13453:                        }
                   13454:                }
                   13455:                if(pit[ch].low_write) {
                   13456:                        pit[ch].count_reg = val;
                   13457:                        pit[ch].low_write = 0;
                   13458:                } else if(pit[ch].high_write) {
                   13459:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   13460:                                pit[ch].count_reg = val << 8;
                   13461:                        } else {
                   13462:                                pit[ch].count_reg |= val << 8;
                   13463:                        }
                   13464:                        pit[ch].high_write = 0;
                   13465:                }
                   13466:                // start count
1.1.1.8   root     13467:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   13468:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   13469:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   13470:                                pit[ch].prev_time = timeGetTime();
                   13471:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     13472:                        }
                   13473:                }
                   13474:                break;
                   13475:        case 3: // ctrl reg
                   13476:                if((val & 0xc0) == 0xc0) {
                   13477:                        // i8254 read-back command
                   13478:                        for(ch = 0; ch < 3; ch++) {
                   13479:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   13480:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   13481:                                        pit[ch].status_latched = 1;
                   13482:                                }
                   13483:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   13484:                                        pit_latch_count(ch);
                   13485:                                }
                   13486:                        }
                   13487:                        break;
                   13488:                }
                   13489:                ch = (val >> 6) & 3;
                   13490:                if(val & 0x30) {
                   13491:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   13492:                        pit[ch].mode = modes[(val >> 1) & 7];
                   13493:                        pit[ch].count_latched = 0;
                   13494:                        pit[ch].low_read = pit[ch].high_read = 0;
                   13495:                        pit[ch].low_write = pit[ch].high_write = 0;
                   13496:                        pit[ch].ctrl_reg = val;
                   13497:                        // stop count
1.1.1.8   root     13498:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     13499:                        pit[ch].count_reg = 0;
                   13500:                } else if(!pit[ch].count_latched) {
                   13501:                        pit_latch_count(ch);
                   13502:                }
                   13503:                break;
                   13504:        }
                   13505: }
                   13506: 
                   13507: UINT8 pit_read(int ch)
                   13508: {
1.1.1.8   root     13509: #ifndef PIT_ALWAYS_RUNNING
1.1       root     13510:        if(!pit_active) {
                   13511:                pit_active = 1;
                   13512:                pit_init();
                   13513:        }
1.1.1.8   root     13514: #endif
1.1       root     13515:        switch(ch) {
                   13516:        case 0:
                   13517:        case 1:
                   13518:        case 2:
                   13519:                if(pit[ch].status_latched) {
                   13520:                        pit[ch].status_latched = 0;
                   13521:                        return(pit[ch].status);
                   13522:                }
                   13523:                // if not latched, through current count
                   13524:                if(!pit[ch].count_latched) {
                   13525:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   13526:                                pit_latch_count(ch);
                   13527:                        }
                   13528:                }
                   13529:                // return latched count
                   13530:                if(pit[ch].low_read) {
                   13531:                        pit[ch].low_read = 0;
                   13532:                        if(!pit[ch].high_read) {
                   13533:                                pit[ch].count_latched = 0;
                   13534:                        }
                   13535:                        return(pit[ch].latch & 0xff);
                   13536:                } else if(pit[ch].high_read) {
                   13537:                        pit[ch].high_read = 0;
                   13538:                        pit[ch].count_latched = 0;
                   13539:                        return((pit[ch].latch >> 8) & 0xff);
                   13540:                }
                   13541:        }
                   13542:        return(0xff);
                   13543: }
                   13544: 
1.1.1.8   root     13545: int pit_run(int ch, UINT32 cur_time)
1.1       root     13546: {
1.1.1.8   root     13547:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     13548:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     13549:                pit[ch].prev_time = pit[ch].expired_time;
                   13550:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   13551:                if(cur_time >= pit[ch].expired_time) {
                   13552:                        pit[ch].prev_time = cur_time;
                   13553:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     13554:                }
1.1.1.8   root     13555:                return(1);
1.1       root     13556:        }
1.1.1.8   root     13557:        return(0);
1.1       root     13558: }
                   13559: 
                   13560: void pit_latch_count(int ch)
                   13561: {
1.1.1.8   root     13562:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     13563:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     13564:                pit_run(ch, cur_time);
                   13565:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     13566:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   13567:                
                   13568:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   13569:                        // decrement counter in 1msec period
                   13570:                        if(pit[ch].next_latch == 0) {
                   13571:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   13572:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   13573:                        }
                   13574:                        if(pit[ch].latch > pit[ch].next_latch) {
                   13575:                                pit[ch].latch--;
                   13576:                        }
                   13577:                } else {
                   13578:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   13579:                        pit[ch].next_latch = 0;
                   13580:                }
1.1.1.8   root     13581:        } else {
                   13582:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     13583:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     13584:        }
                   13585:        pit[ch].count_latched = 1;
                   13586:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   13587:                // lower byte
                   13588:                pit[ch].low_read = 1;
                   13589:                pit[ch].high_read = 0;
                   13590:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   13591:                // upper byte
                   13592:                pit[ch].low_read = 0;
                   13593:                pit[ch].high_read = 1;
                   13594:        } else {
                   13595:                // lower -> upper
1.1.1.14  root     13596:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     13597:        }
                   13598: }
                   13599: 
1.1.1.8   root     13600: int pit_get_expired_time(int ch)
1.1       root     13601: {
1.1.1.22  root     13602:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   13603:        UINT64 val = pit[ch].accum >> 10;
                   13604:        pit[ch].accum -= val << 10;
                   13605:        return((val != 0) ? val : 1);
1.1.1.8   root     13606: }
                   13607: 
1.1.1.25  root     13608: // sio
                   13609: 
                   13610: void sio_init()
                   13611: {
1.1.1.26  root     13612:        memset(sio, 0, sizeof(sio));
                   13613:        memset(sio_mt, 0, sizeof(sio_mt));
                   13614:        
1.1.1.29  root     13615:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     13616:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   13617:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   13618:                
                   13619:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   13620:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     13621:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   13622:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     13623:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   13624:                sio[c].irq_identify = 0x01;     // no pending irq
                   13625:                
                   13626:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   13627:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   13628:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   13629:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   13630:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   13631:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   13632:                
1.1.1.26  root     13633:                if(sio_port_number[c] != 0) {
1.1.1.25  root     13634:                        sio[c].channel = c;
                   13635:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   13636:                }
                   13637:        }
                   13638: }
                   13639: 
                   13640: void sio_finish()
                   13641: {
1.1.1.29  root     13642:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     13643:                if(sio_mt[c].hThread != NULL) {
                   13644:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   13645:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     13646:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     13647:                }
                   13648:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   13649:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   13650:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   13651:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   13652:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   13653:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     13654:        }
                   13655:        sio_release();
                   13656: }
                   13657: 
                   13658: void sio_release()
                   13659: {
1.1.1.29  root     13660:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     13661:                // sio_thread() may access the resources :-(
                   13662:                if(sio_mt[c].hThread == NULL) {
                   13663:                        if(sio[c].send_buffer != NULL) {
                   13664:                                sio[c].send_buffer->release();
                   13665:                                delete sio[c].send_buffer;
                   13666:                                sio[c].send_buffer = NULL;
                   13667:                        }
                   13668:                        if(sio[c].recv_buffer != NULL) {
                   13669:                                sio[c].recv_buffer->release();
                   13670:                                delete sio[c].recv_buffer;
                   13671:                                sio[c].recv_buffer = NULL;
                   13672:                        }
                   13673:                }
1.1.1.25  root     13674:        }
                   13675: }
                   13676: 
                   13677: void sio_write(int c, UINT32 addr, UINT8 data)
                   13678: {
                   13679:        switch(addr & 7) {
                   13680:        case 0:
                   13681:                if(sio[c].selector & 0x80) {
                   13682:                        if(sio[c].divisor.b.l != data) {
                   13683:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13684:                                sio[c].divisor.b.l = data;
                   13685:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13686:                        }
                   13687:                } else {
                   13688:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   13689:                        sio[c].send_buffer->write(data);
                   13690:                        // transmitter holding/shift registers are not empty
                   13691:                        sio[c].line_stat_buf &= ~0x60;
                   13692:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   13693:                        
                   13694:                        if(sio[c].irq_enable & 0x02) {
                   13695:                                sio_update_irq(c);
                   13696:                        }
                   13697:                }
                   13698:                break;
                   13699:        case 1:
                   13700:                if(sio[c].selector & 0x80) {
                   13701:                        if(sio[c].divisor.b.h != data) {
                   13702:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13703:                                sio[c].divisor.b.h = data;
                   13704:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13705:                        }
                   13706:                } else {
                   13707:                        if(sio[c].irq_enable != data) {
                   13708:                                sio[c].irq_enable = data;
                   13709:                                sio_update_irq(c);
                   13710:                        }
                   13711:                }
                   13712:                break;
                   13713:        case 3:
                   13714:                {
                   13715:                        UINT8 line_ctrl = data & 0x3f;
                   13716:                        bool set_brk = ((data & 0x40) != 0);
                   13717:                        
                   13718:                        if(sio[c].line_ctrl != line_ctrl) {
                   13719:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   13720:                                sio[c].line_ctrl = line_ctrl;
                   13721:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   13722:                        }
                   13723:                        if(sio[c].set_brk != set_brk) {
                   13724:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   13725:                                sio[c].set_brk = set_brk;
                   13726:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   13727:                        }
                   13728:                }
                   13729:                sio[c].selector = data;
                   13730:                break;
                   13731:        case 4:
                   13732:                {
                   13733:                        bool set_dtr = ((data & 0x01) != 0);
                   13734:                        bool set_rts = ((data & 0x02) != 0);
                   13735:                        
                   13736:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     13737: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     13738:                                sio[c].set_dtr = set_dtr;
                   13739:                                sio[c].set_rts = set_rts;
1.1.1.26  root     13740: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   13741:                                
                   13742:                                bool state_changed = false;
                   13743:                                
                   13744:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13745:                                if(set_dtr) {
                   13746:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   13747:                                } else {
                   13748:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   13749:                                }
                   13750:                                if(set_rts) {
                   13751:                                        sio[c].modem_stat |= 0x10;      // cts on
                   13752:                                } else {
                   13753:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   13754:                                }
                   13755:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   13756:                                        if(!(sio[c].modem_stat & 0x02)) {
                   13757:                                                if(sio[c].irq_enable & 0x08) {
                   13758:                                                        state_changed = true;
                   13759:                                                }
                   13760:                                                sio[c].modem_stat |= 0x02;
                   13761:                                        }
                   13762:                                }
                   13763:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   13764:                                        if(!(sio[c].modem_stat & 0x01)) {
                   13765:                                                if(sio[c].irq_enable & 0x08) {
                   13766:                                                        state_changed = true;
                   13767:                                                }
                   13768:                                                sio[c].modem_stat |= 0x01;
                   13769:                                        }
                   13770:                                }
                   13771:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   13772:                                
                   13773:                                if(state_changed) {
                   13774:                                        sio_update_irq(c);
                   13775:                                }
1.1.1.25  root     13776:                        }
                   13777:                }
                   13778:                sio[c].modem_ctrl = data;
                   13779:                break;
                   13780:        case 7:
                   13781:                sio[c].scratch = data;
                   13782:                break;
                   13783:        }
                   13784: }
                   13785: 
                   13786: UINT8 sio_read(int c, UINT32 addr)
                   13787: {
                   13788:        switch(addr & 7) {
                   13789:        case 0:
                   13790:                if(sio[c].selector & 0x80) {
                   13791:                        return(sio[c].divisor.b.l);
                   13792:                } else {
                   13793:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   13794:                        UINT8 data = sio[c].recv_buffer->read();
                   13795:                        // data is not ready
                   13796:                        sio[c].line_stat_buf &= ~0x01;
                   13797:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13798:                        
                   13799:                        if(sio[c].irq_enable & 0x01) {
                   13800:                                sio_update_irq(c);
                   13801:                        }
                   13802:                        return(data);
                   13803:                }
                   13804:        case 1:
                   13805:                if(sio[c].selector & 0x80) {
                   13806:                        return(sio[c].divisor.b.h);
                   13807:                } else {
                   13808:                        return(sio[c].irq_enable);
                   13809:                }
                   13810:        case 2:
                   13811:                return(sio[c].irq_identify);
                   13812:        case 3:
                   13813:                return(sio[c].selector);
                   13814:        case 4:
                   13815:                return(sio[c].modem_ctrl);
                   13816:        case 5:
                   13817:                {
                   13818:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   13819:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   13820:                        sio[c].line_stat_err = 0x00;
                   13821:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   13822:                        
                   13823:                        bool state_changed = false;
                   13824:                        
                   13825:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   13826:                                EnterCriticalSection(&sio_mt[c].csSendData);
                   13827:                                if(!sio[c].send_buffer->full()) {
                   13828:                                        // transmitter holding register will be empty first
                   13829:                                        if(sio[c].irq_enable & 0x02) {
                   13830:                                                state_changed = true;
                   13831:                                        }
                   13832:                                        sio[c].line_stat_buf |= 0x20;
                   13833:                                }
                   13834:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   13835:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   13836:                                // transmitter shift register will be empty later
                   13837:                                sio[c].line_stat_buf |= 0x40;
                   13838:                        }
                   13839:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   13840:                                EnterCriticalSection(&sio_mt[c].csRecvData);
                   13841:                                if(!sio[c].recv_buffer->empty()) {
                   13842:                                        // data is ready
                   13843:                                        if(sio[c].irq_enable & 0x01) {
                   13844:                                                state_changed = true;
                   13845:                                        }
                   13846:                                        sio[c].line_stat_buf |= 0x01;
                   13847:                                }
                   13848:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13849:                        }
                   13850:                        if(state_changed) {
                   13851:                                sio_update_irq(c);
                   13852:                        }
                   13853:                        return(val);
                   13854:                }
                   13855:        case 6:
                   13856:                {
                   13857:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   13858:                        UINT8 val = sio[c].modem_stat;
                   13859:                        sio[c].modem_stat &= 0xf0;
                   13860:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   13861:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   13862:                        
                   13863:                        if(sio[c].modem_ctrl & 0x10) {
                   13864:                                // loop-back
                   13865:                                val &= 0x0f;
                   13866:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   13867:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   13868:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   13869:                        }
                   13870:                        return(val);
                   13871:                }
                   13872:        case 7:
                   13873:                return(sio[c].scratch);
                   13874:        }
                   13875:        return(0xff);
                   13876: }
                   13877: 
                   13878: void sio_update(int c)
                   13879: {
                   13880:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   13881:                EnterCriticalSection(&sio_mt[c].csSendData);
                   13882:                if(!sio[c].send_buffer->full()) {
                   13883:                        // transmitter holding/shift registers will be empty
                   13884:                        sio[c].line_stat_buf |= 0x60;
                   13885:                }
                   13886:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   13887:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   13888:                // transmitter shift register will be empty
                   13889:                sio[c].line_stat_buf |= 0x40;
                   13890:        }
                   13891:        if(!(sio[c].line_stat_buf & 0x01)) {
                   13892:                EnterCriticalSection(&sio_mt[c].csRecvData);
                   13893:                if(!sio[c].recv_buffer->empty()) {
                   13894:                        // data is ready
                   13895:                        sio[c].line_stat_buf |= 0x01;
                   13896:                }
                   13897:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   13898:        }
                   13899:        sio_update_irq(c);
                   13900: }
                   13901: 
                   13902: void sio_update_irq(int c)
                   13903: {
                   13904:        int level = -1;
                   13905:        
                   13906:        if(sio[c].irq_enable & 0x08) {
                   13907:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13908:                if((sio[c].modem_stat & 0x0f) != 0) {
                   13909:                        level = 0;
                   13910:                }
                   13911:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   13912:        }
                   13913:        if(sio[c].irq_enable & 0x02) {
                   13914:                if(sio[c].line_stat_buf & 0x20) {
                   13915:                        level = 1;
                   13916:                }
                   13917:        }
                   13918:        if(sio[c].irq_enable & 0x01) {
                   13919:                if(sio[c].line_stat_buf & 0x01) {
                   13920:                        level = 2;
                   13921:                }
                   13922:        }
                   13923:        if(sio[c].irq_enable & 0x04) {
                   13924:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   13925:                if(sio[c].line_stat_err != 0) {
                   13926:                        level = 3;
                   13927:                }
                   13928:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   13929:        }
1.1.1.29  root     13930:        
                   13931:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     13932:        if(level != -1) {
                   13933:                sio[c].irq_identify = level << 1;
1.1.1.29  root     13934:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     13935:        } else {
                   13936:                sio[c].irq_identify = 1;
1.1.1.29  root     13937:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     13938:        }
                   13939: }
                   13940: 
                   13941: DWORD WINAPI sio_thread(void *lpx)
                   13942: {
                   13943:        volatile sio_t *p = (sio_t *)lpx;
                   13944:        sio_mt_t *q = &sio_mt[p->channel];
                   13945:        
                   13946:        char name[] = "COM1";
1.1.1.26  root     13947:        name[3] = '0' + sio_port_number[p->channel];
                   13948:        HANDLE hComm = NULL;
                   13949:        COMMPROP commProp;
                   13950:        DCB dcb;
                   13951:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   13952:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   13953:        
                   13954:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   13955:                if(GetCommProperties(hComm, &commProp)) {
                   13956:                        dwSettableBaud = commProp.dwSettableBaud;
                   13957:                }
1.1.1.25  root     13958:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     13959: //             EscapeCommFunction(hComm, SETRTS);
                   13960: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     13961:                
                   13962:                while(!m_halted) {
                   13963:                        // setup comm port
                   13964:                        bool comm_state_changed = false;
                   13965:                        
                   13966:                        EnterCriticalSection(&q->csLineCtrl);
                   13967:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   13968:                                p->prev_divisor = p->divisor.w;
                   13969:                                p->prev_line_ctrl = p->line_ctrl;
                   13970:                                comm_state_changed = true;
                   13971:                        }
                   13972:                        LeaveCriticalSection(&q->csLineCtrl);
                   13973:                        
                   13974:                        if(comm_state_changed) {
1.1.1.26  root     13975:                                if(GetCommState(hComm, &dcb)) {
                   13976: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   13977:                                        DWORD baud = 115200 / p->prev_divisor;
                   13978:                                        dcb.BaudRate = 9600; // default
                   13979:                                        
                   13980:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   13981:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   13982:                                        // 134.5bps is not supported ???
                   13983: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   13984:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   13985:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   13986:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   13987:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   13988:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   13989:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   13990:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   13991:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   13992:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   13993: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   13994: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   13995: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   13996:                                        
                   13997:                                        switch(p->prev_line_ctrl & 0x03) {
                   13998:                                        case 0x00: dcb.ByteSize = 5; break;
                   13999:                                        case 0x01: dcb.ByteSize = 6; break;
                   14000:                                        case 0x02: dcb.ByteSize = 7; break;
                   14001:                                        case 0x03: dcb.ByteSize = 8; break;
                   14002:                                        }
                   14003:                                        switch(p->prev_line_ctrl & 0x04) {
                   14004:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   14005:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   14006:                                        }
                   14007:                                        switch(p->prev_line_ctrl & 0x38) {
                   14008:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   14009:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   14010:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   14011:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   14012:                                        default:   dcb.Parity = NOPARITY;    break;
                   14013:                                        }
                   14014:                                        dcb.fBinary = TRUE;
                   14015:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   14016:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   14017:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   14018:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   14019:                                        dcb.fTXContinueOnXoff = TRUE;
                   14020:                                        dcb.fOutX = dcb.fInX = FALSE;
                   14021:                                        dcb.fErrorChar = FALSE;
                   14022:                                        dcb.fNull = FALSE;
                   14023:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   14024:                                        dcb.fAbortOnError = FALSE;
                   14025:                                        
                   14026:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     14027:                                }
                   14028:                                
                   14029:                                // check again to apply all comm state changes
                   14030:                                Sleep(10);
                   14031:                                continue;
                   14032:                        }
                   14033:                        
                   14034:                        // set comm pins
                   14035:                        bool change_brk = false;
1.1.1.26  root     14036: //                     bool change_rts = false;
                   14037: //                     bool change_dtr = false;
1.1.1.25  root     14038:                        
                   14039:                        EnterCriticalSection(&q->csModemCtrl);
                   14040:                        if(p->prev_set_brk != p->set_brk) {
                   14041:                                p->prev_set_brk = p->set_brk;
                   14042:                                change_brk = true;
                   14043:                        }
1.1.1.26  root     14044: //                     if(p->prev_set_rts != p->set_rts) {
                   14045: //                             p->prev_set_rts = p->set_rts;
                   14046: //                             change_rts = true;
                   14047: //                     }
                   14048: //                     if(p->prev_set_dtr != p->set_dtr) {
                   14049: //                             p->prev_set_dtr = p->set_dtr;
                   14050: //                             change_dtr = true;
                   14051: //                     }
1.1.1.25  root     14052:                        LeaveCriticalSection(&q->csModemCtrl);
                   14053:                        
                   14054:                        if(change_brk) {
1.1.1.26  root     14055:                                static UINT32 clear_time = 0;
                   14056:                                if(p->prev_set_brk) {
                   14057:                                        EscapeCommFunction(hComm, SETBREAK);
                   14058:                                        clear_time = timeGetTime() + 200;
                   14059:                                } else {
                   14060:                                        // keep break for at least 200msec
                   14061:                                        UINT32 cur_time = timeGetTime();
                   14062:                                        if(clear_time > cur_time) {
                   14063:                                                Sleep(clear_time - cur_time);
                   14064:                                        }
                   14065:                                        EscapeCommFunction(hComm, CLRBREAK);
                   14066:                                }
1.1.1.25  root     14067:                        }
1.1.1.26  root     14068: //                     if(change_rts) {
                   14069: //                             if(p->prev_set_rts) {
                   14070: //                                     EscapeCommFunction(hComm, SETRTS);
                   14071: //                             } else {
                   14072: //                                     EscapeCommFunction(hComm, CLRRTS);
                   14073: //                             }
                   14074: //                     }
                   14075: //                     if(change_dtr) {
                   14076: //                             if(p->prev_set_dtr) {
                   14077: //                                     EscapeCommFunction(hComm, SETDTR);
                   14078: //                             } else {
                   14079: //                                     EscapeCommFunction(hComm, CLRDTR);
                   14080: //                             }
                   14081: //                     }
1.1.1.25  root     14082:                        
                   14083:                        // get comm pins
                   14084:                        DWORD dwModemStat = 0;
                   14085:                        
                   14086:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   14087:                                EnterCriticalSection(&q->csModemStat);
                   14088:                                if(dwModemStat & MS_RLSD_ON) {
                   14089:                                        p->modem_stat |= 0x80;
                   14090:                                } else {
                   14091:                                        p->modem_stat &= ~0x80;
                   14092:                                }
                   14093:                                if(dwModemStat & MS_RING_ON) {
                   14094:                                        p->modem_stat |= 0x40;
                   14095:                                } else {
                   14096:                                        p->modem_stat &= ~0x40;
                   14097:                                }
1.1.1.26  root     14098: //                             if(dwModemStat & MS_DSR_ON) {
                   14099: //                                     p->modem_stat |= 0x20;
                   14100: //                             } else {
                   14101: //                                     p->modem_stat &= ~0x20;
                   14102: //                             }
                   14103: //                             if(dwModemStat & MS_CTS_ON) {
                   14104: //                                     p->modem_stat |= 0x10;
                   14105: //                             } else {
                   14106: //                                     p->modem_stat &= ~0x10;
                   14107: //                             }
1.1.1.25  root     14108:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   14109:                                        p->modem_stat |= 0x08;
                   14110:                                }
                   14111:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   14112:                                        p->modem_stat |= 0x04;
                   14113:                                }
1.1.1.26  root     14114: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   14115: //                                     p->modem_stat |= 0x02;
                   14116: //                             }
                   14117: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   14118: //                                     p->modem_stat |= 0x01;
                   14119: //                             }
1.1.1.25  root     14120:                                LeaveCriticalSection(&q->csModemStat);
                   14121:                        }
                   14122:                        
                   14123:                        // send data
                   14124:                        DWORD dwSend = 0;
                   14125:                        
                   14126:                        EnterCriticalSection(&q->csSendData);
                   14127:                        while(!p->send_buffer->empty()) {
                   14128:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   14129:                        }
                   14130:                        LeaveCriticalSection(&q->csSendData);
                   14131:                        
                   14132:                        if(dwSend != 0) {
                   14133:                                DWORD dwWritten = 0;
                   14134:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   14135:                        }
                   14136:                        
                   14137:                        // get line status and recv data
                   14138:                        DWORD dwLineStat = 0;
                   14139:                        COMSTAT comStat;
                   14140:                        
                   14141:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   14142:                                EnterCriticalSection(&q->csLineStat);
                   14143:                                if(dwLineStat & CE_BREAK) {
                   14144:                                        p->line_stat_err |= 0x10;
                   14145:                                }
                   14146:                                if(dwLineStat & CE_FRAME) {
                   14147:                                        p->line_stat_err |= 0x08;
                   14148:                                }
                   14149:                                if(dwLineStat & CE_RXPARITY) {
                   14150:                                        p->line_stat_err |= 0x04;
                   14151:                                }
                   14152:                                if(dwLineStat & CE_OVERRUN) {
                   14153:                                        p->line_stat_err |= 0x02;
                   14154:                                }
                   14155:                                LeaveCriticalSection(&q->csLineStat);
                   14156:                                
                   14157:                                if(comStat.cbInQue != 0) {
                   14158:                                        EnterCriticalSection(&q->csRecvData);
                   14159:                                        DWORD dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   14160:                                        LeaveCriticalSection(&q->csRecvData);
                   14161:                                        
                   14162:                                        if(dwRecv != 0) {
                   14163:                                                DWORD dwRead = 0;
                   14164:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   14165:                                                        EnterCriticalSection(&q->csRecvData);
                   14166:                                                        for(int i = 0; i < dwRead; i++) {
                   14167:                                                                p->recv_buffer->write(bytBuffer[i]);
                   14168:                                                        }
                   14169:                                                        LeaveCriticalSection(&q->csRecvData);
                   14170:                                                }
                   14171:                                        }
                   14172:                                }
                   14173:                        }
                   14174:                        Sleep(10);
                   14175:                }
                   14176:                CloseHandle(hComm);
                   14177:        }
                   14178:        return 0;
                   14179: }
                   14180: 
1.1.1.8   root     14181: // cmos
                   14182: 
                   14183: void cmos_init()
                   14184: {
                   14185:        memset(cmos, 0, sizeof(cmos));
                   14186:        cmos_addr = 0;
1.1       root     14187:        
1.1.1.8   root     14188:        // from DOSBox
                   14189:        cmos_write(0x0a, 0x26);
                   14190:        cmos_write(0x0b, 0x02);
                   14191:        cmos_write(0x0d, 0x80);
1.1       root     14192: }
                   14193: 
1.1.1.8   root     14194: void cmos_write(int addr, UINT8 val)
1.1       root     14195: {
1.1.1.8   root     14196:        cmos[addr & 0x7f] = val;
                   14197: }
                   14198: 
                   14199: #define CMOS_GET_TIME() { \
                   14200:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   14201:        if(prev_sec != cur_sec) { \
                   14202:                GetLocalTime(&time); \
                   14203:                prev_sec = cur_sec; \
                   14204:        } \
1.1       root     14205: }
1.1.1.8   root     14206: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     14207: 
1.1.1.8   root     14208: UINT8 cmos_read(int addr)
1.1       root     14209: {
1.1.1.8   root     14210:        static SYSTEMTIME time;
                   14211:        static UINT32 prev_sec = 0;
1.1       root     14212:        
1.1.1.8   root     14213:        switch(addr & 0x7f) {
                   14214:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   14215:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   14216:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   14217:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   14218:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   14219:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   14220:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   14221: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   14222:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   14223:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   14224:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   14225:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   14226:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   14227:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   14228:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   14229:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     14230:        }
1.1.1.8   root     14231:        return(cmos[addr & 0x7f]);
1.1       root     14232: }
                   14233: 
1.1.1.7   root     14234: // kbd (a20)
                   14235: 
                   14236: void kbd_init()
                   14237: {
1.1.1.8   root     14238:        kbd_data = kbd_command = 0;
1.1.1.7   root     14239:        kbd_status = 0x18;
                   14240: }
                   14241: 
                   14242: UINT8 kbd_read_data()
                   14243: {
1.1.1.8   root     14244:        kbd_status &= ~1;
1.1.1.7   root     14245:        return(kbd_data);
                   14246: }
                   14247: 
                   14248: void kbd_write_data(UINT8 val)
                   14249: {
                   14250:        switch(kbd_command) {
                   14251:        case 0xd1:
                   14252:                i386_set_a20_line((val >> 1) & 1);
                   14253:                break;
                   14254:        }
                   14255:        kbd_command = 0;
1.1.1.8   root     14256:        kbd_status &= ~8;
1.1.1.7   root     14257: }
                   14258: 
                   14259: UINT8 kbd_read_status()
                   14260: {
                   14261:        return(kbd_status);
                   14262: }
                   14263: 
                   14264: void kbd_write_command(UINT8 val)
                   14265: {
                   14266:        switch(val) {
                   14267:        case 0xd0:
                   14268:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     14269:                kbd_status |= 1;
1.1.1.7   root     14270:                break;
                   14271:        case 0xdd:
                   14272:                i386_set_a20_line(0);
                   14273:                break;
                   14274:        case 0xdf:
                   14275:                i386_set_a20_line(1);
                   14276:                break;
1.1.1.26  root     14277:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   14278:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     14279:                if(!(val & 1)) {
1.1.1.8   root     14280:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     14281:                                // reset pic
                   14282:                                pic_init();
                   14283:                                pic[0].irr = pic[1].irr = 0x00;
                   14284:                                pic[0].imr = pic[1].imr = 0xff;
                   14285:                        }
                   14286:                        CPU_RESET_CALL(CPU_MODEL);
                   14287:                        i386_jmp_far(0x40, 0x67);
                   14288:                }
                   14289:                i386_set_a20_line((val >> 1) & 1);
                   14290:                break;
                   14291:        }
                   14292:        kbd_command = val;
1.1.1.8   root     14293:        kbd_status |= 8;
1.1.1.7   root     14294: }
                   14295: 
1.1.1.9   root     14296: // vga
                   14297: 
                   14298: UINT8 vga_read_status()
                   14299: {
                   14300:        // 60hz
                   14301:        static const int period[3] = {16, 17, 17};
                   14302:        static int index = 0;
                   14303:        UINT32 time = timeGetTime() % period[index];
                   14304:        
                   14305:        index = (index + 1) % 3;
1.1.1.14  root     14306:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     14307: }
                   14308: 
1.1       root     14309: // i/o bus
                   14310: 
1.1.1.29  root     14311: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   14312: //#define SW1US_PATCH
                   14313: 
1.1.1.25  root     14314: #ifdef ENABLE_DEBUG_IOPORT
                   14315: UINT8 read_io_byte_debug(offs_t addr);
                   14316: 
                   14317: UINT8 read_io_byte(offs_t addr)
                   14318: {
                   14319:        UINT8 val = read_io_byte_debug(addr);
                   14320:        if(fdebug != NULL) {
                   14321:                fprintf(fdebug, "inb %04X, %02X\n", addr, val);
                   14322:        }
                   14323:        return(val);
                   14324: }
                   14325: 
                   14326: UINT8 read_io_byte_debug(offs_t addr)
                   14327: #else
1.1       root     14328: UINT8 read_io_byte(offs_t addr)
1.1.1.25  root     14329: #endif
1.1       root     14330: {
                   14331:        switch(addr) {
1.1.1.29  root     14332: #ifdef SW1US_PATCH
                   14333:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   14334:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   14335:                return(sio_read(0, addr - 1));
                   14336: #else
1.1.1.25  root     14337:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   14338:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   14339:                return(dma_read(0, addr));
1.1.1.29  root     14340: #endif
1.1.1.25  root     14341:        case 0x20: case 0x21:
1.1       root     14342:                return(pic_read(0, addr));
1.1.1.25  root     14343:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     14344:                return(pit_read(addr & 0x03));
1.1.1.7   root     14345:        case 0x60:
                   14346:                return(kbd_read_data());
1.1.1.9   root     14347:        case 0x61:
                   14348:                return(system_port);
1.1.1.7   root     14349:        case 0x64:
                   14350:                return(kbd_read_status());
1.1       root     14351:        case 0x71:
1.1.1.8   root     14352:                return(cmos_read(cmos_addr));
1.1.1.25  root     14353:        case 0x81:
                   14354:                return(dma_page_read(0, 2));
                   14355:        case 0x82:
                   14356:                return(dma_page_read(0, 3));
                   14357:        case 0x83:
                   14358:                return(dma_page_read(0, 1));
                   14359:        case 0x87:
                   14360:                return(dma_page_read(0, 0));
                   14361:        case 0x89:
                   14362:                return(dma_page_read(1, 2));
                   14363:        case 0x8a:
                   14364:                return(dma_page_read(1, 3));
                   14365:        case 0x8b:
                   14366:                return(dma_page_read(1, 1));
                   14367:        case 0x8f:
                   14368:                return(dma_page_read(1, 0));
1.1       root     14369:        case 0x92:
1.1.1.3   root     14370:                return((m_a20_mask >> 19) & 2);
1.1.1.25  root     14371:        case 0xa0: case 0xa1:
1.1       root     14372:                return(pic_read(1, addr));
1.1.1.25  root     14373:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   14374:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     14375:                return(dma_read(1, (addr - 0xc0) >> 1));
                   14376: //     case 0x278: case 0x279: case 0x27a:
                   14377: //             return(pio_read(1, addr));
1.1.1.29  root     14378:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   14379:                return(sio_read(3, addr));
1.1.1.25  root     14380:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   14381:                return(sio_read(1, addr));
                   14382:        case 0x378: case 0x379: case 0x37a:
                   14383:                return(pio_read(0, addr));
                   14384:        case 0x3ba: case 0x3da:
1.1.1.9   root     14385:                return(vga_read_status());
1.1.1.29  root     14386:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   14387:                return(sio_read(2, addr));
1.1.1.25  root     14388:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   14389:                return(sio_read(0, addr));
1.1       root     14390:        default:
                   14391: //             error("inb %4x\n", addr);
                   14392:                break;
                   14393:        }
                   14394:        return(0xff);
                   14395: }
                   14396: 
                   14397: UINT16 read_io_word(offs_t addr)
                   14398: {
                   14399:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   14400: }
                   14401: 
                   14402: UINT32 read_io_dword(offs_t addr)
                   14403: {
                   14404:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   14405: }
                   14406: 
                   14407: void write_io_byte(offs_t addr, UINT8 val)
                   14408: {
1.1.1.25  root     14409: #ifdef ENABLE_DEBUG_IOPORT
                   14410:        if(fdebug != NULL) {
                   14411:                fprintf(fdebug, "outb %04X, %02X\n", addr, val);
                   14412:        }
                   14413: #endif
1.1       root     14414:        switch(addr) {
1.1.1.29  root     14415: #ifdef SW1US_PATCH
                   14416:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   14417:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   14418:                sio_write(0, addr - 1, val);
                   14419:                break;
                   14420: #else
1.1.1.25  root     14421:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   14422:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   14423:                dma_write(0, addr, val);
                   14424:                break;
1.1.1.29  root     14425: #endif
1.1.1.25  root     14426:        case 0x20: case 0x21:
1.1       root     14427:                pic_write(0, addr, val);
                   14428:                break;
1.1.1.25  root     14429:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     14430:                pit_write(addr & 0x03, val);
                   14431:                break;
1.1.1.7   root     14432:        case 0x60:
                   14433:                kbd_write_data(val);
                   14434:                break;
1.1.1.9   root     14435:        case 0x61:
                   14436:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   14437:                        // beep on
                   14438: //                     MessageBeep(-1);
                   14439:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   14440:                        // beep off
                   14441:                }
                   14442:                system_port = val;
                   14443:                break;
1.1       root     14444:        case 0x64:
1.1.1.7   root     14445:                kbd_write_command(val);
1.1       root     14446:                break;
                   14447:        case 0x70:
                   14448:                cmos_addr = val;
                   14449:                break;
                   14450:        case 0x71:
1.1.1.8   root     14451:                cmos_write(cmos_addr, val);
1.1       root     14452:                break;
1.1.1.25  root     14453:        case 0x81:
                   14454:                dma_page_write(0, 2, val);
                   14455:        case 0x82:
                   14456:                dma_page_write(0, 3, val);
                   14457:        case 0x83:
                   14458:                dma_page_write(0, 1, val);
                   14459:        case 0x87:
                   14460:                dma_page_write(0, 0, val);
                   14461:        case 0x89:
                   14462:                dma_page_write(1, 2, val);
                   14463:        case 0x8a:
                   14464:                dma_page_write(1, 3, val);
                   14465:        case 0x8b:
                   14466:                dma_page_write(1, 1, val);
                   14467:        case 0x8f:
                   14468:                dma_page_write(1, 0, val);
1.1       root     14469:        case 0x92:
1.1.1.7   root     14470:                i386_set_a20_line((val >> 1) & 1);
1.1       root     14471:                break;
1.1.1.25  root     14472:        case 0xa0: case 0xa1:
1.1       root     14473:                pic_write(1, addr, val);
                   14474:                break;
1.1.1.25  root     14475:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   14476:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     14477:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     14478:                break;
1.1.1.26  root     14479: //     case 0x278: case 0x279: case 0x27a:
                   14480: //             pio_write(1, addr, val);
                   14481: //             break;
1.1.1.29  root     14482:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   14483:                sio_write(3, addr, val);
                   14484:                break;
1.1.1.25  root     14485:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   14486:                sio_write(1, addr, val);
                   14487:                break;
                   14488:        case 0x378: case 0x379: case 0x37a:
                   14489:                pio_write(0, addr, val);
                   14490:                break;
1.1.1.29  root     14491:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   14492:                sio_write(2, addr, val);
                   14493:                break;
1.1.1.25  root     14494:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   14495:                sio_write(0, addr, val);
                   14496:                break;
1.1       root     14497:        default:
                   14498: //             error("outb %4x,%2x\n", addr, val);
                   14499:                break;
                   14500:        }
                   14501: }
                   14502: 
                   14503: void write_io_word(offs_t addr, UINT16 val)
                   14504: {
                   14505:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   14506:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   14507: }
                   14508: 
                   14509: void write_io_dword(offs_t addr, UINT32 val)
                   14510: {
                   14511:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   14512:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   14513:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   14514:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   14515: }

unix.superglobalmegacorp.com

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