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

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.26  root      132: int sio_port_number[2] = {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...)
                    284: typedef UINT32 offs_t;
                    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       root      667: /* ----------------------------------------------------------------------------
                    668:        main
                    669: ---------------------------------------------------------------------------- */
                    670: 
1.1.1.28! root      671: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
        !           672: {
        !           673:        if(dwCtrlType == CTRL_BREAK_EVENT) {
        !           674:                // try to finish this program normally
        !           675:                m_halted = true;
        !           676:                return TRUE;
        !           677:        } else if(dwCtrlType == CTRL_C_EVENT) {
        !           678:                ctrl_c_pressed = true;
        !           679:                return TRUE;
        !           680:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
        !           681:                // this program will be terminated abnormally, do minimum end process
        !           682:                exit_handler();
        !           683:                exit(1);
        !           684:        }
        !           685:        return FALSE;
        !           686: }
        !           687: 
        !           688: void exit_handler()
        !           689: {
        !           690:        if(temp_file_created) {
        !           691:                DeleteFile(temp_file_path);
        !           692:                temp_file_created = false;
        !           693:        }
        !           694:        if(key_buf_char != NULL) {
        !           695:                key_buf_char->release();
        !           696:                delete key_buf_char;
        !           697:                key_buf_char = NULL;
        !           698:        }
        !           699:        if(key_buf_scan != NULL) {
        !           700:                key_buf_scan->release();
        !           701:                delete key_buf_scan;
        !           702:                key_buf_scan = NULL;
        !           703:        }
        !           704:        hardware_release();
        !           705: }
        !           706: 
        !           707: #ifdef USE_THREAD
        !           708: DWORD WINAPI vram_thread(LPVOID)
        !           709: {
        !           710:        while(!m_halted) {
        !           711:                EnterCriticalSection(&vram_crit_sect);
        !           712:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
        !           713:                        vram_flush_char();
        !           714:                }
        !           715:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
        !           716:                        vram_flush_attr();
        !           717:                }
        !           718:                vram_last_length_char = vram_length_char;
        !           719:                vram_last_length_attr = vram_length_attr;
        !           720:                LeaveCriticalSection(&vram_crit_sect);
        !           721:                // this is about half the maximum keyboard repeat rate - any
        !           722:                // lower tends to be jerky, any higher misses updates
        !           723:                Sleep(15);
        !           724:        }
        !           725:        return 0;
        !           726: }
        !           727: #endif
        !           728: 
        !           729: long get_section_in_exec_file(FILE *fp, char *name)
        !           730: {
        !           731:        UINT8 header[0x400];
        !           732:        
        !           733:        long position = ftell(fp);
        !           734:        fseek(fp, 0, SEEK_SET);
        !           735:        fread(header, sizeof(header), 1, fp);
        !           736:        fseek(fp, position, SEEK_SET);
        !           737:        
        !           738:        try {
        !           739:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
        !           740:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
        !           741:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
        !           742:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
        !           743:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
        !           744:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
        !           745:                
        !           746:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
        !           747:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
        !           748:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
        !           749:                                return(sectionHeader->PointerToRawData);
        !           750:                        }
        !           751:                }
        !           752:        } catch(...) {
        !           753:        }
        !           754:        return(0);
        !           755: }
        !           756: 
1.1.1.10  root      757: bool is_started_from_command_prompt()
                    758: {
1.1.1.18  root      759:        bool ret = false;
                    760:        
                    761:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                    762:        if(hLibrary) {
                    763:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                    764:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                    765:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                    766:                if(lpfnGetConsoleProcessList) {
                    767:                        DWORD pl;
                    768:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                    769:                        FreeLibrary(hLibrary);
                    770:                        return(ret);
                    771:                }
                    772:                FreeLibrary(hLibrary);
                    773:        }
                    774:        
                    775:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                    776:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                    777:                DWORD dwParentProcessID = 0;
                    778:                PROCESSENTRY32 pe32;
                    779:                pe32.dwSize = sizeof(PROCESSENTRY32);
                    780:                if(Process32First(hSnapshot, &pe32)) {
                    781:                        do {
                    782:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                    783:                                        dwParentProcessID = pe32.th32ParentProcessID;
                    784:                                        break;
                    785:                                }
                    786:                        } while(Process32Next(hSnapshot, &pe32));
                    787:                }
                    788:                CloseHandle(hSnapshot);
                    789:                if(dwParentProcessID != 0) {
                    790:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                    791:                        if(hProcess != NULL) {
                    792:                                HMODULE hMod;
                    793:                                DWORD cbNeeded;
                    794:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                    795:                                        char module_name[MAX_PATH];
                    796:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                    797:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                    798:                                        }
                    799:                                }
                    800:                                CloseHandle(hProcess);
                    801:                        }
                    802:                }
                    803:        }
                    804:        return(ret);
1.1.1.14  root      805: }
                    806: 
                    807: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                    808: {
1.1.1.24  root      809:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root      810:        OSVERSIONINFOEX osvi;
                    811:        DWORDLONG dwlConditionMask = 0;
                    812:        int op = VER_GREATER_EQUAL;
                    813:        
                    814:        // Initialize the OSVERSIONINFOEX structure.
                    815:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                    816:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                    817:        osvi.dwMajorVersion = dwMajorVersion;
                    818:        osvi.dwMinorVersion = dwMinorVersion;
                    819:        osvi.wServicePackMajor = wServicePackMajor;
                    820:        osvi.wServicePackMinor = wServicePackMinor;
                    821:        
                    822:         // Initialize the condition mask.
                    823:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                    824:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                    825:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                    826:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                    827:        
                    828:        // Perform the test.
                    829:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                    830: }
                    831: 
1.1.1.27  root      832: void get_sio_port_numbers()
                    833: {
                    834:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                    835:        HDEVINFO hDevInfo = 0;
                    836:        HKEY hKey = 0;
                    837:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                    838:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                    839:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                    840:                                char chData[256];
                    841:                                DWORD dwType = 0;
                    842:                                DWORD dwSize = sizeof(chData);
                    843:                                int port_number = 0;
                    844:                                
                    845:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                    846:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                    847:                                                port_number = atoi(chData + 3);
                    848:                                        }
                    849:                                }
                    850:                                RegCloseKey(hKey);
                    851:                                
                    852:                                if(sio_port_number[0] == port_number || sio_port_number[1] == port_number) {
                    853:                                        continue;
                    854:                                }
                    855:                                if(sio_port_number[0] == 0) {
                    856:                                        sio_port_number[0] = port_number;
                    857:                                } else if(sio_port_number[1] == 0) {
                    858:                                        sio_port_number[1] = port_number;
                    859:                                }
                    860:                                if(sio_port_number[0] != 0 && sio_port_number[1] != 0) {
                    861:                                        break;
                    862:                                }
                    863:                        }
                    864:                }
                    865:        }
                    866: }
                    867: 
1.1.1.28! root      868: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
        !           869: 
1.1       root      870: int main(int argc, char *argv[], char *envp[])
                    871: {
1.1.1.9   root      872:        int arg_offset = 0;
                    873:        int standard_env = 0;
1.1.1.14  root      874:        int buf_width = 0, buf_height = 0;
1.1.1.28! root      875:        bool get_console_info_success = false;
        !           876:        bool screen_size_changed = false;
        !           877:        
        !           878:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
        !           879:        GetModuleFileName(NULL, path, MAX_PATH);
        !           880:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root      881:        
1.1.1.27  root      882:        char dummy_argv_0[] = "msdos.exe";
                    883:        char dummy_argv_1[MAX_PATH];
                    884:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                    885:        char new_exec_file[MAX_PATH];
                    886:        bool convert_cmd_file = false;
1.1.1.28! root      887:        unsigned int code_page = 0;
1.1.1.27  root      888:        
                    889:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28! root      890:                // check if command file is embedded to this execution file
        !           891:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root      892:                FILE* fp = fopen(full, "rb");
1.1.1.28! root      893:                long offset = get_section_in_exec_file(fp, ".msdos");
        !           894:                if(offset != 0) {
        !           895:                        UINT8 buffer[14];
        !           896:                        fseek(fp, offset, SEEK_SET);
        !           897:                        fread(buffer, sizeof(buffer), 1, fp);
        !           898:                        
        !           899:                        // restore flags
        !           900:                        stay_busy           = ((buffer[0] & 0x01) != 0);
        !           901:                        no_windows          = ((buffer[0] & 0x02) != 0);
        !           902:                        standard_env        = ((buffer[0] & 0x04) != 0);
        !           903:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
        !           904:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
        !           905:                        if((buffer[0] & 0x20) != 0) {
        !           906:                                get_sio_port_numbers();
        !           907:                        }
        !           908:                        if((buffer[0] & 0x40) != 0) {
        !           909:                                UMB_TOP = EMS_TOP + EMS_SIZE;
        !           910:                                support_ems = true;
1.1.1.27  root      911: #ifdef SUPPORT_XMS
1.1.1.28! root      912:                                support_xms = true;
1.1.1.27  root      913: #endif
1.1.1.28! root      914:                        }
        !           915:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
        !           916:                                buf_width  = buffer[1] | (buffer[2] << 8);
        !           917:                                buf_height = buffer[3] | (buffer[4] << 8);
        !           918:                        }
        !           919:                        if(buffer[5] != 0) {
        !           920:                                major_version = buffer[5];
        !           921:                                minor_version = buffer[6];
        !           922:                        }
        !           923:                        if((code_page = buffer[7] | (buffer[8] << 8)) != 0) {
        !           924:                                SetConsoleCP(code_page);
        !           925:                                SetConsoleOutputCP(code_page);
        !           926:                        }
        !           927:                        int name_len = buffer[9];
        !           928:                        int file_len = buffer[10] | (buffer[11] << 8) | (buffer[12] << 16) | (buffer[13] << 24);
        !           929:                        
        !           930:                        // restore command file name
        !           931:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
        !           932:                        fread(dummy_argv_1, name_len, 1, fp);
        !           933:                        
        !           934:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
        !           935:                                // if original command file exists, create a temporary file name
        !           936:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
        !           937:                                        // create a temporary command file in the current director
        !           938:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root      939:                                } else {
1.1.1.28! root      940:                                        // create a temporary command file in the temporary folder
        !           941:                                        GetTempPath(MAX_PATH, path);
        !           942:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
        !           943:                                                DeleteFile(dummy_argv_1);
        !           944:                                        } else {
        !           945:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
        !           946:                                        }
1.1.1.27  root      947:                                }
1.1.1.28! root      948:                                // check the command file type
        !           949:                                fread(buffer, 2, 1, fp);
        !           950:                                fseek(fp, -2, SEEK_CUR);
        !           951:                                if(memcmp(buffer, "MZ", 2) != 0) {
        !           952:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
        !           953:                                } else {
        !           954:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root      955:                                }
                    956:                        }
1.1.1.28! root      957:                        
        !           958:                        // restore command file
        !           959:                        FILE* fo = fopen(dummy_argv_1, "wb");
        !           960:                        for(int i = 0; i < file_len; i++) {
        !           961:                                fputc(fgetc(fp), fo);
        !           962:                        }
        !           963:                        fclose(fo);
        !           964:                        
        !           965:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
        !           966:                        temp_file_created = true;
        !           967:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
        !           968:                        
        !           969:                        // adjust argc/argv
        !           970:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
        !           971:                                dummy_argv[i + 1] = argv[i];
        !           972:                        }
        !           973:                        argc++;
        !           974:                        argv = dummy_argv;
1.1.1.27  root      975:                }
                    976:                fclose(fp);
                    977:        }
1.1.1.9   root      978:        for(int i = 1; i < argc; i++) {
1.1.1.25  root      979:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                    980:                        stay_busy = true;
                    981:                        arg_offset++;
1.1.1.27  root      982:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                    983:                        if(argv[i][2] != '\0') {
                    984:                                strcpy(new_exec_file, &argv[i][2]);
                    985:                        } else {
                    986:                                strcpy(new_exec_file, "new_exec_file.exe");
                    987:                        }
                    988:                        convert_cmd_file = true;
                    989:                        arg_offset++;
1.1.1.28! root      990:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
        !           991:                        if(IS_NUMERIC(argv[i][2])) {
        !           992:                                code_page = atoi(&argv[i][2]);
        !           993:                        } else {
        !           994:                                code_page = GetConsoleCP();
        !           995:                        }
        !           996:                        arg_offset++;
1.1.1.25  root      997:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                    998:                        no_windows = true;
                    999:                        arg_offset++;
                   1000:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     1001:                        standard_env = 1;
                   1002:                        arg_offset++;
1.1.1.14  root     1003:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   1004:                        ignore_illegal_insn = true;
                   1005:                        arg_offset++;
                   1006:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   1007:                        limit_max_memory = true;
                   1008:                        arg_offset++;
                   1009:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     1010:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   1011:                                buf_width = buf_height = 0;
                   1012:                        }
                   1013:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   1014:                                buf_width = 80;
                   1015:                        }
                   1016:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   1017:                                buf_height = 25;
                   1018:                        }
1.1.1.14  root     1019:                        arg_offset++;
1.1.1.25  root     1020:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28! root     1021:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.25  root     1022:                                char *p1 = &argv[i][2], *p2;
1.1.1.28! root     1023:                                if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
1.1.1.26  root     1024:                                        sio_port_number[1] = atoi(p2 + 1);
1.1.1.25  root     1025:                                }
1.1.1.26  root     1026:                                sio_port_number[0] = atoi(p1);
1.1.1.25  root     1027:                        }
1.1.1.26  root     1028:                        if(sio_port_number[0] == 0 || sio_port_number[1] == 0) {
1.1.1.27  root     1029:                                get_sio_port_numbers();
1.1.1.25  root     1030:                        }
                   1031:                        arg_offset++;
1.1.1.9   root     1032:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     1033:                        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.9   root     1034:                                major_version = argv[i][2] - '0';
1.1.1.17  root     1035:                                minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     1036:                        }
                   1037:                        arg_offset++;
1.1.1.25  root     1038:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   1039:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   1040:                        support_ems = true;
                   1041: #ifdef SUPPORT_XMS
                   1042:                        support_xms = true;
                   1043: #endif
                   1044:                        arg_offset++;
1.1.1.9   root     1045:                } else {
                   1046:                        break;
                   1047:                }
                   1048:        }
                   1049:        if(argc < 2 + arg_offset) {
1.1       root     1050: #ifdef _WIN64
1.1.1.14  root     1051:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     1052: #else
1.1.1.14  root     1053:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     1054: #endif
1.1.1.25  root     1055:                fprintf(stderr,
1.1.1.28! root     1056:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
        !          1057:                        "             [-s[P1[,P2]]] [-vX.XX] [-x] (command file) [options]\n"
1.1.1.25  root     1058:                        "\n"
                   1059:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28! root     1060: #ifdef _WIN64
1.1.1.27  root     1061:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28! root     1062: #else
1.1.1.27  root     1063:                        "\t-c\tconvert command file to 32bit execution file\n"
                   1064: #endif
1.1.1.28! root     1065:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     1066:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   1067:                        "\t-e\tuse a reduced environment block\n"
                   1068:                        "\t-i\tignore invalid instructions\n"
                   1069:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   1070:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   1071:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   1072:                        "\t-v\tset the DOS version\n"
1.1.1.19  root     1073: #ifdef SUPPORT_XMS
1.1.1.28! root     1074:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     1075: #else
1.1.1.28! root     1076:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     1077: #endif
                   1078:                );
1.1.1.10  root     1079:                
                   1080:                if(!is_started_from_command_prompt()) {
                   1081:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   1082:                        while(!_kbhit()) {
                   1083:                                Sleep(10);
                   1084:                        }
                   1085:                }
1.1.1.20  root     1086: #ifdef _DEBUG
                   1087:                _CrtDumpMemoryLeaks();
                   1088: #endif
1.1       root     1089:                return(EXIT_FAILURE);
                   1090:        }
1.1.1.27  root     1091:        if(convert_cmd_file) {
                   1092:                retval = EXIT_FAILURE;
1.1.1.28! root     1093:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     1094:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28! root     1095:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     1096:                        
1.1.1.28! root     1097:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
        !          1098:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
        !          1099:                        } else if((fp = fopen(full, "rb")) == NULL) {
        !          1100:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     1101:                        } else {
1.1.1.28! root     1102:                                long offset = get_section_in_exec_file(fp, ".msdos");
        !          1103:                                if(offset != 0) {
        !          1104:                                        UINT8 buffer[14];
        !          1105:                                        fseek(fp, offset, SEEK_SET);
        !          1106:                                        fread(buffer, sizeof(buffer), 1, fp);
        !          1107:                                        memset(path, 0, sizeof(path));
        !          1108:                                        fread(path, buffer[9], 1, fp);
        !          1109:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
        !          1110:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
        !          1111:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
        !          1112:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
        !          1113:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
        !          1114:                                } else {
        !          1115:                                        // read pe header of msdos.exe
        !          1116:                                        UINT8 header[0x400];
        !          1117:                                        fseek(fp, 0, SEEK_SET);
        !          1118:                                        fread(header, sizeof(header), 1, fp);
        !          1119:                                        
        !          1120:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
        !          1121:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
        !          1122:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
        !          1123:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
        !          1124:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
        !          1125:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
        !          1126:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
        !          1127:                                        
        !          1128:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
        !          1129:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
        !          1130:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
        !          1131:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
        !          1132:                                        if(dwExtraLastSectionBytes != 0) {
        !          1133:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
        !          1134:                                                dwLastSectionSize += dwRemain;
        !          1135:                                        }
        !          1136:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
        !          1137:                                        
        !          1138:                                        // add new section
        !          1139:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
        !          1140:                                        coffHeader->NumberOfSections++;
        !          1141:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
        !          1142:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
        !          1143:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
        !          1144:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
        !          1145:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
        !          1146:                                        
        !          1147:                                        // store msdos.exe
        !          1148:                                        fseek(fp, 0, SEEK_SET);
        !          1149:                                        for(int i = 0; i < dwEndOfFile; i++) {
        !          1150:                                                if((data = fgetc(fp)) != EOF) {
        !          1151:                                                        fputc(data, fo);
        !          1152:                                                } else {
        !          1153:                                                        // we should not reach here :-(
        !          1154:                                                        fputc(0, fo);
        !          1155:                                                }
        !          1156:                                        }
        !          1157:                                        
        !          1158:                                        // store options
        !          1159:                                        UINT8 flags = 0;
        !          1160:                                        if(stay_busy) {
        !          1161:                                                flags |= 0x01;
        !          1162:                                        }
        !          1163:                                        if(no_windows) {
        !          1164:                                                flags |= 0x02;
        !          1165:                                        }
        !          1166:                                        if(standard_env) {
        !          1167:                                                flags |= 0x04;
        !          1168:                                        }
        !          1169:                                        if(ignore_illegal_insn) {
        !          1170:                                                flags |= 0x08;
        !          1171:                                        }
        !          1172:                                        if(limit_max_memory) {
        !          1173:                                                flags |= 0x10;
        !          1174:                                        }
        !          1175:                                        if(sio_port_number[0] != 0 || sio_port_number[1] != 0) {
        !          1176:                                                flags |= 0x20;
        !          1177:                                        }
        !          1178:                                        if(support_ems) {
        !          1179:                                                flags |= 0x40;
        !          1180:                                        }
        !          1181:                                        
        !          1182:                                        fputc(flags, fo);
        !          1183:                                        fputc((buf_width  >> 0) & 0xff, fo);
        !          1184:                                        fputc((buf_width  >> 8) & 0xff, fo);
        !          1185:                                        fputc((buf_height >> 0) & 0xff, fo);
        !          1186:                                        fputc((buf_height >> 8) & 0xff, fo);
        !          1187:                                        fputc(major_version, fo);
        !          1188:                                        fputc(minor_version, fo);
        !          1189:                                        fputc((code_page >> 0) & 0xff, fo);
        !          1190:                                        fputc((code_page >> 8) & 0xff, fo);
        !          1191:                                        
        !          1192:                                        // store command file info
        !          1193:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
        !          1194:                                        int name_len = strlen(name);
        !          1195:                                        fseek(fs, 0, SEEK_END);
        !          1196:                                        long file_size = ftell(fs);
        !          1197:                                        
        !          1198:                                        fputc(name_len, fo);
        !          1199:                                        fputc((file_size >>  0) & 0xff, fo);
        !          1200:                                        fputc((file_size >>  8) & 0xff, fo);
        !          1201:                                        fputc((file_size >> 16) & 0xff, fo);
        !          1202:                                        fputc((file_size >> 24) & 0xff, fo);
        !          1203:                                        fwrite(name, name_len, 1, fo);
        !          1204:                                        
        !          1205:                                        // store command file
        !          1206:                                        fseek(fs, 0, SEEK_SET);
        !          1207:                                        for(int i = 0; i < file_size; i++) {
        !          1208:                                                if((data = fgetc(fs)) != EOF) {
        !          1209:                                                        fputc(data, fo);
        !          1210:                                                } else {
        !          1211:                                                        // we should not reach here :-(
        !          1212:                                                        fputc(0, fo);
        !          1213:                                                }
        !          1214:                                        }
        !          1215:                                        
        !          1216:                                        // store padding data and update pe header
        !          1217:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
        !          1218:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
        !          1219:                                        if(dwExtraRawBytes != 0) {
        !          1220:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
        !          1221:                                                for(int i = 0; i < dwRemain; i++) {
        !          1222:                                                        fputc(0, fo);
        !          1223:                                                }
        !          1224:                                                newSectionHeader->SizeOfRawData += dwRemain;
        !          1225:                                        }
        !          1226:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
        !          1227:                                        
        !          1228:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
        !          1229:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
        !          1230:                                        if(dwExtraNewSectionBytes != 0) {
        !          1231:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
        !          1232:                                                dwNewSectionSize += dwRemain;
        !          1233:                                        }
        !          1234:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
        !          1235:                                        
        !          1236:                                        fseek(fo, 0, SEEK_SET);
        !          1237:                                        fwrite(header, sizeof(header), 1, fo);
        !          1238:                                        
        !          1239:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
        !          1240:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     1241:                                }
                   1242:                        }
                   1243:                        if(fp != NULL) {
                   1244:                                fclose(fp);
                   1245:                        }
                   1246:                        if(fs != NULL) {
                   1247:                                fclose(fs);
                   1248:                        }
                   1249:                        if(fo != NULL) {
                   1250:                                fclose(fo);
                   1251:                        }
                   1252:                }
                   1253: #ifdef _DEBUG
                   1254:                _CrtDumpMemoryLeaks();
                   1255: #endif
                   1256:                return(retval);
                   1257:        }
1.1       root     1258:        
1.1.1.14  root     1259:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   1260:        
1.1.1.23  root     1261:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     1262:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     1263:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     1264:        
1.1.1.28! root     1265:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     1266:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     1267:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     1268:        
1.1.1.14  root     1269:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   1270:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   1271:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   1272:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     1273:                }
                   1274:        }
1.1.1.28! root     1275:        if(get_console_info_success) {
1.1.1.12  root     1276:                scr_width = csbi.dwSize.X;
1.1.1.14  root     1277:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   1278:                
1.1.1.28! root     1279:                // v-text shadow buffer size must be lesser than 0x7fd0
        !          1280:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     1281:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   1282:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   1283:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28! root     1284:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     1285:                                scr_width = 80;
                   1286:                                scr_height = 25;
                   1287:                        }
1.1.1.28! root     1288:                        screen_size_changed = true;
1.1.1.14  root     1289:                }
1.1.1.12  root     1290:        } else {
                   1291:                // for a proof (not a console)
                   1292:                scr_width = 80;
                   1293:                scr_height = 25;
                   1294:        }
1.1.1.14  root     1295:        scr_buf_size.X = scr_width;
                   1296:        scr_buf_size.Y = scr_height;
                   1297:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   1298:        scr_top = csbi.srWindow.Top;
1.1       root     1299:        cursor_moved = false;
                   1300:        
1.1.1.25  root     1301:        key_buf_char = new FIFO(256);
                   1302:        key_buf_scan = new FIFO(256);
1.1       root     1303:        
                   1304:        hardware_init();
                   1305:        
1.1.1.9   root     1306:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     1307:                retval = EXIT_FAILURE;
                   1308:        } else {
1.1.1.27  root     1309: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     1310:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   1311: #endif
                   1312:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   1313:                
1.1.1.28! root     1314:                if(screen_size_changed) {
1.1.1.24  root     1315:                        change_console_size(scr_width, scr_height);
                   1316:                }
1.1.1.8   root     1317:                TIMECAPS caps;
                   1318:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   1319:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.14  root     1320: #ifdef USE_THREAD
                   1321:                InitializeCriticalSection(&vram_crit_sect);
                   1322:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   1323: #endif
1.1       root     1324:                hardware_run();
1.1.1.14  root     1325: #ifdef USE_THREAD
                   1326:                vram_flush();
                   1327:                DeleteCriticalSection(&vram_crit_sect);
                   1328: #endif
1.1.1.24  root     1329:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     1330:                
1.1.1.24  root     1331:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28! root     1332:                if(get_console_info_success) {
1.1.1.23  root     1333:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     1334:                        if(restore_console_on_exit) {
1.1.1.14  root     1335:                                // window can't be bigger than buffer,
                   1336:                                // buffer can't be smaller than window,
                   1337:                                // so make a tiny window,
                   1338:                                // set the required buffer,
                   1339:                                // then set the required window
                   1340:                                SMALL_RECT rect;
                   1341:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   1342:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     1343:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     1344:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     1345:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   1346:                        }
1.1.1.14  root     1347:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   1348:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     1349:                }
1.1.1.24  root     1350:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   1351:                
1.1       root     1352:                msdos_finish();
1.1.1.14  root     1353:                
                   1354:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     1355:        }
                   1356:        
1.1.1.10  root     1357:        hardware_finish();
                   1358:        
1.1.1.28! root     1359:        if(key_buf_char != NULL) {
        !          1360:                key_buf_char->release();
        !          1361:                delete key_buf_char;
        !          1362:                key_buf_char = NULL;
        !          1363:        }
        !          1364:        if(key_buf_scan != NULL) {
        !          1365:                key_buf_scan->release();
        !          1366:                delete key_buf_scan;
        !          1367:                key_buf_scan = NULL;
        !          1368:        }
        !          1369:        if(temp_file_created) {
        !          1370:                DeleteFile(temp_file_path);
        !          1371:                temp_file_created = false;
        !          1372:        }
        !          1373: //     if(argv == dummy_argv) {
        !          1374: //             if(!is_started_from_command_prompt()) {
        !          1375: //                     fprintf(stderr, "\nHit any key to quit...");
        !          1376: //                     while(!_kbhit()) {
        !          1377: //                             Sleep(10);
        !          1378: //                     }
        !          1379: //             }
        !          1380: //     }
1.1.1.20  root     1381: #ifdef _DEBUG
                   1382:        _CrtDumpMemoryLeaks();
                   1383: #endif
1.1       root     1384:        return(retval);
                   1385: }
                   1386: 
1.1.1.20  root     1387: /* ----------------------------------------------------------------------------
                   1388:        console
                   1389: ---------------------------------------------------------------------------- */
                   1390: 
1.1.1.14  root     1391: void change_console_size(int width, int height)
1.1.1.12  root     1392: {
1.1.1.23  root     1393:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     1394:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   1395:        SMALL_RECT rect;
                   1396:        COORD co;
                   1397:        
                   1398:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     1399:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   1400:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   1401:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   1402:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1403:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1404:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   1405:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   1406:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1407:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1408:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     1409:                }
                   1410:        }
1.1.1.14  root     1411:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     1412:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     1413:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     1414:                SetConsoleCursorPosition(hStdout, co);
                   1415:                cursor_moved = true;
                   1416:        }
1.1.1.14  root     1417:        
                   1418:        // window can't be bigger than buffer,
                   1419:        // buffer can't be smaller than window,
                   1420:        // so make a tiny window,
                   1421:        // set the required buffer,
                   1422:        // then set the required window
                   1423:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     1424:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     1425:        co.X = width;
                   1426:        co.Y = height;
1.1.1.12  root     1427:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     1428:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1429:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   1430:        
                   1431:        scr_width = scr_buf_size.X = width;
                   1432:        scr_height = scr_buf_size.Y = height;
                   1433:        scr_top = 0;
                   1434:        
                   1435:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   1436:        
                   1437:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     1438:        text_vram_end_address = text_vram_top_address + regen;
                   1439:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   1440:        
1.1.1.14  root     1441:        if(regen > 0x4000) {
                   1442:                regen = 0x8000;
                   1443:                vram_pages = 1;
                   1444:        } else if(regen > 0x2000) {
                   1445:                regen = 0x4000;
                   1446:                vram_pages = 2;
                   1447:        } else if(regen > 0x1000) {
                   1448:                regen = 0x2000;
                   1449:                vram_pages = 4;
                   1450:        } else {
                   1451:                regen = 0x1000;
                   1452:                vram_pages = 8;
                   1453:        }
1.1.1.15  root     1454:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   1455:        *(UINT16 *)(mem + 0x44c) = regen;
                   1456:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   1457:        
1.1.1.24  root     1458:        mouse.min_position.x = 0;
                   1459:        mouse.min_position.y = 0;
                   1460:        mouse.max_position.x = 8 * scr_width  - 1;
                   1461:        mouse.max_position.y = 8 * scr_height - 1;
                   1462:        
1.1.1.15  root     1463:        restore_console_on_exit = true;
1.1.1.14  root     1464: }
                   1465: 
                   1466: void clear_scr_buffer(WORD attr)
                   1467: {
                   1468:        for(int y = 0; y < scr_height; y++) {
                   1469:                for(int x = 0; x < scr_width; x++) {
                   1470:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   1471:                        SCR_BUF(y,x).Attributes = attr;
                   1472:                }
                   1473:        }
1.1.1.12  root     1474: }
                   1475: 
1.1.1.24  root     1476: bool update_console_input()
1.1       root     1477: {
1.1.1.23  root     1478:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     1479:        DWORD dwNumberOfEvents = 0;
1.1       root     1480:        DWORD dwRead;
                   1481:        INPUT_RECORD ir[16];
1.1.1.24  root     1482:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   1483:        bool result = false;
1.1       root     1484:        
1.1.1.8   root     1485:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   1486:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   1487:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     1488:                                if(ir[i].EventType & MOUSE_EVENT) {
                   1489:                                        if(mouse.active) {
                   1490:                                                if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   1491:                                                        for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   1492:                                                                static const DWORD bits[] = {
                   1493:                                                                        FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   1494:                                                                        RIGHTMOST_BUTTON_PRESSED,       // right
                   1495:                                                                        FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
1.1.1.14  root     1496:                                                                };
1.1.1.24  root     1497:                                                                bool prev_status = mouse.buttons[i].status;
                   1498:                                                                mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   1499:                                                                
                   1500:                                                                if(!prev_status && mouse.buttons[i].status) {
                   1501:                                                                        mouse.buttons[i].pressed_times++;
                   1502:                                                                        mouse.buttons[i].pressed_position.x = mouse.position.x;
                   1503:                                                                        mouse.buttons[i].pressed_position.y = mouse.position.y;
                   1504:                                                                        mouse.status |= 2 << (i * 2);
                   1505:                                                                } else if(prev_status && !mouse.buttons[i].status) {
                   1506:                                                                        mouse.buttons[i].released_times++;
                   1507:                                                                        mouse.buttons[i].released_position.x = mouse.position.x;
                   1508:                                                                        mouse.buttons[i].released_position.y = mouse.position.y;
                   1509:                                                                        mouse.status |= 4 << (i * 2);
                   1510:                                                                }
1.1.1.14  root     1511:                                                        }
1.1.1.24  root     1512:                                                } else if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   1513:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   1514:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   1515:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.14  root     1516:                                                        }
1.1.1.28! root     1517:                                                        // FIXME: character size is always 8x8 ???
        !          1518:                                                        int x = 3 + 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
        !          1519:                                                        int y = 4 + 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
1.1.1.24  root     1520:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   1521:                                                                mouse.position.x = x;
                   1522:                                                                mouse.position.y = y;
                   1523:                                                                mouse.status |= 1;
1.1.1.14  root     1524:                                                        }
                   1525:                                                }
                   1526:                                        }
1.1.1.24  root     1527:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.28! root     1528:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     1529:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.14  root     1530:                                        if(!ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28! root     1531:                                                // break
1.1.1.14  root     1532:                                                kbd_data |= 0x80;
1.1.1.24  root     1533:                                        } else {
1.1.1.28! root     1534:                                                // make
1.1.1.24  root     1535:                                                kbd_data &= 0x7f;
                   1536:                                                
                   1537:                                                // update dos key buffer
                   1538:                                                UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   1539:                                                UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   1540:                                                
                   1541:                                                if(chr == 0) {
                   1542:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1543:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1544:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   1545:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1546:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   1547:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   1548:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   1549:                                                                } else if(scn == 0x35) {
                   1550:                                                                        scn = 0xa4;             // keypad /
                   1551:                                                                }
                   1552:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   1553:                                                                if(scn == 0x07) {
                   1554:                                                                        chr = 0x1e;     // Ctrl+^
                   1555:                                                                } else if(scn == 0x0c) {
                   1556:                                                                        chr = 0x1f;     // Ctrl+_
                   1557:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   1558:                                                                        static const UINT8 ctrl_map[] = {
                   1559:                                                                                0x95,   // keypad /
                   1560:                                                                                0,
                   1561:                                                                                0x96,   // keypad *
                   1562:                                                                                0, 0, 0,
                   1563:                                                                                0x5e,   // F1
                   1564:                                                                                0x5f,   // F2
                   1565:                                                                                0x60,   // F3
                   1566:                                                                                0x61,   // F4
                   1567:                                                                                0x62,   // F5
                   1568:                                                                                0x63,   // F6
                   1569:                                                                                0x64,   // F7
                   1570:                                                                                0x65,   // F8
                   1571:                                                                                0x66,   // F9
                   1572:                                                                                0x67,   // F10
                   1573:                                                                                0,
                   1574:                                                                                0,
                   1575:                                                                                0x77,   // Home
                   1576:                                                                                0x8d,   // Up
                   1577:                                                                                0x84,   // PgUp
                   1578:                                                                                0x8e,   // keypad -
                   1579:                                                                                0x73,   // Left
                   1580:                                                                                0x8f,   // keypad center
                   1581:                                                                                0x74,   // Right
                   1582:                                                                                0x90,   // keyapd +
                   1583:                                                                                0x75,   // End
                   1584:                                                                                0x91,   // Down
                   1585:                                                                                0x76,   // PgDn
                   1586:                                                                                0x92,   // Insert
                   1587:                                                                                0x93,   // Delete
                   1588:                                                                                0, 0, 0,
                   1589:                                                                                0x89,   // F11
                   1590:                                                                                0x8a,   // F12
                   1591:                                                                        };
                   1592:                                                                        scn = ctrl_map[scn - 0x35];
                   1593:                                                                }
                   1594:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   1595:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1596:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   1597:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1598:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   1599:                                                                }
                   1600:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   1601:                                                                scn += 0x85 - 0x57;
                   1602:                                                        }
                   1603:                                                        // ignore shift, ctrl, alt, win and menu keys
                   1604:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
                   1605:                                                                if(chr == 0) {
                   1606:                                                                        key_buf_char->write(0x00);
                   1607:                                                                        key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   1608:                                                                }
                   1609:                                                                key_buf_char->write(chr);
                   1610:                                                                key_buf_scan->write(scn);
                   1611:                                                        }
                   1612:                                                } else {
                   1613:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1614:                                                                chr = 0;
                   1615:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   1616:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   1617:                                                                }
                   1618:                                                        }
                   1619:                                                        key_buf_char->write(chr);
                   1620:                                                        key_buf_scan->write(scn);
                   1621:                                                }
1.1       root     1622:                                        }
1.1.1.24  root     1623:                                        result = key_changed = true;
1.1       root     1624:                                }
                   1625:                        }
                   1626:                }
                   1627:        }
1.1.1.24  root     1628:        return(result);
1.1.1.8   root     1629: }
                   1630: 
1.1.1.14  root     1631: bool update_key_buffer()
1.1.1.8   root     1632: {
1.1.1.24  root     1633:        return(update_console_input() || key_buf_char->count() != 0);
1.1.1.8   root     1634: }
                   1635: 
1.1.1.20  root     1636: /* ----------------------------------------------------------------------------
                   1637:        MS-DOS virtual machine
                   1638: ---------------------------------------------------------------------------- */
                   1639: 
                   1640: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   1641: int msdos_psp_get_file_table(int fd, int psp_seg);
                   1642: void msdos_putch(UINT8 data);
                   1643: 
1.1       root     1644: // process info
                   1645: 
                   1646: process_t *msdos_process_info_create(UINT16 psp_seg)
                   1647: {
                   1648:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1649:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   1650:                        memset(&process[i], 0, sizeof(process_t));
                   1651:                        process[i].psp = psp_seg;
                   1652:                        return(&process[i]);
                   1653:                }
                   1654:        }
                   1655:        fatalerror("too many processes\n");
                   1656:        return(NULL);
                   1657: }
                   1658: 
                   1659: process_t *msdos_process_info_get(UINT16 psp_seg)
                   1660: {
                   1661:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1662:                if(process[i].psp == psp_seg) {
                   1663:                        return(&process[i]);
                   1664:                }
                   1665:        }
                   1666:        fatalerror("invalid psp address\n");
                   1667:        return(NULL);
                   1668: }
                   1669: 
1.1.1.23  root     1670: void msdos_sda_update(int psp_seg)
                   1671: {
                   1672:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   1673:        
                   1674:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1675:                if(process[i].psp == psp_seg) {
                   1676:                        sda->switchar = process[i].switchar;
                   1677:                        sda->current_dta.w.l = process[i].dta.w.l;
                   1678:                        sda->current_dta.w.h = process[i].dta.w.h;
                   1679:                        sda->current_psp = process[i].psp;
                   1680:                        break;
                   1681:                }
                   1682:        }
                   1683:        sda->malloc_strategy = malloc_strategy;
                   1684:        sda->return_code = retval;
                   1685:        sda->current_drive = _getdrive();
                   1686: }
                   1687: 
1.1.1.13  root     1688: // dta info
                   1689: 
                   1690: void msdos_dta_info_init()
                   1691: {
1.1.1.14  root     1692:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     1693:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1694:        }
                   1695: }
                   1696: 
                   1697: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   1698: {
                   1699:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     1700:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1701:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   1702:                        if(free_dta == NULL) {
1.1.1.13  root     1703:                                free_dta = &dtalist[i];
                   1704:                        }
1.1.1.14  root     1705:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     1706:                        return(&dtalist[i]);
                   1707:                }
                   1708:        }
1.1.1.14  root     1709:        if(free_dta) {
1.1.1.13  root     1710:                free_dta->psp = psp_seg;
                   1711:                free_dta->dta = dta_laddr;
                   1712:                return(free_dta);
                   1713:        }
                   1714:        fatalerror("too many dta\n");
                   1715:        return(NULL);
                   1716: }
                   1717: 
                   1718: void msdos_dta_info_free(UINT16 psp_seg)
                   1719: {
1.1.1.14  root     1720:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1721:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     1722:                        FindClose(dtalist[i].find_handle);
                   1723:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1724:                }
                   1725:        }
                   1726: }
                   1727: 
1.1       root     1728: void msdos_cds_update(int drv)
                   1729: {
                   1730:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                   1731:        
                   1732:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                   1733:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   1734:        cds->drive_attrib = 0x4000;     // physical drive
                   1735:        cds->physical_drive_number = drv;
                   1736: }
                   1737: 
1.1.1.17  root     1738: // nls information tables
                   1739: 
                   1740: // uppercase table (func 6502h)
                   1741: void msdos_upper_table_update()
                   1742: {
                   1743:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1744:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1745:                UINT8 c[4];
                   1746:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1747:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1748:                c[0] = 0x80 + i;
                   1749:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   1750:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1751:        }
                   1752: }
                   1753: 
1.1.1.23  root     1754: // lowercase table (func 6503h)
                   1755: void msdos_lower_table_update()
                   1756: {
                   1757:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   1758:        for(unsigned i = 0; i < 0x80; ++i) {
                   1759:                UINT8 c[4];
                   1760:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1761:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1762:                c[0] = 0x80 + i;
                   1763:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   1764:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1765:        }
                   1766: }
                   1767: 
1.1.1.17  root     1768: // filename uppercase table (func 6504h)
                   1769: void msdos_filename_upper_table_init()
                   1770: {
                   1771:        // depended on (file)system, not on active codepage
                   1772:        // temporary solution: just filling data
                   1773:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1774:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1775:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   1776:        }
                   1777: }
                   1778: 
                   1779: // filaname terminator table (func 6505h)
                   1780: void msdos_filename_terminator_table_init()
                   1781: {
                   1782:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   1783:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   1784:        
                   1785:        data[2] = 1;            // marker? (permissible character value)
                   1786:        data[3] = 0x00;         // 00h...FFh
                   1787:        data[4] = 0xff;
                   1788:        data[5] = 0;            // marker? (excluded character)
                   1789:        data[6] = 0x00;         // 00h...20h
                   1790:        data[7] = 0x20;
                   1791:        data[8] = 2;            // marker? (illegal characters for filename)
                   1792:        data[9] = (UINT8)strlen(illegal_chars);
                   1793:        memcpy(data + 10, illegal_chars, data[9]);
                   1794:        
                   1795:        // total length
                   1796:        *(UINT16 *)data = (10 - 2) + data[9];
                   1797: }
                   1798: 
                   1799: // collating table (func 6506h)
                   1800: void msdos_collating_table_update()
                   1801: {
                   1802:        // temporary solution: just filling data
                   1803:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     1804:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     1805:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   1806:        }
                   1807: }
                   1808: 
1.1       root     1809: // dbcs
                   1810: 
                   1811: void msdos_dbcs_table_update()
                   1812: {
                   1813:        UINT8 dbcs_data[DBCS_SIZE];
                   1814:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   1815:        
                   1816:        CPINFO info;
                   1817:        GetCPInfo(active_code_page, &info);
                   1818:        
                   1819:        if(info.MaxCharSize != 1) {
                   1820:                for(int i = 0;; i += 2) {
                   1821:                        UINT8 lo = info.LeadByte[i + 0];
                   1822:                        UINT8 hi = info.LeadByte[i + 1];
                   1823:                        dbcs_data[2 + i + 0] = lo;
                   1824:                        dbcs_data[2 + i + 1] = hi;
                   1825:                        if(lo == 0 && hi == 0) {
                   1826:                                dbcs_data[0] = i + 2;
                   1827:                                break;
                   1828:                        }
                   1829:                }
                   1830:        } else {
                   1831:                dbcs_data[0] = 2;       // ???
                   1832:        }
                   1833:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   1834: }
                   1835: 
1.1.1.17  root     1836: void msdos_dbcs_table_finish()
                   1837: {
                   1838:        if(active_code_page != system_code_page) {
                   1839:                _setmbcp(system_code_page);
                   1840:        }
                   1841: }
                   1842: 
                   1843: void msdos_nls_tables_init()
1.1       root     1844: {
                   1845:        system_code_page = active_code_page = _getmbcp();
1.1.1.17  root     1846:        msdos_upper_table_update();
1.1.1.23  root     1847:        msdos_lower_table_update();
1.1.1.17  root     1848:        msdos_filename_terminator_table_init();
                   1849:        msdos_filename_upper_table_init();
                   1850:        msdos_collating_table_update();
1.1       root     1851:        msdos_dbcs_table_update();
                   1852: }
                   1853: 
1.1.1.17  root     1854: void msdos_nls_tables_update()
1.1       root     1855: {
1.1.1.17  root     1856:        msdos_dbcs_table_update();
                   1857:        msdos_upper_table_update();
1.1.1.23  root     1858:        msdos_lower_table_update();
                   1859: //     msdos_collating_table_update();
1.1       root     1860: }
                   1861: 
                   1862: int msdos_lead_byte_check(UINT8 code)
                   1863: {
                   1864:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   1865:        
                   1866:        for(int i = 0;; i += 2) {
                   1867:                UINT8 lo = dbcs_table[i + 0];
                   1868:                UINT8 hi = dbcs_table[i + 1];
                   1869:                if(lo == 0 && hi == 0) {
                   1870:                        break;
                   1871:                }
                   1872:                if(lo <= code && code <= hi) {
                   1873:                        return(1);
                   1874:                }
                   1875:        }
                   1876:        return(0);
                   1877: }
                   1878: 
1.1.1.20  root     1879: int msdos_ctrl_code_check(UINT8 code)
                   1880: {
1.1.1.22  root     1881:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     1882: }
                   1883: 
1.1       root     1884: // file control
                   1885: 
1.1.1.14  root     1886: char *msdos_remove_double_quote(char *path)
                   1887: {
                   1888:        static char tmp[MAX_PATH];
                   1889:        
                   1890:        memset(tmp, 0, sizeof(tmp));
                   1891:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   1892:                memcpy(tmp, path + 1, strlen(path) - 2);
                   1893:        } else {
                   1894:                strcpy(tmp, path);
                   1895:        }
                   1896:        return(tmp);
                   1897: }
                   1898: 
                   1899: char *msdos_combine_path(char *dir, const char *file)
                   1900: {
                   1901:        static char tmp[MAX_PATH];
                   1902:        char *tmp_dir = msdos_remove_double_quote(dir);
                   1903:        
                   1904:        if(strlen(tmp_dir) == 0) {
                   1905:                strcpy(tmp, file);
                   1906:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   1907:                sprintf(tmp, "%s%s", tmp_dir, file);
                   1908:        } else {
                   1909:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   1910:        }
                   1911:        return(tmp);
                   1912: }
                   1913: 
1.1       root     1914: char *msdos_trimmed_path(char *path, int lfn)
                   1915: {
                   1916:        static char tmp[MAX_PATH];
                   1917:        
                   1918:        if(lfn) {
                   1919:                strcpy(tmp, path);
                   1920:        } else {
                   1921:                // remove space in the path
                   1922:                char *src = path, *dst = tmp;
                   1923:                
                   1924:                while(*src != '\0') {
                   1925:                        if(msdos_lead_byte_check(*src)) {
                   1926:                                *dst++ = *src++;
                   1927:                                *dst++ = *src++;
                   1928:                        } else if(*src != ' ') {
                   1929:                                *dst++ = *src++;
                   1930:                        } else {
                   1931:                                src++;  // skip space
                   1932:                        }
                   1933:                }
                   1934:                *dst = '\0';
                   1935:        }
1.1.1.14  root     1936:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   1937:                // redirect C:\COMMAND.COM to comspec_path
                   1938:                strcpy(tmp, comspec_path);
                   1939:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   1940:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   1941:                static int root_drive_protected = -1;
                   1942:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   1943:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   1944:                
                   1945:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   1946:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   1947:                        strcpy(name, name_temp);
                   1948:                        name_temp[0] = '\0';
                   1949:                        
                   1950:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   1951:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   1952:                                if(root_drive_protected == -1) {
                   1953:                                        FILE *fp = NULL;
                   1954:                                        
                   1955:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   1956:                                        root_drive_protected = 1;
                   1957:                                        try {
                   1958:                                                if((fp = fopen(temp, "w")) != NULL) {
                   1959:                                                        if(fprintf(fp, "TEST") == 4) {
                   1960:                                                                root_drive_protected = 0;
                   1961:                                                        }
                   1962:                                                }
                   1963:                                        } catch(...) {
                   1964:                                        }
                   1965:                                        if(fp != NULL) {
                   1966:                                                fclose(fp);
                   1967:                                        }
                   1968:                                        if(_access(temp, 0) == 0) {
                   1969:                                                remove(temp);
                   1970:                                        }
                   1971:                                }
                   1972:                                if(root_drive_protected == 1) {
                   1973:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   1974:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   1975:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   1976:                                        }
                   1977:                                }
                   1978:                        }
                   1979:                }
                   1980:        }
1.1       root     1981:        return(tmp);
                   1982: }
                   1983: 
1.1.1.28! root     1984: char *msdos_get_multiple_short_path(char *src)
        !          1985: {
        !          1986:        // LONGPATH;LONGPATH;LONGPATH to SHORTPATH;SHORTPATH;SHORTPATH
        !          1987:        static char env_path[ENV_SIZE];
        !          1988:        char tmp[ENV_SIZE], *token;
        !          1989:        
        !          1990:        memset(env_path, 0, sizeof(env_path));
        !          1991:        strcpy(tmp, src);
        !          1992:        token = my_strtok(tmp, ";");
        !          1993:        
        !          1994:        while(token != NULL) {
        !          1995:                if(token[0] != '\0') {
        !          1996:                        char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
        !          1997:                        if(strlen(path) != 0) {
        !          1998:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
        !          1999:                                        strcat(env_path, path);
        !          2000:                                } else {
        !          2001:                                        my_strupr(short_path);
        !          2002:                                        strcat(env_path, short_path);
        !          2003:                                }
        !          2004:                                strcat(env_path, ";");
        !          2005:                        }
        !          2006:                }
        !          2007:                token = my_strtok(NULL, ";");
        !          2008:        }
        !          2009:        int len = strlen(env_path);
        !          2010:        if(len != 0 && env_path[len - 1] == ';') {
        !          2011:                env_path[len - 1] = '\0';
        !          2012:        }
        !          2013:        return(env_path);
        !          2014: }
        !          2015: 
1.1       root     2016: bool match(char *text, char *pattern)
                   2017: {
1.1.1.24  root     2018:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     2019:        switch(*pattern) {
1.1       root     2020:        case '\0':
                   2021:                return !*text;
                   2022:        case '*':
1.1.1.14  root     2023:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     2024:        case '?':
                   2025:                return *text && match(text + 1, pattern + 1);
                   2026:        default:
                   2027:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   2028:        }
                   2029: }
                   2030: 
                   2031: bool msdos_match_volume_label(char *path, char *volume)
                   2032: {
                   2033:        char *p;
                   2034:        
1.1.1.14  root     2035:        if(!*volume) {
                   2036:                return false;
                   2037:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     2038:                return msdos_match_volume_label(p + 1, volume);
                   2039:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   2040:                return msdos_match_volume_label(p + 1, volume);
                   2041:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     2042:                char tmp[MAX_PATH];
                   2043:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   2044:                return match(volume, tmp);
1.1       root     2045:        } else {
                   2046:                return match(volume, path);
                   2047:        }
                   2048: }
                   2049: 
                   2050: char *msdos_fcb_path(fcb_t *fcb)
                   2051: {
                   2052:        static char tmp[MAX_PATH];
                   2053:        char name[9], ext[4];
                   2054:        
                   2055:        memset(name, 0, sizeof(name));
                   2056:        memcpy(name, fcb->file_name, 8);
                   2057:        strcpy(name, msdos_trimmed_path(name, 0));
                   2058:        
                   2059:        memset(ext, 0, sizeof(ext));
                   2060:        memcpy(ext, fcb->file_name + 8, 3);
                   2061:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   2062:        
                   2063:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   2064:                strcpy(name, "*");
                   2065:        }
                   2066:        if(ext[0] == '\0') {
                   2067:                strcpy(tmp, name);
                   2068:        } else {
                   2069:                if(strcmp(ext, "???") == 0) {
                   2070:                        strcpy(ext, "*");
                   2071:                }
                   2072:                sprintf(tmp, "%s.%s", name, ext);
                   2073:        }
                   2074:        return(tmp);
                   2075: }
                   2076: 
                   2077: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   2078: {
                   2079:        char *ext = my_strchr(path, '.');
                   2080:        
                   2081:        memset(fcb->file_name, 0x20, 8 + 3);
                   2082:        if(ext != NULL && path[0] != '.') {
                   2083:                *ext = '\0';
                   2084:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   2085:        }
                   2086:        memcpy(fcb->file_name, path, strlen(path));
                   2087: }
                   2088: 
                   2089: char *msdos_short_path(char *path)
                   2090: {
                   2091:        static char tmp[MAX_PATH];
                   2092:        
1.1.1.24  root     2093:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   2094:                strcpy(tmp, path);
                   2095:        }
1.1       root     2096:        my_strupr(tmp);
                   2097:        return(tmp);
                   2098: }
                   2099: 
1.1.1.13  root     2100: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   2101: {
                   2102:        static char tmp[MAX_PATH];
                   2103: 
1.1.1.14  root     2104:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     2105:                strcpy(tmp, fd->cAlternateFileName);
                   2106:        } else {
                   2107:                strcpy(tmp, fd->cFileName);
                   2108:        }
                   2109:        my_strupr(tmp);
                   2110:        return(tmp);
                   2111: }
                   2112: 
1.1       root     2113: char *msdos_short_full_path(char *path)
                   2114: {
                   2115:        static char tmp[MAX_PATH];
                   2116:        char full[MAX_PATH], *name;
                   2117:        
1.1.1.14  root     2118:        // Full works with non-existent files, but Short does not
1.1       root     2119:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     2120:        *tmp = '\0';
                   2121:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   2122:                name[-1] = '\0';
                   2123:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   2124:                if(len == 0) {
                   2125:                        strcpy(tmp, full);
                   2126:                } else {
                   2127:                        tmp[len++] = '\\';
                   2128:                        strcpy(tmp + len, name);
                   2129:                }
                   2130:        }
1.1       root     2131:        my_strupr(tmp);
                   2132:        return(tmp);
                   2133: }
                   2134: 
                   2135: char *msdos_short_full_dir(char *path)
                   2136: {
                   2137:        static char tmp[MAX_PATH];
                   2138:        char full[MAX_PATH], *name;
                   2139:        
                   2140:        GetFullPathName(path, MAX_PATH, full, &name);
                   2141:        name[-1] = '\0';
1.1.1.24  root     2142:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   2143:                strcpy(tmp, full);
                   2144:        }
1.1       root     2145:        my_strupr(tmp);
                   2146:        return(tmp);
                   2147: }
                   2148: 
                   2149: char *msdos_local_file_path(char *path, int lfn)
                   2150: {
                   2151:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     2152: #if 0
                   2153:        // I have forgotten the reason of this routine... :-(
1.1       root     2154:        if(_access(trimmed, 0) != 0) {
                   2155:                process_t *process = msdos_process_info_get(current_psp);
                   2156:                static char tmp[MAX_PATH];
                   2157:                
                   2158:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   2159:                if(_access(tmp, 0) == 0) {
                   2160:                        return(tmp);
                   2161:                }
                   2162:        }
1.1.1.14  root     2163: #endif
1.1       root     2164:        return(trimmed);
                   2165: }
                   2166: 
1.1.1.11  root     2167: bool msdos_is_con_path(char *path)
                   2168: {
                   2169:        char full[MAX_PATH], *name;
                   2170:        
1.1.1.24  root     2171:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2172:                return(_stricmp(full, "\\\\.\\CON") == 0);
                   2173:        }
                   2174:        return(false);
1.1.1.11  root     2175: }
                   2176: 
1.1.1.14  root     2177: bool msdos_is_nul_path(char *path)
1.1.1.8   root     2178: {
1.1.1.14  root     2179:        char full[MAX_PATH], *name;
1.1.1.8   root     2180:        
1.1.1.24  root     2181:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2182:                return(_stricmp(full, "\\\\.\\NUL") == 0);
                   2183:        }
                   2184:        return(false);
                   2185: }
                   2186: 
                   2187: bool msdos_is_driver_name(char *path)
                   2188: {
                   2189:        char full[MAX_PATH], *name;
                   2190:        
                   2191:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2192:                if(_stricmp(name, "EMMXXXX0") == 0) {
                   2193:                        return(true);
                   2194:                }
                   2195:        }
                   2196:        return(false);
                   2197: }
                   2198: 
                   2199: bool msdos_is_existing_file(char *path)
                   2200: {
                   2201:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   2202:        WIN32_FIND_DATA FindData;
                   2203:        HANDLE hFind;
                   2204:        
                   2205:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   2206:                FindClose(hFind);
                   2207:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   2208:        }
                   2209:        return(false);
1.1.1.8   root     2210: }
                   2211: 
1.1.1.9   root     2212: char *msdos_search_command_com(char *command_path, char *env_path)
                   2213: {
                   2214:        static char tmp[MAX_PATH];
1.1.1.28! root     2215:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     2216:        
1.1.1.28! root     2217:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     2218:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   2219:                sprintf(file_name, "COMMAND.COM");
                   2220:                if(_access(tmp, 0) == 0) {
                   2221:                        return(tmp);
                   2222:                }
                   2223:        }
1.1.1.28! root     2224:        
        !          2225:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     2226:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   2227:                sprintf(file_name, "COMMAND.COM");
                   2228:                if(_access(tmp, 0) == 0) {
                   2229:                        return(tmp);
                   2230:                }
                   2231:        }
1.1.1.28! root     2232:        
        !          2233:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     2234:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   2235:                if(_access(tmp, 0) == 0) {
                   2236:                        return(tmp);
                   2237:                }
                   2238:        }
1.1.1.28! root     2239:        
        !          2240:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
        !          2241:        strcpy(path, env_path);
        !          2242:        char *token = my_strtok(path, ";");
1.1.1.9   root     2243:        while(token != NULL) {
1.1.1.14  root     2244:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     2245:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   2246:                        if(_access(tmp, 0) == 0) {
                   2247:                                return(tmp);
                   2248:                        }
                   2249:                }
                   2250:                token = my_strtok(NULL, ";");
                   2251:        }
                   2252:        return(NULL);
                   2253: }
                   2254: 
1.1.1.14  root     2255: int msdos_drive_number(const char *path)
1.1       root     2256: {
                   2257:        char tmp[MAX_PATH], *name;
                   2258:        
                   2259:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   2260:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   2261:                return(tmp[0] - 'a');
                   2262:        } else {
                   2263:                return(tmp[0] - 'A');
                   2264:        }
                   2265: }
                   2266: 
                   2267: char *msdos_volume_label(char *path)
                   2268: {
                   2269:        static char tmp[MAX_PATH];
                   2270:        char volume[] = "A:\\";
                   2271:        
                   2272:        if(path[1] == ':') {
                   2273:                volume[0] = path[0];
                   2274:        } else {
                   2275:                volume[0] = 'A' + _getdrive() - 1;
                   2276:        }
                   2277:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   2278:                memset(tmp, 0, sizeof(tmp));
                   2279:        }
                   2280:        return(tmp);
                   2281: }
                   2282: 
                   2283: char *msdos_short_volume_label(char *label)
                   2284: {
                   2285:        static char tmp[(8 + 1 + 3) + 1];
                   2286:        char *src = label;
                   2287:        int remain = strlen(label);
                   2288:        char *dst_n = tmp;
                   2289:        char *dst_e = tmp + 9;
                   2290:        
                   2291:        strcpy(tmp, "        .   ");
                   2292:        for(int i = 0; i < 8 && remain > 0; i++) {
                   2293:                if(msdos_lead_byte_check(*src)) {
                   2294:                        if(++i == 8) {
                   2295:                                break;
                   2296:                        }
                   2297:                        *dst_n++ = *src++;
                   2298:                        remain--;
                   2299:                }
                   2300:                *dst_n++ = *src++;
                   2301:                remain--;
                   2302:        }
                   2303:        if(remain > 0) {
                   2304:                for(int i = 0; i < 3 && remain > 0; i++) {
                   2305:                        if(msdos_lead_byte_check(*src)) {
                   2306:                                if(++i == 3) {
                   2307:                                        break;
                   2308:                                }
                   2309:                                *dst_e++ = *src++;
                   2310:                                remain--;
                   2311:                        }
                   2312:                        *dst_e++ = *src++;
                   2313:                        remain--;
                   2314:                }
                   2315:                *dst_e = '\0';
                   2316:        } else {
                   2317:                *dst_n = '\0';
                   2318:        }
                   2319:        my_strupr(tmp);
                   2320:        return(tmp);
                   2321: }
                   2322: 
1.1.1.13  root     2323: errno_t msdos_maperr(unsigned long oserrno)
                   2324: {
                   2325:        _doserrno = oserrno;
1.1.1.14  root     2326:        switch(oserrno) {
1.1.1.13  root     2327:        case ERROR_FILE_NOT_FOUND:         // 2
                   2328:        case ERROR_PATH_NOT_FOUND:         // 3
                   2329:        case ERROR_INVALID_DRIVE:          // 15
                   2330:        case ERROR_NO_MORE_FILES:          // 18
                   2331:        case ERROR_BAD_NETPATH:            // 53
                   2332:        case ERROR_BAD_NET_NAME:           // 67
                   2333:        case ERROR_BAD_PATHNAME:           // 161
                   2334:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   2335:                return ENOENT;
                   2336:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   2337:                return EMFILE;
                   2338:        case ERROR_ACCESS_DENIED:          // 5
                   2339:        case ERROR_CURRENT_DIRECTORY:      // 16
                   2340:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   2341:        case ERROR_CANNOT_MAKE:            // 82
                   2342:        case ERROR_FAIL_I24:               // 83
                   2343:        case ERROR_DRIVE_LOCKED:           // 108
                   2344:        case ERROR_SEEK_ON_DEVICE:         // 132
                   2345:        case ERROR_NOT_LOCKED:             // 158
                   2346:        case ERROR_LOCK_FAILED:            // 167
                   2347:                return EACCES;
                   2348:        case ERROR_INVALID_HANDLE:         // 6
                   2349:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   2350:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   2351:                return EBADF;
                   2352:        case ERROR_ARENA_TRASHED:          // 7
                   2353:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   2354:        case ERROR_INVALID_BLOCK:          // 9
                   2355:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   2356:                return ENOMEM;
                   2357:        case ERROR_BAD_ENVIRONMENT:        // 10
                   2358:                return E2BIG;
                   2359:        case ERROR_BAD_FORMAT:             // 11
                   2360:                return ENOEXEC;
                   2361:        case ERROR_NOT_SAME_DEVICE:        // 17
                   2362:                return EXDEV;
                   2363:        case ERROR_FILE_EXISTS:            // 80
                   2364:        case ERROR_ALREADY_EXISTS:         // 183
                   2365:                return EEXIST;
                   2366:        case ERROR_NO_PROC_SLOTS:          // 89
                   2367:        case ERROR_MAX_THRDS_REACHED:      // 164
                   2368:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   2369:                return EAGAIN;
                   2370:        case ERROR_BROKEN_PIPE:            // 109
                   2371:                return EPIPE;
                   2372:        case ERROR_DISK_FULL:              // 112
                   2373:                return ENOSPC;
                   2374:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   2375:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   2376:                return ECHILD;
                   2377:        case ERROR_DIR_NOT_EMPTY:          // 145
                   2378:                return ENOTEMPTY;
                   2379:        }
1.1.1.14  root     2380:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     2381:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   2382:                return EACCES;
                   2383:        }
1.1.1.14  root     2384:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     2385:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   2386:                return ENOEXEC;
                   2387:        }
                   2388:        return EINVAL;
                   2389: }
                   2390: 
                   2391: int msdos_open(const char *filename, int oflag)
                   2392: {
1.1.1.14  root     2393:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     2394:                return _open(filename, oflag);
                   2395:        }
1.1.1.14  root     2396:        
                   2397:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     2398:        DWORD disposition;
1.1.1.14  root     2399:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   2400:        default:
1.1.1.13  root     2401:        case _O_EXCL:
                   2402:                disposition = OPEN_EXISTING;
                   2403:                break;
                   2404:        case _O_CREAT:
                   2405:                disposition = OPEN_ALWAYS;
                   2406:                break;
                   2407:        case _O_CREAT | _O_EXCL:
                   2408:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   2409:                disposition = CREATE_NEW;
                   2410:                break;
                   2411:        case _O_TRUNC:
                   2412:        case _O_TRUNC | _O_EXCL:
                   2413:                disposition = TRUNCATE_EXISTING;
                   2414:                break;
                   2415:        case _O_CREAT | _O_TRUNC:
                   2416:                disposition = CREATE_ALWAYS;
                   2417:                break;
                   2418:        }
1.1.1.14  root     2419:        
1.1.1.13  root     2420:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   2421:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2422:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2423:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2424:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   2425:                // Retry without FILE_WRITE_ATTRIBUTES.
                   2426:                h = CreateFile(filename, GENERIC_READ,
                   2427:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2428:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2429:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2430:                        errno = msdos_maperr(GetLastError());
                   2431:                        return -1;
                   2432:                }
                   2433:        }
1.1.1.14  root     2434:        
1.1.1.13  root     2435:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     2436:        if(fd == -1) {
1.1.1.13  root     2437:                CloseHandle(h);
                   2438:        }
                   2439:        return fd;
                   2440: }
                   2441: 
1.1.1.14  root     2442: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
1.1       root     2443: {
                   2444:        static int id = 0;
                   2445:        char full[MAX_PATH], *name;
                   2446:        
                   2447:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2448:                strcpy(file_handler[fd].path, full);
                   2449:        } else {
                   2450:                strcpy(file_handler[fd].path, path);
                   2451:        }
1.1.1.14  root     2452:        // isatty makes no distinction between CON & NUL
                   2453:        // GetFileSize fails on CON, succeeds on NUL
                   2454:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   2455:                info = 0x8084;
                   2456:                atty = 0;
                   2457:        } else if(!atty && info == 0x80d3) {
                   2458:                info = msdos_drive_number(".");
                   2459:        }
1.1       root     2460:        file_handler[fd].valid = 1;
                   2461:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
                   2462:        file_handler[fd].atty = atty;
                   2463:        file_handler[fd].mode = mode;
                   2464:        file_handler[fd].info = info;
                   2465:        file_handler[fd].psp = psp_seg;
1.1.1.21  root     2466:        
                   2467:        // init system file table
                   2468:        if(fd < 20) {
                   2469:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   2470:                
                   2471:                memset(sft, 0, 0x3b);
                   2472:                
                   2473:                *(UINT16 *)(sft + 0x00) = 1;
                   2474:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   2475:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   2476:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   2477:                
                   2478:                if(!(file_handler[fd].info & 0x80)) {
                   2479:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   2480:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   2481:                        
                   2482:                        FILETIME time, local;
                   2483:                        HANDLE hHandle;
                   2484:                        WORD dos_date = 0, dos_time = 0;
                   2485:                        DWORD file_size = 0;
                   2486:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   2487:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   2488:                                        FileTimeToLocalFileTime(&time, &local);
                   2489:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   2490:                                }
                   2491:                                file_size = GetFileSize(hHandle, NULL);
                   2492:                        }
                   2493:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   2494:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   2495:                        *(UINT32 *)(sft + 0x11) = file_size;
                   2496:                }
                   2497:                
                   2498:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   2499:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   2500:                my_strupr(fname);
                   2501:                my_strupr(ext);
                   2502:                memset(sft + 0x20, 0x20, 11);
                   2503:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   2504:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   2505:                
                   2506:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   2507:        }
1.1       root     2508: }
                   2509: 
                   2510: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   2511: {
                   2512:        strcpy(file_handler[dst].path, file_handler[src].path);
                   2513:        file_handler[dst].valid = 1;
                   2514:        file_handler[dst].id = file_handler[src].id;
                   2515:        file_handler[dst].atty = file_handler[src].atty;
                   2516:        file_handler[dst].mode = file_handler[src].mode;
                   2517:        file_handler[dst].info = file_handler[src].info;
                   2518:        file_handler[dst].psp = psp_seg;
                   2519: }
                   2520: 
1.1.1.20  root     2521: void msdos_file_handler_close(int fd)
1.1       root     2522: {
                   2523:        file_handler[fd].valid = 0;
1.1.1.21  root     2524:        
                   2525:        if(fd < 20) {
                   2526:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   2527:        }
1.1       root     2528: }
                   2529: 
1.1.1.14  root     2530: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     2531: {
1.1.1.14  root     2532:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   2533:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   2534:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     2535: }
                   2536: 
                   2537: // find file
                   2538: 
                   2539: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   2540: {
                   2541:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2542:                return(0);      // search directory only !!!
                   2543:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   2544:                return(0);
                   2545:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   2546:                return(0);
                   2547:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2548:                return(0);
                   2549:        } else if((attribute & required_mask) != required_mask) {
                   2550:                return(0);
                   2551:        } else {
                   2552:                return(1);
                   2553:        }
                   2554: }
                   2555: 
1.1.1.13  root     2556: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   2557: {
1.1.1.14  root     2558:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     2559:                return 1;
                   2560:        }
                   2561:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     2562:        if(len > 12) {
1.1.1.13  root     2563:                return 0;
                   2564:        }
                   2565:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     2566:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.13  root     2567:                return 0;
                   2568:        }
                   2569:        return 1;
                   2570: }
                   2571: 
1.1       root     2572: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   2573: {
                   2574:        FILETIME local;
                   2575:        
                   2576:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   2577:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   2578:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   2579:        
                   2580:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   2581:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   2582:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   2583:        
                   2584:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   2585:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   2586:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   2587: }
                   2588: 
                   2589: // i/o
                   2590: 
                   2591: void msdos_stdio_reopen()
                   2592: {
                   2593:        if(!file_handler[0].valid) {
                   2594:                _dup2(DUP_STDIN, 0);
                   2595:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   2596:        }
                   2597:        if(!file_handler[1].valid) {
                   2598:                _dup2(DUP_STDOUT, 1);
                   2599:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   2600:        }
                   2601:        if(!file_handler[2].valid) {
                   2602:                _dup2(DUP_STDERR, 2);
                   2603:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   2604:        }
1.1.1.21  root     2605:        if(!file_handler[3].valid) {
                   2606:                _dup2(DUP_STDAUX, 3);
                   2607:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   2608:        }
                   2609:        if(!file_handler[4].valid) {
                   2610:                _dup2(DUP_STDPRN, 4);
                   2611:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   2612:        }
                   2613:        for(int i = 0; i < 5; i++) {
                   2614:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   2615:                        msdos_psp_set_file_table(i, i, current_psp);
                   2616:                }
                   2617:        }
1.1       root     2618: }
                   2619: 
                   2620: int msdos_kbhit()
                   2621: {
                   2622:        msdos_stdio_reopen();
                   2623:        
1.1.1.20  root     2624:        process_t *process = msdos_process_info_get(current_psp);
                   2625:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2626:        
                   2627:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2628:                // stdin is redirected to file
1.1.1.20  root     2629:                return(eof(fd) == 0);
1.1       root     2630:        }
                   2631:        
                   2632:        // check keyboard status
1.1.1.5   root     2633:        if(key_buf_char->count() != 0 || key_code != 0) {
1.1       root     2634:                return(1);
                   2635:        } else {
                   2636:                return(_kbhit());
                   2637:        }
                   2638: }
                   2639: 
                   2640: int msdos_getch_ex(int echo)
                   2641: {
                   2642:        static char prev = 0;
                   2643:        
                   2644:        msdos_stdio_reopen();
                   2645:        
1.1.1.20  root     2646:        process_t *process = msdos_process_info_get(current_psp);
                   2647:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2648:        
                   2649:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2650:                // stdin is redirected to file
                   2651: retry:
                   2652:                char data;
1.1.1.20  root     2653:                if(_read(fd, &data, 1) == 1) {
1.1       root     2654:                        char tmp = data;
                   2655:                        if(data == 0x0a) {
                   2656:                                if(prev == 0x0d) {
                   2657:                                        goto retry; // CRLF -> skip LF
                   2658:                                } else {
                   2659:                                        data = 0x0d; // LF only -> CR
                   2660:                                }
                   2661:                        }
                   2662:                        prev = tmp;
                   2663:                        return(data);
                   2664:                }
                   2665:                return(EOF);
                   2666:        }
                   2667:        
                   2668:        // input from console
1.1.1.5   root     2669:        int key_char, key_scan;
                   2670:        if(key_code != 0) {
                   2671:                key_char = (key_code >> 0) & 0xff;
                   2672:                key_scan = (key_code >> 8) & 0xff;
                   2673:                key_code >>= 16;
                   2674:        } else {
1.1.1.26  root     2675:                while(key_buf_char->count() == 0 && !m_halted && !ctrl_c_pressed) {
1.1.1.23  root     2676:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   2677:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   2678:                                if(_kbhit()) {
                   2679:                                        key_buf_char->write(_getch());
                   2680:                                        key_buf_scan->write(0);
                   2681:                                } else {
                   2682:                                        Sleep(10);
                   2683:                                }
                   2684:                        } else {
                   2685:                                if(!update_key_buffer()) {
                   2686:                                        Sleep(10);
                   2687:                                }
1.1.1.14  root     2688:                        }
                   2689:                }
                   2690:                if(m_halted) {
1.1.1.26  root     2691:                        // ctrl-break pressed - insert CR to terminate input loops
1.1.1.14  root     2692:                        key_char = 0x0d;
                   2693:                        key_scan = 0;
1.1.1.26  root     2694:                } else if(ctrl_c_pressed) {
                   2695:                        // ctrl-c pressed
                   2696:                        key_char = 0x03;
                   2697:                        key_scan = 0;
1.1.1.14  root     2698:                } else {
                   2699:                        key_char = key_buf_char->read();
                   2700:                        key_scan = key_buf_scan->read();
1.1.1.5   root     2701:                }
1.1       root     2702:        }
                   2703:        if(echo && key_char) {
                   2704:                msdos_putch(key_char);
                   2705:        }
                   2706:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   2707: }
                   2708: 
                   2709: inline int msdos_getch()
                   2710: {
                   2711:        return(msdos_getch_ex(0));
                   2712: }
                   2713: 
                   2714: inline int msdos_getche()
                   2715: {
                   2716:        return(msdos_getch_ex(1));
                   2717: }
                   2718: 
                   2719: int msdos_write(int fd, const void *buffer, unsigned int count)
                   2720: {
                   2721:        static int is_cr = 0;
                   2722:        
                   2723:        if(fd == 1 && !file_handler[1].atty) {
                   2724:                // CR+LF -> LF
                   2725:                UINT8 *buf = (UINT8 *)buffer;
                   2726:                for(unsigned int i = 0; i < count; i++) {
                   2727:                        UINT8 data = buf[i];
                   2728:                        if(is_cr) {
                   2729:                                if(data != 0x0a) {
                   2730:                                        UINT8 tmp = 0x0d;
                   2731:                                        _write(1, &tmp, 1);
                   2732:                                }
                   2733:                                _write(1, &data, 1);
                   2734:                                is_cr = 0;
                   2735:                        } else if(data == 0x0d) {
                   2736:                                is_cr = 1;
                   2737:                        } else {
                   2738:                                _write(1, &data, 1);
                   2739:                        }
                   2740:                }
                   2741:                return(count);
                   2742:        }
1.1.1.14  root     2743:        vram_flush();
1.1       root     2744:        return(_write(fd, buffer, count));
                   2745: }
                   2746: 
                   2747: void msdos_putch(UINT8 data)
                   2748: {
                   2749:        static int p = 0;
                   2750:        static int is_kanji = 0;
                   2751:        static int is_esc = 0;
                   2752:        static int stored_x;
                   2753:        static int stored_y;
                   2754:        static WORD stored_a;
1.1.1.20  root     2755:        static char tmp[64], out[64];
1.1       root     2756:        
                   2757:        msdos_stdio_reopen();
                   2758:        
1.1.1.20  root     2759:        process_t *process = msdos_process_info_get(current_psp);
                   2760:        int fd = msdos_psp_get_file_table(1, current_psp);
                   2761:        
                   2762:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2763:                // stdout is redirected to file
1.1.1.20  root     2764:                msdos_write(fd, &data, 1);
1.1       root     2765:                return;
                   2766:        }
1.1.1.23  root     2767:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2768:        
                   2769:        // output to console
                   2770:        tmp[p++] = data;
                   2771:        
1.1.1.14  root     2772:        vram_flush();
                   2773:        
1.1       root     2774:        if(is_kanji) {
                   2775:                // kanji character
                   2776:                is_kanji = 0;
                   2777:        } else if(is_esc) {
                   2778:                // escape sequense
                   2779:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   2780:                        p = is_esc = 0;
                   2781:                } else if(tmp[1] == '=' && p == 4) {
                   2782:                        COORD co;
                   2783:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     2784:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     2785:                        SetConsoleCursorPosition(hStdout, co);
                   2786:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     2787:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     2788:                        cursor_moved = false;
                   2789:                        p = is_esc = 0;
                   2790:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   2791:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2792:                        COORD co;
                   2793:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2794:                        co.X = csbi.dwCursorPosition.X;
                   2795:                        co.Y = csbi.dwCursorPosition.Y;
                   2796:                        WORD wAttributes = csbi.wAttributes;
                   2797:                        
                   2798:                        if(tmp[1] == 'D') {
                   2799:                                co.Y++;
                   2800:                        } else if(tmp[1] == 'E') {
                   2801:                                co.X = 0;
                   2802:                                co.Y++;
                   2803:                        } else if(tmp[1] == 'M') {
                   2804:                                co.Y--;
                   2805:                        } else if(tmp[1] == '*') {
                   2806:                                SMALL_RECT rect;
1.1.1.14  root     2807:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2808:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2809:                                co.X = 0;
                   2810:                                co.Y = csbi.srWindow.Top;
1.1       root     2811:                        } else if(tmp[1] == '[') {
                   2812:                                int param[256], params = 0;
                   2813:                                memset(param, 0, sizeof(param));
                   2814:                                for(int i = 2; i < p; i++) {
                   2815:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   2816:                                                param[params] *= 10;
                   2817:                                                param[params] += tmp[i] - '0';
                   2818:                                        } else {
                   2819:                                                params++;
                   2820:                                        }
                   2821:                                }
                   2822:                                if(data == 'A') {
1.1.1.14  root     2823:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     2824:                                } else if(data == 'B') {
1.1.1.14  root     2825:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     2826:                                } else if(data == 'C') {
1.1.1.14  root     2827:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     2828:                                } else if(data == 'D') {
1.1.1.14  root     2829:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     2830:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     2831:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   2832:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     2833:                                } else if(data == 'J') {
                   2834:                                        SMALL_RECT rect;
1.1.1.14  root     2835:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2836:                                        if(param[0] == 0) {
                   2837:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2838:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2839:                                                if(co.Y < csbi.srWindow.Bottom) {
                   2840:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2841:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2842:                                                }
                   2843:                                        } else if(param[0] == 1) {
1.1.1.14  root     2844:                                                if(co.Y > csbi.srWindow.Top) {
                   2845:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   2846:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2847:                                                }
                   2848:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2849:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2850:                                        } else if(param[0] == 2) {
1.1.1.14  root     2851:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2852:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2853:                                                co.X = co.Y = 0;
                   2854:                                        }
                   2855:                                } else if(data == 'K') {
                   2856:                                        SMALL_RECT rect;
1.1.1.14  root     2857:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2858:                                        if(param[0] == 0) {
                   2859:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2860:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2861:                                        } else if(param[0] == 1) {
                   2862:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2863:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2864:                                        } else if(param[0] == 2) {
                   2865:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2866:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2867:                                        }
                   2868:                                } else if(data == 'L') {
                   2869:                                        SMALL_RECT rect;
1.1.1.14  root     2870:                                        if(params == 0) {
                   2871:                                                param[0] = 1;
1.1       root     2872:                                        }
1.1.1.14  root     2873:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2874:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2875:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2876:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2877:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2878:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     2879:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2880:                                        co.X = 0;
                   2881:                                } else if(data == 'M') {
                   2882:                                        SMALL_RECT rect;
1.1.1.14  root     2883:                                        if(params == 0) {
                   2884:                                                param[0] = 1;
                   2885:                                        }
                   2886:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   2887:                                                clear_scr_buffer(csbi.wAttributes);
                   2888:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2889:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2890:                                        } else {
1.1.1.14  root     2891:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2892:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2893:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2894:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2895:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     2896:                                        }
                   2897:                                        co.X = 0;
                   2898:                                } else if(data == 'h') {
                   2899:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   2900:                                                CONSOLE_CURSOR_INFO cur;
                   2901:                                                GetConsoleCursorInfo(hStdout, &cur);
                   2902:                                                if(cur.bVisible) {
                   2903:                                                        cur.bVisible = FALSE;
1.1.1.14  root     2904: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     2905:                                                }
                   2906:                                        }
                   2907:                                } else if(data == 'l') {
                   2908:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   2909:                                                CONSOLE_CURSOR_INFO cur;
                   2910:                                                GetConsoleCursorInfo(hStdout, &cur);
                   2911:                                                if(!cur.bVisible) {
                   2912:                                                        cur.bVisible = TRUE;
1.1.1.14  root     2913: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     2914:                                                }
                   2915:                                        }
                   2916:                                } else if(data == 'm') {
                   2917:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   2918:                                        int reverse = 0, hidden = 0;
                   2919:                                        for(int i = 0; i < params; i++) {
                   2920:                                                if(param[i] == 1) {
                   2921:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   2922:                                                } else if(param[i] == 4) {
                   2923:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   2924:                                                } else if(param[i] == 7) {
                   2925:                                                        reverse = 1;
                   2926:                                                } else if(param[i] == 8 || param[i] == 16) {
                   2927:                                                        hidden = 1;
                   2928:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   2929:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2930:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   2931:                                                                param[i] -= 16;
                   2932:                                                        } else {
                   2933:                                                                param[i] -= 30;
                   2934:                                                        }
                   2935:                                                        if(param[i] & 1) {
                   2936:                                                                wAttributes |= FOREGROUND_RED;
                   2937:                                                        }
                   2938:                                                        if(param[i] & 2) {
                   2939:                                                                wAttributes |= FOREGROUND_GREEN;
                   2940:                                                        }
                   2941:                                                        if(param[i] & 4) {
                   2942:                                                                wAttributes |= FOREGROUND_BLUE;
                   2943:                                                        }
                   2944:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   2945:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   2946:                                                        if((param[i] - 40) & 1) {
                   2947:                                                                wAttributes |= BACKGROUND_RED;
                   2948:                                                        }
                   2949:                                                        if((param[i] - 40) & 2) {
                   2950:                                                                wAttributes |= BACKGROUND_GREEN;
                   2951:                                                        }
                   2952:                                                        if((param[i] - 40) & 4) {
                   2953:                                                                wAttributes |= BACKGROUND_BLUE;
                   2954:                                                        }
                   2955:                                                }
                   2956:                                        }
                   2957:                                        if(reverse) {
                   2958:                                                wAttributes &= ~0xff;
                   2959:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   2960:                                        }
                   2961:                                        if(hidden) {
                   2962:                                                wAttributes &= ~0x0f;
                   2963:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   2964:                                        }
                   2965:                                } else if(data == 'n') {
                   2966:                                        if(param[0] == 6) {
                   2967:                                                char tmp[16];
                   2968:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   2969:                                                int len = strlen(tmp);
                   2970:                                                for(int i = 0; i < len; i++) {
                   2971:                                                        key_buf_char->write(tmp[i]);
                   2972:                                                        key_buf_scan->write(0x00);
                   2973:                                                }
                   2974:                                        }
                   2975:                                } else if(data == 's') {
                   2976:                                        stored_x = co.X;
                   2977:                                        stored_y = co.Y;
                   2978:                                        stored_a = wAttributes;
                   2979:                                } else if(data == 'u') {
                   2980:                                        co.X = stored_x;
                   2981:                                        co.Y = stored_y;
                   2982:                                        wAttributes = stored_a;
                   2983:                                }
                   2984:                        }
                   2985:                        if(co.X < 0) {
                   2986:                                co.X = 0;
                   2987:                        } else if(co.X >= csbi.dwSize.X) {
                   2988:                                co.X = csbi.dwSize.X - 1;
                   2989:                        }
1.1.1.14  root     2990:                        if(co.Y < csbi.srWindow.Top) {
                   2991:                                co.Y = csbi.srWindow.Top;
                   2992:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   2993:                                co.Y = csbi.srWindow.Bottom;
1.1       root     2994:                        }
                   2995:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   2996:                                SetConsoleCursorPosition(hStdout, co);
                   2997:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     2998:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     2999:                                cursor_moved = false;
                   3000:                        }
                   3001:                        if(wAttributes != csbi.wAttributes) {
                   3002:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   3003:                        }
                   3004:                        p = is_esc = 0;
                   3005:                }
                   3006:                return;
                   3007:        } else {
                   3008:                if(msdos_lead_byte_check(data)) {
                   3009:                        is_kanji = 1;
                   3010:                        return;
                   3011:                } else if(data == 0x1b) {
                   3012:                        is_esc = 1;
                   3013:                        return;
                   3014:                }
                   3015:        }
1.1.1.20  root     3016:        
                   3017:        DWORD q = 0, num;
                   3018:        is_kanji = 0;
                   3019:        for(int i = 0; i < p; i++) {
                   3020:                UINT8 c = tmp[i];
                   3021:                if(is_kanji) {
                   3022:                        is_kanji = 0;
                   3023:                } else if(msdos_lead_byte_check(data)) {
                   3024:                        is_kanji = 1;
                   3025:                } else if(msdos_ctrl_code_check(data)) {
                   3026:                        out[q++] = '^';
                   3027:                        c += 'A' - 1;
                   3028:                }
                   3029:                out[q++] = c;
                   3030:        }
                   3031:        WriteConsole(hStdout, out, q, &num, NULL);
1.1       root     3032:        p = 0;
1.1.1.14  root     3033:        
1.1.1.15  root     3034:        if(!restore_console_on_exit) {
                   3035:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3036:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   3037:                scr_top = csbi.srWindow.Top;
                   3038:        }
1.1       root     3039:        cursor_moved = true;
                   3040: }
                   3041: 
                   3042: int msdos_aux_in()
                   3043: {
1.1.1.21  root     3044:        msdos_stdio_reopen();
                   3045:        
1.1.1.20  root     3046:        process_t *process = msdos_process_info_get(current_psp);
                   3047:        int fd = msdos_psp_get_file_table(3, current_psp);
                   3048:        
                   3049:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     3050:                char data = 0;
1.1.1.20  root     3051:                _read(fd, &data, 1);
1.1       root     3052:                return(data);
                   3053:        } else {
                   3054:                return(EOF);
                   3055:        }
                   3056: }
                   3057: 
                   3058: void msdos_aux_out(char data)
                   3059: {
1.1.1.21  root     3060:        msdos_stdio_reopen();
                   3061:        
1.1.1.20  root     3062:        process_t *process = msdos_process_info_get(current_psp);
                   3063:        int fd = msdos_psp_get_file_table(3, current_psp);
                   3064:        
                   3065:        if(fd < process->max_files && file_handler[fd].valid) {
                   3066:                msdos_write(fd, &data, 1);
1.1       root     3067:        }
                   3068: }
                   3069: 
                   3070: void msdos_prn_out(char data)
                   3071: {
1.1.1.21  root     3072:        msdos_stdio_reopen();
                   3073:        
1.1.1.20  root     3074:        process_t *process = msdos_process_info_get(current_psp);
                   3075:        int fd = msdos_psp_get_file_table(4, current_psp);
                   3076:        
                   3077:        if(fd < process->max_files && file_handler[fd].valid) {
                   3078:                msdos_write(fd, &data, 1);
1.1       root     3079:        }
                   3080: }
                   3081: 
                   3082: // memory control
                   3083: 
1.1.1.19  root     3084: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     3085: {
                   3086:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3087:        
                   3088:        mcb->mz = mz;
                   3089:        mcb->psp = psp;
1.1.1.19  root     3090:        mcb->paragraphs32 = paragraphs;
1.1       root     3091:        return(mcb);
                   3092: }
                   3093: 
                   3094: void msdos_mcb_check(mcb_t *mcb)
                   3095: {
                   3096:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28! root     3097:                #if 0
        !          3098:                        // shutdown now !!!
        !          3099:                        fatalerror("broken memory control block\n");
        !          3100:                #else
        !          3101:                        // return error code and continue
        !          3102:                        throw(0x07); // broken memory control block
        !          3103:                #endif
1.1       root     3104:        }
                   3105: }
                   3106: 
                   3107: int msdos_mem_split(int seg, int paragraphs)
                   3108: {
                   3109:        int mcb_seg = seg - 1;
                   3110:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3111:        msdos_mcb_check(mcb);
                   3112:        
1.1.1.19  root     3113:        if(mcb->paragraphs() > paragraphs) {
1.1       root     3114:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.19  root     3115:                int new_paragraphs = mcb->paragraphs() - paragraphs - 1;
1.1       root     3116:                
                   3117:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   3118:                mcb->mz = 'M';
1.1.1.19  root     3119:                mcb->paragraphs32 = paragraphs;
1.1       root     3120:                return(0);
                   3121:        }
                   3122:        return(-1);
                   3123: }
                   3124: 
                   3125: void msdos_mem_merge(int seg)
                   3126: {
                   3127:        int mcb_seg = seg - 1;
                   3128:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3129:        msdos_mcb_check(mcb);
                   3130:        
                   3131:        while(1) {
                   3132:                if(mcb->mz == 'Z') {
                   3133:                        break;
                   3134:                }
1.1.1.19  root     3135:                int next_seg = mcb_seg + 1 + mcb->paragraphs();
1.1       root     3136:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   3137:                msdos_mcb_check(next_mcb);
                   3138:                
                   3139:                if(next_mcb->psp != 0) {
                   3140:                        break;
                   3141:                }
                   3142:                mcb->mz = next_mcb->mz;
1.1.1.19  root     3143:                mcb->paragraphs32 = mcb->paragraphs() + 1 + next_mcb->paragraphs();
1.1       root     3144:        }
                   3145: }
                   3146: 
1.1.1.8   root     3147: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     3148: {
                   3149:        while(1) {
                   3150:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3151:                
1.1.1.14  root     3152:                if(mcb->psp == 0) {
                   3153:                        msdos_mem_merge(mcb_seg + 1);
                   3154:                } else {
                   3155:                        msdos_mcb_check(mcb);
                   3156:                }
1.1.1.8   root     3157:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.19  root     3158:                        if(mcb->psp == 0 && mcb->paragraphs() >= paragraphs) {
1.1       root     3159:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   3160:                                mcb->psp = current_psp;
                   3161:                                return(mcb_seg + 1);
                   3162:                        }
                   3163:                }
                   3164:                if(mcb->mz == 'Z') {
                   3165:                        break;
                   3166:                }
1.1.1.19  root     3167:                mcb_seg += 1 + mcb->paragraphs();
1.1       root     3168:        }
                   3169:        return(-1);
                   3170: }
                   3171: 
                   3172: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   3173: {
                   3174:        int mcb_seg = seg - 1;
                   3175:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3176:        msdos_mcb_check(mcb);
1.1.1.19  root     3177:        int current_paragraphs = mcb->paragraphs();
1.1       root     3178:        
                   3179:        msdos_mem_merge(seg);
1.1.1.19  root     3180:        if(paragraphs > mcb->paragraphs()) {
1.1.1.14  root     3181:                if(max_paragraphs) {
1.1.1.19  root     3182:                        *max_paragraphs = mcb->paragraphs();
1.1.1.14  root     3183:                }
1.1       root     3184:                msdos_mem_split(seg, current_paragraphs);
                   3185:                return(-1);
                   3186:        }
                   3187:        msdos_mem_split(seg, paragraphs);
                   3188:        return(0);
                   3189: }
                   3190: 
                   3191: void msdos_mem_free(int seg)
                   3192: {
                   3193:        int mcb_seg = seg - 1;
                   3194:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3195:        msdos_mcb_check(mcb);
                   3196:        
                   3197:        mcb->psp = 0;
                   3198:        msdos_mem_merge(seg);
                   3199: }
                   3200: 
1.1.1.8   root     3201: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     3202: {
                   3203:        int max_paragraphs = 0;
                   3204:        
                   3205:        while(1) {
                   3206:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3207:                msdos_mcb_check(mcb);
                   3208:                
1.1.1.8   root     3209:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.19  root     3210:                        if(mcb->psp == 0 && mcb->paragraphs() > max_paragraphs) {
                   3211:                                max_paragraphs = mcb->paragraphs();
1.1       root     3212:                        }
                   3213:                }
                   3214:                if(mcb->mz == 'Z') {
                   3215:                        break;
                   3216:                }
1.1.1.19  root     3217:                mcb_seg += 1 + mcb->paragraphs();
1.1       root     3218:        }
1.1.1.14  root     3219:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     3220: }
                   3221: 
1.1.1.8   root     3222: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   3223: {
                   3224:        int last_seg = -1;
                   3225:        
                   3226:        while(1) {
                   3227:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3228:                msdos_mcb_check(mcb);
                   3229:                
1.1.1.14  root     3230:                if(mcb->psp == psp) {
1.1.1.8   root     3231:                        last_seg = mcb_seg;
                   3232:                }
1.1.1.14  root     3233:                if(mcb->mz == 'Z') {
                   3234:                        break;
                   3235:                }
1.1.1.19  root     3236:                mcb_seg += 1 + mcb->paragraphs();
1.1.1.8   root     3237:        }
                   3238:        return(last_seg);
                   3239: }
                   3240: 
1.1.1.19  root     3241: int msdos_mem_get_umb_linked()
                   3242: {
                   3243:        int mcb_seg = first_mcb;
                   3244:        
                   3245:        while(1) {
                   3246:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3247:                msdos_mcb_check(mcb);
                   3248:                
                   3249:                if(mcb->mz == 'Z') {
                   3250:                        if(mcb_seg >= (UMB_TOP >> 4)) {
                   3251:                                return(-1);
                   3252:                        }
                   3253:                        break;
                   3254:                }
                   3255:                mcb_seg += 1 + mcb->paragraphs();
                   3256:        }
                   3257:        return(0);
                   3258: }
                   3259: 
                   3260: int msdos_mem_link_umb()
                   3261: {
                   3262:        int mcb_seg = first_mcb;
                   3263:        
                   3264:        while(1) {
                   3265:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3266:                msdos_mcb_check(mcb);
                   3267:                mcb_seg += 1 + mcb->paragraphs();
                   3268:                
                   3269:                if(mcb->mz == 'Z') {
                   3270:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   3271:                                mcb->mz = 'M';
1.1.1.20  root     3272:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     3273:                                return(-1);
                   3274:                        }
                   3275:                        break;
                   3276:                }
                   3277:        }
                   3278:        return(0);
                   3279: }
                   3280: 
                   3281: int msdos_mem_unlink_umb()
                   3282: {
                   3283:        int mcb_seg = first_mcb;
                   3284:        
                   3285:        while(1) {
                   3286:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3287:                msdos_mcb_check(mcb);
                   3288:                mcb_seg += 1 + mcb->paragraphs();
                   3289:                
                   3290:                if(mcb->mz == 'Z') {
                   3291:                        break;
                   3292:                } else {
                   3293:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   3294:                                mcb->mz = 'Z';
1.1.1.20  root     3295:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     3296:                                return(-1);
                   3297:                        }
                   3298:                }
                   3299:        }
                   3300:        return(0);
                   3301: }
                   3302: 
1.1       root     3303: // environment
                   3304: 
                   3305: void msdos_env_set_argv(int env_seg, char *argv)
                   3306: {
                   3307:        char *dst = (char *)(mem + (env_seg << 4));
                   3308:        
                   3309:        while(1) {
                   3310:                if(dst[0] == 0) {
                   3311:                        break;
                   3312:                }
                   3313:                dst += strlen(dst) + 1;
                   3314:        }
                   3315:        *dst++ = 0; // end of environment
                   3316:        *dst++ = 1; // top of argv[0]
                   3317:        *dst++ = 0;
                   3318:        memcpy(dst, argv, strlen(argv));
                   3319:        dst += strlen(argv);
                   3320:        *dst++ = 0;
                   3321:        *dst++ = 0;
                   3322: }
                   3323: 
                   3324: char *msdos_env_get_argv(int env_seg)
                   3325: {
                   3326:        static char env[ENV_SIZE];
                   3327:        char *src = env;
                   3328:        
                   3329:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   3330:        while(1) {
                   3331:                if(src[0] == 0) {
                   3332:                        if(src[1] == 1) {
                   3333:                                return(src + 3);
                   3334:                        }
                   3335:                        break;
                   3336:                }
                   3337:                src += strlen(src) + 1;
                   3338:        }
                   3339:        return(NULL);
                   3340: }
                   3341: 
                   3342: char *msdos_env_get(int env_seg, const char *name)
                   3343: {
                   3344:        static char env[ENV_SIZE];
                   3345:        char *src = env;
                   3346:        
                   3347:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   3348:        while(1) {
                   3349:                if(src[0] == 0) {
                   3350:                        break;
                   3351:                }
                   3352:                int len = strlen(src);
                   3353:                char *n = my_strtok(src, "=");
                   3354:                char *v = src + strlen(n) + 1;
                   3355:                
                   3356:                if(_stricmp(name, n) == 0) {
                   3357:                        return(v);
                   3358:                }
                   3359:                src += len + 1;
                   3360:        }
                   3361:        return(NULL);
                   3362: }
                   3363: 
                   3364: void msdos_env_set(int env_seg, char *name, char *value)
                   3365: {
                   3366:        char env[ENV_SIZE];
                   3367:        char *src = env;
                   3368:        char *dst = (char *)(mem + (env_seg << 4));
                   3369:        char *argv = msdos_env_get_argv(env_seg);
                   3370:        int done = 0;
                   3371:        
                   3372:        memcpy(src, dst, ENV_SIZE);
                   3373:        memset(dst, 0, ENV_SIZE);
                   3374:        while(1) {
                   3375:                if(src[0] == 0) {
                   3376:                        break;
                   3377:                }
                   3378:                int len = strlen(src);
                   3379:                char *n = my_strtok(src, "=");
                   3380:                char *v = src + strlen(n) + 1;
                   3381:                char tmp[1024];
                   3382:                
                   3383:                if(_stricmp(name, n) == 0) {
                   3384:                        sprintf(tmp, "%s=%s", n, value);
                   3385:                        done = 1;
                   3386:                } else {
                   3387:                        sprintf(tmp, "%s=%s", n, v);
                   3388:                }
                   3389:                memcpy(dst, tmp, strlen(tmp));
                   3390:                dst += strlen(tmp) + 1;
                   3391:                src += len + 1;
                   3392:        }
                   3393:        if(!done) {
                   3394:                char tmp[1024];
                   3395:                
                   3396:                sprintf(tmp, "%s=%s", name, value);
                   3397:                memcpy(dst, tmp, strlen(tmp));
                   3398:                dst += strlen(tmp) + 1;
                   3399:        }
                   3400:        if(argv) {
                   3401:                *dst++ = 0; // end of environment
                   3402:                *dst++ = 1; // top of argv[0]
                   3403:                *dst++ = 0;
                   3404:                memcpy(dst, argv, strlen(argv));
                   3405:                dst += strlen(argv);
                   3406:                *dst++ = 0;
                   3407:                *dst++ = 0;
                   3408:        }
                   3409: }
                   3410: 
                   3411: // process
                   3412: 
1.1.1.8   root     3413: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     3414: {
                   3415:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3416:        
                   3417:        memset(psp, 0, PSP_SIZE);
                   3418:        psp->exit[0] = 0xcd;
                   3419:        psp->exit[1] = 0x20;
1.1.1.8   root     3420:        psp->first_mcb = mcb_seg;
1.1       root     3421:        psp->far_call = 0xea;
                   3422:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   3423:        psp->cpm_entry.w.h = 0xf000;
                   3424:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   3425:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   3426:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   3427:        psp->parent_psp = parent_psp;
1.1.1.20  root     3428:        if(parent_psp == (UINT16)-1) {
                   3429:                for(int i = 0; i < 20; i++) {
                   3430:                        if(file_handler[i].valid) {
                   3431:                                psp->file_table[i] = i;
                   3432:                        } else {
                   3433:                                psp->file_table[i] = 0xff;
                   3434:                        }
1.1       root     3435:                }
1.1.1.20  root     3436:        } else {
                   3437:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     3438:        }
                   3439:        psp->env_seg = env_seg;
                   3440:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     3441:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     3442:        psp->file_table_size = 20;
                   3443:        psp->file_table_ptr.w.l = 0x18;
                   3444:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     3445:        psp->service[0] = 0xcd;
                   3446:        psp->service[1] = 0x21;
                   3447:        psp->service[2] = 0xcb;
                   3448:        return(psp);
                   3449: }
                   3450: 
1.1.1.20  root     3451: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   3452: {
                   3453:        if(psp_seg && fd < 20) {
                   3454:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3455:                psp->file_table[fd] = value;
                   3456:        }
                   3457: }
                   3458: 
                   3459: int msdos_psp_get_file_table(int fd, int psp_seg)
                   3460: {
                   3461:        if(psp_seg && fd < 20) {
                   3462:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3463:                fd = psp->file_table[fd];
                   3464:        }
                   3465:        return fd;
                   3466: }
                   3467: 
1.1       root     3468: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   3469: {
                   3470:        // load command file
                   3471:        int fd = -1;
                   3472:        int dos_command = 0;
1.1.1.24  root     3473:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1       root     3474:        
                   3475:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3476:        int opt_len = mem[opt_ofs];
                   3477:        memset(opt, 0, sizeof(opt));
                   3478:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3479:        
1.1.1.14  root     3480:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   3481:                // this is a batch file, run command.com
                   3482:                char tmp[MAX_PATH];
                   3483:                if(opt_len != 0) {
                   3484:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   3485:                } else {
                   3486:                        sprintf(tmp, "/C %s", cmd);
                   3487:                }
                   3488:                strcpy(opt, tmp);
                   3489:                opt_len = strlen(opt);
                   3490:                mem[opt_ofs] = opt_len;
                   3491:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3492:                strcpy(command, comspec_path);
                   3493:                strcpy(name_tmp, "COMMAND.COM");
                   3494:        } else {
                   3495:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   3496:                        // redirect C:\COMMAND.COM to comspec_path
                   3497:                        strcpy(command, comspec_path);
                   3498:                } else {
                   3499:                        strcpy(command, cmd);
                   3500:                }
1.1.1.24  root     3501:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   3502:                        return(-1);
                   3503:                }
1.1.1.14  root     3504:                memset(name_tmp, 0, sizeof(name_tmp));
                   3505:                strcpy(name_tmp, name);
                   3506:                
                   3507:                // check command.com
                   3508:                if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) {
                   3509:                        if(opt_len == 0) {
                   3510: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   3511:                                process_t *current_process = NULL;
                   3512:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   3513:                                        if(process[i].psp == current_psp) {
                   3514:                                                current_process = &process[i];
                   3515:                                                break;
                   3516:                                        }
                   3517:                                }
                   3518:                                if(current_process != NULL) {
                   3519:                                        param->cmd_line.dw = current_process->dta.dw;
                   3520:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3521:                                        opt_len = mem[opt_ofs];
                   3522:                                        memset(opt, 0, sizeof(opt));
                   3523:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3524:                                }
                   3525:                        }
                   3526:                        for(int i = 0; i < opt_len; i++) {
                   3527:                                if(opt[i] == ' ') {
                   3528:                                        continue;
                   3529:                                }
                   3530:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   3531:                                        for(int j = i + 3; j < opt_len; j++) {
                   3532:                                                if(opt[j] == ' ') {
                   3533:                                                        continue;
                   3534:                                                }
                   3535:                                                char *token = my_strtok(opt + j, " ");
                   3536:                                                
                   3537:                                                if(strlen(token) >= 5 && _stricmp(&token[strlen(token) - 4], ".BAT") == 0) {
                   3538:                                                        // this is a batch file, okay to run command.com
                   3539:                                                } else {
                   3540:                                                        // run program directly without command.com
                   3541:                                                        strcpy(command, token);
                   3542:                                                        char tmp[MAX_PATH];
                   3543:                                                        strcpy(tmp, token + strlen(token) + 1);
                   3544:                                                        strcpy(opt, tmp);
                   3545:                                                        opt_len = strlen(opt);
                   3546:                                                        mem[opt_ofs] = opt_len;
                   3547:                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3548:                                                        dos_command = 1;
                   3549:                                                }
                   3550:                                                break;
1.1       root     3551:                                        }
                   3552:                                }
1.1.1.14  root     3553:                                break;
1.1       root     3554:                        }
                   3555:                }
                   3556:        }
                   3557:        
                   3558:        // load command file
                   3559:        strcpy(path, command);
                   3560:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3561:                sprintf(path, "%s.COM", command);
                   3562:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3563:                        sprintf(path, "%s.EXE", command);
                   3564:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     3565:                                sprintf(path, "%s.BAT", command);
                   3566:                                if(_access(path, 0) == 0) {
                   3567:                                        // this is a batch file, run command.com
                   3568:                                        char tmp[MAX_PATH];
                   3569:                                        if(opt_len != 0) {
                   3570:                                                sprintf(tmp, "/C %s %s", path, opt);
                   3571:                                        } else {
                   3572:                                                sprintf(tmp, "/C %s", path);
                   3573:                                        }
                   3574:                                        strcpy(opt, tmp);
                   3575:                                        opt_len = strlen(opt);
                   3576:                                        mem[opt_ofs] = opt_len;
                   3577:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3578:                                        strcpy(path, comspec_path);
                   3579:                                        strcpy(name_tmp, "COMMAND.COM");
                   3580:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3581:                                } else {
                   3582:                                        // search path in parent environments
                   3583:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3584:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   3585:                                        if(env != NULL) {
                   3586:                                                char env_path[4096];
                   3587:                                                strcpy(env_path, env);
                   3588:                                                char *token = my_strtok(env_path, ";");
                   3589:                                                
                   3590:                                                while(token != NULL) {
                   3591:                                                        if(strlen(token) != 0) {
                   3592:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   3593:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3594:                                                                        break;
                   3595:                                                                }
                   3596:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   3597:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3598:                                                                        break;
                   3599:                                                                }
                   3600:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   3601:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3602:                                                                        break;
                   3603:                                                                }
                   3604:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   3605:                                                                if(_access(path, 0) == 0) {
                   3606:                                                                        // this is a batch file, run command.com
                   3607:                                                                        char tmp[MAX_PATH];
                   3608:                                                                        if(opt_len != 0) {
                   3609:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   3610:                                                                        } else {
                   3611:                                                                                sprintf(tmp, "/C %s", path);
                   3612:                                                                        }
                   3613:                                                                        strcpy(opt, tmp);
                   3614:                                                                        opt_len = strlen(opt);
                   3615:                                                                        mem[opt_ofs] = opt_len;
                   3616:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3617:                                                                        strcpy(path, comspec_path);
                   3618:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   3619:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3620:                                                                        break;
                   3621:                                                                }
1.1.1.8   root     3622:                                                        }
1.1.1.14  root     3623:                                                        token = my_strtok(NULL, ";");
1.1       root     3624:                                                }
                   3625:                                        }
                   3626:                                }
                   3627:                        }
                   3628:                }
                   3629:        }
                   3630:        if(fd == -1) {
                   3631:                if(dos_command) {
                   3632:                        // may be dos command
                   3633:                        char tmp[MAX_PATH];
                   3634:                        sprintf(tmp, "%s %s", command, opt);
                   3635:                        system(tmp);
                   3636:                        return(0);
                   3637:                } else {
                   3638:                        return(-1);
                   3639:                }
                   3640:        }
                   3641:        _read(fd, file_buffer, sizeof(file_buffer));
                   3642:        _close(fd);
                   3643:        
                   3644:        // copy environment
                   3645:        int env_seg, psp_seg;
                   3646:        
1.1.1.8   root     3647:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1       root     3648:                return(-1);
                   3649:        }
                   3650:        if(param->env_seg == 0) {
                   3651:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3652:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   3653:        } else {
                   3654:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   3655:        }
                   3656:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   3657:        
                   3658:        // check exe header
                   3659:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     3660:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     3661:        UINT16 cs, ss, ip, sp;
                   3662:        
                   3663:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   3664:                // memory allocation
                   3665:                int header_size = header->header_size * 16;
                   3666:                int load_size = header->pages * 512 - header_size;
                   3667:                if(header_size + load_size < 512) {
                   3668:                        load_size = 512 - header_size;
                   3669:                }
                   3670:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   3671:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   3672:                        msdos_mem_free(env_seg);
                   3673:                        return(-1);
                   3674:                }
                   3675:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   3676:                if(paragraphs > free_paragraphs) {
                   3677:                        paragraphs = free_paragraphs;
                   3678:                }
1.1.1.8   root     3679:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     3680:                        msdos_mem_free(env_seg);
                   3681:                        return(-1);
                   3682:                }
                   3683:                // relocation
                   3684:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3685:                for(int i = 0; i < header->relocations; i++) {
                   3686:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   3687:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   3688:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   3689:                }
                   3690:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   3691:                // segments
                   3692:                cs = header->init_cs + start_seg;
                   3693:                ss = header->init_ss + start_seg;
                   3694:                ip = header->init_ip;
                   3695:                sp = header->init_sp - 2; // for symdeb
                   3696:        } else {
                   3697:                // memory allocation
                   3698:                paragraphs = free_paragraphs;
1.1.1.8   root     3699:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     3700:                        msdos_mem_free(env_seg);
                   3701:                        return(-1);
                   3702:                }
                   3703:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3704:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   3705:                // segments
                   3706:                cs = ss = psp_seg;
                   3707:                ip = 0x100;
                   3708:                sp = 0xfffe;
                   3709:        }
                   3710:        
                   3711:        // create psp
1.1.1.3   root     3712: #if defined(HAS_I386)
                   3713:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   3714: #else
                   3715:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_pc - SREG_BASE(CS);
                   3716: #endif
                   3717:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     3718:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   3719:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   3720:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   3721:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   3722:        
                   3723:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   3724:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   3725:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   3726:        
1.1.1.4   root     3727:        for(int i = 0; i < 8; i++) {
                   3728:                if(name_tmp[i] == '.') {
                   3729:                        mcb_psp->prog_name[i] = '\0';
                   3730:                        break;
                   3731:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   3732:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3733:                        i++;
                   3734:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3735:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   3736:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   3737:                } else {
                   3738:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3739:                }
                   3740:        }
                   3741:        
1.1       root     3742:        // process info
                   3743:        process_t *process = msdos_process_info_create(psp_seg);
                   3744:        strcpy(process->module_dir, msdos_short_full_dir(path));
                   3745:        process->dta.w.l = 0x80;
                   3746:        process->dta.w.h = psp_seg;
                   3747:        process->switchar = '/';
                   3748:        process->max_files = 20;
                   3749:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   3750:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     3751:        process->parent_ds = SREG(DS);
1.1       root     3752:        
                   3753:        current_psp = psp_seg;
1.1.1.23  root     3754:        msdos_sda_update(current_psp);
1.1       root     3755:        
                   3756:        if(al == 0x00) {
                   3757:                int_10h_feh_called = int_10h_ffh_called = false;
                   3758:                
                   3759:                // registers and segments
                   3760:                REG16(AX) = REG16(BX) = 0x00;
                   3761:                REG16(CX) = 0xff;
                   3762:                REG16(DX) = psp_seg;
                   3763:                REG16(SI) = ip;
                   3764:                REG16(DI) = sp;
                   3765:                REG16(SP) = sp;
1.1.1.3   root     3766:                SREG(DS) = SREG(ES) = psp_seg;
                   3767:                SREG(SS) = ss;
                   3768:                i386_load_segment_descriptor(DS);
                   3769:                i386_load_segment_descriptor(ES);
                   3770:                i386_load_segment_descriptor(SS);
1.1       root     3771:                
                   3772:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   3773:                i386_jmp_far(cs, ip);
                   3774:        } else if(al == 0x01) {
                   3775:                // copy ss:sp and cs:ip to param block
                   3776:                param->sp = sp;
                   3777:                param->ss = ss;
                   3778:                param->ip = ip;
                   3779:                param->cs = cs;
                   3780:        }
                   3781:        return(0);
                   3782: }
                   3783: 
                   3784: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   3785: {
                   3786:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3787:        
                   3788:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   3789:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   3790:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   3791:        
1.1.1.3   root     3792:        SREG(SS) = psp->stack.w.h;
                   3793:        i386_load_segment_descriptor(SS);
1.1       root     3794:        REG16(SP) = psp->stack.w.l;
                   3795:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   3796:        
1.1.1.28! root     3797: //     process_t *current_process = msdos_process_info_get(psp_seg);
        !          3798:        process_t *current_process = NULL;
        !          3799:        for(int i = 0; i < MAX_PROCESS; i++) {
        !          3800:                if(process[i].psp == psp_seg) {
        !          3801:                        current_process = &process[i];
        !          3802:                        break;
        !          3803:                }
        !          3804:        }
        !          3805:        if(current_process == NULL) {
        !          3806:                throw(0x1f); // general failure
        !          3807:        }
        !          3808:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
        !          3809:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
        !          3810:        if(current_process->called_by_int2eh) {
        !          3811:                REG16(AX) = ret;
        !          3812:        }
        !          3813:        SREG(DS) = current_process->parent_ds;
1.1.1.14  root     3814:        i386_load_segment_descriptor(DS);
1.1       root     3815:        
                   3816:        if(mem_free) {
1.1.1.8   root     3817:                int mcb_seg;
                   3818:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   3819:                        msdos_mem_free(mcb_seg + 1);
                   3820:                }
                   3821:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   3822:                        msdos_mem_free(mcb_seg + 1);
                   3823:                }
1.1       root     3824:                
                   3825:                for(int i = 0; i < MAX_FILES; i++) {
                   3826:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   3827:                                _close(i);
1.1.1.20  root     3828:                                msdos_file_handler_close(i);
                   3829:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     3830:                        }
                   3831:                }
1.1.1.13  root     3832:                msdos_dta_info_free(psp_seg);
1.1       root     3833:        }
1.1.1.14  root     3834:        msdos_stdio_reopen();
1.1       root     3835:        
1.1.1.28! root     3836:        memset(current_process, 0, sizeof(process_t));
1.1       root     3837:        
                   3838:        current_psp = psp->parent_psp;
                   3839:        retval = ret;
1.1.1.23  root     3840:        msdos_sda_update(current_psp);
1.1       root     3841: }
                   3842: 
                   3843: // drive
                   3844: 
                   3845: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   3846: {
                   3847:        *seg = DPB_TOP >> 4;
                   3848:        *ofs = sizeof(dpb_t) * drive_num;
                   3849:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   3850:        
                   3851:        if(!force_update && dpb->free_clusters != 0) {
                   3852:                return(dpb->bytes_per_sector ? 1 : 0);
                   3853:        }
                   3854:        memset(dpb, 0, sizeof(dpb_t));
                   3855:        
                   3856:        int res = 0;
                   3857:        char dev[64];
                   3858:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   3859:        
1.1.1.17  root     3860:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     3861:        if(hFile != INVALID_HANDLE_VALUE) {
                   3862:                DISK_GEOMETRY geo;
                   3863:                DWORD dwSize;
                   3864:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   3865:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   3866:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   3867:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     3868:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     3869:                        switch(geo.MediaType) {
                   3870:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   3871:                                dpb->media_type = 0xff;
                   3872:                                break;
                   3873:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   3874:                                dpb->media_type = 0xfe;
                   3875:                                break;
                   3876:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   3877:                                dpb->media_type = 0xfd;
                   3878:                                break;
                   3879:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   3880:                                dpb->media_type = 0xfc;
                   3881:                                break;
                   3882:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   3883:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   3884:                                dpb->media_type = 0xf9;
                   3885:                                break;
                   3886:                        case FixedMedia:        // hard disk
                   3887:                        case RemovableMedia:
1.1.1.19  root     3888:                        case Unknown:
1.1       root     3889:                                dpb->media_type = 0xf8;
                   3890:                                break;
                   3891:                        default:
                   3892:                                dpb->media_type = 0xf0;
                   3893:                                break;
                   3894:                        }
                   3895:                        res = 1;
                   3896:                }
                   3897:                dpb->drive_num = drive_num;
                   3898:                dpb->unit_num = drive_num;
                   3899:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   3900:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     3901:                dpb->info_sector = 0xffff;
                   3902:                dpb->backup_boot_sector = 0xffff;
1.1       root     3903:                dpb->free_clusters = 0xffff;
1.1.1.14  root     3904:                dpb->free_search_cluster = 0xffffffff;
1.1       root     3905:                CloseHandle(hFile);
                   3906:        }
                   3907:        return(res);
                   3908: }
                   3909: 
                   3910: // pc bios
                   3911: 
1.1.1.19  root     3912: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   3913: {
                   3914:        static unsigned __int64 start_msec_since_midnight = 0;
                   3915:        static unsigned __int64 start_msec_since_hostboot = 0;
                   3916:        
                   3917:        if(start_msec_since_midnight == 0) {
                   3918:                SYSTEMTIME time;
                   3919:                GetLocalTime(&time);
                   3920:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   3921:                start_msec_since_hostboot = cur_msec;
                   3922:        }
                   3923:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   3924:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   3925:        return (UINT32)tick;
                   3926: }
                   3927: 
                   3928: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   3929: {
                   3930:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   3931:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   3932:        
                   3933:        if(prev_tick > next_tick) {
                   3934:                mem[0x470] = 1;
                   3935:        }
                   3936:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   3937: }
                   3938: 
1.1.1.14  root     3939: inline void pcbios_irq0()
                   3940: {
                   3941:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     3942:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     3943: }
                   3944: 
1.1.1.16  root     3945: int pcbios_get_text_vram_address(int page)
1.1       root     3946: {
                   3947:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     3948:                return TEXT_VRAM_TOP;
1.1       root     3949:        } else {
1.1.1.14  root     3950:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     3951:        }
                   3952: }
                   3953: 
1.1.1.16  root     3954: int pcbios_get_shadow_buffer_address(int page)
1.1       root     3955: {
1.1.1.14  root     3956:        if(!int_10h_feh_called) {
1.1.1.16  root     3957:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     3958:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     3959:                return SHADOW_BUF_TOP;
                   3960:        } else {
1.1.1.14  root     3961:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     3962:        }
                   3963: }
                   3964: 
1.1.1.16  root     3965: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     3966: {
1.1.1.16  root     3967:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     3968: }
                   3969: 
1.1.1.16  root     3970: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     3971: {
1.1.1.14  root     3972:        // clear the existing screen, not just the new one
                   3973:        int clr_height = max(height, scr_height);
                   3974:        
1.1.1.16  root     3975:        if(scr_width != width || scr_height != height) {
                   3976:                change_console_size(width, height);
1.1.1.14  root     3977:        }
                   3978:        mem[0x462] = 0;
                   3979:        *(UINT16 *)(mem + 0x44e) = 0;
                   3980:        
1.1.1.16  root     3981:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   3982:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   3983:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   3984:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     3985:        
1.1.1.23  root     3986:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     3987:        if(clr_screen) {
1.1.1.14  root     3988:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   3989:                        mem[ofs++] = 0x20;
                   3990:                        mem[ofs++] = 0x07;
                   3991:                }
                   3992:                
                   3993:                EnterCriticalSection(&vram_crit_sect);
                   3994:                for(int y = 0; y < clr_height; y++) {
                   3995:                        for(int x = 0; x < scr_width; x++) {
                   3996:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3997:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3998:                        }
                   3999:                }
                   4000:                SMALL_RECT rect;
1.1.1.14  root     4001:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   4002:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4003:                vram_length_char = vram_last_length_char = 0;
                   4004:                vram_length_attr = vram_last_length_attr = 0;
                   4005:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4006:        }
1.1.1.14  root     4007:        COORD co;
                   4008:        co.X = 0;
                   4009:        co.Y = scr_top;
                   4010:        SetConsoleCursorPosition(hStdout, co);
                   4011:        cursor_moved = true;
                   4012:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     4013: }
                   4014: 
1.1.1.16  root     4015: inline void pcbios_int_10h_00h()
                   4016: {
                   4017:        switch(REG8(AL) & 0x7f) {
                   4018:        case 0x70: // v-text mode
                   4019:        case 0x71: // extended cga v-text mode
                   4020:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   4021:                break;
                   4022:        default:
                   4023:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   4024:                break;
                   4025:        }
                   4026:        if(REG8(AL) & 0x80) {
                   4027:                mem[0x487] |= 0x80;
                   4028:        } else {
                   4029:                mem[0x487] &= ~0x80;
                   4030:        }
                   4031:        mem[0x449] = REG8(AL) & 0x7f;
                   4032: }
                   4033: 
1.1       root     4034: inline void pcbios_int_10h_01h()
                   4035: {
1.1.1.13  root     4036:        mem[0x460] = REG8(CL);
                   4037:        mem[0x461] = REG8(CH);
1.1.1.14  root     4038:        
1.1.1.23  root     4039:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4040:        CONSOLE_CURSOR_INFO ci;
                   4041:        GetConsoleCursorInfo(hStdout, &ci);
                   4042: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   4043: //     if(ci.bVisible) {
                   4044:                int lines = max(8, REG8(CL) + 1);
                   4045:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   4046: //     }
                   4047:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     4048: }
                   4049: 
                   4050: inline void pcbios_int_10h_02h()
                   4051: {
1.1.1.14  root     4052:        // continuously setting the cursor effectively stops it blinking
                   4053:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     4054:                COORD co;
                   4055:                co.X = REG8(DL);
1.1.1.14  root     4056:                co.Y = REG8(DH) + scr_top;
                   4057:                
                   4058:                // some programs hide the cursor by moving it off screen
                   4059:                static bool hidden = false;
1.1.1.23  root     4060:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4061:                CONSOLE_CURSOR_INFO ci;
                   4062:                GetConsoleCursorInfo(hStdout, &ci);
                   4063:                
                   4064:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   4065:                        if(ci.bVisible) {
                   4066:                                ci.bVisible = FALSE;
                   4067: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4068:                                hidden = true;
                   4069:                        }
                   4070:                } else if(hidden) {
                   4071:                        if(!ci.bVisible) {
                   4072:                                ci.bVisible = TRUE;
                   4073: //                             SetConsoleCursorInfo(hStdout, &ci);
                   4074:                        }
                   4075:                        hidden = false;
                   4076:                }
1.1       root     4077:        }
1.1.1.14  root     4078:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   4079:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     4080: }
                   4081: 
                   4082: inline void pcbios_int_10h_03h()
                   4083: {
1.1.1.14  root     4084:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4085:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4086:        REG8(CL) = mem[0x460];
                   4087:        REG8(CH) = mem[0x461];
                   4088: }
                   4089: 
                   4090: inline void pcbios_int_10h_05h()
                   4091: {
1.1.1.14  root     4092:        if(REG8(AL) >= vram_pages) {
                   4093:                return;
                   4094:        }
                   4095:        if(mem[0x462] != REG8(AL)) {
                   4096:                vram_flush();
                   4097:                
1.1.1.23  root     4098:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4099:                SMALL_RECT rect;
1.1.1.14  root     4100:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4101:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4102:                
1.1.1.16  root     4103:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     4104:                        for(int x = 0; x < scr_width; x++) {
                   4105:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4106:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4107:                        }
                   4108:                }
1.1.1.16  root     4109:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     4110:                        for(int x = 0; x < scr_width; x++) {
                   4111:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   4112:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     4113:                        }
                   4114:                }
1.1.1.14  root     4115:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4116:                
                   4117:                COORD co;
1.1.1.14  root     4118:                co.X = mem[0x450 + REG8(AL) * 2];
                   4119:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   4120:                if(co.Y < scr_top + scr_height) {
                   4121:                        SetConsoleCursorPosition(hStdout, co);
                   4122:                }
1.1       root     4123:        }
1.1.1.14  root     4124:        mem[0x462] = REG8(AL);
                   4125:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   4126:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     4127:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     4128:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     4129:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     4130:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     4131: }
                   4132: 
                   4133: inline void pcbios_int_10h_06h()
                   4134: {
1.1.1.14  root     4135:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4136:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4137:                return;
                   4138:        }
                   4139:        vram_flush();
                   4140:        
1.1.1.23  root     4141:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4142:        SMALL_RECT rect;
1.1.1.14  root     4143:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4144:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4145:        
                   4146:        int right = min(REG8(DL), scr_width - 1);
                   4147:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4148:        
                   4149:        if(REG8(AL) == 0) {
1.1.1.14  root     4150:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4151:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4152:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4153:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4154:                        }
                   4155:                }
                   4156:        } else {
1.1.1.14  root     4157:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     4158:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4159:                                if(y2 <= bottom) {
                   4160:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4161:                                } else {
1.1.1.14  root     4162:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4163:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4164:                                }
1.1.1.14  root     4165:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4166:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4167:                        }
                   4168:                }
                   4169:        }
1.1.1.14  root     4170:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4171: }
                   4172: 
                   4173: inline void pcbios_int_10h_07h()
                   4174: {
1.1.1.14  root     4175:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   4176:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   4177:                return;
                   4178:        }
                   4179:        vram_flush();
                   4180:        
1.1.1.23  root     4181:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4182:        SMALL_RECT rect;
1.1.1.14  root     4183:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   4184:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4185:        
                   4186:        int right = min(REG8(DL), scr_width - 1);
                   4187:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     4188:        
                   4189:        if(REG8(AL) == 0) {
1.1.1.14  root     4190:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     4191:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4192:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4193:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4194:                        }
                   4195:                }
                   4196:        } else {
1.1.1.14  root     4197:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     4198:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     4199:                                if(y2 >= REG8(CH)) {
                   4200:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     4201:                                } else {
1.1.1.14  root     4202:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   4203:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     4204:                                }
1.1.1.14  root     4205:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   4206:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     4207:                        }
                   4208:                }
                   4209:        }
1.1.1.14  root     4210:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4211: }
                   4212: 
                   4213: inline void pcbios_int_10h_08h()
                   4214: {
                   4215:        COORD co;
                   4216:        DWORD num;
                   4217:        
1.1.1.14  root     4218:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4219:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     4220:        
                   4221:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4222:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4223:                co.Y += scr_top;
                   4224:                vram_flush();
1.1       root     4225:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   4226:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   4227:                REG8(AL) = scr_char[0];
                   4228:                REG8(AH) = scr_attr[0];
                   4229:        } else {
1.1.1.16  root     4230:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     4231:        }
                   4232: }
                   4233: 
                   4234: inline void pcbios_int_10h_09h()
                   4235: {
                   4236:        COORD co;
                   4237:        
1.1.1.14  root     4238:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4239:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4240:        
1.1.1.16  root     4241:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4242:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4243:        
                   4244:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4245:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4246:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4247:                while(dest < end) {
                   4248:                        write_text_vram_char(dest - vram, REG8(AL));
                   4249:                        mem[dest++] = REG8(AL);
                   4250:                        write_text_vram_attr(dest - vram, REG8(BL));
                   4251:                        mem[dest++] = REG8(BL);
1.1       root     4252:                }
1.1.1.14  root     4253:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4254:        } else {
1.1.1.14  root     4255:                while(dest < end) {
1.1       root     4256:                        mem[dest++] = REG8(AL);
                   4257:                        mem[dest++] = REG8(BL);
                   4258:                }
                   4259:        }
                   4260: }
                   4261: 
                   4262: inline void pcbios_int_10h_0ah()
                   4263: {
                   4264:        COORD co;
                   4265:        
1.1.1.14  root     4266:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4267:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4268:        
1.1.1.16  root     4269:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   4270:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     4271:        
                   4272:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     4273:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4274:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4275:                while(dest < end) {
                   4276:                        write_text_vram_char(dest - vram, REG8(AL));
                   4277:                        mem[dest++] = REG8(AL);
                   4278:                        dest++;
1.1       root     4279:                }
1.1.1.14  root     4280:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     4281:        } else {
1.1.1.14  root     4282:                while(dest < end) {
1.1       root     4283:                        mem[dest++] = REG8(AL);
                   4284:                        dest++;
                   4285:                }
                   4286:        }
                   4287: }
                   4288: 
                   4289: inline void pcbios_int_10h_0eh()
                   4290: {
1.1.1.14  root     4291:        DWORD num;
                   4292:        COORD co;
                   4293:        
                   4294:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   4295:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   4296:        
                   4297:        if(REG8(AL) == 7) {
                   4298:                //MessageBeep(-1);
                   4299:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   4300:                if(REG8(AL) == 10) {
                   4301:                        vram_flush();
                   4302:                }
1.1.1.23  root     4303:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     4304:                cursor_moved = true;
                   4305:        } else {
1.1.1.16  root     4306:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     4307:                if(mem[0x462] == REG8(BH)) {
                   4308:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     4309:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     4310:                        write_text_vram_char(dest - vram, REG8(AL));
                   4311:                        LeaveCriticalSection(&vram_crit_sect);
                   4312:                        
1.1.1.23  root     4313:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4314:                        if(++co.X == scr_width) {
                   4315:                                co.X = 0;
                   4316:                                if(++co.Y == scr_height) {
                   4317:                                        vram_flush();
                   4318:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   4319:                                        cursor_moved = true;
                   4320:                                }
                   4321:                        }
                   4322:                        if(!cursor_moved) {
                   4323:                                co.Y += scr_top;
                   4324:                                SetConsoleCursorPosition(hStdout, co);
                   4325:                                cursor_moved = true;
                   4326:                        }
                   4327:                }
                   4328:                mem[dest] = REG8(AL);
                   4329:        }
1.1       root     4330: }
                   4331: 
                   4332: inline void pcbios_int_10h_0fh()
                   4333: {
                   4334:        REG8(AL) = mem[0x449];
                   4335:        REG8(AH) = mem[0x44a];
                   4336:        REG8(BH) = mem[0x462];
                   4337: }
                   4338: 
1.1.1.14  root     4339: inline void pcbios_int_10h_11h()
                   4340: {
                   4341:        switch(REG8(AL)) {
1.1.1.16  root     4342:        case 0x01:
1.1.1.14  root     4343:        case 0x11:
1.1.1.16  root     4344:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     4345:                break;
1.1.1.16  root     4346:        case 0x02:
1.1.1.14  root     4347:        case 0x12:
1.1.1.16  root     4348:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4349:                break;
1.1.1.16  root     4350:        case 0x04:
1.1.1.14  root     4351:        case 0x14:
1.1.1.16  root     4352:                pcbios_set_console_size(80, 25, true);
                   4353:                break;
                   4354:        case 0x18:
                   4355:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     4356:                break;
                   4357:        case 0x30:
                   4358:                SREG(ES) = 0;
                   4359:                i386_load_segment_descriptor(ES);
                   4360:                REG16(BP) = 0;
                   4361:                REG16(CX) = mem[0x485];
                   4362:                REG8(DL) = mem[0x484];
                   4363:                break;
                   4364:        }
                   4365: }
                   4366: 
                   4367: inline void pcbios_int_10h_12h()
                   4368: {
1.1.1.16  root     4369:        switch(REG8(BL)) {
                   4370:        case 0x10:
1.1.1.14  root     4371:                REG16(BX) = 0x0003;
                   4372:                REG16(CX) = 0x0009;
1.1.1.16  root     4373:                break;
1.1.1.14  root     4374:        }
                   4375: }
                   4376: 
1.1       root     4377: inline void pcbios_int_10h_13h()
                   4378: {
1.1.1.3   root     4379:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     4380:        COORD co;
                   4381:        DWORD num;
                   4382:        
                   4383:        co.X = REG8(DL);
1.1.1.14  root     4384:        co.Y = REG8(DH) + scr_top;
                   4385:        
                   4386:        vram_flush();
1.1       root     4387:        
                   4388:        switch(REG8(AL)) {
                   4389:        case 0x00:
                   4390:        case 0x01:
                   4391:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4392:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4393:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4394:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4395:                        SetConsoleCursorPosition(hStdout, co);
                   4396:                        
                   4397:                        if(csbi.wAttributes != REG8(BL)) {
                   4398:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   4399:                        }
1.1.1.14  root     4400:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   4401:                        
1.1       root     4402:                        if(csbi.wAttributes != REG8(BL)) {
                   4403:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4404:                        }
                   4405:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     4406:                                if(!restore_console_on_exit) {
                   4407:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4408:                                        scr_top = csbi.srWindow.Top;
                   4409:                                }
1.1.1.14  root     4410:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4411:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4412:                                SetConsoleCursorPosition(hStdout, co);
                   4413:                        } else {
                   4414:                                cursor_moved = true;
                   4415:                        }
                   4416:                } else {
1.1.1.3   root     4417:                        m_CF = 1;
1.1       root     4418:                }
                   4419:                break;
                   4420:        case 0x02:
                   4421:        case 0x03:
                   4422:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4423:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4424:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4425:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4426:                        SetConsoleCursorPosition(hStdout, co);
                   4427:                        
                   4428:                        WORD wAttributes = csbi.wAttributes;
                   4429:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   4430:                                if(wAttributes != mem[ofs + 1]) {
                   4431:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   4432:                                        wAttributes = mem[ofs + 1];
                   4433:                                }
1.1.1.14  root     4434:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     4435:                        }
                   4436:                        if(csbi.wAttributes != wAttributes) {
                   4437:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4438:                        }
                   4439:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     4440:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4441:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4442:                                SetConsoleCursorPosition(hStdout, co);
                   4443:                        } else {
                   4444:                                cursor_moved = true;
                   4445:                        }
                   4446:                } else {
1.1.1.3   root     4447:                        m_CF = 1;
1.1       root     4448:                }
                   4449:                break;
                   4450:        case 0x10:
                   4451:        case 0x11:
                   4452:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4453:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4454:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   4455:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   4456:                        for(int i = 0; i < num; i++) {
                   4457:                                mem[ofs++] = scr_char[i];
                   4458:                                mem[ofs++] = scr_attr[i];
                   4459:                                if(REG8(AL) == 0x11) {
                   4460:                                        mem[ofs++] = 0;
                   4461:                                        mem[ofs++] = 0;
                   4462:                                }
                   4463:                        }
                   4464:                } else {
1.1.1.16  root     4465:                        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     4466:                                mem[ofs++] = mem[src++];
                   4467:                                mem[ofs++] = mem[src++];
                   4468:                                if(REG8(AL) == 0x11) {
                   4469:                                        mem[ofs++] = 0;
                   4470:                                        mem[ofs++] = 0;
                   4471:                                }
1.1.1.14  root     4472:                                if(++co.X == scr_width) {
                   4473:                                        if(++co.Y == scr_height) {
1.1       root     4474:                                                break;
                   4475:                                        }
                   4476:                                        co.X = 0;
                   4477:                                }
                   4478:                        }
                   4479:                }
                   4480:                break;
                   4481:        case 0x20:
                   4482:        case 0x21:
                   4483:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4484:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4485:                        int len = min(REG16(CX), scr_width * scr_height);
                   4486:                        for(int i = 0; i < len; i++) {
1.1       root     4487:                                scr_char[i] = mem[ofs++];
                   4488:                                scr_attr[i] = mem[ofs++];
                   4489:                                if(REG8(AL) == 0x21) {
                   4490:                                        ofs += 2;
                   4491:                                }
                   4492:                        }
1.1.1.14  root     4493:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4494:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4495:                } else {
1.1.1.16  root     4496:                        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     4497:                                mem[dest++] = mem[ofs++];
                   4498:                                mem[dest++] = mem[ofs++];
                   4499:                                if(REG8(AL) == 0x21) {
                   4500:                                        ofs += 2;
                   4501:                                }
1.1.1.14  root     4502:                                if(++co.X == scr_width) {
                   4503:                                        if(++co.Y == scr_height) {
1.1       root     4504:                                                break;
                   4505:                                        }
                   4506:                                        co.X = 0;
                   4507:                                }
                   4508:                        }
                   4509:                }
                   4510:                break;
                   4511:        default:
1.1.1.22  root     4512:                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     4513:                m_CF = 1;
1.1       root     4514:                break;
                   4515:        }
                   4516: }
                   4517: 
1.1.1.14  root     4518: inline void pcbios_int_10h_1ah()
                   4519: {
                   4520:        switch(REG8(AL)) {
                   4521:        case 0x00:
                   4522:                REG8(AL) = 0x1a;
                   4523:                REG8(BL) = 0x08;
                   4524:                REG8(BH) = 0x00;
                   4525:                break;
                   4526:        default:
1.1.1.22  root     4527:                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     4528:                m_CF = 1;
                   4529:                break;
                   4530:        }
                   4531: }
                   4532: 
1.1       root     4533: inline void pcbios_int_10h_1dh()
                   4534: {
                   4535:        switch(REG8(AL)) {
                   4536:        case 0x01:
                   4537:                break;
                   4538:        case 0x02:
                   4539:                REG16(BX) = 0;
                   4540:                break;
                   4541:        default:
1.1.1.22  root     4542:                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));
                   4543:                m_CF = 1;
                   4544:                break;
                   4545:        }
                   4546: }
                   4547: 
                   4548: inline void pcbios_int_10h_4fh()
                   4549: {
                   4550:        switch(REG8(AL)) {
                   4551:        case 0x00:
                   4552:                REG8(AH) = 0x02; // not supported
                   4553:                break;
                   4554:        case 0x01:
                   4555:        case 0x02:
                   4556:        case 0x03:
                   4557:        case 0x04:
                   4558:        case 0x05:
                   4559:        case 0x06:
                   4560:        case 0x07:
                   4561:        case 0x08:
                   4562:        case 0x09:
                   4563:        case 0x0a:
                   4564:        case 0x0b:
                   4565:        case 0x0c:
                   4566:                REG8(AH) = 0x01; // failed
                   4567:                break;
                   4568:        default:
                   4569:                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     4570:                m_CF = 1;
1.1       root     4571:                break;
                   4572:        }
                   4573: }
                   4574: 
                   4575: inline void pcbios_int_10h_82h()
                   4576: {
                   4577:        static UINT8 mode = 0;
                   4578:        
                   4579:        switch(REG8(AL)) {
1.1.1.22  root     4580:        case 0x00:
1.1       root     4581:                if(REG8(BL) != 0xff) {
                   4582:                        mode = REG8(BL);
                   4583:                }
                   4584:                REG8(AL) = mode;
                   4585:                break;
                   4586:        default:
1.1.1.22  root     4587:                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     4588:                m_CF = 1;
1.1       root     4589:                break;
                   4590:        }
                   4591: }
                   4592: 
1.1.1.22  root     4593: inline void pcbios_int_10h_83h()
                   4594: {
                   4595:        static UINT8 mode = 0;
                   4596:        
                   4597:        switch(REG8(AL)) {
                   4598:        case 0x00:
                   4599:                REG16(AX) = 0; // offset???
                   4600:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   4601:                i386_load_segment_descriptor(ES);
                   4602:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   4603:                break;
                   4604:        default:
                   4605:                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));
                   4606:                m_CF = 1;
                   4607:                break;
                   4608:        }
                   4609: }
                   4610: 
                   4611: inline void pcbios_int_10h_90h()
                   4612: {
                   4613:        REG8(AL) = mem[0x449];
                   4614: }
                   4615: 
                   4616: inline void pcbios_int_10h_91h()
                   4617: {
                   4618:        REG8(AL) = 0x04; // VGA
                   4619: }
                   4620: 
                   4621: inline void pcbios_int_10h_efh()
                   4622: {
                   4623:        REG16(DX) = 0xffff;
                   4624: }
                   4625: 
1.1       root     4626: inline void pcbios_int_10h_feh()
                   4627: {
                   4628:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4629:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     4630:                i386_load_segment_descriptor(ES);
1.1.1.8   root     4631:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     4632:        }
                   4633:        int_10h_feh_called = true;
                   4634: }
                   4635: 
                   4636: inline void pcbios_int_10h_ffh()
                   4637: {
                   4638:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     4639:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4640:                COORD co;
                   4641:                DWORD num;
                   4642:                
1.1.1.14  root     4643:                vram_flush();
                   4644:                
                   4645:                co.X = (REG16(DI) >> 1) % scr_width;
                   4646:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     4647:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   4648:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     4649:                int len;
                   4650:                for(len = 0; ofs < end; len++) {
                   4651:                        scr_char[len] = mem[ofs++];
                   4652:                        scr_attr[len] = mem[ofs++];
                   4653:                }
                   4654:                co.Y += scr_top;
                   4655:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4656:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4657:        }
                   4658:        int_10h_ffh_called = true;
                   4659: }
                   4660: 
1.1.1.25  root     4661: inline void pcbios_int_14h_00h()
                   4662: {
                   4663:        if(REG16(DX) < 2) {
                   4664:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   4665:                UINT8 selector = sio_read(REG16(DX), 3);
                   4666:                selector &= ~0x3f;
                   4667:                selector |= REG8(AL) & 0x1f;
                   4668:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   4669:                sio_write(REG16(DX), 3, selector | 0x80);
                   4670:                sio_write(REG16(DX), 0, divisor & 0xff);
                   4671:                sio_write(REG16(DX), 1, divisor >> 8);
                   4672:                sio_write(REG16(DX), 3, selector);
                   4673:                REG8(AH) = sio_read(REG16(DX), 5);
                   4674:                REG8(AL) = sio_read(REG16(DX), 6);
                   4675:        } else {
                   4676:                REG8(AH) = 0x80;
                   4677:        }
                   4678: }
                   4679: 
                   4680: inline void pcbios_int_14h_01h()
                   4681: {
                   4682:        if(REG16(DX) < 2) {
                   4683:                UINT8 selector = sio_read(REG16(DX), 3);
                   4684:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4685:                sio_write(REG16(DX), 0, REG8(AL));
                   4686:                sio_write(REG16(DX), 3, selector);
                   4687:                REG8(AH) = sio_read(REG16(DX), 5);
                   4688:        } else {
                   4689:                REG8(AH) = 0x80;
                   4690:        }
                   4691: }
                   4692: 
                   4693: inline void pcbios_int_14h_02h()
                   4694: {
                   4695:        if(REG16(DX) < 2) {
                   4696:                UINT8 selector = sio_read(REG16(DX), 3);
                   4697:                sio_write(REG16(DX), 3, selector & ~0x80);
                   4698:                REG8(AL) = sio_read(REG16(DX), 0);
                   4699:                sio_write(REG16(DX), 3, selector);
                   4700:                REG8(AH) = sio_read(REG16(DX), 5);
                   4701:        } else {
                   4702:                REG8(AH) = 0x80;
                   4703:        }
                   4704: }
                   4705: 
                   4706: inline void pcbios_int_14h_03h()
                   4707: {
                   4708:        if(REG16(DX) < 2) {
                   4709:                REG8(AH) = sio_read(REG16(DX), 5);
                   4710:                REG8(AL) = sio_read(REG16(DX), 6);
                   4711:        } else {
                   4712:                REG8(AH) = 0x80;
                   4713:        }
                   4714: }
                   4715: 
                   4716: inline void pcbios_int_14h_04h()
                   4717: {
                   4718:        if(REG16(DX) < 2) {
                   4719:                UINT8 selector = sio_read(REG16(DX), 3);
                   4720:                if(REG8(CH) <= 0x03) {
                   4721:                        selector = (selector & ~0x03) | REG8(CH);
                   4722:                }
                   4723:                if(REG8(BL) == 0x00) {
                   4724:                        selector &= ~0x04;
                   4725:                } else if(REG8(BL) == 0x01) {
                   4726:                        selector |= 0x04;
                   4727:                }
                   4728:                if(REG8(BH) == 0x00) {
                   4729:                        selector = (selector & ~0x38) | 0x00;
                   4730:                } else if(REG8(BH) == 0x01) {
                   4731:                        selector = (selector & ~0x38) | 0x08;
                   4732:                } else if(REG8(BH) == 0x02) {
                   4733:                        selector = (selector & ~0x38) | 0x18;
                   4734:                } else if(REG8(BH) == 0x03) {
                   4735:                        selector = (selector & ~0x38) | 0x28;
                   4736:                } else if(REG8(BH) == 0x04) {
                   4737:                        selector = (selector & ~0x38) | 0x38;
                   4738:                }
                   4739:                if(REG8(AL) == 0x00) {
                   4740:                        selector |= 0x40;
                   4741:                } else if(REG8(AL) == 0x01) {
                   4742:                        selector &= ~0x40;
                   4743:                }
                   4744:                if(REG8(CL) <= 0x0b) {
                   4745:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   4746:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   4747:                        sio_write(REG16(DX), 3, selector | 0x80);
                   4748:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   4749:                        sio_write(REG16(DX), 1, divisor >> 8);
                   4750:                }
                   4751:                sio_write(REG16(DX), 3, selector);
                   4752:                REG8(AH) = sio_read(REG16(DX), 5);
                   4753:                REG8(AL) = sio_read(REG16(DX), 6);
                   4754:        } else {
                   4755:                REG8(AH) = 0x80;
                   4756:        }
                   4757: }
                   4758: 
                   4759: inline void pcbios_int_14h_05h()
                   4760: {
                   4761:        if(REG16(DX) < 2) {
                   4762:                if(REG8(AL) == 0x00) {
                   4763:                        REG8(BL) = sio_read(REG16(DX), 4);
                   4764:                        REG8(AH) = sio_read(REG16(DX), 5);
                   4765:                        REG8(AL) = sio_read(REG16(DX), 6);
                   4766:                } else if(REG8(AL) == 0x01) {
                   4767:                        sio_write(REG16(DX), 4, REG8(BL));
                   4768:                        REG8(AH) = sio_read(REG16(DX), 5);
                   4769:                        REG8(AL) = sio_read(REG16(DX), 6);
                   4770:                } else {
                   4771:                        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));
                   4772:                }
                   4773:        } else {
                   4774:                REG8(AH) = 0x80;
                   4775:        }
                   4776: }
                   4777: 
1.1.1.14  root     4778: inline void pcbios_int_15h_10h()
                   4779: {
1.1.1.22  root     4780:        switch(REG8(AL)) {
                   4781:        case 0x00:
1.1.1.14  root     4782:                Sleep(10);
                   4783:                hardware_update();
1.1.1.22  root     4784:                break;
                   4785:        default:
                   4786:                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     4787:                REG8(AH) = 0x86;
                   4788:                m_CF = 1;
                   4789:        }
                   4790: }
                   4791: 
1.1       root     4792: inline void pcbios_int_15h_23h()
                   4793: {
                   4794:        switch(REG8(AL)) {
1.1.1.22  root     4795:        case 0x00:
1.1.1.8   root     4796:                REG8(CL) = cmos_read(0x2d);
                   4797:                REG8(CH) = cmos_read(0x2e);
1.1       root     4798:                break;
1.1.1.22  root     4799:        case 0x01:
1.1.1.8   root     4800:                cmos_write(0x2d, REG8(CL));
                   4801:                cmos_write(0x2e, REG8(CH));
1.1       root     4802:                break;
                   4803:        default:
1.1.1.22  root     4804:                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     4805:                REG8(AH) = 0x86;
1.1.1.3   root     4806:                m_CF = 1;
1.1       root     4807:                break;
                   4808:        }
                   4809: }
                   4810: 
                   4811: inline void pcbios_int_15h_24h()
                   4812: {
                   4813:        switch(REG8(AL)) {
1.1.1.22  root     4814:        case 0x00:
1.1.1.3   root     4815:                i386_set_a20_line(0);
1.1       root     4816:                REG8(AH) = 0;
                   4817:                break;
1.1.1.22  root     4818:        case 0x01:
1.1.1.3   root     4819:                i386_set_a20_line(1);
1.1       root     4820:                REG8(AH) = 0;
                   4821:                break;
1.1.1.22  root     4822:        case 0x02:
1.1       root     4823:                REG8(AH) = 0;
1.1.1.3   root     4824:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     4825:                REG16(CX) = 0;
                   4826:                break;
1.1.1.22  root     4827:        case 0x03:
1.1       root     4828:                REG16(AX) = 0;
                   4829:                REG16(BX) = 0;
                   4830:                break;
1.1.1.22  root     4831:        default:
                   4832:                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));
                   4833:                REG8(AH) = 0x86;
                   4834:                m_CF = 1;
                   4835:                break;
1.1       root     4836:        }
                   4837: }
                   4838: 
                   4839: inline void pcbios_int_15h_49h()
                   4840: {
1.1.1.27  root     4841:        REG8(AH) = 0x00;
                   4842:        REG8(BL) = 0x00; // DOS/V
1.1       root     4843: }
                   4844: 
1.1.1.22  root     4845: inline void pcbios_int_15h_50h()
                   4846: {
                   4847:        switch(REG8(AL)) {
                   4848:        case 0x00:
                   4849:        case 0x01:
                   4850:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   4851:                        REG8(AH) = 0x01; // invalid font type in bh
                   4852:                        m_CF = 1;
1.1.1.27  root     4853:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     4854:                        REG8(AH) = 0x02; // bl not zero
                   4855:                        m_CF = 1;
                   4856:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   4857:                        REG8(AH) = 0x04; // invalid code page
                   4858:                        m_CF = 1;
1.1.1.27  root     4859:                } else if(REG8(AL) == 0x01) {
                   4860:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     4861:                        m_CF = 1;
1.1.1.27  root     4862:                } else {
                   4863:                        // dummy font read routine is at fffd:000d
                   4864:                        SREG(ES) = 0xfffd;
                   4865:                        i386_load_segment_descriptor(ES);
                   4866:                        REG16(BX) = 0x0d;
                   4867:                        REG8(AH) = 0x00; // success
1.1.1.22  root     4868:                }
                   4869:                break;
                   4870:        default:
                   4871:                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));
                   4872:                REG8(AH) = 0x86;
                   4873:                m_CF = 1;
                   4874:                break;
                   4875:        }
                   4876: }
                   4877: 
1.1       root     4878: inline void pcbios_int_15h_86h()
                   4879: {
                   4880:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     4881:        UINT32 msec = usec / 1000;
                   4882:        
                   4883:        while(msec) {
                   4884:                UINT32 tmp = min(msec, 100);
                   4885:                if(msec - tmp < 10) {
                   4886:                        tmp = msec;
                   4887:                }
                   4888:                Sleep(tmp);
                   4889:                
                   4890:                if(m_halted) {
                   4891:                        return;
                   4892:                }
                   4893:                msec -= tmp;
                   4894:        }
1.1       root     4895: }
                   4896: 
                   4897: inline void pcbios_int_15h_87h()
                   4898: {
                   4899:        // copy extended memory (from DOSBox)
                   4900:        int len = REG16(CX) * 2;
1.1.1.3   root     4901:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     4902:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   4903:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   4904:        memcpy(mem + dst, mem + src, len);
                   4905:        REG16(AX) = 0x00;
                   4906: }
                   4907: 
                   4908: inline void pcbios_int_15h_88h()
                   4909: {
1.1.1.17  root     4910:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     4911: }
                   4912: 
                   4913: inline void pcbios_int_15h_89h()
                   4914: {
1.1.1.21  root     4915: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     4916:        // switch to protected mode (from DOSBox)
                   4917:        write_io_byte(0x20, 0x10);
                   4918:        write_io_byte(0x21, REG8(BH));
                   4919:        write_io_byte(0x21, 0x00);
                   4920:        write_io_byte(0xa0, 0x10);
                   4921:        write_io_byte(0xa1, REG8(BL));
                   4922:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     4923:        i386_set_a20_line(1);
                   4924:        int ofs = SREG_BASE(ES) + REG16(SI);
                   4925:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   4926:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   4927:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   4928:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   4929: #if defined(HAS_I386)
                   4930:        m_cr[0] |= 1;
                   4931: #else
                   4932:        m_msw |= 1;
                   4933: #endif
                   4934:        SREG(DS) = 0x18;
                   4935:        SREG(ES) = 0x20;
                   4936:        SREG(SS) = 0x28;
                   4937:        i386_load_segment_descriptor(DS);
                   4938:        i386_load_segment_descriptor(ES);
                   4939:        i386_load_segment_descriptor(SS);
1.1.1.21  root     4940:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     4941:        REG16(SP) += 6;
1.1.1.3   root     4942: #if defined(HAS_I386)
1.1.1.21  root     4943:        UINT32 flags = get_flags();
                   4944:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   4945:        set_flags(flags);
1.1.1.3   root     4946: #else
1.1.1.21  root     4947:        UINT32 flags = CompressFlags();
                   4948:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   4949:        ExpandFlags(flags);
1.1.1.3   root     4950: #endif
1.1       root     4951:        REG16(AX) = 0x00;
1.1.1.21  root     4952:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     4953: #else
1.1.1.21  root     4954:        // i86/i186/v30: protected mode is not supported
1.1       root     4955:        REG8(AH) = 0x86;
1.1.1.3   root     4956:        m_CF = 1;
1.1       root     4957: #endif
                   4958: }
                   4959: 
1.1.1.21  root     4960: inline void pcbios_int_15h_8ah()
                   4961: {
                   4962:        UINT32 size = MAX_MEM - 0x100000;
                   4963:        REG16(AX) = size & 0xffff;
                   4964:        REG16(DX) = size >> 16;
                   4965: }
                   4966: 
1.1.1.3   root     4967: #if defined(HAS_I386)
1.1       root     4968: inline void pcbios_int_15h_c9h()
                   4969: {
                   4970:        REG8(AH) = 0x00;
                   4971:        REG8(CH) = cpu_type;
                   4972:        REG8(CL) = cpu_step;
                   4973: }
1.1.1.3   root     4974: #endif
1.1       root     4975: 
                   4976: inline void pcbios_int_15h_cah()
                   4977: {
                   4978:        switch(REG8(AL)) {
1.1.1.22  root     4979:        case 0x00:
1.1       root     4980:                if(REG8(BL) > 0x3f) {
                   4981:                        REG8(AH) = 0x03;
1.1.1.3   root     4982:                        m_CF = 1;
1.1       root     4983:                } else if(REG8(BL) < 0x0e) {
                   4984:                        REG8(AH) = 0x04;
1.1.1.3   root     4985:                        m_CF = 1;
1.1       root     4986:                } else {
1.1.1.8   root     4987:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     4988:                }
                   4989:                break;
1.1.1.22  root     4990:        case 0x01:
1.1       root     4991:                if(REG8(BL) > 0x3f) {
                   4992:                        REG8(AH) = 0x03;
1.1.1.3   root     4993:                        m_CF = 1;
1.1       root     4994:                } else if(REG8(BL) < 0x0e) {
                   4995:                        REG8(AH) = 0x04;
1.1.1.3   root     4996:                        m_CF = 1;
1.1       root     4997:                } else {
1.1.1.8   root     4998:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     4999:                }
                   5000:                break;
                   5001:        default:
1.1.1.22  root     5002:                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     5003:                REG8(AH) = 0x86;
1.1.1.3   root     5004:                m_CF = 1;
1.1       root     5005:                break;
                   5006:        }
                   5007: }
                   5008: 
1.1.1.22  root     5009: inline void pcbios_int_15h_e8h()
1.1.1.17  root     5010: {
1.1.1.22  root     5011:        switch(REG8(AL)) {
                   5012: #if defined(HAS_I386)
                   5013:        case 0x01:
                   5014:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   5015:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   5016:                break;
1.1.1.17  root     5017: #endif
1.1.1.22  root     5018:        default:
                   5019:                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));
                   5020:                REG8(AH) = 0x86;
                   5021:                m_CF = 1;
                   5022:                break;
                   5023:        }
                   5024: }
1.1.1.17  root     5025: 
1.1.1.16  root     5026: UINT32 pcbios_get_key_code(bool clear_buffer)
1.1       root     5027: {
                   5028:        UINT32 code = 0;
                   5029:        
                   5030:        if(key_buf_char->count() == 0) {
1.1.1.14  root     5031:                if(!update_key_buffer()) {
                   5032:                        if(clear_buffer) {
                   5033:                                Sleep(10);
                   5034:                        } else {
                   5035:                                maybe_idle();
                   5036:                        }
                   5037:                }
1.1       root     5038:        }
                   5039:        if(!clear_buffer) {
                   5040:                key_buf_char->store_buffer();
                   5041:                key_buf_scan->store_buffer();
                   5042:        }
                   5043:        if(key_buf_char->count() != 0) {
                   5044:                code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   5045:        }
                   5046:        if(key_buf_char->count() != 0) {
                   5047:                code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   5048:        }
                   5049:        if(!clear_buffer) {
                   5050:                key_buf_char->restore_buffer();
                   5051:                key_buf_scan->restore_buffer();
                   5052:        }
                   5053:        return code;
                   5054: }
                   5055: 
                   5056: inline void pcbios_int_16h_00h()
                   5057: {
1.1.1.14  root     5058:        while(key_code == 0 && !m_halted) {
1.1.1.16  root     5059:                key_code = pcbios_get_key_code(true);
1.1       root     5060:        }
                   5061:        if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   5062:                if(REG8(AH) == 0x10) {
                   5063:                        key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   5064:                } else {
                   5065:                        key_code = ((key_code >> 16) & 0xff00);
                   5066:                }
                   5067:        }
                   5068:        REG16(AX) = key_code & 0xffff;
                   5069:        key_code >>= 16;
                   5070: }
                   5071: 
                   5072: inline void pcbios_int_16h_01h()
                   5073: {
1.1.1.5   root     5074:        UINT32 key_code_tmp = key_code;
1.1       root     5075:        
1.1.1.5   root     5076:        if(key_code_tmp == 0) {
1.1.1.16  root     5077:                key_code_tmp = pcbios_get_key_code(false);
1.1.1.5   root     5078:        }
1.1.1.14  root     5079:        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   5080:                if(REG8(AH) == 0x11) {
                   5081:                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   5082:                } else {
                   5083:                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1       root     5084:                }
                   5085:        }
1.1.1.5   root     5086:        if(key_code_tmp != 0) {
                   5087:                REG16(AX) = key_code_tmp & 0xffff;
1.1       root     5088:        }
1.1.1.3   root     5089: #if defined(HAS_I386)
1.1.1.5   root     5090:        m_ZF = (key_code_tmp == 0);
1.1.1.3   root     5091: #else
1.1.1.5   root     5092:        m_ZeroVal = (key_code_tmp != 0);
1.1.1.3   root     5093: #endif
1.1       root     5094: }
                   5095: 
                   5096: inline void pcbios_int_16h_02h()
                   5097: {
                   5098:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   5099:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   5100:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   5101:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   5102:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   5103:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   5104:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   5105:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   5106: }
                   5107: 
                   5108: inline void pcbios_int_16h_03h()
                   5109: {
                   5110:        static UINT16 status = 0;
                   5111:        
                   5112:        switch(REG8(AL)) {
                   5113:        case 0x05:
                   5114:                status = REG16(BX);
                   5115:                break;
                   5116:        case 0x06:
                   5117:                REG16(BX) = status;
                   5118:                break;
                   5119:        default:
1.1.1.3   root     5120:                m_CF = 1;
1.1       root     5121:                break;
                   5122:        }
                   5123: }
                   5124: 
                   5125: inline void pcbios_int_16h_05h()
                   5126: {
1.1.1.14  root     5127:        key_buf_char->write(REG8(CL));
                   5128:        key_buf_scan->write(REG8(CH));
1.1       root     5129:        REG8(AL) = 0x00;
                   5130: }
                   5131: 
                   5132: inline void pcbios_int_16h_12h()
                   5133: {
                   5134:        pcbios_int_16h_02h();
                   5135:        
                   5136:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   5137:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   5138:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   5139:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   5140:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   5141:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   5142:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   5143:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   5144: }
                   5145: 
                   5146: inline void pcbios_int_16h_13h()
                   5147: {
                   5148:        static UINT16 status = 0;
                   5149:        
                   5150:        switch(REG8(AL)) {
                   5151:        case 0x00:
                   5152:                status = REG16(DX);
                   5153:                break;
                   5154:        case 0x01:
                   5155:                REG16(DX) = status;
                   5156:                break;
                   5157:        default:
1.1.1.22  root     5158:                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     5159:                m_CF = 1;
1.1       root     5160:                break;
                   5161:        }
                   5162: }
                   5163: 
                   5164: inline void pcbios_int_16h_14h()
                   5165: {
                   5166:        static UINT8 status = 0;
                   5167:        
                   5168:        switch(REG8(AL)) {
                   5169:        case 0x00:
                   5170:        case 0x01:
                   5171:                status = REG8(AL);
                   5172:                break;
                   5173:        case 0x02:
                   5174:                REG8(AL) = status;
                   5175:                break;
                   5176:        default:
1.1.1.22  root     5177:                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     5178:                m_CF = 1;
1.1       root     5179:                break;
                   5180:        }
                   5181: }
                   5182: 
1.1.1.24  root     5183: inline void pcbios_int_16h_55h()
                   5184: {
                   5185:        switch(REG8(AL)) {
                   5186:        case 0x00:
                   5187:                // keyboard tsr is not present
                   5188:                break;
                   5189:        case 0xfe:
                   5190:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   5191:                break;
                   5192:        case 0xff:
                   5193:                break;
                   5194:        default:
                   5195:                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));
                   5196:                m_CF = 1;
                   5197:                break;
                   5198:        }
                   5199: }
                   5200: 
1.1       root     5201: inline void pcbios_int_1ah_00h()
                   5202: {
1.1.1.19  root     5203:        pcbios_update_daily_timer_counter(timeGetTime());
                   5204:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   5205:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   5206:        REG8(AL) = mem[0x470];
                   5207:        mem[0x470] = 0;
1.1       root     5208: }
                   5209: 
                   5210: inline int to_bcd(int t)
                   5211: {
                   5212:        int u = (t % 100) / 10;
                   5213:        return (u << 4) | (t % 10);
                   5214: }
                   5215: 
                   5216: inline void pcbios_int_1ah_02h()
                   5217: {
                   5218:        SYSTEMTIME time;
                   5219:        
                   5220:        GetLocalTime(&time);
                   5221:        REG8(CH) = to_bcd(time.wHour);
                   5222:        REG8(CL) = to_bcd(time.wMinute);
                   5223:        REG8(DH) = to_bcd(time.wSecond);
                   5224:        REG8(DL) = 0x00;
                   5225: }
                   5226: 
                   5227: inline void pcbios_int_1ah_04h()
                   5228: {
                   5229:        SYSTEMTIME time;
                   5230:        
                   5231:        GetLocalTime(&time);
                   5232:        REG8(CH) = to_bcd(time.wYear / 100);
                   5233:        REG8(CL) = to_bcd(time.wYear);
                   5234:        REG8(DH) = to_bcd(time.wMonth);
                   5235:        REG8(DL) = to_bcd(time.wDay);
                   5236: }
                   5237: 
                   5238: inline void pcbios_int_1ah_0ah()
                   5239: {
                   5240:        SYSTEMTIME time;
                   5241:        FILETIME file_time;
                   5242:        WORD dos_date, dos_time;
                   5243:        
                   5244:        GetLocalTime(&time);
                   5245:        SystemTimeToFileTime(&time, &file_time);
                   5246:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   5247:        REG16(CX) = dos_date;
                   5248: }
                   5249: 
                   5250: // msdos system call
                   5251: 
                   5252: inline void msdos_int_21h_00h()
                   5253: {
1.1.1.3   root     5254:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     5255: }
                   5256: 
                   5257: inline void msdos_int_21h_01h()
                   5258: {
                   5259:        REG8(AL) = msdos_getche();
1.1.1.26  root     5260:        ctrl_c_detected = ctrl_c_pressed;
                   5261:        
1.1.1.8   root     5262:        // some seconds may be passed in console
1.1       root     5263:        hardware_update();
                   5264: }
                   5265: 
                   5266: inline void msdos_int_21h_02h()
                   5267: {
                   5268:        msdos_putch(REG8(DL));
1.1.1.26  root     5269:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5270: }
                   5271: 
                   5272: inline void msdos_int_21h_03h()
                   5273: {
                   5274:        REG8(AL) = msdos_aux_in();
                   5275: }
                   5276: 
                   5277: inline void msdos_int_21h_04h()
                   5278: {
                   5279:        msdos_aux_out(REG8(DL));
                   5280: }
                   5281: 
                   5282: inline void msdos_int_21h_05h()
                   5283: {
                   5284:        msdos_prn_out(REG8(DL));
                   5285: }
                   5286: 
                   5287: inline void msdos_int_21h_06h()
                   5288: {
                   5289:        if(REG8(DL) == 0xff) {
                   5290:                if(msdos_kbhit()) {
                   5291:                        REG8(AL) = msdos_getch();
1.1.1.3   root     5292: #if defined(HAS_I386)
                   5293:                        m_ZF = 0;
                   5294: #else
                   5295:                        m_ZeroVal = 1;
                   5296: #endif
1.1       root     5297:                } else {
                   5298:                        REG8(AL) = 0;
1.1.1.3   root     5299: #if defined(HAS_I386)
                   5300:                        m_ZF = 1;
                   5301: #else
                   5302:                        m_ZeroVal = 0;
                   5303: #endif
1.1.1.14  root     5304:                        maybe_idle();
1.1       root     5305:                }
                   5306:        } else {
                   5307:                msdos_putch(REG8(DL));
                   5308:        }
                   5309: }
                   5310: 
                   5311: inline void msdos_int_21h_07h()
                   5312: {
                   5313:        REG8(AL) = msdos_getch();
1.1.1.26  root     5314:        
1.1.1.8   root     5315:        // some seconds may be passed in console
1.1       root     5316:        hardware_update();
                   5317: }
                   5318: 
                   5319: inline void msdos_int_21h_08h()
                   5320: {
                   5321:        REG8(AL) = msdos_getch();
1.1.1.26  root     5322:        ctrl_c_detected = ctrl_c_pressed;
                   5323:        
1.1.1.8   root     5324:        // some seconds may be passed in console
1.1       root     5325:        hardware_update();
                   5326: }
                   5327: 
                   5328: inline void msdos_int_21h_09h()
                   5329: {
1.1.1.21  root     5330:        msdos_stdio_reopen();
                   5331:        
1.1.1.20  root     5332:        process_t *process = msdos_process_info_get(current_psp);
                   5333:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5334:        
1.1.1.14  root     5335:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   5336:        int len = 0;
1.1       root     5337:        
1.1.1.14  root     5338:        while(str[len] != '$' && len < 0x10000) {
                   5339:                len++;
                   5340:        }
1.1.1.20  root     5341:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5342:                // stdout is redirected to file
1.1.1.20  root     5343:                msdos_write(fd, str, len);
1.1       root     5344:        } else {
                   5345:                for(int i = 0; i < len; i++) {
1.1.1.14  root     5346:                        msdos_putch(str[i]);
1.1       root     5347:                }
                   5348:        }
1.1.1.26  root     5349:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5350: }
                   5351: 
                   5352: inline void msdos_int_21h_0ah()
                   5353: {
1.1.1.3   root     5354:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     5355:        int max = mem[ofs] - 1;
                   5356:        UINT8 *buf = mem + ofs + 2;
                   5357:        int chr, p = 0;
                   5358:        
                   5359:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.26  root     5360:                if(ctrl_c_pressed) {
                   5361:                        p = 0;
                   5362:                        msdos_putch(chr);
                   5363:                        break;
                   5364:                } else if(chr == 0x00) {
1.1       root     5365:                        // skip 2nd byte
                   5366:                        msdos_getch();
                   5367:                } else if(chr == 0x08) {
                   5368:                        // back space
                   5369:                        if(p > 0) {
                   5370:                                p--;
1.1.1.20  root     5371:                                if(msdos_ctrl_code_check(buf[p])) {
                   5372:                                        msdos_putch(chr);
                   5373:                                        msdos_putch(chr);
                   5374:                                        msdos_putch(' ');
                   5375:                                        msdos_putch(' ');
                   5376:                                        msdos_putch(chr);
                   5377:                                        msdos_putch(chr);
                   5378:                                } else {
                   5379:                                        msdos_putch(chr);
                   5380:                                        msdos_putch(' ');
                   5381:                                        msdos_putch(chr);
                   5382:                                }
1.1       root     5383:                        }
                   5384:                } else if(p < max) {
                   5385:                        buf[p++] = chr;
                   5386:                        msdos_putch(chr);
                   5387:                }
                   5388:        }
                   5389:        buf[p] = 0x0d;
                   5390:        mem[ofs + 1] = p;
1.1.1.26  root     5391:        ctrl_c_detected = ctrl_c_pressed;
                   5392:        
1.1.1.8   root     5393:        // some seconds may be passed in console
1.1       root     5394:        hardware_update();
                   5395: }
                   5396: 
                   5397: inline void msdos_int_21h_0bh()
                   5398: {
                   5399:        if(msdos_kbhit()) {
                   5400:                REG8(AL) = 0xff;
                   5401:        } else {
                   5402:                REG8(AL) = 0x00;
1.1.1.14  root     5403:                maybe_idle();
1.1       root     5404:        }
1.1.1.26  root     5405:        ctrl_c_detected = ctrl_c_pressed;
1.1       root     5406: }
                   5407: 
                   5408: inline void msdos_int_21h_0ch()
                   5409: {
                   5410:        // clear key buffer
1.1.1.21  root     5411:        msdos_stdio_reopen();
                   5412:        
1.1.1.20  root     5413:        process_t *process = msdos_process_info_get(current_psp);
                   5414:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5415:        
                   5416:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5417:                // stdin is redirected to file
                   5418:        } else {
                   5419:                while(msdos_kbhit()) {
                   5420:                        msdos_getch();
                   5421:                }
                   5422:        }
                   5423:        
                   5424:        switch(REG8(AL)) {
                   5425:        case 0x01:
                   5426:                msdos_int_21h_01h();
                   5427:                break;
                   5428:        case 0x06:
                   5429:                msdos_int_21h_06h();
                   5430:                break;
                   5431:        case 0x07:
                   5432:                msdos_int_21h_07h();
                   5433:                break;
                   5434:        case 0x08:
                   5435:                msdos_int_21h_08h();
                   5436:                break;
                   5437:        case 0x0a:
                   5438:                msdos_int_21h_0ah();
                   5439:                break;
                   5440:        default:
1.1.1.22  root     5441: //             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));
                   5442: //             REG16(AX) = 0x01;
                   5443: //             m_CF = 1;
1.1       root     5444:                break;
                   5445:        }
                   5446: }
                   5447: 
                   5448: inline void msdos_int_21h_0dh()
                   5449: {
                   5450: }
                   5451: 
                   5452: inline void msdos_int_21h_0eh()
                   5453: {
                   5454:        if(REG8(DL) < 26) {
                   5455:                _chdrive(REG8(DL) + 1);
                   5456:                msdos_cds_update(REG8(DL));
1.1.1.23  root     5457:                msdos_sda_update(current_psp);
1.1       root     5458:        }
                   5459:        REG8(AL) = 26; // zdrive
                   5460: }
                   5461: 
1.1.1.14  root     5462: inline void msdos_int_21h_0fh()
                   5463: {
                   5464:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5465:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5466:        char *path = msdos_fcb_path(fcb);
                   5467:        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     5468:        
1.1.1.14  root     5469:        if(hFile == INVALID_HANDLE_VALUE) {
                   5470:                REG8(AL) = 0xff;
                   5471:        } else {
                   5472:                REG8(AL) = 0;
                   5473:                fcb->current_block = 0;
                   5474:                fcb->record_size = 128;
                   5475:                fcb->file_size = GetFileSize(hFile, NULL);
                   5476:                fcb->handle = hFile;
                   5477:                fcb->cur_record = 0;
                   5478:        }
                   5479: }
                   5480: 
                   5481: inline void msdos_int_21h_10h()
                   5482: {
                   5483:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5484:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5485:        
                   5486:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   5487: }
                   5488: 
1.1       root     5489: inline void msdos_int_21h_11h()
                   5490: {
1.1.1.3   root     5491:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5492:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5493:        
                   5494:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5495:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5496:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5497:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5498:        char *path = msdos_fcb_path(fcb);
                   5499:        WIN32_FIND_DATA fd;
                   5500:        
1.1.1.13  root     5501:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5502:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5503:                FindClose(dtainfo->find_handle);
                   5504:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5505:        }
                   5506:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     5507:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   5508:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     5509:        
1.1.1.14  root     5510:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   5511:                dtainfo->allowable_mask &= ~8;
1.1       root     5512:        }
1.1.1.14  root     5513:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   5514:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5515:                      !msdos_find_file_has_8dot3name(&fd)) {
                   5516:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5517:                                FindClose(dtainfo->find_handle);
                   5518:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5519:                                break;
                   5520:                        }
                   5521:                }
                   5522:        }
1.1.1.13  root     5523:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5524:                if(ext_fcb->flag == 0xff) {
                   5525:                        ext_find->flag = 0xff;
                   5526:                        memset(ext_find->reserved, 0, 5);
                   5527:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5528:                }
                   5529:                find->drive = _getdrive();
1.1.1.13  root     5530:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5531:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5532:                find->nt_res = 0;
                   5533:                msdos_find_file_conv_local_time(&fd);
                   5534:                find->create_time_ms = 0;
                   5535:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5536:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5537:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5538:                find->cluster_hi = find->cluster_lo = 0;
                   5539:                find->file_size = fd.nFileSizeLow;
                   5540:                REG8(AL) = 0x00;
1.1.1.14  root     5541:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5542:                if(ext_fcb->flag == 0xff) {
                   5543:                        ext_find->flag = 0xff;
                   5544:                        memset(ext_find->reserved, 0, 5);
                   5545:                        ext_find->attribute = 8;
                   5546:                }
                   5547:                find->drive = _getdrive();
                   5548:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5549:                find->attribute = 8;
                   5550:                find->nt_res = 0;
                   5551:                msdos_find_file_conv_local_time(&fd);
                   5552:                find->create_time_ms = 0;
                   5553:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5554:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5555:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5556:                find->cluster_hi = find->cluster_lo = 0;
                   5557:                find->file_size = 0;
1.1.1.14  root     5558:                dtainfo->allowable_mask &= ~8;
1.1       root     5559:                REG8(AL) = 0x00;
                   5560:        } else {
                   5561:                REG8(AL) = 0xff;
                   5562:        }
                   5563: }
                   5564: 
                   5565: inline void msdos_int_21h_12h()
                   5566: {
1.1.1.3   root     5567:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     5568: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5569:        
                   5570:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5571:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5572:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5573:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5574:        WIN32_FIND_DATA fd;
                   5575:        
1.1.1.13  root     5576:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5577:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5578:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     5579:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5580:                              !msdos_find_file_has_8dot3name(&fd)) {
                   5581:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5582:                                        FindClose(dtainfo->find_handle);
                   5583:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5584:                                        break;
                   5585:                                }
                   5586:                        }
                   5587:                } else {
1.1.1.13  root     5588:                        FindClose(dtainfo->find_handle);
                   5589:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5590:                }
                   5591:        }
1.1.1.13  root     5592:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5593:                if(ext_fcb->flag == 0xff) {
                   5594:                        ext_find->flag = 0xff;
                   5595:                        memset(ext_find->reserved, 0, 5);
                   5596:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5597:                }
                   5598:                find->drive = _getdrive();
1.1.1.13  root     5599:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5600:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5601:                find->nt_res = 0;
                   5602:                msdos_find_file_conv_local_time(&fd);
                   5603:                find->create_time_ms = 0;
                   5604:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5605:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5606:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5607:                find->cluster_hi = find->cluster_lo = 0;
                   5608:                find->file_size = fd.nFileSizeLow;
                   5609:                REG8(AL) = 0x00;
1.1.1.14  root     5610:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5611:                if(ext_fcb->flag == 0xff) {
                   5612:                        ext_find->flag = 0xff;
                   5613:                        memset(ext_find->reserved, 0, 5);
                   5614:                        ext_find->attribute = 8;
                   5615:                }
                   5616:                find->drive = _getdrive();
                   5617:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5618:                find->attribute = 8;
                   5619:                find->nt_res = 0;
                   5620:                msdos_find_file_conv_local_time(&fd);
                   5621:                find->create_time_ms = 0;
                   5622:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5623:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5624:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5625:                find->cluster_hi = find->cluster_lo = 0;
                   5626:                find->file_size = 0;
1.1.1.14  root     5627:                dtainfo->allowable_mask &= ~8;
1.1       root     5628:                REG8(AL) = 0x00;
                   5629:        } else {
                   5630:                REG8(AL) = 0xff;
                   5631:        }
                   5632: }
                   5633: 
                   5634: inline void msdos_int_21h_13h()
                   5635: {
1.1.1.3   root     5636:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     5637:                REG8(AL) = 0xff;
                   5638:        } else {
                   5639:                REG8(AL) = 0x00;
                   5640:        }
                   5641: }
                   5642: 
1.1.1.16  root     5643: inline void msdos_int_21h_14h()
                   5644: {
                   5645:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5646:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5647:        process_t *process = msdos_process_info_get(current_psp);
                   5648:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5649:        DWORD num = 0;
                   5650:        
                   5651:        memset(mem + dta_laddr, 0, fcb->record_size);
                   5652:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5653:                REG8(AL) = 1;
                   5654:        } else {
                   5655:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5656:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5657:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5658:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5659:        }
                   5660: }
                   5661: 
                   5662: inline void msdos_int_21h_15h()
1.1.1.14  root     5663: {
                   5664:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5665:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     5666:        process_t *process = msdos_process_info_get(current_psp);
                   5667:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5668:        DWORD num = 0;
1.1.1.14  root     5669:        
1.1.1.16  root     5670:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5671:                REG8(AL) = 1;
                   5672:        } else {
                   5673:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5674:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5675:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5676:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5677:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   5678:        }
                   5679: }
                   5680: 
                   5681: inline void msdos_int_21h_16h()
                   5682: {
                   5683:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5684:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     5685:        char *path = msdos_fcb_path(fcb);
                   5686:        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     5687:        
1.1.1.14  root     5688:        if(hFile == INVALID_HANDLE_VALUE) {
                   5689:                REG8(AL) = 0xff;
                   5690:        } else {
                   5691:                REG8(AL) = 0;
                   5692:                fcb->current_block = 0;
                   5693:                fcb->record_size = 128;
                   5694:                fcb->file_size = 0;
                   5695:                fcb->handle = hFile;
                   5696:                fcb->cur_record = 0;
                   5697:        }
                   5698: }
                   5699: 
1.1.1.16  root     5700: inline void msdos_int_21h_17h()
                   5701: {
                   5702:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5703:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   5704:        char *path_src = msdos_fcb_path(fcb_src);
                   5705:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   5706:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   5707:        char *path_dst = msdos_fcb_path(fcb_dst);
                   5708:        
                   5709:        if(rename(path_src, path_dst)) {
                   5710:                REG8(AL) = 0xff;
                   5711:        } else {
                   5712:                REG8(AL) = 0;
                   5713:        }
                   5714: }
                   5715: 
1.1       root     5716: inline void msdos_int_21h_18h()
                   5717: {
                   5718:        REG8(AL) = 0x00;
                   5719: }
                   5720: 
                   5721: inline void msdos_int_21h_19h()
                   5722: {
                   5723:        REG8(AL) = _getdrive() - 1;
                   5724: }
                   5725: 
                   5726: inline void msdos_int_21h_1ah()
                   5727: {
                   5728:        process_t *process = msdos_process_info_get(current_psp);
                   5729:        
                   5730:        process->dta.w.l = REG16(DX);
1.1.1.3   root     5731:        process->dta.w.h = SREG(DS);
1.1.1.23  root     5732:        msdos_sda_update(current_psp);
1.1       root     5733: }
                   5734: 
                   5735: inline void msdos_int_21h_1bh()
                   5736: {
                   5737:        int drive_num = _getdrive() - 1;
                   5738:        UINT16 seg, ofs;
                   5739:        
                   5740:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5741:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5742:                REG8(AL) = dpb->highest_sector_num + 1;
                   5743:                REG16(CX) = dpb->bytes_per_sector;
                   5744:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     5745:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     5746:        } else {
                   5747:                REG8(AL) = 0xff;
1.1.1.3   root     5748:                m_CF = 1;
1.1       root     5749:        }
                   5750: 
                   5751: }
                   5752: 
                   5753: inline void msdos_int_21h_1ch()
                   5754: {
                   5755:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   5756:        UINT16 seg, ofs;
                   5757:        
                   5758:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5759:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5760:                REG8(AL) = dpb->highest_sector_num + 1;
                   5761:                REG16(CX) = dpb->bytes_per_sector;
                   5762:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     5763:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     5764:        } else {
                   5765:                REG8(AL) = 0xff;
1.1.1.3   root     5766:                m_CF = 1;
1.1       root     5767:        }
                   5768: 
                   5769: }
                   5770: 
                   5771: inline void msdos_int_21h_1dh()
                   5772: {
                   5773:        REG8(AL) = 0;
                   5774: }
                   5775: 
                   5776: inline void msdos_int_21h_1eh()
                   5777: {
                   5778:        REG8(AL) = 0;
                   5779: }
                   5780: 
                   5781: inline void msdos_int_21h_1fh()
                   5782: {
                   5783:        int drive_num = _getdrive() - 1;
                   5784:        UINT16 seg, ofs;
                   5785:        
                   5786:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5787:                REG8(AL) = 0;
1.1.1.3   root     5788:                SREG(DS) = seg;
                   5789:                i386_load_segment_descriptor(DS);
1.1       root     5790:                REG16(BX) = ofs;
                   5791:        } else {
                   5792:                REG8(AL) = 0xff;
1.1.1.3   root     5793:                m_CF = 1;
1.1       root     5794:        }
                   5795: }
                   5796: 
                   5797: inline void msdos_int_21h_20h()
                   5798: {
                   5799:        REG8(AL) = 0;
                   5800: }
                   5801: 
1.1.1.14  root     5802: inline void msdos_int_21h_21h()
                   5803: {
                   5804:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5805:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5806:        
                   5807:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5808:                REG8(AL) = 1;
                   5809:        } else {
                   5810:                process_t *process = msdos_process_info_get(current_psp);
                   5811:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5812:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     5813:                DWORD num = 0;
1.1.1.14  root     5814:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5815:                        REG8(AL) = 1;
                   5816:                } else {
                   5817:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5818:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     5819:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     5820:                }
                   5821:        }
                   5822: }
                   5823: 
                   5824: inline void msdos_int_21h_22h()
                   5825: {
                   5826:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5827:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5828:        
                   5829:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5830:                REG8(AL) = 0xff;
                   5831:        } else {
                   5832:                process_t *process = msdos_process_info_get(current_psp);
                   5833:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     5834:                DWORD num = 0;
1.1.1.14  root     5835:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   5836:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5837:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5838:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     5839:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     5840:        }
                   5841: }
                   5842: 
1.1.1.16  root     5843: inline void msdos_int_21h_23h()
                   5844: {
                   5845:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5846:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5847:        char *path = msdos_fcb_path(fcb);
                   5848:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   5849:        
                   5850:        if(hFile == INVALID_HANDLE_VALUE) {
                   5851:                REG8(AL) = 0xff;
                   5852:        } else {
                   5853:                UINT32 size = GetFileSize(hFile, NULL);
                   5854:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   5855:                REG8(AL) = 0;
                   5856:        }
                   5857: }
                   5858: 
                   5859: inline void msdos_int_21h_24h()
                   5860: {
                   5861:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5862:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5863:        
                   5864:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   5865: }
                   5866: 
1.1       root     5867: inline void msdos_int_21h_25h()
                   5868: {
                   5869:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     5870:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     5871: }
                   5872: 
                   5873: inline void msdos_int_21h_26h()
                   5874: {
                   5875:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   5876:        
                   5877:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   5878:        psp->first_mcb = REG16(DX) + 16;
                   5879:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5880:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5881:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5882:        psp->parent_psp = 0;
                   5883: }
                   5884: 
1.1.1.16  root     5885: inline void msdos_int_21h_27h()
                   5886: {
                   5887:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5888:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5889:        
                   5890:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5891:                REG8(AL) = 1;
                   5892:        } else {
                   5893:                process_t *process = msdos_process_info_get(current_psp);
                   5894:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5895:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   5896:                DWORD num = 0;
                   5897:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   5898:                        REG8(AL) = 1;
                   5899:                } else {
                   5900:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5901:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   5902:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5903:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   5904:                }
                   5905:        }
                   5906: }
                   5907: 
                   5908: inline void msdos_int_21h_28h()
                   5909: {
                   5910:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5911:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5912:        
                   5913:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5914:                REG8(AL) = 0xff;
                   5915:        } else {
                   5916:                process_t *process = msdos_process_info_get(current_psp);
                   5917:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5918:                DWORD num = 0;
                   5919:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   5920:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5921:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5922:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   5923:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   5924:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   5925:        }
                   5926: }
                   5927: 
1.1       root     5928: inline void msdos_int_21h_29h()
                   5929: {
1.1.1.20  root     5930:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   5931:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     5932:        UINT8 drv = 0;
                   5933:        char sep_chars[] = ":.;,=+";
                   5934:        char end_chars[] = "\\<>|/\"[]";
                   5935:        char spc_chars[] = " \t";
                   5936:        
1.1.1.20  root     5937:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   5938:        buffer[1023] = 0;
                   5939:        memset(name, 0x20, sizeof(name));
                   5940:        memset(ext, 0x20, sizeof(ext));
                   5941:        
1.1       root     5942:        if(REG8(AL) & 1) {
1.1.1.20  root     5943:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   5944:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     5945:                        ofs++;
                   5946:                }
                   5947:        }
1.1.1.20  root     5948:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     5949:        
1.1.1.24  root     5950:        if(buffer[ofs + 1] == ':') {
                   5951:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   5952:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     5953:                        ofs += 2;
1.1.1.24  root     5954:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   5955:                                ofs++;
                   5956:                        }
                   5957:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   5958:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     5959:                        ofs += 2;
1.1.1.24  root     5960:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   5961:                                ofs++;
                   5962:                        }
1.1       root     5963:                }
                   5964:        }
1.1.1.20  root     5965:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   5966:                UINT8 c = buffer[ofs];
                   5967:                if(is_kanji) {
                   5968:                        is_kanji = 0;
                   5969:                } else if(msdos_lead_byte_check(c)) {
                   5970:                        is_kanji = 1;
                   5971:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     5972:                        break;
                   5973:                } else if(c >= 'a' && c <= 'z') {
                   5974:                        c -= 0x20;
                   5975:                }
                   5976:                ofs++;
                   5977:                name[i] = c;
                   5978:        }
1.1.1.20  root     5979:        if(buffer[ofs] == '.') {
1.1       root     5980:                ofs++;
1.1.1.20  root     5981:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   5982:                        UINT8 c = buffer[ofs];
                   5983:                        if(is_kanji) {
                   5984:                                is_kanji = 0;
                   5985:                        } else if(msdos_lead_byte_check(c)) {
                   5986:                                is_kanji = 1;
                   5987:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     5988:                                break;
                   5989:                        } else if(c >= 'a' && c <= 'z') {
                   5990:                                c -= 0x20;
                   5991:                        }
                   5992:                        ofs++;
                   5993:                        ext[i] = c;
                   5994:                }
                   5995:        }
1.1.1.20  root     5996:        int si = REG16(SI) + ofs;
1.1.1.3   root     5997:        int ds = SREG(DS);
1.1       root     5998:        while(si > 0xffff) {
                   5999:                si -= 0x10;
                   6000:                ds++;
                   6001:        }
                   6002:        REG16(SI) = si;
1.1.1.3   root     6003:        SREG(DS) = ds;
                   6004:        i386_load_segment_descriptor(DS);
1.1       root     6005:        
1.1.1.3   root     6006:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     6007:        if(!(REG8(AL) & 2) || drv != 0) {
                   6008:                fcb[0] = drv;
                   6009:        }
                   6010:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   6011:                memcpy(fcb + 1, name, 8);
                   6012:        }
                   6013:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   6014:                memcpy(fcb + 9, ext, 3);
                   6015:        }
                   6016:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     6017:                if(fcb[i] == '*') {
                   6018:                        found_star = 1;
                   6019:                }
                   6020:                if(found_star) {
                   6021:                        fcb[i] = '?';
                   6022:                }
                   6023:        }
1.1.1.20  root     6024:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     6025:                if(fcb[i] == '*') {
                   6026:                        found_star = 1;
                   6027:                }
                   6028:                if(found_star) {
                   6029:                        fcb[i] = '?';
                   6030:                }
                   6031:        }
                   6032:        
                   6033:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   6034:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   6035:                        REG8(AL) = 0x01;
1.1.1.20  root     6036:                } else {
                   6037:                        REG8(AL) = 0x00;
1.1       root     6038:                }
                   6039:        } else {
                   6040:                REG8(AL) = 0xff;
                   6041:        }
                   6042: }
                   6043: 
                   6044: inline void msdos_int_21h_2ah()
                   6045: {
                   6046:        SYSTEMTIME sTime;
                   6047:        
                   6048:        GetLocalTime(&sTime);
                   6049:        REG16(CX) = sTime.wYear;
                   6050:        REG8(DH) = (UINT8)sTime.wMonth;
                   6051:        REG8(DL) = (UINT8)sTime.wDay;
                   6052:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   6053: }
                   6054: 
                   6055: inline void msdos_int_21h_2bh()
                   6056: {
1.1.1.14  root     6057:        REG8(AL) = 0xff;
1.1       root     6058: }
                   6059: 
                   6060: inline void msdos_int_21h_2ch()
                   6061: {
                   6062:        SYSTEMTIME sTime;
                   6063:        
                   6064:        GetLocalTime(&sTime);
                   6065:        REG8(CH) = (UINT8)sTime.wHour;
                   6066:        REG8(CL) = (UINT8)sTime.wMinute;
                   6067:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     6068:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     6069: }
                   6070: 
                   6071: inline void msdos_int_21h_2dh()
                   6072: {
                   6073:        REG8(AL) = 0x00;
                   6074: }
                   6075: 
                   6076: inline void msdos_int_21h_2eh()
                   6077: {
                   6078:        process_t *process = msdos_process_info_get(current_psp);
                   6079:        
                   6080:        process->verify = REG8(AL);
                   6081: }
                   6082: 
                   6083: inline void msdos_int_21h_2fh()
                   6084: {
                   6085:        process_t *process = msdos_process_info_get(current_psp);
                   6086:        
                   6087:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     6088:        SREG(ES) = process->dta.w.h;
                   6089:        i386_load_segment_descriptor(ES);
1.1       root     6090: }
                   6091: 
                   6092: inline void msdos_int_21h_30h()
                   6093: {
                   6094:        // Version Flag / OEM
1.1.1.27  root     6095:        if(REG8(AL) == 0x01) {
                   6096:                REG16(BX) = 0x1000;     // DOS is in HMA
1.1       root     6097:        } else {
1.1.1.27  root     6098:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   6099:                // but this is not correct on Windows 98 SE
                   6100: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   6101:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     6102:        }
1.1.1.27  root     6103:        REG16(CX) = 0x0000;
1.1.1.9   root     6104:        REG8(AL) = major_version;       // 7
                   6105:        REG8(AH) = minor_version;       // 10
1.1       root     6106: }
                   6107: 
                   6108: inline void msdos_int_21h_31h()
                   6109: {
1.1.1.14  root     6110:        msdos_mem_realloc(current_psp, REG16(DX), NULL);
1.1       root     6111:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   6112: }
                   6113: 
                   6114: inline void msdos_int_21h_32h()
                   6115: {
                   6116:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   6117:        UINT16 seg, ofs;
                   6118:        
                   6119:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   6120:                REG8(AL) = 0;
1.1.1.3   root     6121:                SREG(DS) = seg;
                   6122:                i386_load_segment_descriptor(DS);
1.1       root     6123:                REG16(BX) = ofs;
                   6124:        } else {
                   6125:                REG8(AL) = 0xff;
1.1.1.3   root     6126:                m_CF = 1;
1.1       root     6127:        }
                   6128: }
                   6129: 
                   6130: inline void msdos_int_21h_33h()
                   6131: {
                   6132:        char path[MAX_PATH];
                   6133:        
                   6134:        switch(REG8(AL)) {
                   6135:        case 0x00:
1.1.1.26  root     6136:                REG8(DL) = ctrl_c_checking;
1.1       root     6137:                break;
                   6138:        case 0x01:
1.1.1.26  root     6139:                ctrl_c_checking = REG8(DL);
1.1       root     6140:                break;
                   6141:        case 0x05:
                   6142:                GetSystemDirectory(path, MAX_PATH);
                   6143:                if(path[0] >= 'a' && path[0] <= 'z') {
                   6144:                        REG8(DL) = path[0] - 'a' + 1;
                   6145:                } else {
                   6146:                        REG8(DL) = path[0] - 'A' + 1;
                   6147:                }
                   6148:                break;
                   6149:        case 0x06:
1.1.1.2   root     6150:                // MS-DOS version (7.10)
1.1       root     6151:                REG8(BL) = 7;
1.1.1.2   root     6152:                REG8(BH) = 10;
1.1       root     6153:                REG8(DL) = 0;
                   6154:                REG8(DH) = 0x10; // in HMA
                   6155:                break;
1.1.1.6   root     6156:        case 0x07:
                   6157:                if(REG8(DL) == 0) {
                   6158:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   6159:                } else if(REG8(DL) == 1) {
                   6160:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   6161:                }
                   6162:                break;
1.1       root     6163:        default:
1.1.1.22  root     6164:                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     6165:                REG16(AX) = 0x01;
1.1.1.3   root     6166:                m_CF = 1;
1.1       root     6167:                break;
                   6168:        }
                   6169: }
                   6170: 
1.1.1.23  root     6171: inline void msdos_int_21h_34h()
                   6172: {
                   6173:        SREG(ES) = SDA_TOP >> 4;
                   6174:        i386_load_segment_descriptor(ES);
                   6175:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   6176: }
                   6177: 
1.1       root     6178: inline void msdos_int_21h_35h()
                   6179: {
                   6180:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     6181:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   6182:        i386_load_segment_descriptor(ES);
1.1       root     6183: }
                   6184: 
                   6185: inline void msdos_int_21h_36h()
                   6186: {
                   6187:        struct _diskfree_t df = {0};
                   6188:        
                   6189:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   6190:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   6191:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     6192:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   6193:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     6194:        } else {
                   6195:                REG16(AX) = 0xffff;
                   6196:        }
                   6197: }
                   6198: 
                   6199: inline void msdos_int_21h_37h()
                   6200: {
1.1.1.22  root     6201:        static UINT8 dev_flag = 0xff;
1.1       root     6202:        
                   6203:        switch(REG8(AL)) {
                   6204:        case 0x00:
1.1.1.22  root     6205:                {
                   6206:                        process_t *process = msdos_process_info_get(current_psp);
                   6207:                        REG8(AL) = 0x00;
                   6208:                        REG8(DL) = process->switchar;
                   6209:                }
1.1       root     6210:                break;
                   6211:        case 0x01:
1.1.1.22  root     6212:                {
                   6213:                        process_t *process = msdos_process_info_get(current_psp);
                   6214:                        REG8(AL) = 0x00;
                   6215:                        process->switchar = REG8(DL);
1.1.1.23  root     6216:                        msdos_sda_update(current_psp);
1.1.1.22  root     6217:                }
                   6218:                break;
                   6219:        case 0x02:
                   6220:                REG8(DL) = dev_flag;
                   6221:                break;
                   6222:        case 0x03:
                   6223:                dev_flag = REG8(DL);
                   6224:                break;
                   6225:        case 0xd0:
                   6226:        case 0xd1:
                   6227:        case 0xd2:
                   6228:        case 0xd3:
                   6229:        case 0xd4:
                   6230:        case 0xd5:
                   6231:        case 0xd6:
                   6232:        case 0xd7:
                   6233:        case 0xdc:
                   6234:        case 0xdd:
                   6235:        case 0xde:
                   6236:        case 0xdf:
                   6237:                // diet ???
                   6238:                REG16(AX) = 1;
1.1       root     6239:                break;
                   6240:        default:
1.1.1.22  root     6241:                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     6242:                REG16(AX) = 1;
                   6243:                break;
                   6244:        }
                   6245: }
                   6246: 
1.1.1.19  root     6247: int get_country_info(country_info_t *ci)
1.1.1.17  root     6248: {
                   6249:        char LCdata[80];
                   6250:        
1.1.1.19  root     6251:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     6252:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   6253:        ci->currency_dec_digits = atoi(LCdata);
                   6254:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   6255:        ci->currency_format = *LCdata - '0';
                   6256:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   6257:        ci->date_format = *LCdata - '0';
                   6258:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   6259:        memcpy(&ci->currency_symbol, LCdata, 4);
                   6260:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   6261:        *ci->date_sep = *LCdata;
                   6262:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   6263:        *ci->dec_sep = *LCdata;
                   6264:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   6265:        *ci->list_sep = *LCdata;
                   6266:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   6267:        *ci->thou_sep = *LCdata;
                   6268:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   6269:        *ci->time_sep = *LCdata;
                   6270:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   6271:        if(strchr(LCdata, 'H') != NULL) {
                   6272:                ci->time_format = 1;
                   6273:        }
1.1.1.27  root     6274:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     6275:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     6276:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   6277:        return atoi(LCdata);
                   6278: }
                   6279: 
1.1.1.14  root     6280: inline void msdos_int_21h_38h()
                   6281: {
                   6282:        switch(REG8(AL)) {
                   6283:        case 0x00:
1.1.1.19  root     6284:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     6285:                break;
                   6286:        default:
1.1.1.22  root     6287:                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     6288:                REG16(AX) = 2;
                   6289:                m_CF = 1;
                   6290:                break;
                   6291:        }
                   6292: }
                   6293: 
1.1       root     6294: inline void msdos_int_21h_39h(int lfn)
                   6295: {
1.1.1.3   root     6296:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6297:                REG16(AX) = errno;
1.1.1.3   root     6298:                m_CF = 1;
1.1       root     6299:        }
                   6300: }
                   6301: 
                   6302: inline void msdos_int_21h_3ah(int lfn)
                   6303: {
1.1.1.3   root     6304:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6305:                REG16(AX) = errno;
1.1.1.3   root     6306:                m_CF = 1;
1.1       root     6307:        }
                   6308: }
                   6309: 
                   6310: inline void msdos_int_21h_3bh(int lfn)
                   6311: {
1.1.1.3   root     6312:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     6313:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     6314:                m_CF = 1;
1.1       root     6315:        }
                   6316: }
                   6317: 
                   6318: inline void msdos_int_21h_3ch()
                   6319: {
1.1.1.3   root     6320:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6321:        int attr = GetFileAttributes(path);
                   6322:        int fd = -1;
1.1.1.11  root     6323:        UINT16 info;
1.1       root     6324:        
1.1.1.11  root     6325:        if(msdos_is_con_path(path)) {
                   6326:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   6327:                info = 0x80d3;
1.1.1.14  root     6328:        } else if(msdos_is_nul_path(path)) {
                   6329:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6330:                info = 0x80d3;
1.1.1.24  root     6331:        } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     6332:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   6333:                info = 0x80d3;
1.1       root     6334:        } else {
                   6335:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     6336:                info = msdos_drive_number(path);
1.1       root     6337:        }
                   6338:        if(fd != -1) {
                   6339:                if(attr == -1) {
                   6340:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   6341:                }
                   6342:                SetFileAttributes(path, attr);
                   6343:                REG16(AX) = fd;
1.1.1.11  root     6344:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     6345:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6346:        } else {
                   6347:                REG16(AX) = errno;
1.1.1.3   root     6348:                m_CF = 1;
1.1       root     6349:        }
                   6350: }
                   6351: 
                   6352: inline void msdos_int_21h_3dh()
                   6353: {
1.1.1.3   root     6354:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6355:        int mode = REG8(AL) & 0x03;
1.1.1.11  root     6356:        int fd = -1;
                   6357:        UINT16 info;
1.1       root     6358:        
                   6359:        if(mode < 0x03) {
1.1.1.11  root     6360:                if(msdos_is_con_path(path)) {
1.1.1.13  root     6361:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     6362:                        info = 0x80d3;
1.1.1.14  root     6363:                } else if(msdos_is_nul_path(path)) {
                   6364:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   6365:                        info = 0x80d3;
1.1.1.24  root     6366:                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     6367:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   6368:                        info = 0x80d3;
1.1.1.11  root     6369:                } else {
1.1.1.13  root     6370:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     6371:                        info = msdos_drive_number(path);
                   6372:                }
1.1       root     6373:                if(fd != -1) {
                   6374:                        REG16(AX) = fd;
1.1.1.11  root     6375:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     6376:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     6377:                } else {
                   6378:                        REG16(AX) = errno;
1.1.1.3   root     6379:                        m_CF = 1;
1.1       root     6380:                }
                   6381:        } else {
                   6382:                REG16(AX) = 0x0c;
1.1.1.3   root     6383:                m_CF = 1;
1.1       root     6384:        }
                   6385: }
                   6386: 
                   6387: inline void msdos_int_21h_3eh()
                   6388: {
                   6389:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6390:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6391:        
1.1.1.20  root     6392:        if(fd < process->max_files && file_handler[fd].valid) {
                   6393:                _close(fd);
                   6394:                msdos_file_handler_close(fd);
                   6395:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     6396:        } else {
                   6397:                REG16(AX) = 0x06;
1.1.1.3   root     6398:                m_CF = 1;
1.1       root     6399:        }
                   6400: }
                   6401: 
                   6402: inline void msdos_int_21h_3fh()
                   6403: {
                   6404:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6405:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6406:        
1.1.1.20  root     6407:        if(fd < process->max_files && file_handler[fd].valid) {
                   6408:                if(file_mode[file_handler[fd].mode].in) {
                   6409:                        if(file_handler[fd].atty) {
1.1       root     6410:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     6411:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     6412:                                int max = REG16(CX);
                   6413:                                int p = 0;
                   6414:                                
                   6415:                                while(max > p) {
                   6416:                                        int chr = msdos_getch();
                   6417:                                        
1.1.1.26  root     6418:                                        if(ctrl_c_pressed) {
                   6419:                                                p = 0;
                   6420:                                                buf[p++] = 0x0d;
                   6421:                                                if(max > p) {
                   6422:                                                        buf[p++] = 0x0a;
                   6423:                                                }
                   6424:                                                msdos_putch(chr);
                   6425:                                                msdos_putch('\n');
                   6426:                                                break;
                   6427:                                        } else if(chr == 0x00) {
1.1       root     6428:                                                // skip 2nd byte
                   6429:                                                msdos_getch();
                   6430:                                        } else if(chr == 0x0d) {
                   6431:                                                // carriage return
                   6432:                                                buf[p++] = 0x0d;
                   6433:                                                if(max > p) {
                   6434:                                                        buf[p++] = 0x0a;
                   6435:                                                }
1.1.1.14  root     6436:                                                msdos_putch('\n');
1.1       root     6437:                                                break;
                   6438:                                        } else if(chr == 0x08) {
                   6439:                                                // back space
                   6440:                                                if(p > 0) {
                   6441:                                                        p--;
1.1.1.20  root     6442:                                                        if(msdos_ctrl_code_check(buf[p])) {
                   6443:                                                                msdos_putch(chr);
                   6444:                                                                msdos_putch(chr);
                   6445:                                                                msdos_putch(' ');
                   6446:                                                                msdos_putch(' ');
                   6447:                                                                msdos_putch(chr);
                   6448:                                                                msdos_putch(chr);
                   6449:                                                        } else {
                   6450:                                                                msdos_putch(chr);
                   6451:                                                                msdos_putch(' ');
                   6452:                                                                msdos_putch(chr);
                   6453:                                                        }
1.1       root     6454:                                                }
                   6455:                                        } else {
                   6456:                                                buf[p++] = chr;
                   6457:                                                msdos_putch(chr);
                   6458:                                        }
                   6459:                                }
                   6460:                                REG16(AX) = p;
1.1.1.26  root     6461:                                
1.1.1.8   root     6462:                                // some seconds may be passed in console
1.1       root     6463:                                hardware_update();
                   6464:                        } else {
1.1.1.20  root     6465:                                REG16(AX) = _read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6466:                        }
                   6467:                } else {
                   6468:                        REG16(AX) = 0x05;
1.1.1.3   root     6469:                        m_CF = 1;
1.1       root     6470:                }
                   6471:        } else {
                   6472:                REG16(AX) = 0x06;
1.1.1.3   root     6473:                m_CF = 1;
1.1       root     6474:        }
                   6475: }
                   6476: 
                   6477: inline void msdos_int_21h_40h()
                   6478: {
                   6479:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6480:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6481:        
1.1.1.20  root     6482:        if(fd < process->max_files && file_handler[fd].valid) {
                   6483:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     6484:                        if(REG16(CX)) {
1.1.1.20  root     6485:                                if(file_handler[fd].atty) {
1.1       root     6486:                                        // BX is stdout/stderr or is redirected to stdout
                   6487:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     6488:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     6489:                                        }
                   6490:                                        REG16(AX) = REG16(CX);
                   6491:                                } else {
1.1.1.20  root     6492:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6493:                                }
                   6494:                        } else {
1.1.1.20  root     6495:                                UINT32 pos = _tell(fd);
                   6496:                                _lseek(fd, 0, SEEK_END);
                   6497:                                UINT32 size = _tell(fd);
1.1.1.12  root     6498:                                if(pos < size) {
1.1.1.20  root     6499:                                        _lseek(fd, pos, SEEK_SET);
                   6500:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     6501:                                } else {
                   6502:                                        for(UINT32 i = size; i < pos; i++) {
                   6503:                                                UINT8 tmp = 0;
1.1.1.23  root     6504:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     6505:                                        }
1.1.1.20  root     6506:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     6507:                                }
1.1.1.23  root     6508:                                REG16(AX) = 0;
1.1       root     6509:                        }
                   6510:                } else {
                   6511:                        REG16(AX) = 0x05;
1.1.1.3   root     6512:                        m_CF = 1;
1.1       root     6513:                }
                   6514:        } else {
                   6515:                REG16(AX) = 0x06;
1.1.1.3   root     6516:                m_CF = 1;
1.1       root     6517:        }
                   6518: }
                   6519: 
                   6520: inline void msdos_int_21h_41h(int lfn)
                   6521: {
1.1.1.3   root     6522:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6523:                REG16(AX) = errno;
1.1.1.3   root     6524:                m_CF = 1;
1.1       root     6525:        }
                   6526: }
                   6527: 
                   6528: inline void msdos_int_21h_42h()
                   6529: {
                   6530:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6531:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6532:        
1.1.1.20  root     6533:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     6534:                if(REG8(AL) < 0x03) {
                   6535:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     6536:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   6537:                        UINT32 pos = _tell(fd);
1.1       root     6538:                        REG16(AX) = pos & 0xffff;
                   6539:                        REG16(DX) = (pos >> 16);
                   6540:                } else {
                   6541:                        REG16(AX) = 0x01;
1.1.1.3   root     6542:                        m_CF = 1;
1.1       root     6543:                }
                   6544:        } else {
                   6545:                REG16(AX) = 0x06;
1.1.1.3   root     6546:                m_CF = 1;
1.1       root     6547:        }
                   6548: }
                   6549: 
                   6550: inline void msdos_int_21h_43h(int lfn)
                   6551: {
1.1.1.3   root     6552:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     6553:        int attr;
                   6554:        
1.1.1.14  root     6555:        if(!lfn && REG8(AL) > 2) {
                   6556:                REG16(AX) = 0x01;
                   6557:                m_CF = 1;
                   6558:                return;
                   6559:        }
                   6560:        switch(REG8(lfn ? BL : AL)) {
1.1       root     6561:        case 0x00:
                   6562:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     6563:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   6564:                } else {
                   6565:                        REG16(AX) = (UINT16)GetLastError();
                   6566:                        m_CF = 1;
                   6567:                }
                   6568:                break;
                   6569:        case 0x01:
                   6570:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   6571:                        REG16(AX) = (UINT16)GetLastError();
                   6572:                        m_CF = 1;
                   6573:                }
                   6574:                break;
                   6575:        case 0x02:
                   6576:                {
                   6577:                        DWORD size = GetCompressedFileSize(path, NULL);
                   6578:                        if(size != INVALID_FILE_SIZE) {
                   6579:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   6580:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   6581:                                        // this isn't correct if the file is in the NTFS MFT
                   6582:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   6583:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   6584:                                        }
                   6585:                                }
                   6586:                                REG16(AX) = LOWORD(size);
                   6587:                                REG16(DX) = HIWORD(size);
                   6588:                        } else {
                   6589:                                REG16(AX) = (UINT16)GetLastError();
                   6590:                                m_CF = 1;
1.1       root     6591:                        }
1.1.1.14  root     6592:                }
                   6593:                break;
                   6594:        case 0x03:
                   6595:        case 0x05:
                   6596:        case 0x07:
                   6597:                {
                   6598:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6599:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6600:                                FILETIME local, time;
                   6601:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   6602:                                if(REG8(BL) == 7) {
                   6603:                                        ULARGE_INTEGER hund;
                   6604:                                        hund.LowPart = local.dwLowDateTime;
                   6605:                                        hund.HighPart = local.dwHighDateTime;
                   6606:                                        hund.QuadPart += REG16(SI) * 100000;
                   6607:                                        local.dwLowDateTime = hund.LowPart;
                   6608:                                        local.dwHighDateTime = hund.HighPart;
                   6609:                                }
                   6610:                                LocalFileTimeToFileTime(&local, &time);
                   6611:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   6612:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   6613:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   6614:                                        REG16(AX) = (UINT16)GetLastError();
                   6615:                                        m_CF = 1;
                   6616:                                }
                   6617:                                CloseHandle(hFile);
                   6618:                        } else {
                   6619:                                REG16(AX) = (UINT16)GetLastError();
                   6620:                                m_CF = 1;
1.1       root     6621:                        }
1.1.1.14  root     6622:                }
                   6623:                break;
                   6624:        case 0x04:
                   6625:        case 0x06:
                   6626:        case 0x08:
                   6627:                {
                   6628:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   6629:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   6630:                                FILETIME *time, local;
                   6631:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   6632:                                                   0x06 ? &fad.ftLastAccessTime :
                   6633:                                                          &fad.ftCreationTime;
                   6634:                                FileTimeToLocalFileTime(time, &local);
                   6635:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   6636:                                if(REG8(BL) == 0x08) {
                   6637:                                        ULARGE_INTEGER hund;
                   6638:                                        hund.LowPart = local.dwLowDateTime;
                   6639:                                        hund.HighPart = local.dwHighDateTime;
                   6640:                                        hund.QuadPart /= 100000;
                   6641:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   6642:                                }
                   6643:                        } else {
                   6644:                                REG16(AX) = (UINT16)GetLastError();
                   6645:                                m_CF = 1;
1.1       root     6646:                        }
1.1.1.14  root     6647:                }
                   6648:                break;
                   6649:        default:
1.1.1.22  root     6650:                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     6651:                REG16(AX) = 0x01;
                   6652:                m_CF = 1;
                   6653:                break;
                   6654:        }
                   6655: }
                   6656: 
                   6657: inline void msdos_int_21h_44h()
                   6658: {
1.1.1.22  root     6659:        static UINT16 iteration_count = 0;
                   6660:        
1.1.1.20  root     6661:        process_t *process = msdos_process_info_get(current_psp);
                   6662:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   6663:        
1.1.1.14  root     6664:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   6665:        
                   6666:        switch(REG8(AL)) {
                   6667:        case 0x00:
                   6668:        case 0x01:
                   6669:        case 0x02:
                   6670:        case 0x03:
                   6671:        case 0x04:
                   6672:        case 0x05:
                   6673:        case 0x06:
                   6674:        case 0x07:
1.1.1.20  root     6675:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   6676:                        REG16(AX) = 0x06;
                   6677:                        m_CF = 1;
                   6678:                        return;
1.1.1.14  root     6679:                }
                   6680:                break;
                   6681:        case 0x08:
                   6682:        case 0x09:
                   6683:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   6684:                        // invalid drive number
                   6685:                        REG16(AX) = 0x0f;
                   6686:                        m_CF = 1;
                   6687:                        return;
                   6688:                } else {
                   6689:                        if(REG8(BL) == 0) {
                   6690:                                val = GetDriveType(NULL);
                   6691:                        } else {
                   6692:                                char tmp[8];
                   6693:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   6694:                                val = GetDriveType(tmp);
                   6695:                        }
                   6696:                        if(val == DRIVE_NO_ROOT_DIR) {
                   6697:                                // no drive
                   6698:                                REG16(AX) = 0x0f;
                   6699:                                m_CF = 1;
                   6700:                                return;
1.1       root     6701:                        }
                   6702:                }
                   6703:                break;
                   6704:        }
                   6705:        switch(REG8(AL)) {
                   6706:        case 0x00: // get ioctrl data
1.1.1.20  root     6707:                REG16(DX) = file_handler[fd].info;
1.1       root     6708:                break;
                   6709:        case 0x01: // set ioctrl data
1.1.1.20  root     6710:                file_handler[fd].info |= REG8(DL);
1.1       root     6711:                break;
                   6712:        case 0x02: // recv from character device
                   6713:        case 0x03: // send to character device
                   6714:        case 0x04: // recv from block device
                   6715:        case 0x05: // send to block device
                   6716:                REG16(AX) = 0x05;
1.1.1.3   root     6717:                m_CF = 1;
1.1       root     6718:                break;
                   6719:        case 0x06: // get read status
1.1.1.20  root     6720:                if(file_mode[file_handler[fd].mode].in) {
                   6721:                        if(file_handler[fd].atty) {
1.1.1.14  root     6722:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     6723:                        } else {
1.1.1.20  root     6724:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     6725:                        }
1.1.1.14  root     6726:                } else {
                   6727:                        REG8(AL) = 0x00;
1.1       root     6728:                }
                   6729:                break;
                   6730:        case 0x07: // get write status
1.1.1.20  root     6731:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     6732:                        REG8(AL) = 0xff;
                   6733:                } else {
                   6734:                        REG8(AL) = 0x00;
1.1       root     6735:                }
                   6736:                break;
                   6737:        case 0x08: // check removable drive
1.1.1.14  root     6738:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   6739:                        // removable drive
                   6740:                        REG16(AX) = 0x00;
1.1       root     6741:                } else {
1.1.1.14  root     6742:                        // fixed drive
                   6743:                        REG16(AX) = 0x01;
1.1       root     6744:                }
                   6745:                break;
                   6746:        case 0x09: // check remote drive
1.1.1.14  root     6747:                if(val == DRIVE_REMOTE) {
                   6748:                        // remote drive
                   6749:                        REG16(DX) = 0x1000;
1.1       root     6750:                } else {
1.1.1.14  root     6751:                        // local drive
                   6752:                        REG16(DX) = 0x00;
1.1       root     6753:                }
                   6754:                break;
1.1.1.21  root     6755:        case 0x0a: // check remote handle
                   6756:                REG16(DX) = 0x00; // FIXME
                   6757:                break;
1.1       root     6758:        case 0x0b: // set retry count
                   6759:                break;
1.1.1.22  root     6760:        case 0x0c: // generic character device request
                   6761:                if(REG8(CL) == 0x45) {
                   6762:                        // set iteration (retry) count
                   6763:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   6764:                } else if(REG8(CL) == 0x4a) {
                   6765:                        // select code page
                   6766:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   6767:                        msdos_nls_tables_update();
                   6768:                } else if(REG8(CL) == 0x65) {
                   6769:                        // get iteration (retry) count
                   6770:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   6771:                } else if(REG8(CL) == 0x6a) {
                   6772:                        // query selected code page
                   6773:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   6774:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   6775:                        
                   6776:                        CPINFO info;
                   6777:                        GetCPInfo(active_code_page, &info);
                   6778:                        
                   6779:                        if(info.MaxCharSize != 1) {
                   6780:                                for(int i = 0;; i++) {
                   6781:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   6782:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   6783:                                        
                   6784:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   6785:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   6786:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   6787:                                        
                   6788:                                        if(lo == 0 && hi == 0) {
                   6789:                                                break;
                   6790:                                        }
                   6791:                                }
                   6792:                        }
                   6793:                } else if(REG8(CL) == 0x7f) {
                   6794:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   6795:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   6796:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   6797:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   6798:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   6799:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   6800:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   6801:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   6802:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   6803:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   6804:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   6805:                } else {
                   6806:                        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));
                   6807:                        REG16(AX) = 0x01; // invalid function
                   6808:                        m_CF = 1;
                   6809:                }
                   6810:                break;
                   6811:        case 0x0d: // generic block device request
                   6812:                if(REG8(CL) == 0x40) {
                   6813:                        // set device parameters
                   6814:                } else if(REG8(CL) == 0x46) {
                   6815:                        // set volume serial number
                   6816:                } else if(REG8(CL) == 0x4a) {
                   6817:                        // lock logical volume
                   6818:                } else if(REG8(CL) == 0x4b) {
                   6819:                        // lock physical volume
                   6820:                } else if(REG8(CL) == 0x60) {
                   6821:                        // get device parameters
                   6822:                        char dev[] = "\\\\.\\A:";
                   6823:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   6824:                        
                   6825:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6826:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6827:                                DISK_GEOMETRY geo;
                   6828:                                DWORD dwSize;
                   6829:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6830:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   6831:                                        switch(geo.MediaType) {
                   6832:                                        case F5_360_512:
                   6833:                                        case F5_320_512:
                   6834:                                        case F5_320_1024:
                   6835:                                        case F5_180_512:
                   6836:                                        case F5_160_512:
                   6837:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   6838:                                                break;
                   6839:                                        case F5_1Pt2_512:
                   6840:                                        case F3_1Pt2_512:
                   6841:                                        case F3_1Pt23_1024:
                   6842:                                        case F5_1Pt23_1024:
                   6843:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   6844:                                                break;
                   6845:                                        case F3_720_512:
                   6846:                                        case F3_640_512:
                   6847:                                        case F5_640_512:
                   6848:                                        case F5_720_512:
                   6849:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   6850:                                                break;
                   6851:                                        case F8_256_128:
                   6852:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   6853:                                                break;
                   6854:                                        case FixedMedia:
                   6855:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   6856:                                                break;
                   6857:                                        case F3_1Pt44_512:
                   6858:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   6859:                                                break;
                   6860:                                        case F3_2Pt88_512:
                   6861:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   6862:                                                break;
                   6863:                                        default:
                   6864:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   6865: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   6866:                                                break;
                   6867:                                        }
                   6868:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   6869:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   6870:                                        switch(geo.MediaType) {
                   6871:                                        case F5_360_512:
                   6872:                                        case F5_320_512:
                   6873:                                        case F5_320_1024:
                   6874:                                        case F5_180_512:
                   6875:                                        case F5_160_512:
                   6876:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   6877:                                                break;
                   6878:                                        default:
                   6879:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   6880:                                                break;
                   6881:                                        }
                   6882:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   6883:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   6884:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   6885:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   6886:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   6887:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   6888:                                        switch(geo.MediaType) {
                   6889:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6890:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   6891:                                                break;
                   6892:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6893:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   6894:                                                break;
                   6895:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6896:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   6897:                                                break;
                   6898:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6899:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   6900:                                                break;
                   6901:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6902:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6903:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   6904:                                                break;
                   6905:                                        case FixedMedia:        // hard disk
                   6906:                                        case RemovableMedia:
                   6907:                                        case Unknown:
                   6908:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   6909:                                                break;
                   6910:                                        default:
                   6911:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   6912:                                                break;
                   6913:                                        }
                   6914:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   6915:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   6916:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   6917:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   6918:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   6919:                                        // 21h  BYTE    device type
                   6920:                                        // 22h  WORD    device attributes (removable or not, etc)
                   6921:                                } else {
                   6922:                                        REG16(AX) = 0x0f; // invalid drive
                   6923:                                        m_CF = 1;
                   6924:                                }
                   6925:                                CloseHandle(hFile);
                   6926:                        } else {
                   6927:                                REG16(AX) = 0x0f; // invalid drive
                   6928:                                m_CF = 1;
                   6929:                        }
                   6930:                } else if(REG8(CL) == 0x66) {
                   6931:                        // get volume serial number
                   6932:                        char path[] = "A:\\";
                   6933:                        char volume_label[MAX_PATH];
                   6934:                        DWORD serial_number = 0;
                   6935:                        char file_system[MAX_PATH];
                   6936:                        
                   6937:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   6938:                        
                   6939:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   6940:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   6941:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   6942:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   6943:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   6944:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   6945:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   6946:                        } else {
                   6947:                                REG16(AX) = 0x0f; // invalid drive
                   6948:                                m_CF = 1;
                   6949:                        }
                   6950:                } else if(REG8(CL) == 0x67) {
                   6951:                        // get access flag
                   6952:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   6953:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   6954:                } else if(REG8(CL) == 0x68) {
                   6955:                        // sense media type
                   6956:                        char dev[64];
                   6957:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   6958:                        
                   6959:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6960:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6961:                                DISK_GEOMETRY geo;
                   6962:                                DWORD dwSize;
                   6963:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6964:                                        switch(geo.MediaType) {
                   6965:                                        case F3_720_512:
                   6966:                                        case F5_720_512:
                   6967:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6968:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   6969:                                                break;
                   6970:                                        case F3_1Pt44_512:
                   6971:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6972:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   6973:                                                break;
                   6974:                                        case F3_2Pt88_512:
                   6975:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6976:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   6977:                                                break;
                   6978:                                        default:
                   6979:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   6980:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   6981:                                                break;
                   6982:                                        }
                   6983:                                } else {
                   6984:                                        REG16(AX) = 0x0f; // invalid drive
                   6985:                                        m_CF = 1;
                   6986:                                }
                   6987:                                CloseHandle(hFile);
                   6988:                        } else {
                   6989:                                REG16(AX) = 0x0f; // invalid drive
                   6990:                                m_CF = 1;
                   6991:                        }
                   6992:                } else if(REG8(CL) == 0x6a) {
                   6993:                        // unlock logical volume
                   6994:                } else if(REG8(CL) == 0x6b) {
                   6995:                        // unlock physical volume
                   6996:                } else {
                   6997:                        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));
                   6998:                        REG16(AX) = 0x01; // invalid function
                   6999:                        m_CF = 1;
                   7000:                }
                   7001:                break;
                   7002:        case 0x0e: // get logical drive map
                   7003:                {
                   7004:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7005:                        if(!(GetLogicalDrives() & bits)) {
                   7006:                                REG16(AX) = 0x0f; // invalid drive
                   7007:                                m_CF = 1;
                   7008:                        } else {
                   7009:                                REG8(AL) = 0;
                   7010:                        }
                   7011:                }
                   7012:                break;
                   7013:        case 0x0f: // set logical drive map
                   7014:                {
                   7015:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7016:                        if(!(GetLogicalDrives() & bits)) {
                   7017:                                REG16(AX) = 0x0f; // invalid drive
                   7018:                                m_CF = 1;
                   7019:                        }
                   7020:                }
                   7021:                break;
                   7022:        case 0x10: // query generic ioctrl capability (handle)
                   7023:                switch(REG8(CL)) {
                   7024:                case 0x45:
                   7025:                case 0x4a:
                   7026:                case 0x65:
                   7027:                case 0x6a:
                   7028:                case 0x7f:
                   7029:                        REG16(AX) = 0x0000; // supported
                   7030:                        break;
                   7031:                default:
                   7032:                        REG8(AL) = 0x01; // ioctl capability not available
                   7033:                        m_CF = 1;
                   7034:                        break;
                   7035:                }
                   7036:                break;
                   7037:        case 0x11: // query generic ioctrl capability (drive)
                   7038:                switch(REG8(CL)) {
                   7039:                case 0x40:
                   7040:                case 0x46:
                   7041:                case 0x4a:
                   7042:                case 0x4b:
                   7043:                case 0x60:
                   7044:                case 0x66:
                   7045:                case 0x67:
                   7046:                case 0x68:
                   7047:                case 0x6a:
                   7048:                case 0x6b:
                   7049:                        REG16(AX) = 0x0000; // supported
                   7050:                        break;
                   7051:                default:
                   7052:                        REG8(AL) = 0x01; // ioctl capability not available
                   7053:                        m_CF = 1;
                   7054:                        break;
                   7055:                }
                   7056:                break;
                   7057:        case 0x12: // determine dos type
                   7058:        case 0x51: // concurrent dos v3.2+ - installation check
                   7059:        case 0x52: // determine dos type/get dr dos versuin
                   7060:                REG16(AX) = 0x01; // this  is not DR-DOS
                   7061:                m_CF = 1;
                   7062:                break;
1.1       root     7063:        default:
1.1.1.22  root     7064:                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     7065:                REG16(AX) = 0x01;
1.1.1.3   root     7066:                m_CF = 1;
1.1       root     7067:                break;
                   7068:        }
                   7069: }
                   7070: 
                   7071: inline void msdos_int_21h_45h()
                   7072: {
                   7073:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7074:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7075:        
1.1.1.20  root     7076:        if(fd < process->max_files && file_handler[fd].valid) {
                   7077:                int dup_fd = _dup(fd);
                   7078:                if(dup_fd != -1) {
                   7079:                        REG16(AX) = dup_fd;
                   7080:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7081: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7082:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7083:                } else {
                   7084:                        REG16(AX) = errno;
1.1.1.3   root     7085:                        m_CF = 1;
1.1       root     7086:                }
                   7087:        } else {
                   7088:                REG16(AX) = 0x06;
1.1.1.3   root     7089:                m_CF = 1;
1.1       root     7090:        }
                   7091: }
                   7092: 
                   7093: inline void msdos_int_21h_46h()
                   7094: {
                   7095:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7096:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7097:        int dup_fd = REG16(CX);
                   7098:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     7099:        
1.1.1.20  root     7100:        if(REG16(BX) == REG16(CX)) {
                   7101:                REG16(AX) = 0x06;
                   7102:                m_CF = 1;
                   7103:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   7104:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   7105:                        _close(tmp_fd);
                   7106:                        msdos_file_handler_close(tmp_fd);
                   7107:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   7108:                }
                   7109:                if(_dup2(fd, dup_fd) != -1) {
                   7110:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   7111: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   7112:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     7113:                } else {
                   7114:                        REG16(AX) = errno;
1.1.1.3   root     7115:                        m_CF = 1;
1.1       root     7116:                }
                   7117:        } else {
                   7118:                REG16(AX) = 0x06;
1.1.1.3   root     7119:                m_CF = 1;
1.1       root     7120:        }
                   7121: }
                   7122: 
                   7123: inline void msdos_int_21h_47h(int lfn)
                   7124: {
                   7125:        char path[MAX_PATH];
                   7126:        
                   7127:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   7128:                if(path[1] == ':') {
                   7129:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     7130:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     7131:                } else {
1.1.1.3   root     7132:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     7133:                }
                   7134:        } else {
                   7135:                REG16(AX) = errno;
1.1.1.3   root     7136:                m_CF = 1;
1.1       root     7137:        }
                   7138: }
                   7139: 
                   7140: inline void msdos_int_21h_48h()
                   7141: {
1.1.1.19  root     7142:        int seg, umb_linked;
1.1       root     7143:        
1.1.1.8   root     7144:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     7145:                // unlink umb not to allocate memory in umb
                   7146:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   7147:                        msdos_mem_unlink_umb();
                   7148:                }
1.1.1.8   root     7149:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7150:                        REG16(AX) = seg;
                   7151:                } else {
                   7152:                        REG16(AX) = 0x08;
                   7153:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   7154:                        m_CF = 1;
                   7155:                }
1.1.1.19  root     7156:                if(umb_linked != 0) {
                   7157:                        msdos_mem_link_umb();
                   7158:                }
1.1.1.8   root     7159:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   7160:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7161:                        REG16(AX) = seg;
                   7162:                } else {
                   7163:                        REG16(AX) = 0x08;
                   7164:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   7165:                        m_CF = 1;
                   7166:                }
                   7167:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   7168:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   7169:                        REG16(AX) = seg;
                   7170:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   7171:                        REG16(AX) = seg;
                   7172:                } else {
                   7173:                        REG16(AX) = 0x08;
                   7174:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   7175:                        m_CF = 1;
                   7176:                }
1.1       root     7177:        }
                   7178: }
                   7179: 
                   7180: inline void msdos_int_21h_49h()
                   7181: {
1.1.1.14  root     7182:        int mcb_seg = SREG(ES) - 1;
                   7183:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   7184:        
                   7185:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7186:                msdos_mem_free(SREG(ES));
                   7187:        } else {
1.1.1.28! root     7188:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.14  root     7189:                m_CF = 1;
                   7190:        }
1.1       root     7191: }
                   7192: 
                   7193: inline void msdos_int_21h_4ah()
                   7194: {
1.1.1.14  root     7195:        int mcb_seg = SREG(ES) - 1;
                   7196:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     7197:        int max_paragraphs;
                   7198:        
1.1.1.14  root     7199:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   7200:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   7201:                        REG16(AX) = 0x08;
                   7202:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   7203:                        m_CF = 1;
                   7204:                }
                   7205:        } else {
1.1.1.28! root     7206:                REG16(AX) = 0x09; // illegal memory block address 
1.1.1.3   root     7207:                m_CF = 1;
1.1       root     7208:        }
                   7209: }
                   7210: 
                   7211: inline void msdos_int_21h_4bh()
                   7212: {
1.1.1.3   root     7213:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7214:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     7215:        
                   7216:        switch(REG8(AL)) {
                   7217:        case 0x00:
                   7218:        case 0x01:
                   7219:                if(msdos_process_exec(command, param, REG8(AL))) {
                   7220:                        REG16(AX) = 0x02;
1.1.1.3   root     7221:                        m_CF = 1;
1.1       root     7222:                }
                   7223:                break;
1.1.1.14  root     7224:        case 0x03:
                   7225:                {
                   7226:                        int fd;
                   7227:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   7228:                                REG16(AX) = 0x02;
                   7229:                                m_CF = 1;
                   7230:                                break;
                   7231:                        }
                   7232:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   7233:                        _close(fd);
                   7234:                        
                   7235:                        UINT16 *overlay = (UINT16 *)param;
                   7236:                        
                   7237:                        // check exe header
                   7238:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   7239:                        int header_size = 0;
                   7240:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   7241:                                header_size = header->header_size * 16;
                   7242:                                // relocation
                   7243:                                int start_seg = overlay[1];
                   7244:                                for(int i = 0; i < header->relocations; i++) {
                   7245:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   7246:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   7247:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   7248:                                }
                   7249:                        }
                   7250:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   7251:                }
                   7252:                break;
1.1       root     7253:        default:
1.1.1.22  root     7254:                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     7255:                REG16(AX) = 0x01;
1.1.1.3   root     7256:                m_CF = 1;
1.1       root     7257:                break;
                   7258:        }
                   7259: }
                   7260: 
                   7261: inline void msdos_int_21h_4ch()
                   7262: {
                   7263:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   7264: }
                   7265: 
                   7266: inline void msdos_int_21h_4dh()
                   7267: {
                   7268:        REG16(AX) = retval;
                   7269: }
                   7270: 
                   7271: inline void msdos_int_21h_4eh()
                   7272: {
                   7273:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7274:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7275:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     7276:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7277:        WIN32_FIND_DATA fd;
                   7278:        
1.1.1.14  root     7279:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   7280:        find->find_magic = FIND_MAGIC;
                   7281:        find->dta_index = dtainfo - dtalist;
1.1       root     7282:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     7283:        dtainfo->allowable_mask = REG8(CL);
                   7284:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     7285:        
1.1.1.14  root     7286:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   7287:                dtainfo->allowable_mask &= ~8;
1.1       root     7288:        }
1.1.1.14  root     7289:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   7290:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7291:                      !msdos_find_file_has_8dot3name(&fd)) {
                   7292:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7293:                                FindClose(dtainfo->find_handle);
                   7294:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7295:                                break;
                   7296:                        }
                   7297:                }
                   7298:        }
1.1.1.13  root     7299:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7300:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7301:                msdos_find_file_conv_local_time(&fd);
                   7302:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7303:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7304:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7305:                REG16(AX) = 0;
1.1.1.14  root     7306:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7307:                find->attrib = 8;
                   7308:                find->size = 0;
                   7309:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7310:                dtainfo->allowable_mask &= ~8;
1.1       root     7311:                REG16(AX) = 0;
                   7312:        } else {
                   7313:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     7314:                m_CF = 1;
1.1       root     7315:        }
                   7316: }
                   7317: 
                   7318: inline void msdos_int_21h_4fh()
                   7319: {
                   7320:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7321:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7322:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     7323:        WIN32_FIND_DATA fd;
                   7324:        
1.1.1.14  root     7325:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   7326:                REG16(AX) = 0x12;
                   7327:                m_CF = 1;
                   7328:                return;
                   7329:        }
                   7330:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     7331:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7332:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     7333:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7334:                              !msdos_find_file_has_8dot3name(&fd)) {
                   7335:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7336:                                        FindClose(dtainfo->find_handle);
                   7337:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7338:                                        break;
                   7339:                                }
                   7340:                        }
                   7341:                } else {
1.1.1.13  root     7342:                        FindClose(dtainfo->find_handle);
                   7343:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7344:                }
                   7345:        }
1.1.1.13  root     7346:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7347:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7348:                msdos_find_file_conv_local_time(&fd);
                   7349:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   7350:                find->size = fd.nFileSizeLow;
1.1.1.13  root     7351:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     7352:                REG16(AX) = 0;
1.1.1.14  root     7353:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7354:                find->attrib = 8;
                   7355:                find->size = 0;
                   7356:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7357:                dtainfo->allowable_mask &= ~8;
1.1       root     7358:                REG16(AX) = 0;
                   7359:        } else {
                   7360:                REG16(AX) = 0x12;
1.1.1.3   root     7361:                m_CF = 1;
1.1       root     7362:        }
                   7363: }
                   7364: 
                   7365: inline void msdos_int_21h_50h()
                   7366: {
1.1.1.8   root     7367:        if(current_psp != REG16(BX)) {
                   7368:                process_t *process = msdos_process_info_get(current_psp);
                   7369:                if(process != NULL) {
                   7370:                        process->psp = REG16(BX);
                   7371:                }
                   7372:                current_psp = REG16(BX);
1.1.1.23  root     7373:                msdos_sda_update(current_psp);
1.1.1.8   root     7374:        }
1.1       root     7375: }
                   7376: 
                   7377: inline void msdos_int_21h_51h()
                   7378: {
                   7379:        REG16(BX) = current_psp;
                   7380: }
                   7381: 
                   7382: inline void msdos_int_21h_52h()
                   7383: {
1.1.1.25  root     7384:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     7385:        i386_load_segment_descriptor(ES);
1.1.1.25  root     7386:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     7387: }
                   7388: 
                   7389: inline void msdos_int_21h_54h()
                   7390: {
                   7391:        process_t *process = msdos_process_info_get(current_psp);
                   7392:        
                   7393:        REG8(AL) = process->verify;
                   7394: }
                   7395: 
                   7396: inline void msdos_int_21h_55h()
                   7397: {
                   7398:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   7399:        
                   7400:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   7401:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   7402:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   7403:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   7404:        psp->parent_psp = current_psp;
                   7405: }
                   7406: 
                   7407: inline void msdos_int_21h_56h(int lfn)
                   7408: {
                   7409:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     7410:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   7411:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     7412:        
                   7413:        if(rename(src, dst)) {
                   7414:                REG16(AX) = errno;
1.1.1.3   root     7415:                m_CF = 1;
1.1       root     7416:        }
                   7417: }
                   7418: 
                   7419: inline void msdos_int_21h_57h()
                   7420: {
                   7421:        FILETIME time, local;
1.1.1.14  root     7422:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     7423:        HANDLE hHandle;
1.1       root     7424:        
1.1.1.21  root     7425:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     7426:                REG16(AX) = (UINT16)GetLastError();
                   7427:                m_CF = 1;
                   7428:                return;
                   7429:        }
                   7430:        ctime = atime = mtime = NULL;
                   7431:        
1.1       root     7432:        switch(REG8(AL)) {
                   7433:        case 0x00:
1.1.1.6   root     7434:        case 0x01:
1.1.1.14  root     7435:                mtime = &time;
1.1.1.6   root     7436:                break;
                   7437:        case 0x04:
                   7438:        case 0x05:
1.1.1.14  root     7439:                atime = &time;
1.1       root     7440:                break;
1.1.1.6   root     7441:        case 0x06:
                   7442:        case 0x07:
1.1.1.14  root     7443:                ctime = &time;
                   7444:                break;
                   7445:        default:
1.1.1.22  root     7446:                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     7447:                REG16(AX) = 0x01;
                   7448:                m_CF = 1;
                   7449:                return;
                   7450:        }
                   7451:        if(REG8(AL) & 1) {
1.1       root     7452:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   7453:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     7454:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     7455:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7456:                        m_CF = 1;
1.1       root     7457:                }
1.1.1.14  root     7458:        } else {
1.1.1.21  root     7459:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     7460:                        // assume a device and use the current time
                   7461:                        GetSystemTimeAsFileTime(&time);
                   7462:                }
                   7463:                FileTimeToLocalFileTime(&time, &local);
                   7464:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     7465:        }
                   7466: }
                   7467: 
                   7468: inline void msdos_int_21h_58h()
                   7469: {
                   7470:        switch(REG8(AL)) {
                   7471:        case 0x00:
1.1.1.7   root     7472:                REG16(AX) = malloc_strategy;
                   7473:                break;
                   7474:        case 0x01:
1.1.1.24  root     7475: //             switch(REG16(BX)) {
                   7476:                switch(REG8(BL)) {
1.1.1.7   root     7477:                case 0x0000:
                   7478:                case 0x0001:
                   7479:                case 0x0002:
                   7480:                case 0x0040:
                   7481:                case 0x0041:
                   7482:                case 0x0042:
                   7483:                case 0x0080:
                   7484:                case 0x0081:
                   7485:                case 0x0082:
                   7486:                        malloc_strategy = REG16(BX);
1.1.1.23  root     7487:                        msdos_sda_update(current_psp);
1.1.1.7   root     7488:                        break;
                   7489:                default:
1.1.1.22  root     7490:                        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     7491:                        REG16(AX) = 0x01;
                   7492:                        m_CF = 1;
                   7493:                        break;
                   7494:                }
                   7495:                break;
                   7496:        case 0x02:
1.1.1.19  root     7497:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     7498:                break;
                   7499:        case 0x03:
1.1.1.24  root     7500: //             switch(REG16(BX)) {
                   7501:                switch(REG8(BL)) {
1.1.1.7   root     7502:                case 0x0000:
1.1.1.19  root     7503:                        msdos_mem_unlink_umb();
                   7504:                        break;
1.1.1.7   root     7505:                case 0x0001:
1.1.1.19  root     7506:                        msdos_mem_link_umb();
1.1.1.7   root     7507:                        break;
                   7508:                default:
1.1.1.22  root     7509:                        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     7510:                        REG16(AX) = 0x01;
                   7511:                        m_CF = 1;
                   7512:                        break;
                   7513:                }
1.1       root     7514:                break;
                   7515:        default:
1.1.1.22  root     7516:                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     7517:                REG16(AX) = 0x01;
1.1.1.3   root     7518:                m_CF = 1;
1.1       root     7519:                break;
                   7520:        }
                   7521: }
                   7522: 
                   7523: inline void msdos_int_21h_59h()
                   7524: {
1.1.1.23  root     7525:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   7526:        
                   7527:        REG16(AX) = sda->extended_error_code;
                   7528:        REG8(BH) = sda->error_class;
                   7529:        REG8(BL) = sda->suggested_action;
                   7530:        REG8(CH) = sda->locus_of_last_error;
1.1       root     7531: }
                   7532: 
                   7533: inline void msdos_int_21h_5ah()
                   7534: {
1.1.1.3   root     7535:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7536:        int len = strlen(path);
                   7537:        char tmp[MAX_PATH];
                   7538:        
                   7539:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   7540:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7541:                
                   7542:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7543:                REG16(AX) = fd;
                   7544:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7545:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7546:                
                   7547:                strcpy(path, tmp);
                   7548:                int dx = REG16(DX) + len;
1.1.1.3   root     7549:                int ds = SREG(DS);
1.1       root     7550:                while(dx > 0xffff) {
                   7551:                        dx -= 0x10;
                   7552:                        ds++;
                   7553:                }
                   7554:                REG16(DX) = dx;
1.1.1.3   root     7555:                SREG(DS) = ds;
                   7556:                i386_load_segment_descriptor(DS);
1.1       root     7557:        } else {
                   7558:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7559:                m_CF = 1;
1.1       root     7560:        }
                   7561: }
                   7562: 
                   7563: inline void msdos_int_21h_5bh()
                   7564: {
1.1.1.3   root     7565:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7566:        
1.1.1.24  root     7567:        if(msdos_is_existing_file(path)) {
1.1       root     7568:                // already exists
                   7569:                REG16(AX) = 0x50;
1.1.1.3   root     7570:                m_CF = 1;
1.1       root     7571:        } else {
                   7572:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7573:                
                   7574:                if(fd != -1) {
                   7575:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7576:                        REG16(AX) = fd;
                   7577:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7578:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7579:                } else {
                   7580:                        REG16(AX) = errno;
1.1.1.3   root     7581:                        m_CF = 1;
1.1       root     7582:                }
                   7583:        }
                   7584: }
                   7585: 
                   7586: inline void msdos_int_21h_5ch()
                   7587: {
                   7588:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7589:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7590:        
1.1.1.20  root     7591:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     7592:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   7593:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     7594:                        UINT32 pos = _tell(fd);
                   7595:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   7596:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     7597:                                REG16(AX) = errno;
1.1.1.3   root     7598:                                m_CF = 1;
1.1       root     7599:                        }
1.1.1.20  root     7600:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     7601:                        
1.1       root     7602:                        // some seconds may be passed in _locking()
                   7603:                        hardware_update();
                   7604:                } else {
                   7605:                        REG16(AX) = 0x01;
1.1.1.3   root     7606:                        m_CF = 1;
1.1       root     7607:                }
                   7608:        } else {
                   7609:                REG16(AX) = 0x06;
1.1.1.3   root     7610:                m_CF = 1;
1.1       root     7611:        }
                   7612: }
                   7613: 
1.1.1.22  root     7614: inline void msdos_int_21h_5dh()
                   7615: {
                   7616:        switch(REG8(AL)) {
                   7617:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     7618:                SREG(DS) = (SDA_TOP >> 4);
                   7619:                i386_load_segment_descriptor(DS);
                   7620:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   7621:                REG16(CX) = 0x80;
                   7622:                REG16(DX) = 0x1a;
                   7623:                break;
                   7624:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     7625:                REG16(AX) = 0x01;
                   7626:                m_CF = 1;
                   7627:                break;
                   7628:        case 0x08: // set redirected printer mode
                   7629:        case 0x09: // flush redirected printer output
                   7630:        case 0x0a: // set extended error information
                   7631:                break;
                   7632:        default:
                   7633:                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));
                   7634:                REG16(AX) = 0x01;
                   7635:                m_CF = 1;
                   7636:                break;
                   7637:        }
                   7638: }
                   7639: 
1.1       root     7640: inline void msdos_int_21h_60h(int lfn)
                   7641: {
1.1.1.14  root     7642:        char full[MAX_PATH], *path;
                   7643:        
1.1       root     7644:        if(lfn) {
1.1.1.14  root     7645:                char *name;
                   7646:                *full = '\0';
1.1.1.3   root     7647:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     7648:                switch(REG8(CL)) {
                   7649:                case 1:
                   7650:                        GetShortPathName(full, full, MAX_PATH);
                   7651:                        my_strupr(full);
                   7652:                        break;
                   7653:                case 2:
                   7654:                        GetLongPathName(full, full, MAX_PATH);
                   7655:                        break;
                   7656:                }
                   7657:                path = full;
                   7658:        } else {
                   7659:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   7660:        }
                   7661:        if(*path != '\0') {
                   7662:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     7663:        } else {
1.1.1.14  root     7664:                REG16(AX) = (UINT16)GetLastError();
                   7665:                m_CF = 1;
1.1       root     7666:        }
                   7667: }
                   7668: 
                   7669: inline void msdos_int_21h_61h()
                   7670: {
                   7671:        REG8(AL) = 0;
                   7672: }
                   7673: 
                   7674: inline void msdos_int_21h_62h()
                   7675: {
                   7676:        REG16(BX) = current_psp;
                   7677: }
                   7678: 
                   7679: inline void msdos_int_21h_63h()
                   7680: {
                   7681:        switch(REG8(AL)) {
                   7682:        case 0x00:
1.1.1.3   root     7683:                SREG(DS) = (DBCS_TABLE >> 4);
                   7684:                i386_load_segment_descriptor(DS);
1.1       root     7685:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   7686:                REG8(AL) = 0x00;
                   7687:                break;
1.1.1.22  root     7688:        case 0x01: // set korean input mode
                   7689:        case 0x02: // get korean input mode
                   7690:                REG8(AL) = 0xff; // not supported
                   7691:                break;
1.1       root     7692:        default:
1.1.1.22  root     7693:                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     7694:                REG16(AX) = 0x01;
1.1.1.3   root     7695:                m_CF = 1;
1.1       root     7696:                break;
                   7697:        }
                   7698: }
                   7699: 
1.1.1.25  root     7700: UINT16 get_extended_country_info(UINT8 func)
1.1       root     7701: {
1.1.1.25  root     7702:        switch(func) {
1.1.1.17  root     7703:        case 0x01:
                   7704:                if(REG16(CX) >= 5) {
1.1.1.19  root     7705:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     7706:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   7707:                                REG16(CX) = sizeof(data);
                   7708:                        ZeroMemory(data, sizeof(data));
                   7709:                        data[0] = 0x01;
                   7710:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     7711:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     7712:                        *(UINT16 *)(data + 5) = active_code_page;
                   7713:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     7714: //                     REG16(AX) = active_code_page;
1.1.1.17  root     7715:                } else {
1.1.1.25  root     7716:                        return(0x08); // insufficient memory
1.1.1.17  root     7717:                }
                   7718:                break;
                   7719:        case 0x02:
                   7720:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   7721:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   7722:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     7723: //             REG16(AX) = active_code_page;
1.1.1.17  root     7724:                REG16(CX) = 0x05;
                   7725:                break;
1.1.1.23  root     7726:        case 0x03:
                   7727:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   7728:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   7729:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     7730: //             REG16(AX) = active_code_page;
1.1.1.23  root     7731:                REG16(CX) = 0x05;
                   7732:                break;
1.1.1.17  root     7733:        case 0x04:
                   7734:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   7735:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   7736:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     7737: //             REG16(AX) = active_code_page;
1.1.1.17  root     7738:                REG16(CX) = 0x05;
                   7739:                break;
                   7740:        case 0x05:
                   7741:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   7742:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   7743:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     7744: //             REG16(AX) = active_code_page;
1.1.1.17  root     7745:                REG16(CX) = 0x05;
                   7746:                break;
                   7747:        case 0x06:
                   7748:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   7749:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   7750:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     7751: //             REG16(AX) = active_code_page;
1.1.1.17  root     7752:                REG16(CX) = 0x05;
                   7753:                break;
1.1       root     7754:        case 0x07:
1.1.1.3   root     7755:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   7756:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   7757:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     7758: //             REG16(AX) = active_code_page;
1.1       root     7759:                REG16(CX) = 0x05;
                   7760:                break;
1.1.1.25  root     7761:        default:
                   7762:                return(0x01); // function number invalid
                   7763:        }
                   7764:        return(0x00);
                   7765: }
                   7766: 
                   7767: inline void msdos_int_21h_65h()
                   7768: {
                   7769:        char tmp[0x10000];
                   7770:        
                   7771:        switch(REG8(AL)) {
                   7772:        case 0x01:
                   7773:        case 0x02:
                   7774:        case 0x03:
                   7775:        case 0x04:
                   7776:        case 0x05:
                   7777:        case 0x06:
                   7778:        case 0x07:
                   7779:                {
                   7780:                        UINT16 result = get_extended_country_info(REG8(AL));
                   7781:                        if(result) {
                   7782:                                REG16(AX) = result;
                   7783:                                m_CF = 1;
                   7784:                        } else {
                   7785:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   7786:                        }
                   7787:                }
                   7788:                break;
1.1       root     7789:        case 0x20:
1.1.1.25  root     7790:        case 0xa0:
1.1.1.19  root     7791:                memset(tmp, 0, sizeof(tmp));
                   7792:                tmp[0] = REG8(DL);
1.1       root     7793:                my_strupr(tmp);
                   7794:                REG8(DL) = tmp[0];
                   7795:                break;
                   7796:        case 0x21:
1.1.1.25  root     7797:        case 0xa1:
1.1       root     7798:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     7799:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     7800:                my_strupr(tmp);
1.1.1.3   root     7801:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     7802:                break;
                   7803:        case 0x22:
1.1.1.25  root     7804:        case 0xa2:
1.1.1.3   root     7805:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     7806:                break;
1.1.1.25  root     7807:        case 0x23:
                   7808:                // FIXME: need to check multi-byte (kanji) charactre?
                   7809:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   7810:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   7811:                        REG16(AX) = 0x00;
                   7812:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   7813:                        // 826dh/828eh: multi-byte (kanji) N and n
                   7814:                        REG16(AX) = 0x01;
                   7815:                } else {
                   7816:                        REG16(AX) = 0x02;
                   7817:                }
                   7818:                break;
1.1       root     7819:        default:
1.1.1.22  root     7820:                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     7821:                REG16(AX) = 0x01;
1.1.1.3   root     7822:                m_CF = 1;
1.1       root     7823:                break;
                   7824:        }
                   7825: }
                   7826: 
                   7827: inline void msdos_int_21h_66h()
                   7828: {
                   7829:        switch(REG8(AL)) {
                   7830:        case 0x01:
                   7831:                REG16(BX) = active_code_page;
                   7832:                REG16(DX) = system_code_page;
                   7833:                break;
                   7834:        case 0x02:
                   7835:                if(active_code_page == REG16(BX)) {
                   7836:                        REG16(AX) = 0xeb41;
                   7837:                } else if(_setmbcp(REG16(BX)) == 0) {
                   7838:                        active_code_page = REG16(BX);
1.1.1.17  root     7839:                        msdos_nls_tables_update();
1.1       root     7840:                        REG16(AX) = 0xeb41;
                   7841:                } else {
                   7842:                        REG16(AX) = 0x25;
1.1.1.3   root     7843:                        m_CF = 1;
1.1       root     7844:                }
                   7845:                break;
                   7846:        default:
1.1.1.22  root     7847:                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     7848:                REG16(AX) = 0x01;
1.1.1.3   root     7849:                m_CF = 1;
1.1       root     7850:                break;
                   7851:        }
                   7852: }
                   7853: 
                   7854: inline void msdos_int_21h_67h()
                   7855: {
                   7856:        process_t *process = msdos_process_info_get(current_psp);
                   7857:        
                   7858:        if(REG16(BX) <= MAX_FILES) {
                   7859:                process->max_files = max(REG16(BX), 20);
                   7860:        } else {
                   7861:                REG16(AX) = 0x08;
1.1.1.3   root     7862:                m_CF = 1;
1.1       root     7863:        }
                   7864: }
                   7865: 
                   7866: inline void msdos_int_21h_68h()
                   7867: {
                   7868:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7869:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7870:        
1.1.1.20  root     7871:        if(fd < process->max_files && file_handler[fd].valid) {
                   7872:                // fflush(_fdopen(fd, ""));
1.1       root     7873:        } else {
                   7874:                REG16(AX) = 0x06;
1.1.1.3   root     7875:                m_CF = 1;
1.1       root     7876:        }
                   7877: }
                   7878: 
                   7879: inline void msdos_int_21h_69h()
                   7880: {
1.1.1.3   root     7881:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7882:        char path[] = "A:\\";
                   7883:        char volume_label[MAX_PATH];
                   7884:        DWORD serial_number = 0;
                   7885:        char file_system[MAX_PATH];
                   7886:        
                   7887:        if(REG8(BL) == 0) {
                   7888:                path[0] = 'A' + _getdrive() - 1;
                   7889:        } else {
                   7890:                path[0] = 'A' + REG8(BL) - 1;
                   7891:        }
                   7892:        
                   7893:        switch(REG8(AL)) {
                   7894:        case 0x00:
                   7895:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   7896:                        info->info_level = 0;
                   7897:                        info->serial_number = serial_number;
                   7898:                        memset(info->volume_label, 0x20, 11);
                   7899:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   7900:                        memset(info->file_system, 0x20, 8);
                   7901:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   7902:                } else {
                   7903:                        REG16(AX) = errno;
1.1.1.3   root     7904:                        m_CF = 1;
1.1       root     7905:                }
                   7906:                break;
                   7907:        case 0x01:
                   7908:                REG16(AX) = 0x03;
1.1.1.3   root     7909:                m_CF = 1;
1.1       root     7910:        }
                   7911: }
                   7912: 
                   7913: inline void msdos_int_21h_6ah()
                   7914: {
                   7915:        REG8(AH) = 0x68;
                   7916:        msdos_int_21h_68h();
                   7917: }
                   7918: 
                   7919: inline void msdos_int_21h_6bh()
                   7920: {
                   7921:        REG8(AL) = 0;
                   7922: }
                   7923: 
                   7924: inline void msdos_int_21h_6ch(int lfn)
                   7925: {
1.1.1.3   root     7926:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     7927:        int mode = REG8(BL) & 0x03;
                   7928:        
                   7929:        if(mode < 0x03) {
1.1.1.24  root     7930:                if(msdos_is_existing_file(path) || msdos_is_driver_name(path)) {
1.1       root     7931:                        // file exists
                   7932:                        if(REG8(DL) & 1) {
1.1.1.11  root     7933:                                int fd = -1;
                   7934:                                UINT16 info;
1.1       root     7935:                                
1.1.1.11  root     7936:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     7937:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     7938:                                        info = 0x80d3;
1.1.1.14  root     7939:                                } else if(msdos_is_nul_path(path)) {
                   7940:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7941:                                        info = 0x80d3;
1.1.1.24  root     7942:                                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     7943:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7944:                                        info = 0x80d3;
1.1.1.11  root     7945:                                } else {
1.1.1.13  root     7946:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     7947:                                        info = msdos_drive_number(path);
                   7948:                                }
1.1       root     7949:                                if(fd != -1) {
                   7950:                                        REG16(AX) = fd;
                   7951:                                        REG16(CX) = 1;
1.1.1.11  root     7952:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     7953:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7954:                                } else {
                   7955:                                        REG16(AX) = errno;
1.1.1.3   root     7956:                                        m_CF = 1;
1.1       root     7957:                                }
                   7958:                        } else if(REG8(DL) & 2) {
                   7959:                                int attr = GetFileAttributes(path);
                   7960:                                int fd = -1;
1.1.1.11  root     7961:                                UINT16 info;
1.1       root     7962:                                
1.1.1.11  root     7963:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     7964:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     7965:                                        info = 0x80d3;
1.1.1.14  root     7966:                                } else if(msdos_is_nul_path(path)) {
                   7967:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7968:                                        info = 0x80d3;
1.1.1.24  root     7969:                                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     7970:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7971:                                        info = 0x80d3;
1.1       root     7972:                                } else {
                   7973:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     7974:                                        info = msdos_drive_number(path);
1.1       root     7975:                                }
                   7976:                                if(fd != -1) {
                   7977:                                        if(attr == -1) {
                   7978:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   7979:                                        }
                   7980:                                        SetFileAttributes(path, attr);
                   7981:                                        REG16(AX) = fd;
                   7982:                                        REG16(CX) = 3;
1.1.1.11  root     7983:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     7984:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7985:                                } else {
                   7986:                                        REG16(AX) = errno;
1.1.1.3   root     7987:                                        m_CF = 1;
1.1       root     7988:                                }
                   7989:                        } else {
                   7990:                                REG16(AX) = 0x50;
1.1.1.3   root     7991:                                m_CF = 1;
1.1       root     7992:                        }
                   7993:                } else {
                   7994:                        // file not exists
                   7995:                        if(REG8(DL) & 0x10) {
                   7996:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7997:                                
                   7998:                                if(fd != -1) {
                   7999:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   8000:                                        REG16(AX) = fd;
                   8001:                                        REG16(CX) = 2;
                   8002:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     8003:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8004:                                } else {
                   8005:                                        REG16(AX) = errno;
1.1.1.3   root     8006:                                        m_CF = 1;
1.1       root     8007:                                }
                   8008:                        } else {
                   8009:                                REG16(AX) = 0x02;
1.1.1.3   root     8010:                                m_CF = 1;
1.1       root     8011:                        }
                   8012:                }
                   8013:        } else {
                   8014:                REG16(AX) = 0x0c;
1.1.1.3   root     8015:                m_CF = 1;
1.1       root     8016:        }
                   8017: }
                   8018: 
                   8019: inline void msdos_int_21h_710dh()
                   8020: {
                   8021:        // reset drive
                   8022: }
                   8023: 
1.1.1.17  root     8024: inline void msdos_int_21h_7141h(int lfn)
                   8025: {
                   8026:        if(REG16(SI) == 0) {
                   8027:                msdos_int_21h_41h(lfn);
                   8028:                return;
                   8029:        }
                   8030:        if(REG16(SI) != 1) {
                   8031:                REG16(AX) = 5;
                   8032:                m_CF = 1;
                   8033:        }
                   8034:        /* wild card and matching attributes... */
                   8035:        char tmp[MAX_PATH * 2];
                   8036:        // copy search pathname (and quick check overrun)
                   8037:        ZeroMemory(tmp, sizeof(tmp));
                   8038:        tmp[MAX_PATH - 1] = '\0';
                   8039:        tmp[MAX_PATH] = 1;
                   8040:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   8041:        
                   8042:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   8043:                REG16(AX) = 1;
                   8044:                m_CF = 1;
                   8045:                return;
                   8046:        }
                   8047:        for(char *s = tmp; *s; ++s) {
                   8048:                if(*s == '/') {
                   8049:                        *s = '\\';
                   8050:                }
                   8051:        }
                   8052:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   8053:        if(tmp_name) {
                   8054:                ++tmp_name;
                   8055:        } else {
                   8056:                tmp_name = strchr(tmp, ':');
                   8057:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   8058:        }
                   8059:        
                   8060:        WIN32_FIND_DATAA fd;
                   8061:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   8062:        if(fh == INVALID_HANDLE_VALUE) {
                   8063:                REG16(AX) = 2;
                   8064:                m_CF = 1;
                   8065:                return;
                   8066:        }
                   8067:        do {
                   8068:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   8069:                        strcpy(tmp_name, fd.cFileName);
                   8070:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   8071:                                REG16(AX) = 5;
                   8072:                                m_CF = 1;
                   8073:                                break;
                   8074:                        }
                   8075:                }
                   8076:        } while(FindNextFileA(fh, &fd));
                   8077:        if(!m_CF) {
                   8078:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   8079:                        m_CF = 1;
                   8080:                        REG16(AX) = 2;
                   8081:                }
                   8082:        }
                   8083:        FindClose(fh);
                   8084: }
                   8085: 
1.1       root     8086: inline void msdos_int_21h_714eh()
                   8087: {
                   8088:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8089:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   8090:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8091:        WIN32_FIND_DATA fd;
                   8092:        
1.1.1.13  root     8093:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   8094:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8095:                FindClose(dtainfo->find_handle);
                   8096:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8097:        }
                   8098:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8099:        dtainfo->allowable_mask = REG8(CL);
                   8100:        dtainfo->required_mask = REG8(CH);
                   8101:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8102:        
1.1.1.14  root     8103:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8104:                dtainfo->allowable_mask &= ~8;
1.1       root     8105:        }
1.1.1.14  root     8106:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8107:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8108:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8109:                                FindClose(dtainfo->find_handle);
                   8110:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8111:                                break;
                   8112:                        }
                   8113:                }
                   8114:        }
1.1.1.13  root     8115:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8116:                find->attrib = fd.dwFileAttributes;
                   8117:                msdos_find_file_conv_local_time(&fd);
                   8118:                if(REG16(SI) == 0) {
                   8119:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8120:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8121:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8122:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8123:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8124:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8125:                } else {
                   8126:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8127:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8128:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8129:                }
                   8130:                find->size_hi = fd.nFileSizeHigh;
                   8131:                find->size_lo = fd.nFileSizeLow;
                   8132:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8133:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8134:                REG16(AX) = dtainfo - dtalist + 1;
                   8135:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8136:                // volume label
                   8137:                find->attrib = 8;
                   8138:                find->size_hi = find->size_lo = 0;
                   8139:                strcpy(find->full_name, process->volume_label);
                   8140:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8141:                dtainfo->allowable_mask &= ~8;
                   8142:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     8143:        } else {
                   8144:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     8145:                m_CF = 1;
1.1       root     8146:        }
                   8147: }
                   8148: 
                   8149: inline void msdos_int_21h_714fh()
                   8150: {
                   8151:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     8152:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8153:        WIN32_FIND_DATA fd;
                   8154:        
1.1.1.14  root     8155:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8156:                REG16(AX) = 6;
1.1.1.13  root     8157:                m_CF = 1;
                   8158:                return;
                   8159:        }
1.1.1.14  root     8160:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8161:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8162:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8163:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     8164:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8165:                                        FindClose(dtainfo->find_handle);
                   8166:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8167:                                        break;
                   8168:                                }
                   8169:                        }
                   8170:                } else {
1.1.1.13  root     8171:                        FindClose(dtainfo->find_handle);
                   8172:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8173:                }
                   8174:        }
1.1.1.13  root     8175:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8176:                find->attrib = fd.dwFileAttributes;
                   8177:                msdos_find_file_conv_local_time(&fd);
                   8178:                if(REG16(SI) == 0) {
                   8179:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   8180:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   8181:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   8182:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   8183:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   8184:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   8185:                } else {
                   8186:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   8187:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   8188:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   8189:                }
                   8190:                find->size_hi = fd.nFileSizeHigh;
                   8191:                find->size_lo = fd.nFileSizeLow;
                   8192:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     8193:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     8194:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8195:                // volume label
                   8196:                find->attrib = 8;
                   8197:                find->size_hi = find->size_lo = 0;
                   8198:                strcpy(find->full_name, process->volume_label);
                   8199:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     8200:                dtainfo->allowable_mask &= ~8;
1.1       root     8201:        } else {
                   8202:                REG16(AX) = 0x12;
1.1.1.3   root     8203:                m_CF = 1;
1.1       root     8204:        }
                   8205: }
                   8206: 
                   8207: inline void msdos_int_21h_71a0h()
                   8208: {
                   8209:        DWORD max_component_len, file_sys_flag;
                   8210:        
1.1.1.14  root     8211:        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))) {
                   8212:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   8213:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     8214:                REG16(CX) = (UINT16)max_component_len;          // 255
                   8215:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   8216:        } else {
                   8217:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8218:                m_CF = 1;
1.1       root     8219:        }
                   8220: }
                   8221: 
                   8222: inline void msdos_int_21h_71a1h()
                   8223: {
1.1.1.14  root     8224:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   8225:                REG16(AX) = 6;
1.1.1.13  root     8226:                m_CF = 1;
                   8227:                return;
                   8228:        }
1.1.1.14  root     8229:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     8230:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8231:                FindClose(dtainfo->find_handle);
                   8232:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8233:        }
                   8234: }
                   8235: 
                   8236: inline void msdos_int_21h_71a6h()
                   8237: {
                   8238:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8239:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8240:        
1.1.1.3   root     8241:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     8242:        struct _stat64 status;
                   8243:        DWORD serial_number = 0;
                   8244:        
1.1.1.20  root     8245:        if(fd < process->max_files && file_handler[fd].valid) {
                   8246:                if(_fstat64(fd, &status) == 0) {
                   8247:                        if(file_handler[fd].path[1] == ':') {
1.1       root     8248:                                // NOTE: we need to consider the network file path "\\host\share\"
                   8249:                                char volume[] = "A:\\";
1.1.1.20  root     8250:                                volume[0] = file_handler[fd].path[1];
1.1       root     8251:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   8252:                        }
1.1.1.20  root     8253:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     8254:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   8255:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   8256:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   8257:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   8258:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   8259:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   8260:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   8261:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   8262:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   8263:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     8264:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     8265:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     8266:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     8267:                } else {
                   8268:                        REG16(AX) = errno;
1.1.1.3   root     8269:                        m_CF = 1;
1.1       root     8270:                }
                   8271:        } else {
                   8272:                REG16(AX) = 0x06;
1.1.1.3   root     8273:                m_CF = 1;
1.1       root     8274:        }
                   8275: }
                   8276: 
                   8277: inline void msdos_int_21h_71a7h()
                   8278: {
                   8279:        switch(REG8(BL)) {
                   8280:        case 0x00:
1.1.1.3   root     8281:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     8282:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8283:                        m_CF = 1;
1.1       root     8284:                }
                   8285:                break;
                   8286:        case 0x01:
                   8287:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     8288:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     8289:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     8290:                        m_CF = 1;
1.1       root     8291:                }
                   8292:                break;
                   8293:        default:
1.1.1.22  root     8294:                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     8295:                REG16(AX) = 0x01;
1.1.1.3   root     8296:                m_CF = 1;
1.1       root     8297:                break;
                   8298:        }
                   8299: }
                   8300: 
                   8301: inline void msdos_int_21h_71a8h()
                   8302: {
                   8303:        if(REG8(DH) == 0) {
                   8304:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     8305:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8306:                memset(fcb, 0x20, sizeof(fcb));
                   8307:                int len = strlen(tmp);
1.1.1.21  root     8308:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     8309:                        if(tmp[i] == '.') {
                   8310:                                pos = 8;
                   8311:                        } else {
                   8312:                                if(msdos_lead_byte_check(tmp[i])) {
                   8313:                                        fcb[pos++] = tmp[i++];
                   8314:                                }
                   8315:                                fcb[pos++] = tmp[i];
                   8316:                        }
                   8317:                }
1.1.1.3   root     8318:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     8319:        } else {
1.1.1.3   root     8320:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     8321:        }
                   8322: }
                   8323: 
1.1.1.22  root     8324: inline void msdos_int_21h_71aah()
                   8325: {
                   8326:        char drv[] = "A:", path[MAX_PATH];
                   8327:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8328:        
                   8329:        if(REG8(BL) == 0) {
                   8330:                drv[0] = 'A' + _getdrive() - 1;
                   8331:        } else {
                   8332:                drv[0] = 'A' + REG8(BL) - 1;
                   8333:        }
                   8334:        switch(REG8(BH)) {
                   8335:        case 0x00:
                   8336:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   8337:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   8338:                        if(GetLogicalDrives() & bits) {
                   8339:                                REG16(AX) = 0x0f; // invalid drive
                   8340:                        } else {
                   8341:                                REG16(AX) = 0x03; // path not found
                   8342:                        }
                   8343:                        m_CF = 1;
                   8344:                }
                   8345:                break;
                   8346:        case 0x01:
                   8347:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   8348:                        REG16(AX) = 0x0f; // invalid drive
                   8349:                        m_CF = 1;
                   8350:                }
                   8351:                break;
                   8352:        case 0x02:
                   8353:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   8354:                        REG16(AX) = 0x0f; // invalid drive
                   8355:                        m_CF = 1;
                   8356:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   8357:                        REG16(AX) = 0x0f; // invalid drive
                   8358:                        m_CF = 1;
                   8359:                } else {
                   8360:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   8361:                }
                   8362:                break;
                   8363:        default:
                   8364:                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));
                   8365:                REG16(AX) = 0x01;
                   8366:                m_CF = 1;
                   8367:                break;
                   8368:        }
                   8369: }
                   8370: 
1.1.1.14  root     8371: inline void msdos_int_21h_7300h()
                   8372: {
                   8373:        if(REG8(AL) == 0) {
                   8374:                REG8(AL) = REG8(CL);
                   8375:                REG8(AH) = 0;
                   8376:        } else {
                   8377:                REG16(AX) = 0x01;
                   8378:                m_CF = 1;
                   8379:        }
                   8380: }
                   8381: 
                   8382: inline void msdos_int_21h_7302h()
                   8383: {
                   8384:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   8385:        UINT16 seg, ofs;
                   8386:        
                   8387:        if(REG16(CX) < 0x3f) {
                   8388:                REG8(AL) = 0x18;
                   8389:                m_CF = 1;
                   8390:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8391:                REG8(AL) = 0xff;
                   8392:                m_CF = 1;
                   8393:        } else {
                   8394:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   8395:        }
                   8396: }
                   8397: 
1.1       root     8398: inline void msdos_int_21h_7303h()
                   8399: {
1.1.1.3   root     8400:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8401:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     8402:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   8403:        
                   8404:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   8405:                info->size_of_structure = sizeof(ext_space_info_t);
                   8406:                info->structure_version = 0;
                   8407:                info->sectors_per_cluster = sectors_per_cluster;
                   8408:                info->bytes_per_sector = bytes_per_sector;
                   8409:                info->available_clusters_on_drive = free_clusters;
                   8410:                info->total_clusters_on_drive = total_clusters;
                   8411:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   8412:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   8413:                info->available_allocation_units = free_clusters;       // ???
                   8414:                info->total_allocation_units = total_clusters;          // ???
                   8415:        } else {
                   8416:                REG16(AX) = errno;
1.1.1.3   root     8417:                m_CF = 1;
1.1       root     8418:        }
                   8419: }
                   8420: 
                   8421: inline void msdos_int_25h()
                   8422: {
                   8423:        UINT16 seg, ofs;
                   8424:        DWORD dwSize;
                   8425:        
1.1.1.3   root     8426: #if defined(HAS_I386)
                   8427:        I386OP(pushf)();
                   8428: #else
                   8429:        PREFIX86(_pushf());
                   8430: #endif
1.1       root     8431:        
                   8432:        if(!(REG8(AL) < 26)) {
                   8433:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8434:                m_CF = 1;
1.1       root     8435:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8436:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8437:                m_CF = 1;
1.1       root     8438:        } else {
                   8439:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8440:                char dev[64];
                   8441:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8442:                
                   8443:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8444:                if(hFile == INVALID_HANDLE_VALUE) {
                   8445:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8446:                        m_CF = 1;
1.1       root     8447:                } else {
1.1.1.19  root     8448:                        UINT32 top_sector  = REG16(DX);
                   8449:                        UINT16 sector_num  = REG16(CX);
                   8450:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8451:                        
                   8452:                        if(sector_num == 0xffff) {
                   8453:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8454:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8455:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8456:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8457:                                buffer_addr = (seg << 4) + ofs;
                   8458:                        }
                   8459: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8460: //                             REG8(AL) = 0x02; // drive not ready
                   8461: //                             m_CF = 1;
                   8462: //                     } else 
                   8463:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8464:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8465:                                m_CF = 1;
1.1.1.19  root     8466:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8467:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     8468:                                m_CF = 1;
1.1       root     8469:                        }
                   8470:                        CloseHandle(hFile);
                   8471:                }
                   8472:        }
                   8473: }
                   8474: 
                   8475: inline void msdos_int_26h()
                   8476: {
                   8477:        // this operation may cause serious damage for drives, so always returns error...
                   8478:        UINT16 seg, ofs;
                   8479:        DWORD dwSize;
                   8480:        
1.1.1.3   root     8481: #if defined(HAS_I386)
                   8482:        I386OP(pushf)();
                   8483: #else
                   8484:        PREFIX86(_pushf());
                   8485: #endif
1.1       root     8486:        
                   8487:        if(!(REG8(AL) < 26)) {
                   8488:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8489:                m_CF = 1;
1.1       root     8490:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8491:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8492:                m_CF = 1;
1.1       root     8493:        } else {
                   8494:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8495:                char dev[64];
                   8496:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8497:                
                   8498:                if(dpb->media_type == 0xf8) {
                   8499:                        // this drive is not a floppy
1.1.1.6   root     8500: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   8501: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   8502: //                     }
1.1       root     8503:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8504:                        m_CF = 1;
1.1       root     8505:                } else {
                   8506:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8507:                        if(hFile == INVALID_HANDLE_VALUE) {
                   8508:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8509:                                m_CF = 1;
1.1       root     8510:                        } else {
1.1.1.19  root     8511:                                UINT32 top_sector  = REG16(DX);
                   8512:                                UINT16 sector_num  = REG16(CX);
                   8513:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8514:                                
                   8515:                                if(sector_num == 0xffff) {
                   8516:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8517:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8518:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8519:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8520:                                        buffer_addr = (seg << 4) + ofs;
                   8521:                                }
1.1       root     8522:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8523:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8524:                                        m_CF = 1;
1.1.1.19  root     8525:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8526:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8527:                                        m_CF = 1;
1.1.1.19  root     8528:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8529:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     8530:                                        m_CF = 1;
1.1       root     8531:                                }
                   8532:                                CloseHandle(hFile);
                   8533:                        }
                   8534:                }
                   8535:        }
                   8536: }
                   8537: 
                   8538: inline void msdos_int_27h()
                   8539: {
1.1.1.14  root     8540:        msdos_mem_realloc(SREG(CS), (REG16(DX) + 15) >> 4, NULL);
1.1.1.3   root     8541:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1.1.14  root     8542:        
                   8543:        // int_21h_4bh succeeded
                   8544:        m_CF = 0;
1.1       root     8545: }
                   8546: 
                   8547: inline void msdos_int_29h()
                   8548: {
1.1.1.14  root     8549: #if 1
                   8550:        // need to check escape sequences
1.1       root     8551:        msdos_putch(REG8(AL));
1.1.1.14  root     8552: #else
                   8553:        DWORD num;
                   8554:        vram_flush();
1.1.1.23  root     8555:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     8556:        cursor_moved = true;
                   8557: #endif
1.1       root     8558: }
                   8559: 
                   8560: inline void msdos_int_2eh()
                   8561: {
                   8562:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   8563:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8564:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     8565:        char *token = my_strtok(tmp, " ");
                   8566:        strcpy(command, token);
                   8567:        strcpy(opt, token + strlen(token) + 1);
                   8568:        
                   8569:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   8570:        param->env_seg = 0;
                   8571:        param->cmd_line.w.l = 44;
                   8572:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   8573:        param->fcb1.w.l = 24;
                   8574:        param->fcb1.w.h = (WORK_TOP >> 4);
                   8575:        param->fcb2.w.l = 24;
                   8576:        param->fcb2.w.h = (WORK_TOP >> 4);
                   8577:        
                   8578:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   8579:        
                   8580:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   8581:        cmd_line->len = strlen(opt);
                   8582:        strcpy(cmd_line->cmd, opt);
                   8583:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   8584:        
1.1.1.28! root     8585:        try {
        !          8586:                if(msdos_process_exec(command, param, 0)) {
        !          8587:                        REG16(AX) = 0xffff; // error before processing command
        !          8588:                } else {
        !          8589:                        // set flag to set retval to ax when the started process is terminated
        !          8590:                        process_t *process = msdos_process_info_get(current_psp);
        !          8591:                        process->called_by_int2eh = true;
        !          8592:                }
        !          8593:        } catch(...) {
        !          8594:                REG16(AX) = 0xffff; // error before processing command
        !          8595:        }
1.1       root     8596: }
                   8597: 
1.1.1.22  root     8598: inline void msdos_int_2fh_01h()
                   8599: {
                   8600:        switch(REG8(AL)) {
                   8601:        case 0x00:
                   8602:                REG8(AL) = 0x01; // print.com is not installed, can't install
                   8603:                break;
                   8604:        default:
                   8605:                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));
                   8606:                REG16(AX) = 0x01;
                   8607:                m_CF = 1;
                   8608:                break;
                   8609:        }
                   8610: }
                   8611: 
                   8612: inline void msdos_int_2fh_05h()
                   8613: {
                   8614:        switch(REG8(AL)) {
                   8615:        case 0x00:
                   8616:                REG8(AL) = 0x01; // critical error handler is not installed, can't install
                   8617:                break;
                   8618:        default:
                   8619:                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));
                   8620:                REG16(AX) = 0x01;
                   8621:                m_CF = 1;
                   8622:                break;
                   8623:        }
                   8624: }
                   8625: 
                   8626: inline void msdos_int_2fh_06h()
                   8627: {
                   8628:        switch(REG8(AL)) {
                   8629:        case 0x00:
                   8630:                REG8(AL) = 0x01; // assign is not installed, can't install
                   8631:                break;
                   8632:        default:
                   8633:                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));
                   8634:                REG16(AX) = 0x01;
                   8635:                m_CF = 1;
                   8636:                break;
                   8637:        }
                   8638: }
                   8639: 
                   8640: inline void msdos_int_2fh_08h()
                   8641: {
                   8642:        switch(REG8(AL)) {
                   8643:        case 0x00:
                   8644:                REG8(AL) = 0x01; // driver.sys is not installed, can't install
                   8645:                break;
                   8646:        default:
                   8647:                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));
                   8648:                REG16(AX) = 0x01;
                   8649:                m_CF = 1;
                   8650:                break;
                   8651:        }
                   8652: }
                   8653: 
                   8654: inline void msdos_int_2fh_10h()
                   8655: {
                   8656:        switch(REG8(AL)) {
                   8657:        case 0x00:
                   8658:                REG8(AL) = 0x01; // share is not installed, can't install
                   8659:                break;
                   8660:        default:
                   8661:                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));
                   8662:                REG16(AX) = 0x01;
                   8663:                m_CF = 1;
                   8664:                break;
                   8665:        }
                   8666: }
                   8667: 
                   8668: inline void msdos_int_2fh_11h()
                   8669: {
                   8670:        switch(REG8(AL)) {
                   8671:        case 0x00:
                   8672:                REG8(AL) = 0x01; // mscdex is not installed, can't install
                   8673:                break;
                   8674:        default:
                   8675:                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));
                   8676:                REG16(AX) = 0x01;
                   8677:                m_CF = 1;
                   8678:                break;
                   8679:        }
                   8680: }
                   8681: 
1.1.1.21  root     8682: inline void msdos_int_2fh_12h()
                   8683: {
                   8684:        switch(REG8(AL)) {
1.1.1.22  root     8685:        case 0x00:
                   8686:                REG8(AL) = 0xff;
                   8687:                break;
1.1.1.21  root     8688:        case 0x16:
                   8689:                if(REG16(BX) < 20) {
                   8690:                        SREG(ES) = SFT_TOP >> 4;
                   8691:                        i386_load_segment_descriptor(ES);
                   8692:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   8693:                        
                   8694:                        // update system file table
                   8695:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   8696:                        if(file_handler[REG16(BX)].valid) {
                   8697:                                int count = 0;
                   8698:                                for(int i = 0; i < 20; i++) {
                   8699:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   8700:                                                count++;
                   8701:                                        }
                   8702:                                }
                   8703:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   8704:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   8705:                                _lseek(REG16(BX), 0, SEEK_END);
                   8706:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   8707:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   8708:                        } else {
                   8709:                                memset(sft, 0, 0x3b);
                   8710:                        }
                   8711:                } else {
                   8712:                        REG16(AX) = 0x06;
                   8713:                        m_CF = 1;
                   8714:                }
                   8715:                break;
                   8716:        case 0x20:
                   8717:                {
                   8718:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8719:                        
                   8720:                        if(fd < 20) {
                   8721:                                SREG(ES) = current_psp;
                   8722:                                i386_load_segment_descriptor(ES);
                   8723:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   8724:                        } else {
                   8725:                                REG16(AX) = 0x06;
                   8726:                                m_CF = 1;
                   8727:                        }
                   8728:                }
                   8729:                break;
1.1.1.22  root     8730:        case 0x2e:
                   8731:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
                   8732:                        SREG(ES) = ERR_TABLE_TOP >> 4;
                   8733:                        i386_load_segment_descriptor(ES);
                   8734:                        REG16(DI) = 0;
                   8735:                }
                   8736:                break;
                   8737:        default:
                   8738:                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));
                   8739:                REG16(AX) = 0x01;
                   8740:                m_CF = 1;
                   8741:                break;
                   8742:        }
                   8743: }
                   8744: 
                   8745: inline void msdos_int_2fh_14h()
                   8746: {
                   8747:        switch(REG8(AL)) {
                   8748:        case 0x00:
1.1.1.25  root     8749:                REG8(AL) = 0xff; // nlsfunc.com is installed
                   8750:                break;
                   8751:        case 0x01:
                   8752:        case 0x03:
                   8753:                REG8(AL) = 0x00;
                   8754:                active_code_page = REG16(BX);
                   8755:                msdos_nls_tables_update();
                   8756:                break;
                   8757:        case 0x02:
                   8758:                REG8(AL) = get_extended_country_info(REG16(BP));
                   8759:                break;
                   8760:        case 0x04:
                   8761:                REG8(AL) = 0x00;
                   8762:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     8763:                break;
                   8764:        default:
                   8765:                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));
                   8766:                REG16(AX) = 0x01;
                   8767:                m_CF = 1;
                   8768:                break;
                   8769:        }
                   8770: }
                   8771: 
                   8772: inline void msdos_int_2fh_15h()
                   8773: {
                   8774:        switch(REG8(AL)) {
                   8775:        case 0x00:
                   8776:                // function not supported, do not clear AX
                   8777:                break;
                   8778:        case 0x0b:
                   8779:                // mscdex.exe is not installed
                   8780:                break;
                   8781:        case 0xff:
                   8782:                // corelcdx is not installed
                   8783:                break;
1.1.1.21  root     8784:        default:
1.1.1.22  root     8785:                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     8786:                REG16(AX) = 0x01;
                   8787:                m_CF = 1;
                   8788:                break;
                   8789:        }
                   8790: }
                   8791: 
1.1       root     8792: inline void msdos_int_2fh_16h()
                   8793: {
                   8794:        switch(REG8(AL)) {
                   8795:        case 0x00:
1.1.1.14  root     8796:                if(no_windows) {
                   8797:                        REG8(AL) = 0;
                   8798:                } else {
1.1       root     8799:                        OSVERSIONINFO osvi;
                   8800:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
                   8801:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                   8802:                        GetVersionEx(&osvi);
                   8803:                        REG8(AL) = osvi.dwMajorVersion;
                   8804:                        REG8(AH) = osvi.dwMinorVersion;
                   8805:                }
                   8806:                break;
1.1.1.22  root     8807:        case 0x0a:
                   8808:                if(!no_windows) {
                   8809:                        OSVERSIONINFO osvi;
                   8810:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
                   8811:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                   8812:                        GetVersionEx(&osvi);
                   8813:                        REG16(AX) = 0x0000;
                   8814:                        REG8(BH) = osvi.dwMajorVersion;
                   8815:                        REG8(BL) = osvi.dwMinorVersion;
                   8816:                        REG16(CX) = 0x0003; // enhanced
                   8817:                }
                   8818:                break;
                   8819:        case 0x0e:
                   8820:        case 0x0f:
                   8821:        case 0x11:
                   8822:        case 0x12:
                   8823:        case 0x13:
                   8824:        case 0x14:
                   8825:        case 0x87:
                   8826:                // function not supported, do not clear AX
                   8827:                break;
1.1.1.14  root     8828:        case 0x80:
                   8829:                Sleep(10);
                   8830:                hardware_update();
                   8831:                REG8(AL) = 0;
                   8832:                break;
1.1.1.22  root     8833:        case 0x8e:
                   8834:                REG16(AX) = 0x00; // failed
                   8835:                break;
1.1.1.20  root     8836:        case 0x8f:
                   8837:                switch(REG8(DH)) {
                   8838:                case 0x00:
                   8839:                case 0x02:
                   8840:                case 0x03:
                   8841:                        REG16(AX) = 0x00;
                   8842:                        break;
                   8843:                case 0x01:
                   8844:                        REG16(AX) = 0x168f;
                   8845:                        break;
                   8846:                }
                   8847:                break;
1.1       root     8848:        default:
1.1.1.22  root     8849:                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));
                   8850:                REG16(AX) = 0x01;
                   8851:                m_CF = 1;
                   8852:                break;
                   8853:        }
                   8854: }
                   8855: 
                   8856: inline void msdos_int_2fh_19h()
                   8857: {
                   8858:        switch(REG8(AL)) {
                   8859:        case 0x00:
                   8860:                // shellb.com is not installed
                   8861:                REG8(AL) = 0x00;
                   8862:                break;
                   8863:        case 0x01:
                   8864:        case 0x02:
                   8865:        case 0x03:
                   8866:        case 0x04:
                   8867:                REG16(AX) = 0x01;
                   8868:                m_CF = 1;
                   8869:                break;
                   8870:        default:
                   8871:                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     8872:                REG16(AX) = 0x01;
1.1.1.3   root     8873:                m_CF = 1;
1.1       root     8874:                break;
                   8875:        }
                   8876: }
                   8877: 
                   8878: inline void msdos_int_2fh_1ah()
                   8879: {
                   8880:        switch(REG8(AL)) {
                   8881:        case 0x00:
                   8882:                // ansi.sys is installed
                   8883:                REG8(AL) = 0xff;
                   8884:                break;
                   8885:        default:
1.1.1.22  root     8886:                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));
                   8887:                REG16(AX) = 0x01;
                   8888:                m_CF = 1;
                   8889:                break;
                   8890:        }
                   8891: }
                   8892: 
                   8893: inline void msdos_int_2fh_1bh()
                   8894: {
                   8895:        switch(REG8(AL)) {
                   8896:        case 0x00:
                   8897:                // xma2ems.sys is not installed
                   8898:                REG8(AL) = 0x00;
                   8899:                break;
                   8900:        default:
                   8901:                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     8902:                REG16(AX) = 0x01;
1.1.1.3   root     8903:                m_CF = 1;
1.1       root     8904:                break;
                   8905:        }
                   8906: }
                   8907: 
                   8908: inline void msdos_int_2fh_43h()
                   8909: {
                   8910:        switch(REG8(AL)) {
                   8911:        case 0x00:
1.1.1.19  root     8912:                // xms is installed ?
                   8913: #ifdef SUPPORT_XMS
                   8914:                if(support_xms) {
                   8915:                        REG8(AL) = 0x80;
                   8916:                } else
                   8917: #endif
                   8918:                REG8(AL) = 0x00;
                   8919:                break;
                   8920:        case 0x10:
                   8921:                SREG(ES) = XMS_TOP >> 4;
                   8922:                i386_load_segment_descriptor(ES);
1.1.1.26  root     8923:                REG16(BX) = 0x15;
1.1       root     8924:                break;
                   8925:        default:
1.1.1.22  root     8926:                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));
                   8927:                REG16(AX) = 0x01;
                   8928:                m_CF = 1;
                   8929:                break;
                   8930:        }
                   8931: }
                   8932: 
                   8933: inline void msdos_int_2fh_46h()
                   8934: {
                   8935:        switch(REG8(AL)) {
                   8936:        case 0x80:
                   8937:                // windows v3.0 is not installed
                   8938:                break;
                   8939:        default:
                   8940:                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));
                   8941:                REG16(AX) = 0x01;
                   8942:                m_CF = 1;
                   8943:                break;
                   8944:        }
                   8945: }
                   8946: 
                   8947: inline void msdos_int_2fh_48h()
                   8948: {
                   8949:        switch(REG8(AL)) {
                   8950:        case 0x00:
                   8951:                // doskey is not installed
                   8952:                break;
                   8953:        case 0x10:
                   8954:                msdos_int_21h_0ah();
                   8955:                REG16(AX) = 0x00;
                   8956:                break;
                   8957:        default:
                   8958:                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     8959:                REG16(AX) = 0x01;
1.1.1.3   root     8960:                m_CF = 1;
1.1       root     8961:                break;
                   8962:        }
                   8963: }
                   8964: 
                   8965: inline void msdos_int_2fh_4ah()
                   8966: {
1.1.1.19  root     8967:        // hma is not installed
1.1       root     8968:        switch(REG8(AL)) {
                   8969:        case 0x01:
                   8970:        case 0x02:
1.1.1.19  root     8971:                // hma is not used
1.1.1.27  root     8972:                REG16(BX) = 0x0000;
1.1.1.3   root     8973:                SREG(ES) = 0xffff;
                   8974:                i386_load_segment_descriptor(ES);
1.1       root     8975:                REG16(DI) = 0xffff;
                   8976:                break;
1.1.1.19  root     8977:        case 0x03:
                   8978:                // unable to allocate
                   8979:                REG16(DI) = 0xffff;
                   8980:                break;
                   8981:        case 0x04:
                   8982:                // function not supported, do not clear AX
                   8983:                break;
1.1.1.22  root     8984:        case 0x10: // smartdrv installation check
                   8985:        case 0x11: // dblspace installation check
                   8986:                break;
                   8987:        default:
                   8988:                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));
                   8989:                REG16(AX) = 0x01;
                   8990:                m_CF = 1;
                   8991:                break;
                   8992:        }
                   8993: }
                   8994: 
                   8995: inline void msdos_int_2fh_4bh()
                   8996: {
                   8997:        switch(REG8(AL)) {
1.1.1.24  root     8998:        case 0x01:
1.1.1.22  root     8999:        case 0x02:
1.1.1.24  root     9000:                // task switcher not loaded
                   9001:                break;
                   9002:        case 0x03:
                   9003:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   9004:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     9005:                break;
1.1       root     9006:        default:
1.1.1.22  root     9007:                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     9008:                REG16(AX) = 0x01;
1.1.1.3   root     9009:                m_CF = 1;
1.1       root     9010:                break;
                   9011:        }
                   9012: }
                   9013: 
                   9014: inline void msdos_int_2fh_4fh()
                   9015: {
                   9016:        switch(REG8(AL)) {
                   9017:        case 0x00:
1.1.1.27  root     9018:                // biling is installed
                   9019:                REG16(AX) = 0x0000;
                   9020:                REG8(DL) = 0x01;        // major version
                   9021:                REG8(DH) = 0x00;        // minor version
1.1       root     9022:                break;
                   9023:        case 0x01:
1.1.1.27  root     9024:                REG16(AX) = 0x0000;
1.1       root     9025:                REG16(BX) = active_code_page;
                   9026:                break;
                   9027:        default:
1.1.1.22  root     9028:                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));
                   9029:                REG16(AX) = 0x01;
                   9030:                m_CF = 1;
                   9031:                break;
                   9032:        }
                   9033: }
                   9034: 
                   9035: inline void msdos_int_2fh_55h()
                   9036: {
                   9037:        switch(REG8(AL)) {
                   9038:        case 0x00:
                   9039:        case 0x01:
                   9040: //             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));
                   9041:                break;
                   9042:        default:
                   9043:                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     9044:                REG16(AX) = 0x01;
1.1.1.3   root     9045:                m_CF = 1;
1.1       root     9046:                break;
                   9047:        }
                   9048: }
                   9049: 
1.1.1.24  root     9050: inline void msdos_int_2fh_adh()
                   9051: {
                   9052:        switch(REG8(AL)) {
                   9053:        case 0x00:
                   9054:                // display.sys is installed
                   9055:                REG8(AL) = 0xff;
                   9056:                REG16(BX) = 0x100; // ???
                   9057:                break;
                   9058:        case 0x01:
                   9059:                active_code_page = REG16(BX);
                   9060:                msdos_nls_tables_update();
                   9061:                REG16(AX) = 0x01;
                   9062:                break;
                   9063:        case 0x02:
                   9064:                REG16(BX) = active_code_page;
                   9065:                break;
                   9066:        case 0x03:
                   9067:                // FIXME
                   9068:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   9069:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   9070:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   9071:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   9072:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   9073:                break;
                   9074:        case 0x80:
                   9075:                break; // keyb.com is not installed
                   9076:        default:
                   9077:                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));
                   9078:                REG16(AX) = 0x01;
                   9079:                m_CF = 1;
                   9080:                break;
                   9081:        }
                   9082: }
                   9083: 
1.1       root     9084: inline void msdos_int_2fh_aeh()
                   9085: {
                   9086:        switch(REG8(AL)) {
                   9087:        case 0x00:
1.1.1.28! root     9088:                // FIXME: we need to check the given command line
        !          9089:                REG8(AL) = 0x00; // the command should be executed as usual
        !          9090: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     9091:                break;
                   9092:        case 0x01:
                   9093:                {
                   9094:                        char command[MAX_PATH];
                   9095:                        memset(command, 0, sizeof(command));
1.1.1.3   root     9096:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     9097:                        
                   9098:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   9099:                        param->env_seg = 0;
                   9100:                        param->cmd_line.w.l = 44;
                   9101:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   9102:                        param->fcb1.w.l = 24;
                   9103:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   9104:                        param->fcb2.w.l = 24;
                   9105:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   9106:                        
                   9107:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   9108:                        
                   9109:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     9110:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   9111:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     9112:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   9113:                        
1.1.1.28! root     9114:                        try {
        !          9115:                                msdos_process_exec(command, param, 0);
        !          9116:                        } catch(...) {
        !          9117:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     9118:                        }
                   9119:                }
                   9120:                break;
                   9121:        default:
1.1.1.22  root     9122:                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     9123:                REG16(AX) = 0x01;
1.1.1.3   root     9124:                m_CF = 1;
1.1       root     9125:                break;
                   9126:        }
                   9127: }
                   9128: 
                   9129: inline void msdos_int_2fh_b7h()
                   9130: {
                   9131:        switch(REG8(AL)) {
                   9132:        case 0x00:
                   9133:                // append is not installed
                   9134:                REG8(AL) = 0;
                   9135:                break;
1.1.1.22  root     9136:        case 0x07:
                   9137:        case 0x11:
                   9138:                break;
1.1       root     9139:        default:
1.1.1.22  root     9140:                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     9141:                REG16(AX) = 0x01;
1.1.1.3   root     9142:                m_CF = 1;
1.1       root     9143:                break;
                   9144:        }
                   9145: }
                   9146: 
1.1.1.24  root     9147: inline void msdos_int_33h_0000h()
                   9148: {
                   9149:        REG16(AX) = 0xffff; // hardware/driver installed
                   9150:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   9151: }
                   9152: 
                   9153: inline void msdos_int_33h_0001h()
                   9154: {
                   9155:        if(!mouse.active) {
                   9156:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9157:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   9158:                }
                   9159:                mouse.active = true;
                   9160:                pic[1].imr &= ~0x10;    // enable irq12
                   9161:        }
                   9162: }
                   9163: 
                   9164: inline void msdos_int_33h_0002h()
                   9165: {
                   9166:        if(mouse.active) {
                   9167:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   9168:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   9169:                }
                   9170:                mouse.active = false;
                   9171:                pic[1].imr |= 0x10;     // disable irq12
                   9172:        }
                   9173: }
                   9174: 
                   9175: inline void msdos_int_33h_0003h()
                   9176: {
                   9177:        REG16(BX) = mouse.get_buttons();
                   9178:        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   9179:        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   9180: }
                   9181: 
                   9182: inline void msdos_int_33h_0005h()
                   9183: {
                   9184:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9185:                int idx = REG16(BX);
                   9186:                REG16(BX) = mouse.buttons[idx].pressed_times;
                   9187:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].pressed_position.x));
                   9188:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].pressed_position.y));
                   9189:                mouse.buttons[idx].pressed_times = 0;
                   9190:        } else {
                   9191:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9192:        }
                   9193:        REG16(AX) = mouse.get_buttons();
                   9194: }
                   9195: 
                   9196: inline void msdos_int_33h_0006h()
                   9197: {
                   9198:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   9199:                int idx = REG16(BX);
                   9200:                REG16(BX) = mouse.buttons[idx].released_times;
                   9201:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].released_position.x));
                   9202:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].released_position.y));
                   9203:                mouse.buttons[idx].released_times = 0;
                   9204:        } else {
                   9205:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   9206:        }
                   9207:        REG16(AX) = mouse.get_buttons();
                   9208: }
                   9209: 
                   9210: inline void msdos_int_33h_0007h()
                   9211: {
                   9212:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   9213:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   9214: }
                   9215: 
                   9216: inline void msdos_int_33h_0008h()
                   9217: {
                   9218:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   9219:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   9220: }
                   9221: 
                   9222: inline void msdos_int_33h_0009h()
                   9223: {
                   9224:        mouse.hot_spot[0] = REG16(BX);
                   9225:        mouse.hot_spot[1] = REG16(CX);
                   9226: }
                   9227: 
                   9228: inline void msdos_int_33h_000bh()
                   9229: {
                   9230:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   9231:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   9232:        mouse.prev_position.x = mouse.position.x;
                   9233:        mouse.prev_position.y = mouse.position.y;
                   9234:        REG16(CX) = dx;
                   9235:        REG16(DX) = dy;
                   9236: }
                   9237: 
                   9238: inline void msdos_int_33h_000ch()
                   9239: {
                   9240:        mouse.call_mask = REG16(CX);
                   9241:        mouse.call_addr.w.l = REG16(DX);
                   9242:        mouse.call_addr.w.h = SREG(ES);
                   9243: }
                   9244: 
                   9245: inline void msdos_int_33h_000fh()
                   9246: {
                   9247:        mouse.mickey.x = REG16(CX);
                   9248:        mouse.mickey.y = REG16(DX);
                   9249: }
                   9250: 
                   9251: inline void msdos_int_33h_0011h()
                   9252: {
                   9253:        REG16(AX) = 0xffff;
                   9254:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   9255: }
                   9256: 
                   9257: inline void msdos_int_33h_0014h()
                   9258: {
                   9259:        UINT16 old_mask = mouse.call_mask;
                   9260:        UINT16 old_ofs = mouse.call_addr.w.l;
                   9261:        UINT16 old_seg = mouse.call_addr.w.h;
                   9262:        
                   9263:        mouse.call_mask = REG16(CX);
                   9264:        mouse.call_addr.w.l = REG16(DX);
                   9265:        mouse.call_addr.w.h = SREG(ES);
                   9266:        
                   9267:        REG16(CX) = old_mask;
                   9268:        REG16(DX) = old_ofs;
                   9269:        SREG(ES) = old_seg;
                   9270:        i386_load_segment_descriptor(ES);
                   9271: }
                   9272: 
                   9273: inline void msdos_int_33h_0015h()
                   9274: {
                   9275:        REG16(BX) = sizeof(mouse);
                   9276: }
                   9277: 
                   9278: inline void msdos_int_33h_0016h()
                   9279: {
                   9280:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   9281: }
                   9282: 
                   9283: inline void msdos_int_33h_0017h()
                   9284: {
                   9285:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   9286: }
                   9287: 
                   9288: inline void msdos_int_33h_001ah()
                   9289: {
                   9290:        mouse.sensitivity[0] = REG16(BX);
                   9291:        mouse.sensitivity[1] = REG16(CX);
                   9292:        mouse.sensitivity[2] = REG16(DX);
                   9293: }
                   9294: 
                   9295: inline void msdos_int_33h_001bh()
                   9296: {
                   9297:        REG16(BX) = mouse.sensitivity[0];
                   9298:        REG16(CX) = mouse.sensitivity[1];
                   9299:        REG16(DX) = mouse.sensitivity[2];
                   9300: }
                   9301: 
                   9302: inline void msdos_int_33h_001dh()
                   9303: {
                   9304:        mouse.display_page = REG16(BX);
                   9305: }
                   9306: 
                   9307: inline void msdos_int_33h_001eh()
                   9308: {
                   9309:        REG16(BX) = mouse.display_page;
                   9310: }
                   9311: 
                   9312: inline void msdos_int_33h_0021h()
                   9313: {
                   9314:        REG16(AX) = 0xffff;
                   9315:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   9316: }
                   9317: 
                   9318: inline void msdos_int_33h_0022h()
                   9319: {
                   9320:        mouse.language = REG16(BX);
                   9321: }
                   9322: 
                   9323: inline void msdos_int_33h_0023h()
                   9324: {
                   9325:        REG16(BX) = mouse.language;
                   9326: }
                   9327: 
                   9328: inline void msdos_int_33h_0024h()
                   9329: {
                   9330:        REG16(BX) = 0x0805; // V8.05
                   9331:        REG16(CX) = 0x0400; // PS/2
                   9332: }
                   9333: 
                   9334: inline void msdos_int_33h_0026h()
                   9335: {
                   9336:        REG16(BX) = 0x0000;
                   9337:        REG16(CX) = mouse.max_position.x;
                   9338:        REG16(DX) = mouse.max_position.y;
                   9339: }
                   9340: 
                   9341: inline void msdos_int_33h_002ah()
                   9342: {
                   9343:        REG16(AX) = mouse.active ? 0 : 0xffff;
                   9344:        REG16(BX) = mouse.hot_spot[0];
                   9345:        REG16(CX) = mouse.hot_spot[1];
                   9346:        REG16(DX) = 4; // PS/2
                   9347: }
                   9348: 
                   9349: inline void msdos_int_33h_0031h()
                   9350: {
                   9351:        REG16(AX) = mouse.min_position.x;
                   9352:        REG16(BX) = mouse.min_position.y;
                   9353:        REG16(CX) = mouse.max_position.x;
                   9354:        REG16(DX) = mouse.max_position.y;
                   9355: }
                   9356: 
                   9357: inline void msdos_int_33h_0032h()
                   9358: {
                   9359:        REG16(AX) = 0;
                   9360: //     REG16(AX) |= 0x8000; // 0025h
                   9361:        REG16(AX) |= 0x4000; // 0026h
                   9362: //     REG16(AX) |= 0x2000; // 0027h
                   9363: //     REG16(AX) |= 0x1000; // 0028h
                   9364: //     REG16(AX) |= 0x0800; // 0029h
                   9365:        REG16(AX) |= 0x0400; // 002ah
                   9366: //     REG16(AX) |= 0x0200; // 002bh
                   9367: //     REG16(AX) |= 0x0100; // 002ch
                   9368: //     REG16(AX) |= 0x0080; // 002dh
                   9369: //     REG16(AX) |= 0x0040; // 002eh
                   9370:        REG16(AX) |= 0x0020; // 002fh
                   9371: //     REG16(AX) |= 0x0010; // 0030h
                   9372:        REG16(AX) |= 0x0008; // 0031h
                   9373:        REG16(AX) |= 0x0004; // 0032h
                   9374: //     REG16(AX) |= 0x0002; // 0033h
                   9375: //     REG16(AX) |= 0x0001; // 0034h
                   9376: }
                   9377: 
1.1.1.19  root     9378: inline void msdos_int_67h_40h()
                   9379: {
                   9380:        if(!support_ems) {
                   9381:                REG8(AH) = 0x84;
                   9382:        } else {
                   9383:                REG8(AH) = 0x00;
                   9384:        }
                   9385: }
                   9386: 
                   9387: inline void msdos_int_67h_41h()
                   9388: {
                   9389:        if(!support_ems) {
                   9390:                REG8(AH) = 0x84;
                   9391:        } else {
                   9392:                REG8(AH) = 0x00;
                   9393:                REG16(BX) = EMS_TOP >> 4;
                   9394:        }
                   9395: }
                   9396: 
                   9397: inline void msdos_int_67h_42h()
                   9398: {
                   9399:        if(!support_ems) {
                   9400:                REG8(AH) = 0x84;
                   9401:        } else {
                   9402:                REG8(AH) = 0x00;
                   9403:                REG16(BX) = free_ems_pages;
                   9404:                REG16(DX) = MAX_EMS_PAGES;
                   9405:        }
                   9406: }
                   9407: 
                   9408: inline void msdos_int_67h_43h()
                   9409: {
                   9410:        if(!support_ems) {
                   9411:                REG8(AH) = 0x84;
                   9412:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   9413:                REG8(AH) = 0x87;
                   9414:        } else if(REG16(BX) > free_ems_pages) {
                   9415:                REG8(AH) = 0x88;
                   9416:        } else if(REG16(BX) == 0) {
                   9417:                REG8(AH) = 0x89;
                   9418:        } else {
                   9419:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9420:                        if(!ems_handles[i].allocated) {
                   9421:                                ems_allocate_pages(i, REG16(BX));
                   9422:                                REG8(AH) = 0x00;
                   9423:                                REG16(DX) = i;
                   9424:                                return;
                   9425:                        }
                   9426:                }
                   9427:                REG8(AH) = 0x85;
                   9428:        }
                   9429: }
                   9430: 
                   9431: inline void msdos_int_67h_44h()
                   9432: {
                   9433:        if(!support_ems) {
                   9434:                REG8(AH) = 0x84;
                   9435:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9436:                REG8(AH) = 0x83;
                   9437:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   9438:                REG8(AH) = 0x8a;
                   9439: //     } else if(!(REG8(AL) < 4)) {
                   9440: //             REG8(AH) = 0x8b;
                   9441:        } else if(REG16(BX) == 0xffff) {
                   9442:                ems_unmap_page(REG8(AL) & 3);
                   9443:                REG8(AH) = 0x00;
                   9444:        } else {
                   9445:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   9446:                REG8(AH) = 0x00;
                   9447:        }
                   9448: }
                   9449: 
                   9450: inline void msdos_int_67h_45h()
                   9451: {
                   9452:        if(!support_ems) {
                   9453:                REG8(AH) = 0x84;
                   9454:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9455:                REG8(AH) = 0x83;
                   9456:        } else {
                   9457:                ems_release_pages(REG16(DX));
                   9458:                REG8(AH) = 0x00;
                   9459:        }
                   9460: }
                   9461: 
                   9462: inline void msdos_int_67h_46h()
                   9463: {
                   9464:        if(!support_ems) {
                   9465:                REG8(AH) = 0x84;
                   9466:        } else {
                   9467:                REG16(AX) = 0x0032; // EMS 3.2
                   9468: //             REG16(AX) = 0x0040; // EMS 4.0
                   9469:        }
                   9470: }
                   9471: 
                   9472: inline void msdos_int_67h_47h()
                   9473: {
                   9474:        // NOTE: the map data should be stored in the specified ems page, not process data
                   9475:        process_t *process = msdos_process_info_get(current_psp);
                   9476:        
                   9477:        if(!support_ems) {
                   9478:                REG8(AH) = 0x84;
                   9479: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9480: //             REG8(AH) = 0x83;
                   9481:        } else if(process->ems_pages_stored) {
                   9482:                REG8(AH) = 0x8d;
                   9483:        } else {
                   9484:                for(int i = 0; i < 4; i++) {
                   9485:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   9486:                        process->ems_pages[i].page   = ems_pages[i].page;
                   9487:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   9488:                }
                   9489:                process->ems_pages_stored = true;
                   9490:                REG8(AH) = 0x00;
                   9491:        }
                   9492: }
                   9493: 
                   9494: inline void msdos_int_67h_48h()
                   9495: {
                   9496:        // NOTE: the map data should be restored from the specified ems page, not process data
                   9497:        process_t *process = msdos_process_info_get(current_psp);
                   9498:        
                   9499:        if(!support_ems) {
                   9500:                REG8(AH) = 0x84;
                   9501: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9502: //             REG8(AH) = 0x83;
                   9503:        } else if(!process->ems_pages_stored) {
                   9504:                REG8(AH) = 0x8e;
                   9505:        } else {
                   9506:                for(int i = 0; i < 4; i++) {
                   9507:                        if(process->ems_pages[i].mapped) {
                   9508:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   9509:                        } else {
                   9510:                                ems_unmap_page(i);
                   9511:                        }
                   9512:                }
                   9513:                process->ems_pages_stored = false;
                   9514:                REG8(AH) = 0x00;
                   9515:        }
                   9516: }
                   9517: 
                   9518: inline void msdos_int_67h_4bh()
                   9519: {
                   9520:        if(!support_ems) {
                   9521:                REG8(AH) = 0x84;
                   9522:        } else {
                   9523:                REG8(AH) = 0x00;
                   9524:                REG16(BX) = 0;
                   9525:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9526:                        if(ems_handles[i].allocated) {
                   9527:                                REG16(BX)++;
                   9528:                        }
                   9529:                }
                   9530:        }
                   9531: }
                   9532: 
                   9533: inline void msdos_int_67h_4ch()
                   9534: {
                   9535:        if(!support_ems) {
                   9536:                REG8(AH) = 0x84;
                   9537:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9538:                REG8(AH) = 0x83;
                   9539:        } else {
                   9540:                REG8(AH) = 0x00;
                   9541:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   9542:        }
                   9543: }
                   9544: 
                   9545: inline void msdos_int_67h_4dh()
                   9546: {
                   9547:        if(!support_ems) {
                   9548:                REG8(AH) = 0x84;
                   9549:        } else {
                   9550:                REG8(AH) = 0x00;
                   9551:                REG16(BX) = 0;
                   9552:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9553:                        if(ems_handles[i].allocated) {
                   9554:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   9555:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   9556:                                REG16(BX)++;
                   9557:                        }
                   9558:                }
                   9559:        }
                   9560: }
                   9561: 
1.1.1.20  root     9562: inline void msdos_int_67h_4eh()
                   9563: {
                   9564:        if(!support_ems) {
                   9565:                REG8(AH) = 0x84;
                   9566:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   9567:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   9568:                        // save page map
                   9569:                        for(int i = 0; i < 4; i++) {
                   9570:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   9571:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   9572:                        }
                   9573:                }
                   9574:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   9575:                        // restore page map
                   9576:                        for(int i = 0; i < 4; i++) {
                   9577:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   9578:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   9579:                                
                   9580:                                if(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   9581:                                        ems_map_page(i, handle, page);
                   9582:                                } else {
                   9583:                                        ems_unmap_page(i);
                   9584:                                }
                   9585:                        }
                   9586:                }
                   9587:                REG8(AH) = 0x00;
                   9588:        } else if(REG8(AL) == 0x03) {
                   9589:                REG8(AH) = 0x00;
1.1.1.21  root     9590:                REG8(AL) = 4 * 4;
                   9591:        } else {
1.1.1.22  root     9592:                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     9593:                REG8(AH) = 0x8f;
                   9594:        }
                   9595: }
                   9596: 
                   9597: inline void msdos_int_67h_4fh()
                   9598: {
                   9599:        if(!support_ems) {
                   9600:                REG8(AH) = 0x84;
                   9601:        } else if(REG8(AL) == 0x00) {
                   9602:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   9603:                
                   9604:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   9605:                for(int i = 0; i < count; i++) {
                   9606:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   9607:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   9608:                        
                   9609: //                     if(!(physical < 4)) {
                   9610: //                             REG8(AH) = 0x8b;
                   9611: //                             return;
                   9612: //                     }
                   9613:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   9614:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   9615:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   9616:                }
                   9617:                REG8(AH) = 0x00;
                   9618:        } else if(REG8(AL) == 0x01) {
                   9619:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   9620:                
                   9621:                for(int i = 0; i < count; i++) {
                   9622:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   9623:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   9624:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   9625:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   9626:                        
                   9627: //                     if(!(physical < 4)) {
                   9628: //                             REG8(AH) = 0x8b;
                   9629: //                             return;
                   9630: //                     } else
                   9631:                        if(!(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
                   9632:                                REG8(AH) = 0x83;
                   9633:                                return;
                   9634:                        } else if(logical == 0xffff) {
                   9635:                                ems_unmap_page(physical & 3);
                   9636:                        } else if(logical < ems_handles[handle].pages) {
                   9637:                                ems_map_page(physical & 3, handle, logical);
                   9638:                        } else {
                   9639:                                REG8(AH) = 0x8a;
                   9640:                                return;
                   9641:                        }
                   9642:                }
                   9643:                REG8(AH) = 0x00;
                   9644:        } else if(REG8(AL) == 0x02) {
                   9645:                REG8(AH) = 0x00;
                   9646:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     9647:        } else {
1.1.1.22  root     9648:                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     9649:                REG8(AH) = 0x8f;
                   9650:        }
                   9651: }
                   9652: 
                   9653: inline void msdos_int_67h_50h()
                   9654: {
                   9655:        if(!support_ems) {
                   9656:                REG8(AH) = 0x84;
                   9657:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9658:                REG8(AH) = 0x83;
                   9659:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9660:                for(int i = 0; i < REG16(CX); i++) {
                   9661:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   9662:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   9663:                        
                   9664:                        if(REG8(AL) == 0x01) {
                   9665:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   9666:                        }
                   9667: //                     if(!(physical < 4)) {
                   9668: //                             REG8(AH) = 0x8b;
                   9669: //                             return;
                   9670: //                     } else
                   9671:                        if(logical == 0xffff) {
                   9672:                                ems_unmap_page(physical & 3);
                   9673:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   9674:                                ems_map_page(physical & 3, REG16(DX), logical);
                   9675:                        } else {
                   9676:                                REG8(AH) = 0x8a;
                   9677:                                return;
                   9678:                        }
                   9679:                }
                   9680:                REG8(AH) = 0x00;
                   9681:        } else {
1.1.1.22  root     9682:                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     9683:                REG8(AH) = 0x8f;
                   9684:        }
                   9685: }
                   9686: 
1.1.1.19  root     9687: inline void msdos_int_67h_51h()
                   9688: {
                   9689:        if(!support_ems) {
                   9690:                REG8(AH) = 0x84;
                   9691:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9692:                REG8(AH) = 0x83;
                   9693:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   9694:                REG8(AH) = 0x87;
                   9695:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   9696:                REG8(AH) = 0x88;
                   9697:        } else {
                   9698:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   9699:                REG8(AH) = 0x00;
                   9700:        }
                   9701: }
                   9702: 
1.1.1.20  root     9703: inline void msdos_int_67h_52h()
                   9704: {
                   9705:        if(!support_ems) {
                   9706:                REG8(AH) = 0x84;
                   9707:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9708:                REG8(AH) = 0x83;
                   9709:        } else if(REG8(AL) == 0x00) {
                   9710:                REG8(AL) = 0x00; // handle is volatile
                   9711:                REG8(AH) = 0x00;
                   9712:        } else if(REG8(AL) == 0x01) {
                   9713:                if(REG8(BL) == 0x00) {
                   9714:                        REG8(AH) = 0x00;
                   9715:                } else {
                   9716:                        REG8(AH) = 0x90; // undefined attribute type
                   9717:                }
                   9718:        } else if(REG8(AL) == 0x02) {
                   9719:                REG8(AL) = 0x00; // only volatile handles supported
                   9720:                REG8(AH) = 0x00;
                   9721:        } else {
1.1.1.22  root     9722:                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     9723:                REG8(AH) = 0x8f;
                   9724:        }
                   9725: }
                   9726: 
1.1.1.19  root     9727: inline void msdos_int_67h_53h()
                   9728: {
                   9729:        if(!support_ems) {
                   9730:                REG8(AH) = 0x84;
                   9731:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9732:                REG8(AH) = 0x83;
                   9733:        } else if(REG8(AL) == 0x00) {
                   9734:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   9735:                REG8(AH) = 0x00;
                   9736:        } else if(REG8(AL) == 0x01) {
                   9737:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9738:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   9739:                                REG8(AH) = 0xa1;
                   9740:                                return;
                   9741:                        }
                   9742:                }
                   9743:                REG8(AH) = 0x00;
                   9744:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   9745:        } else {
1.1.1.22  root     9746:                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     9747:                REG8(AH) = 0x8f;
1.1.1.19  root     9748:        }
                   9749: }
                   9750: 
                   9751: inline void msdos_int_67h_54h()
                   9752: {
                   9753:        if(!support_ems) {
                   9754:                REG8(AH) = 0x84;
                   9755:        } else if(REG8(AL) == 0x00) {
                   9756:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9757:                        if(ems_handles[i].allocated) {
                   9758:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   9759:                        } else {
                   9760:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   9761:                        }
                   9762:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   9763:                }
                   9764:                REG8(AH) = 0x00;
                   9765:                REG8(AL) = MAX_EMS_HANDLES;
                   9766:        } else if(REG8(AL) == 0x01) {
                   9767:                REG8(AH) = 0xa0; // not found
                   9768:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9769:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   9770:                                REG8(AH) = 0x00;
                   9771:                                REG16(DX) = i;
                   9772:                                break;
                   9773:                        }
                   9774:                }
                   9775:        } else if(REG8(AL) == 0x02) {
                   9776:                REG8(AH) = 0x00;
                   9777:                REG16(BX) = MAX_EMS_HANDLES;
                   9778:        } else {
1.1.1.22  root     9779:                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     9780:                REG8(AH) = 0x8f;
                   9781:        }
                   9782: }
                   9783: 
                   9784: inline void msdos_int_67h_57h_tmp()
                   9785: {
                   9786:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   9787:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   9788:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   9789:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   9790:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   9791:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   9792:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   9793:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   9794:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   9795:        
                   9796:        UINT8 *src_buffer, *dest_buffer;
                   9797:        UINT32 src_addr, dest_addr;
                   9798:        UINT32 src_addr_max, dest_addr_max;
                   9799:        
                   9800:        if(src_type == 0) {
                   9801:                src_buffer = mem;
                   9802:                src_addr = (src_seg << 4) + src_ofs;
                   9803:                src_addr_max = MAX_MEM;
                   9804:        } else {
                   9805:                if(!(src_handle < MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
                   9806:                        REG8(AH) = 0x83;
                   9807:                        return;
                   9808:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   9809:                        REG8(AH) = 0x8a;
                   9810:                        return;
                   9811:                }
                   9812:                src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   9813:                src_addr = src_ofs;
                   9814:                src_addr_max = 0x4000;
                   9815:        }
                   9816:        if(dest_type == 0) {
                   9817:                dest_buffer = mem;
                   9818:                dest_addr = (dest_seg << 4) + dest_ofs;
                   9819:                dest_addr_max = MAX_MEM;
                   9820:        } else {
                   9821:                if(!(dest_handle < MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
                   9822:                        REG8(AH) = 0x83;
                   9823:                        return;
                   9824:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   9825:                        REG8(AH) = 0x8a;
                   9826:                        return;
                   9827:                }
                   9828:                dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   9829:                dest_addr = dest_ofs;
                   9830:                dest_addr_max = 0x4000;
                   9831:        }
                   9832:        for(int i = 0; i < copy_length; i++) {
                   9833:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   9834:                        if(REG8(AL) == 0x00) {
                   9835:                                dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   9836:                        } else if(REG8(AL) == 0x01) {
                   9837:                                UINT8 tmp = dest_buffer[dest_addr];
                   9838:                                dest_buffer[dest_addr++] = src_buffer[src_addr];
                   9839:                                src_buffer[src_addr++] = tmp;
                   9840:                        }
                   9841:                } else {
                   9842:                        REG8(AH) = 0x93;
                   9843:                        return;
                   9844:                }
                   9845:        }
                   9846:        REG8(AH) = 0x80;
                   9847: }
                   9848: 
                   9849: inline void msdos_int_67h_57h()
                   9850: {
                   9851:        if(!support_ems) {
                   9852:                REG8(AH) = 0x84;
                   9853:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9854:                struct {
                   9855:                        UINT16 handle;
                   9856:                        UINT16 page;
                   9857:                        bool mapped;
                   9858:                } tmp_pages[4];
                   9859:                
                   9860:                // unmap pages to copy memory data to ems buffer
                   9861:                for(int i = 0; i < 4; i++) {
                   9862:                        tmp_pages[i].handle = ems_pages[i].handle;
                   9863:                        tmp_pages[i].page   = ems_pages[i].page;
                   9864:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   9865:                        ems_unmap_page(i);
                   9866:                }
                   9867:                
                   9868:                // run move/exchange operation
                   9869:                msdos_int_67h_57h_tmp();
                   9870:                
                   9871:                // restore unmapped pages
                   9872:                for(int i = 0; i < 4; i++) {
                   9873:                        if(tmp_pages[i].mapped) {
                   9874:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   9875:                        }
                   9876:                }
                   9877:        } else {
1.1.1.22  root     9878:                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     9879:                REG8(AH) = 0x8f;
                   9880:        }
                   9881: }
                   9882: 
                   9883: inline void msdos_int_67h_58h()
                   9884: {
                   9885:        if(!support_ems) {
                   9886:                REG8(AH) = 0x84;
                   9887:        } else if(REG8(AL) == 0x00) {
                   9888:                for(int i = 0; i < 4; i++) {
                   9889:                        *(UINT16 *)(mem +SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   9890:                        *(UINT16 *)(mem +SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
                   9891:                }
                   9892:                REG8(AH) = 0x00;
                   9893:                REG16(CX) = 4;
                   9894:        } else if(REG8(AL) == 0x01) {
                   9895:                REG8(AH) = 0x00;
                   9896:                REG16(CX) = 4;
                   9897:        } else {
1.1.1.22  root     9898:                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     9899:                REG8(AH) = 0x8f;
                   9900:        }
                   9901: }
                   9902: 
                   9903: inline void msdos_int_67h_5ah()
                   9904: {
                   9905:        if(!support_ems) {
1.1.1.19  root     9906:                REG8(AH) = 0x84;
1.1.1.20  root     9907:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   9908:                REG8(AH) = 0x87;
                   9909:        } else if(REG16(BX) > free_ems_pages) {
                   9910:                REG8(AH) = 0x88;
                   9911: //     } else if(REG16(BX) == 0) {
                   9912: //             REG8(AH) = 0x89;
                   9913:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9914:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9915:                        if(!ems_handles[i].allocated) {
                   9916:                                ems_allocate_pages(i, REG16(BX));
                   9917:                                REG8(AH) = 0x00;
                   9918:                                REG16(DX) = i;
                   9919:                                return;
                   9920:                        }
                   9921:                }
                   9922:                REG8(AH) = 0x85;
                   9923:        } else {
1.1.1.22  root     9924:                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     9925:                REG8(AH) = 0x8f;
1.1.1.19  root     9926:        }
                   9927: }
                   9928: 
                   9929: #ifdef SUPPORT_XMS
                   9930: 
1.1.1.26  root     9931: inline void msdos_xms_init()
                   9932: {
                   9933:        memset(xms_handles, 0, sizeof(xms_handles));
                   9934:        xms_a20_local_enb_count = 0;
                   9935: }
                   9936: 
1.1.1.19  root     9937: inline void msdos_call_xms_00h()
                   9938: {
                   9939:        REG16(AX) = 0x0270; // V2.70
                   9940:        REG16(BX) = 0x0000;
                   9941: //     REG16(DX) = 0x0000; // hma does not exist
                   9942:        REG16(DX) = 0x0001; // hma does exist
                   9943: }
                   9944: 
                   9945: inline void msdos_call_xms_01h()
                   9946: {
                   9947:        REG16(AX) = 0x0000;
                   9948: //     REG8(BL) = 0x90; // hma does not exist
                   9949:        REG8(BL) = 0x91; // hma is already used
                   9950: }
                   9951: 
                   9952: inline void msdos_call_xms_02h()
                   9953: {
                   9954:        REG16(AX) = 0x0000;
                   9955: //     REG8(BL) = 0x90; // hma does not exist
                   9956:        REG8(BL) = 0x91; // hma is already used
                   9957: }
                   9958: 
                   9959: inline void msdos_call_xms_03h()
                   9960: {
                   9961:        i386_set_a20_line(1);
                   9962:        REG16(AX) = 0x0001;
                   9963:        REG8(BL) = 0x00;
                   9964: }
                   9965: 
                   9966: inline void msdos_call_xms_04h()
                   9967: {
1.1.1.21  root     9968:        i386_set_a20_line(0);
                   9969:        REG16(AX) = 0x0001;
                   9970:        REG8(BL) = 0x00;
1.1.1.19  root     9971: }
                   9972: 
                   9973: inline void msdos_call_xms_05h()
                   9974: {
                   9975:        i386_set_a20_line(1);
                   9976:        REG16(AX) = 0x0001;
                   9977:        REG8(BL) = 0x00;
1.1.1.21  root     9978:        xms_a20_local_enb_count++;
1.1.1.19  root     9979: }
                   9980: 
                   9981: void msdos_call_xms_06h()
                   9982: {
1.1.1.21  root     9983:        if(xms_a20_local_enb_count > 0) {
                   9984:                xms_a20_local_enb_count--;
                   9985:        }
                   9986:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     9987:                i386_set_a20_line(0);
1.1.1.21  root     9988:        }
                   9989:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     9990:                REG16(AX) = 0x0000;
                   9991:                REG8(BL) = 0x94;
1.1.1.21  root     9992:        } else {
                   9993:                REG16(AX) = 0x0001;
                   9994:                REG8(BL) = 0x00;
1.1.1.19  root     9995:        }
                   9996: }
                   9997: 
                   9998: inline void msdos_call_xms_07h()
                   9999: {
                   10000:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   10001:        REG8(BL) = 0x00;
                   10002: }
                   10003: 
                   10004: inline void msdos_call_xms_08h()
                   10005: {
                   10006:        REG16(AX) = REG16(DX) = 0x0000;
                   10007:        
                   10008:        int mcb_seg = EMB_TOP >> 4;
                   10009:        
                   10010:        while(1) {
                   10011:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10012:                
                   10013:                if(mcb->psp == 0) {
                   10014:                        if(REG16(AX) < mcb->size_kb()) {
                   10015:                                REG16(AX) = mcb->size_kb();
                   10016:                        }
                   10017:                        REG16(DX) += mcb->size_kb();
                   10018:                }
                   10019:                if(mcb->mz == 'Z') {
                   10020:                        break;
                   10021:                }
                   10022:                mcb_seg += 1 + mcb->paragraphs();
                   10023:        }
                   10024:        
                   10025:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   10026:                REG8(BL) = 0xa0;
                   10027:        } else {
                   10028:                REG8(BL) = 0x00;
                   10029:        }
                   10030: }
                   10031: 
                   10032: inline void msdos_call_xms_09h()
                   10033: {
                   10034:        for(int i = 1; i <= MAX_XMS_HANDLES; i++) {
                   10035:                if(!xms_handles[i].allocated) {
                   10036:                        if((xms_handles[i].seg = msdos_mem_alloc(EMB_TOP >> 4, REG16(DX) * 64, 0)) != -1) {
                   10037:                                xms_handles[i].size_kb = REG16(DX);
                   10038:                                xms_handles[i].lock = 0;
                   10039:                                xms_handles[i].allocated = true;
                   10040:                                
                   10041:                                REG16(AX) = 0x0001;
                   10042:                                REG16(DX) = i;
                   10043:                                REG8(BL) = 0x00;
                   10044:                        } else {
                   10045:                                REG16(AX) = REG16(DX) = 0x0000;
                   10046:                                REG8(BL) = 0xa0;
                   10047:                        }
                   10048:                        return;
                   10049:                }
                   10050:        }
                   10051:        REG16(AX) = REG16(DX) = 0x0000;
                   10052:        REG8(BL) = 0xa1;
                   10053: }
                   10054: 
                   10055: inline void msdos_call_xms_0ah()
                   10056: {
                   10057:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   10058:                REG16(AX) = 0x0000;
                   10059:                REG8(BL) = 0xa2;
                   10060:        } else if(xms_handles[REG16(DX)].lock > 0) {
                   10061:                REG16(AX) = 0x0000;
                   10062:                REG8(BL) = 0xab;
                   10063:        } else {
                   10064:                msdos_mem_free(xms_handles[REG16(DX)].seg);
                   10065:                xms_handles[REG16(DX)].allocated = false;
                   10066:                
                   10067:                REG16(AX) = 0x0001;
                   10068:                REG8(BL) = 0x00;
                   10069:        }
                   10070: }
                   10071: 
                   10072: inline void msdos_call_xms_0bh()
                   10073: {
                   10074:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   10075:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   10076:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   10077:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   10078:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   10079:        
                   10080:        UINT8 *src_buffer, *dest_buffer;
                   10081:        UINT32 src_addr_max, dest_addr_max;
                   10082:        
                   10083:        if(src_handle == 0) {
                   10084:                src_buffer = mem;
                   10085:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   10086:                src_addr_max = MAX_MEM;
                   10087:        } else {
                   10088:                if(!(src_handle >= 1 && src_handle <= MAX_XMS_HANDLES && xms_handles[src_handle].allocated)) {
                   10089:                        REG16(AX) = 0x0000;
                   10090:                        REG8(BL) = 0xa3;
                   10091:                        return;
                   10092:                }
                   10093:                src_buffer = mem + (xms_handles[src_handle].seg << 4);
                   10094:                src_addr_max = xms_handles[src_handle].size_kb * 1024;
                   10095:        }
                   10096:        if(dest_handle == 0) {
                   10097:                dest_buffer = mem;
                   10098:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   10099:                dest_addr_max = MAX_MEM;
                   10100:        } else {
                   10101:                if(!(dest_handle >= 1 && dest_handle <= MAX_XMS_HANDLES && xms_handles[dest_handle].allocated)) {
                   10102:                        REG16(AX) = 0x0000;
                   10103:                        REG8(BL) = 0xa5;
                   10104:                        return;
                   10105:                }
                   10106:                dest_buffer = mem + (xms_handles[dest_handle].seg << 4);
                   10107:                dest_addr_max = xms_handles[dest_handle].size_kb * 1024;
                   10108:        }
                   10109:        for(int i = 0; i < copy_length; i++) {
                   10110:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   10111:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   10112:                } else {
                   10113:                        break;
                   10114:                }
                   10115:        }
                   10116:        REG16(AX) = 0x0001;
                   10117:        REG8(BL) = 0x00;
                   10118: }
                   10119: 
                   10120: inline void msdos_call_xms_0ch()
                   10121: {
                   10122:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   10123:                REG16(AX) = 0x0000;
                   10124:                REG8(BL) = 0xa2;
                   10125:        } else {
                   10126:                xms_handles[REG16(DX)].lock++;
                   10127:                REG16(AX) = 0x0001;
                   10128:                REG8(BL) = 0x00;
                   10129:                UINT32 addr = xms_handles[REG16(DX)].seg << 4;
                   10130:                REG16(DX) = (addr >> 16) & 0xffff;
                   10131:                REG16(BX) = (addr      ) & 0xffff;
                   10132:        }
                   10133: }
                   10134: 
                   10135: inline void msdos_call_xms_0dh()
                   10136: {
                   10137:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   10138:                REG16(AX) = 0x0000;
                   10139:                REG8(BL) = 0xa2;
                   10140:        } else if(!(xms_handles[REG16(DX)].lock > 0)) {
                   10141:                REG16(AX) = 0x0000;
                   10142:                REG8(BL) = 0xaa;
                   10143:        } else {
                   10144:                xms_handles[REG16(DX)].lock--;
                   10145:                REG16(AX) = 0x0001;
                   10146:                REG8(BL) = 0x00;
                   10147:        }
                   10148: }
                   10149: 
                   10150: inline void msdos_call_xms_0eh()
                   10151: {
                   10152:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   10153:                REG16(AX) = 0x0000;
                   10154:                REG8(BL) = 0xa2;
                   10155:        } else {
                   10156:                REG16(AX) = 0x0001;
                   10157:                REG8(BH) = xms_handles[REG16(DX)].lock;
                   10158:                REG8(BL) = 0x00;
                   10159:                for(int i = 1; i <= MAX_XMS_HANDLES; i++) {
                   10160:                        if(!xms_handles[i].allocated) {
                   10161:                                REG8(BL)++;
                   10162:                        }
                   10163:                }
                   10164:                REG16(DX) = xms_handles[REG16(DX)].size_kb;
                   10165:        }
                   10166: }
                   10167: 
                   10168: inline void msdos_call_xms_0fh()
                   10169: {
                   10170:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   10171:                REG16(AX) = 0x0000;
                   10172:                REG8(BL) = 0xa2;
                   10173:        } else if(xms_handles[REG16(DX)].lock > 0) {
                   10174:                REG16(AX) = 0x0000;
                   10175:                REG8(BL) = 0xab;
                   10176:        } else if(msdos_mem_realloc(xms_handles[REG16(DX)].seg, REG16(BX) * 64, NULL)) {
                   10177:                REG16(AX) = 0x0000;
                   10178:                REG8(BL) = 0xa0;
                   10179:        } else {
                   10180:                REG16(AX) = 0x0001;
                   10181:                REG8(BL) = 0x00;
                   10182:        }
                   10183: }
                   10184: 
                   10185: inline void msdos_call_xms_10h()
                   10186: {
                   10187:        int seg;
                   10188:        
                   10189:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   10190:                REG16(AX) = 0x0001;
                   10191:                REG16(BX) = seg;
                   10192:        } else {
                   10193:                REG16(AX) = 0x0000;
                   10194:                REG8(BL) = 0xb0;
                   10195:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   10196:        }
                   10197: }
                   10198: 
                   10199: inline void msdos_call_xms_11h()
                   10200: {
                   10201:        int mcb_seg = REG16(DX) - 1;
                   10202:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10203:        
                   10204:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10205:                msdos_mem_free(REG16(DX));
                   10206:                REG16(AX) = 0x0001;
                   10207:                REG8(BL) = 0x00;
                   10208:        } else {
                   10209:                REG16(AX) = 0x0000;
                   10210:                REG8(BL) = 0xb2;
                   10211:        }
                   10212: }
                   10213: 
                   10214: inline void msdos_call_xms_12h()
                   10215: {
                   10216:        int mcb_seg = REG16(DX) - 1;
                   10217:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10218:        int max_paragraphs;
                   10219:        
                   10220:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10221:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   10222:                        REG16(AX) = 0x0001;
                   10223:                        REG8(BL) = 0x00;
                   10224:                } else {
                   10225:                        REG16(AX) = 0x0000;
                   10226:                        REG8(BL) = 0xb0;
                   10227:                        REG16(DX) = max_paragraphs;
                   10228:                }
                   10229:        } else {
                   10230:                REG16(AX) = 0x0000;
                   10231:                REG8(BL) = 0xb2;
                   10232:        }
                   10233: }
                   10234: 
                   10235: #endif
                   10236: 
1.1.1.26  root     10237: UINT16 msdos_get_equipment()
                   10238: {
                   10239:        static UINT16 equip = 0;
                   10240:        
                   10241:        if(equip == 0) {
                   10242: #ifdef SUPPORT_FPU
                   10243:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   10244: #endif
                   10245:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   10246:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   10247: //             equip |= (1 << 8);      // 0 if DMA installed
                   10248:                equip |= (2 << 9);      // number of serial ports
                   10249:                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     10250:                
        !          10251:                // check only A: and B: if it is floppy drive
        !          10252:                DWORD dwDrives = GetLogicalDrives();
        !          10253:                int n = 0;
        !          10254:                for(int i = 0; i < 2; i++) {
        !          10255:                        if(dwDrives & (1 << i)) {
        !          10256:                                char volume[] = "A:\\";
        !          10257:                                volume[0] = 'A' + i;
        !          10258:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
        !          10259:                                        n++;
        !          10260:                                }
        !          10261:                        }
        !          10262:                }
        !          10263:                if(n != 0) {
        !          10264:                        equip |= (1 << 0);      // floppy disk(s) installed
        !          10265:                        n--;
        !          10266:                        equip |= (n << 6);      // number of floppies installed less 1
        !          10267:                }
        !          10268: //             if(joyGetNumDevs() != 0) {
        !          10269: //                     equip |= (1 << 12);     // game port installed
        !          10270: //             }
1.1.1.26  root     10271:        }
                   10272:        return(equip);
                   10273: }
                   10274: 
1.1       root     10275: void msdos_syscall(unsigned num)
                   10276: {
1.1.1.22  root     10277: #ifdef ENABLE_DEBUG_SYSCALL
                   10278:        if(num == 0x68) {
                   10279:                // dummy interrupt for EMS (int 67h)
                   10280:                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));
                   10281:        } else if(num == 0x69) {
                   10282:                // dummy interrupt for XMS (call far)
                   10283:                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));
                   10284:        } else if(num == 0x6a) {
                   10285:                // dummy interrupt for case map routine pointed in the country info
                   10286:        } else {
                   10287:                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));
                   10288:        }
                   10289: #endif
1.1.1.26  root     10290:        ctrl_c_pressed = ctrl_c_detected = false;
1.1.1.22  root     10291:        
1.1       root     10292:        switch(num) {
                   10293:        case 0x00:
1.1.1.28! root     10294:                try {
        !          10295:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
        !          10296:                        error("division by zero\n");
        !          10297:                } catch(...) {
        !          10298:                        fatalerror("division by zero detected, and failed to terminate current process\n");
        !          10299:                }
1.1       root     10300:                break;
                   10301:        case 0x04:
1.1.1.28! root     10302:                try {
        !          10303:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
        !          10304:                        error("overflow\n");
        !          10305:                } catch(...) {
        !          10306:                        fatalerror("overflow detected, and failed to terminate current process\n");
        !          10307:                }
1.1       root     10308:                break;
                   10309:        case 0x06:
                   10310:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     10311:                if(!ignore_illegal_insn) {
1.1.1.28! root     10312:                        try {
        !          10313:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
        !          10314:                                error("illegal instruction\n");
        !          10315:                        } catch(...) {
        !          10316:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
        !          10317:                        }
1.1.1.14  root     10318:                } else {
                   10319: #if defined(HAS_I386)
                   10320:                        m_eip++;
                   10321: #else
                   10322:                        m_pc++;
                   10323: #endif
                   10324:                }
1.1       root     10325:                break;
1.1.1.8   root     10326:        case 0x08:
1.1.1.14  root     10327: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     10328:        case 0x09:
                   10329:        case 0x0a:
                   10330:        case 0x0b:
                   10331:        case 0x0c:
                   10332:        case 0x0d:
                   10333:        case 0x0e:
                   10334:        case 0x0f:
                   10335:                // EOI
                   10336:                pic[0].isr &= ~(1 << (num - 0x08));
                   10337:                pic_update();
                   10338:                break;
1.1       root     10339:        case 0x10:
                   10340:                // PC BIOS - Video
1.1.1.14  root     10341:                if(!restore_console_on_exit) {
1.1.1.15  root     10342:                        change_console_size(scr_width, scr_height);
1.1       root     10343:                }
1.1.1.3   root     10344:                m_CF = 0;
1.1       root     10345:                switch(REG8(AH)) {
1.1.1.16  root     10346:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     10347:                case 0x01: pcbios_int_10h_01h(); break;
                   10348:                case 0x02: pcbios_int_10h_02h(); break;
                   10349:                case 0x03: pcbios_int_10h_03h(); break;
                   10350:                case 0x05: pcbios_int_10h_05h(); break;
                   10351:                case 0x06: pcbios_int_10h_06h(); break;
                   10352:                case 0x07: pcbios_int_10h_07h(); break;
                   10353:                case 0x08: pcbios_int_10h_08h(); break;
                   10354:                case 0x09: pcbios_int_10h_09h(); break;
                   10355:                case 0x0a: pcbios_int_10h_0ah(); break;
                   10356:                case 0x0b: break;
                   10357:                case 0x0c: break;
                   10358:                case 0x0d: break;
                   10359:                case 0x0e: pcbios_int_10h_0eh(); break;
                   10360:                case 0x0f: pcbios_int_10h_0fh(); break;
                   10361:                case 0x10: break;
1.1.1.14  root     10362:                case 0x11: pcbios_int_10h_11h(); break;
                   10363:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     10364:                case 0x13: pcbios_int_10h_13h(); break;
                   10365:                case 0x18: REG8(AL) = 0x86; break;
1.1.1.14  root     10366:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     10367:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   10368:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     10369:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     10370:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   10371:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     10372:                case 0x4f: pcbios_int_10h_4fh(); break;
                   10373:                case 0x80: m_CF = 1; break; // unknown
                   10374:                case 0x81: m_CF = 1; break; // unknown
1.1       root     10375:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     10376:                case 0x83: pcbios_int_10h_83h(); break;
                   10377:                case 0x8b: break;
                   10378:                case 0x8c: m_CF = 1; break; // unknown
                   10379:                case 0x8d: m_CF = 1; break; // unknown
                   10380:                case 0x8e: m_CF = 1; break; // unknown
                   10381:                case 0x90: pcbios_int_10h_90h(); break;
                   10382:                case 0x91: pcbios_int_10h_91h(); break;
                   10383:                case 0x92: break;
                   10384:                case 0x93: break;
                   10385:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     10386:                case 0xfa: break; // ega register interface library is not installed
1.1       root     10387:                case 0xfe: pcbios_int_10h_feh(); break;
                   10388:                case 0xff: pcbios_int_10h_ffh(); break;
                   10389:                default:
1.1.1.22  root     10390:                        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));
                   10391:                        m_CF = 1;
1.1       root     10392:                        break;
                   10393:                }
                   10394:                break;
                   10395:        case 0x11:
                   10396:                // PC BIOS - Get Equipment List
1.1.1.26  root     10397:                REG16(AX) = msdos_get_equipment();
1.1       root     10398:                break;
                   10399:        case 0x12:
                   10400:                // PC BIOS - Get Memory Size
                   10401:                REG16(AX) = MEMORY_END / 1024;
                   10402:                break;
                   10403:        case 0x13:
                   10404:                // PC BIOS - Disk
1.1.1.22  root     10405: //             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     10406:                REG8(AH) = 0xff;
1.1.1.3   root     10407:                m_CF = 1;
1.1       root     10408:                break;
                   10409:        case 0x14:
                   10410:                // PC BIOS - Serial I/O
1.1.1.25  root     10411:                switch(REG8(AH)) {
                   10412:                case 0x00: pcbios_int_14h_00h(); break;
                   10413:                case 0x01: pcbios_int_14h_01h(); break;
                   10414:                case 0x02: pcbios_int_14h_02h(); break;
                   10415:                case 0x03: pcbios_int_14h_03h(); break;
                   10416:                case 0x04: pcbios_int_14h_04h(); break;
                   10417:                case 0x05: pcbios_int_14h_05h(); break;
                   10418:                default:
                   10419:                        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));
                   10420:                        break;
                   10421:                }
1.1       root     10422:                break;
                   10423:        case 0x15:
                   10424:                // PC BIOS
1.1.1.3   root     10425:                m_CF = 0;
1.1       root     10426:                switch(REG8(AH)) {
1.1.1.14  root     10427:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     10428:                case 0x23: pcbios_int_15h_23h(); break;
                   10429:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     10430:                case 0x41: break;
1.1       root     10431:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     10432:                case 0x50: pcbios_int_15h_50h(); break;
1.1       root     10433:                case 0x86: pcbios_int_15h_86h(); break;
                   10434:                case 0x87: pcbios_int_15h_87h(); break;
                   10435:                case 0x88: pcbios_int_15h_88h(); break;
                   10436:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     10437:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     10438:                case 0xc0: // PS/2 ???
                   10439:                case 0xc1:
                   10440:                case 0xc2:
                   10441:                        REG8(AH) = 0x86;
                   10442:                        m_CF = 1;
                   10443:                        break;
1.1.1.3   root     10444: #if defined(HAS_I386)
1.1       root     10445:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     10446: #endif
1.1       root     10447:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     10448:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     10449:                default:
1.1.1.22  root     10450:                        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));
                   10451:                        REG8(AH) = 0x86;
1.1.1.3   root     10452:                        m_CF = 1;
1.1       root     10453:                        break;
                   10454:                }
                   10455:                break;
                   10456:        case 0x16:
                   10457:                // PC BIOS - Keyboard
1.1.1.3   root     10458:                m_CF = 0;
1.1       root     10459:                switch(REG8(AH)) {
                   10460:                case 0x00: pcbios_int_16h_00h(); break;
                   10461:                case 0x01: pcbios_int_16h_01h(); break;
                   10462:                case 0x02: pcbios_int_16h_02h(); break;
                   10463:                case 0x03: pcbios_int_16h_03h(); break;
                   10464:                case 0x05: pcbios_int_16h_05h(); break;
                   10465:                case 0x10: pcbios_int_16h_00h(); break;
                   10466:                case 0x11: pcbios_int_16h_01h(); break;
                   10467:                case 0x12: pcbios_int_16h_12h(); break;
                   10468:                case 0x13: pcbios_int_16h_13h(); break;
                   10469:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     10470:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.22  root     10471:                case 0xda: break; // unknown
                   10472:                case 0xff: break; // unknown
1.1       root     10473:                default:
1.1.1.22  root     10474:                        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     10475:                        break;
                   10476:                }
                   10477:                break;
                   10478:        case 0x17:
                   10479:                // PC BIOS - Printer
1.1.1.22  root     10480: //             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     10481:                break;
                   10482:        case 0x1a:
                   10483:                // PC BIOS - Timer
1.1.1.3   root     10484:                m_CF = 0;
1.1       root     10485:                switch(REG8(AH)) {
                   10486:                case 0x00: pcbios_int_1ah_00h(); break;
                   10487:                case 0x01: break;
                   10488:                case 0x02: pcbios_int_1ah_02h(); break;
                   10489:                case 0x03: break;
                   10490:                case 0x04: pcbios_int_1ah_04h(); break;
                   10491:                case 0x05: break;
                   10492:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   10493:                case 0x0b: break;
1.1.1.14  root     10494:                case 0x35: break; // Word Perfect Third Party Interface?
                   10495:                case 0x36: break; // Word Perfect Third Party Interface
                   10496:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     10497:                default:
1.1.1.22  root     10498:                        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     10499:                        break;
                   10500:                }
                   10501:                break;
                   10502:        case 0x20:
1.1.1.28! root     10503:                try {
        !          10504:                        msdos_process_terminate(SREG(CS), retval, 1);
        !          10505:                } catch(...) {
        !          10506:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
        !          10507:                }
1.1       root     10508:                break;
                   10509:        case 0x21:
                   10510:                // MS-DOS System Call
1.1.1.3   root     10511:                m_CF = 0;
1.1.1.28! root     10512:                try {
        !          10513:                        switch(REG8(AH)) {
        !          10514:                        case 0x00: msdos_int_21h_00h(); break;
        !          10515:                        case 0x01: msdos_int_21h_01h(); break;
        !          10516:                        case 0x02: msdos_int_21h_02h(); break;
        !          10517:                        case 0x03: msdos_int_21h_03h(); break;
        !          10518:                        case 0x04: msdos_int_21h_04h(); break;
        !          10519:                        case 0x05: msdos_int_21h_05h(); break;
        !          10520:                        case 0x06: msdos_int_21h_06h(); break;
        !          10521:                        case 0x07: msdos_int_21h_07h(); break;
        !          10522:                        case 0x08: msdos_int_21h_08h(); break;
        !          10523:                        case 0x09: msdos_int_21h_09h(); break;
        !          10524:                        case 0x0a: msdos_int_21h_0ah(); break;
        !          10525:                        case 0x0b: msdos_int_21h_0bh(); break;
        !          10526:                        case 0x0c: msdos_int_21h_0ch(); break;
        !          10527:                        case 0x0d: msdos_int_21h_0dh(); break;
        !          10528:                        case 0x0e: msdos_int_21h_0eh(); break;
        !          10529:                        case 0x0f: msdos_int_21h_0fh(); break;
        !          10530:                        case 0x10: msdos_int_21h_10h(); break;
        !          10531:                        case 0x11: msdos_int_21h_11h(); break;
        !          10532:                        case 0x12: msdos_int_21h_12h(); break;
        !          10533:                        case 0x13: msdos_int_21h_13h(); break;
        !          10534:                        case 0x14: msdos_int_21h_14h(); break;
        !          10535:                        case 0x15: msdos_int_21h_15h(); break;
        !          10536:                        case 0x16: msdos_int_21h_16h(); break;
        !          10537:                        case 0x17: msdos_int_21h_17h(); break;
        !          10538:                        case 0x18: msdos_int_21h_18h(); break;
        !          10539:                        case 0x19: msdos_int_21h_19h(); break;
        !          10540:                        case 0x1a: msdos_int_21h_1ah(); break;
        !          10541:                        case 0x1b: msdos_int_21h_1bh(); break;
        !          10542:                        case 0x1c: msdos_int_21h_1ch(); break;
        !          10543:                        case 0x1d: msdos_int_21h_1dh(); break;
        !          10544:                        case 0x1e: msdos_int_21h_1eh(); break;
        !          10545:                        case 0x1f: msdos_int_21h_1fh(); break;
        !          10546:                        case 0x20: msdos_int_21h_20h(); break;
        !          10547:                        case 0x21: msdos_int_21h_21h(); break;
        !          10548:                        case 0x22: msdos_int_21h_22h(); break;
        !          10549:                        case 0x23: msdos_int_21h_23h(); break;
        !          10550:                        case 0x24: msdos_int_21h_24h(); break;
        !          10551:                        case 0x25: msdos_int_21h_25h(); break;
        !          10552:                        case 0x26: msdos_int_21h_26h(); break;
        !          10553:                        case 0x27: msdos_int_21h_27h(); break;
        !          10554:                        case 0x28: msdos_int_21h_28h(); break;
        !          10555:                        case 0x29: msdos_int_21h_29h(); break;
        !          10556:                        case 0x2a: msdos_int_21h_2ah(); break;
        !          10557:                        case 0x2b: msdos_int_21h_2bh(); break;
        !          10558:                        case 0x2c: msdos_int_21h_2ch(); break;
        !          10559:                        case 0x2d: msdos_int_21h_2dh(); break;
        !          10560:                        case 0x2e: msdos_int_21h_2eh(); break;
        !          10561:                        case 0x2f: msdos_int_21h_2fh(); break;
        !          10562:                        case 0x30: msdos_int_21h_30h(); break;
        !          10563:                        case 0x31: msdos_int_21h_31h(); break;
        !          10564:                        case 0x32: msdos_int_21h_32h(); break;
        !          10565:                        case 0x33: msdos_int_21h_33h(); break;
        !          10566:                        case 0x34: msdos_int_21h_34h(); break;
        !          10567:                        case 0x35: msdos_int_21h_35h(); break;
        !          10568:                        case 0x36: msdos_int_21h_36h(); break;
        !          10569:                        case 0x37: msdos_int_21h_37h(); break;
        !          10570:                        case 0x38: msdos_int_21h_38h(); break;
        !          10571:                        case 0x39: msdos_int_21h_39h(0); break;
        !          10572:                        case 0x3a: msdos_int_21h_3ah(0); break;
        !          10573:                        case 0x3b: msdos_int_21h_3bh(0); break;
        !          10574:                        case 0x3c: msdos_int_21h_3ch(); break;
        !          10575:                        case 0x3d: msdos_int_21h_3dh(); break;
        !          10576:                        case 0x3e: msdos_int_21h_3eh(); break;
        !          10577:                        case 0x3f: msdos_int_21h_3fh(); break;
        !          10578:                        case 0x40: msdos_int_21h_40h(); break;
        !          10579:                        case 0x41: msdos_int_21h_41h(0); break;
        !          10580:                        case 0x42: msdos_int_21h_42h(); break;
        !          10581:                        case 0x43: msdos_int_21h_43h(0); break;
        !          10582:                        case 0x44: msdos_int_21h_44h(); break;
        !          10583:                        case 0x45: msdos_int_21h_45h(); break;
        !          10584:                        case 0x46: msdos_int_21h_46h(); break;
        !          10585:                        case 0x47: msdos_int_21h_47h(0); break;
        !          10586:                        case 0x48: msdos_int_21h_48h(); break;
        !          10587:                        case 0x49: msdos_int_21h_49h(); break;
        !          10588:                        case 0x4a: msdos_int_21h_4ah(); break;
        !          10589:                        case 0x4b: msdos_int_21h_4bh(); break;
        !          10590:                        case 0x4c: msdos_int_21h_4ch(); break;
        !          10591:                        case 0x4d: msdos_int_21h_4dh(); break;
        !          10592:                        case 0x4e: msdos_int_21h_4eh(); break;
        !          10593:                        case 0x4f: msdos_int_21h_4fh(); break;
        !          10594:                        case 0x50: msdos_int_21h_50h(); break;
        !          10595:                        case 0x51: msdos_int_21h_51h(); break;
        !          10596:                        case 0x52: msdos_int_21h_52h(); break;
        !          10597:                        // 0x53: translate bios parameter block to drive param bock
        !          10598:                        case 0x54: msdos_int_21h_54h(); break;
        !          10599:                        case 0x55: msdos_int_21h_55h(); break;
        !          10600:                        case 0x56: msdos_int_21h_56h(0); break;
        !          10601:                        case 0x57: msdos_int_21h_57h(); break;
        !          10602:                        case 0x58: msdos_int_21h_58h(); break;
        !          10603:                        case 0x59: msdos_int_21h_59h(); break;
        !          10604:                        case 0x5a: msdos_int_21h_5ah(); break;
        !          10605:                        case 0x5b: msdos_int_21h_5bh(); break;
        !          10606:                        case 0x5c: msdos_int_21h_5ch(); break;
        !          10607:                        case 0x5d: msdos_int_21h_5dh(); break;
        !          10608:                        // 0x5e: ms-network
        !          10609:                        // 0x5f: ms-network
        !          10610:                        case 0x60: msdos_int_21h_60h(0); break;
        !          10611:                        case 0x61: msdos_int_21h_61h(); break;
        !          10612:                        case 0x62: msdos_int_21h_62h(); break;
        !          10613:                        case 0x63: msdos_int_21h_63h(); break;
        !          10614:                        // 0x64: set device driver lockahead flag
        !          10615:                        case 0x65: msdos_int_21h_65h(); break;
        !          10616:                        case 0x66: msdos_int_21h_66h(); break;
        !          10617:                        case 0x67: msdos_int_21h_67h(); break;
        !          10618:                        case 0x68: msdos_int_21h_68h(); break;
        !          10619:                        case 0x69: msdos_int_21h_69h(); break;
        !          10620:                        case 0x6a: msdos_int_21h_6ah(); break;
        !          10621:                        case 0x6b: msdos_int_21h_6bh(); break;
        !          10622:                        case 0x6c: msdos_int_21h_6ch(0); break;
        !          10623:                        // 0x6d: find first rom program
        !          10624:                        // 0x6e: find next rom program
        !          10625:                        // 0x6f: get/set rom scan start address
        !          10626:                        // 0x70: windows95 get/set internationalization information
        !          10627:                        case 0x71:
        !          10628:                                // windows95 long filename functions
        !          10629:                                switch(REG8(AL)) {
        !          10630:                                case 0x0d: msdos_int_21h_710dh(); break;
        !          10631:                                case 0x39: msdos_int_21h_39h(1); break;
        !          10632:                                case 0x3a: msdos_int_21h_3ah(1); break;
        !          10633:                                case 0x3b: msdos_int_21h_3bh(1); break;
        !          10634:                                case 0x41: msdos_int_21h_7141h(1); break;
        !          10635:                                case 0x43: msdos_int_21h_43h(1); break;
        !          10636:                                case 0x47: msdos_int_21h_47h(1); break;
        !          10637:                                case 0x4e: msdos_int_21h_714eh(); break;
        !          10638:                                case 0x4f: msdos_int_21h_714fh(); break;
        !          10639:                                case 0x56: msdos_int_21h_56h(1); break;
        !          10640:                                case 0x60: msdos_int_21h_60h(1); break;
        !          10641:                                case 0x6c: msdos_int_21h_6ch(1); break;
        !          10642:                                case 0xa0: msdos_int_21h_71a0h(); break;
        !          10643:                                case 0xa1: msdos_int_21h_71a1h(); break;
        !          10644:                                case 0xa6: msdos_int_21h_71a6h(); break;
        !          10645:                                case 0xa7: msdos_int_21h_71a7h(); break;
        !          10646:                                case 0xa8: msdos_int_21h_71a8h(); break;
        !          10647:                                // 0xa9: server create/open file
        !          10648:                                case 0xaa: msdos_int_21h_71aah(); break;
        !          10649:                                default:
        !          10650:                                        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));
        !          10651:                                        REG16(AX) = 0x7100;
        !          10652:                                        m_CF = 1;
        !          10653:                                        break;
        !          10654:                                }
        !          10655:                                break;
        !          10656:                        // 0x72: Windows95 beta - LFN FindClose
        !          10657:                        case 0x73:
        !          10658:                                // windows95 fat32 functions
        !          10659:                                switch(REG8(AL)) {
        !          10660:                                case 0x00: msdos_int_21h_7300h(); break;
        !          10661:                                // 0x01: set drive locking ???
        !          10662:                                case 0x02: msdos_int_21h_7302h(); break;
        !          10663:                                case 0x03: msdos_int_21h_7303h(); break;
        !          10664:                                // 0x04: set dpb to use for formatting
        !          10665:                                // 0x05: extended absolute disk read/write
        !          10666:                                default:
        !          10667:                                        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));
        !          10668:                                        REG16(AX) = 0x7300;
        !          10669:                                        m_CF = 1;
        !          10670:                                        break;
        !          10671:                                }
1.1       root     10672:                                break;
                   10673:                        default:
1.1.1.22  root     10674:                                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     10675:                                REG16(AX) = 0x01;
1.1.1.3   root     10676:                                m_CF = 1;
1.1       root     10677:                                break;
                   10678:                        }
1.1.1.28! root     10679:                } catch(int error) {
        !          10680:                        REG16(AX) = error;
        !          10681:                        m_CF = 1;
        !          10682:                } catch(...) {
        !          10683:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     10684:                        m_CF = 1;
1.1       root     10685:                }
1.1.1.3   root     10686:                if(m_CF) {
1.1.1.23  root     10687:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10688:                        sda->extended_error_code = REG16(AX);
                   10689:                        switch(sda->extended_error_code) {
                   10690:                        case  4: // Too many open files
                   10691:                        case  8: // Insufficient memory
                   10692:                                sda->error_class = 1; // Out of resource
                   10693:                                break;
                   10694:                        case  5: // Access denied
                   10695:                                sda->error_class = 3; // Authorization
                   10696:                                break;
                   10697:                        case  7: // Memory control block destroyed
                   10698:                                sda->error_class = 4; // Internal
                   10699:                                break;
                   10700:                        case  2: // File not found
                   10701:                        case  3: // Path not found
                   10702:                        case 15: // Invaid drive specified
                   10703:                        case 18: // No more files
                   10704:                                sda->error_class = 8; // Not found
                   10705:                                break;
                   10706:                        case 32: // Sharing violation
                   10707:                        case 33: // Lock violation
                   10708:                                sda->error_class = 10; // Locked
                   10709:                                break;
                   10710: //                     case 16: // Removal of current directory attempted
                   10711:                        case 19: // Attempted write on protected disk
                   10712:                        case 21: // Drive not ready
                   10713: //                     case 29: // Write failure
                   10714: //                     case 30: // Read failure
                   10715: //                     case 82: // Cannot create subdirectory
                   10716:                                sda->error_class = 11; // Media
                   10717:                                break;
                   10718:                        case 80: // File already exists
                   10719:                                sda->error_class = 12; // Already exist
                   10720:                                break;
                   10721:                        default:
                   10722:                                sda->error_class = 13; // Unknown
                   10723:                                break;
                   10724:                        }
                   10725:                        sda->suggested_action = 1; // Retry
                   10726:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     10727:                }
1.1.1.26  root     10728:                if(ctrl_c_checking && ctrl_c_detected) {
                   10729:                        // raise int 23h
                   10730: #if defined(HAS_I386)
                   10731:                        m_ext = 0; // not an external interrupt
                   10732:                        i386_trap(0x23, 1, 0);
                   10733:                        m_ext = 1;
                   10734: #else
                   10735:                        PREFIX86(_interrupt)(0x23);
                   10736: #endif
                   10737:                }
1.1       root     10738:                break;
                   10739:        case 0x22:
                   10740:                fatalerror("int 22h (terminate address)\n");
                   10741:        case 0x23:
1.1.1.28! root     10742:                try {
        !          10743:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
        !          10744:                } catch(...) {
        !          10745:                        fatalerror("failed to terminate the current process by int 23h\n");
        !          10746:                }
1.1       root     10747:                break;
                   10748:        case 0x24:
1.1.1.28! root     10749:                try {
        !          10750:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
        !          10751:                } catch(...) {
        !          10752:                        fatalerror("failed to terminate the current process by int 24h\n");
        !          10753:                }
1.1       root     10754:                break;
                   10755:        case 0x25:
                   10756:                msdos_int_25h();
                   10757:                break;
                   10758:        case 0x26:
                   10759:                msdos_int_26h();
                   10760:                break;
                   10761:        case 0x27:
1.1.1.28! root     10762:                try {
        !          10763:                        msdos_int_27h();
        !          10764:                } catch(...) {
        !          10765:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
        !          10766:                }
1.1       root     10767:                break;
                   10768:        case 0x28:
                   10769:                Sleep(10);
                   10770:                break;
                   10771:        case 0x29:
                   10772:                msdos_int_29h();
                   10773:                break;
                   10774:        case 0x2e:
                   10775:                msdos_int_2eh();
                   10776:                break;
                   10777:        case 0x2f:
                   10778:                // multiplex interrupt
                   10779:                switch(REG8(AH)) {
1.1.1.22  root     10780:                case 0x00: break;
                   10781:                case 0x01: msdos_int_2fh_01h(); break;
                   10782:                case 0x05: msdos_int_2fh_05h(); break;
                   10783:                case 0x06: msdos_int_2fh_06h(); break;
                   10784:                case 0x08: msdos_int_2fh_08h(); break;
                   10785:                case 0x10: msdos_int_2fh_10h(); break;
                   10786:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     10787:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.22  root     10788:                case 0x14: msdos_int_2fh_14h(); break;
                   10789:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     10790:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     10791:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     10792:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.22  root     10793:                case 0x1b: msdos_int_2fh_1bh(); break;
1.1       root     10794:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     10795:                case 0x46: msdos_int_2fh_46h(); break;
                   10796:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     10797:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     10798:                case 0x4b: msdos_int_2fh_4bh(); break;
                   10799:                case 0x4c: break; // unknown
                   10800:                case 0x4d: break; // unknown
                   10801:                case 0x4e: break; // unknown
1.1       root     10802:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     10803:                case 0x55: msdos_int_2fh_55h(); break;
                   10804:                case 0x58: break; // unknown
                   10805:                case 0x74: break; // unknown
1.1.1.24  root     10806:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     10807:                case 0xae: msdos_int_2fh_aeh(); break;
                   10808:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.22  root     10809:                case 0xe9: break; // unknown
                   10810:                case 0xfe: break; // norton utilities ???
                   10811:                default:
                   10812:                        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));
                   10813:                        break;
1.1       root     10814:                }
                   10815:                break;
1.1.1.24  root     10816:        case 0x33:
                   10817:                switch(REG8(AH)) {
                   10818:                case 0x00:
                   10819:                        // Mouse
                   10820:                        switch(REG8(AL)) {
                   10821:                        case 0x00: msdos_int_33h_0000h(); break;
                   10822:                        case 0x01: msdos_int_33h_0001h(); break;
                   10823:                        case 0x02: msdos_int_33h_0002h(); break;
                   10824:                        case 0x03: msdos_int_33h_0003h(); break;
                   10825:                        case 0x04: break; // position mouse cursor
                   10826:                        case 0x05: msdos_int_33h_0005h(); break;
                   10827:                        case 0x06: msdos_int_33h_0006h(); break;
                   10828:                        case 0x07: msdos_int_33h_0007h(); break;
                   10829:                        case 0x08: msdos_int_33h_0008h(); break;
                   10830:                        case 0x09: msdos_int_33h_0009h(); break;
                   10831:                        case 0x0a: break; // define text cursor
                   10832:                        case 0x0b: msdos_int_33h_000bh(); break;
                   10833:                        case 0x0c: msdos_int_33h_000ch(); break;
                   10834:                        case 0x0d: break; // light pen emulation on
                   10835:                        case 0x0e: break; // light pen emulation off
                   10836:                        case 0x0f: msdos_int_33h_000fh(); break;
                   10837:                        case 0x10: break; // define screen region for updating
                   10838:                        case 0x11: msdos_int_33h_0011h(); break;
                   10839:                        case 0x12: REG16(AX) = 0xffff; break; // set large graphics cursor block
                   10840:                        case 0x13: break; // define double-speed threshold
                   10841:                        case 0x14: msdos_int_33h_0014h(); break;
                   10842:                        case 0x15: msdos_int_33h_0015h(); break;
                   10843:                        case 0x16: msdos_int_33h_0016h(); break;
                   10844:                        case 0x17: msdos_int_33h_0017h(); break;
                   10845:                        case 0x1a: msdos_int_33h_001ah(); break;
                   10846:                        case 0x1b: msdos_int_33h_001bh(); break;
                   10847:                        case 0x1d: msdos_int_33h_001dh(); break;
                   10848:                        case 0x1e: msdos_int_33h_001eh(); break;
                   10849:                        case 0x21: msdos_int_33h_0021h(); break;
                   10850:                        case 0x22: msdos_int_33h_0022h(); break;
                   10851:                        case 0x23: msdos_int_33h_0023h(); break;
                   10852:                        case 0x24: msdos_int_33h_0024h(); break;
                   10853:                        case 0x26: msdos_int_33h_0026h(); break;
                   10854:                        case 0x2a: msdos_int_33h_002ah(); break;
                   10855:                        case 0x2f: break; // mouse hardware reset
                   10856:                        case 0x31: msdos_int_33h_0031h(); break;
                   10857:                        case 0x32: msdos_int_33h_0032h(); break;
                   10858:                        default:
                   10859:                                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));
                   10860:                                break;
                   10861:                        }
                   10862:                        break;
                   10863:                default:
                   10864:                        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));
                   10865:                        break;
                   10866:                }
                   10867:                break;
1.1.1.19  root     10868:        case 0x68:
                   10869:                // dummy interrupt for EMS (int 67h)
                   10870:                switch(REG8(AH)) {
                   10871:                case 0x40: msdos_int_67h_40h(); break;
                   10872:                case 0x41: msdos_int_67h_41h(); break;
                   10873:                case 0x42: msdos_int_67h_42h(); break;
                   10874:                case 0x43: msdos_int_67h_43h(); break;
                   10875:                case 0x44: msdos_int_67h_44h(); break;
                   10876:                case 0x45: msdos_int_67h_45h(); break;
                   10877:                case 0x46: msdos_int_67h_46h(); break;
                   10878:                case 0x47: msdos_int_67h_47h(); break;
                   10879:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     10880:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   10881:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     10882:                case 0x4b: msdos_int_67h_4bh(); break;
                   10883:                case 0x4c: msdos_int_67h_4ch(); break;
                   10884:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     10885:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     10886:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     10887:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     10888:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     10889:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     10890:                case 0x53: msdos_int_67h_53h(); break;
                   10891:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     10892:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   10893:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   10894:                case 0x57: msdos_int_67h_57h(); break;
                   10895:                case 0x58: msdos_int_67h_58h(); break;
                   10896:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   10897:                case 0x5a: msdos_int_67h_5ah(); break;
                   10898:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   10899:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   10900:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.19  root     10901:                default:
1.1.1.22  root     10902:                        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     10903:                        REG8(AH) = 0x84;
                   10904:                        break;
                   10905:                }
                   10906:                break;
                   10907: #ifdef SUPPORT_XMS
                   10908:        case 0x69:
                   10909:                // dummy interrupt for XMS (call far)
1.1.1.28! root     10910:                try {
        !          10911:                        switch(REG8(AH)) {
        !          10912:                        case 0x00: msdos_call_xms_00h(); break;
        !          10913:                        case 0x01: msdos_call_xms_01h(); break;
        !          10914:                        case 0x02: msdos_call_xms_02h(); break;
        !          10915:                        case 0x03: msdos_call_xms_03h(); break;
        !          10916:                        case 0x04: msdos_call_xms_04h(); break;
        !          10917:                        case 0x05: msdos_call_xms_05h(); break;
        !          10918:                        case 0x06: msdos_call_xms_06h(); break;
        !          10919:                        case 0x07: msdos_call_xms_07h(); break;
        !          10920:                        case 0x08: msdos_call_xms_08h(); break;
        !          10921:                        case 0x09: msdos_call_xms_09h(); break;
        !          10922:                        case 0x0a: msdos_call_xms_0ah(); break;
        !          10923:                        case 0x0b: msdos_call_xms_0bh(); break;
        !          10924:                        case 0x0c: msdos_call_xms_0ch(); break;
        !          10925:                        case 0x0d: msdos_call_xms_0dh(); break;
        !          10926:                        case 0x0e: msdos_call_xms_0eh(); break;
        !          10927:                        case 0x0f: msdos_call_xms_0fh(); break;
        !          10928:                        case 0x10: msdos_call_xms_10h(); break;
        !          10929:                        case 0x11: msdos_call_xms_11h(); break;
        !          10930:                        case 0x12: msdos_call_xms_12h(); break;
        !          10931:                        // 0x88: XMS 3.0 - Query free extended memory
        !          10932:                        // 0x89: XMS 3.0 - Allocate any extended memory
        !          10933:                        // 0x8e: XMS 3.0 - Get extended EMB handle information
        !          10934:                        // 0x8f: XMS 3.0 - Reallocate any extended memory block
        !          10935:                        default:
        !          10936:                                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));
        !          10937:                                REG16(AX) = 0x0000;
        !          10938:                                REG8(BL) = 0x80; // function not implemented
        !          10939:                                break;
        !          10940:                        }
        !          10941:                } catch(...) {
1.1.1.19  root     10942:                        REG16(AX) = 0x0000;
1.1.1.28! root     10943:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     10944:                }
                   10945:                break;
                   10946: #endif
                   10947:        case 0x6a:
1.1.1.24  root     10948:                // irq12 (mouse)
                   10949:                mouse_push_ax = REG16(AX);
                   10950:                mouse_push_bx = REG16(BX);
                   10951:                mouse_push_cx = REG16(CX);
                   10952:                mouse_push_dx = REG16(DX);
                   10953:                mouse_push_si = REG16(SI);
                   10954:                mouse_push_di = REG16(DI);
                   10955:                
                   10956:                if(mouse.active && mouse.call_addr.dw != 0) {
                   10957:                        REG16(AX) = mouse.status_irq;
                   10958:                        REG16(BX) = mouse.get_buttons();
                   10959:                        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   10960:                        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   10961:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   10962:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   10963:                        
                   10964:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   10965:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   10966:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   10967:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   10968:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   10969:                } else {
                   10970:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   10971:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   10972:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   10973:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   10974:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   10975:                }
                   10976:                break;
                   10977:        case 0x6b:
                   10978:                // end of irq12 (mouse)
                   10979:                REG16(AX) = mouse_push_ax;
                   10980:                REG16(BX) = mouse_push_bx;
                   10981:                REG16(CX) = mouse_push_cx;
                   10982:                REG16(DX) = mouse_push_dx;
                   10983:                REG16(SI) = mouse_push_si;
                   10984:                REG16(DI) = mouse_push_di;
                   10985:                
                   10986:                // EOI
                   10987:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   10988:                        pic[0].isr &= ~(1 << 2); // master
                   10989:                }
                   10990:                pic_update();
                   10991:                break;
                   10992:        case 0x6c:
1.1.1.19  root     10993:                // dummy interrupt for case map routine pointed in the country info
                   10994:                if(REG8(AL) >= 0x80) {
                   10995:                        char tmp[2] = {0};
                   10996:                        tmp[0] = REG8(AL);
                   10997:                        my_strupr(tmp);
                   10998:                        REG8(AL) = tmp[0];
                   10999:                }
                   11000:                break;
1.1.1.27  root     11001:        case 0x6d:
                   11002:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   11003:                REG8(AL) = 0x86; // not supported
                   11004:                m_CF = 1;
                   11005:                break;
1.1.1.8   root     11006:        case 0x70:
                   11007:        case 0x71:
                   11008:        case 0x72:
                   11009:        case 0x73:
                   11010:        case 0x74:
                   11011:        case 0x75:
                   11012:        case 0x76:
                   11013:        case 0x77:
                   11014:                // EOI
                   11015:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   11016:                        pic[0].isr &= ~(1 << 2); // master
                   11017:                }
                   11018:                pic_update();
                   11019:                break;
1.1       root     11020:        default:
1.1.1.22  root     11021: //             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     11022:                break;
                   11023:        }
                   11024:        
                   11025:        // update cursor position
                   11026:        if(cursor_moved) {
                   11027:                CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.23  root     11028:                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.15  root     11029:                if(!restore_console_on_exit) {
                   11030:                        scr_top = csbi.srWindow.Top;
                   11031:                }
1.1       root     11032:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
1.1.1.14  root     11033:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
1.1       root     11034:                cursor_moved = false;
                   11035:        }
                   11036: }
                   11037: 
                   11038: // init
                   11039: 
                   11040: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   11041: {
                   11042:        // init file handler
                   11043:        memset(file_handler, 0, sizeof(file_handler));
                   11044:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   11045:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   11046:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     11047: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     11048:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     11049: #else
                   11050:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   11051: #endif
                   11052:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     11053:        }
1.1.1.21  root     11054: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     11055:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.21  root     11056: #else
                   11057:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1       root     11058: #endif
1.1.1.21  root     11059:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   11060:        }
1.1       root     11061:        _dup2(0, DUP_STDIN);
                   11062:        _dup2(1, DUP_STDOUT);
                   11063:        _dup2(2, DUP_STDERR);
1.1.1.21  root     11064:        _dup2(3, DUP_STDAUX);
                   11065:        _dup2(4, DUP_STDPRN);
1.1       root     11066:        
1.1.1.24  root     11067:        // init mouse
                   11068:        memset(&mouse, 0, sizeof(mouse));
                   11069:        mouse.max_position.x = 8 * scr_width  - 1;
                   11070:        mouse.max_position.y = 8 * scr_height - 1;
                   11071:        mouse.mickey.x = 8;
                   11072:        mouse.mickey.y = 16;
                   11073:        
1.1.1.26  root     11074: #ifdef SUPPORT_XMS
                   11075:        // init xms
                   11076:        msdos_xms_init();
                   11077: #endif
                   11078:        
1.1       root     11079:        // init process
                   11080:        memset(process, 0, sizeof(process));
                   11081:        
1.1.1.13  root     11082:        // init dtainfo
                   11083:        msdos_dta_info_init();
                   11084:        
1.1       root     11085:        // init memory
                   11086:        memset(mem, 0, sizeof(mem));
                   11087:        
                   11088:        // bios data area
1.1.1.23  root     11089:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     11090:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   11091:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     11092:        CONSOLE_FONT_INFO cfi;
                   11093:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   11094:        
                   11095:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   11096:        text_vram_top_address = TEXT_VRAM_TOP;
                   11097:        text_vram_end_address = text_vram_top_address + regen;
                   11098:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   11099:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   11100:        
                   11101:        if(regen > 0x4000) {
                   11102:                regen = 0x8000;
                   11103:                vram_pages = 1;
                   11104:        } else if(regen > 0x2000) {
                   11105:                regen = 0x4000;
                   11106:                vram_pages = 2;
                   11107:        } else if(regen > 0x1000) {
                   11108:                regen = 0x2000;
                   11109:                vram_pages = 4;
                   11110:        } else {
                   11111:                regen = 0x1000;
                   11112:                vram_pages = 8;
                   11113:        }
1.1       root     11114:        
1.1.1.25  root     11115:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   11116:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
                   11117: //     *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   11118: //     *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
                   11119:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.26  root     11120: //     *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
1.1.1.25  root     11121: //     *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
                   11122:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.26  root     11123:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1       root     11124:        *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
                   11125:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     11126:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   11127:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     11128:        *(UINT16 *)(mem + 0x44e) = 0;
                   11129:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     11130:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     11131:        *(UINT8  *)(mem + 0x460) = 7;
                   11132:        *(UINT8  *)(mem + 0x461) = 7;
                   11133:        *(UINT8  *)(mem + 0x462) = 0;
                   11134:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     11135:        *(UINT8  *)(mem + 0x465) = 0x09;
                   11136:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     11137:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   11138:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   11139:        *(UINT8  *)(mem + 0x487) = 0x60;
                   11140:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.25  root     11141:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.14  root     11142:        
                   11143:        // initial screen
                   11144:        SMALL_RECT rect;
                   11145:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   11146:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   11147:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   11148:                for(int x = 0; x < scr_width; x++) {
                   11149:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   11150:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   11151:                }
                   11152:        }
1.1       root     11153:        
1.1.1.19  root     11154:        // init mcb
1.1       root     11155:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     11156:        
                   11157:        // iret table
                   11158:        // note: int 2eh vector should address the routine in command.com,
                   11159:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   11160:        // so move iret table into allocated memory block
                   11161:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
                   11162:        msdos_mcb_create(seg++, 'M', -1, IRET_SIZE >> 4);
                   11163:        IRET_TOP = seg << 4;
                   11164:        seg += IRET_SIZE >> 4;
1.1.1.25  root     11165:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     11166:        
                   11167:        // dummy xms/ems device
                   11168:        msdos_mcb_create(seg++, 'M', -1, XMS_SIZE >> 4);
                   11169:        XMS_TOP = seg << 4;
                   11170:        seg += XMS_SIZE >> 4;
                   11171:        
                   11172:        // environment
1.1       root     11173:        msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
                   11174:        int env_seg = seg;
                   11175:        int ofs = 0;
1.1.1.28! root     11176:        char env_msdos_path[ENV_SIZE] = "", env_path[ENV_SIZE] = "", env_temp[ENV_SIZE] = "", *path;
1.1       root     11177:        
1.1.1.28! root     11178:        if((path = getenv("MSDOS_PATH")) != NULL) {
        !          11179:                strcpy(env_msdos_path, msdos_get_multiple_short_path(path));
        !          11180:                if(env_msdos_path[0] != '\0') {
        !          11181:                        strcat(env_path, env_msdos_path);
1.1.1.14  root     11182:                }
                   11183:        }
1.1.1.28! root     11184:        if((path = getenv("PATH")) != NULL) {
        !          11185:                if(env_path[0] != '\0') {
        !          11186:                        strcat(env_path, ";");
1.1.1.9   root     11187:                }
1.1.1.28! root     11188:                strcat(env_path, msdos_get_multiple_short_path(path));
1.1.1.9   root     11189:        }
1.1.1.28! root     11190:        if((path = getenv("MSDOS_TEMP")) != NULL || (path = getenv("TEMP")) != NULL || (path = getenv("TMP")) != NULL) {
        !          11191:                strcpy(env_temp, msdos_get_multiple_short_path(path));
1.1.1.15  root     11192:        }
1.1.1.28! root     11193:        if((path = getenv("MSDOS_COMSPEC")) != NULL || (path = msdos_search_command_com(argv[0], env_path)) != NULL) {
        !          11194:                strcpy(comspec_path, msdos_get_multiple_short_path(path));
1.1.1.24  root     11195:        }
1.1.1.9   root     11196:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     11197:                // lower to upper
1.1.1.28! root     11198:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     11199:                strcpy(tmp, *p);
                   11200:                for(int i = 0;; i++) {
                   11201:                        if(tmp[i] == '=') {
                   11202:                                tmp[i] = '\0';
                   11203:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     11204:                                my_strupr(name);
1.1       root     11205:                                tmp[i] = '=';
                   11206:                                break;
                   11207:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28! root     11208:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     11209:                        }
                   11210:                }
1.1.1.18  root     11211:                if(strncmp(tmp, "MSDOS_COMSPEC=", 14) == 0) {
                   11212:                        // ignore MSDOS_COMSPEC
1.1.1.24  root     11213:                } else if(strncmp(tmp, "MSDOS_TEMP=", 11) == 0) {
                   11214:                        // ignore MSDOS_TEMP
1.1.1.28! root     11215:                } else if(standard_env && strstr(";COMSPEC;INCLUDE;LIB;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     11216:                        // ignore non standard environments
                   11217:                } else {
1.1       root     11218:                        if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     11219:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.28! root     11220:                        } else if(strncmp(tmp, "MSDOS_PATH=",  11) == 0) {
        !          11221:                                if(env_msdos_path[0] != '\0') {
        !          11222:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
        !          11223:                                } else {
        !          11224:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
        !          11225:                                }
        !          11226:                        } else if(strncmp(tmp, "PATH=",  5) == 0) {
        !          11227:                                if(env_path[0] != '\0') {
        !          11228:                                        sprintf(tmp, "PATH=%s", env_path);
        !          11229:                                } else {
        !          11230:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
        !          11231:                                }
        !          11232:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
        !          11233:                                if(env_temp[0] != '\0') {
        !          11234:                                        sprintf(tmp, "TEMP=%s", env_temp);
        !          11235:                                } else {
        !          11236:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
        !          11237:                                }
        !          11238:                        } else if(strncmp(tmp, "TMP=",  4) == 0) {
        !          11239:                                if(env_temp[0] != '\0') {
        !          11240:                                        sprintf(tmp, "TMP=%s", env_temp);
        !          11241:                                } else {
        !          11242:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     11243:                                }
                   11244:                        }
                   11245:                        int len = strlen(tmp);
1.1.1.14  root     11246:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     11247:                                fatalerror("too many environments\n");
                   11248:                        }
                   11249:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   11250:                        ofs += len + 1;
                   11251:                }
                   11252:        }
                   11253:        seg += (ENV_SIZE >> 4);
                   11254:        
                   11255:        // psp
                   11256:        msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
                   11257:        current_psp = seg;
1.1.1.14  root     11258:        msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
1.1       root     11259:        seg += (PSP_SIZE >> 4);
                   11260:        
1.1.1.19  root     11261:        // first free mcb in conventional memory
                   11262:        msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     11263:        first_mcb = seg;
                   11264:        
1.1.1.19  root     11265:        // dummy mcb to link to umb
                   11266:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   11267:        
                   11268:        // first free mcb in upper memory block
1.1.1.8   root     11269:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     11270:        
1.1.1.19  root     11271: #ifdef SUPPORT_XMS
                   11272:        // first free mcb in extended memory block
                   11273:        msdos_mcb_create(EMB_TOP >> 4, 'Z', 0, (EMB_END >> 4) - (EMB_TOP >> 4) - 1);
                   11274: #endif
                   11275:        
1.1.1.26  root     11276:        // interrupt vector
                   11277:        for(int i = 0; i < 0x80; i++) {
                   11278:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   11279:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   11280:        }
                   11281:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0010;       // fffd:0010 irq0 (system timer)
                   11282:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   11283:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   11284:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   11285:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   11286:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   11287:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   11288:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   11289:        
                   11290:        // dummy devices (CON -> ... -> CONFIG$ -> NUL -> EMMXXXX0)
                   11291:        static const struct {
                   11292:                UINT16 attributes;
                   11293:                char *dev_name;
                   11294:        } dummy_devices[] = {
                   11295:                {0x8013, "CON     "},
                   11296:                {0x8000, "AUX     "},
                   11297:                {0xa0c0, "PRN     "},
                   11298:                {0x8008, "CLOCK$  "},
                   11299:                {0x8000, "COM1    "},
                   11300:                {0xa0c0, "LPT1    "},
                   11301:                {0xa0c0, "LPT2    "},
                   11302:                {0xa0c0, "LPT3    "},
                   11303:                {0x8000, "COM2    "},
                   11304:                {0x8000, "COM3    "},
                   11305:                {0x8000, "COM4    "},
                   11306:                {0xc000, "CONFIG$ "},
                   11307:        };
                   11308:        static const UINT8 dummy_device_routine[] = {
                   11309:                // from NUL device of Windows 98 SE
                   11310:                // or word ptr ES:[BX+03],0100
                   11311:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   11312:                // retf
                   11313:                0xcb,
                   11314:        };
                   11315:        for(int i = 0; i < 12; i++) {
                   11316:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
                   11317:                if(i == 11) {
                   11318:                        device->next_driver.w.l = offsetof(dos_info_t, nul_device);
                   11319:                        device->next_driver.w.h = DOS_INFO_TOP >> 4;
                   11320:                } else {
                   11321:                        device->next_driver.w.l = 22 + 18 * (i + 1);
                   11322:                        device->next_driver.w.h = DEVICE_TOP >> 4;
                   11323:                }
                   11324:                device->attributes = dummy_devices[i].attributes;
                   11325:                device->strategy = 22 + 18 * 12;
                   11326:                device->interrupt = 22 + 18 * 12 + 6;
                   11327:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
                   11328:        }
                   11329:        memcpy(mem + DEVICE_TOP + 22 + 18 * 12, dummy_device_routine, sizeof(dummy_device_routine));
                   11330:        
1.1.1.25  root     11331:        // dos info
                   11332:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   11333:        dos_info->magic_word = 1;
                   11334:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   11335:        dos_info->first_dpb.w.l = 0;
                   11336:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   11337:        dos_info->first_sft.w.l = 0;
                   11338:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     11339:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     11340:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     11341:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     11342:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   11343:        dos_info->max_sector_len = 512;
                   11344:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   11345:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   11346:        dos_info->cds.w.l = 0;
                   11347:        dos_info->cds.w.h = CDS_TOP >> 4;
                   11348:        dos_info->fcb_table.w.l = 0;
                   11349:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   11350:        dos_info->last_drive = 'Z' - 'A' + 1;
                   11351:        dos_info->buffers_x = 20;
                   11352:        dos_info->buffers_y = 0;
                   11353:        dos_info->boot_drive = 'C' - 'A' + 1;
                   11354:        dos_info->nul_device.next_driver.w.l = 0;
                   11355:        dos_info->nul_device.next_driver.w.h = XMS_TOP >> 4;
                   11356:        dos_info->nul_device.attributes = 0x8004;
1.1.1.26  root     11357:        dos_info->nul_device.strategy = offsetof(dos_info_t, nul_device_routine);
                   11358:        dos_info->nul_device.interrupt = offsetof(dos_info_t, nul_device_routine) + 6;
1.1.1.25  root     11359:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   11360:        dos_info->disk_buf_heads.w.l = 0;
                   11361:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   11362:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   11363:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.26  root     11364:        memcpy(dos_info->nul_device_routine, dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     11365:        
                   11366:        char *env;
                   11367:        if((env = getenv("LASTDRIVE")) != NULL) {
                   11368:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   11369:                        dos_info->last_drive = env[0] - 'A' + 1;
                   11370:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   11371:                        dos_info->last_drive = env[0] - 'a' + 1;
                   11372:                }
                   11373:        }
                   11374:        if((env = getenv("windir")) != NULL) {
                   11375:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   11376:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   11377:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   11378:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   11379:                }
                   11380:        }
                   11381: #if defined(HAS_I386)
                   11382:        dos_info->i386_or_later = 1;
                   11383: #else
                   11384:        dos_info->i386_or_later = 0;
                   11385: #endif
                   11386:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   11387:        
1.1.1.27  root     11388:        // ems (int 67h) and xms
1.1.1.25  root     11389:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   11390:        xms_device->next_driver.w.l = 0xffff;
                   11391:        xms_device->next_driver.w.h = 0xffff;
                   11392:        xms_device->attributes = 0xc000;
1.1.1.26  root     11393:        xms_device->strategy = 0x18;
                   11394:        xms_device->interrupt = 0x18 + 6;
1.1.1.25  root     11395:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   11396:        
1.1.1.26  root     11397:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   11398:        mem[XMS_TOP + 0x13] = 0x68;
                   11399:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     11400: #ifdef SUPPORT_XMS
                   11401:        if(support_xms) {
1.1.1.26  root     11402:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   11403:                mem[XMS_TOP + 0x16] = 0x69;
                   11404:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     11405:        } else
                   11406: #endif
1.1.1.26  root     11407:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
                   11408:        memcpy(mem + XMS_TOP + 0x18, dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     11409:        
1.1.1.26  root     11410:        // irq12 routine (mouse)
1.1.1.24  root     11411:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   11412:        mem[0xfffd0 + 0x01] = 0x6a;
                   11413:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   11414:        mem[0xfffd0 + 0x03] = 0xff;
                   11415:        mem[0xfffd0 + 0x04] = 0xff;
                   11416:        mem[0xfffd0 + 0x05] = 0xff;
                   11417:        mem[0xfffd0 + 0x06] = 0xff;
                   11418:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   11419:        mem[0xfffd0 + 0x08] = 0x6b;
                   11420:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   11421:        
1.1.1.27  root     11422:        // case map routine
                   11423:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   11424:        mem[0xfffd0 + 0x0b] = 0x6c;
                   11425:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   11426:        
                   11427:        // font read routine
                   11428:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   11429:        mem[0xfffd0 + 0x0e] = 0x6d;
                   11430:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     11431:        
1.1.1.26  root     11432:        // irq0 routine (system time)
1.1.1.24  root     11433:        mem[0xfffd0 + 0x10] = 0xcd;     // int 1ch
                   11434:        mem[0xfffd0 + 0x11] = 0x1c;
                   11435:        mem[0xfffd0 + 0x12] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   11436:        mem[0xfffd0 + 0x13] = 0x08;
                   11437:        mem[0xfffd0 + 0x14] = 0x00;
                   11438:        mem[0xfffd0 + 0x15] = ((IRET_TOP >> 4)     ) & 0xff;
                   11439:        mem[0xfffd0 + 0x16] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     11440:        
1.1.1.26  root     11441:        // boot routine
1.1       root     11442:        mem[0xffff0] = 0xf4;    // halt
                   11443:        mem[0xffff1] = 0xcd;    // int 21h
                   11444:        mem[0xffff2] = 0x21;
                   11445:        mem[0xffff3] = 0xcb;    // retf
                   11446:        
1.1.1.24  root     11447:        mem[0xffff5] = '0';     // rom date
                   11448:        mem[0xffff6] = '2';
                   11449:        mem[0xffff7] = '/';
                   11450:        mem[0xffff8] = '2';
                   11451:        mem[0xffff9] = '2';
                   11452:        mem[0xffffa] = '/';
                   11453:        mem[0xffffb] = '0';
                   11454:        mem[0xffffc] = '6';
                   11455:        mem[0xffffe] = 0xfc;    // machine id
                   11456:        mem[0xfffff] = 0x00;
                   11457:        
1.1       root     11458:        // param block
                   11459:        // + 0: param block (22bytes)
                   11460:        // +24: fcb1/2 (20bytes)
                   11461:        // +44: command tail (128bytes)
                   11462:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11463:        param->env_seg = 0;
                   11464:        param->cmd_line.w.l = 44;
                   11465:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11466:        param->fcb1.w.l = 24;
                   11467:        param->fcb1.w.h = (WORK_TOP >> 4);
                   11468:        param->fcb2.w.l = 24;
                   11469:        param->fcb2.w.h = (WORK_TOP >> 4);
                   11470:        
                   11471:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11472:        
                   11473:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   11474:        if(argc > 1) {
                   11475:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   11476:                for(int i = 2; i < argc; i++) {
                   11477:                        char tmp[128];
                   11478:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   11479:                        strcpy(cmd_line->cmd, tmp);
                   11480:                }
                   11481:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   11482:        } else {
                   11483:                cmd_line->len = 0;
                   11484:        }
                   11485:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11486:        
                   11487:        // system file table
1.1.1.21  root     11488:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   11489:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     11490:        
1.1.1.19  root     11491:        // disk buffer header (from DOSBox)
                   11492:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   11493:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   11494:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   11495:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   11496:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   11497:        
1.1       root     11498:        // current directory structure
                   11499:        msdos_cds_update(_getdrive() - 1);
                   11500:        
                   11501:        // fcb table
                   11502:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     11503:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     11504:        
1.1.1.22  root     11505:        // error table
                   11506:        *(UINT8 *)(mem + ERR_TABLE_TOP + 0) = 0xff;
                   11507:        *(UINT8 *)(mem + ERR_TABLE_TOP + 1) = 0x04;
                   11508:        *(UINT8 *)(mem + ERR_TABLE_TOP + 2) = 0x00;
                   11509:        *(UINT8 *)(mem + ERR_TABLE_TOP + 3) = 0x00;
                   11510:        
1.1.1.17  root     11511:        // nls stuff
                   11512:        msdos_nls_tables_init();
1.1       root     11513:        
                   11514:        // execute command
1.1.1.28! root     11515:        try {
        !          11516:                if(msdos_process_exec(argv[0], param, 0)) {
        !          11517:                        fatalerror("'%s' not found\n", argv[0]);
        !          11518:                }
        !          11519:        } catch(...) {
        !          11520:                // we should not reach here :-(
        !          11521:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     11522:        }
                   11523:        retval = 0;
                   11524:        return(0);
                   11525: }
                   11526: 
                   11527: #define remove_std_file(path) { \
                   11528:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   11529:        if(fd != -1) { \
                   11530:                _lseek(fd, 0, SEEK_END); \
                   11531:                int size = _tell(fd); \
                   11532:                _close(fd); \
                   11533:                if(size == 0) { \
                   11534:                        remove(path); \
                   11535:                } \
                   11536:        } \
                   11537: }
                   11538: 
                   11539: void msdos_finish()
                   11540: {
                   11541:        for(int i = 0; i < MAX_FILES; i++) {
                   11542:                if(file_handler[i].valid) {
                   11543:                        _close(i);
                   11544:                }
                   11545:        }
1.1.1.21  root     11546: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     11547:        remove_std_file("stdaux.txt");
1.1.1.21  root     11548: #endif
                   11549: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     11550:        remove_std_file("stdprn.txt");
                   11551: #endif
                   11552:        msdos_dbcs_table_finish();
                   11553: }
                   11554: 
                   11555: /* ----------------------------------------------------------------------------
                   11556:        PC/AT hardware emulation
                   11557: ---------------------------------------------------------------------------- */
                   11558: 
                   11559: void hardware_init()
                   11560: {
1.1.1.3   root     11561:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     11562:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     11563:        m_IF = 1;
1.1.1.3   root     11564: #if defined(HAS_I386)
1.1       root     11565:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   11566:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     11567: #endif
                   11568:        i386_set_a20_line(0);
1.1.1.14  root     11569:        
1.1.1.19  root     11570:        ems_init();
1.1.1.25  root     11571:        dma_init();
1.1       root     11572:        pic_init();
1.1.1.25  root     11573:        pio_init();
1.1.1.8   root     11574: #ifdef PIT_ALWAYS_RUNNING
                   11575:        pit_init();
                   11576: #else
1.1       root     11577:        pit_active = 0;
                   11578: #endif
1.1.1.25  root     11579:        sio_init();
1.1.1.8   root     11580:        cmos_init();
                   11581:        kbd_init();
1.1       root     11582: }
                   11583: 
1.1.1.10  root     11584: void hardware_finish()
                   11585: {
                   11586: #if defined(HAS_I386)
                   11587:        vtlb_free(m_vtlb);
                   11588: #endif
1.1.1.19  root     11589:        ems_finish();
1.1.1.25  root     11590:        sio_finish();
1.1.1.10  root     11591: }
                   11592: 
1.1.1.28! root     11593: void hardware_release()
        !          11594: {
        !          11595:        // release hardware resources when this program will be terminated abnormally
        !          11596: #ifdef EXPORT_DEBUG_TO_FILE
        !          11597:        if(fdebug != NULL) {
        !          11598:                fclose(fdebug);
        !          11599:                fdebug = NULL;
        !          11600:        }
        !          11601: #endif
        !          11602: #if defined(HAS_I386)
        !          11603:        vtlb_free(m_vtlb);
        !          11604: #endif
        !          11605:        ems_release();
        !          11606:        sio_release();
        !          11607: }
        !          11608: 
1.1       root     11609: void hardware_run()
                   11610: {
                   11611:        int ops = 0;
                   11612:        
1.1.1.22  root     11613: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28! root     11614:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.22  root     11615:        fdebug = fopen("debug.log", "w");
                   11616: #endif
1.1.1.3   root     11617:        while(!m_halted) {
1.1.1.22  root     11618: #ifdef ENABLE_DEBUG_DASM
                   11619:                if(dasm > 0) {
1.1       root     11620:                        char buffer[256];
1.1.1.3   root     11621: #if defined(HAS_I386)
1.1.1.22  root     11622:                        UINT32 flags = get_flags();
1.1.1.19  root     11623:                        UINT32 eip = m_eip;
1.1.1.3   root     11624: #else
1.1.1.22  root     11625:                        UINT32 flags = CompressFlags();
1.1.1.19  root     11626:                        UINT32 eip = m_pc - SREG_BASE(CS);
1.1.1.3   root     11627: #endif
                   11628:                        UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1       root     11629:                        
1.1.1.3   root     11630: #if defined(HAS_I386)
                   11631:                        if(m_operand_size) {
1.1       root     11632:                                CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3   root     11633:                        } else
                   11634: #endif
                   11635:                        CPU_DISASSEMBLE_CALL(x86_16);
1.1.1.22  root     11636:                        
                   11637:                        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",
                   11638:                        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,
                   11639: #if defined(HAS_I386)
                   11640:                        PROTECTED_MODE ? "PE" : "--",
                   11641: #else
                   11642:                        "--",
                   11643: #endif
                   11644:                        (flags & 0x40000) ? 'A' : '-',
                   11645:                        (flags & 0x20000) ? 'V' : '-',
                   11646:                        (flags & 0x10000) ? 'R' : '-',
                   11647:                        (flags & 0x04000) ? 'N' : '-',
                   11648:                        (flags & 0x02000) ? '1' : '0',
                   11649:                        (flags & 0x01000) ? '1' : '0',
                   11650:                        (flags & 0x00800) ? 'O' : '-',
                   11651:                        (flags & 0x00400) ? 'D' : '-',
                   11652:                        (flags & 0x00200) ? 'I' : '-',
                   11653:                        (flags & 0x00100) ? 'T' : '-',
                   11654:                        (flags & 0x00080) ? 'S' : '-',
                   11655:                        (flags & 0x00040) ? 'Z' : '-',
                   11656:                        (flags & 0x00010) ? 'A' : '-',
                   11657:                        (flags & 0x00004) ? 'P' : '-',
                   11658:                        (flags & 0x00001) ? 'C' : '-');
                   11659:                        fprintf(fdebug, "%04X:%04X\t%s\n", SREG(CS), (unsigned)eip, buffer);
                   11660:                        dasm--;
1.1       root     11661:                }
                   11662: #endif
1.1.1.3   root     11663: #if defined(HAS_I386)
                   11664:                m_cycles = 1;
1.1       root     11665:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     11666: #else
                   11667:                CPU_EXECUTE_CALL(CPU_MODEL);
                   11668: #endif
1.1.1.14  root     11669: #if defined(HAS_I386)
                   11670:                if(m_eip != m_prev_eip) {
                   11671: #else
                   11672:                if(m_pc != m_prevpc) {
                   11673: #endif
                   11674:                        iops++;
                   11675:                }
1.1.1.8   root     11676:                if(++ops == 16384) {
1.1       root     11677:                        hardware_update();
                   11678:                        ops = 0;
                   11679:                }
                   11680:        }
1.1.1.22  root     11681: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28! root     11682:        if(fdebug != NULL) {
        !          11683:                fclose(fdebug);
        !          11684:                fdebug = NULL;
        !          11685:        }
1.1.1.22  root     11686: #endif
1.1       root     11687: }
                   11688: 
                   11689: void hardware_update()
                   11690: {
1.1.1.8   root     11691:        static UINT32 prev_time = 0;
                   11692:        UINT32 cur_time = timeGetTime();
                   11693:        
                   11694:        if(prev_time != cur_time) {
                   11695:                // update pit and raise irq0
                   11696: #ifndef PIT_ALWAYS_RUNNING
                   11697:                if(pit_active)
                   11698: #endif
                   11699:                {
                   11700:                        if(pit_run(0, cur_time)) {
                   11701:                                pic_req(0, 0, 1);
                   11702:                        }
                   11703:                        pit_run(1, cur_time);
                   11704:                        pit_run(2, cur_time);
                   11705:                }
1.1.1.24  root     11706:                
1.1.1.25  root     11707:                // update sio and raise irq4/3
                   11708:                for(int c = 0; c < 2; c++) {
                   11709:                        sio_update(c);
                   11710:                }
                   11711:                
1.1.1.24  root     11712:                // update keyboard and mouse
1.1.1.14  root     11713:                static UINT32 prev_tick = 0;
                   11714:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     11715:                
1.1.1.14  root     11716:                if(prev_tick != cur_tick) {
                   11717:                        // update keyboard flags
                   11718:                        UINT8 state;
1.1.1.24  root     11719:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   11720:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   11721:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   11722:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   11723:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   11724:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   11725:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   11726:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     11727:                        mem[0x417] = state;
                   11728:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   11729:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   11730:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   11731:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     11732: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   11733: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     11734:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   11735:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   11736:                        mem[0x418] = state;
                   11737:                        
1.1.1.24  root     11738:                        // update console input if needed
                   11739:                        if(!key_changed || mouse.active) {
                   11740:                                update_console_input();
                   11741:                        }
                   11742:                        
                   11743:                        // raise irq1 if key is pressed/released
                   11744:                        if(key_changed) {
1.1.1.8   root     11745:                                pic_req(0, 1, 1);
1.1.1.24  root     11746:                                key_changed = false;
                   11747:                        }
                   11748:                        
                   11749:                        // raise irq12 if mouse status is changed
                   11750:                        if(mouse.status & mouse.call_mask) {
                   11751:                                if(mouse.active) {
                   11752:                                        pic_req(1, 4, 1);
                   11753:                                        mouse.status_irq = mouse.status & mouse.call_mask;
                   11754:                                }
                   11755:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     11756:                        }
1.1.1.24  root     11757:                        
1.1.1.14  root     11758:                        prev_tick = cur_tick;
1.1.1.8   root     11759:                }
1.1.1.24  root     11760:                
1.1.1.19  root     11761:                // update daily timer counter
                   11762:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     11763:                
1.1.1.8   root     11764:                prev_time = cur_time;
1.1       root     11765:        }
                   11766: }
                   11767: 
1.1.1.19  root     11768: // ems
                   11769: 
                   11770: void ems_init()
                   11771: {
                   11772:        memset(ems_handles, 0, sizeof(ems_handles));
                   11773:        memset(ems_pages, 0, sizeof(ems_pages));
                   11774:        free_ems_pages = MAX_EMS_PAGES;
                   11775: }
                   11776: 
                   11777: void ems_finish()
                   11778: {
1.1.1.28! root     11779:        ems_release();
        !          11780: }
        !          11781: 
        !          11782: void ems_release()
        !          11783: {
1.1.1.19  root     11784:        for(int i = 0; i < MAX_EMS_HANDLES; i++) {
1.1.1.28! root     11785:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     11786:                        free(ems_handles[i].buffer);
                   11787:                        ems_handles[i].buffer = NULL;
                   11788:                }
                   11789:        }
                   11790: }
                   11791: 
                   11792: void ems_allocate_pages(int handle, int pages)
                   11793: {
                   11794:        if(pages > 0) {
                   11795:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   11796:        } else {
                   11797:                ems_handles[handle].buffer = NULL;
                   11798:        }
                   11799:        ems_handles[handle].pages = pages;
                   11800:        ems_handles[handle].allocated = true;
                   11801:        free_ems_pages -= pages;
                   11802: }
                   11803: 
                   11804: void ems_reallocate_pages(int handle, int pages)
                   11805: {
                   11806:        if(ems_handles[handle].allocated) {
                   11807:                if(ems_handles[handle].pages != pages) {
                   11808:                        UINT8 *new_buffer = NULL;
                   11809:                        
                   11810:                        if(pages > 0) {
                   11811:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   11812:                        }
                   11813:                        if(ems_handles[handle].buffer) {
                   11814:                                if(new_buffer) {
                   11815:                                        if(pages > ems_handles[handle].pages) {
                   11816:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   11817:                                        } else {
                   11818:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   11819:                                        }
                   11820:                                }
                   11821:                                free(ems_handles[handle].buffer);
                   11822:                                ems_handles[handle].buffer = NULL;
                   11823:                        }
                   11824:                        free_ems_pages += ems_handles[handle].pages;
                   11825:                        
                   11826:                        ems_handles[handle].buffer = new_buffer;
                   11827:                        ems_handles[handle].pages = pages;
                   11828:                        free_ems_pages -= pages;
                   11829:                }
                   11830:        } else {
                   11831:                ems_allocate_pages(handle, pages);
                   11832:        }
                   11833: }
                   11834: 
                   11835: void ems_release_pages(int handle)
                   11836: {
                   11837:        if(ems_handles[handle].allocated) {
                   11838:                if(ems_handles[handle].buffer) {
                   11839:                        free(ems_handles[handle].buffer);
                   11840:                        ems_handles[handle].buffer = NULL;
                   11841:                }
                   11842:                free_ems_pages += ems_handles[handle].pages;
                   11843:                ems_handles[handle].allocated = false;
                   11844:        }
                   11845: }
                   11846: 
                   11847: void ems_map_page(int physical, int handle, int logical)
                   11848: {
                   11849:        if(ems_pages[physical].mapped) {
                   11850:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   11851:                        return;
                   11852:                }
                   11853:                ems_unmap_page(physical);
                   11854:        }
                   11855:        if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   11856:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   11857:        }
                   11858:        ems_pages[physical].handle = handle;
                   11859:        ems_pages[physical].page = logical;
                   11860:        ems_pages[physical].mapped = true;
                   11861: }
                   11862: 
                   11863: void ems_unmap_page(int physical)
                   11864: {
                   11865:        if(ems_pages[physical].mapped) {
                   11866:                int handle = ems_pages[physical].handle;
                   11867:                int logical = ems_pages[physical].page;
                   11868:                
                   11869:                if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   11870:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   11871:                }
                   11872:                ems_pages[physical].mapped = false;
                   11873:        }
                   11874: }
                   11875: 
1.1.1.25  root     11876: // dma
1.1       root     11877: 
1.1.1.25  root     11878: void dma_init()
1.1       root     11879: {
1.1.1.26  root     11880:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     11881:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     11882: //             for(int ch = 0; ch < 4; ch++) {
                   11883: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   11884: //             }
1.1.1.25  root     11885:                dma_reset(c);
                   11886:        }
1.1       root     11887: }
                   11888: 
1.1.1.25  root     11889: void dma_reset(int c)
1.1       root     11890: {
1.1.1.25  root     11891:        dma[c].low_high = false;
                   11892:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   11893:        dma[c].mask = 0xff;
                   11894: }
                   11895: 
                   11896: void dma_write(int c, UINT32 addr, UINT8 data)
                   11897: {
                   11898:        int ch = (addr >> 1) & 3;
                   11899:        UINT8 bit = 1 << (data & 3);
                   11900:        
                   11901:        switch(addr & 0x0f) {
                   11902:        case 0x00: case 0x02: case 0x04: case 0x06:
                   11903:                if(dma[c].low_high) {
                   11904:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     11905:                } else {
1.1.1.25  root     11906:                        dma[c].ch[ch].bareg.b.l = data;
                   11907:                }
                   11908:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   11909:                dma[c].low_high = !dma[c].low_high;
                   11910:                break;
                   11911:        case 0x01: case 0x03: case 0x05: case 0x07:
                   11912:                if(dma[c].low_high) {
                   11913:                        dma[c].ch[ch].bcreg.b.h = data;
                   11914:                } else {
                   11915:                        dma[c].ch[ch].bcreg.b.l = data;
                   11916:                }
                   11917:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   11918:                dma[c].low_high = !dma[c].low_high;
                   11919:                break;
                   11920:        case 0x08:
                   11921:                // command register
                   11922:                dma[c].cmd = data;
                   11923:                break;
                   11924:        case 0x09:
                   11925:                // dma[c].request register
                   11926:                if(data & 4) {
                   11927:                        if(!(dma[c].req & bit)) {
                   11928:                                dma[c].req |= bit;
                   11929: //                             dma_run(c, ch);
                   11930:                        }
                   11931:                } else {
                   11932:                        dma[c].req &= ~bit;
                   11933:                }
                   11934:                break;
                   11935:        case 0x0a:
                   11936:                // single mask register
                   11937:                if(data & 4) {
                   11938:                        dma[c].mask |= bit;
                   11939:                } else {
                   11940:                        dma[c].mask &= ~bit;
                   11941:                }
                   11942:                break;
                   11943:        case 0x0b:
                   11944:                // mode register
                   11945:                dma[c].ch[data & 3].mode = data;
                   11946:                break;
                   11947:        case 0x0c:
                   11948:                dma[c].low_high = false;
                   11949:                break;
                   11950:        case 0x0d:
                   11951:                // clear master
                   11952:                dma_reset(c);
                   11953:                break;
                   11954:        case 0x0e:
                   11955:                // clear mask register
                   11956:                dma[c].mask = 0;
                   11957:                break;
                   11958:        case 0x0f:
                   11959:                // all mask register
                   11960:                dma[c].mask = data & 0x0f;
                   11961:                break;
                   11962:        }
                   11963: }
                   11964: 
                   11965: UINT8 dma_read(int c, UINT32 addr)
                   11966: {
                   11967:        int ch = (addr >> 1) & 3;
                   11968:        UINT8 val = 0xff;
                   11969:        
                   11970:        switch(addr & 0x0f) {
                   11971:        case 0x00: case 0x02: case 0x04: case 0x06:
                   11972:                if(dma[c].low_high) {
                   11973:                        val = dma[c].ch[ch].areg.b.h;
                   11974:                } else {
                   11975:                        val = dma[c].ch[ch].areg.b.l;
                   11976:                }
                   11977:                dma[c].low_high = !dma[c].low_high;
                   11978:                return(val);
                   11979:        case 0x01: case 0x03: case 0x05: case 0x07:
                   11980:                if(dma[c].low_high) {
                   11981:                        val = dma[c].ch[ch].creg.b.h;
                   11982:                } else {
                   11983:                        val = dma[c].ch[ch].creg.b.l;
                   11984:                }
                   11985:                dma[c].low_high = !dma[c].low_high;
                   11986:                return(val);
                   11987:        case 0x08:
                   11988:                // status register
                   11989:                val = (dma[c].req << 4) | dma[c].tc;
                   11990:                dma[c].tc = 0;
                   11991:                return(val);
                   11992:        case 0x0d:
1.1.1.26  root     11993:                // temporary register (intel 82374 does not support)
1.1.1.25  root     11994:                return(dma[c].tmp & 0xff);
1.1.1.26  root     11995:        case 0x0f:
                   11996:                // mask register (intel 82374 does support)
                   11997:                return(dma[c].mask);
1.1.1.25  root     11998:        }
                   11999:        return(0xff);
                   12000: }
                   12001: 
                   12002: void dma_page_write(int c, int ch, UINT8 data)
                   12003: {
                   12004:        dma[c].ch[ch].pagereg = data;
                   12005: }
                   12006: 
                   12007: UINT8 dma_page_read(int c, int ch)
                   12008: {
                   12009:        return(dma[c].ch[ch].pagereg);
                   12010: }
                   12011: 
                   12012: void dma_run(int c, int ch)
                   12013: {
                   12014:        UINT8 bit = 1 << ch;
                   12015:        
                   12016:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   12017:                // execute dma
                   12018:                while(dma[c].req & bit) {
                   12019:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   12020:                                // memory -> memory
                   12021:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   12022:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   12023:                                
                   12024:                                if(c == 0) {
                   12025:                                        dma[c].tmp = read_byte(saddr);
                   12026:                                        write_byte(daddr, dma[c].tmp);
                   12027:                                } else {
                   12028:                                        dma[c].tmp = read_word(saddr << 1);
                   12029:                                        write_word(daddr << 1, dma[c].tmp);
                   12030:                                }
                   12031:                                if(!(dma[c].cmd & 0x02)) {
                   12032:                                        if(dma[c].ch[0].mode & 0x20) {
                   12033:                                                dma[c].ch[0].areg.w--;
                   12034:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   12035:                                                        dma[c].ch[0].pagereg--;
                   12036:                                                }
                   12037:                                        } else {
                   12038:                                                dma[c].ch[0].areg.w++;
                   12039:                                                if(dma[c].ch[0].areg.w == 0) {
                   12040:                                                        dma[c].ch[0].pagereg++;
                   12041:                                                }
                   12042:                                        }
                   12043:                                }
                   12044:                                if(dma[c].ch[1].mode & 0x20) {
                   12045:                                        dma[c].ch[1].areg.w--;
                   12046:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   12047:                                                dma[c].ch[1].pagereg--;
                   12048:                                        }
                   12049:                                } else {
                   12050:                                        dma[c].ch[1].areg.w++;
                   12051:                                        if(dma[c].ch[1].areg.w == 0) {
                   12052:                                                dma[c].ch[1].pagereg++;
                   12053:                                        }
                   12054:                                }
                   12055:                                
                   12056:                                // check dma condition
                   12057:                                if(dma[c].ch[0].creg.w-- == 0) {
                   12058:                                        if(dma[c].ch[0].mode & 0x10) {
                   12059:                                                // self initialize
                   12060:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   12061:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   12062:                                        } else {
                   12063: //                                             dma[c].mask |= bit;
                   12064:                                        }
                   12065:                                }
                   12066:                                if(dma[c].ch[1].creg.w-- == 0) {
                   12067:                                        // terminal count
                   12068:                                        if(dma[c].ch[1].mode & 0x10) {
                   12069:                                                // self initialize
                   12070:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   12071:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   12072:                                        } else {
                   12073:                                                dma[c].mask |= bit;
                   12074:                                        }
                   12075:                                        dma[c].req &= ~bit;
                   12076:                                        dma[c].tc |= bit;
                   12077:                                }
                   12078:                        } else {
                   12079:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   12080:                                
                   12081:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   12082:                                        // verify
                   12083:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   12084:                                        // io -> memory
                   12085:                                        if(c == 0) {
                   12086:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   12087:                                                write_byte(addr, dma[c].tmp);
                   12088:                                        } else {
                   12089:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   12090:                                                write_word(addr << 1, dma[c].tmp);
                   12091:                                        }
                   12092:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   12093:                                        // memory -> io
                   12094:                                        if(c == 0) {
                   12095:                                                dma[c].tmp = read_byte(addr);
                   12096:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   12097:                                        } else {
                   12098:                                                dma[c].tmp = read_word(addr << 1);
                   12099:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   12100:                                        }
                   12101:                                }
                   12102:                                if(dma[c].ch[ch].mode & 0x20) {
                   12103:                                        dma[c].ch[ch].areg.w--;
                   12104:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   12105:                                                dma[c].ch[ch].pagereg--;
                   12106:                                        }
                   12107:                                } else {
                   12108:                                        dma[c].ch[ch].areg.w++;
                   12109:                                        if(dma[c].ch[ch].areg.w == 0) {
                   12110:                                                dma[c].ch[ch].pagereg++;
                   12111:                                        }
                   12112:                                }
                   12113:                                
                   12114:                                // check dma condition
                   12115:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   12116:                                        // terminal count
                   12117:                                        if(dma[c].ch[ch].mode & 0x10) {
                   12118:                                                // self initialize
                   12119:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   12120:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   12121:                                        } else {
                   12122:                                                dma[c].mask |= bit;
                   12123:                                        }
                   12124:                                        dma[c].req &= ~bit;
                   12125:                                        dma[c].tc |= bit;
                   12126:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   12127:                                        // single mode
                   12128:                                        break;
                   12129:                                }
                   12130:                        }
                   12131:                }
                   12132:        }
                   12133: }
                   12134: 
                   12135: // pic
                   12136: 
                   12137: void pic_init()
                   12138: {
                   12139:        memset(pic, 0, sizeof(pic));
                   12140:        pic[0].imr = pic[1].imr = 0xff;
                   12141:        
                   12142:        // from bochs bios
                   12143:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   12144:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   12145:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   12146:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   12147:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   12148:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   12149:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   12150:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   12151:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   12152: }
                   12153: 
                   12154: void pic_write(int c, UINT32 addr, UINT8 data)
                   12155: {
                   12156:        if(addr & 1) {
                   12157:                if(pic[c].icw2_r) {
                   12158:                        // icw2
                   12159:                        pic[c].icw2 = data;
                   12160:                        pic[c].icw2_r = 0;
                   12161:                } else if(pic[c].icw3_r) {
                   12162:                        // icw3
                   12163:                        pic[c].icw3 = data;
                   12164:                        pic[c].icw3_r = 0;
                   12165:                } else if(pic[c].icw4_r) {
                   12166:                        // icw4
                   12167:                        pic[c].icw4 = data;
                   12168:                        pic[c].icw4_r = 0;
                   12169:                } else {
                   12170:                        // ocw1
1.1       root     12171:                        pic[c].imr = data;
                   12172:                }
                   12173:        } else {
                   12174:                if(data & 0x10) {
                   12175:                        // icw1
                   12176:                        pic[c].icw1 = data;
                   12177:                        pic[c].icw2_r = 1;
                   12178:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   12179:                        pic[c].icw4_r = data & 1;
                   12180:                        pic[c].irr = 0;
                   12181:                        pic[c].isr = 0;
                   12182:                        pic[c].imr = 0;
                   12183:                        pic[c].prio = 0;
                   12184:                        if(!(pic[c].icw1 & 1)) {
                   12185:                                pic[c].icw4 = 0;
                   12186:                        }
                   12187:                        pic[c].ocw3 = 0;
                   12188:                } else if(data & 8) {
                   12189:                        // ocw3
                   12190:                        if(!(data & 2)) {
                   12191:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   12192:                        }
                   12193:                        if(!(data & 0x40)) {
                   12194:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   12195:                        }
                   12196:                        pic[c].ocw3 = data;
                   12197:                } else {
                   12198:                        // ocw2
                   12199:                        int level = 0;
                   12200:                        if(data & 0x40) {
                   12201:                                level = data & 7;
                   12202:                        } else {
                   12203:                                if(!pic[c].isr) {
                   12204:                                        return;
                   12205:                                }
                   12206:                                level = pic[c].prio;
                   12207:                                while(!(pic[c].isr & (1 << level))) {
                   12208:                                        level = (level + 1) & 7;
                   12209:                                }
                   12210:                        }
                   12211:                        if(data & 0x80) {
                   12212:                                pic[c].prio = (level + 1) & 7;
                   12213:                        }
                   12214:                        if(data & 0x20) {
                   12215:                                pic[c].isr &= ~(1 << level);
                   12216:                        }
                   12217:                }
                   12218:        }
                   12219:        pic_update();
                   12220: }
                   12221: 
                   12222: UINT8 pic_read(int c, UINT32 addr)
                   12223: {
                   12224:        if(addr & 1) {
                   12225:                return(pic[c].imr);
                   12226:        } else {
                   12227:                // polling mode is not supported...
                   12228:                //if(pic[c].ocw3 & 4) {
                   12229:                //      return ???;
                   12230:                //}
                   12231:                if(pic[c].ocw3 & 1) {
                   12232:                        return(pic[c].isr);
                   12233:                } else {
                   12234:                        return(pic[c].irr);
                   12235:                }
                   12236:        }
                   12237: }
                   12238: 
                   12239: void pic_req(int c, int level, int signal)
                   12240: {
                   12241:        if(signal) {
                   12242:                pic[c].irr |= (1 << level);
                   12243:        } else {
                   12244:                pic[c].irr &= ~(1 << level);
                   12245:        }
                   12246:        pic_update();
                   12247: }
                   12248: 
                   12249: int pic_ack()
                   12250: {
                   12251:        // ack (INTA=L)
                   12252:        pic[pic_req_chip].isr |= pic_req_bit;
                   12253:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   12254:        if(pic_req_chip > 0) {
                   12255:                // update isr and irr of master
                   12256:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   12257:                pic[pic_req_chip - 1].isr |= slave;
                   12258:                pic[pic_req_chip - 1].irr &= ~slave;
                   12259:        }
                   12260:        //if(pic[pic_req_chip].icw4 & 1) {
                   12261:                // 8086 mode
                   12262:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   12263:        //} else {
                   12264:        //      // 8080 mode
                   12265:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   12266:        //      if(pic[pic_req_chip].icw1 & 4) {
                   12267:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   12268:        //      } else {
                   12269:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   12270:        //      }
                   12271:        //      vector = 0xcd | (addr << 8);
                   12272:        //}
                   12273:        if(pic[pic_req_chip].icw4 & 2) {
                   12274:                // auto eoi
                   12275:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   12276:        }
                   12277:        return(vector);
                   12278: }
                   12279: 
                   12280: void pic_update()
                   12281: {
                   12282:        for(int c = 0; c < 2; c++) {
                   12283:                UINT8 irr = pic[c].irr;
                   12284:                if(c + 1 < 2) {
                   12285:                        // this is master
                   12286:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   12287:                                // request from slave
                   12288:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   12289:                        }
                   12290:                }
                   12291:                irr &= (~pic[c].imr);
                   12292:                if(!irr) {
                   12293:                        break;
                   12294:                }
                   12295:                if(!(pic[c].ocw3 & 0x20)) {
                   12296:                        irr |= pic[c].isr;
                   12297:                }
                   12298:                int level = pic[c].prio;
                   12299:                UINT8 bit = 1 << level;
                   12300:                while(!(irr & bit)) {
                   12301:                        level = (level + 1) & 7;
                   12302:                        bit = 1 << level;
                   12303:                }
                   12304:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   12305:                        // check slave
                   12306:                        continue;
                   12307:                }
                   12308:                if(pic[c].isr & bit) {
                   12309:                        break;
                   12310:                }
                   12311:                // interrupt request
                   12312:                pic_req_chip = c;
                   12313:                pic_req_level = level;
                   12314:                pic_req_bit = bit;
1.1.1.3   root     12315:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     12316:                return;
                   12317:        }
1.1.1.3   root     12318:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     12319: }
1.1       root     12320: 
1.1.1.25  root     12321: // pio
                   12322: 
                   12323: void pio_init()
                   12324: {
1.1.1.26  root     12325:        memset(pio, 0, sizeof(pio));
1.1.1.25  root     12326:        for(int c = 0; c < 2; c++) {
                   12327:                pio[c].stat = 0xde;
                   12328:                pio[c].ctrl = 0x0c;
                   12329:        }
                   12330: }
                   12331: 
                   12332: void pio_write(int c, UINT32 addr, UINT8 data)
                   12333: {
                   12334:        switch(addr & 3) {
                   12335:        case 0:
                   12336:                pio[c].data = data;
                   12337:                break;
                   12338:        case 2:
                   12339:                pio[c].ctrl = data;
                   12340:                break;
                   12341:        }
                   12342: }
                   12343: 
                   12344: UINT8 pio_read(int c, UINT32 addr)
                   12345: {
                   12346:        switch(addr & 3) {
                   12347:        case 0:
                   12348:                return(pio[c].data);
                   12349:        case 1:
                   12350:                return(pio[c].stat);
                   12351:        case 2:
                   12352:                return(pio[c].ctrl);
                   12353:        }
                   12354:        return(0xff);
                   12355: }
                   12356: 
1.1       root     12357: // pit
                   12358: 
1.1.1.22  root     12359: #define PIT_FREQ 1193182ULL
1.1       root     12360: #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)
                   12361: 
                   12362: void pit_init()
                   12363: {
1.1.1.8   root     12364:        memset(pit, 0, sizeof(pit));
1.1       root     12365:        for(int ch = 0; ch < 3; ch++) {
                   12366:                pit[ch].count = 0x10000;
                   12367:                pit[ch].ctrl_reg = 0x34;
                   12368:                pit[ch].mode = 3;
                   12369:        }
                   12370:        
                   12371:        // from bochs bios
                   12372:        pit_write(3, 0x34);
                   12373:        pit_write(0, 0x00);
                   12374:        pit_write(0, 0x00);
                   12375: }
                   12376: 
                   12377: void pit_write(int ch, UINT8 val)
                   12378: {
1.1.1.8   root     12379: #ifndef PIT_ALWAYS_RUNNING
1.1       root     12380:        if(!pit_active) {
                   12381:                pit_active = 1;
                   12382:                pit_init();
                   12383:        }
1.1.1.8   root     12384: #endif
1.1       root     12385:        switch(ch) {
                   12386:        case 0:
                   12387:        case 1:
                   12388:        case 2:
                   12389:                // write count register
                   12390:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   12391:                        if(pit[ch].ctrl_reg & 0x10) {
                   12392:                                pit[ch].low_write = 1;
                   12393:                        }
                   12394:                        if(pit[ch].ctrl_reg & 0x20) {
                   12395:                                pit[ch].high_write = 1;
                   12396:                        }
                   12397:                }
                   12398:                if(pit[ch].low_write) {
                   12399:                        pit[ch].count_reg = val;
                   12400:                        pit[ch].low_write = 0;
                   12401:                } else if(pit[ch].high_write) {
                   12402:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   12403:                                pit[ch].count_reg = val << 8;
                   12404:                        } else {
                   12405:                                pit[ch].count_reg |= val << 8;
                   12406:                        }
                   12407:                        pit[ch].high_write = 0;
                   12408:                }
                   12409:                // start count
1.1.1.8   root     12410:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   12411:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   12412:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   12413:                                pit[ch].prev_time = timeGetTime();
                   12414:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     12415:                        }
                   12416:                }
                   12417:                break;
                   12418:        case 3: // ctrl reg
                   12419:                if((val & 0xc0) == 0xc0) {
                   12420:                        // i8254 read-back command
                   12421:                        for(ch = 0; ch < 3; ch++) {
                   12422:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   12423:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   12424:                                        pit[ch].status_latched = 1;
                   12425:                                }
                   12426:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   12427:                                        pit_latch_count(ch);
                   12428:                                }
                   12429:                        }
                   12430:                        break;
                   12431:                }
                   12432:                ch = (val >> 6) & 3;
                   12433:                if(val & 0x30) {
                   12434:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   12435:                        pit[ch].mode = modes[(val >> 1) & 7];
                   12436:                        pit[ch].count_latched = 0;
                   12437:                        pit[ch].low_read = pit[ch].high_read = 0;
                   12438:                        pit[ch].low_write = pit[ch].high_write = 0;
                   12439:                        pit[ch].ctrl_reg = val;
                   12440:                        // stop count
1.1.1.8   root     12441:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     12442:                        pit[ch].count_reg = 0;
                   12443:                } else if(!pit[ch].count_latched) {
                   12444:                        pit_latch_count(ch);
                   12445:                }
                   12446:                break;
                   12447:        }
                   12448: }
                   12449: 
                   12450: UINT8 pit_read(int ch)
                   12451: {
1.1.1.8   root     12452: #ifndef PIT_ALWAYS_RUNNING
1.1       root     12453:        if(!pit_active) {
                   12454:                pit_active = 1;
                   12455:                pit_init();
                   12456:        }
1.1.1.8   root     12457: #endif
1.1       root     12458:        switch(ch) {
                   12459:        case 0:
                   12460:        case 1:
                   12461:        case 2:
                   12462:                if(pit[ch].status_latched) {
                   12463:                        pit[ch].status_latched = 0;
                   12464:                        return(pit[ch].status);
                   12465:                }
                   12466:                // if not latched, through current count
                   12467:                if(!pit[ch].count_latched) {
                   12468:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   12469:                                pit_latch_count(ch);
                   12470:                        }
                   12471:                }
                   12472:                // return latched count
                   12473:                if(pit[ch].low_read) {
                   12474:                        pit[ch].low_read = 0;
                   12475:                        if(!pit[ch].high_read) {
                   12476:                                pit[ch].count_latched = 0;
                   12477:                        }
                   12478:                        return(pit[ch].latch & 0xff);
                   12479:                } else if(pit[ch].high_read) {
                   12480:                        pit[ch].high_read = 0;
                   12481:                        pit[ch].count_latched = 0;
                   12482:                        return((pit[ch].latch >> 8) & 0xff);
                   12483:                }
                   12484:        }
                   12485:        return(0xff);
                   12486: }
                   12487: 
1.1.1.8   root     12488: int pit_run(int ch, UINT32 cur_time)
1.1       root     12489: {
1.1.1.8   root     12490:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     12491:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     12492:                pit[ch].prev_time = pit[ch].expired_time;
                   12493:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   12494:                if(cur_time >= pit[ch].expired_time) {
                   12495:                        pit[ch].prev_time = cur_time;
                   12496:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     12497:                }
1.1.1.8   root     12498:                return(1);
1.1       root     12499:        }
1.1.1.8   root     12500:        return(0);
1.1       root     12501: }
                   12502: 
                   12503: void pit_latch_count(int ch)
                   12504: {
1.1.1.8   root     12505:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     12506:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     12507:                pit_run(ch, cur_time);
                   12508:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     12509:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   12510:                
                   12511:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   12512:                        // decrement counter in 1msec period
                   12513:                        if(pit[ch].next_latch == 0) {
                   12514:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   12515:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   12516:                        }
                   12517:                        if(pit[ch].latch > pit[ch].next_latch) {
                   12518:                                pit[ch].latch--;
                   12519:                        }
                   12520:                } else {
                   12521:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   12522:                        pit[ch].next_latch = 0;
                   12523:                }
1.1.1.8   root     12524:        } else {
                   12525:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     12526:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     12527:        }
                   12528:        pit[ch].count_latched = 1;
                   12529:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   12530:                // lower byte
                   12531:                pit[ch].low_read = 1;
                   12532:                pit[ch].high_read = 0;
                   12533:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   12534:                // upper byte
                   12535:                pit[ch].low_read = 0;
                   12536:                pit[ch].high_read = 1;
                   12537:        } else {
                   12538:                // lower -> upper
1.1.1.14  root     12539:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     12540:        }
                   12541: }
                   12542: 
1.1.1.8   root     12543: int pit_get_expired_time(int ch)
1.1       root     12544: {
1.1.1.22  root     12545:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   12546:        UINT64 val = pit[ch].accum >> 10;
                   12547:        pit[ch].accum -= val << 10;
                   12548:        return((val != 0) ? val : 1);
1.1.1.8   root     12549: }
                   12550: 
1.1.1.25  root     12551: // sio
                   12552: 
                   12553: void sio_init()
                   12554: {
1.1.1.26  root     12555:        memset(sio, 0, sizeof(sio));
                   12556:        memset(sio_mt, 0, sizeof(sio_mt));
                   12557:        
1.1.1.25  root     12558:        for(int c = 0; c < 2; c++) {
                   12559:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   12560:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   12561:                
                   12562:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   12563:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     12564:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   12565:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     12566:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   12567:                sio[c].irq_identify = 0x01;     // no pending irq
                   12568:                
                   12569:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   12570:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   12571:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   12572:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   12573:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   12574:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   12575:                
1.1.1.26  root     12576:                if(sio_port_number[c] != 0) {
1.1.1.25  root     12577:                        sio[c].channel = c;
                   12578:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   12579:                }
                   12580:        }
                   12581: }
                   12582: 
                   12583: void sio_finish()
                   12584: {
                   12585:        for(int c = 0; c < 2; c++) {
                   12586:                if(sio_mt[c].hThread != NULL) {
                   12587:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   12588:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28! root     12589:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     12590:                }
                   12591:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   12592:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   12593:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   12594:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   12595:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   12596:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28! root     12597:        }
        !          12598:        sio_release();
        !          12599: }
        !          12600: 
        !          12601: void sio_release()
        !          12602: {
        !          12603:        for(int c = 0; c < 2; c++) {
        !          12604:                // sio_thread() may access the resources :-(
        !          12605:                if(sio_mt[c].hThread == NULL) {
        !          12606:                        if(sio[c].send_buffer != NULL) {
        !          12607:                                sio[c].send_buffer->release();
        !          12608:                                delete sio[c].send_buffer;
        !          12609:                                sio[c].send_buffer = NULL;
        !          12610:                        }
        !          12611:                        if(sio[c].recv_buffer != NULL) {
        !          12612:                                sio[c].recv_buffer->release();
        !          12613:                                delete sio[c].recv_buffer;
        !          12614:                                sio[c].recv_buffer = NULL;
        !          12615:                        }
        !          12616:                }
1.1.1.25  root     12617:        }
                   12618: }
                   12619: 
                   12620: void sio_write(int c, UINT32 addr, UINT8 data)
                   12621: {
                   12622:        switch(addr & 7) {
                   12623:        case 0:
                   12624:                if(sio[c].selector & 0x80) {
                   12625:                        if(sio[c].divisor.b.l != data) {
                   12626:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   12627:                                sio[c].divisor.b.l = data;
                   12628:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   12629:                        }
                   12630:                } else {
                   12631:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   12632:                        sio[c].send_buffer->write(data);
                   12633:                        // transmitter holding/shift registers are not empty
                   12634:                        sio[c].line_stat_buf &= ~0x60;
                   12635:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   12636:                        
                   12637:                        if(sio[c].irq_enable & 0x02) {
                   12638:                                sio_update_irq(c);
                   12639:                        }
                   12640:                }
                   12641:                break;
                   12642:        case 1:
                   12643:                if(sio[c].selector & 0x80) {
                   12644:                        if(sio[c].divisor.b.h != data) {
                   12645:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   12646:                                sio[c].divisor.b.h = data;
                   12647:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   12648:                        }
                   12649:                } else {
                   12650:                        if(sio[c].irq_enable != data) {
                   12651:                                sio[c].irq_enable = data;
                   12652:                                sio_update_irq(c);
                   12653:                        }
                   12654:                }
                   12655:                break;
                   12656:        case 3:
                   12657:                {
                   12658:                        UINT8 line_ctrl = data & 0x3f;
                   12659:                        bool set_brk = ((data & 0x40) != 0);
                   12660:                        
                   12661:                        if(sio[c].line_ctrl != line_ctrl) {
                   12662:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   12663:                                sio[c].line_ctrl = line_ctrl;
                   12664:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   12665:                        }
                   12666:                        if(sio[c].set_brk != set_brk) {
                   12667:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   12668:                                sio[c].set_brk = set_brk;
                   12669:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   12670:                        }
                   12671:                }
                   12672:                sio[c].selector = data;
                   12673:                break;
                   12674:        case 4:
                   12675:                {
                   12676:                        bool set_dtr = ((data & 0x01) != 0);
                   12677:                        bool set_rts = ((data & 0x02) != 0);
                   12678:                        
                   12679:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     12680: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     12681:                                sio[c].set_dtr = set_dtr;
                   12682:                                sio[c].set_rts = set_rts;
1.1.1.26  root     12683: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   12684:                                
                   12685:                                bool state_changed = false;
                   12686:                                
                   12687:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   12688:                                if(set_dtr) {
                   12689:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   12690:                                } else {
                   12691:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   12692:                                }
                   12693:                                if(set_rts) {
                   12694:                                        sio[c].modem_stat |= 0x10;      // cts on
                   12695:                                } else {
                   12696:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   12697:                                }
                   12698:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   12699:                                        if(!(sio[c].modem_stat & 0x02)) {
                   12700:                                                if(sio[c].irq_enable & 0x08) {
                   12701:                                                        state_changed = true;
                   12702:                                                }
                   12703:                                                sio[c].modem_stat |= 0x02;
                   12704:                                        }
                   12705:                                }
                   12706:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   12707:                                        if(!(sio[c].modem_stat & 0x01)) {
                   12708:                                                if(sio[c].irq_enable & 0x08) {
                   12709:                                                        state_changed = true;
                   12710:                                                }
                   12711:                                                sio[c].modem_stat |= 0x01;
                   12712:                                        }
                   12713:                                }
                   12714:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   12715:                                
                   12716:                                if(state_changed) {
                   12717:                                        sio_update_irq(c);
                   12718:                                }
1.1.1.25  root     12719:                        }
                   12720:                }
                   12721:                sio[c].modem_ctrl = data;
                   12722:                break;
                   12723:        case 7:
                   12724:                sio[c].scratch = data;
                   12725:                break;
                   12726:        }
                   12727: }
                   12728: 
                   12729: UINT8 sio_read(int c, UINT32 addr)
                   12730: {
                   12731:        switch(addr & 7) {
                   12732:        case 0:
                   12733:                if(sio[c].selector & 0x80) {
                   12734:                        return(sio[c].divisor.b.l);
                   12735:                } else {
                   12736:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   12737:                        UINT8 data = sio[c].recv_buffer->read();
                   12738:                        // data is not ready
                   12739:                        sio[c].line_stat_buf &= ~0x01;
                   12740:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   12741:                        
                   12742:                        if(sio[c].irq_enable & 0x01) {
                   12743:                                sio_update_irq(c);
                   12744:                        }
                   12745:                        return(data);
                   12746:                }
                   12747:        case 1:
                   12748:                if(sio[c].selector & 0x80) {
                   12749:                        return(sio[c].divisor.b.h);
                   12750:                } else {
                   12751:                        return(sio[c].irq_enable);
                   12752:                }
                   12753:        case 2:
                   12754:                return(sio[c].irq_identify);
                   12755:        case 3:
                   12756:                return(sio[c].selector);
                   12757:        case 4:
                   12758:                return(sio[c].modem_ctrl);
                   12759:        case 5:
                   12760:                {
                   12761:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   12762:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   12763:                        sio[c].line_stat_err = 0x00;
                   12764:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   12765:                        
                   12766:                        bool state_changed = false;
                   12767:                        
                   12768:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   12769:                                EnterCriticalSection(&sio_mt[c].csSendData);
                   12770:                                if(!sio[c].send_buffer->full()) {
                   12771:                                        // transmitter holding register will be empty first
                   12772:                                        if(sio[c].irq_enable & 0x02) {
                   12773:                                                state_changed = true;
                   12774:                                        }
                   12775:                                        sio[c].line_stat_buf |= 0x20;
                   12776:                                }
                   12777:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   12778:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   12779:                                // transmitter shift register will be empty later
                   12780:                                sio[c].line_stat_buf |= 0x40;
                   12781:                        }
                   12782:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   12783:                                EnterCriticalSection(&sio_mt[c].csRecvData);
                   12784:                                if(!sio[c].recv_buffer->empty()) {
                   12785:                                        // data is ready
                   12786:                                        if(sio[c].irq_enable & 0x01) {
                   12787:                                                state_changed = true;
                   12788:                                        }
                   12789:                                        sio[c].line_stat_buf |= 0x01;
                   12790:                                }
                   12791:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   12792:                        }
                   12793:                        if(state_changed) {
                   12794:                                sio_update_irq(c);
                   12795:                        }
                   12796:                        return(val);
                   12797:                }
                   12798:        case 6:
                   12799:                {
                   12800:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   12801:                        UINT8 val = sio[c].modem_stat;
                   12802:                        sio[c].modem_stat &= 0xf0;
                   12803:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   12804:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   12805:                        
                   12806:                        if(sio[c].modem_ctrl & 0x10) {
                   12807:                                // loop-back
                   12808:                                val &= 0x0f;
                   12809:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   12810:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   12811:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   12812:                        }
                   12813:                        return(val);
                   12814:                }
                   12815:        case 7:
                   12816:                return(sio[c].scratch);
                   12817:        }
                   12818:        return(0xff);
                   12819: }
                   12820: 
                   12821: void sio_update(int c)
                   12822: {
                   12823:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   12824:                EnterCriticalSection(&sio_mt[c].csSendData);
                   12825:                if(!sio[c].send_buffer->full()) {
                   12826:                        // transmitter holding/shift registers will be empty
                   12827:                        sio[c].line_stat_buf |= 0x60;
                   12828:                }
                   12829:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   12830:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   12831:                // transmitter shift register will be empty
                   12832:                sio[c].line_stat_buf |= 0x40;
                   12833:        }
                   12834:        if(!(sio[c].line_stat_buf & 0x01)) {
                   12835:                EnterCriticalSection(&sio_mt[c].csRecvData);
                   12836:                if(!sio[c].recv_buffer->empty()) {
                   12837:                        // data is ready
                   12838:                        sio[c].line_stat_buf |= 0x01;
                   12839:                }
                   12840:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   12841:        }
                   12842:        sio_update_irq(c);
                   12843: }
                   12844: 
                   12845: void sio_update_irq(int c)
                   12846: {
                   12847:        int level = -1;
                   12848:        
                   12849:        if(sio[c].irq_enable & 0x08) {
                   12850:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   12851:                if((sio[c].modem_stat & 0x0f) != 0) {
                   12852:                        level = 0;
                   12853:                }
                   12854:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   12855:        }
                   12856:        if(sio[c].irq_enable & 0x02) {
                   12857:                if(sio[c].line_stat_buf & 0x20) {
                   12858:                        level = 1;
                   12859:                }
                   12860:        }
                   12861:        if(sio[c].irq_enable & 0x01) {
                   12862:                if(sio[c].line_stat_buf & 0x01) {
                   12863:                        level = 2;
                   12864:                }
                   12865:        }
                   12866:        if(sio[c].irq_enable & 0x04) {
                   12867:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   12868:                if(sio[c].line_stat_err != 0) {
                   12869:                        level = 3;
                   12870:                }
                   12871:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   12872:        }
                   12873:        if(level != -1) {
                   12874:                sio[c].irq_identify = level << 1;
                   12875:                pic_req(0, (c == 0) ? 4 : 3, 1);
                   12876:        } else {
                   12877:                sio[c].irq_identify = 1;
                   12878:                pic_req(0, (c == 0) ? 4 : 3, 0);
                   12879:        }
                   12880: }
                   12881: 
                   12882: DWORD WINAPI sio_thread(void *lpx)
                   12883: {
                   12884:        volatile sio_t *p = (sio_t *)lpx;
                   12885:        sio_mt_t *q = &sio_mt[p->channel];
                   12886:        
                   12887:        char name[] = "COM1";
1.1.1.26  root     12888:        name[3] = '0' + sio_port_number[p->channel];
                   12889:        HANDLE hComm = NULL;
                   12890:        COMMPROP commProp;
                   12891:        DCB dcb;
                   12892:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   12893:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   12894:        
                   12895:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   12896:                if(GetCommProperties(hComm, &commProp)) {
                   12897:                        dwSettableBaud = commProp.dwSettableBaud;
                   12898:                }
1.1.1.25  root     12899:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     12900: //             EscapeCommFunction(hComm, SETRTS);
                   12901: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     12902:                
                   12903:                while(!m_halted) {
                   12904:                        // setup comm port
                   12905:                        bool comm_state_changed = false;
                   12906:                        
                   12907:                        EnterCriticalSection(&q->csLineCtrl);
                   12908:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   12909:                                p->prev_divisor = p->divisor.w;
                   12910:                                p->prev_line_ctrl = p->line_ctrl;
                   12911:                                comm_state_changed = true;
                   12912:                        }
                   12913:                        LeaveCriticalSection(&q->csLineCtrl);
                   12914:                        
                   12915:                        if(comm_state_changed) {
1.1.1.26  root     12916:                                if(GetCommState(hComm, &dcb)) {
                   12917: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   12918:                                        DWORD baud = 115200 / p->prev_divisor;
                   12919:                                        dcb.BaudRate = 9600; // default
                   12920:                                        
                   12921:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   12922:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   12923:                                        // 134.5bps is not supported ???
                   12924: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   12925:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   12926:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   12927:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   12928:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   12929:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   12930:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   12931:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   12932:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   12933:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   12934: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   12935: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   12936: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   12937:                                        
                   12938:                                        switch(p->prev_line_ctrl & 0x03) {
                   12939:                                        case 0x00: dcb.ByteSize = 5; break;
                   12940:                                        case 0x01: dcb.ByteSize = 6; break;
                   12941:                                        case 0x02: dcb.ByteSize = 7; break;
                   12942:                                        case 0x03: dcb.ByteSize = 8; break;
                   12943:                                        }
                   12944:                                        switch(p->prev_line_ctrl & 0x04) {
                   12945:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   12946:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   12947:                                        }
                   12948:                                        switch(p->prev_line_ctrl & 0x38) {
                   12949:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   12950:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   12951:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   12952:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   12953:                                        default:   dcb.Parity = NOPARITY;    break;
                   12954:                                        }
                   12955:                                        dcb.fBinary = TRUE;
                   12956:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   12957:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   12958:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   12959:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   12960:                                        dcb.fTXContinueOnXoff = TRUE;
                   12961:                                        dcb.fOutX = dcb.fInX = FALSE;
                   12962:                                        dcb.fErrorChar = FALSE;
                   12963:                                        dcb.fNull = FALSE;
                   12964:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   12965:                                        dcb.fAbortOnError = FALSE;
                   12966:                                        
                   12967:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     12968:                                }
                   12969:                                
                   12970:                                // check again to apply all comm state changes
                   12971:                                Sleep(10);
                   12972:                                continue;
                   12973:                        }
                   12974:                        
                   12975:                        // set comm pins
                   12976:                        bool change_brk = false;
1.1.1.26  root     12977: //                     bool change_rts = false;
                   12978: //                     bool change_dtr = false;
1.1.1.25  root     12979:                        
                   12980:                        EnterCriticalSection(&q->csModemCtrl);
                   12981:                        if(p->prev_set_brk != p->set_brk) {
                   12982:                                p->prev_set_brk = p->set_brk;
                   12983:                                change_brk = true;
                   12984:                        }
1.1.1.26  root     12985: //                     if(p->prev_set_rts != p->set_rts) {
                   12986: //                             p->prev_set_rts = p->set_rts;
                   12987: //                             change_rts = true;
                   12988: //                     }
                   12989: //                     if(p->prev_set_dtr != p->set_dtr) {
                   12990: //                             p->prev_set_dtr = p->set_dtr;
                   12991: //                             change_dtr = true;
                   12992: //                     }
1.1.1.25  root     12993:                        LeaveCriticalSection(&q->csModemCtrl);
                   12994:                        
                   12995:                        if(change_brk) {
1.1.1.26  root     12996:                                static UINT32 clear_time = 0;
                   12997:                                if(p->prev_set_brk) {
                   12998:                                        EscapeCommFunction(hComm, SETBREAK);
                   12999:                                        clear_time = timeGetTime() + 200;
                   13000:                                } else {
                   13001:                                        // keep break for at least 200msec
                   13002:                                        UINT32 cur_time = timeGetTime();
                   13003:                                        if(clear_time > cur_time) {
                   13004:                                                Sleep(clear_time - cur_time);
                   13005:                                        }
                   13006:                                        EscapeCommFunction(hComm, CLRBREAK);
                   13007:                                }
1.1.1.25  root     13008:                        }
1.1.1.26  root     13009: //                     if(change_rts) {
                   13010: //                             if(p->prev_set_rts) {
                   13011: //                                     EscapeCommFunction(hComm, SETRTS);
                   13012: //                             } else {
                   13013: //                                     EscapeCommFunction(hComm, CLRRTS);
                   13014: //                             }
                   13015: //                     }
                   13016: //                     if(change_dtr) {
                   13017: //                             if(p->prev_set_dtr) {
                   13018: //                                     EscapeCommFunction(hComm, SETDTR);
                   13019: //                             } else {
                   13020: //                                     EscapeCommFunction(hComm, CLRDTR);
                   13021: //                             }
                   13022: //                     }
1.1.1.25  root     13023:                        
                   13024:                        // get comm pins
                   13025:                        DWORD dwModemStat = 0;
                   13026:                        
                   13027:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   13028:                                EnterCriticalSection(&q->csModemStat);
                   13029:                                if(dwModemStat & MS_RLSD_ON) {
                   13030:                                        p->modem_stat |= 0x80;
                   13031:                                } else {
                   13032:                                        p->modem_stat &= ~0x80;
                   13033:                                }
                   13034:                                if(dwModemStat & MS_RING_ON) {
                   13035:                                        p->modem_stat |= 0x40;
                   13036:                                } else {
                   13037:                                        p->modem_stat &= ~0x40;
                   13038:                                }
1.1.1.26  root     13039: //                             if(dwModemStat & MS_DSR_ON) {
                   13040: //                                     p->modem_stat |= 0x20;
                   13041: //                             } else {
                   13042: //                                     p->modem_stat &= ~0x20;
                   13043: //                             }
                   13044: //                             if(dwModemStat & MS_CTS_ON) {
                   13045: //                                     p->modem_stat |= 0x10;
                   13046: //                             } else {
                   13047: //                                     p->modem_stat &= ~0x10;
                   13048: //                             }
1.1.1.25  root     13049:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   13050:                                        p->modem_stat |= 0x08;
                   13051:                                }
                   13052:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   13053:                                        p->modem_stat |= 0x04;
                   13054:                                }
1.1.1.26  root     13055: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   13056: //                                     p->modem_stat |= 0x02;
                   13057: //                             }
                   13058: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   13059: //                                     p->modem_stat |= 0x01;
                   13060: //                             }
1.1.1.25  root     13061:                                LeaveCriticalSection(&q->csModemStat);
                   13062:                        }
                   13063:                        
                   13064:                        // send data
                   13065:                        DWORD dwSend = 0;
                   13066:                        
                   13067:                        EnterCriticalSection(&q->csSendData);
                   13068:                        while(!p->send_buffer->empty()) {
                   13069:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   13070:                        }
                   13071:                        LeaveCriticalSection(&q->csSendData);
                   13072:                        
                   13073:                        if(dwSend != 0) {
                   13074:                                DWORD dwWritten = 0;
                   13075:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   13076:                        }
                   13077:                        
                   13078:                        // get line status and recv data
                   13079:                        DWORD dwLineStat = 0;
                   13080:                        COMSTAT comStat;
                   13081:                        
                   13082:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   13083:                                EnterCriticalSection(&q->csLineStat);
                   13084:                                if(dwLineStat & CE_BREAK) {
                   13085:                                        p->line_stat_err |= 0x10;
                   13086:                                }
                   13087:                                if(dwLineStat & CE_FRAME) {
                   13088:                                        p->line_stat_err |= 0x08;
                   13089:                                }
                   13090:                                if(dwLineStat & CE_RXPARITY) {
                   13091:                                        p->line_stat_err |= 0x04;
                   13092:                                }
                   13093:                                if(dwLineStat & CE_OVERRUN) {
                   13094:                                        p->line_stat_err |= 0x02;
                   13095:                                }
                   13096:                                LeaveCriticalSection(&q->csLineStat);
                   13097:                                
                   13098:                                if(comStat.cbInQue != 0) {
                   13099:                                        EnterCriticalSection(&q->csRecvData);
                   13100:                                        DWORD dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   13101:                                        LeaveCriticalSection(&q->csRecvData);
                   13102:                                        
                   13103:                                        if(dwRecv != 0) {
                   13104:                                                DWORD dwRead = 0;
                   13105:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   13106:                                                        EnterCriticalSection(&q->csRecvData);
                   13107:                                                        for(int i = 0; i < dwRead; i++) {
                   13108:                                                                p->recv_buffer->write(bytBuffer[i]);
                   13109:                                                        }
                   13110:                                                        LeaveCriticalSection(&q->csRecvData);
                   13111:                                                }
                   13112:                                        }
                   13113:                                }
                   13114:                        }
                   13115:                        Sleep(10);
                   13116:                }
                   13117:                CloseHandle(hComm);
                   13118:        }
                   13119:        return 0;
                   13120: }
                   13121: 
1.1.1.8   root     13122: // cmos
                   13123: 
                   13124: void cmos_init()
                   13125: {
                   13126:        memset(cmos, 0, sizeof(cmos));
                   13127:        cmos_addr = 0;
1.1       root     13128:        
1.1.1.8   root     13129:        // from DOSBox
                   13130:        cmos_write(0x0a, 0x26);
                   13131:        cmos_write(0x0b, 0x02);
                   13132:        cmos_write(0x0d, 0x80);
1.1       root     13133: }
                   13134: 
1.1.1.8   root     13135: void cmos_write(int addr, UINT8 val)
1.1       root     13136: {
1.1.1.8   root     13137:        cmos[addr & 0x7f] = val;
                   13138: }
                   13139: 
                   13140: #define CMOS_GET_TIME() { \
                   13141:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   13142:        if(prev_sec != cur_sec) { \
                   13143:                GetLocalTime(&time); \
                   13144:                prev_sec = cur_sec; \
                   13145:        } \
1.1       root     13146: }
1.1.1.8   root     13147: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     13148: 
1.1.1.8   root     13149: UINT8 cmos_read(int addr)
1.1       root     13150: {
1.1.1.8   root     13151:        static SYSTEMTIME time;
                   13152:        static UINT32 prev_sec = 0;
1.1       root     13153:        
1.1.1.8   root     13154:        switch(addr & 0x7f) {
                   13155:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   13156:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   13157:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   13158:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   13159:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   13160:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   13161:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   13162: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   13163:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   13164:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   13165:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   13166:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   13167:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   13168:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   13169:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   13170:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     13171:        }
1.1.1.8   root     13172:        return(cmos[addr & 0x7f]);
1.1       root     13173: }
                   13174: 
1.1.1.7   root     13175: // kbd (a20)
                   13176: 
                   13177: void kbd_init()
                   13178: {
1.1.1.8   root     13179:        kbd_data = kbd_command = 0;
1.1.1.7   root     13180:        kbd_status = 0x18;
                   13181: }
                   13182: 
                   13183: UINT8 kbd_read_data()
                   13184: {
1.1.1.8   root     13185:        kbd_status &= ~1;
1.1.1.7   root     13186:        return(kbd_data);
                   13187: }
                   13188: 
                   13189: void kbd_write_data(UINT8 val)
                   13190: {
                   13191:        switch(kbd_command) {
                   13192:        case 0xd1:
                   13193:                i386_set_a20_line((val >> 1) & 1);
                   13194:                break;
                   13195:        }
                   13196:        kbd_command = 0;
1.1.1.8   root     13197:        kbd_status &= ~8;
1.1.1.7   root     13198: }
                   13199: 
                   13200: UINT8 kbd_read_status()
                   13201: {
                   13202:        return(kbd_status);
                   13203: }
                   13204: 
                   13205: void kbd_write_command(UINT8 val)
                   13206: {
                   13207:        switch(val) {
                   13208:        case 0xd0:
                   13209:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     13210:                kbd_status |= 1;
1.1.1.7   root     13211:                break;
                   13212:        case 0xdd:
                   13213:                i386_set_a20_line(0);
                   13214:                break;
                   13215:        case 0xdf:
                   13216:                i386_set_a20_line(1);
                   13217:                break;
1.1.1.26  root     13218:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   13219:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     13220:                if(!(val & 1)) {
1.1.1.8   root     13221:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     13222:                                // reset pic
                   13223:                                pic_init();
                   13224:                                pic[0].irr = pic[1].irr = 0x00;
                   13225:                                pic[0].imr = pic[1].imr = 0xff;
                   13226:                        }
                   13227:                        CPU_RESET_CALL(CPU_MODEL);
                   13228:                        i386_jmp_far(0x40, 0x67);
                   13229:                }
                   13230:                i386_set_a20_line((val >> 1) & 1);
                   13231:                break;
                   13232:        }
                   13233:        kbd_command = val;
1.1.1.8   root     13234:        kbd_status |= 8;
1.1.1.7   root     13235: }
                   13236: 
1.1.1.9   root     13237: // vga
                   13238: 
                   13239: UINT8 vga_read_status()
                   13240: {
                   13241:        // 60hz
                   13242:        static const int period[3] = {16, 17, 17};
                   13243:        static int index = 0;
                   13244:        UINT32 time = timeGetTime() % period[index];
                   13245:        
                   13246:        index = (index + 1) % 3;
1.1.1.14  root     13247:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     13248: }
                   13249: 
1.1       root     13250: // i/o bus
                   13251: 
1.1.1.25  root     13252: #ifdef ENABLE_DEBUG_IOPORT
                   13253: UINT8 read_io_byte_debug(offs_t addr);
                   13254: 
                   13255: UINT8 read_io_byte(offs_t addr)
                   13256: {
                   13257:        UINT8 val = read_io_byte_debug(addr);
                   13258:        if(fdebug != NULL) {
                   13259:                fprintf(fdebug, "inb %04X, %02X\n", addr, val);
                   13260:        }
                   13261:        return(val);
                   13262: }
                   13263: 
                   13264: UINT8 read_io_byte_debug(offs_t addr)
                   13265: #else
1.1       root     13266: UINT8 read_io_byte(offs_t addr)
1.1.1.25  root     13267: #endif
1.1       root     13268: {
                   13269:        switch(addr) {
1.1.1.25  root     13270:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   13271:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   13272:                return(dma_read(0, addr));
                   13273:        case 0x20: case 0x21:
1.1       root     13274:                return(pic_read(0, addr));
1.1.1.25  root     13275:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     13276:                return(pit_read(addr & 0x03));
1.1.1.7   root     13277:        case 0x60:
                   13278:                return(kbd_read_data());
1.1.1.9   root     13279:        case 0x61:
                   13280:                return(system_port);
1.1.1.7   root     13281:        case 0x64:
                   13282:                return(kbd_read_status());
1.1       root     13283:        case 0x71:
1.1.1.8   root     13284:                return(cmos_read(cmos_addr));
1.1.1.25  root     13285:        case 0x81:
                   13286:                return(dma_page_read(0, 2));
                   13287:        case 0x82:
                   13288:                return(dma_page_read(0, 3));
                   13289:        case 0x83:
                   13290:                return(dma_page_read(0, 1));
                   13291:        case 0x87:
                   13292:                return(dma_page_read(0, 0));
                   13293:        case 0x89:
                   13294:                return(dma_page_read(1, 2));
                   13295:        case 0x8a:
                   13296:                return(dma_page_read(1, 3));
                   13297:        case 0x8b:
                   13298:                return(dma_page_read(1, 1));
                   13299:        case 0x8f:
                   13300:                return(dma_page_read(1, 0));
1.1       root     13301:        case 0x92:
1.1.1.3   root     13302:                return((m_a20_mask >> 19) & 2);
1.1.1.25  root     13303:        case 0xa0: case 0xa1:
1.1       root     13304:                return(pic_read(1, addr));
1.1.1.25  root     13305:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   13306:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     13307:                return(dma_read(1, (addr - 0xc0) >> 1));
                   13308: //     case 0x278: case 0x279: case 0x27a:
                   13309: //             return(pio_read(1, addr));
1.1.1.25  root     13310:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   13311:                return(sio_read(1, addr));
                   13312:        case 0x378: case 0x379: case 0x37a:
                   13313:                return(pio_read(0, addr));
                   13314:        case 0x3ba: case 0x3da:
1.1.1.9   root     13315:                return(vga_read_status());
1.1.1.25  root     13316:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   13317:                return(sio_read(0, addr));
1.1       root     13318:        default:
                   13319: //             error("inb %4x\n", addr);
                   13320:                break;
                   13321:        }
                   13322:        return(0xff);
                   13323: }
                   13324: 
                   13325: UINT16 read_io_word(offs_t addr)
                   13326: {
                   13327:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   13328: }
                   13329: 
                   13330: UINT32 read_io_dword(offs_t addr)
                   13331: {
                   13332:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   13333: }
                   13334: 
                   13335: void write_io_byte(offs_t addr, UINT8 val)
                   13336: {
1.1.1.25  root     13337: #ifdef ENABLE_DEBUG_IOPORT
                   13338:        if(fdebug != NULL) {
                   13339:                fprintf(fdebug, "outb %04X, %02X\n", addr, val);
                   13340:        }
                   13341: #endif
1.1       root     13342:        switch(addr) {
1.1.1.25  root     13343:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   13344:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   13345:                dma_write(0, addr, val);
                   13346:                break;
                   13347:        case 0x20: case 0x21:
1.1       root     13348:                pic_write(0, addr, val);
                   13349:                break;
1.1.1.25  root     13350:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     13351:                pit_write(addr & 0x03, val);
                   13352:                break;
1.1.1.7   root     13353:        case 0x60:
                   13354:                kbd_write_data(val);
                   13355:                break;
1.1.1.9   root     13356:        case 0x61:
                   13357:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   13358:                        // beep on
                   13359: //                     MessageBeep(-1);
                   13360:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   13361:                        // beep off
                   13362:                }
                   13363:                system_port = val;
                   13364:                break;
1.1       root     13365:        case 0x64:
1.1.1.7   root     13366:                kbd_write_command(val);
1.1       root     13367:                break;
                   13368:        case 0x70:
                   13369:                cmos_addr = val;
                   13370:                break;
                   13371:        case 0x71:
1.1.1.8   root     13372:                cmos_write(cmos_addr, val);
1.1       root     13373:                break;
1.1.1.25  root     13374:        case 0x81:
                   13375:                dma_page_write(0, 2, val);
                   13376:        case 0x82:
                   13377:                dma_page_write(0, 3, val);
                   13378:        case 0x83:
                   13379:                dma_page_write(0, 1, val);
                   13380:        case 0x87:
                   13381:                dma_page_write(0, 0, val);
                   13382:        case 0x89:
                   13383:                dma_page_write(1, 2, val);
                   13384:        case 0x8a:
                   13385:                dma_page_write(1, 3, val);
                   13386:        case 0x8b:
                   13387:                dma_page_write(1, 1, val);
                   13388:        case 0x8f:
                   13389:                dma_page_write(1, 0, val);
1.1       root     13390:        case 0x92:
1.1.1.7   root     13391:                i386_set_a20_line((val >> 1) & 1);
1.1       root     13392:                break;
1.1.1.25  root     13393:        case 0xa0: case 0xa1:
1.1       root     13394:                pic_write(1, addr, val);
                   13395:                break;
1.1.1.25  root     13396:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   13397:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     13398:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     13399:                break;
1.1.1.26  root     13400: //     case 0x278: case 0x279: case 0x27a:
                   13401: //             pio_write(1, addr, val);
                   13402: //             break;
1.1.1.25  root     13403:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   13404:                sio_write(1, addr, val);
                   13405:                break;
                   13406:        case 0x378: case 0x379: case 0x37a:
                   13407:                pio_write(0, addr, val);
                   13408:                break;
                   13409:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   13410:                sio_write(0, addr, val);
                   13411:                break;
1.1       root     13412:        default:
                   13413: //             error("outb %4x,%2x\n", addr, val);
                   13414:                break;
                   13415:        }
                   13416: }
                   13417: 
                   13418: void write_io_word(offs_t addr, UINT16 val)
                   13419: {
                   13420:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   13421:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   13422: }
                   13423: 
                   13424: void write_io_dword(offs_t addr, UINT32 val)
                   13425: {
                   13426:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   13427:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   13428:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   13429:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   13430: }

unix.superglobalmegacorp.com

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