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

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: 
                     10: #define fatalerror(...) { \
                     11:        fprintf(stderr, __VA_ARGS__); \
                     12:        exit(1); \
                     13: }
                     14: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       15: #define nolog(...)
                     16: 
                     17: //#define ENABLE_DEBUG
                     18: #ifdef ENABLE_DEBUG
                     19:        #define EXPORT_DEBUG_TO_FILE
                     20:        #define ENABLE_DEBUG_DASM
                     21:        #define ENABLE_DEBUG_SYSCALL
                     22:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25! root       23:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       24:        
                     25:        #ifdef EXPORT_DEBUG_TO_FILE
                     26:                FILE* fdebug = NULL;
                     27:        #else
                     28:                #define fdebug stderr
                     29:        #endif
                     30:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     31:                #define unimplemented_10h fatalerror
1.1.1.25! root       32:                #define unimplemented_14h fatalerror
1.1.1.22  root       33:                #define unimplemented_15h fatalerror
                     34:                #define unimplemented_16h fatalerror
                     35:                #define unimplemented_1ah fatalerror
                     36:                #define unimplemented_21h fatalerror
                     37:                #define unimplemented_2fh fatalerror
1.1.1.24  root       38:                #define unimplemented_33h fatalerror
1.1.1.22  root       39:                #define unimplemented_67h fatalerror
                     40:                #define unimplemented_xms fatalerror
                     41:        #endif
                     42: #endif
                     43: #ifndef unimplemented_10h
                     44:        #define unimplemented_10h nolog
                     45: #endif
1.1.1.25! root       46: #ifndef unimplemented_14h
        !            47:        #define unimplemented_14h nolog
        !            48: #endif
1.1.1.22  root       49: #ifndef unimplemented_15h
                     50:        #define unimplemented_15h nolog
                     51: #endif
                     52: #ifndef unimplemented_16h
                     53:        #define unimplemented_16h nolog
                     54: #endif
                     55: #ifndef unimplemented_1ah
                     56:        #define unimplemented_1ah nolog
                     57: #endif
                     58: #ifndef unimplemented_21h
                     59:        #define unimplemented_21h nolog
                     60: #endif
                     61: #ifndef unimplemented_2fh
                     62:        #define unimplemented_2fh nolog
                     63: #endif
1.1.1.24  root       64: #ifndef unimplemented_33h
                     65:        #define unimplemented_33h nolog
                     66: #endif
1.1.1.22  root       67: #ifndef unimplemented_67h
                     68:        #define unimplemented_67h nolog
                     69: #endif
                     70: #ifndef unimplemented_xms
                     71:        #define unimplemented_xms nolog
                     72: #endif
                     73: 
                     74: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     75: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     76: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       77: 
1.1.1.12  root       78: #if defined(__MINGW32__)
                     79: extern "C" int _CRT_glob = 0;
                     80: #endif
                     81: 
                     82: /*
                     83:        kludge for "more-standardized" C++
                     84: */
                     85: #if !defined(_MSC_VER)
                     86: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     87: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     88: #define min(a,b) kludge_min(a,b)
                     89: #define max(a,b) kludge_max(a,b)
1.1.1.14  root       90: #elif _MSC_VER >= 1400
                     91: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                     92: {
                     93: }
                     94: #endif
                     95: 
                     96: #define USE_THREAD
                     97: 
                     98: #ifdef USE_THREAD
                     99: static CRITICAL_SECTION vram_crit_sect;
                    100: #else
                    101: #define EnterCriticalSection(x)
                    102: #define LeaveCriticalSection(x)
                    103: #define vram_flush()
1.1.1.12  root      104: #endif
                    105: 
1.1.1.14  root      106: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    107: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    108: 
                    109: void change_console_size(int width, int height);
                    110: void clear_scr_buffer(WORD attr);
                    111: 
                    112: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    113: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    114: static COORD vram_coord_char, vram_coord_attr;
                    115: 
                    116: bool ignore_illegal_insn = false;
                    117: bool limit_max_memory = false;
                    118: bool no_windows = false;
                    119: //bool ctrl_break = false;
                    120: bool stay_busy = false;
                    121: UINT32 iops = 0;
1.1.1.19  root      122: bool support_ems = false;
                    123: #ifdef SUPPORT_XMS
                    124: bool support_xms = false;
                    125: #endif
1.1.1.14  root      126: 
                    127: BOOL is_vista_or_later;
                    128: 
                    129: inline void maybe_idle()
                    130: {
                    131:        // if it appears to be in a tight loop, assume waiting for input
                    132:        // allow for one updated video character, for a spinning cursor
                    133:        if(!stay_busy && iops < 1000 && vram_length_char <= 1 && vram_length_attr <= 1) {
                    134:                Sleep(10);
                    135:        }
                    136:        iops = 0;
                    137: }
1.1.1.12  root      138: 
1.1       root      139: /* ----------------------------------------------------------------------------
1.1.1.3   root      140:        MAME i86/i386
1.1       root      141: ---------------------------------------------------------------------------- */
                    142: 
1.1.1.10  root      143: #ifndef __BIG_ENDIAN__
1.1       root      144: #define LSB_FIRST
1.1.1.10  root      145: #endif
1.1       root      146: 
                    147: #ifndef INLINE
                    148: #define INLINE inline
                    149: #endif
                    150: #define U64(v) UINT64(v)
                    151: 
                    152: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    153: #define logerror(...)
                    154: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    155: #define popmessage(...)
                    156: 
                    157: /*****************************************************************************/
1.1.1.10  root      158: /* src/emu/devcpu.h */
                    159: 
                    160: // CPU interface functions
                    161: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    162: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    163: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    164: 
                    165: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    166: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    167: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    168: 
                    169: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    170: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    171: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    172: 
                    173: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    174: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    175: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    176: 
                    177: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    178: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    179: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    180: 
1.1.1.14  root      181: #define CPU_MODEL_STR(name)                    #name
                    182: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    183: 
1.1.1.10  root      184: /*****************************************************************************/
                    185: /* src/emu/didisasm.h */
                    186: 
                    187: // Disassembler constants
                    188: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    189: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    190: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    191: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    192: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    193: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    194: 
                    195: /*****************************************************************************/
1.1       root      196: /* src/emu/diexec.h */
                    197: 
                    198: // I/O line states
                    199: enum line_state
                    200: {
                    201:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    202:        ASSERT_LINE,                            // assert an interrupt immediately
                    203:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    204:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    205: };
                    206: 
                    207: // I/O line definitions
                    208: enum
                    209: {
                    210:        INPUT_LINE_IRQ = 0,
                    211:        INPUT_LINE_NMI
                    212: };
                    213: 
                    214: /*****************************************************************************/
1.1.1.10  root      215: /* src/emu/dimemory.h */
1.1       root      216: 
1.1.1.10  root      217: // Translation intentions
                    218: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    219: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    220: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    221: 
                    222: const int TRANSLATE_READ            = 0;        // translate for read
                    223: const int TRANSLATE_WRITE           = 1;        // translate for write
                    224: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    225: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    226: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    227: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    228: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    229: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    230: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      231: 
1.1.1.10  root      232: /*****************************************************************************/
                    233: /* src/emu/emucore.h */
1.1       root      234: 
1.1.1.10  root      235: // constants for expression endianness
                    236: enum endianness_t
                    237: {
                    238:        ENDIANNESS_LITTLE,
                    239:        ENDIANNESS_BIG
                    240: };
1.1       root      241: 
1.1.1.10  root      242: // declare native endianness to be one or the other
                    243: #ifdef LSB_FIRST
                    244: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    245: #else
                    246: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    247: #endif
                    248: 
                    249: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    250: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    251: 
                    252: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    253: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    254: 
                    255: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    256: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      257: 
                    258: /*****************************************************************************/
                    259: /* src/emu/memory.h */
                    260: 
1.1.1.10  root      261: // address spaces
                    262: enum address_spacenum
                    263: {
                    264:        AS_0,                           // first address space
                    265:        AS_1,                           // second address space
                    266:        AS_2,                           // third address space
                    267:        AS_3,                           // fourth address space
                    268:        ADDRESS_SPACES,                 // maximum number of address spaces
                    269: 
                    270:        // alternate address space names for common use
                    271:        AS_PROGRAM = AS_0,              // program address space
                    272:        AS_DATA = AS_1,                 // data address space
                    273:        AS_IO = AS_2                    // I/O address space
                    274: };
                    275: 
1.1       root      276: // offsets and addresses are 32-bit (for now...)
                    277: typedef UINT32 offs_t;
                    278: 
                    279: // read accessors
                    280: UINT8 read_byte(offs_t byteaddress)
                    281: {
1.1.1.4   root      282: #if defined(HAS_I386)
1.1       root      283:        if(byteaddress < MAX_MEM) {
                    284:                return mem[byteaddress];
1.1.1.3   root      285: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    286: //             return read_byte(byteaddress & 0xfffff);
1.1       root      287:        }
                    288:        return 0;
1.1.1.4   root      289: #else
                    290:        return mem[byteaddress];
                    291: #endif
1.1       root      292: }
                    293: 
                    294: UINT16 read_word(offs_t byteaddress)
                    295: {
1.1.1.14  root      296:        if(byteaddress == 0x41c) {
                    297:                // pointer to first free slot in keyboard buffer
                    298:                // XXX: the buffer itself doesn't actually exist in DOS memory
                    299:                if(key_buf_char->count() == 0) {
                    300:                        maybe_idle();
                    301:                }
                    302:                return (UINT16)key_buf_char->count();
                    303:        }
1.1.1.4   root      304: #if defined(HAS_I386)
1.1       root      305:        if(byteaddress < MAX_MEM - 1) {
                    306:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      307: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    308: //             return read_word(byteaddress & 0xfffff);
1.1       root      309:        }
                    310:        return 0;
1.1.1.4   root      311: #else
                    312:        return *(UINT16 *)(mem + byteaddress);
                    313: #endif
1.1       root      314: }
                    315: 
                    316: UINT32 read_dword(offs_t byteaddress)
                    317: {
1.1.1.4   root      318: #if defined(HAS_I386)
1.1       root      319:        if(byteaddress < MAX_MEM - 3) {
                    320:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      321: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    322: //             return read_dword(byteaddress & 0xfffff);
1.1       root      323:        }
                    324:        return 0;
1.1.1.4   root      325: #else
                    326:        return *(UINT32 *)(mem + byteaddress);
                    327: #endif
1.1       root      328: }
                    329: 
                    330: // write accessors
1.1.1.14  root      331: #ifdef USE_THREAD
                    332: void vram_flush_char()
                    333: {
                    334:        if(vram_length_char != 0) {
                    335:                DWORD num;
1.1.1.23  root      336:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      337:                vram_length_char = vram_last_length_char = 0;
                    338:        }
                    339: }
                    340: 
                    341: void vram_flush_attr()
                    342: {
                    343:        if(vram_length_attr != 0) {
                    344:                DWORD num;
1.1.1.23  root      345:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      346:                vram_length_attr = vram_last_length_attr = 0;
                    347:        }
                    348: }
                    349: 
                    350: void vram_flush()
                    351: {
                    352:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    353:                EnterCriticalSection(&vram_crit_sect);
                    354:                vram_flush_char();
                    355:                vram_flush_attr();
                    356:                LeaveCriticalSection(&vram_crit_sect);
                    357:        }
                    358: }
                    359: #endif
                    360: 
                    361: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      362: {
1.1.1.14  root      363: #ifdef USE_THREAD
                    364:        static offs_t first_offset_char, last_offset_char;
                    365:        
                    366:        if(vram_length_char != 0) {
                    367:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    368:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    369:                        return;
                    370:                }
                    371:                if(offset != last_offset_char + 2) {
                    372:                        vram_flush_char();
                    373:                }
                    374:        }
                    375:        if(vram_length_char == 0) {
                    376:                first_offset_char = offset;
                    377:                vram_coord_char.X = (offset >> 1) % scr_width;
                    378:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    379:        }
                    380:        scr_char[vram_length_char++] = data;
                    381:        last_offset_char = offset;
                    382: #else
1.1.1.8   root      383:        COORD co;
                    384:        DWORD num;
                    385:        
1.1.1.14  root      386:        co.X = (offset >> 1) % scr_width;
                    387:        co.Y = (offset >> 1) / scr_width;
                    388:        scr_char[0] = data;
1.1.1.23  root      389:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      390: #endif
                    391: }
                    392: 
                    393: void write_text_vram_attr(offs_t offset, UINT8 data)
                    394: {
                    395: #ifdef USE_THREAD
                    396:        static offs_t first_offset_attr, last_offset_attr;
                    397:        
                    398:        if(vram_length_attr != 0) {
                    399:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    400:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    401:                        return;
                    402:                }
                    403:                if(offset != last_offset_attr + 2) {
                    404:                        vram_flush_attr();
                    405:                }
                    406:        }
                    407:        if(vram_length_attr == 0) {
                    408:                first_offset_attr = offset;
                    409:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    410:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    411:        }
                    412:        scr_attr[vram_length_attr++] = data;
                    413:        last_offset_attr = offset;
                    414: #else
                    415:        COORD co;
                    416:        DWORD num;
1.1.1.8   root      417:        
1.1.1.14  root      418:        co.X = (offset >> 1) % scr_width;
                    419:        co.Y = (offset >> 1) / scr_width;
                    420:        scr_attr[0] = data;
1.1.1.23  root      421:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      422: #endif
                    423: }
                    424: 
                    425: void write_text_vram_byte(offs_t offset, UINT8 data)
                    426: {
                    427:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      428:        if(offset & 1) {
1.1.1.14  root      429:                write_text_vram_attr(offset, data);
1.1.1.8   root      430:        } else {
1.1.1.14  root      431:                write_text_vram_char(offset, data);
1.1.1.8   root      432:        }
1.1.1.14  root      433:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      434: }
                    435: 
                    436: void write_text_vram_word(offs_t offset, UINT16 data)
                    437: {
1.1.1.14  root      438:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      439:        if(offset & 1) {
1.1.1.14  root      440:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    441:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      442:        } else {
1.1.1.14  root      443:                write_text_vram_char(offset    , (data     ) & 0xff);
                    444:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      445:        }
1.1.1.14  root      446:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      447: }
                    448: 
                    449: void write_text_vram_dword(offs_t offset, UINT32 data)
                    450: {
1.1.1.14  root      451:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      452:        if(offset & 1) {
1.1.1.14  root      453:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    454:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    455:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    456:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    457:        } else {
                    458:                write_text_vram_char(offset    , (data      ) & 0xff);
                    459:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    460:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    461:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      462:        }
1.1.1.14  root      463:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      464: }
                    465: 
1.1       root      466: void write_byte(offs_t byteaddress, UINT8 data)
                    467: {
1.1.1.8   root      468:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      469:                mem[byteaddress] = data;
1.1.1.8   root      470:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      471:                if(!restore_console_on_exit) {
                    472:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      473:                }
1.1.1.8   root      474:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    475:                mem[byteaddress] = data;
                    476:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    477:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    478:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      479:                }
                    480:                mem[byteaddress] = data;
1.1.1.4   root      481: #if defined(HAS_I386)
1.1.1.3   root      482:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      483: #else
                    484:        } else {
                    485: #endif
1.1.1.3   root      486:                mem[byteaddress] = data;
1.1       root      487:        }
                    488: }
                    489: 
                    490: void write_word(offs_t byteaddress, UINT16 data)
                    491: {
1.1.1.8   root      492:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      493:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    494:                        COORD co;
                    495:                        co.X = data & 0xff;
                    496:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      497:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      498:                }
1.1.1.3   root      499:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      500:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      501:                if(!restore_console_on_exit) {
                    502:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      503:                }
1.1.1.8   root      504:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    505:                *(UINT16 *)(mem + byteaddress) = data;
                    506:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    507:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    508:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      509:                }
                    510:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      511: #if defined(HAS_I386)
1.1.1.3   root      512:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      513: #else
                    514:        } else {
                    515: #endif
1.1.1.3   root      516:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      517:        }
                    518: }
                    519: 
                    520: void write_dword(offs_t byteaddress, UINT32 data)
                    521: {
1.1.1.8   root      522:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      523:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      524:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      525:                if(!restore_console_on_exit) {
                    526:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      527:                }
1.1.1.8   root      528:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    529:                *(UINT32 *)(mem + byteaddress) = data;
                    530:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    531:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    532:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      533:                }
                    534:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      535: #if defined(HAS_I386)
1.1.1.3   root      536:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      537: #else
                    538:        } else {
                    539: #endif
1.1.1.3   root      540:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      541:        }
                    542: }
                    543: 
                    544: #define read_decrypted_byte read_byte
                    545: #define read_decrypted_word read_word
                    546: #define read_decrypted_dword read_dword
                    547: 
1.1.1.3   root      548: #define read_raw_byte read_byte
                    549: #define write_raw_byte write_byte
                    550: 
                    551: #define read_word_unaligned read_word
                    552: #define write_word_unaligned write_word
                    553: 
                    554: #define read_io_word_unaligned read_io_word
                    555: #define write_io_word_unaligned write_io_word
                    556: 
1.1       root      557: UINT8 read_io_byte(offs_t byteaddress);
                    558: UINT16 read_io_word(offs_t byteaddress);
                    559: UINT32 read_io_dword(offs_t byteaddress);
                    560: 
                    561: void write_io_byte(offs_t byteaddress, UINT8 data);
                    562: void write_io_word(offs_t byteaddress, UINT16 data);
                    563: void write_io_dword(offs_t byteaddress, UINT32 data);
                    564: 
                    565: /*****************************************************************************/
                    566: /* src/osd/osdcomm.h */
                    567: 
                    568: /* Highly useful macro for compile-time knowledge of an array size */
                    569: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    570: 
1.1.1.3   root      571: #if defined(HAS_I386)
1.1.1.10  root      572:        static CPU_TRANSLATE(i386);
                    573:        #include "mame/lib/softfloat/softfloat.c"
                    574:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      575:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      576: #elif defined(HAS_I286)
1.1.1.10  root      577:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      578: #else
1.1.1.10  root      579:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      580: #endif
1.1.1.22  root      581: #ifdef ENABLE_DEBUG_DASM
1.1.1.10  root      582:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1.1.22  root      583:        int dasm = 0;
1.1       root      584: #endif
                    585: 
1.1.1.3   root      586: #if defined(HAS_I386)
                    587:        #define SREG(x)                         m_sreg[x].selector
                    588:        #define SREG_BASE(x)                    m_sreg[x].base
                    589: 
                    590:        int cpu_type, cpu_step;
                    591: #else
                    592:        #define REG8(x)                         m_regs.b[x]
                    593:        #define REG16(x)                        m_regs.w[x]
                    594:        #define SREG(x)                         m_sregs[x]
                    595:        #define SREG_BASE(x)                    m_base[x]
                    596:        #define m_CF                            m_CarryVal
                    597:        #define m_a20_mask                      AMASK
                    598:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    599:        #if defined(HAS_I286)
                    600:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    601:        #else
                    602:                #define i386_set_a20_line(x)
                    603:        #endif
                    604:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    605: #endif
1.1       root      606: 
                    607: void i386_jmp_far(UINT16 selector, UINT32 address)
                    608: {
1.1.1.3   root      609: #if defined(HAS_I386)
1.1       root      610:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      611:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      612:        } else {
1.1.1.3   root      613:                SREG(CS) = selector;
                    614:                m_performed_intersegment_jump = 1;
                    615:                i386_load_segment_descriptor(CS);
                    616:                m_eip = address;
                    617:                CHANGE_PC(m_eip);
1.1       root      618:        }
1.1.1.3   root      619: #elif defined(HAS_I286)
                    620:        i80286_code_descriptor(selector, address, 1);
                    621: #else
                    622:        SREG(CS) = selector;
                    623:        i386_load_segment_descriptor(CS);
                    624:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    625: #endif
1.1       root      626: }
                    627: 
1.1.1.24  root      628: /*
                    629: void i386_call_far(UINT16 selector, UINT32 address)
                    630: {
                    631: #if defined(HAS_I386)
                    632:        if(PROTECTED_MODE && !V8086_MODE) {
                    633:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    634:        } else {
                    635:                PUSH16(SREG(CS));
                    636:                PUSH16(m_eip);
                    637:                SREG(CS) = selector;
                    638:                m_performed_intersegment_jump = 1;
                    639:                i386_load_segment_descriptor(CS);
                    640:                m_eip = address;
                    641:                CHANGE_PC(m_eip);
                    642:        }
                    643: #else
                    644:        UINT16 ip = m_pc - SREG_BASE(CS);
                    645:        UINT16 cs = SREG(CS);
                    646: #if defined(HAS_I286)
                    647:        i80286_code_descriptor(selector, address, 2);
                    648: #else
                    649:        SREG(CS) = selector;
                    650:        i386_load_segment_descriptor(CS);
                    651:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    652: #endif
                    653:        PUSH(cs);
                    654:        PUSH(ip);
                    655:        CHANGE_PC(m_pc);
                    656: #endif
                    657: }
                    658: */
                    659: 
1.1       root      660: /* ----------------------------------------------------------------------------
                    661:        main
                    662: ---------------------------------------------------------------------------- */
                    663: 
1.1.1.10  root      664: bool is_started_from_command_prompt()
                    665: {
1.1.1.18  root      666:        bool ret = false;
                    667:        
                    668:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                    669:        if(hLibrary) {
                    670:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                    671:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                    672:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                    673:                if(lpfnGetConsoleProcessList) {
                    674:                        DWORD pl;
                    675:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                    676:                        FreeLibrary(hLibrary);
                    677:                        return(ret);
                    678:                }
                    679:                FreeLibrary(hLibrary);
                    680:        }
                    681:        
                    682:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                    683:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                    684:                DWORD dwParentProcessID = 0;
                    685:                PROCESSENTRY32 pe32;
                    686:                pe32.dwSize = sizeof(PROCESSENTRY32);
                    687:                if(Process32First(hSnapshot, &pe32)) {
                    688:                        do {
                    689:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                    690:                                        dwParentProcessID = pe32.th32ParentProcessID;
                    691:                                        break;
                    692:                                }
                    693:                        } while(Process32Next(hSnapshot, &pe32));
                    694:                }
                    695:                CloseHandle(hSnapshot);
                    696:                if(dwParentProcessID != 0) {
                    697:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                    698:                        if(hProcess != NULL) {
                    699:                                HMODULE hMod;
                    700:                                DWORD cbNeeded;
                    701:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                    702:                                        char module_name[MAX_PATH];
                    703:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                    704:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                    705:                                        }
                    706:                                }
                    707:                                CloseHandle(hProcess);
                    708:                        }
                    709:                }
                    710:        }
                    711:        return(ret);
1.1.1.14  root      712: }
                    713: 
                    714: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                    715: {
1.1.1.24  root      716:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root      717:        OSVERSIONINFOEX osvi;
                    718:        DWORDLONG dwlConditionMask = 0;
                    719:        int op = VER_GREATER_EQUAL;
                    720:        
                    721:        // Initialize the OSVERSIONINFOEX structure.
                    722:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                    723:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                    724:        osvi.dwMajorVersion = dwMajorVersion;
                    725:        osvi.dwMinorVersion = dwMinorVersion;
                    726:        osvi.wServicePackMajor = wServicePackMajor;
                    727:        osvi.wServicePackMinor = wServicePackMinor;
                    728:        
                    729:         // Initialize the condition mask.
                    730:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                    731:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                    732:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                    733:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                    734:        
                    735:        // Perform the test.
                    736:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                    737: }
                    738: 
                    739: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                    740: {
                    741:        if(dwCtrlType == CTRL_BREAK_EVENT || dwCtrlType == CTRL_C_EVENT) {
                    742:                m_halted = 1;
                    743:                return TRUE;
                    744:        }
                    745:        return FALSE;
                    746: }
                    747: 
                    748: #ifdef USE_THREAD
                    749: DWORD WINAPI vram_thread(LPVOID)
                    750: {
                    751:        while(!m_halted) {
                    752:                EnterCriticalSection(&vram_crit_sect);
                    753:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                    754:                        vram_flush_char();
                    755:                }
                    756:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                    757:                        vram_flush_attr();
                    758:                }
                    759:                vram_last_length_char = vram_length_char;
                    760:                vram_last_length_attr = vram_length_attr;
                    761:                LeaveCriticalSection(&vram_crit_sect);
                    762:                // this is about half the maximum keyboard repeat rate - any
                    763:                // lower tends to be jerky, any higher misses updates
                    764:                Sleep(15);
1.1.1.10  root      765:        }
1.1.1.14  root      766:        return 0;
1.1.1.10  root      767: }
1.1.1.14  root      768: #endif
1.1.1.10  root      769: 
1.1.1.9   root      770: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                    771: 
1.1       root      772: int main(int argc, char *argv[], char *envp[])
                    773: {
1.1.1.9   root      774:        int arg_offset = 0;
                    775:        int standard_env = 0;
1.1.1.14  root      776:        int buf_width = 0, buf_height = 0;
1.1.1.15  root      777:        BOOL bSuccess, bChangeScreenSize = FALSE;
1.1       root      778:        
1.1.1.9   root      779:        for(int i = 1; i < argc; i++) {
1.1.1.25! root      780:                if(_strnicmp(argv[i], "-b", 2) == 0) {
        !           781:                        stay_busy = true;
        !           782:                        arg_offset++;
        !           783:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
        !           784:                        no_windows = true;
        !           785:                        arg_offset++;
        !           786:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root      787:                        standard_env = 1;
                    788:                        arg_offset++;
1.1.1.14  root      789:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                    790:                        ignore_illegal_insn = true;
                    791:                        arg_offset++;
                    792:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                    793:                        limit_max_memory = true;
                    794:                        arg_offset++;
                    795:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root      796:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                    797:                                buf_width = buf_height = 0;
                    798:                        }
                    799:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                    800:                                buf_width = 80;
                    801:                        }
                    802:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                    803:                                buf_height = 25;
                    804:                        }
1.1.1.14  root      805:                        arg_offset++;
1.1.1.25! root      806:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
        !           807:                        if(strlen(argv[i]) > 2) {
        !           808:                                char *p1 = &argv[i][2], *p2;
        !           809:                                if((p2 = strchr(p1, ',')) != NULL) {
        !           810:                                        sio[1].port_number = atoi(p2 + 1);
        !           811:                                }
        !           812:                                sio[0].port_number = atoi(p1);
        !           813:                        }
        !           814:                        if(sio[0].port_number == 0 || sio[1].port_number == 0) {
        !           815:                                SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
        !           816:                                HDEVINFO hDevInfo = 0;
        !           817:                                HKEY hKey = 0;
        !           818:                                if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
        !           819:                                        for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
        !           820:                                                if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
        !           821:                                                        char chData[256];
        !           822:                                                        DWORD dwType = 0;
        !           823:                                                        DWORD dwSize = sizeof(chData);
        !           824:                                                        int port_number = 0;
        !           825:                                                        
        !           826:                                                        if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
        !           827:                                                                if(_strnicmp(chData, "COM", 3) == 0) {
        !           828:                                                                        port_number = atoi(chData + 3);
        !           829:                                                                }
        !           830:                                                        }
        !           831:                                                        RegCloseKey(hKey);
        !           832:                                                        
        !           833:                                                        if(sio[0].port_number == port_number || sio[1].port_number == port_number) {
        !           834:                                                                continue;
        !           835:                                                        }
        !           836:                                                        if(sio[0].port_number == 0) {
        !           837:                                                                sio[0].port_number = port_number;
        !           838:                                                        } else if(sio[1].port_number == 0) {
        !           839:                                                                sio[1].port_number = port_number;
        !           840:                                                        }
        !           841:                                                        if(sio[0].port_number != 0 && sio[1].port_number != 0) {
        !           842:                                                                break;
        !           843:                                                        }
        !           844:                                                }
        !           845:                                        }
        !           846:                                }
        !           847:                        }
        !           848:                        arg_offset++;
1.1.1.9   root      849:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root      850:                        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      851:                                major_version = argv[i][2] - '0';
1.1.1.17  root      852:                                minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root      853:                        }
                    854:                        arg_offset++;
1.1.1.25! root      855:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
        !           856:                        UMB_TOP = EMS_TOP + EMS_SIZE;
        !           857:                        support_ems = true;
        !           858: #ifdef SUPPORT_XMS
        !           859:                        support_xms = true;
        !           860: #endif
        !           861:                        arg_offset++;
1.1.1.9   root      862:                } else {
                    863:                        break;
                    864:                }
                    865:        }
                    866:        
                    867:        if(argc < 2 + arg_offset) {
1.1       root      868: #ifdef _WIN64
1.1.1.14  root      869:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root      870: #else
1.1.1.14  root      871:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root      872: #endif
1.1.1.25! root      873:                fprintf(stderr,
        !           874:                        "Usage: MSDOS [-b] [-d] [-e] [-i] [-m] [-n[L[,C]]] [-s[P1[,P2]]] [-vX.XX] [-x]\n"
        !           875:                        "             (command file) [options]\n"
        !           876:                        "\n"
        !           877:                        "\t-b\tstay busy during keyboard polling\n"
        !           878:                        "\t-d\tpretend running under straight DOS, not Windows\n"
        !           879:                        "\t-e\tuse a reduced environment block\n"
        !           880:                        "\t-i\tignore invalid instructions\n"
        !           881:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
        !           882:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
        !           883:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
        !           884:                        "\t-v\tset the DOS version\n"
1.1.1.19  root      885: #ifdef SUPPORT_XMS
1.1.1.25! root      886:                        "\t-x\tenable XMS/EMS\n"
1.1.1.19  root      887: #else
1.1.1.25! root      888:                        "\t-x\tenable EMS\n"
1.1.1.19  root      889: #endif
                    890:                );
1.1.1.10  root      891:                
                    892:                if(!is_started_from_command_prompt()) {
                    893:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                    894:                        while(!_kbhit()) {
                    895:                                Sleep(10);
                    896:                        }
                    897:                }
1.1.1.20  root      898: #ifdef _DEBUG
                    899:                _CrtDumpMemoryLeaks();
                    900: #endif
1.1       root      901:                return(EXIT_FAILURE);
                    902:        }
                    903:        
1.1.1.14  root      904:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                    905:        
1.1.1.23  root      906:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root      907:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root      908:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root      909:        
1.1.1.12  root      910:        bSuccess = GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root      911:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root      912:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root      913:        
1.1.1.14  root      914:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                    915:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                    916:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                    917:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root      918:                }
                    919:        }
1.1.1.12  root      920:        if(bSuccess) {
                    921:                scr_width = csbi.dwSize.X;
1.1.1.14  root      922:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                    923:                
                    924:                // v-text shadow buffer size is 0x7ff0
                    925:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7ff0) ||
                    926:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                    927:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                    928:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
                    929:                        if(scr_width * scr_height * 2 > 0x7ff0) {
                    930:                                scr_width = 80;
                    931:                                scr_height = 25;
                    932:                        }
1.1.1.15  root      933:                        bChangeScreenSize = TRUE;
1.1.1.14  root      934:                }
1.1.1.12  root      935:        } else {
                    936:                // for a proof (not a console)
                    937:                scr_width = 80;
                    938:                scr_height = 25;
                    939:        }
1.1.1.14  root      940:        scr_buf_size.X = scr_width;
                    941:        scr_buf_size.Y = scr_height;
                    942:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                    943:        scr_top = csbi.srWindow.Top;
1.1       root      944:        cursor_moved = false;
                    945:        
1.1.1.25! root      946:        key_buf_char = new FIFO(256);
        !           947:        key_buf_scan = new FIFO(256);
1.1       root      948:        
                    949:        hardware_init();
                    950:        
1.1.1.9   root      951:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root      952:                retval = EXIT_FAILURE;
                    953:        } else {
1.1.1.14  root      954: #if defined(_MSC_VER) && _MSC_VER >= 1400
                    955:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                    956: #endif
                    957:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                    958:                
1.1.1.24  root      959:                if(bChangeScreenSize) {
                    960:                        change_console_size(scr_width, scr_height);
                    961:                }
1.1.1.8   root      962:                TIMECAPS caps;
                    963:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                    964:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.14  root      965: #ifdef USE_THREAD
                    966:                InitializeCriticalSection(&vram_crit_sect);
                    967:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                    968: #endif
1.1       root      969:                hardware_run();
1.1.1.14  root      970: #ifdef USE_THREAD
                    971:                vram_flush();
                    972:                DeleteCriticalSection(&vram_crit_sect);
                    973: #endif
1.1.1.24  root      974:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root      975:                
1.1.1.24  root      976:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.12  root      977:                if(bSuccess) {
1.1.1.23  root      978:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root      979:                        if(restore_console_on_exit) {
1.1.1.14  root      980:                                // window can't be bigger than buffer,
                    981:                                // buffer can't be smaller than window,
                    982:                                // so make a tiny window,
                    983:                                // set the required buffer,
                    984:                                // then set the required window
                    985:                                SMALL_RECT rect;
                    986:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                    987:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root      988:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root      989:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root      990:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                    991:                        }
1.1.1.14  root      992:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                    993:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root      994:                }
1.1.1.24  root      995:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                    996:                
1.1       root      997:                msdos_finish();
1.1.1.14  root      998:                
                    999:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     1000:        }
                   1001:        
1.1.1.10  root     1002:        hardware_finish();
                   1003:        
1.1.1.25! root     1004:        key_buf_char->release();
1.1       root     1005:        delete key_buf_char;
1.1.1.25! root     1006:        key_buf_scan->release();
1.1       root     1007:        delete key_buf_scan;
                   1008:        
1.1.1.12  root     1009: //     SetConsoleTextAttribute(hStdout, csbi.wAttributes);
1.1       root     1010:        
1.1.1.20  root     1011: #ifdef _DEBUG
                   1012:        _CrtDumpMemoryLeaks();
                   1013: #endif
1.1       root     1014:        return(retval);
                   1015: }
                   1016: 
1.1.1.20  root     1017: /* ----------------------------------------------------------------------------
                   1018:        console
                   1019: ---------------------------------------------------------------------------- */
                   1020: 
1.1.1.14  root     1021: void change_console_size(int width, int height)
1.1.1.12  root     1022: {
1.1.1.23  root     1023:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     1024:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   1025:        SMALL_RECT rect;
                   1026:        COORD co;
                   1027:        
                   1028:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     1029:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   1030:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   1031:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   1032:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1033:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1034:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   1035:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   1036:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   1037:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1038:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     1039:                }
                   1040:        }
1.1.1.14  root     1041:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     1042:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     1043:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     1044:                SetConsoleCursorPosition(hStdout, co);
                   1045:                cursor_moved = true;
                   1046:        }
1.1.1.14  root     1047:        
                   1048:        // window can't be bigger than buffer,
                   1049:        // buffer can't be smaller than window,
                   1050:        // so make a tiny window,
                   1051:        // set the required buffer,
                   1052:        // then set the required window
                   1053:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     1054:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     1055:        co.X = width;
                   1056:        co.Y = height;
1.1.1.12  root     1057:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     1058:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   1059:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   1060:        
                   1061:        scr_width = scr_buf_size.X = width;
                   1062:        scr_height = scr_buf_size.Y = height;
                   1063:        scr_top = 0;
                   1064:        
                   1065:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   1066:        
                   1067:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     1068:        text_vram_end_address = text_vram_top_address + regen;
                   1069:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   1070:        
1.1.1.14  root     1071:        if(regen > 0x4000) {
                   1072:                regen = 0x8000;
                   1073:                vram_pages = 1;
                   1074:        } else if(regen > 0x2000) {
                   1075:                regen = 0x4000;
                   1076:                vram_pages = 2;
                   1077:        } else if(regen > 0x1000) {
                   1078:                regen = 0x2000;
                   1079:                vram_pages = 4;
                   1080:        } else {
                   1081:                regen = 0x1000;
                   1082:                vram_pages = 8;
                   1083:        }
1.1.1.15  root     1084:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   1085:        *(UINT16 *)(mem + 0x44c) = regen;
                   1086:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   1087:        
1.1.1.24  root     1088:        mouse.min_position.x = 0;
                   1089:        mouse.min_position.y = 0;
                   1090:        mouse.max_position.x = 8 * scr_width  - 1;
                   1091:        mouse.max_position.y = 8 * scr_height - 1;
                   1092:        
1.1.1.15  root     1093:        restore_console_on_exit = true;
1.1.1.14  root     1094: }
                   1095: 
                   1096: void clear_scr_buffer(WORD attr)
                   1097: {
                   1098:        for(int y = 0; y < scr_height; y++) {
                   1099:                for(int x = 0; x < scr_width; x++) {
                   1100:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   1101:                        SCR_BUF(y,x).Attributes = attr;
                   1102:                }
                   1103:        }
1.1.1.12  root     1104: }
                   1105: 
1.1.1.24  root     1106: bool update_console_input()
1.1       root     1107: {
1.1.1.23  root     1108:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     1109:        DWORD dwNumberOfEvents = 0;
1.1       root     1110:        DWORD dwRead;
                   1111:        INPUT_RECORD ir[16];
1.1.1.24  root     1112:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   1113:        bool result = false;
1.1       root     1114:        
1.1.1.8   root     1115:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   1116:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   1117:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     1118:                                if(ir[i].EventType & MOUSE_EVENT) {
                   1119:                                        if(mouse.active) {
                   1120:                                                if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   1121:                                                        for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   1122:                                                                static const DWORD bits[] = {
                   1123:                                                                        FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   1124:                                                                        RIGHTMOST_BUTTON_PRESSED,       // right
                   1125:                                                                        FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
1.1.1.14  root     1126:                                                                };
1.1.1.24  root     1127:                                                                bool prev_status = mouse.buttons[i].status;
                   1128:                                                                mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   1129:                                                                
                   1130:                                                                if(!prev_status && mouse.buttons[i].status) {
                   1131:                                                                        mouse.buttons[i].pressed_times++;
                   1132:                                                                        mouse.buttons[i].pressed_position.x = mouse.position.x;
                   1133:                                                                        mouse.buttons[i].pressed_position.y = mouse.position.y;
                   1134:                                                                        mouse.status |= 2 << (i * 2);
                   1135:                                                                } else if(prev_status && !mouse.buttons[i].status) {
                   1136:                                                                        mouse.buttons[i].released_times++;
                   1137:                                                                        mouse.buttons[i].released_position.x = mouse.position.x;
                   1138:                                                                        mouse.buttons[i].released_position.y = mouse.position.y;
                   1139:                                                                        mouse.status |= 4 << (i * 2);
                   1140:                                                                }
1.1.1.14  root     1141:                                                        }
1.1.1.24  root     1142:                                                } else if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   1143:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   1144:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   1145:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.14  root     1146:                                                        }
1.1.1.24  root     1147:                                                        // FIXME: character is always 8x8 ???
                   1148:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   1149:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   1150:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   1151:                                                                mouse.position.x = x;
                   1152:                                                                mouse.position.y = y;
                   1153:                                                                mouse.status |= 1;
1.1.1.14  root     1154:                                                        }
                   1155:                                                }
                   1156:                                        }
1.1.1.24  root     1157:                                } else if(ir[i].EventType & KEY_EVENT) {
                   1158:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.14  root     1159:                                        if(!ir[i].Event.KeyEvent.bKeyDown) {
                   1160:                                                kbd_data |= 0x80;
1.1.1.24  root     1161:                                        } else {
                   1162:                                                kbd_data &= 0x7f;
                   1163:                                                
                   1164:                                                // update dos key buffer
                   1165:                                                UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   1166:                                                UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   1167:                                                
                   1168:                                                if(chr == 0) {
                   1169:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1170:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1171:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   1172:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1173:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   1174:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   1175:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   1176:                                                                } else if(scn == 0x35) {
                   1177:                                                                        scn = 0xa4;             // keypad /
                   1178:                                                                }
                   1179:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   1180:                                                                if(scn == 0x07) {
                   1181:                                                                        chr = 0x1e;     // Ctrl+^
                   1182:                                                                } else if(scn == 0x0c) {
                   1183:                                                                        chr = 0x1f;     // Ctrl+_
                   1184:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   1185:                                                                        static const UINT8 ctrl_map[] = {
                   1186:                                                                                0x95,   // keypad /
                   1187:                                                                                0,
                   1188:                                                                                0x96,   // keypad *
                   1189:                                                                                0, 0, 0,
                   1190:                                                                                0x5e,   // F1
                   1191:                                                                                0x5f,   // F2
                   1192:                                                                                0x60,   // F3
                   1193:                                                                                0x61,   // F4
                   1194:                                                                                0x62,   // F5
                   1195:                                                                                0x63,   // F6
                   1196:                                                                                0x64,   // F7
                   1197:                                                                                0x65,   // F8
                   1198:                                                                                0x66,   // F9
                   1199:                                                                                0x67,   // F10
                   1200:                                                                                0,
                   1201:                                                                                0,
                   1202:                                                                                0x77,   // Home
                   1203:                                                                                0x8d,   // Up
                   1204:                                                                                0x84,   // PgUp
                   1205:                                                                                0x8e,   // keypad -
                   1206:                                                                                0x73,   // Left
                   1207:                                                                                0x8f,   // keypad center
                   1208:                                                                                0x74,   // Right
                   1209:                                                                                0x90,   // keyapd +
                   1210:                                                                                0x75,   // End
                   1211:                                                                                0x91,   // Down
                   1212:                                                                                0x76,   // PgDn
                   1213:                                                                                0x92,   // Insert
                   1214:                                                                                0x93,   // Delete
                   1215:                                                                                0, 0, 0,
                   1216:                                                                                0x89,   // F11
                   1217:                                                                                0x8a,   // F12
                   1218:                                                                        };
                   1219:                                                                        scn = ctrl_map[scn - 0x35];
                   1220:                                                                }
                   1221:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   1222:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   1223:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   1224:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   1225:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   1226:                                                                }
                   1227:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   1228:                                                                scn += 0x85 - 0x57;
                   1229:                                                        }
                   1230:                                                        // ignore shift, ctrl, alt, win and menu keys
                   1231:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
                   1232:                                                                if(chr == 0) {
                   1233:                                                                        key_buf_char->write(0x00);
                   1234:                                                                        key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   1235:                                                                }
                   1236:                                                                key_buf_char->write(chr);
                   1237:                                                                key_buf_scan->write(scn);
                   1238:                                                        }
                   1239:                                                } else {
                   1240:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   1241:                                                                chr = 0;
                   1242:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   1243:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   1244:                                                                }
                   1245:                                                        }
                   1246:                                                        key_buf_char->write(chr);
                   1247:                                                        key_buf_scan->write(scn);
                   1248:                                                }
1.1       root     1249:                                        }
1.1.1.24  root     1250:                                        result = key_changed = true;
1.1       root     1251:                                }
                   1252:                        }
                   1253:                }
                   1254:        }
1.1.1.24  root     1255:        return(result);
1.1.1.8   root     1256: }
                   1257: 
1.1.1.14  root     1258: bool update_key_buffer()
1.1.1.8   root     1259: {
1.1.1.24  root     1260:        return(update_console_input() || key_buf_char->count() != 0);
1.1.1.8   root     1261: }
                   1262: 
1.1.1.20  root     1263: /* ----------------------------------------------------------------------------
                   1264:        MS-DOS virtual machine
                   1265: ---------------------------------------------------------------------------- */
                   1266: 
                   1267: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   1268: int msdos_psp_get_file_table(int fd, int psp_seg);
                   1269: void msdos_putch(UINT8 data);
                   1270: 
1.1       root     1271: // process info
                   1272: 
                   1273: process_t *msdos_process_info_create(UINT16 psp_seg)
                   1274: {
                   1275:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1276:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   1277:                        memset(&process[i], 0, sizeof(process_t));
                   1278:                        process[i].psp = psp_seg;
                   1279:                        return(&process[i]);
                   1280:                }
                   1281:        }
                   1282:        fatalerror("too many processes\n");
                   1283:        return(NULL);
                   1284: }
                   1285: 
                   1286: process_t *msdos_process_info_get(UINT16 psp_seg)
                   1287: {
                   1288:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1289:                if(process[i].psp == psp_seg) {
                   1290:                        return(&process[i]);
                   1291:                }
                   1292:        }
                   1293:        fatalerror("invalid psp address\n");
                   1294:        return(NULL);
                   1295: }
                   1296: 
1.1.1.23  root     1297: void msdos_sda_update(int psp_seg)
                   1298: {
                   1299:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   1300:        
                   1301:        for(int i = 0; i < MAX_PROCESS; i++) {
                   1302:                if(process[i].psp == psp_seg) {
                   1303:                        sda->switchar = process[i].switchar;
                   1304:                        sda->current_dta.w.l = process[i].dta.w.l;
                   1305:                        sda->current_dta.w.h = process[i].dta.w.h;
                   1306:                        sda->current_psp = process[i].psp;
                   1307:                        break;
                   1308:                }
                   1309:        }
                   1310:        sda->malloc_strategy = malloc_strategy;
                   1311:        sda->return_code = retval;
                   1312:        sda->current_drive = _getdrive();
                   1313: }
                   1314: 
1.1.1.13  root     1315: // dta info
                   1316: 
                   1317: void msdos_dta_info_init()
                   1318: {
1.1.1.14  root     1319:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     1320:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1321:        }
                   1322: }
                   1323: 
                   1324: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   1325: {
                   1326:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     1327:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1328:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   1329:                        if(free_dta == NULL) {
1.1.1.13  root     1330:                                free_dta = &dtalist[i];
                   1331:                        }
1.1.1.14  root     1332:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     1333:                        return(&dtalist[i]);
                   1334:                }
                   1335:        }
1.1.1.14  root     1336:        if(free_dta) {
1.1.1.13  root     1337:                free_dta->psp = psp_seg;
                   1338:                free_dta->dta = dta_laddr;
                   1339:                return(free_dta);
                   1340:        }
                   1341:        fatalerror("too many dta\n");
                   1342:        return(NULL);
                   1343: }
                   1344: 
                   1345: void msdos_dta_info_free(UINT16 psp_seg)
                   1346: {
1.1.1.14  root     1347:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   1348:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     1349:                        FindClose(dtalist[i].find_handle);
                   1350:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   1351:                }
                   1352:        }
                   1353: }
                   1354: 
1.1       root     1355: void msdos_cds_update(int drv)
                   1356: {
                   1357:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                   1358:        
                   1359:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                   1360:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   1361:        cds->drive_attrib = 0x4000;     // physical drive
                   1362:        cds->physical_drive_number = drv;
                   1363: }
                   1364: 
1.1.1.17  root     1365: // nls information tables
                   1366: 
                   1367: // uppercase table (func 6502h)
                   1368: void msdos_upper_table_update()
                   1369: {
                   1370:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1371:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1372:                UINT8 c[4];
                   1373:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1374:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1375:                c[0] = 0x80 + i;
                   1376:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   1377:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1378:        }
                   1379: }
                   1380: 
1.1.1.23  root     1381: // lowercase table (func 6503h)
                   1382: void msdos_lower_table_update()
                   1383: {
                   1384:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   1385:        for(unsigned i = 0; i < 0x80; ++i) {
                   1386:                UINT8 c[4];
                   1387:                *(UINT32 *)c = 0;                               // reset internal conversion state
                   1388:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage) 
                   1389:                c[0] = 0x80 + i;
                   1390:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   1391:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   1392:        }
                   1393: }
                   1394: 
1.1.1.17  root     1395: // filename uppercase table (func 6504h)
                   1396: void msdos_filename_upper_table_init()
                   1397: {
                   1398:        // depended on (file)system, not on active codepage
                   1399:        // temporary solution: just filling data
                   1400:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     1401:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     1402:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   1403:        }
                   1404: }
                   1405: 
                   1406: // filaname terminator table (func 6505h)
                   1407: void msdos_filename_terminator_table_init()
                   1408: {
                   1409:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   1410:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   1411:        
                   1412:        data[2] = 1;            // marker? (permissible character value)
                   1413:        data[3] = 0x00;         // 00h...FFh
                   1414:        data[4] = 0xff;
                   1415:        data[5] = 0;            // marker? (excluded character)
                   1416:        data[6] = 0x00;         // 00h...20h
                   1417:        data[7] = 0x20;
                   1418:        data[8] = 2;            // marker? (illegal characters for filename)
                   1419:        data[9] = (UINT8)strlen(illegal_chars);
                   1420:        memcpy(data + 10, illegal_chars, data[9]);
                   1421:        
                   1422:        // total length
                   1423:        *(UINT16 *)data = (10 - 2) + data[9];
                   1424: }
                   1425: 
                   1426: // collating table (func 6506h)
                   1427: void msdos_collating_table_update()
                   1428: {
                   1429:        // temporary solution: just filling data
                   1430:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     1431:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     1432:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   1433:        }
                   1434: }
                   1435: 
1.1       root     1436: // dbcs
                   1437: 
                   1438: void msdos_dbcs_table_update()
                   1439: {
                   1440:        UINT8 dbcs_data[DBCS_SIZE];
                   1441:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   1442:        
                   1443:        CPINFO info;
                   1444:        GetCPInfo(active_code_page, &info);
                   1445:        
                   1446:        if(info.MaxCharSize != 1) {
                   1447:                for(int i = 0;; i += 2) {
                   1448:                        UINT8 lo = info.LeadByte[i + 0];
                   1449:                        UINT8 hi = info.LeadByte[i + 1];
                   1450:                        dbcs_data[2 + i + 0] = lo;
                   1451:                        dbcs_data[2 + i + 1] = hi;
                   1452:                        if(lo == 0 && hi == 0) {
                   1453:                                dbcs_data[0] = i + 2;
                   1454:                                break;
                   1455:                        }
                   1456:                }
                   1457:        } else {
                   1458:                dbcs_data[0] = 2;       // ???
                   1459:        }
                   1460:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   1461: }
                   1462: 
1.1.1.17  root     1463: void msdos_dbcs_table_finish()
                   1464: {
                   1465:        if(active_code_page != system_code_page) {
                   1466:                _setmbcp(system_code_page);
                   1467:        }
                   1468: }
                   1469: 
                   1470: void msdos_nls_tables_init()
1.1       root     1471: {
                   1472:        system_code_page = active_code_page = _getmbcp();
1.1.1.17  root     1473:        msdos_upper_table_update();
1.1.1.23  root     1474:        msdos_lower_table_update();
1.1.1.17  root     1475:        msdos_filename_terminator_table_init();
                   1476:        msdos_filename_upper_table_init();
                   1477:        msdos_collating_table_update();
1.1       root     1478:        msdos_dbcs_table_update();
                   1479: }
                   1480: 
1.1.1.17  root     1481: void msdos_nls_tables_update()
1.1       root     1482: {
1.1.1.17  root     1483:        msdos_dbcs_table_update();
                   1484:        msdos_upper_table_update();
1.1.1.23  root     1485:        msdos_lower_table_update();
                   1486: //     msdos_collating_table_update();
1.1       root     1487: }
                   1488: 
                   1489: int msdos_lead_byte_check(UINT8 code)
                   1490: {
                   1491:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   1492:        
                   1493:        for(int i = 0;; i += 2) {
                   1494:                UINT8 lo = dbcs_table[i + 0];
                   1495:                UINT8 hi = dbcs_table[i + 1];
                   1496:                if(lo == 0 && hi == 0) {
                   1497:                        break;
                   1498:                }
                   1499:                if(lo <= code && code <= hi) {
                   1500:                        return(1);
                   1501:                }
                   1502:        }
                   1503:        return(0);
                   1504: }
                   1505: 
1.1.1.20  root     1506: int msdos_ctrl_code_check(UINT8 code)
                   1507: {
1.1.1.22  root     1508:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     1509: }
                   1510: 
1.1       root     1511: // file control
                   1512: 
1.1.1.14  root     1513: char *msdos_remove_double_quote(char *path)
                   1514: {
                   1515:        static char tmp[MAX_PATH];
                   1516:        
                   1517:        memset(tmp, 0, sizeof(tmp));
                   1518:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   1519:                memcpy(tmp, path + 1, strlen(path) - 2);
                   1520:        } else {
                   1521:                strcpy(tmp, path);
                   1522:        }
                   1523:        return(tmp);
                   1524: }
                   1525: 
                   1526: char *msdos_combine_path(char *dir, const char *file)
                   1527: {
                   1528:        static char tmp[MAX_PATH];
                   1529:        char *tmp_dir = msdos_remove_double_quote(dir);
                   1530:        
                   1531:        if(strlen(tmp_dir) == 0) {
                   1532:                strcpy(tmp, file);
                   1533:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   1534:                sprintf(tmp, "%s%s", tmp_dir, file);
                   1535:        } else {
                   1536:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   1537:        }
                   1538:        return(tmp);
                   1539: }
                   1540: 
1.1       root     1541: char *msdos_trimmed_path(char *path, int lfn)
                   1542: {
                   1543:        static char tmp[MAX_PATH];
                   1544:        
                   1545:        if(lfn) {
                   1546:                strcpy(tmp, path);
                   1547:        } else {
                   1548:                // remove space in the path
                   1549:                char *src = path, *dst = tmp;
                   1550:                
                   1551:                while(*src != '\0') {
                   1552:                        if(msdos_lead_byte_check(*src)) {
                   1553:                                *dst++ = *src++;
                   1554:                                *dst++ = *src++;
                   1555:                        } else if(*src != ' ') {
                   1556:                                *dst++ = *src++;
                   1557:                        } else {
                   1558:                                src++;  // skip space
                   1559:                        }
                   1560:                }
                   1561:                *dst = '\0';
                   1562:        }
1.1.1.14  root     1563:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   1564:                // redirect C:\COMMAND.COM to comspec_path
                   1565:                strcpy(tmp, comspec_path);
                   1566:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   1567:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   1568:                static int root_drive_protected = -1;
                   1569:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   1570:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   1571:                
                   1572:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   1573:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   1574:                        strcpy(name, name_temp);
                   1575:                        name_temp[0] = '\0';
                   1576:                        
                   1577:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   1578:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   1579:                                if(root_drive_protected == -1) {
                   1580:                                        FILE *fp = NULL;
                   1581:                                        
                   1582:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   1583:                                        root_drive_protected = 1;
                   1584:                                        try {
                   1585:                                                if((fp = fopen(temp, "w")) != NULL) {
                   1586:                                                        if(fprintf(fp, "TEST") == 4) {
                   1587:                                                                root_drive_protected = 0;
                   1588:                                                        }
                   1589:                                                }
                   1590:                                        } catch(...) {
                   1591:                                        }
                   1592:                                        if(fp != NULL) {
                   1593:                                                fclose(fp);
                   1594:                                        }
                   1595:                                        if(_access(temp, 0) == 0) {
                   1596:                                                remove(temp);
                   1597:                                        }
                   1598:                                }
                   1599:                                if(root_drive_protected == 1) {
                   1600:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   1601:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   1602:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   1603:                                        }
                   1604:                                }
                   1605:                        }
                   1606:                }
                   1607:        }
1.1       root     1608:        return(tmp);
                   1609: }
                   1610: 
                   1611: bool match(char *text, char *pattern)
                   1612: {
1.1.1.24  root     1613:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     1614:        switch(*pattern) {
1.1       root     1615:        case '\0':
                   1616:                return !*text;
                   1617:        case '*':
1.1.1.14  root     1618:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     1619:        case '?':
                   1620:                return *text && match(text + 1, pattern + 1);
                   1621:        default:
                   1622:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   1623:        }
                   1624: }
                   1625: 
                   1626: bool msdos_match_volume_label(char *path, char *volume)
                   1627: {
                   1628:        char *p;
                   1629:        
1.1.1.14  root     1630:        if(!*volume) {
                   1631:                return false;
                   1632:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     1633:                return msdos_match_volume_label(p + 1, volume);
                   1634:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   1635:                return msdos_match_volume_label(p + 1, volume);
                   1636:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     1637:                char tmp[MAX_PATH];
                   1638:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   1639:                return match(volume, tmp);
1.1       root     1640:        } else {
                   1641:                return match(volume, path);
                   1642:        }
                   1643: }
                   1644: 
                   1645: char *msdos_fcb_path(fcb_t *fcb)
                   1646: {
                   1647:        static char tmp[MAX_PATH];
                   1648:        char name[9], ext[4];
                   1649:        
                   1650:        memset(name, 0, sizeof(name));
                   1651:        memcpy(name, fcb->file_name, 8);
                   1652:        strcpy(name, msdos_trimmed_path(name, 0));
                   1653:        
                   1654:        memset(ext, 0, sizeof(ext));
                   1655:        memcpy(ext, fcb->file_name + 8, 3);
                   1656:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   1657:        
                   1658:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   1659:                strcpy(name, "*");
                   1660:        }
                   1661:        if(ext[0] == '\0') {
                   1662:                strcpy(tmp, name);
                   1663:        } else {
                   1664:                if(strcmp(ext, "???") == 0) {
                   1665:                        strcpy(ext, "*");
                   1666:                }
                   1667:                sprintf(tmp, "%s.%s", name, ext);
                   1668:        }
                   1669:        return(tmp);
                   1670: }
                   1671: 
                   1672: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   1673: {
                   1674:        char *ext = my_strchr(path, '.');
                   1675:        
                   1676:        memset(fcb->file_name, 0x20, 8 + 3);
                   1677:        if(ext != NULL && path[0] != '.') {
                   1678:                *ext = '\0';
                   1679:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   1680:        }
                   1681:        memcpy(fcb->file_name, path, strlen(path));
                   1682: }
                   1683: 
                   1684: char *msdos_short_path(char *path)
                   1685: {
                   1686:        static char tmp[MAX_PATH];
                   1687:        
1.1.1.24  root     1688:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   1689:                strcpy(tmp, path);
                   1690:        }
1.1       root     1691:        my_strupr(tmp);
                   1692:        return(tmp);
                   1693: }
                   1694: 
1.1.1.13  root     1695: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   1696: {
                   1697:        static char tmp[MAX_PATH];
                   1698: 
1.1.1.14  root     1699:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     1700:                strcpy(tmp, fd->cAlternateFileName);
                   1701:        } else {
                   1702:                strcpy(tmp, fd->cFileName);
                   1703:        }
                   1704:        my_strupr(tmp);
                   1705:        return(tmp);
                   1706: }
                   1707: 
1.1       root     1708: char *msdos_short_full_path(char *path)
                   1709: {
                   1710:        static char tmp[MAX_PATH];
                   1711:        char full[MAX_PATH], *name;
                   1712:        
1.1.1.14  root     1713:        // Full works with non-existent files, but Short does not
1.1       root     1714:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     1715:        *tmp = '\0';
                   1716:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   1717:                name[-1] = '\0';
                   1718:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   1719:                if(len == 0) {
                   1720:                        strcpy(tmp, full);
                   1721:                } else {
                   1722:                        tmp[len++] = '\\';
                   1723:                        strcpy(tmp + len, name);
                   1724:                }
                   1725:        }
1.1       root     1726:        my_strupr(tmp);
                   1727:        return(tmp);
                   1728: }
                   1729: 
                   1730: char *msdos_short_full_dir(char *path)
                   1731: {
                   1732:        static char tmp[MAX_PATH];
                   1733:        char full[MAX_PATH], *name;
                   1734:        
                   1735:        GetFullPathName(path, MAX_PATH, full, &name);
                   1736:        name[-1] = '\0';
1.1.1.24  root     1737:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   1738:                strcpy(tmp, full);
                   1739:        }
1.1       root     1740:        my_strupr(tmp);
                   1741:        return(tmp);
                   1742: }
                   1743: 
                   1744: char *msdos_local_file_path(char *path, int lfn)
                   1745: {
                   1746:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     1747: #if 0
                   1748:        // I have forgotten the reason of this routine... :-(
1.1       root     1749:        if(_access(trimmed, 0) != 0) {
                   1750:                process_t *process = msdos_process_info_get(current_psp);
                   1751:                static char tmp[MAX_PATH];
                   1752:                
                   1753:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   1754:                if(_access(tmp, 0) == 0) {
                   1755:                        return(tmp);
                   1756:                }
                   1757:        }
1.1.1.14  root     1758: #endif
1.1       root     1759:        return(trimmed);
                   1760: }
                   1761: 
1.1.1.11  root     1762: bool msdos_is_con_path(char *path)
                   1763: {
                   1764:        char full[MAX_PATH], *name;
                   1765:        
1.1.1.24  root     1766:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   1767:                return(_stricmp(full, "\\\\.\\CON") == 0);
                   1768:        }
                   1769:        return(false);
1.1.1.11  root     1770: }
                   1771: 
1.1.1.14  root     1772: bool msdos_is_nul_path(char *path)
1.1.1.8   root     1773: {
1.1.1.14  root     1774:        char full[MAX_PATH], *name;
1.1.1.8   root     1775:        
1.1.1.24  root     1776:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   1777:                return(_stricmp(full, "\\\\.\\NUL") == 0);
                   1778:        }
                   1779:        return(false);
                   1780: }
                   1781: 
                   1782: bool msdos_is_driver_name(char *path)
                   1783: {
                   1784:        char full[MAX_PATH], *name;
                   1785:        
                   1786:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   1787:                if(_stricmp(name, "EMMXXXX0") == 0) {
                   1788:                        return(true);
                   1789:                }
                   1790:        }
                   1791:        return(false);
                   1792: }
                   1793: 
                   1794: bool msdos_is_existing_file(char *path)
                   1795: {
                   1796:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   1797:        WIN32_FIND_DATA FindData;
                   1798:        HANDLE hFind;
                   1799:        
                   1800:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   1801:                FindClose(hFind);
                   1802:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   1803:        }
                   1804:        return(false);
1.1.1.8   root     1805: }
                   1806: 
1.1.1.9   root     1807: char *msdos_search_command_com(char *command_path, char *env_path)
                   1808: {
                   1809:        static char tmp[MAX_PATH];
                   1810:        char path[MAX_PATH], *file_name;
                   1811:        
                   1812:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   1813:                sprintf(file_name, "COMMAND.COM");
                   1814:                if(_access(tmp, 0) == 0) {
                   1815:                        return(tmp);
                   1816:                }
                   1817:        }
                   1818:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   1819:                sprintf(file_name, "COMMAND.COM");
                   1820:                if(_access(tmp, 0) == 0) {
                   1821:                        return(tmp);
                   1822:                }
                   1823:        }
                   1824:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   1825:                if(_access(tmp, 0) == 0) {
                   1826:                        return(tmp);
                   1827:                }
                   1828:        }
                   1829:        char *token = my_strtok(env_path, ";");
                   1830:        while(token != NULL) {
1.1.1.14  root     1831:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     1832:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   1833:                        if(_access(tmp, 0) == 0) {
                   1834:                                return(tmp);
                   1835:                        }
                   1836:                }
                   1837:                token = my_strtok(NULL, ";");
                   1838:        }
                   1839:        return(NULL);
                   1840: }
                   1841: 
1.1.1.14  root     1842: int msdos_drive_number(const char *path)
1.1       root     1843: {
                   1844:        char tmp[MAX_PATH], *name;
                   1845:        
                   1846:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   1847:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   1848:                return(tmp[0] - 'a');
                   1849:        } else {
                   1850:                return(tmp[0] - 'A');
                   1851:        }
                   1852: }
                   1853: 
                   1854: char *msdos_volume_label(char *path)
                   1855: {
                   1856:        static char tmp[MAX_PATH];
                   1857:        char volume[] = "A:\\";
                   1858:        
                   1859:        if(path[1] == ':') {
                   1860:                volume[0] = path[0];
                   1861:        } else {
                   1862:                volume[0] = 'A' + _getdrive() - 1;
                   1863:        }
                   1864:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   1865:                memset(tmp, 0, sizeof(tmp));
                   1866:        }
                   1867:        return(tmp);
                   1868: }
                   1869: 
                   1870: char *msdos_short_volume_label(char *label)
                   1871: {
                   1872:        static char tmp[(8 + 1 + 3) + 1];
                   1873:        char *src = label;
                   1874:        int remain = strlen(label);
                   1875:        char *dst_n = tmp;
                   1876:        char *dst_e = tmp + 9;
                   1877:        
                   1878:        strcpy(tmp, "        .   ");
                   1879:        for(int i = 0; i < 8 && remain > 0; i++) {
                   1880:                if(msdos_lead_byte_check(*src)) {
                   1881:                        if(++i == 8) {
                   1882:                                break;
                   1883:                        }
                   1884:                        *dst_n++ = *src++;
                   1885:                        remain--;
                   1886:                }
                   1887:                *dst_n++ = *src++;
                   1888:                remain--;
                   1889:        }
                   1890:        if(remain > 0) {
                   1891:                for(int i = 0; i < 3 && remain > 0; i++) {
                   1892:                        if(msdos_lead_byte_check(*src)) {
                   1893:                                if(++i == 3) {
                   1894:                                        break;
                   1895:                                }
                   1896:                                *dst_e++ = *src++;
                   1897:                                remain--;
                   1898:                        }
                   1899:                        *dst_e++ = *src++;
                   1900:                        remain--;
                   1901:                }
                   1902:                *dst_e = '\0';
                   1903:        } else {
                   1904:                *dst_n = '\0';
                   1905:        }
                   1906:        my_strupr(tmp);
                   1907:        return(tmp);
                   1908: }
                   1909: 
1.1.1.13  root     1910: errno_t msdos_maperr(unsigned long oserrno)
                   1911: {
                   1912:        _doserrno = oserrno;
1.1.1.14  root     1913:        switch(oserrno) {
1.1.1.13  root     1914:        case ERROR_FILE_NOT_FOUND:         // 2
                   1915:        case ERROR_PATH_NOT_FOUND:         // 3
                   1916:        case ERROR_INVALID_DRIVE:          // 15
                   1917:        case ERROR_NO_MORE_FILES:          // 18
                   1918:        case ERROR_BAD_NETPATH:            // 53
                   1919:        case ERROR_BAD_NET_NAME:           // 67
                   1920:        case ERROR_BAD_PATHNAME:           // 161
                   1921:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   1922:                return ENOENT;
                   1923:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   1924:                return EMFILE;
                   1925:        case ERROR_ACCESS_DENIED:          // 5
                   1926:        case ERROR_CURRENT_DIRECTORY:      // 16
                   1927:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   1928:        case ERROR_CANNOT_MAKE:            // 82
                   1929:        case ERROR_FAIL_I24:               // 83
                   1930:        case ERROR_DRIVE_LOCKED:           // 108
                   1931:        case ERROR_SEEK_ON_DEVICE:         // 132
                   1932:        case ERROR_NOT_LOCKED:             // 158
                   1933:        case ERROR_LOCK_FAILED:            // 167
                   1934:                return EACCES;
                   1935:        case ERROR_INVALID_HANDLE:         // 6
                   1936:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   1937:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   1938:                return EBADF;
                   1939:        case ERROR_ARENA_TRASHED:          // 7
                   1940:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   1941:        case ERROR_INVALID_BLOCK:          // 9
                   1942:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   1943:                return ENOMEM;
                   1944:        case ERROR_BAD_ENVIRONMENT:        // 10
                   1945:                return E2BIG;
                   1946:        case ERROR_BAD_FORMAT:             // 11
                   1947:                return ENOEXEC;
                   1948:        case ERROR_NOT_SAME_DEVICE:        // 17
                   1949:                return EXDEV;
                   1950:        case ERROR_FILE_EXISTS:            // 80
                   1951:        case ERROR_ALREADY_EXISTS:         // 183
                   1952:                return EEXIST;
                   1953:        case ERROR_NO_PROC_SLOTS:          // 89
                   1954:        case ERROR_MAX_THRDS_REACHED:      // 164
                   1955:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   1956:                return EAGAIN;
                   1957:        case ERROR_BROKEN_PIPE:            // 109
                   1958:                return EPIPE;
                   1959:        case ERROR_DISK_FULL:              // 112
                   1960:                return ENOSPC;
                   1961:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   1962:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   1963:                return ECHILD;
                   1964:        case ERROR_DIR_NOT_EMPTY:          // 145
                   1965:                return ENOTEMPTY;
                   1966:        }
1.1.1.14  root     1967:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     1968:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   1969:                return EACCES;
                   1970:        }
1.1.1.14  root     1971:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     1972:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   1973:                return ENOEXEC;
                   1974:        }
                   1975:        return EINVAL;
                   1976: }
                   1977: 
                   1978: int msdos_open(const char *filename, int oflag)
                   1979: {
1.1.1.14  root     1980:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     1981:                return _open(filename, oflag);
                   1982:        }
1.1.1.14  root     1983:        
                   1984:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     1985:        DWORD disposition;
1.1.1.14  root     1986:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   1987:        default:
1.1.1.13  root     1988:        case _O_EXCL:
                   1989:                disposition = OPEN_EXISTING;
                   1990:                break;
                   1991:        case _O_CREAT:
                   1992:                disposition = OPEN_ALWAYS;
                   1993:                break;
                   1994:        case _O_CREAT | _O_EXCL:
                   1995:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   1996:                disposition = CREATE_NEW;
                   1997:                break;
                   1998:        case _O_TRUNC:
                   1999:        case _O_TRUNC | _O_EXCL:
                   2000:                disposition = TRUNCATE_EXISTING;
                   2001:                break;
                   2002:        case _O_CREAT | _O_TRUNC:
                   2003:                disposition = CREATE_ALWAYS;
                   2004:                break;
                   2005:        }
1.1.1.14  root     2006:        
1.1.1.13  root     2007:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   2008:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2009:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2010:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2011:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   2012:                // Retry without FILE_WRITE_ATTRIBUTES.
                   2013:                h = CreateFile(filename, GENERIC_READ,
                   2014:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   2015:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     2016:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     2017:                        errno = msdos_maperr(GetLastError());
                   2018:                        return -1;
                   2019:                }
                   2020:        }
1.1.1.14  root     2021:        
1.1.1.13  root     2022:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     2023:        if(fd == -1) {
1.1.1.13  root     2024:                CloseHandle(h);
                   2025:        }
                   2026:        return fd;
                   2027: }
                   2028: 
1.1.1.14  root     2029: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
1.1       root     2030: {
                   2031:        static int id = 0;
                   2032:        char full[MAX_PATH], *name;
                   2033:        
                   2034:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   2035:                strcpy(file_handler[fd].path, full);
                   2036:        } else {
                   2037:                strcpy(file_handler[fd].path, path);
                   2038:        }
1.1.1.14  root     2039:        // isatty makes no distinction between CON & NUL
                   2040:        // GetFileSize fails on CON, succeeds on NUL
                   2041:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   2042:                info = 0x8084;
                   2043:                atty = 0;
                   2044:        } else if(!atty && info == 0x80d3) {
                   2045:                info = msdos_drive_number(".");
                   2046:        }
1.1       root     2047:        file_handler[fd].valid = 1;
                   2048:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
                   2049:        file_handler[fd].atty = atty;
                   2050:        file_handler[fd].mode = mode;
                   2051:        file_handler[fd].info = info;
                   2052:        file_handler[fd].psp = psp_seg;
1.1.1.21  root     2053:        
                   2054:        // init system file table
                   2055:        if(fd < 20) {
                   2056:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   2057:                
                   2058:                memset(sft, 0, 0x3b);
                   2059:                
                   2060:                *(UINT16 *)(sft + 0x00) = 1;
                   2061:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   2062:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   2063:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   2064:                
                   2065:                if(!(file_handler[fd].info & 0x80)) {
                   2066:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   2067:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   2068:                        
                   2069:                        FILETIME time, local;
                   2070:                        HANDLE hHandle;
                   2071:                        WORD dos_date = 0, dos_time = 0;
                   2072:                        DWORD file_size = 0;
                   2073:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   2074:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   2075:                                        FileTimeToLocalFileTime(&time, &local);
                   2076:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   2077:                                }
                   2078:                                file_size = GetFileSize(hHandle, NULL);
                   2079:                        }
                   2080:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   2081:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   2082:                        *(UINT32 *)(sft + 0x11) = file_size;
                   2083:                }
                   2084:                
                   2085:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   2086:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   2087:                my_strupr(fname);
                   2088:                my_strupr(ext);
                   2089:                memset(sft + 0x20, 0x20, 11);
                   2090:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   2091:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   2092:                
                   2093:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   2094:        }
1.1       root     2095: }
                   2096: 
                   2097: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   2098: {
                   2099:        strcpy(file_handler[dst].path, file_handler[src].path);
                   2100:        file_handler[dst].valid = 1;
                   2101:        file_handler[dst].id = file_handler[src].id;
                   2102:        file_handler[dst].atty = file_handler[src].atty;
                   2103:        file_handler[dst].mode = file_handler[src].mode;
                   2104:        file_handler[dst].info = file_handler[src].info;
                   2105:        file_handler[dst].psp = psp_seg;
                   2106: }
                   2107: 
1.1.1.20  root     2108: void msdos_file_handler_close(int fd)
1.1       root     2109: {
                   2110:        file_handler[fd].valid = 0;
1.1.1.21  root     2111:        
                   2112:        if(fd < 20) {
                   2113:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   2114:        }
1.1       root     2115: }
                   2116: 
1.1.1.14  root     2117: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     2118: {
1.1.1.14  root     2119:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   2120:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   2121:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     2122: }
                   2123: 
                   2124: // find file
                   2125: 
                   2126: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   2127: {
                   2128:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2129:                return(0);      // search directory only !!!
                   2130:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   2131:                return(0);
                   2132:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   2133:                return(0);
                   2134:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   2135:                return(0);
                   2136:        } else if((attribute & required_mask) != required_mask) {
                   2137:                return(0);
                   2138:        } else {
                   2139:                return(1);
                   2140:        }
                   2141: }
                   2142: 
1.1.1.13  root     2143: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   2144: {
1.1.1.14  root     2145:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     2146:                return 1;
                   2147:        }
                   2148:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     2149:        if(len > 12) {
1.1.1.13  root     2150:                return 0;
                   2151:        }
                   2152:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     2153:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.13  root     2154:                return 0;
                   2155:        }
                   2156:        return 1;
                   2157: }
                   2158: 
1.1       root     2159: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   2160: {
                   2161:        FILETIME local;
                   2162:        
                   2163:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   2164:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   2165:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   2166:        
                   2167:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   2168:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   2169:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   2170:        
                   2171:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   2172:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   2173:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   2174: }
                   2175: 
                   2176: // i/o
                   2177: 
                   2178: void msdos_stdio_reopen()
                   2179: {
                   2180:        if(!file_handler[0].valid) {
                   2181:                _dup2(DUP_STDIN, 0);
                   2182:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   2183:        }
                   2184:        if(!file_handler[1].valid) {
                   2185:                _dup2(DUP_STDOUT, 1);
                   2186:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   2187:        }
                   2188:        if(!file_handler[2].valid) {
                   2189:                _dup2(DUP_STDERR, 2);
                   2190:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   2191:        }
1.1.1.21  root     2192:        if(!file_handler[3].valid) {
                   2193:                _dup2(DUP_STDAUX, 3);
                   2194:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   2195:        }
                   2196:        if(!file_handler[4].valid) {
                   2197:                _dup2(DUP_STDPRN, 4);
                   2198:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   2199:        }
                   2200:        for(int i = 0; i < 5; i++) {
                   2201:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   2202:                        msdos_psp_set_file_table(i, i, current_psp);
                   2203:                }
                   2204:        }
1.1       root     2205: }
                   2206: 
                   2207: int msdos_kbhit()
                   2208: {
                   2209:        msdos_stdio_reopen();
                   2210:        
1.1.1.20  root     2211:        process_t *process = msdos_process_info_get(current_psp);
                   2212:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2213:        
                   2214:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2215:                // stdin is redirected to file
1.1.1.20  root     2216:                return(eof(fd) == 0);
1.1       root     2217:        }
                   2218:        
                   2219:        // check keyboard status
1.1.1.5   root     2220:        if(key_buf_char->count() != 0 || key_code != 0) {
1.1       root     2221:                return(1);
                   2222:        } else {
                   2223:                return(_kbhit());
                   2224:        }
                   2225: }
                   2226: 
                   2227: int msdos_getch_ex(int echo)
                   2228: {
                   2229:        static char prev = 0;
                   2230:        
                   2231:        msdos_stdio_reopen();
                   2232:        
1.1.1.20  root     2233:        process_t *process = msdos_process_info_get(current_psp);
                   2234:        int fd = msdos_psp_get_file_table(0, current_psp);
                   2235:        
                   2236:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2237:                // stdin is redirected to file
                   2238: retry:
                   2239:                char data;
1.1.1.20  root     2240:                if(_read(fd, &data, 1) == 1) {
1.1       root     2241:                        char tmp = data;
                   2242:                        if(data == 0x0a) {
                   2243:                                if(prev == 0x0d) {
                   2244:                                        goto retry; // CRLF -> skip LF
                   2245:                                } else {
                   2246:                                        data = 0x0d; // LF only -> CR
                   2247:                                }
                   2248:                        }
                   2249:                        prev = tmp;
                   2250:                        return(data);
                   2251:                }
                   2252:                return(EOF);
                   2253:        }
                   2254:        
                   2255:        // input from console
1.1.1.5   root     2256:        int key_char, key_scan;
                   2257:        if(key_code != 0) {
                   2258:                key_char = (key_code >> 0) & 0xff;
                   2259:                key_scan = (key_code >> 8) & 0xff;
                   2260:                key_code >>= 16;
                   2261:        } else {
1.1.1.14  root     2262:                while(key_buf_char->count() == 0 && !m_halted) {
1.1.1.23  root     2263:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   2264:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   2265:                                if(_kbhit()) {
                   2266:                                        key_buf_char->write(_getch());
                   2267:                                        key_buf_scan->write(0);
                   2268:                                } else {
                   2269:                                        Sleep(10);
                   2270:                                }
                   2271:                        } else {
                   2272:                                if(!update_key_buffer()) {
                   2273:                                        Sleep(10);
                   2274:                                }
1.1.1.14  root     2275:                        }
                   2276:                }
                   2277:                if(m_halted) {
                   2278:                        // ctrl-c pressed - insert CR to terminate input loops
                   2279:                        key_char = 0x0d;
                   2280:                        key_scan = 0;
                   2281:                } else {
                   2282:                        key_char = key_buf_char->read();
                   2283:                        key_scan = key_buf_scan->read();
1.1.1.5   root     2284:                }
1.1       root     2285:        }
                   2286:        if(echo && key_char) {
                   2287:                msdos_putch(key_char);
                   2288:        }
                   2289:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   2290: }
                   2291: 
                   2292: inline int msdos_getch()
                   2293: {
                   2294:        return(msdos_getch_ex(0));
                   2295: }
                   2296: 
                   2297: inline int msdos_getche()
                   2298: {
                   2299:        return(msdos_getch_ex(1));
                   2300: }
                   2301: 
                   2302: int msdos_write(int fd, const void *buffer, unsigned int count)
                   2303: {
                   2304:        static int is_cr = 0;
                   2305:        
                   2306:        if(fd == 1 && !file_handler[1].atty) {
                   2307:                // CR+LF -> LF
                   2308:                UINT8 *buf = (UINT8 *)buffer;
                   2309:                for(unsigned int i = 0; i < count; i++) {
                   2310:                        UINT8 data = buf[i];
                   2311:                        if(is_cr) {
                   2312:                                if(data != 0x0a) {
                   2313:                                        UINT8 tmp = 0x0d;
                   2314:                                        _write(1, &tmp, 1);
                   2315:                                }
                   2316:                                _write(1, &data, 1);
                   2317:                                is_cr = 0;
                   2318:                        } else if(data == 0x0d) {
                   2319:                                is_cr = 1;
                   2320:                        } else {
                   2321:                                _write(1, &data, 1);
                   2322:                        }
                   2323:                }
                   2324:                return(count);
                   2325:        }
1.1.1.14  root     2326:        vram_flush();
1.1       root     2327:        return(_write(fd, buffer, count));
                   2328: }
                   2329: 
                   2330: void msdos_putch(UINT8 data)
                   2331: {
                   2332:        static int p = 0;
                   2333:        static int is_kanji = 0;
                   2334:        static int is_esc = 0;
                   2335:        static int stored_x;
                   2336:        static int stored_y;
                   2337:        static WORD stored_a;
1.1.1.20  root     2338:        static char tmp[64], out[64];
1.1       root     2339:        
                   2340:        msdos_stdio_reopen();
                   2341:        
1.1.1.20  root     2342:        process_t *process = msdos_process_info_get(current_psp);
                   2343:        int fd = msdos_psp_get_file_table(1, current_psp);
                   2344:        
                   2345:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     2346:                // stdout is redirected to file
1.1.1.20  root     2347:                msdos_write(fd, &data, 1);
1.1       root     2348:                return;
                   2349:        }
1.1.1.23  root     2350:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2351:        
                   2352:        // output to console
                   2353:        tmp[p++] = data;
                   2354:        
1.1.1.14  root     2355:        vram_flush();
                   2356:        
1.1       root     2357:        if(is_kanji) {
                   2358:                // kanji character
                   2359:                is_kanji = 0;
                   2360:        } else if(is_esc) {
                   2361:                // escape sequense
                   2362:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   2363:                        p = is_esc = 0;
                   2364:                } else if(tmp[1] == '=' && p == 4) {
                   2365:                        COORD co;
                   2366:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     2367:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     2368:                        SetConsoleCursorPosition(hStdout, co);
                   2369:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     2370:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     2371:                        cursor_moved = false;
                   2372:                        p = is_esc = 0;
                   2373:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   2374:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2375:                        COORD co;
                   2376:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2377:                        co.X = csbi.dwCursorPosition.X;
                   2378:                        co.Y = csbi.dwCursorPosition.Y;
                   2379:                        WORD wAttributes = csbi.wAttributes;
                   2380:                        
                   2381:                        if(tmp[1] == 'D') {
                   2382:                                co.Y++;
                   2383:                        } else if(tmp[1] == 'E') {
                   2384:                                co.X = 0;
                   2385:                                co.Y++;
                   2386:                        } else if(tmp[1] == 'M') {
                   2387:                                co.Y--;
                   2388:                        } else if(tmp[1] == '*') {
                   2389:                                SMALL_RECT rect;
1.1.1.14  root     2390:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2391:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2392:                                co.X = 0;
                   2393:                                co.Y = csbi.srWindow.Top;
1.1       root     2394:                        } else if(tmp[1] == '[') {
                   2395:                                int param[256], params = 0;
                   2396:                                memset(param, 0, sizeof(param));
                   2397:                                for(int i = 2; i < p; i++) {
                   2398:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   2399:                                                param[params] *= 10;
                   2400:                                                param[params] += tmp[i] - '0';
                   2401:                                        } else {
                   2402:                                                params++;
                   2403:                                        }
                   2404:                                }
                   2405:                                if(data == 'A') {
1.1.1.14  root     2406:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     2407:                                } else if(data == 'B') {
1.1.1.14  root     2408:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     2409:                                } else if(data == 'C') {
1.1.1.14  root     2410:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     2411:                                } else if(data == 'D') {
1.1.1.14  root     2412:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     2413:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     2414:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   2415:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     2416:                                } else if(data == 'J') {
                   2417:                                        SMALL_RECT rect;
1.1.1.14  root     2418:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2419:                                        if(param[0] == 0) {
                   2420:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2421:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2422:                                                if(co.Y < csbi.srWindow.Bottom) {
                   2423:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2424:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2425:                                                }
                   2426:                                        } else if(param[0] == 1) {
1.1.1.14  root     2427:                                                if(co.Y > csbi.srWindow.Top) {
                   2428:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   2429:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2430:                                                }
                   2431:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2432:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2433:                                        } else if(param[0] == 2) {
1.1.1.14  root     2434:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2435:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2436:                                                co.X = co.Y = 0;
                   2437:                                        }
                   2438:                                } else if(data == 'K') {
                   2439:                                        SMALL_RECT rect;
1.1.1.14  root     2440:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2441:                                        if(param[0] == 0) {
                   2442:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2443:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2444:                                        } else if(param[0] == 1) {
                   2445:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     2446:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2447:                                        } else if(param[0] == 2) {
                   2448:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     2449:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2450:                                        }
                   2451:                                } else if(data == 'L') {
                   2452:                                        SMALL_RECT rect;
1.1.1.14  root     2453:                                        if(params == 0) {
                   2454:                                                param[0] = 1;
1.1       root     2455:                                        }
1.1.1.14  root     2456:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2457:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2458:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2459:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2460:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     2461:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     2462:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2463:                                        co.X = 0;
                   2464:                                } else if(data == 'M') {
                   2465:                                        SMALL_RECT rect;
1.1.1.14  root     2466:                                        if(params == 0) {
                   2467:                                                param[0] = 1;
                   2468:                                        }
                   2469:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   2470:                                                clear_scr_buffer(csbi.wAttributes);
                   2471:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2472:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     2473:                                        } else {
1.1.1.14  root     2474:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2475:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2476:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   2477:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2478:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     2479:                                        }
                   2480:                                        co.X = 0;
                   2481:                                } else if(data == 'h') {
                   2482:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   2483:                                                CONSOLE_CURSOR_INFO cur;
                   2484:                                                GetConsoleCursorInfo(hStdout, &cur);
                   2485:                                                if(cur.bVisible) {
                   2486:                                                        cur.bVisible = FALSE;
1.1.1.14  root     2487: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     2488:                                                }
                   2489:                                        }
                   2490:                                } else if(data == 'l') {
                   2491:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   2492:                                                CONSOLE_CURSOR_INFO cur;
                   2493:                                                GetConsoleCursorInfo(hStdout, &cur);
                   2494:                                                if(!cur.bVisible) {
                   2495:                                                        cur.bVisible = TRUE;
1.1.1.14  root     2496: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     2497:                                                }
                   2498:                                        }
                   2499:                                } else if(data == 'm') {
                   2500:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   2501:                                        int reverse = 0, hidden = 0;
                   2502:                                        for(int i = 0; i < params; i++) {
                   2503:                                                if(param[i] == 1) {
                   2504:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   2505:                                                } else if(param[i] == 4) {
                   2506:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   2507:                                                } else if(param[i] == 7) {
                   2508:                                                        reverse = 1;
                   2509:                                                } else if(param[i] == 8 || param[i] == 16) {
                   2510:                                                        hidden = 1;
                   2511:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   2512:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2513:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   2514:                                                                param[i] -= 16;
                   2515:                                                        } else {
                   2516:                                                                param[i] -= 30;
                   2517:                                                        }
                   2518:                                                        if(param[i] & 1) {
                   2519:                                                                wAttributes |= FOREGROUND_RED;
                   2520:                                                        }
                   2521:                                                        if(param[i] & 2) {
                   2522:                                                                wAttributes |= FOREGROUND_GREEN;
                   2523:                                                        }
                   2524:                                                        if(param[i] & 4) {
                   2525:                                                                wAttributes |= FOREGROUND_BLUE;
                   2526:                                                        }
                   2527:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   2528:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   2529:                                                        if((param[i] - 40) & 1) {
                   2530:                                                                wAttributes |= BACKGROUND_RED;
                   2531:                                                        }
                   2532:                                                        if((param[i] - 40) & 2) {
                   2533:                                                                wAttributes |= BACKGROUND_GREEN;
                   2534:                                                        }
                   2535:                                                        if((param[i] - 40) & 4) {
                   2536:                                                                wAttributes |= BACKGROUND_BLUE;
                   2537:                                                        }
                   2538:                                                }
                   2539:                                        }
                   2540:                                        if(reverse) {
                   2541:                                                wAttributes &= ~0xff;
                   2542:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   2543:                                        }
                   2544:                                        if(hidden) {
                   2545:                                                wAttributes &= ~0x0f;
                   2546:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   2547:                                        }
                   2548:                                } else if(data == 'n') {
                   2549:                                        if(param[0] == 6) {
                   2550:                                                char tmp[16];
                   2551:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   2552:                                                int len = strlen(tmp);
                   2553:                                                for(int i = 0; i < len; i++) {
                   2554:                                                        key_buf_char->write(tmp[i]);
                   2555:                                                        key_buf_scan->write(0x00);
                   2556:                                                }
                   2557:                                        }
                   2558:                                } else if(data == 's') {
                   2559:                                        stored_x = co.X;
                   2560:                                        stored_y = co.Y;
                   2561:                                        stored_a = wAttributes;
                   2562:                                } else if(data == 'u') {
                   2563:                                        co.X = stored_x;
                   2564:                                        co.Y = stored_y;
                   2565:                                        wAttributes = stored_a;
                   2566:                                }
                   2567:                        }
                   2568:                        if(co.X < 0) {
                   2569:                                co.X = 0;
                   2570:                        } else if(co.X >= csbi.dwSize.X) {
                   2571:                                co.X = csbi.dwSize.X - 1;
                   2572:                        }
1.1.1.14  root     2573:                        if(co.Y < csbi.srWindow.Top) {
                   2574:                                co.Y = csbi.srWindow.Top;
                   2575:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   2576:                                co.Y = csbi.srWindow.Bottom;
1.1       root     2577:                        }
                   2578:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   2579:                                SetConsoleCursorPosition(hStdout, co);
                   2580:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     2581:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     2582:                                cursor_moved = false;
                   2583:                        }
                   2584:                        if(wAttributes != csbi.wAttributes) {
                   2585:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   2586:                        }
                   2587:                        p = is_esc = 0;
                   2588:                }
                   2589:                return;
                   2590:        } else {
                   2591:                if(msdos_lead_byte_check(data)) {
                   2592:                        is_kanji = 1;
                   2593:                        return;
                   2594:                } else if(data == 0x1b) {
                   2595:                        is_esc = 1;
                   2596:                        return;
                   2597:                }
                   2598:        }
1.1.1.20  root     2599:        
                   2600:        DWORD q = 0, num;
                   2601:        is_kanji = 0;
                   2602:        for(int i = 0; i < p; i++) {
                   2603:                UINT8 c = tmp[i];
                   2604:                if(is_kanji) {
                   2605:                        is_kanji = 0;
                   2606:                } else if(msdos_lead_byte_check(data)) {
                   2607:                        is_kanji = 1;
                   2608:                } else if(msdos_ctrl_code_check(data)) {
                   2609:                        out[q++] = '^';
                   2610:                        c += 'A' - 1;
                   2611:                }
                   2612:                out[q++] = c;
                   2613:        }
                   2614:        WriteConsole(hStdout, out, q, &num, NULL);
1.1       root     2615:        p = 0;
1.1.1.14  root     2616:        
1.1.1.15  root     2617:        if(!restore_console_on_exit) {
                   2618:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2619:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2620:                scr_top = csbi.srWindow.Top;
                   2621:        }
1.1       root     2622:        cursor_moved = true;
                   2623: }
                   2624: 
                   2625: int msdos_aux_in()
                   2626: {
1.1.1.21  root     2627:        msdos_stdio_reopen();
                   2628:        
1.1.1.20  root     2629:        process_t *process = msdos_process_info_get(current_psp);
                   2630:        int fd = msdos_psp_get_file_table(3, current_psp);
                   2631:        
                   2632:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     2633:                char data = 0;
1.1.1.20  root     2634:                _read(fd, &data, 1);
1.1       root     2635:                return(data);
                   2636:        } else {
                   2637:                return(EOF);
                   2638:        }
                   2639: }
                   2640: 
                   2641: void msdos_aux_out(char data)
                   2642: {
1.1.1.21  root     2643:        msdos_stdio_reopen();
                   2644:        
1.1.1.20  root     2645:        process_t *process = msdos_process_info_get(current_psp);
                   2646:        int fd = msdos_psp_get_file_table(3, current_psp);
                   2647:        
                   2648:        if(fd < process->max_files && file_handler[fd].valid) {
                   2649:                msdos_write(fd, &data, 1);
1.1       root     2650:        }
                   2651: }
                   2652: 
                   2653: void msdos_prn_out(char data)
                   2654: {
1.1.1.21  root     2655:        msdos_stdio_reopen();
                   2656:        
1.1.1.20  root     2657:        process_t *process = msdos_process_info_get(current_psp);
                   2658:        int fd = msdos_psp_get_file_table(4, current_psp);
                   2659:        
                   2660:        if(fd < process->max_files && file_handler[fd].valid) {
                   2661:                msdos_write(fd, &data, 1);
1.1       root     2662:        }
                   2663: }
                   2664: 
                   2665: // memory control
                   2666: 
1.1.1.19  root     2667: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     2668: {
                   2669:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2670:        
                   2671:        mcb->mz = mz;
                   2672:        mcb->psp = psp;
1.1.1.19  root     2673:        mcb->paragraphs32 = paragraphs;
1.1       root     2674:        return(mcb);
                   2675: }
                   2676: 
                   2677: void msdos_mcb_check(mcb_t *mcb)
                   2678: {
                   2679:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
                   2680:                fatalerror("broken mcb\n");
                   2681:        }
                   2682: }
                   2683: 
                   2684: int msdos_mem_split(int seg, int paragraphs)
                   2685: {
                   2686:        int mcb_seg = seg - 1;
                   2687:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2688:        msdos_mcb_check(mcb);
                   2689:        
1.1.1.19  root     2690:        if(mcb->paragraphs() > paragraphs) {
1.1       root     2691:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.19  root     2692:                int new_paragraphs = mcb->paragraphs() - paragraphs - 1;
1.1       root     2693:                
                   2694:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   2695:                mcb->mz = 'M';
1.1.1.19  root     2696:                mcb->paragraphs32 = paragraphs;
1.1       root     2697:                return(0);
                   2698:        }
                   2699:        return(-1);
                   2700: }
                   2701: 
                   2702: void msdos_mem_merge(int seg)
                   2703: {
                   2704:        int mcb_seg = seg - 1;
                   2705:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2706:        msdos_mcb_check(mcb);
                   2707:        
                   2708:        while(1) {
                   2709:                if(mcb->mz == 'Z') {
                   2710:                        break;
                   2711:                }
1.1.1.19  root     2712:                int next_seg = mcb_seg + 1 + mcb->paragraphs();
1.1       root     2713:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   2714:                msdos_mcb_check(next_mcb);
                   2715:                
                   2716:                if(next_mcb->psp != 0) {
                   2717:                        break;
                   2718:                }
                   2719:                mcb->mz = next_mcb->mz;
1.1.1.19  root     2720:                mcb->paragraphs32 = mcb->paragraphs() + 1 + next_mcb->paragraphs();
1.1       root     2721:        }
                   2722: }
                   2723: 
1.1.1.8   root     2724: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     2725: {
                   2726:        while(1) {
                   2727:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2728:                
1.1.1.14  root     2729:                if(mcb->psp == 0) {
                   2730:                        msdos_mem_merge(mcb_seg + 1);
                   2731:                } else {
                   2732:                        msdos_mcb_check(mcb);
                   2733:                }
1.1.1.8   root     2734:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.19  root     2735:                        if(mcb->psp == 0 && mcb->paragraphs() >= paragraphs) {
1.1       root     2736:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   2737:                                mcb->psp = current_psp;
                   2738:                                return(mcb_seg + 1);
                   2739:                        }
                   2740:                }
                   2741:                if(mcb->mz == 'Z') {
                   2742:                        break;
                   2743:                }
1.1.1.19  root     2744:                mcb_seg += 1 + mcb->paragraphs();
1.1       root     2745:        }
                   2746:        return(-1);
                   2747: }
                   2748: 
                   2749: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   2750: {
                   2751:        int mcb_seg = seg - 1;
                   2752:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2753:        msdos_mcb_check(mcb);
1.1.1.19  root     2754:        int current_paragraphs = mcb->paragraphs();
1.1       root     2755:        
                   2756:        msdos_mem_merge(seg);
1.1.1.19  root     2757:        if(paragraphs > mcb->paragraphs()) {
1.1.1.14  root     2758:                if(max_paragraphs) {
1.1.1.19  root     2759:                        *max_paragraphs = mcb->paragraphs();
1.1.1.14  root     2760:                }
1.1       root     2761:                msdos_mem_split(seg, current_paragraphs);
                   2762:                return(-1);
                   2763:        }
                   2764:        msdos_mem_split(seg, paragraphs);
                   2765:        return(0);
                   2766: }
                   2767: 
                   2768: void msdos_mem_free(int seg)
                   2769: {
                   2770:        int mcb_seg = seg - 1;
                   2771:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2772:        msdos_mcb_check(mcb);
                   2773:        
                   2774:        mcb->psp = 0;
                   2775:        msdos_mem_merge(seg);
                   2776: }
                   2777: 
1.1.1.8   root     2778: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     2779: {
                   2780:        int max_paragraphs = 0;
                   2781:        
                   2782:        while(1) {
                   2783:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2784:                msdos_mcb_check(mcb);
                   2785:                
1.1.1.8   root     2786:                if(!(new_process && mcb->mz != 'Z')) {
1.1.1.19  root     2787:                        if(mcb->psp == 0 && mcb->paragraphs() > max_paragraphs) {
                   2788:                                max_paragraphs = mcb->paragraphs();
1.1       root     2789:                        }
                   2790:                }
                   2791:                if(mcb->mz == 'Z') {
                   2792:                        break;
                   2793:                }
1.1.1.19  root     2794:                mcb_seg += 1 + mcb->paragraphs();
1.1       root     2795:        }
1.1.1.14  root     2796:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     2797: }
                   2798: 
1.1.1.8   root     2799: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   2800: {
                   2801:        int last_seg = -1;
                   2802:        
                   2803:        while(1) {
                   2804:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2805:                msdos_mcb_check(mcb);
                   2806:                
1.1.1.14  root     2807:                if(mcb->psp == psp) {
1.1.1.8   root     2808:                        last_seg = mcb_seg;
                   2809:                }
1.1.1.14  root     2810:                if(mcb->mz == 'Z') {
                   2811:                        break;
                   2812:                }
1.1.1.19  root     2813:                mcb_seg += 1 + mcb->paragraphs();
1.1.1.8   root     2814:        }
                   2815:        return(last_seg);
                   2816: }
                   2817: 
1.1.1.19  root     2818: int msdos_mem_get_umb_linked()
                   2819: {
                   2820:        int mcb_seg = first_mcb;
                   2821:        
                   2822:        while(1) {
                   2823:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2824:                msdos_mcb_check(mcb);
                   2825:                
                   2826:                if(mcb->mz == 'Z') {
                   2827:                        if(mcb_seg >= (UMB_TOP >> 4)) {
                   2828:                                return(-1);
                   2829:                        }
                   2830:                        break;
                   2831:                }
                   2832:                mcb_seg += 1 + mcb->paragraphs();
                   2833:        }
                   2834:        return(0);
                   2835: }
                   2836: 
                   2837: int msdos_mem_link_umb()
                   2838: {
                   2839:        int mcb_seg = first_mcb;
                   2840:        
                   2841:        while(1) {
                   2842:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2843:                msdos_mcb_check(mcb);
                   2844:                mcb_seg += 1 + mcb->paragraphs();
                   2845:                
                   2846:                if(mcb->mz == 'Z') {
                   2847:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   2848:                                mcb->mz = 'M';
1.1.1.20  root     2849:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     2850:                                return(-1);
                   2851:                        }
                   2852:                        break;
                   2853:                }
                   2854:        }
                   2855:        return(0);
                   2856: }
                   2857: 
                   2858: int msdos_mem_unlink_umb()
                   2859: {
                   2860:        int mcb_seg = first_mcb;
                   2861:        
                   2862:        while(1) {
                   2863:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   2864:                msdos_mcb_check(mcb);
                   2865:                mcb_seg += 1 + mcb->paragraphs();
                   2866:                
                   2867:                if(mcb->mz == 'Z') {
                   2868:                        break;
                   2869:                } else {
                   2870:                        if(mcb_seg == (MEMORY_END >> 4) - 1) {
                   2871:                                mcb->mz = 'Z';
1.1.1.20  root     2872:                                ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     2873:                                return(-1);
                   2874:                        }
                   2875:                }
                   2876:        }
                   2877:        return(0);
                   2878: }
                   2879: 
1.1       root     2880: // environment
                   2881: 
                   2882: void msdos_env_set_argv(int env_seg, char *argv)
                   2883: {
                   2884:        char *dst = (char *)(mem + (env_seg << 4));
                   2885:        
                   2886:        while(1) {
                   2887:                if(dst[0] == 0) {
                   2888:                        break;
                   2889:                }
                   2890:                dst += strlen(dst) + 1;
                   2891:        }
                   2892:        *dst++ = 0; // end of environment
                   2893:        *dst++ = 1; // top of argv[0]
                   2894:        *dst++ = 0;
                   2895:        memcpy(dst, argv, strlen(argv));
                   2896:        dst += strlen(argv);
                   2897:        *dst++ = 0;
                   2898:        *dst++ = 0;
                   2899: }
                   2900: 
                   2901: char *msdos_env_get_argv(int env_seg)
                   2902: {
                   2903:        static char env[ENV_SIZE];
                   2904:        char *src = env;
                   2905:        
                   2906:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   2907:        while(1) {
                   2908:                if(src[0] == 0) {
                   2909:                        if(src[1] == 1) {
                   2910:                                return(src + 3);
                   2911:                        }
                   2912:                        break;
                   2913:                }
                   2914:                src += strlen(src) + 1;
                   2915:        }
                   2916:        return(NULL);
                   2917: }
                   2918: 
                   2919: char *msdos_env_get(int env_seg, const char *name)
                   2920: {
                   2921:        static char env[ENV_SIZE];
                   2922:        char *src = env;
                   2923:        
                   2924:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   2925:        while(1) {
                   2926:                if(src[0] == 0) {
                   2927:                        break;
                   2928:                }
                   2929:                int len = strlen(src);
                   2930:                char *n = my_strtok(src, "=");
                   2931:                char *v = src + strlen(n) + 1;
                   2932:                
                   2933:                if(_stricmp(name, n) == 0) {
                   2934:                        return(v);
                   2935:                }
                   2936:                src += len + 1;
                   2937:        }
                   2938:        return(NULL);
                   2939: }
                   2940: 
                   2941: void msdos_env_set(int env_seg, char *name, char *value)
                   2942: {
                   2943:        char env[ENV_SIZE];
                   2944:        char *src = env;
                   2945:        char *dst = (char *)(mem + (env_seg << 4));
                   2946:        char *argv = msdos_env_get_argv(env_seg);
                   2947:        int done = 0;
                   2948:        
                   2949:        memcpy(src, dst, ENV_SIZE);
                   2950:        memset(dst, 0, ENV_SIZE);
                   2951:        while(1) {
                   2952:                if(src[0] == 0) {
                   2953:                        break;
                   2954:                }
                   2955:                int len = strlen(src);
                   2956:                char *n = my_strtok(src, "=");
                   2957:                char *v = src + strlen(n) + 1;
                   2958:                char tmp[1024];
                   2959:                
                   2960:                if(_stricmp(name, n) == 0) {
                   2961:                        sprintf(tmp, "%s=%s", n, value);
                   2962:                        done = 1;
                   2963:                } else {
                   2964:                        sprintf(tmp, "%s=%s", n, v);
                   2965:                }
                   2966:                memcpy(dst, tmp, strlen(tmp));
                   2967:                dst += strlen(tmp) + 1;
                   2968:                src += len + 1;
                   2969:        }
                   2970:        if(!done) {
                   2971:                char tmp[1024];
                   2972:                
                   2973:                sprintf(tmp, "%s=%s", name, value);
                   2974:                memcpy(dst, tmp, strlen(tmp));
                   2975:                dst += strlen(tmp) + 1;
                   2976:        }
                   2977:        if(argv) {
                   2978:                *dst++ = 0; // end of environment
                   2979:                *dst++ = 1; // top of argv[0]
                   2980:                *dst++ = 0;
                   2981:                memcpy(dst, argv, strlen(argv));
                   2982:                dst += strlen(argv);
                   2983:                *dst++ = 0;
                   2984:                *dst++ = 0;
                   2985:        }
                   2986: }
                   2987: 
                   2988: // process
                   2989: 
1.1.1.8   root     2990: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     2991: {
                   2992:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   2993:        
                   2994:        memset(psp, 0, PSP_SIZE);
                   2995:        psp->exit[0] = 0xcd;
                   2996:        psp->exit[1] = 0x20;
1.1.1.8   root     2997:        psp->first_mcb = mcb_seg;
1.1       root     2998:        psp->far_call = 0xea;
                   2999:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   3000:        psp->cpm_entry.w.h = 0xf000;
                   3001:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   3002:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   3003:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   3004:        psp->parent_psp = parent_psp;
1.1.1.20  root     3005:        if(parent_psp == (UINT16)-1) {
                   3006:                for(int i = 0; i < 20; i++) {
                   3007:                        if(file_handler[i].valid) {
                   3008:                                psp->file_table[i] = i;
                   3009:                        } else {
                   3010:                                psp->file_table[i] = 0xff;
                   3011:                        }
1.1       root     3012:                }
1.1.1.20  root     3013:        } else {
                   3014:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     3015:        }
                   3016:        psp->env_seg = env_seg;
                   3017:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     3018:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     3019:        psp->file_table_size = 20;
                   3020:        psp->file_table_ptr.w.l = 0x18;
                   3021:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     3022:        psp->service[0] = 0xcd;
                   3023:        psp->service[1] = 0x21;
                   3024:        psp->service[2] = 0xcb;
                   3025:        return(psp);
                   3026: }
                   3027: 
1.1.1.20  root     3028: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   3029: {
                   3030:        if(psp_seg && fd < 20) {
                   3031:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3032:                psp->file_table[fd] = value;
                   3033:        }
                   3034: }
                   3035: 
                   3036: int msdos_psp_get_file_table(int fd, int psp_seg)
                   3037: {
                   3038:        if(psp_seg && fd < 20) {
                   3039:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3040:                fd = psp->file_table[fd];
                   3041:        }
                   3042:        return fd;
                   3043: }
                   3044: 
1.1       root     3045: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   3046: {
                   3047:        // load command file
                   3048:        int fd = -1;
                   3049:        int dos_command = 0;
1.1.1.24  root     3050:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1       root     3051:        
                   3052:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3053:        int opt_len = mem[opt_ofs];
                   3054:        memset(opt, 0, sizeof(opt));
                   3055:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3056:        
1.1.1.14  root     3057:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   3058:                // this is a batch file, run command.com
                   3059:                char tmp[MAX_PATH];
                   3060:                if(opt_len != 0) {
                   3061:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   3062:                } else {
                   3063:                        sprintf(tmp, "/C %s", cmd);
                   3064:                }
                   3065:                strcpy(opt, tmp);
                   3066:                opt_len = strlen(opt);
                   3067:                mem[opt_ofs] = opt_len;
                   3068:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3069:                strcpy(command, comspec_path);
                   3070:                strcpy(name_tmp, "COMMAND.COM");
                   3071:        } else {
                   3072:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   3073:                        // redirect C:\COMMAND.COM to comspec_path
                   3074:                        strcpy(command, comspec_path);
                   3075:                } else {
                   3076:                        strcpy(command, cmd);
                   3077:                }
1.1.1.24  root     3078:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   3079:                        return(-1);
                   3080:                }
1.1.1.14  root     3081:                memset(name_tmp, 0, sizeof(name_tmp));
                   3082:                strcpy(name_tmp, name);
                   3083:                
                   3084:                // check command.com
                   3085:                if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) {
                   3086:                        if(opt_len == 0) {
                   3087: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   3088:                                process_t *current_process = NULL;
                   3089:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   3090:                                        if(process[i].psp == current_psp) {
                   3091:                                                current_process = &process[i];
                   3092:                                                break;
                   3093:                                        }
                   3094:                                }
                   3095:                                if(current_process != NULL) {
                   3096:                                        param->cmd_line.dw = current_process->dta.dw;
                   3097:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   3098:                                        opt_len = mem[opt_ofs];
                   3099:                                        memset(opt, 0, sizeof(opt));
                   3100:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   3101:                                }
                   3102:                        }
                   3103:                        for(int i = 0; i < opt_len; i++) {
                   3104:                                if(opt[i] == ' ') {
                   3105:                                        continue;
                   3106:                                }
                   3107:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   3108:                                        for(int j = i + 3; j < opt_len; j++) {
                   3109:                                                if(opt[j] == ' ') {
                   3110:                                                        continue;
                   3111:                                                }
                   3112:                                                char *token = my_strtok(opt + j, " ");
                   3113:                                                
                   3114:                                                if(strlen(token) >= 5 && _stricmp(&token[strlen(token) - 4], ".BAT") == 0) {
                   3115:                                                        // this is a batch file, okay to run command.com
                   3116:                                                } else {
                   3117:                                                        // run program directly without command.com
                   3118:                                                        strcpy(command, token);
                   3119:                                                        char tmp[MAX_PATH];
                   3120:                                                        strcpy(tmp, token + strlen(token) + 1);
                   3121:                                                        strcpy(opt, tmp);
                   3122:                                                        opt_len = strlen(opt);
                   3123:                                                        mem[opt_ofs] = opt_len;
                   3124:                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3125:                                                        dos_command = 1;
                   3126:                                                }
                   3127:                                                break;
1.1       root     3128:                                        }
                   3129:                                }
1.1.1.14  root     3130:                                break;
1.1       root     3131:                        }
                   3132:                }
                   3133:        }
                   3134:        
                   3135:        // load command file
                   3136:        strcpy(path, command);
                   3137:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3138:                sprintf(path, "%s.COM", command);
                   3139:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   3140:                        sprintf(path, "%s.EXE", command);
                   3141:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     3142:                                sprintf(path, "%s.BAT", command);
                   3143:                                if(_access(path, 0) == 0) {
                   3144:                                        // this is a batch file, run command.com
                   3145:                                        char tmp[MAX_PATH];
                   3146:                                        if(opt_len != 0) {
                   3147:                                                sprintf(tmp, "/C %s %s", path, opt);
                   3148:                                        } else {
                   3149:                                                sprintf(tmp, "/C %s", path);
                   3150:                                        }
                   3151:                                        strcpy(opt, tmp);
                   3152:                                        opt_len = strlen(opt);
                   3153:                                        mem[opt_ofs] = opt_len;
                   3154:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3155:                                        strcpy(path, comspec_path);
                   3156:                                        strcpy(name_tmp, "COMMAND.COM");
                   3157:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3158:                                } else {
                   3159:                                        // search path in parent environments
                   3160:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3161:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   3162:                                        if(env != NULL) {
                   3163:                                                char env_path[4096];
                   3164:                                                strcpy(env_path, env);
                   3165:                                                char *token = my_strtok(env_path, ";");
                   3166:                                                
                   3167:                                                while(token != NULL) {
                   3168:                                                        if(strlen(token) != 0) {
                   3169:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   3170:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3171:                                                                        break;
                   3172:                                                                }
                   3173:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   3174:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3175:                                                                        break;
                   3176:                                                                }
                   3177:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   3178:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   3179:                                                                        break;
                   3180:                                                                }
                   3181:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   3182:                                                                if(_access(path, 0) == 0) {
                   3183:                                                                        // this is a batch file, run command.com
                   3184:                                                                        char tmp[MAX_PATH];
                   3185:                                                                        if(opt_len != 0) {
                   3186:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   3187:                                                                        } else {
                   3188:                                                                                sprintf(tmp, "/C %s", path);
                   3189:                                                                        }
                   3190:                                                                        strcpy(opt, tmp);
                   3191:                                                                        opt_len = strlen(opt);
                   3192:                                                                        mem[opt_ofs] = opt_len;
                   3193:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   3194:                                                                        strcpy(path, comspec_path);
                   3195:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   3196:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   3197:                                                                        break;
                   3198:                                                                }
1.1.1.8   root     3199:                                                        }
1.1.1.14  root     3200:                                                        token = my_strtok(NULL, ";");
1.1       root     3201:                                                }
                   3202:                                        }
                   3203:                                }
                   3204:                        }
                   3205:                }
                   3206:        }
                   3207:        if(fd == -1) {
                   3208:                if(dos_command) {
                   3209:                        // may be dos command
                   3210:                        char tmp[MAX_PATH];
                   3211:                        sprintf(tmp, "%s %s", command, opt);
                   3212:                        system(tmp);
                   3213:                        return(0);
                   3214:                } else {
                   3215:                        return(-1);
                   3216:                }
                   3217:        }
                   3218:        _read(fd, file_buffer, sizeof(file_buffer));
                   3219:        _close(fd);
                   3220:        
                   3221:        // copy environment
                   3222:        int env_seg, psp_seg;
                   3223:        
1.1.1.8   root     3224:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1       root     3225:                return(-1);
                   3226:        }
                   3227:        if(param->env_seg == 0) {
                   3228:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   3229:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   3230:        } else {
                   3231:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   3232:        }
                   3233:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   3234:        
                   3235:        // check exe header
                   3236:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     3237:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     3238:        UINT16 cs, ss, ip, sp;
                   3239:        
                   3240:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   3241:                // memory allocation
                   3242:                int header_size = header->header_size * 16;
                   3243:                int load_size = header->pages * 512 - header_size;
                   3244:                if(header_size + load_size < 512) {
                   3245:                        load_size = 512 - header_size;
                   3246:                }
                   3247:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   3248:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   3249:                        msdos_mem_free(env_seg);
                   3250:                        return(-1);
                   3251:                }
                   3252:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   3253:                if(paragraphs > free_paragraphs) {
                   3254:                        paragraphs = free_paragraphs;
                   3255:                }
1.1.1.8   root     3256:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     3257:                        msdos_mem_free(env_seg);
                   3258:                        return(-1);
                   3259:                }
                   3260:                // relocation
                   3261:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3262:                for(int i = 0; i < header->relocations; i++) {
                   3263:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   3264:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   3265:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   3266:                }
                   3267:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   3268:                // segments
                   3269:                cs = header->init_cs + start_seg;
                   3270:                ss = header->init_ss + start_seg;
                   3271:                ip = header->init_ip;
                   3272:                sp = header->init_sp - 2; // for symdeb
                   3273:        } else {
                   3274:                // memory allocation
                   3275:                paragraphs = free_paragraphs;
1.1.1.8   root     3276:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     3277:                        msdos_mem_free(env_seg);
                   3278:                        return(-1);
                   3279:                }
                   3280:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   3281:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   3282:                // segments
                   3283:                cs = ss = psp_seg;
                   3284:                ip = 0x100;
                   3285:                sp = 0xfffe;
                   3286:        }
                   3287:        
                   3288:        // create psp
1.1.1.3   root     3289: #if defined(HAS_I386)
                   3290:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   3291: #else
                   3292:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_pc - SREG_BASE(CS);
                   3293: #endif
                   3294:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     3295:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   3296:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   3297:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   3298:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   3299:        
                   3300:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   3301:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   3302:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   3303:        
1.1.1.4   root     3304:        for(int i = 0; i < 8; i++) {
                   3305:                if(name_tmp[i] == '.') {
                   3306:                        mcb_psp->prog_name[i] = '\0';
                   3307:                        break;
                   3308:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   3309:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3310:                        i++;
                   3311:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3312:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   3313:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   3314:                } else {
                   3315:                        mcb_psp->prog_name[i] = name_tmp[i];
                   3316:                }
                   3317:        }
                   3318:        
1.1       root     3319:        // process info
                   3320:        process_t *process = msdos_process_info_create(psp_seg);
                   3321:        strcpy(process->module_dir, msdos_short_full_dir(path));
                   3322:        process->dta.w.l = 0x80;
                   3323:        process->dta.w.h = psp_seg;
                   3324:        process->switchar = '/';
                   3325:        process->max_files = 20;
                   3326:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   3327:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     3328:        process->parent_ds = SREG(DS);
1.1       root     3329:        
                   3330:        current_psp = psp_seg;
1.1.1.23  root     3331:        msdos_sda_update(current_psp);
1.1       root     3332:        
                   3333:        if(al == 0x00) {
                   3334:                int_10h_feh_called = int_10h_ffh_called = false;
                   3335:                
                   3336:                // registers and segments
                   3337:                REG16(AX) = REG16(BX) = 0x00;
                   3338:                REG16(CX) = 0xff;
                   3339:                REG16(DX) = psp_seg;
                   3340:                REG16(SI) = ip;
                   3341:                REG16(DI) = sp;
                   3342:                REG16(SP) = sp;
1.1.1.3   root     3343:                SREG(DS) = SREG(ES) = psp_seg;
                   3344:                SREG(SS) = ss;
                   3345:                i386_load_segment_descriptor(DS);
                   3346:                i386_load_segment_descriptor(ES);
                   3347:                i386_load_segment_descriptor(SS);
1.1       root     3348:                
                   3349:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   3350:                i386_jmp_far(cs, ip);
                   3351:        } else if(al == 0x01) {
                   3352:                // copy ss:sp and cs:ip to param block
                   3353:                param->sp = sp;
                   3354:                param->ss = ss;
                   3355:                param->ip = ip;
                   3356:                param->cs = cs;
                   3357:        }
                   3358:        return(0);
                   3359: }
                   3360: 
                   3361: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   3362: {
                   3363:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   3364:        
                   3365:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   3366:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   3367:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   3368:        
1.1.1.3   root     3369:        SREG(SS) = psp->stack.w.h;
                   3370:        i386_load_segment_descriptor(SS);
1.1       root     3371:        REG16(SP) = psp->stack.w.l;
                   3372:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   3373:        
                   3374:        process_t *process = msdos_process_info_get(psp_seg);
                   3375:        int_10h_feh_called = process->parent_int_10h_feh_called;
                   3376:        int_10h_ffh_called = process->parent_int_10h_ffh_called;
1.1.1.14  root     3377:        SREG(DS) = process->parent_ds;
                   3378:        i386_load_segment_descriptor(DS);
1.1       root     3379:        
                   3380:        if(mem_free) {
1.1.1.8   root     3381:                int mcb_seg;
                   3382:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   3383:                        msdos_mem_free(mcb_seg + 1);
                   3384:                }
                   3385:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   3386:                        msdos_mem_free(mcb_seg + 1);
                   3387:                }
1.1       root     3388:                
                   3389:                for(int i = 0; i < MAX_FILES; i++) {
                   3390:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   3391:                                _close(i);
1.1.1.20  root     3392:                                msdos_file_handler_close(i);
                   3393:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     3394:                        }
                   3395:                }
1.1.1.13  root     3396:                msdos_dta_info_free(psp_seg);
1.1       root     3397:        }
1.1.1.14  root     3398:        msdos_stdio_reopen();
1.1       root     3399:        
                   3400:        memset(process, 0, sizeof(process_t));
                   3401:        
                   3402:        current_psp = psp->parent_psp;
                   3403:        retval = ret;
1.1.1.23  root     3404:        msdos_sda_update(current_psp);
1.1       root     3405: }
                   3406: 
                   3407: // drive
                   3408: 
                   3409: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   3410: {
                   3411:        *seg = DPB_TOP >> 4;
                   3412:        *ofs = sizeof(dpb_t) * drive_num;
                   3413:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   3414:        
                   3415:        if(!force_update && dpb->free_clusters != 0) {
                   3416:                return(dpb->bytes_per_sector ? 1 : 0);
                   3417:        }
                   3418:        memset(dpb, 0, sizeof(dpb_t));
                   3419:        
                   3420:        int res = 0;
                   3421:        char dev[64];
                   3422:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   3423:        
1.1.1.17  root     3424:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     3425:        if(hFile != INVALID_HANDLE_VALUE) {
                   3426:                DISK_GEOMETRY geo;
                   3427:                DWORD dwSize;
                   3428:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   3429:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   3430:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   3431:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     3432:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     3433:                        switch(geo.MediaType) {
                   3434:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   3435:                                dpb->media_type = 0xff;
                   3436:                                break;
                   3437:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   3438:                                dpb->media_type = 0xfe;
                   3439:                                break;
                   3440:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   3441:                                dpb->media_type = 0xfd;
                   3442:                                break;
                   3443:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   3444:                                dpb->media_type = 0xfc;
                   3445:                                break;
                   3446:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   3447:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   3448:                                dpb->media_type = 0xf9;
                   3449:                                break;
                   3450:                        case FixedMedia:        // hard disk
                   3451:                        case RemovableMedia:
1.1.1.19  root     3452:                        case Unknown:
1.1       root     3453:                                dpb->media_type = 0xf8;
                   3454:                                break;
                   3455:                        default:
                   3456:                                dpb->media_type = 0xf0;
                   3457:                                break;
                   3458:                        }
                   3459:                        res = 1;
                   3460:                }
                   3461:                dpb->drive_num = drive_num;
                   3462:                dpb->unit_num = drive_num;
                   3463:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   3464:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     3465:                dpb->info_sector = 0xffff;
                   3466:                dpb->backup_boot_sector = 0xffff;
1.1       root     3467:                dpb->free_clusters = 0xffff;
1.1.1.14  root     3468:                dpb->free_search_cluster = 0xffffffff;
1.1       root     3469:                CloseHandle(hFile);
                   3470:        }
                   3471:        return(res);
                   3472: }
                   3473: 
                   3474: // pc bios
                   3475: 
1.1.1.19  root     3476: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   3477: {
                   3478:        static unsigned __int64 start_msec_since_midnight = 0;
                   3479:        static unsigned __int64 start_msec_since_hostboot = 0;
                   3480:        
                   3481:        if(start_msec_since_midnight == 0) {
                   3482:                SYSTEMTIME time;
                   3483:                GetLocalTime(&time);
                   3484:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   3485:                start_msec_since_hostboot = cur_msec;
                   3486:        }
                   3487:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   3488:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   3489:        return (UINT32)tick;
                   3490: }
                   3491: 
                   3492: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   3493: {
                   3494:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   3495:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   3496:        
                   3497:        if(prev_tick > next_tick) {
                   3498:                mem[0x470] = 1;
                   3499:        }
                   3500:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   3501: }
                   3502: 
1.1.1.14  root     3503: inline void pcbios_irq0()
                   3504: {
                   3505:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     3506:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     3507: }
                   3508: 
1.1.1.16  root     3509: int pcbios_get_text_vram_address(int page)
1.1       root     3510: {
                   3511:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     3512:                return TEXT_VRAM_TOP;
1.1       root     3513:        } else {
1.1.1.14  root     3514:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     3515:        }
                   3516: }
                   3517: 
1.1.1.16  root     3518: int pcbios_get_shadow_buffer_address(int page)
1.1       root     3519: {
1.1.1.14  root     3520:        if(!int_10h_feh_called) {
1.1.1.16  root     3521:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     3522:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     3523:                return SHADOW_BUF_TOP;
                   3524:        } else {
1.1.1.14  root     3525:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     3526:        }
                   3527: }
                   3528: 
1.1.1.16  root     3529: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     3530: {
1.1.1.16  root     3531:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     3532: }
                   3533: 
1.1.1.16  root     3534: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     3535: {
1.1.1.14  root     3536:        // clear the existing screen, not just the new one
                   3537:        int clr_height = max(height, scr_height);
                   3538:        
1.1.1.16  root     3539:        if(scr_width != width || scr_height != height) {
                   3540:                change_console_size(width, height);
1.1.1.14  root     3541:        }
                   3542:        mem[0x462] = 0;
                   3543:        *(UINT16 *)(mem + 0x44e) = 0;
                   3544:        
1.1.1.16  root     3545:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   3546:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   3547:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   3548:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     3549:        
1.1.1.23  root     3550:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     3551:        if(clr_screen) {
1.1.1.14  root     3552:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   3553:                        mem[ofs++] = 0x20;
                   3554:                        mem[ofs++] = 0x07;
                   3555:                }
                   3556:                
                   3557:                EnterCriticalSection(&vram_crit_sect);
                   3558:                for(int y = 0; y < clr_height; y++) {
                   3559:                        for(int x = 0; x < scr_width; x++) {
                   3560:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3561:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3562:                        }
                   3563:                }
                   3564:                SMALL_RECT rect;
1.1.1.14  root     3565:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   3566:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3567:                vram_length_char = vram_last_length_char = 0;
                   3568:                vram_length_attr = vram_last_length_attr = 0;
                   3569:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     3570:        }
1.1.1.14  root     3571:        COORD co;
                   3572:        co.X = 0;
                   3573:        co.Y = scr_top;
                   3574:        SetConsoleCursorPosition(hStdout, co);
                   3575:        cursor_moved = true;
                   3576:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     3577: }
                   3578: 
1.1.1.16  root     3579: inline void pcbios_int_10h_00h()
                   3580: {
                   3581:        switch(REG8(AL) & 0x7f) {
                   3582:        case 0x70: // v-text mode
                   3583:        case 0x71: // extended cga v-text mode
                   3584:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   3585:                break;
                   3586:        default:
                   3587:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   3588:                break;
                   3589:        }
                   3590:        if(REG8(AL) & 0x80) {
                   3591:                mem[0x487] |= 0x80;
                   3592:        } else {
                   3593:                mem[0x487] &= ~0x80;
                   3594:        }
                   3595:        mem[0x449] = REG8(AL) & 0x7f;
                   3596: }
                   3597: 
1.1       root     3598: inline void pcbios_int_10h_01h()
                   3599: {
1.1.1.13  root     3600:        mem[0x460] = REG8(CL);
                   3601:        mem[0x461] = REG8(CH);
1.1.1.14  root     3602:        
1.1.1.23  root     3603:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     3604:        CONSOLE_CURSOR_INFO ci;
                   3605:        GetConsoleCursorInfo(hStdout, &ci);
                   3606: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   3607: //     if(ci.bVisible) {
                   3608:                int lines = max(8, REG8(CL) + 1);
                   3609:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   3610: //     }
                   3611:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     3612: }
                   3613: 
                   3614: inline void pcbios_int_10h_02h()
                   3615: {
1.1.1.14  root     3616:        // continuously setting the cursor effectively stops it blinking
                   3617:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     3618:                COORD co;
                   3619:                co.X = REG8(DL);
1.1.1.14  root     3620:                co.Y = REG8(DH) + scr_top;
                   3621:                
                   3622:                // some programs hide the cursor by moving it off screen
                   3623:                static bool hidden = false;
1.1.1.23  root     3624:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     3625:                CONSOLE_CURSOR_INFO ci;
                   3626:                GetConsoleCursorInfo(hStdout, &ci);
                   3627:                
                   3628:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   3629:                        if(ci.bVisible) {
                   3630:                                ci.bVisible = FALSE;
                   3631: //                             SetConsoleCursorInfo(hStdout, &ci);
                   3632:                                hidden = true;
                   3633:                        }
                   3634:                } else if(hidden) {
                   3635:                        if(!ci.bVisible) {
                   3636:                                ci.bVisible = TRUE;
                   3637: //                             SetConsoleCursorInfo(hStdout, &ci);
                   3638:                        }
                   3639:                        hidden = false;
                   3640:                }
1.1       root     3641:        }
1.1.1.14  root     3642:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   3643:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     3644: }
                   3645: 
                   3646: inline void pcbios_int_10h_03h()
                   3647: {
1.1.1.14  root     3648:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   3649:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     3650:        REG8(CL) = mem[0x460];
                   3651:        REG8(CH) = mem[0x461];
                   3652: }
                   3653: 
                   3654: inline void pcbios_int_10h_05h()
                   3655: {
1.1.1.14  root     3656:        if(REG8(AL) >= vram_pages) {
                   3657:                return;
                   3658:        }
                   3659:        if(mem[0x462] != REG8(AL)) {
                   3660:                vram_flush();
                   3661:                
1.1.1.23  root     3662:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3663:                SMALL_RECT rect;
1.1.1.14  root     3664:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   3665:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     3666:                
1.1.1.16  root     3667:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     3668:                        for(int x = 0; x < scr_width; x++) {
                   3669:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   3670:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     3671:                        }
                   3672:                }
1.1.1.16  root     3673:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     3674:                        for(int x = 0; x < scr_width; x++) {
                   3675:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   3676:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     3677:                        }
                   3678:                }
1.1.1.14  root     3679:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     3680:                
                   3681:                COORD co;
1.1.1.14  root     3682:                co.X = mem[0x450 + REG8(AL) * 2];
                   3683:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   3684:                if(co.Y < scr_top + scr_height) {
                   3685:                        SetConsoleCursorPosition(hStdout, co);
                   3686:                }
1.1       root     3687:        }
1.1.1.14  root     3688:        mem[0x462] = REG8(AL);
                   3689:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   3690:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     3691:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     3692:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     3693:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     3694:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     3695: }
                   3696: 
                   3697: inline void pcbios_int_10h_06h()
                   3698: {
1.1.1.14  root     3699:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   3700:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   3701:                return;
                   3702:        }
                   3703:        vram_flush();
                   3704:        
1.1.1.23  root     3705:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3706:        SMALL_RECT rect;
1.1.1.14  root     3707:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   3708:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3709:        
                   3710:        int right = min(REG8(DL), scr_width - 1);
                   3711:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     3712:        
                   3713:        if(REG8(AL) == 0) {
1.1.1.14  root     3714:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     3715:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     3716:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3717:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     3718:                        }
                   3719:                }
                   3720:        } else {
1.1.1.14  root     3721:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     3722:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     3723:                                if(y2 <= bottom) {
                   3724:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     3725:                                } else {
1.1.1.14  root     3726:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3727:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     3728:                                }
1.1.1.14  root     3729:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   3730:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     3731:                        }
                   3732:                }
                   3733:        }
1.1.1.14  root     3734:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     3735: }
                   3736: 
                   3737: inline void pcbios_int_10h_07h()
                   3738: {
1.1.1.14  root     3739:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   3740:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   3741:                return;
                   3742:        }
                   3743:        vram_flush();
                   3744:        
1.1.1.23  root     3745:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3746:        SMALL_RECT rect;
1.1.1.14  root     3747:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   3748:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3749:        
                   3750:        int right = min(REG8(DL), scr_width - 1);
                   3751:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     3752:        
                   3753:        if(REG8(AL) == 0) {
1.1.1.14  root     3754:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     3755:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     3756:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3757:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     3758:                        }
                   3759:                }
                   3760:        } else {
1.1.1.14  root     3761:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     3762:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     3763:                                if(y2 >= REG8(CH)) {
                   3764:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     3765:                                } else {
1.1.1.14  root     3766:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3767:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     3768:                                }
1.1.1.14  root     3769:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   3770:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     3771:                        }
                   3772:                }
                   3773:        }
1.1.1.14  root     3774:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     3775: }
                   3776: 
                   3777: inline void pcbios_int_10h_08h()
                   3778: {
                   3779:        COORD co;
                   3780:        DWORD num;
                   3781:        
1.1.1.14  root     3782:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   3783:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     3784:        
                   3785:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     3786:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     3787:                co.Y += scr_top;
                   3788:                vram_flush();
1.1       root     3789:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   3790:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   3791:                REG8(AL) = scr_char[0];
                   3792:                REG8(AH) = scr_attr[0];
                   3793:        } else {
1.1.1.16  root     3794:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     3795:        }
                   3796: }
                   3797: 
                   3798: inline void pcbios_int_10h_09h()
                   3799: {
                   3800:        COORD co;
                   3801:        
1.1.1.14  root     3802:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   3803:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   3804:        
1.1.1.16  root     3805:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   3806:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     3807:        
                   3808:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     3809:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     3810:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     3811:                while(dest < end) {
                   3812:                        write_text_vram_char(dest - vram, REG8(AL));
                   3813:                        mem[dest++] = REG8(AL);
                   3814:                        write_text_vram_attr(dest - vram, REG8(BL));
                   3815:                        mem[dest++] = REG8(BL);
1.1       root     3816:                }
1.1.1.14  root     3817:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     3818:        } else {
1.1.1.14  root     3819:                while(dest < end) {
1.1       root     3820:                        mem[dest++] = REG8(AL);
                   3821:                        mem[dest++] = REG8(BL);
                   3822:                }
                   3823:        }
                   3824: }
                   3825: 
                   3826: inline void pcbios_int_10h_0ah()
                   3827: {
                   3828:        COORD co;
                   3829:        
1.1.1.14  root     3830:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   3831:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   3832:        
1.1.1.16  root     3833:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   3834:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     3835:        
                   3836:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     3837:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     3838:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     3839:                while(dest < end) {
                   3840:                        write_text_vram_char(dest - vram, REG8(AL));
                   3841:                        mem[dest++] = REG8(AL);
                   3842:                        dest++;
1.1       root     3843:                }
1.1.1.14  root     3844:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     3845:        } else {
1.1.1.14  root     3846:                while(dest < end) {
1.1       root     3847:                        mem[dest++] = REG8(AL);
                   3848:                        dest++;
                   3849:                }
                   3850:        }
                   3851: }
                   3852: 
                   3853: inline void pcbios_int_10h_0eh()
                   3854: {
1.1.1.14  root     3855:        DWORD num;
                   3856:        COORD co;
                   3857:        
                   3858:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   3859:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   3860:        
                   3861:        if(REG8(AL) == 7) {
                   3862:                //MessageBeep(-1);
                   3863:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   3864:                if(REG8(AL) == 10) {
                   3865:                        vram_flush();
                   3866:                }
1.1.1.23  root     3867:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     3868:                cursor_moved = true;
                   3869:        } else {
1.1.1.16  root     3870:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     3871:                if(mem[0x462] == REG8(BH)) {
                   3872:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     3873:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     3874:                        write_text_vram_char(dest - vram, REG8(AL));
                   3875:                        LeaveCriticalSection(&vram_crit_sect);
                   3876:                        
1.1.1.23  root     3877:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     3878:                        if(++co.X == scr_width) {
                   3879:                                co.X = 0;
                   3880:                                if(++co.Y == scr_height) {
                   3881:                                        vram_flush();
                   3882:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   3883:                                        cursor_moved = true;
                   3884:                                }
                   3885:                        }
                   3886:                        if(!cursor_moved) {
                   3887:                                co.Y += scr_top;
                   3888:                                SetConsoleCursorPosition(hStdout, co);
                   3889:                                cursor_moved = true;
                   3890:                        }
                   3891:                }
                   3892:                mem[dest] = REG8(AL);
                   3893:        }
1.1       root     3894: }
                   3895: 
                   3896: inline void pcbios_int_10h_0fh()
                   3897: {
                   3898:        REG8(AL) = mem[0x449];
                   3899:        REG8(AH) = mem[0x44a];
                   3900:        REG8(BH) = mem[0x462];
                   3901: }
                   3902: 
1.1.1.14  root     3903: inline void pcbios_int_10h_11h()
                   3904: {
                   3905:        switch(REG8(AL)) {
1.1.1.16  root     3906:        case 0x01:
1.1.1.14  root     3907:        case 0x11:
1.1.1.16  root     3908:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     3909:                break;
1.1.1.16  root     3910:        case 0x02:
1.1.1.14  root     3911:        case 0x12:
1.1.1.16  root     3912:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     3913:                break;
1.1.1.16  root     3914:        case 0x04:
1.1.1.14  root     3915:        case 0x14:
1.1.1.16  root     3916:                pcbios_set_console_size(80, 25, true);
                   3917:                break;
                   3918:        case 0x18:
                   3919:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     3920:                break;
                   3921:        case 0x30:
                   3922:                SREG(ES) = 0;
                   3923:                i386_load_segment_descriptor(ES);
                   3924:                REG16(BP) = 0;
                   3925:                REG16(CX) = mem[0x485];
                   3926:                REG8(DL) = mem[0x484];
                   3927:                break;
                   3928:        }
                   3929: }
                   3930: 
                   3931: inline void pcbios_int_10h_12h()
                   3932: {
1.1.1.16  root     3933:        switch(REG8(BL)) {
                   3934:        case 0x10:
1.1.1.14  root     3935:                REG16(BX) = 0x0003;
                   3936:                REG16(CX) = 0x0009;
1.1.1.16  root     3937:                break;
1.1.1.14  root     3938:        }
                   3939: }
                   3940: 
1.1       root     3941: inline void pcbios_int_10h_13h()
                   3942: {
1.1.1.3   root     3943:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     3944:        COORD co;
                   3945:        DWORD num;
                   3946:        
                   3947:        co.X = REG8(DL);
1.1.1.14  root     3948:        co.Y = REG8(DH) + scr_top;
                   3949:        
                   3950:        vram_flush();
1.1       root     3951:        
                   3952:        switch(REG8(AL)) {
                   3953:        case 0x00:
                   3954:        case 0x01:
                   3955:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     3956:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3957:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3958:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   3959:                        SetConsoleCursorPosition(hStdout, co);
                   3960:                        
                   3961:                        if(csbi.wAttributes != REG8(BL)) {
                   3962:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   3963:                        }
1.1.1.14  root     3964:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   3965:                        
1.1       root     3966:                        if(csbi.wAttributes != REG8(BL)) {
                   3967:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3968:                        }
                   3969:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     3970:                                if(!restore_console_on_exit) {
                   3971:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   3972:                                        scr_top = csbi.srWindow.Top;
                   3973:                                }
1.1.1.14  root     3974:                                co.X = mem[0x450 + REG8(BH) * 2];
                   3975:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     3976:                                SetConsoleCursorPosition(hStdout, co);
                   3977:                        } else {
                   3978:                                cursor_moved = true;
                   3979:                        }
                   3980:                } else {
1.1.1.3   root     3981:                        m_CF = 1;
1.1       root     3982:                }
                   3983:                break;
                   3984:        case 0x02:
                   3985:        case 0x03:
                   3986:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     3987:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3988:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3989:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   3990:                        SetConsoleCursorPosition(hStdout, co);
                   3991:                        
                   3992:                        WORD wAttributes = csbi.wAttributes;
                   3993:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   3994:                                if(wAttributes != mem[ofs + 1]) {
                   3995:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   3996:                                        wAttributes = mem[ofs + 1];
                   3997:                                }
1.1.1.14  root     3998:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     3999:                        }
                   4000:                        if(csbi.wAttributes != wAttributes) {
                   4001:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   4002:                        }
                   4003:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     4004:                                co.X = mem[0x450 + REG8(BH) * 2];
                   4005:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     4006:                                SetConsoleCursorPosition(hStdout, co);
                   4007:                        } else {
                   4008:                                cursor_moved = true;
                   4009:                        }
                   4010:                } else {
1.1.1.3   root     4011:                        m_CF = 1;
1.1       root     4012:                }
                   4013:                break;
                   4014:        case 0x10:
                   4015:        case 0x11:
                   4016:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4017:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4018:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   4019:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   4020:                        for(int i = 0; i < num; i++) {
                   4021:                                mem[ofs++] = scr_char[i];
                   4022:                                mem[ofs++] = scr_attr[i];
                   4023:                                if(REG8(AL) == 0x11) {
                   4024:                                        mem[ofs++] = 0;
                   4025:                                        mem[ofs++] = 0;
                   4026:                                }
                   4027:                        }
                   4028:                } else {
1.1.1.16  root     4029:                        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     4030:                                mem[ofs++] = mem[src++];
                   4031:                                mem[ofs++] = mem[src++];
                   4032:                                if(REG8(AL) == 0x11) {
                   4033:                                        mem[ofs++] = 0;
                   4034:                                        mem[ofs++] = 0;
                   4035:                                }
1.1.1.14  root     4036:                                if(++co.X == scr_width) {
                   4037:                                        if(++co.Y == scr_height) {
1.1       root     4038:                                                break;
                   4039:                                        }
                   4040:                                        co.X = 0;
                   4041:                                }
                   4042:                        }
                   4043:                }
                   4044:                break;
                   4045:        case 0x20:
                   4046:        case 0x21:
                   4047:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     4048:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     4049:                        int len = min(REG16(CX), scr_width * scr_height);
                   4050:                        for(int i = 0; i < len; i++) {
1.1       root     4051:                                scr_char[i] = mem[ofs++];
                   4052:                                scr_attr[i] = mem[ofs++];
                   4053:                                if(REG8(AL) == 0x21) {
                   4054:                                        ofs += 2;
                   4055:                                }
                   4056:                        }
1.1.1.14  root     4057:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4058:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4059:                } else {
1.1.1.16  root     4060:                        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     4061:                                mem[dest++] = mem[ofs++];
                   4062:                                mem[dest++] = mem[ofs++];
                   4063:                                if(REG8(AL) == 0x21) {
                   4064:                                        ofs += 2;
                   4065:                                }
1.1.1.14  root     4066:                                if(++co.X == scr_width) {
                   4067:                                        if(++co.Y == scr_height) {
1.1       root     4068:                                                break;
                   4069:                                        }
                   4070:                                        co.X = 0;
                   4071:                                }
                   4072:                        }
                   4073:                }
                   4074:                break;
                   4075:        default:
1.1.1.22  root     4076:                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     4077:                m_CF = 1;
1.1       root     4078:                break;
                   4079:        }
                   4080: }
                   4081: 
1.1.1.14  root     4082: inline void pcbios_int_10h_1ah()
                   4083: {
                   4084:        switch(REG8(AL)) {
                   4085:        case 0x00:
                   4086:                REG8(AL) = 0x1a;
                   4087:                REG8(BL) = 0x08;
                   4088:                REG8(BH) = 0x00;
                   4089:                break;
                   4090:        default:
1.1.1.22  root     4091:                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     4092:                m_CF = 1;
                   4093:                break;
                   4094:        }
                   4095: }
                   4096: 
1.1       root     4097: inline void pcbios_int_10h_1dh()
                   4098: {
                   4099:        switch(REG8(AL)) {
                   4100:        case 0x01:
                   4101:                break;
                   4102:        case 0x02:
                   4103:                REG16(BX) = 0;
                   4104:                break;
                   4105:        default:
1.1.1.22  root     4106:                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));
                   4107:                m_CF = 1;
                   4108:                break;
                   4109:        }
                   4110: }
                   4111: 
                   4112: inline void pcbios_int_10h_4fh()
                   4113: {
                   4114:        switch(REG8(AL)) {
                   4115:        case 0x00:
                   4116:                REG8(AH) = 0x02; // not supported
                   4117:                break;
                   4118:        case 0x01:
                   4119:        case 0x02:
                   4120:        case 0x03:
                   4121:        case 0x04:
                   4122:        case 0x05:
                   4123:        case 0x06:
                   4124:        case 0x07:
                   4125:        case 0x08:
                   4126:        case 0x09:
                   4127:        case 0x0a:
                   4128:        case 0x0b:
                   4129:        case 0x0c:
                   4130:                REG8(AH) = 0x01; // failed
                   4131:                break;
                   4132:        default:
                   4133:                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     4134:                m_CF = 1;
1.1       root     4135:                break;
                   4136:        }
                   4137: }
                   4138: 
                   4139: inline void pcbios_int_10h_82h()
                   4140: {
                   4141:        static UINT8 mode = 0;
                   4142:        
                   4143:        switch(REG8(AL)) {
1.1.1.22  root     4144:        case 0x00:
1.1       root     4145:                if(REG8(BL) != 0xff) {
                   4146:                        mode = REG8(BL);
                   4147:                }
                   4148:                REG8(AL) = mode;
                   4149:                break;
                   4150:        default:
1.1.1.22  root     4151:                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     4152:                m_CF = 1;
1.1       root     4153:                break;
                   4154:        }
                   4155: }
                   4156: 
1.1.1.22  root     4157: inline void pcbios_int_10h_83h()
                   4158: {
                   4159:        static UINT8 mode = 0;
                   4160:        
                   4161:        switch(REG8(AL)) {
                   4162:        case 0x00:
                   4163:                REG16(AX) = 0; // offset???
                   4164:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   4165:                i386_load_segment_descriptor(ES);
                   4166:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   4167:                break;
                   4168:        default:
                   4169:                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));
                   4170:                m_CF = 1;
                   4171:                break;
                   4172:        }
                   4173: }
                   4174: 
                   4175: inline void pcbios_int_10h_90h()
                   4176: {
                   4177:        REG8(AL) = mem[0x449];
                   4178: }
                   4179: 
                   4180: inline void pcbios_int_10h_91h()
                   4181: {
                   4182:        REG8(AL) = 0x04; // VGA
                   4183: }
                   4184: 
                   4185: inline void pcbios_int_10h_efh()
                   4186: {
                   4187:        REG16(DX) = 0xffff;
                   4188: }
                   4189: 
1.1       root     4190: inline void pcbios_int_10h_feh()
                   4191: {
                   4192:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     4193:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     4194:                i386_load_segment_descriptor(ES);
1.1.1.8   root     4195:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     4196:        }
                   4197:        int_10h_feh_called = true;
                   4198: }
                   4199: 
                   4200: inline void pcbios_int_10h_ffh()
                   4201: {
                   4202:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     4203:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4204:                COORD co;
                   4205:                DWORD num;
                   4206:                
1.1.1.14  root     4207:                vram_flush();
                   4208:                
                   4209:                co.X = (REG16(DI) >> 1) % scr_width;
                   4210:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     4211:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   4212:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     4213:                int len;
                   4214:                for(len = 0; ofs < end; len++) {
                   4215:                        scr_char[len] = mem[ofs++];
                   4216:                        scr_attr[len] = mem[ofs++];
                   4217:                }
                   4218:                co.Y += scr_top;
                   4219:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   4220:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     4221:        }
                   4222:        int_10h_ffh_called = true;
                   4223: }
                   4224: 
1.1.1.25! root     4225: inline void pcbios_int_14h_00h()
        !          4226: {
        !          4227:        if(REG16(DX) < 2) {
        !          4228:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
        !          4229:                UINT8 selector = sio_read(REG16(DX), 3);
        !          4230:                selector &= ~0x3f;
        !          4231:                selector |= REG8(AL) & 0x1f;
        !          4232:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
        !          4233:                sio_write(REG16(DX), 3, selector | 0x80);
        !          4234:                sio_write(REG16(DX), 0, divisor & 0xff);
        !          4235:                sio_write(REG16(DX), 1, divisor >> 8);
        !          4236:                sio_write(REG16(DX), 3, selector);
        !          4237:                REG8(AH) = sio_read(REG16(DX), 5);
        !          4238:                REG8(AL) = sio_read(REG16(DX), 6);
        !          4239:        } else {
        !          4240:                REG8(AH) = 0x80;
        !          4241:        }
        !          4242: }
        !          4243: 
        !          4244: inline void pcbios_int_14h_01h()
        !          4245: {
        !          4246:        if(REG16(DX) < 2) {
        !          4247:                UINT8 selector = sio_read(REG16(DX), 3);
        !          4248:                sio_write(REG16(DX), 3, selector & ~0x80);
        !          4249:                sio_write(REG16(DX), 0, REG8(AL));
        !          4250:                sio_write(REG16(DX), 3, selector);
        !          4251:                REG8(AH) = sio_read(REG16(DX), 5);
        !          4252:        } else {
        !          4253:                REG8(AH) = 0x80;
        !          4254:        }
        !          4255: }
        !          4256: 
        !          4257: inline void pcbios_int_14h_02h()
        !          4258: {
        !          4259:        if(REG16(DX) < 2) {
        !          4260:                UINT8 selector = sio_read(REG16(DX), 3);
        !          4261:                sio_write(REG16(DX), 3, selector & ~0x80);
        !          4262:                REG8(AL) = sio_read(REG16(DX), 0);
        !          4263:                sio_write(REG16(DX), 3, selector);
        !          4264:                REG8(AH) = sio_read(REG16(DX), 5);
        !          4265:        } else {
        !          4266:                REG8(AH) = 0x80;
        !          4267:        }
        !          4268: }
        !          4269: 
        !          4270: inline void pcbios_int_14h_03h()
        !          4271: {
        !          4272:        if(REG16(DX) < 2) {
        !          4273:                REG8(AH) = sio_read(REG16(DX), 5);
        !          4274:                REG8(AL) = sio_read(REG16(DX), 6);
        !          4275:        } else {
        !          4276:                REG8(AH) = 0x80;
        !          4277:        }
        !          4278: }
        !          4279: 
        !          4280: inline void pcbios_int_14h_04h()
        !          4281: {
        !          4282:        if(REG16(DX) < 2) {
        !          4283:                UINT8 selector = sio_read(REG16(DX), 3);
        !          4284:                if(REG8(CH) <= 0x03) {
        !          4285:                        selector = (selector & ~0x03) | REG8(CH);
        !          4286:                }
        !          4287:                if(REG8(BL) == 0x00) {
        !          4288:                        selector &= ~0x04;
        !          4289:                } else if(REG8(BL) == 0x01) {
        !          4290:                        selector |= 0x04;
        !          4291:                }
        !          4292:                if(REG8(BH) == 0x00) {
        !          4293:                        selector = (selector & ~0x38) | 0x00;
        !          4294:                } else if(REG8(BH) == 0x01) {
        !          4295:                        selector = (selector & ~0x38) | 0x08;
        !          4296:                } else if(REG8(BH) == 0x02) {
        !          4297:                        selector = (selector & ~0x38) | 0x18;
        !          4298:                } else if(REG8(BH) == 0x03) {
        !          4299:                        selector = (selector & ~0x38) | 0x28;
        !          4300:                } else if(REG8(BH) == 0x04) {
        !          4301:                        selector = (selector & ~0x38) | 0x38;
        !          4302:                }
        !          4303:                if(REG8(AL) == 0x00) {
        !          4304:                        selector |= 0x40;
        !          4305:                } else if(REG8(AL) == 0x01) {
        !          4306:                        selector &= ~0x40;
        !          4307:                }
        !          4308:                if(REG8(CL) <= 0x0b) {
        !          4309:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
        !          4310:                        UINT16 divisor = 115200 / rate[REG8(CL)];
        !          4311:                        sio_write(REG16(DX), 3, selector | 0x80);
        !          4312:                        sio_write(REG16(DX), 0, divisor & 0xff);
        !          4313:                        sio_write(REG16(DX), 1, divisor >> 8);
        !          4314:                }
        !          4315:                sio_write(REG16(DX), 3, selector);
        !          4316:                REG8(AH) = sio_read(REG16(DX), 5);
        !          4317:                REG8(AL) = sio_read(REG16(DX), 6);
        !          4318:        } else {
        !          4319:                REG8(AH) = 0x80;
        !          4320:        }
        !          4321: }
        !          4322: 
        !          4323: inline void pcbios_int_14h_05h()
        !          4324: {
        !          4325:        if(REG16(DX) < 2) {
        !          4326:                if(REG8(AL) == 0x00) {
        !          4327:                        REG8(BL) = sio_read(REG16(DX), 4);
        !          4328:                        REG8(AH) = sio_read(REG16(DX), 5);
        !          4329:                        REG8(AL) = sio_read(REG16(DX), 6);
        !          4330:                } else if(REG8(AL) == 0x01) {
        !          4331:                        sio_write(REG16(DX), 4, REG8(BL));
        !          4332:                        REG8(AH) = sio_read(REG16(DX), 5);
        !          4333:                        REG8(AL) = sio_read(REG16(DX), 6);
        !          4334:                } else {
        !          4335:                        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));
        !          4336:                }
        !          4337:        } else {
        !          4338:                REG8(AH) = 0x80;
        !          4339:        }
        !          4340: }
        !          4341: 
1.1.1.14  root     4342: inline void pcbios_int_15h_10h()
                   4343: {
1.1.1.22  root     4344:        switch(REG8(AL)) {
                   4345:        case 0x00:
1.1.1.14  root     4346:                Sleep(10);
                   4347:                hardware_update();
1.1.1.22  root     4348:                break;
                   4349:        default:
                   4350:                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     4351:                REG8(AH) = 0x86;
                   4352:                m_CF = 1;
                   4353:        }
                   4354: }
                   4355: 
1.1       root     4356: inline void pcbios_int_15h_23h()
                   4357: {
                   4358:        switch(REG8(AL)) {
1.1.1.22  root     4359:        case 0x00:
1.1.1.8   root     4360:                REG8(CL) = cmos_read(0x2d);
                   4361:                REG8(CH) = cmos_read(0x2e);
1.1       root     4362:                break;
1.1.1.22  root     4363:        case 0x01:
1.1.1.8   root     4364:                cmos_write(0x2d, REG8(CL));
                   4365:                cmos_write(0x2e, REG8(CH));
1.1       root     4366:                break;
                   4367:        default:
1.1.1.22  root     4368:                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     4369:                REG8(AH) = 0x86;
1.1.1.3   root     4370:                m_CF = 1;
1.1       root     4371:                break;
                   4372:        }
                   4373: }
                   4374: 
                   4375: inline void pcbios_int_15h_24h()
                   4376: {
                   4377:        switch(REG8(AL)) {
1.1.1.22  root     4378:        case 0x00:
1.1.1.3   root     4379:                i386_set_a20_line(0);
1.1       root     4380:                REG8(AH) = 0;
                   4381:                break;
1.1.1.22  root     4382:        case 0x01:
1.1.1.3   root     4383:                i386_set_a20_line(1);
1.1       root     4384:                REG8(AH) = 0;
                   4385:                break;
1.1.1.22  root     4386:        case 0x02:
1.1       root     4387:                REG8(AH) = 0;
1.1.1.3   root     4388:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     4389:                REG16(CX) = 0;
                   4390:                break;
1.1.1.22  root     4391:        case 0x03:
1.1       root     4392:                REG16(AX) = 0;
                   4393:                REG16(BX) = 0;
                   4394:                break;
1.1.1.22  root     4395:        default:
                   4396:                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));
                   4397:                REG8(AH) = 0x86;
                   4398:                m_CF = 1;
                   4399:                break;
1.1       root     4400:        }
                   4401: }
                   4402: 
                   4403: inline void pcbios_int_15h_49h()
                   4404: {
1.1.1.14  root     4405:        REG8(AH) = 0;
                   4406:        REG8(BL) = 0;   // DOS/V
1.1       root     4407: }
                   4408: 
1.1.1.22  root     4409: inline void pcbios_int_15h_50h()
                   4410: {
                   4411:        switch(REG8(AL)) {
                   4412:        case 0x00:
                   4413:        case 0x01:
                   4414:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   4415:                        REG8(AH) = 0x01; // invalid font type in bh
                   4416:                        m_CF = 1;
                   4417:                } else if(REG8(BL) != 0) {
                   4418:                        REG8(AH) = 0x02; // bl not zero
                   4419:                        m_CF = 1;
                   4420:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   4421:                        REG8(AH) = 0x04; // invalid code page
                   4422:                        m_CF = 1;
                   4423:                } else {
                   4424:                        REG8(AH) = 0x86; // finally, this function is not supported
                   4425:                        m_CF = 1;
                   4426:                }
                   4427:                break;
                   4428:        default:
                   4429:                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));
                   4430:                REG8(AH) = 0x86;
                   4431:                m_CF = 1;
                   4432:                break;
                   4433:        }
                   4434: }
                   4435: 
1.1       root     4436: inline void pcbios_int_15h_86h()
                   4437: {
                   4438:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     4439:        UINT32 msec = usec / 1000;
                   4440:        
                   4441:        while(msec) {
                   4442:                UINT32 tmp = min(msec, 100);
                   4443:                if(msec - tmp < 10) {
                   4444:                        tmp = msec;
                   4445:                }
                   4446:                Sleep(tmp);
                   4447:                
                   4448:                if(m_halted) {
                   4449:                        return;
                   4450:                }
                   4451:                msec -= tmp;
                   4452:        }
1.1       root     4453: }
                   4454: 
                   4455: inline void pcbios_int_15h_87h()
                   4456: {
                   4457:        // copy extended memory (from DOSBox)
                   4458:        int len = REG16(CX) * 2;
1.1.1.3   root     4459:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     4460:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   4461:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   4462:        memcpy(mem + dst, mem + src, len);
                   4463:        REG16(AX) = 0x00;
                   4464: }
                   4465: 
                   4466: inline void pcbios_int_15h_88h()
                   4467: {
1.1.1.17  root     4468:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     4469: }
                   4470: 
                   4471: inline void pcbios_int_15h_89h()
                   4472: {
1.1.1.21  root     4473: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     4474:        // switch to protected mode (from DOSBox)
                   4475:        write_io_byte(0x20, 0x10);
                   4476:        write_io_byte(0x21, REG8(BH));
                   4477:        write_io_byte(0x21, 0x00);
                   4478:        write_io_byte(0xa0, 0x10);
                   4479:        write_io_byte(0xa1, REG8(BL));
                   4480:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     4481:        i386_set_a20_line(1);
                   4482:        int ofs = SREG_BASE(ES) + REG16(SI);
                   4483:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   4484:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   4485:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   4486:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   4487: #if defined(HAS_I386)
                   4488:        m_cr[0] |= 1;
                   4489: #else
                   4490:        m_msw |= 1;
                   4491: #endif
                   4492:        SREG(DS) = 0x18;
                   4493:        SREG(ES) = 0x20;
                   4494:        SREG(SS) = 0x28;
                   4495:        i386_load_segment_descriptor(DS);
                   4496:        i386_load_segment_descriptor(ES);
                   4497:        i386_load_segment_descriptor(SS);
1.1.1.21  root     4498:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     4499:        REG16(SP) += 6;
1.1.1.3   root     4500: #if defined(HAS_I386)
1.1.1.21  root     4501:        UINT32 flags = get_flags();
                   4502:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   4503:        set_flags(flags);
1.1.1.3   root     4504: #else
1.1.1.21  root     4505:        UINT32 flags = CompressFlags();
                   4506:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   4507:        ExpandFlags(flags);
1.1.1.3   root     4508: #endif
1.1       root     4509:        REG16(AX) = 0x00;
1.1.1.21  root     4510:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     4511: #else
1.1.1.21  root     4512:        // i86/i186/v30: protected mode is not supported
1.1       root     4513:        REG8(AH) = 0x86;
1.1.1.3   root     4514:        m_CF = 1;
1.1       root     4515: #endif
                   4516: }
                   4517: 
1.1.1.21  root     4518: inline void pcbios_int_15h_8ah()
                   4519: {
                   4520:        UINT32 size = MAX_MEM - 0x100000;
                   4521:        REG16(AX) = size & 0xffff;
                   4522:        REG16(DX) = size >> 16;
                   4523: }
                   4524: 
1.1.1.3   root     4525: #if defined(HAS_I386)
1.1       root     4526: inline void pcbios_int_15h_c9h()
                   4527: {
                   4528:        REG8(AH) = 0x00;
                   4529:        REG8(CH) = cpu_type;
                   4530:        REG8(CL) = cpu_step;
                   4531: }
1.1.1.3   root     4532: #endif
1.1       root     4533: 
                   4534: inline void pcbios_int_15h_cah()
                   4535: {
                   4536:        switch(REG8(AL)) {
1.1.1.22  root     4537:        case 0x00:
1.1       root     4538:                if(REG8(BL) > 0x3f) {
                   4539:                        REG8(AH) = 0x03;
1.1.1.3   root     4540:                        m_CF = 1;
1.1       root     4541:                } else if(REG8(BL) < 0x0e) {
                   4542:                        REG8(AH) = 0x04;
1.1.1.3   root     4543:                        m_CF = 1;
1.1       root     4544:                } else {
1.1.1.8   root     4545:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     4546:                }
                   4547:                break;
1.1.1.22  root     4548:        case 0x01:
1.1       root     4549:                if(REG8(BL) > 0x3f) {
                   4550:                        REG8(AH) = 0x03;
1.1.1.3   root     4551:                        m_CF = 1;
1.1       root     4552:                } else if(REG8(BL) < 0x0e) {
                   4553:                        REG8(AH) = 0x04;
1.1.1.3   root     4554:                        m_CF = 1;
1.1       root     4555:                } else {
1.1.1.8   root     4556:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     4557:                }
                   4558:                break;
                   4559:        default:
1.1.1.22  root     4560:                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     4561:                REG8(AH) = 0x86;
1.1.1.3   root     4562:                m_CF = 1;
1.1       root     4563:                break;
                   4564:        }
                   4565: }
                   4566: 
1.1.1.22  root     4567: inline void pcbios_int_15h_e8h()
1.1.1.17  root     4568: {
1.1.1.22  root     4569:        switch(REG8(AL)) {
                   4570: #if defined(HAS_I386)
                   4571:        case 0x01:
                   4572:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   4573:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   4574:                break;
1.1.1.17  root     4575: #endif
1.1.1.22  root     4576:        default:
                   4577:                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));
                   4578:                REG8(AH) = 0x86;
                   4579:                m_CF = 1;
                   4580:                break;
                   4581:        }
                   4582: }
1.1.1.17  root     4583: 
1.1.1.16  root     4584: UINT32 pcbios_get_key_code(bool clear_buffer)
1.1       root     4585: {
                   4586:        UINT32 code = 0;
                   4587:        
                   4588:        if(key_buf_char->count() == 0) {
1.1.1.14  root     4589:                if(!update_key_buffer()) {
                   4590:                        if(clear_buffer) {
                   4591:                                Sleep(10);
                   4592:                        } else {
                   4593:                                maybe_idle();
                   4594:                        }
                   4595:                }
1.1       root     4596:        }
                   4597:        if(!clear_buffer) {
                   4598:                key_buf_char->store_buffer();
                   4599:                key_buf_scan->store_buffer();
                   4600:        }
                   4601:        if(key_buf_char->count() != 0) {
                   4602:                code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   4603:        }
                   4604:        if(key_buf_char->count() != 0) {
                   4605:                code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   4606:        }
                   4607:        if(!clear_buffer) {
                   4608:                key_buf_char->restore_buffer();
                   4609:                key_buf_scan->restore_buffer();
                   4610:        }
                   4611:        return code;
                   4612: }
                   4613: 
                   4614: inline void pcbios_int_16h_00h()
                   4615: {
1.1.1.14  root     4616:        while(key_code == 0 && !m_halted) {
1.1.1.16  root     4617:                key_code = pcbios_get_key_code(true);
1.1       root     4618:        }
                   4619:        if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   4620:                if(REG8(AH) == 0x10) {
                   4621:                        key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   4622:                } else {
                   4623:                        key_code = ((key_code >> 16) & 0xff00);
                   4624:                }
                   4625:        }
                   4626:        REG16(AX) = key_code & 0xffff;
                   4627:        key_code >>= 16;
                   4628: }
                   4629: 
                   4630: inline void pcbios_int_16h_01h()
                   4631: {
1.1.1.5   root     4632:        UINT32 key_code_tmp = key_code;
1.1       root     4633:        
1.1.1.5   root     4634:        if(key_code_tmp == 0) {
1.1.1.16  root     4635:                key_code_tmp = pcbios_get_key_code(false);
1.1.1.5   root     4636:        }
1.1.1.14  root     4637:        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   4638:                if(REG8(AH) == 0x11) {
                   4639:                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   4640:                } else {
                   4641:                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1       root     4642:                }
                   4643:        }
1.1.1.5   root     4644:        if(key_code_tmp != 0) {
                   4645:                REG16(AX) = key_code_tmp & 0xffff;
1.1       root     4646:        }
1.1.1.3   root     4647: #if defined(HAS_I386)
1.1.1.5   root     4648:        m_ZF = (key_code_tmp == 0);
1.1.1.3   root     4649: #else
1.1.1.5   root     4650:        m_ZeroVal = (key_code_tmp != 0);
1.1.1.3   root     4651: #endif
1.1       root     4652: }
                   4653: 
                   4654: inline void pcbios_int_16h_02h()
                   4655: {
                   4656:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   4657:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   4658:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   4659:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   4660:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   4661:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   4662:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   4663:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   4664: }
                   4665: 
                   4666: inline void pcbios_int_16h_03h()
                   4667: {
                   4668:        static UINT16 status = 0;
                   4669:        
                   4670:        switch(REG8(AL)) {
                   4671:        case 0x05:
                   4672:                status = REG16(BX);
                   4673:                break;
                   4674:        case 0x06:
                   4675:                REG16(BX) = status;
                   4676:                break;
                   4677:        default:
1.1.1.3   root     4678:                m_CF = 1;
1.1       root     4679:                break;
                   4680:        }
                   4681: }
                   4682: 
                   4683: inline void pcbios_int_16h_05h()
                   4684: {
1.1.1.14  root     4685:        key_buf_char->write(REG8(CL));
                   4686:        key_buf_scan->write(REG8(CH));
1.1       root     4687:        REG8(AL) = 0x00;
                   4688: }
                   4689: 
                   4690: inline void pcbios_int_16h_12h()
                   4691: {
                   4692:        pcbios_int_16h_02h();
                   4693:        
                   4694:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   4695:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   4696:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   4697:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   4698:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   4699:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   4700:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   4701:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   4702: }
                   4703: 
                   4704: inline void pcbios_int_16h_13h()
                   4705: {
                   4706:        static UINT16 status = 0;
                   4707:        
                   4708:        switch(REG8(AL)) {
                   4709:        case 0x00:
                   4710:                status = REG16(DX);
                   4711:                break;
                   4712:        case 0x01:
                   4713:                REG16(DX) = status;
                   4714:                break;
                   4715:        default:
1.1.1.22  root     4716:                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     4717:                m_CF = 1;
1.1       root     4718:                break;
                   4719:        }
                   4720: }
                   4721: 
                   4722: inline void pcbios_int_16h_14h()
                   4723: {
                   4724:        static UINT8 status = 0;
                   4725:        
                   4726:        switch(REG8(AL)) {
                   4727:        case 0x00:
                   4728:        case 0x01:
                   4729:                status = REG8(AL);
                   4730:                break;
                   4731:        case 0x02:
                   4732:                REG8(AL) = status;
                   4733:                break;
                   4734:        default:
1.1.1.22  root     4735:                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     4736:                m_CF = 1;
1.1       root     4737:                break;
                   4738:        }
                   4739: }
                   4740: 
1.1.1.24  root     4741: inline void pcbios_int_16h_55h()
                   4742: {
                   4743:        switch(REG8(AL)) {
                   4744:        case 0x00:
                   4745:                // keyboard tsr is not present
                   4746:                break;
                   4747:        case 0xfe:
                   4748:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   4749:                break;
                   4750:        case 0xff:
                   4751:                break;
                   4752:        default:
                   4753:                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));
                   4754:                m_CF = 1;
                   4755:                break;
                   4756:        }
                   4757: }
                   4758: 
1.1       root     4759: inline void pcbios_int_1ah_00h()
                   4760: {
1.1.1.19  root     4761:        pcbios_update_daily_timer_counter(timeGetTime());
                   4762:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   4763:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   4764:        REG8(AL) = mem[0x470];
                   4765:        mem[0x470] = 0;
1.1       root     4766: }
                   4767: 
                   4768: inline int to_bcd(int t)
                   4769: {
                   4770:        int u = (t % 100) / 10;
                   4771:        return (u << 4) | (t % 10);
                   4772: }
                   4773: 
                   4774: inline void pcbios_int_1ah_02h()
                   4775: {
                   4776:        SYSTEMTIME time;
                   4777:        
                   4778:        GetLocalTime(&time);
                   4779:        REG8(CH) = to_bcd(time.wHour);
                   4780:        REG8(CL) = to_bcd(time.wMinute);
                   4781:        REG8(DH) = to_bcd(time.wSecond);
                   4782:        REG8(DL) = 0x00;
                   4783: }
                   4784: 
                   4785: inline void pcbios_int_1ah_04h()
                   4786: {
                   4787:        SYSTEMTIME time;
                   4788:        
                   4789:        GetLocalTime(&time);
                   4790:        REG8(CH) = to_bcd(time.wYear / 100);
                   4791:        REG8(CL) = to_bcd(time.wYear);
                   4792:        REG8(DH) = to_bcd(time.wMonth);
                   4793:        REG8(DL) = to_bcd(time.wDay);
                   4794: }
                   4795: 
                   4796: inline void pcbios_int_1ah_0ah()
                   4797: {
                   4798:        SYSTEMTIME time;
                   4799:        FILETIME file_time;
                   4800:        WORD dos_date, dos_time;
                   4801:        
                   4802:        GetLocalTime(&time);
                   4803:        SystemTimeToFileTime(&time, &file_time);
                   4804:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   4805:        REG16(CX) = dos_date;
                   4806: }
                   4807: 
                   4808: // msdos system call
                   4809: 
                   4810: inline void msdos_int_21h_00h()
                   4811: {
1.1.1.3   root     4812:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     4813: }
                   4814: 
                   4815: inline void msdos_int_21h_01h()
                   4816: {
                   4817:        REG8(AL) = msdos_getche();
1.1.1.8   root     4818:        // some seconds may be passed in console
1.1       root     4819:        hardware_update();
                   4820: }
                   4821: 
                   4822: inline void msdos_int_21h_02h()
                   4823: {
                   4824:        msdos_putch(REG8(DL));
                   4825: }
                   4826: 
                   4827: inline void msdos_int_21h_03h()
                   4828: {
                   4829:        REG8(AL) = msdos_aux_in();
                   4830: }
                   4831: 
                   4832: inline void msdos_int_21h_04h()
                   4833: {
                   4834:        msdos_aux_out(REG8(DL));
                   4835: }
                   4836: 
                   4837: inline void msdos_int_21h_05h()
                   4838: {
                   4839:        msdos_prn_out(REG8(DL));
                   4840: }
                   4841: 
                   4842: inline void msdos_int_21h_06h()
                   4843: {
                   4844:        if(REG8(DL) == 0xff) {
                   4845:                if(msdos_kbhit()) {
                   4846:                        REG8(AL) = msdos_getch();
1.1.1.3   root     4847: #if defined(HAS_I386)
                   4848:                        m_ZF = 0;
                   4849: #else
                   4850:                        m_ZeroVal = 1;
                   4851: #endif
1.1       root     4852:                } else {
                   4853:                        REG8(AL) = 0;
1.1.1.3   root     4854: #if defined(HAS_I386)
                   4855:                        m_ZF = 1;
                   4856: #else
                   4857:                        m_ZeroVal = 0;
                   4858: #endif
1.1.1.14  root     4859:                        maybe_idle();
1.1       root     4860:                }
                   4861:        } else {
                   4862:                msdos_putch(REG8(DL));
                   4863:        }
                   4864: }
                   4865: 
                   4866: inline void msdos_int_21h_07h()
                   4867: {
                   4868:        REG8(AL) = msdos_getch();
1.1.1.8   root     4869:        // some seconds may be passed in console
1.1       root     4870:        hardware_update();
                   4871: }
                   4872: 
                   4873: inline void msdos_int_21h_08h()
                   4874: {
                   4875:        REG8(AL) = msdos_getch();
1.1.1.8   root     4876:        // some seconds may be passed in console
1.1       root     4877:        hardware_update();
                   4878: }
                   4879: 
                   4880: inline void msdos_int_21h_09h()
                   4881: {
1.1.1.21  root     4882:        msdos_stdio_reopen();
                   4883:        
1.1.1.20  root     4884:        process_t *process = msdos_process_info_get(current_psp);
                   4885:        int fd = msdos_psp_get_file_table(1, current_psp);
                   4886:        
1.1.1.14  root     4887:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   4888:        int len = 0;
1.1       root     4889:        
1.1.1.14  root     4890:        while(str[len] != '$' && len < 0x10000) {
                   4891:                len++;
                   4892:        }
1.1.1.20  root     4893:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4894:                // stdout is redirected to file
1.1.1.20  root     4895:                msdos_write(fd, str, len);
1.1       root     4896:        } else {
                   4897:                for(int i = 0; i < len; i++) {
1.1.1.14  root     4898:                        msdos_putch(str[i]);
1.1       root     4899:                }
                   4900:        }
                   4901: }
                   4902: 
                   4903: inline void msdos_int_21h_0ah()
                   4904: {
1.1.1.3   root     4905:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     4906:        int max = mem[ofs] - 1;
                   4907:        UINT8 *buf = mem + ofs + 2;
                   4908:        int chr, p = 0;
                   4909:        
                   4910:        while((chr = msdos_getch()) != 0x0d) {
                   4911:                if(chr == 0x00) {
                   4912:                        // skip 2nd byte
                   4913:                        msdos_getch();
                   4914:                } else if(chr == 0x08) {
                   4915:                        // back space
                   4916:                        if(p > 0) {
                   4917:                                p--;
1.1.1.20  root     4918:                                if(msdos_ctrl_code_check(buf[p])) {
                   4919:                                        msdos_putch(chr);
                   4920:                                        msdos_putch(chr);
                   4921:                                        msdos_putch(' ');
                   4922:                                        msdos_putch(' ');
                   4923:                                        msdos_putch(chr);
                   4924:                                        msdos_putch(chr);
                   4925:                                } else {
                   4926:                                        msdos_putch(chr);
                   4927:                                        msdos_putch(' ');
                   4928:                                        msdos_putch(chr);
                   4929:                                }
1.1       root     4930:                        }
                   4931:                } else if(p < max) {
                   4932:                        buf[p++] = chr;
                   4933:                        msdos_putch(chr);
                   4934:                }
                   4935:        }
                   4936:        buf[p] = 0x0d;
                   4937:        mem[ofs + 1] = p;
1.1.1.8   root     4938:        // some seconds may be passed in console
1.1       root     4939:        hardware_update();
                   4940: }
                   4941: 
                   4942: inline void msdos_int_21h_0bh()
                   4943: {
                   4944:        if(msdos_kbhit()) {
                   4945:                REG8(AL) = 0xff;
                   4946:        } else {
                   4947:                REG8(AL) = 0x00;
1.1.1.14  root     4948:                maybe_idle();
1.1       root     4949:        }
                   4950: }
                   4951: 
                   4952: inline void msdos_int_21h_0ch()
                   4953: {
                   4954:        // clear key buffer
1.1.1.21  root     4955:        msdos_stdio_reopen();
                   4956:        
1.1.1.20  root     4957:        process_t *process = msdos_process_info_get(current_psp);
                   4958:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4959:        
                   4960:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4961:                // stdin is redirected to file
                   4962:        } else {
                   4963:                while(msdos_kbhit()) {
                   4964:                        msdos_getch();
                   4965:                }
                   4966:        }
                   4967:        
                   4968:        switch(REG8(AL)) {
                   4969:        case 0x01:
                   4970:                msdos_int_21h_01h();
                   4971:                break;
                   4972:        case 0x06:
                   4973:                msdos_int_21h_06h();
                   4974:                break;
                   4975:        case 0x07:
                   4976:                msdos_int_21h_07h();
                   4977:                break;
                   4978:        case 0x08:
                   4979:                msdos_int_21h_08h();
                   4980:                break;
                   4981:        case 0x0a:
                   4982:                msdos_int_21h_0ah();
                   4983:                break;
                   4984:        default:
1.1.1.22  root     4985: //             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));
                   4986: //             REG16(AX) = 0x01;
                   4987: //             m_CF = 1;
1.1       root     4988:                break;
                   4989:        }
                   4990: }
                   4991: 
                   4992: inline void msdos_int_21h_0dh()
                   4993: {
                   4994: }
                   4995: 
                   4996: inline void msdos_int_21h_0eh()
                   4997: {
                   4998:        if(REG8(DL) < 26) {
                   4999:                _chdrive(REG8(DL) + 1);
                   5000:                msdos_cds_update(REG8(DL));
1.1.1.23  root     5001:                msdos_sda_update(current_psp);
1.1       root     5002:        }
                   5003:        REG8(AL) = 26; // zdrive
                   5004: }
                   5005: 
1.1.1.14  root     5006: inline void msdos_int_21h_0fh()
                   5007: {
                   5008:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5009:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5010:        char *path = msdos_fcb_path(fcb);
                   5011:        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     5012:        
1.1.1.14  root     5013:        if(hFile == INVALID_HANDLE_VALUE) {
                   5014:                REG8(AL) = 0xff;
                   5015:        } else {
                   5016:                REG8(AL) = 0;
                   5017:                fcb->current_block = 0;
                   5018:                fcb->record_size = 128;
                   5019:                fcb->file_size = GetFileSize(hFile, NULL);
                   5020:                fcb->handle = hFile;
                   5021:                fcb->cur_record = 0;
                   5022:        }
                   5023: }
                   5024: 
                   5025: inline void msdos_int_21h_10h()
                   5026: {
                   5027:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5028:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5029:        
                   5030:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   5031: }
                   5032: 
1.1       root     5033: inline void msdos_int_21h_11h()
                   5034: {
1.1.1.3   root     5035:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5036:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5037:        
                   5038:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5039:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5040:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5041:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5042:        char *path = msdos_fcb_path(fcb);
                   5043:        WIN32_FIND_DATA fd;
                   5044:        
1.1.1.13  root     5045:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5046:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5047:                FindClose(dtainfo->find_handle);
                   5048:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5049:        }
                   5050:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     5051:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   5052:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     5053:        
1.1.1.14  root     5054:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   5055:                dtainfo->allowable_mask &= ~8;
1.1       root     5056:        }
1.1.1.14  root     5057:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   5058:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5059:                      !msdos_find_file_has_8dot3name(&fd)) {
                   5060:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5061:                                FindClose(dtainfo->find_handle);
                   5062:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5063:                                break;
                   5064:                        }
                   5065:                }
                   5066:        }
1.1.1.13  root     5067:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5068:                if(ext_fcb->flag == 0xff) {
                   5069:                        ext_find->flag = 0xff;
                   5070:                        memset(ext_find->reserved, 0, 5);
                   5071:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5072:                }
                   5073:                find->drive = _getdrive();
1.1.1.13  root     5074:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5075:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5076:                find->nt_res = 0;
                   5077:                msdos_find_file_conv_local_time(&fd);
                   5078:                find->create_time_ms = 0;
                   5079:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5080:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5081:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5082:                find->cluster_hi = find->cluster_lo = 0;
                   5083:                find->file_size = fd.nFileSizeLow;
                   5084:                REG8(AL) = 0x00;
1.1.1.14  root     5085:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5086:                if(ext_fcb->flag == 0xff) {
                   5087:                        ext_find->flag = 0xff;
                   5088:                        memset(ext_find->reserved, 0, 5);
                   5089:                        ext_find->attribute = 8;
                   5090:                }
                   5091:                find->drive = _getdrive();
                   5092:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5093:                find->attribute = 8;
                   5094:                find->nt_res = 0;
                   5095:                msdos_find_file_conv_local_time(&fd);
                   5096:                find->create_time_ms = 0;
                   5097:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5098:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5099:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5100:                find->cluster_hi = find->cluster_lo = 0;
                   5101:                find->file_size = 0;
1.1.1.14  root     5102:                dtainfo->allowable_mask &= ~8;
1.1       root     5103:                REG8(AL) = 0x00;
                   5104:        } else {
                   5105:                REG8(AL) = 0xff;
                   5106:        }
                   5107: }
                   5108: 
                   5109: inline void msdos_int_21h_12h()
                   5110: {
1.1.1.3   root     5111:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     5112: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5113:        
                   5114:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     5115:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5116:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   5117:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     5118:        WIN32_FIND_DATA fd;
                   5119:        
1.1.1.13  root     5120:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   5121:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   5122:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     5123:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     5124:                              !msdos_find_file_has_8dot3name(&fd)) {
                   5125:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   5126:                                        FindClose(dtainfo->find_handle);
                   5127:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5128:                                        break;
                   5129:                                }
                   5130:                        }
                   5131:                } else {
1.1.1.13  root     5132:                        FindClose(dtainfo->find_handle);
                   5133:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     5134:                }
                   5135:        }
1.1.1.13  root     5136:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     5137:                if(ext_fcb->flag == 0xff) {
                   5138:                        ext_find->flag = 0xff;
                   5139:                        memset(ext_find->reserved, 0, 5);
                   5140:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5141:                }
                   5142:                find->drive = _getdrive();
1.1.1.13  root     5143:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     5144:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   5145:                find->nt_res = 0;
                   5146:                msdos_find_file_conv_local_time(&fd);
                   5147:                find->create_time_ms = 0;
                   5148:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5149:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5150:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5151:                find->cluster_hi = find->cluster_lo = 0;
                   5152:                find->file_size = fd.nFileSizeLow;
                   5153:                REG8(AL) = 0x00;
1.1.1.14  root     5154:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     5155:                if(ext_fcb->flag == 0xff) {
                   5156:                        ext_find->flag = 0xff;
                   5157:                        memset(ext_find->reserved, 0, 5);
                   5158:                        ext_find->attribute = 8;
                   5159:                }
                   5160:                find->drive = _getdrive();
                   5161:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   5162:                find->attribute = 8;
                   5163:                find->nt_res = 0;
                   5164:                msdos_find_file_conv_local_time(&fd);
                   5165:                find->create_time_ms = 0;
                   5166:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   5167:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   5168:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   5169:                find->cluster_hi = find->cluster_lo = 0;
                   5170:                find->file_size = 0;
1.1.1.14  root     5171:                dtainfo->allowable_mask &= ~8;
1.1       root     5172:                REG8(AL) = 0x00;
                   5173:        } else {
                   5174:                REG8(AL) = 0xff;
                   5175:        }
                   5176: }
                   5177: 
                   5178: inline void msdos_int_21h_13h()
                   5179: {
1.1.1.3   root     5180:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     5181:                REG8(AL) = 0xff;
                   5182:        } else {
                   5183:                REG8(AL) = 0x00;
                   5184:        }
                   5185: }
                   5186: 
1.1.1.16  root     5187: inline void msdos_int_21h_14h()
                   5188: {
                   5189:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5190:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5191:        process_t *process = msdos_process_info_get(current_psp);
                   5192:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5193:        DWORD num = 0;
                   5194:        
                   5195:        memset(mem + dta_laddr, 0, fcb->record_size);
                   5196:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5197:                REG8(AL) = 1;
                   5198:        } else {
                   5199:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5200:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5201:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5202:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5203:        }
                   5204: }
                   5205: 
                   5206: inline void msdos_int_21h_15h()
1.1.1.14  root     5207: {
                   5208:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5209:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     5210:        process_t *process = msdos_process_info_get(current_psp);
                   5211:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5212:        DWORD num = 0;
1.1.1.14  root     5213:        
1.1.1.16  root     5214:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5215:                REG8(AL) = 1;
                   5216:        } else {
                   5217:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5218:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   5219:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   5220:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   5221:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   5222:        }
                   5223: }
                   5224: 
                   5225: inline void msdos_int_21h_16h()
                   5226: {
                   5227:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5228:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     5229:        char *path = msdos_fcb_path(fcb);
                   5230:        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     5231:        
1.1.1.14  root     5232:        if(hFile == INVALID_HANDLE_VALUE) {
                   5233:                REG8(AL) = 0xff;
                   5234:        } else {
                   5235:                REG8(AL) = 0;
                   5236:                fcb->current_block = 0;
                   5237:                fcb->record_size = 128;
                   5238:                fcb->file_size = 0;
                   5239:                fcb->handle = hFile;
                   5240:                fcb->cur_record = 0;
                   5241:        }
                   5242: }
                   5243: 
1.1.1.16  root     5244: inline void msdos_int_21h_17h()
                   5245: {
                   5246:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5247:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   5248:        char *path_src = msdos_fcb_path(fcb_src);
                   5249:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   5250:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   5251:        char *path_dst = msdos_fcb_path(fcb_dst);
                   5252:        
                   5253:        if(rename(path_src, path_dst)) {
                   5254:                REG8(AL) = 0xff;
                   5255:        } else {
                   5256:                REG8(AL) = 0;
                   5257:        }
                   5258: }
                   5259: 
1.1       root     5260: inline void msdos_int_21h_18h()
                   5261: {
                   5262:        REG8(AL) = 0x00;
                   5263: }
                   5264: 
                   5265: inline void msdos_int_21h_19h()
                   5266: {
                   5267:        REG8(AL) = _getdrive() - 1;
                   5268: }
                   5269: 
                   5270: inline void msdos_int_21h_1ah()
                   5271: {
                   5272:        process_t *process = msdos_process_info_get(current_psp);
                   5273:        
                   5274:        process->dta.w.l = REG16(DX);
1.1.1.3   root     5275:        process->dta.w.h = SREG(DS);
1.1.1.23  root     5276:        msdos_sda_update(current_psp);
1.1       root     5277: }
                   5278: 
                   5279: inline void msdos_int_21h_1bh()
                   5280: {
                   5281:        int drive_num = _getdrive() - 1;
                   5282:        UINT16 seg, ofs;
                   5283:        
                   5284:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5285:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5286:                REG8(AL) = dpb->highest_sector_num + 1;
                   5287:                REG16(CX) = dpb->bytes_per_sector;
                   5288:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     5289:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     5290:        } else {
                   5291:                REG8(AL) = 0xff;
1.1.1.3   root     5292:                m_CF = 1;
1.1       root     5293:        }
                   5294: 
                   5295: }
                   5296: 
                   5297: inline void msdos_int_21h_1ch()
                   5298: {
                   5299:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   5300:        UINT16 seg, ofs;
                   5301:        
                   5302:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5303:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5304:                REG8(AL) = dpb->highest_sector_num + 1;
                   5305:                REG16(CX) = dpb->bytes_per_sector;
                   5306:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     5307:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     5308:        } else {
                   5309:                REG8(AL) = 0xff;
1.1.1.3   root     5310:                m_CF = 1;
1.1       root     5311:        }
                   5312: 
                   5313: }
                   5314: 
                   5315: inline void msdos_int_21h_1dh()
                   5316: {
                   5317:        REG8(AL) = 0;
                   5318: }
                   5319: 
                   5320: inline void msdos_int_21h_1eh()
                   5321: {
                   5322:        REG8(AL) = 0;
                   5323: }
                   5324: 
                   5325: inline void msdos_int_21h_1fh()
                   5326: {
                   5327:        int drive_num = _getdrive() - 1;
                   5328:        UINT16 seg, ofs;
                   5329:        
                   5330:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5331:                REG8(AL) = 0;
1.1.1.3   root     5332:                SREG(DS) = seg;
                   5333:                i386_load_segment_descriptor(DS);
1.1       root     5334:                REG16(BX) = ofs;
                   5335:        } else {
                   5336:                REG8(AL) = 0xff;
1.1.1.3   root     5337:                m_CF = 1;
1.1       root     5338:        }
                   5339: }
                   5340: 
                   5341: inline void msdos_int_21h_20h()
                   5342: {
                   5343:        REG8(AL) = 0;
                   5344: }
                   5345: 
1.1.1.14  root     5346: inline void msdos_int_21h_21h()
                   5347: {
                   5348:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5349:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5350:        
                   5351:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5352:                REG8(AL) = 1;
                   5353:        } else {
                   5354:                process_t *process = msdos_process_info_get(current_psp);
                   5355:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5356:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     5357:                DWORD num = 0;
1.1.1.14  root     5358:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   5359:                        REG8(AL) = 1;
                   5360:                } else {
                   5361:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5362:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     5363:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     5364:                }
                   5365:        }
                   5366: }
                   5367: 
                   5368: inline void msdos_int_21h_22h()
                   5369: {
                   5370:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5371:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5372:        
                   5373:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5374:                REG8(AL) = 0xff;
                   5375:        } else {
                   5376:                process_t *process = msdos_process_info_get(current_psp);
                   5377:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     5378:                DWORD num = 0;
1.1.1.14  root     5379:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   5380:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5381:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5382:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     5383:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     5384:        }
                   5385: }
                   5386: 
1.1.1.16  root     5387: inline void msdos_int_21h_23h()
                   5388: {
                   5389:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5390:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5391:        char *path = msdos_fcb_path(fcb);
                   5392:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   5393:        
                   5394:        if(hFile == INVALID_HANDLE_VALUE) {
                   5395:                REG8(AL) = 0xff;
                   5396:        } else {
                   5397:                UINT32 size = GetFileSize(hFile, NULL);
                   5398:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   5399:                REG8(AL) = 0;
                   5400:        }
                   5401: }
                   5402: 
                   5403: inline void msdos_int_21h_24h()
                   5404: {
                   5405:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5406:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5407:        
                   5408:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   5409: }
                   5410: 
1.1       root     5411: inline void msdos_int_21h_25h()
                   5412: {
                   5413:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     5414:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     5415: }
                   5416: 
                   5417: inline void msdos_int_21h_26h()
                   5418: {
                   5419:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   5420:        
                   5421:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   5422:        psp->first_mcb = REG16(DX) + 16;
                   5423:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5424:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5425:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5426:        psp->parent_psp = 0;
                   5427: }
                   5428: 
1.1.1.16  root     5429: inline void msdos_int_21h_27h()
                   5430: {
                   5431:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5432:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5433:        
                   5434:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5435:                REG8(AL) = 1;
                   5436:        } else {
                   5437:                process_t *process = msdos_process_info_get(current_psp);
                   5438:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5439:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   5440:                DWORD num = 0;
                   5441:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   5442:                        REG8(AL) = 1;
                   5443:                } else {
                   5444:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5445:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   5446:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   5447:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   5448:                }
                   5449:        }
                   5450: }
                   5451: 
                   5452: inline void msdos_int_21h_28h()
                   5453: {
                   5454:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   5455:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   5456:        
                   5457:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5458:                REG8(AL) = 0xff;
                   5459:        } else {
                   5460:                process_t *process = msdos_process_info_get(current_psp);
                   5461:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   5462:                DWORD num = 0;
                   5463:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   5464:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   5465:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   5466:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   5467:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   5468:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   5469:        }
                   5470: }
                   5471: 
1.1       root     5472: inline void msdos_int_21h_29h()
                   5473: {
1.1.1.20  root     5474:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   5475:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     5476:        UINT8 drv = 0;
                   5477:        char sep_chars[] = ":.;,=+";
                   5478:        char end_chars[] = "\\<>|/\"[]";
                   5479:        char spc_chars[] = " \t";
                   5480:        
1.1.1.20  root     5481:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   5482:        buffer[1023] = 0;
                   5483:        memset(name, 0x20, sizeof(name));
                   5484:        memset(ext, 0x20, sizeof(ext));
                   5485:        
1.1       root     5486:        if(REG8(AL) & 1) {
1.1.1.20  root     5487:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   5488:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     5489:                        ofs++;
                   5490:                }
                   5491:        }
1.1.1.20  root     5492:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     5493:        
1.1.1.24  root     5494:        if(buffer[ofs + 1] == ':') {
                   5495:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   5496:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     5497:                        ofs += 2;
1.1.1.24  root     5498:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   5499:                                ofs++;
                   5500:                        }
                   5501:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   5502:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     5503:                        ofs += 2;
1.1.1.24  root     5504:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   5505:                                ofs++;
                   5506:                        }
1.1       root     5507:                }
                   5508:        }
1.1.1.20  root     5509:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   5510:                UINT8 c = buffer[ofs];
                   5511:                if(is_kanji) {
                   5512:                        is_kanji = 0;
                   5513:                } else if(msdos_lead_byte_check(c)) {
                   5514:                        is_kanji = 1;
                   5515:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     5516:                        break;
                   5517:                } else if(c >= 'a' && c <= 'z') {
                   5518:                        c -= 0x20;
                   5519:                }
                   5520:                ofs++;
                   5521:                name[i] = c;
                   5522:        }
1.1.1.20  root     5523:        if(buffer[ofs] == '.') {
1.1       root     5524:                ofs++;
1.1.1.20  root     5525:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   5526:                        UINT8 c = buffer[ofs];
                   5527:                        if(is_kanji) {
                   5528:                                is_kanji = 0;
                   5529:                        } else if(msdos_lead_byte_check(c)) {
                   5530:                                is_kanji = 1;
                   5531:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     5532:                                break;
                   5533:                        } else if(c >= 'a' && c <= 'z') {
                   5534:                                c -= 0x20;
                   5535:                        }
                   5536:                        ofs++;
                   5537:                        ext[i] = c;
                   5538:                }
                   5539:        }
1.1.1.20  root     5540:        int si = REG16(SI) + ofs;
1.1.1.3   root     5541:        int ds = SREG(DS);
1.1       root     5542:        while(si > 0xffff) {
                   5543:                si -= 0x10;
                   5544:                ds++;
                   5545:        }
                   5546:        REG16(SI) = si;
1.1.1.3   root     5547:        SREG(DS) = ds;
                   5548:        i386_load_segment_descriptor(DS);
1.1       root     5549:        
1.1.1.3   root     5550:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     5551:        if(!(REG8(AL) & 2) || drv != 0) {
                   5552:                fcb[0] = drv;
                   5553:        }
                   5554:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   5555:                memcpy(fcb + 1, name, 8);
                   5556:        }
                   5557:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   5558:                memcpy(fcb + 9, ext, 3);
                   5559:        }
                   5560:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     5561:                if(fcb[i] == '*') {
                   5562:                        found_star = 1;
                   5563:                }
                   5564:                if(found_star) {
                   5565:                        fcb[i] = '?';
                   5566:                }
                   5567:        }
1.1.1.20  root     5568:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     5569:                if(fcb[i] == '*') {
                   5570:                        found_star = 1;
                   5571:                }
                   5572:                if(found_star) {
                   5573:                        fcb[i] = '?';
                   5574:                }
                   5575:        }
                   5576:        
                   5577:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   5578:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   5579:                        REG8(AL) = 0x01;
1.1.1.20  root     5580:                } else {
                   5581:                        REG8(AL) = 0x00;
1.1       root     5582:                }
                   5583:        } else {
                   5584:                REG8(AL) = 0xff;
                   5585:        }
                   5586: }
                   5587: 
                   5588: inline void msdos_int_21h_2ah()
                   5589: {
                   5590:        SYSTEMTIME sTime;
                   5591:        
                   5592:        GetLocalTime(&sTime);
                   5593:        REG16(CX) = sTime.wYear;
                   5594:        REG8(DH) = (UINT8)sTime.wMonth;
                   5595:        REG8(DL) = (UINT8)sTime.wDay;
                   5596:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   5597: }
                   5598: 
                   5599: inline void msdos_int_21h_2bh()
                   5600: {
1.1.1.14  root     5601:        REG8(AL) = 0xff;
1.1       root     5602: }
                   5603: 
                   5604: inline void msdos_int_21h_2ch()
                   5605: {
                   5606:        SYSTEMTIME sTime;
                   5607:        
                   5608:        GetLocalTime(&sTime);
                   5609:        REG8(CH) = (UINT8)sTime.wHour;
                   5610:        REG8(CL) = (UINT8)sTime.wMinute;
                   5611:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     5612:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     5613: }
                   5614: 
                   5615: inline void msdos_int_21h_2dh()
                   5616: {
                   5617:        REG8(AL) = 0x00;
                   5618: }
                   5619: 
                   5620: inline void msdos_int_21h_2eh()
                   5621: {
                   5622:        process_t *process = msdos_process_info_get(current_psp);
                   5623:        
                   5624:        process->verify = REG8(AL);
                   5625: }
                   5626: 
                   5627: inline void msdos_int_21h_2fh()
                   5628: {
                   5629:        process_t *process = msdos_process_info_get(current_psp);
                   5630:        
                   5631:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     5632:        SREG(ES) = process->dta.w.h;
                   5633:        i386_load_segment_descriptor(ES);
1.1       root     5634: }
                   5635: 
                   5636: inline void msdos_int_21h_30h()
                   5637: {
                   5638:        // Version Flag / OEM
                   5639:        if(REG8(AL) == 1) {
                   5640:                REG8(BH) = 0x00;        // not in ROM
                   5641:        } else {
                   5642:                REG8(BH) = 0xff;        // OEM = Microsoft
                   5643:        }
1.1.1.9   root     5644:        REG8(AL) = major_version;       // 7
                   5645:        REG8(AH) = minor_version;       // 10
1.1       root     5646: }
                   5647: 
                   5648: inline void msdos_int_21h_31h()
                   5649: {
1.1.1.14  root     5650:        msdos_mem_realloc(current_psp, REG16(DX), NULL);
1.1       root     5651:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   5652: }
                   5653: 
                   5654: inline void msdos_int_21h_32h()
                   5655: {
                   5656:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   5657:        UINT16 seg, ofs;
                   5658:        
                   5659:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   5660:                REG8(AL) = 0;
1.1.1.3   root     5661:                SREG(DS) = seg;
                   5662:                i386_load_segment_descriptor(DS);
1.1       root     5663:                REG16(BX) = ofs;
                   5664:        } else {
                   5665:                REG8(AL) = 0xff;
1.1.1.3   root     5666:                m_CF = 1;
1.1       root     5667:        }
                   5668: }
                   5669: 
                   5670: inline void msdos_int_21h_33h()
                   5671: {
                   5672:        static UINT8 state = 0x00;
                   5673:        char path[MAX_PATH];
                   5674:        
                   5675:        switch(REG8(AL)) {
                   5676:        case 0x00:
                   5677:                REG8(DL) = state;
                   5678:                break;
                   5679:        case 0x01:
                   5680:                state = REG8(DL);
                   5681:                break;
                   5682:        case 0x05:
                   5683:                GetSystemDirectory(path, MAX_PATH);
                   5684:                if(path[0] >= 'a' && path[0] <= 'z') {
                   5685:                        REG8(DL) = path[0] - 'a' + 1;
                   5686:                } else {
                   5687:                        REG8(DL) = path[0] - 'A' + 1;
                   5688:                }
                   5689:                break;
                   5690:        case 0x06:
1.1.1.2   root     5691:                // MS-DOS version (7.10)
1.1       root     5692:                REG8(BL) = 7;
1.1.1.2   root     5693:                REG8(BH) = 10;
1.1       root     5694:                REG8(DL) = 0;
                   5695:                REG8(DH) = 0x10; // in HMA
                   5696:                break;
1.1.1.6   root     5697:        case 0x07:
                   5698:                if(REG8(DL) == 0) {
                   5699:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   5700:                } else if(REG8(DL) == 1) {
                   5701:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   5702:                }
                   5703:                break;
1.1       root     5704:        default:
1.1.1.22  root     5705:                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     5706:                REG16(AX) = 0x01;
1.1.1.3   root     5707:                m_CF = 1;
1.1       root     5708:                break;
                   5709:        }
                   5710: }
                   5711: 
1.1.1.23  root     5712: inline void msdos_int_21h_34h()
                   5713: {
                   5714:        SREG(ES) = SDA_TOP >> 4;
                   5715:        i386_load_segment_descriptor(ES);
                   5716:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   5717: }
                   5718: 
1.1       root     5719: inline void msdos_int_21h_35h()
                   5720: {
                   5721:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     5722:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   5723:        i386_load_segment_descriptor(ES);
1.1       root     5724: }
                   5725: 
                   5726: inline void msdos_int_21h_36h()
                   5727: {
                   5728:        struct _diskfree_t df = {0};
                   5729:        
                   5730:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   5731:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   5732:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     5733:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   5734:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     5735:        } else {
                   5736:                REG16(AX) = 0xffff;
                   5737:        }
                   5738: }
                   5739: 
                   5740: inline void msdos_int_21h_37h()
                   5741: {
1.1.1.22  root     5742:        static UINT8 dev_flag = 0xff;
1.1       root     5743:        
                   5744:        switch(REG8(AL)) {
                   5745:        case 0x00:
1.1.1.22  root     5746:                {
                   5747:                        process_t *process = msdos_process_info_get(current_psp);
                   5748:                        REG8(AL) = 0x00;
                   5749:                        REG8(DL) = process->switchar;
                   5750:                }
1.1       root     5751:                break;
                   5752:        case 0x01:
1.1.1.22  root     5753:                {
                   5754:                        process_t *process = msdos_process_info_get(current_psp);
                   5755:                        REG8(AL) = 0x00;
                   5756:                        process->switchar = REG8(DL);
1.1.1.23  root     5757:                        msdos_sda_update(current_psp);
1.1.1.22  root     5758:                }
                   5759:                break;
                   5760:        case 0x02:
                   5761:                REG8(DL) = dev_flag;
                   5762:                break;
                   5763:        case 0x03:
                   5764:                dev_flag = REG8(DL);
                   5765:                break;
                   5766:        case 0xd0:
                   5767:        case 0xd1:
                   5768:        case 0xd2:
                   5769:        case 0xd3:
                   5770:        case 0xd4:
                   5771:        case 0xd5:
                   5772:        case 0xd6:
                   5773:        case 0xd7:
                   5774:        case 0xdc:
                   5775:        case 0xdd:
                   5776:        case 0xde:
                   5777:        case 0xdf:
                   5778:                // diet ???
                   5779:                REG16(AX) = 1;
1.1       root     5780:                break;
                   5781:        default:
1.1.1.22  root     5782:                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     5783:                REG16(AX) = 1;
                   5784:                break;
                   5785:        }
                   5786: }
                   5787: 
1.1.1.19  root     5788: int get_country_info(country_info_t *ci)
1.1.1.17  root     5789: {
                   5790:        char LCdata[80];
                   5791:        
1.1.1.19  root     5792:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     5793:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   5794:        ci->currency_dec_digits = atoi(LCdata);
                   5795:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   5796:        ci->currency_format = *LCdata - '0';
                   5797:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   5798:        ci->date_format = *LCdata - '0';
                   5799:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   5800:        memcpy(&ci->currency_symbol, LCdata, 4);
                   5801:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   5802:        *ci->date_sep = *LCdata;
                   5803:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   5804:        *ci->dec_sep = *LCdata;
                   5805:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   5806:        *ci->list_sep = *LCdata;
                   5807:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   5808:        *ci->thou_sep = *LCdata;
                   5809:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   5810:        *ci->time_sep = *LCdata;
                   5811:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   5812:        if(strchr(LCdata, 'H') != NULL) {
                   5813:                ci->time_format = 1;
                   5814:        }
1.1.1.24  root     5815:        ci->case_map.w.l = 0x000c; // FFFD:000C
                   5816:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     5817:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   5818:        return atoi(LCdata);
                   5819: }
                   5820: 
1.1.1.14  root     5821: inline void msdos_int_21h_38h()
                   5822: {
                   5823:        switch(REG8(AL)) {
                   5824:        case 0x00:
1.1.1.19  root     5825:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     5826:                break;
                   5827:        default:
1.1.1.22  root     5828:                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     5829:                REG16(AX) = 2;
                   5830:                m_CF = 1;
                   5831:                break;
                   5832:        }
                   5833: }
                   5834: 
1.1       root     5835: inline void msdos_int_21h_39h(int lfn)
                   5836: {
1.1.1.3   root     5837:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     5838:                REG16(AX) = errno;
1.1.1.3   root     5839:                m_CF = 1;
1.1       root     5840:        }
                   5841: }
                   5842: 
                   5843: inline void msdos_int_21h_3ah(int lfn)
                   5844: {
1.1.1.3   root     5845:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     5846:                REG16(AX) = errno;
1.1.1.3   root     5847:                m_CF = 1;
1.1       root     5848:        }
                   5849: }
                   5850: 
                   5851: inline void msdos_int_21h_3bh(int lfn)
                   5852: {
1.1.1.3   root     5853:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     5854:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     5855:                m_CF = 1;
1.1       root     5856:        }
                   5857: }
                   5858: 
                   5859: inline void msdos_int_21h_3ch()
                   5860: {
1.1.1.3   root     5861:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     5862:        int attr = GetFileAttributes(path);
                   5863:        int fd = -1;
1.1.1.11  root     5864:        UINT16 info;
1.1       root     5865:        
1.1.1.11  root     5866:        if(msdos_is_con_path(path)) {
                   5867:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   5868:                info = 0x80d3;
1.1.1.14  root     5869:        } else if(msdos_is_nul_path(path)) {
                   5870:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   5871:                info = 0x80d3;
1.1.1.24  root     5872:        } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     5873:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   5874:                info = 0x80d3;
1.1       root     5875:        } else {
                   5876:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     5877:                info = msdos_drive_number(path);
1.1       root     5878:        }
                   5879:        if(fd != -1) {
                   5880:                if(attr == -1) {
                   5881:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   5882:                }
                   5883:                SetFileAttributes(path, attr);
                   5884:                REG16(AX) = fd;
1.1.1.11  root     5885:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     5886:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     5887:        } else {
                   5888:                REG16(AX) = errno;
1.1.1.3   root     5889:                m_CF = 1;
1.1       root     5890:        }
                   5891: }
                   5892: 
                   5893: inline void msdos_int_21h_3dh()
                   5894: {
1.1.1.3   root     5895:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     5896:        int mode = REG8(AL) & 0x03;
1.1.1.11  root     5897:        int fd = -1;
                   5898:        UINT16 info;
1.1       root     5899:        
                   5900:        if(mode < 0x03) {
1.1.1.11  root     5901:                if(msdos_is_con_path(path)) {
1.1.1.13  root     5902:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     5903:                        info = 0x80d3;
1.1.1.14  root     5904:                } else if(msdos_is_nul_path(path)) {
                   5905:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   5906:                        info = 0x80d3;
1.1.1.24  root     5907:                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     5908:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   5909:                        info = 0x80d3;
1.1.1.11  root     5910:                } else {
1.1.1.13  root     5911:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     5912:                        info = msdos_drive_number(path);
                   5913:                }
1.1       root     5914:                if(fd != -1) {
                   5915:                        REG16(AX) = fd;
1.1.1.11  root     5916:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     5917:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     5918:                } else {
                   5919:                        REG16(AX) = errno;
1.1.1.3   root     5920:                        m_CF = 1;
1.1       root     5921:                }
                   5922:        } else {
                   5923:                REG16(AX) = 0x0c;
1.1.1.3   root     5924:                m_CF = 1;
1.1       root     5925:        }
                   5926: }
                   5927: 
                   5928: inline void msdos_int_21h_3eh()
                   5929: {
                   5930:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     5931:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     5932:        
1.1.1.20  root     5933:        if(fd < process->max_files && file_handler[fd].valid) {
                   5934:                _close(fd);
                   5935:                msdos_file_handler_close(fd);
                   5936:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     5937:        } else {
                   5938:                REG16(AX) = 0x06;
1.1.1.3   root     5939:                m_CF = 1;
1.1       root     5940:        }
                   5941: }
                   5942: 
                   5943: inline void msdos_int_21h_3fh()
                   5944: {
                   5945:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     5946:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     5947:        
1.1.1.20  root     5948:        if(fd < process->max_files && file_handler[fd].valid) {
                   5949:                if(file_mode[file_handler[fd].mode].in) {
                   5950:                        if(file_handler[fd].atty) {
1.1       root     5951:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     5952:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     5953:                                int max = REG16(CX);
                   5954:                                int p = 0;
                   5955:                                
                   5956:                                while(max > p) {
                   5957:                                        int chr = msdos_getch();
                   5958:                                        
                   5959:                                        if(chr == 0x00) {
                   5960:                                                // skip 2nd byte
                   5961:                                                msdos_getch();
                   5962:                                        } else if(chr == 0x0d) {
                   5963:                                                // carriage return
                   5964:                                                buf[p++] = 0x0d;
                   5965:                                                if(max > p) {
                   5966:                                                        buf[p++] = 0x0a;
                   5967:                                                }
1.1.1.14  root     5968:                                                msdos_putch('\n');
1.1       root     5969:                                                break;
                   5970:                                        } else if(chr == 0x08) {
                   5971:                                                // back space
                   5972:                                                if(p > 0) {
                   5973:                                                        p--;
1.1.1.20  root     5974:                                                        if(msdos_ctrl_code_check(buf[p])) {
                   5975:                                                                msdos_putch(chr);
                   5976:                                                                msdos_putch(chr);
                   5977:                                                                msdos_putch(' ');
                   5978:                                                                msdos_putch(' ');
                   5979:                                                                msdos_putch(chr);
                   5980:                                                                msdos_putch(chr);
                   5981:                                                        } else {
                   5982:                                                                msdos_putch(chr);
                   5983:                                                                msdos_putch(' ');
                   5984:                                                                msdos_putch(chr);
                   5985:                                                        }
1.1       root     5986:                                                }
                   5987:                                        } else {
                   5988:                                                buf[p++] = chr;
                   5989:                                                msdos_putch(chr);
                   5990:                                        }
                   5991:                                }
                   5992:                                REG16(AX) = p;
1.1.1.8   root     5993:                                // some seconds may be passed in console
1.1       root     5994:                                hardware_update();
                   5995:                        } else {
1.1.1.20  root     5996:                                REG16(AX) = _read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     5997:                        }
                   5998:                } else {
                   5999:                        REG16(AX) = 0x05;
1.1.1.3   root     6000:                        m_CF = 1;
1.1       root     6001:                }
                   6002:        } else {
                   6003:                REG16(AX) = 0x06;
1.1.1.3   root     6004:                m_CF = 1;
1.1       root     6005:        }
                   6006: }
                   6007: 
                   6008: inline void msdos_int_21h_40h()
                   6009: {
                   6010:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6011:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6012:        
1.1.1.20  root     6013:        if(fd < process->max_files && file_handler[fd].valid) {
                   6014:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     6015:                        if(REG16(CX)) {
1.1.1.20  root     6016:                                if(file_handler[fd].atty) {
1.1       root     6017:                                        // BX is stdout/stderr or is redirected to stdout
                   6018:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     6019:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     6020:                                        }
                   6021:                                        REG16(AX) = REG16(CX);
                   6022:                                } else {
1.1.1.20  root     6023:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     6024:                                }
                   6025:                        } else {
1.1.1.20  root     6026:                                UINT32 pos = _tell(fd);
                   6027:                                _lseek(fd, 0, SEEK_END);
                   6028:                                UINT32 size = _tell(fd);
1.1.1.12  root     6029:                                if(pos < size) {
1.1.1.20  root     6030:                                        _lseek(fd, pos, SEEK_SET);
                   6031:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     6032:                                } else {
                   6033:                                        for(UINT32 i = size; i < pos; i++) {
                   6034:                                                UINT8 tmp = 0;
1.1.1.23  root     6035:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     6036:                                        }
1.1.1.20  root     6037:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     6038:                                }
1.1.1.23  root     6039:                                REG16(AX) = 0;
1.1       root     6040:                        }
                   6041:                } else {
                   6042:                        REG16(AX) = 0x05;
1.1.1.3   root     6043:                        m_CF = 1;
1.1       root     6044:                }
                   6045:        } else {
                   6046:                REG16(AX) = 0x06;
1.1.1.3   root     6047:                m_CF = 1;
1.1       root     6048:        }
                   6049: }
                   6050: 
                   6051: inline void msdos_int_21h_41h(int lfn)
                   6052: {
1.1.1.3   root     6053:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     6054:                REG16(AX) = errno;
1.1.1.3   root     6055:                m_CF = 1;
1.1       root     6056:        }
                   6057: }
                   6058: 
                   6059: inline void msdos_int_21h_42h()
                   6060: {
                   6061:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6062:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6063:        
1.1.1.20  root     6064:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     6065:                if(REG8(AL) < 0x03) {
                   6066:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     6067:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   6068:                        UINT32 pos = _tell(fd);
1.1       root     6069:                        REG16(AX) = pos & 0xffff;
                   6070:                        REG16(DX) = (pos >> 16);
                   6071:                } else {
                   6072:                        REG16(AX) = 0x01;
1.1.1.3   root     6073:                        m_CF = 1;
1.1       root     6074:                }
                   6075:        } else {
                   6076:                REG16(AX) = 0x06;
1.1.1.3   root     6077:                m_CF = 1;
1.1       root     6078:        }
                   6079: }
                   6080: 
                   6081: inline void msdos_int_21h_43h(int lfn)
                   6082: {
1.1.1.3   root     6083:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     6084:        int attr;
                   6085:        
1.1.1.14  root     6086:        if(!lfn && REG8(AL) > 2) {
                   6087:                REG16(AX) = 0x01;
                   6088:                m_CF = 1;
                   6089:                return;
                   6090:        }
                   6091:        switch(REG8(lfn ? BL : AL)) {
1.1       root     6092:        case 0x00:
                   6093:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     6094:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   6095:                } else {
                   6096:                        REG16(AX) = (UINT16)GetLastError();
                   6097:                        m_CF = 1;
                   6098:                }
                   6099:                break;
                   6100:        case 0x01:
                   6101:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   6102:                        REG16(AX) = (UINT16)GetLastError();
                   6103:                        m_CF = 1;
                   6104:                }
                   6105:                break;
                   6106:        case 0x02:
                   6107:                {
                   6108:                        DWORD size = GetCompressedFileSize(path, NULL);
                   6109:                        if(size != INVALID_FILE_SIZE) {
                   6110:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   6111:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   6112:                                        // this isn't correct if the file is in the NTFS MFT
                   6113:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   6114:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   6115:                                        }
                   6116:                                }
                   6117:                                REG16(AX) = LOWORD(size);
                   6118:                                REG16(DX) = HIWORD(size);
                   6119:                        } else {
                   6120:                                REG16(AX) = (UINT16)GetLastError();
                   6121:                                m_CF = 1;
1.1       root     6122:                        }
1.1.1.14  root     6123:                }
                   6124:                break;
                   6125:        case 0x03:
                   6126:        case 0x05:
                   6127:        case 0x07:
                   6128:                {
                   6129:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6130:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6131:                                FILETIME local, time;
                   6132:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   6133:                                if(REG8(BL) == 7) {
                   6134:                                        ULARGE_INTEGER hund;
                   6135:                                        hund.LowPart = local.dwLowDateTime;
                   6136:                                        hund.HighPart = local.dwHighDateTime;
                   6137:                                        hund.QuadPart += REG16(SI) * 100000;
                   6138:                                        local.dwLowDateTime = hund.LowPart;
                   6139:                                        local.dwHighDateTime = hund.HighPart;
                   6140:                                }
                   6141:                                LocalFileTimeToFileTime(&local, &time);
                   6142:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   6143:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   6144:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   6145:                                        REG16(AX) = (UINT16)GetLastError();
                   6146:                                        m_CF = 1;
                   6147:                                }
                   6148:                                CloseHandle(hFile);
                   6149:                        } else {
                   6150:                                REG16(AX) = (UINT16)GetLastError();
                   6151:                                m_CF = 1;
1.1       root     6152:                        }
1.1.1.14  root     6153:                }
                   6154:                break;
                   6155:        case 0x04:
                   6156:        case 0x06:
                   6157:        case 0x08:
                   6158:                {
                   6159:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   6160:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   6161:                                FILETIME *time, local;
                   6162:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   6163:                                                   0x06 ? &fad.ftLastAccessTime :
                   6164:                                                          &fad.ftCreationTime;
                   6165:                                FileTimeToLocalFileTime(time, &local);
                   6166:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   6167:                                if(REG8(BL) == 0x08) {
                   6168:                                        ULARGE_INTEGER hund;
                   6169:                                        hund.LowPart = local.dwLowDateTime;
                   6170:                                        hund.HighPart = local.dwHighDateTime;
                   6171:                                        hund.QuadPart /= 100000;
                   6172:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   6173:                                }
                   6174:                        } else {
                   6175:                                REG16(AX) = (UINT16)GetLastError();
                   6176:                                m_CF = 1;
1.1       root     6177:                        }
1.1.1.14  root     6178:                }
                   6179:                break;
                   6180:        default:
1.1.1.22  root     6181:                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     6182:                REG16(AX) = 0x01;
                   6183:                m_CF = 1;
                   6184:                break;
                   6185:        }
                   6186: }
                   6187: 
                   6188: inline void msdos_int_21h_44h()
                   6189: {
1.1.1.22  root     6190:        static UINT16 iteration_count = 0;
                   6191:        
1.1.1.20  root     6192:        process_t *process = msdos_process_info_get(current_psp);
                   6193:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   6194:        
1.1.1.14  root     6195:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   6196:        
                   6197:        switch(REG8(AL)) {
                   6198:        case 0x00:
                   6199:        case 0x01:
                   6200:        case 0x02:
                   6201:        case 0x03:
                   6202:        case 0x04:
                   6203:        case 0x05:
                   6204:        case 0x06:
                   6205:        case 0x07:
1.1.1.20  root     6206:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   6207:                        REG16(AX) = 0x06;
                   6208:                        m_CF = 1;
                   6209:                        return;
1.1.1.14  root     6210:                }
                   6211:                break;
                   6212:        case 0x08:
                   6213:        case 0x09:
                   6214:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   6215:                        // invalid drive number
                   6216:                        REG16(AX) = 0x0f;
                   6217:                        m_CF = 1;
                   6218:                        return;
                   6219:                } else {
                   6220:                        if(REG8(BL) == 0) {
                   6221:                                val = GetDriveType(NULL);
                   6222:                        } else {
                   6223:                                char tmp[8];
                   6224:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   6225:                                val = GetDriveType(tmp);
                   6226:                        }
                   6227:                        if(val == DRIVE_NO_ROOT_DIR) {
                   6228:                                // no drive
                   6229:                                REG16(AX) = 0x0f;
                   6230:                                m_CF = 1;
                   6231:                                return;
1.1       root     6232:                        }
                   6233:                }
                   6234:                break;
                   6235:        }
                   6236:        switch(REG8(AL)) {
                   6237:        case 0x00: // get ioctrl data
1.1.1.20  root     6238:                REG16(DX) = file_handler[fd].info;
1.1       root     6239:                break;
                   6240:        case 0x01: // set ioctrl data
1.1.1.20  root     6241:                file_handler[fd].info |= REG8(DL);
1.1       root     6242:                break;
                   6243:        case 0x02: // recv from character device
                   6244:        case 0x03: // send to character device
                   6245:        case 0x04: // recv from block device
                   6246:        case 0x05: // send to block device
                   6247:                REG16(AX) = 0x05;
1.1.1.3   root     6248:                m_CF = 1;
1.1       root     6249:                break;
                   6250:        case 0x06: // get read status
1.1.1.20  root     6251:                if(file_mode[file_handler[fd].mode].in) {
                   6252:                        if(file_handler[fd].atty) {
1.1.1.14  root     6253:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     6254:                        } else {
1.1.1.20  root     6255:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     6256:                        }
1.1.1.14  root     6257:                } else {
                   6258:                        REG8(AL) = 0x00;
1.1       root     6259:                }
                   6260:                break;
                   6261:        case 0x07: // get write status
1.1.1.20  root     6262:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     6263:                        REG8(AL) = 0xff;
                   6264:                } else {
                   6265:                        REG8(AL) = 0x00;
1.1       root     6266:                }
                   6267:                break;
                   6268:        case 0x08: // check removable drive
1.1.1.14  root     6269:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   6270:                        // removable drive
                   6271:                        REG16(AX) = 0x00;
1.1       root     6272:                } else {
1.1.1.14  root     6273:                        // fixed drive
                   6274:                        REG16(AX) = 0x01;
1.1       root     6275:                }
                   6276:                break;
                   6277:        case 0x09: // check remote drive
1.1.1.14  root     6278:                if(val == DRIVE_REMOTE) {
                   6279:                        // remote drive
                   6280:                        REG16(DX) = 0x1000;
1.1       root     6281:                } else {
1.1.1.14  root     6282:                        // local drive
                   6283:                        REG16(DX) = 0x00;
1.1       root     6284:                }
                   6285:                break;
1.1.1.21  root     6286:        case 0x0a: // check remote handle
                   6287:                REG16(DX) = 0x00; // FIXME
                   6288:                break;
1.1       root     6289:        case 0x0b: // set retry count
                   6290:                break;
1.1.1.22  root     6291:        case 0x0c: // generic character device request
                   6292:                if(REG8(CL) == 0x45) {
                   6293:                        // set iteration (retry) count
                   6294:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   6295:                } else if(REG8(CL) == 0x4a) {
                   6296:                        // select code page
                   6297:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   6298:                        msdos_nls_tables_update();
                   6299:                } else if(REG8(CL) == 0x65) {
                   6300:                        // get iteration (retry) count
                   6301:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   6302:                } else if(REG8(CL) == 0x6a) {
                   6303:                        // query selected code page
                   6304:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   6305:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   6306:                        
                   6307:                        CPINFO info;
                   6308:                        GetCPInfo(active_code_page, &info);
                   6309:                        
                   6310:                        if(info.MaxCharSize != 1) {
                   6311:                                for(int i = 0;; i++) {
                   6312:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   6313:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   6314:                                        
                   6315:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   6316:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   6317:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   6318:                                        
                   6319:                                        if(lo == 0 && hi == 0) {
                   6320:                                                break;
                   6321:                                        }
                   6322:                                }
                   6323:                        }
                   6324:                } else if(REG8(CL) == 0x7f) {
                   6325:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   6326:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   6327:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   6328:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   6329:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   6330:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   6331:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   6332:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   6333:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   6334:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   6335:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   6336:                } else {
                   6337:                        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));
                   6338:                        REG16(AX) = 0x01; // invalid function
                   6339:                        m_CF = 1;
                   6340:                }
                   6341:                break;
                   6342:        case 0x0d: // generic block device request
                   6343:                if(REG8(CL) == 0x40) {
                   6344:                        // set device parameters
                   6345:                } else if(REG8(CL) == 0x46) {
                   6346:                        // set volume serial number
                   6347:                } else if(REG8(CL) == 0x4a) {
                   6348:                        // lock logical volume
                   6349:                } else if(REG8(CL) == 0x4b) {
                   6350:                        // lock physical volume
                   6351:                } else if(REG8(CL) == 0x60) {
                   6352:                        // get device parameters
                   6353:                        char dev[] = "\\\\.\\A:";
                   6354:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   6355:                        
                   6356:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6357:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6358:                                DISK_GEOMETRY geo;
                   6359:                                DWORD dwSize;
                   6360:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6361:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   6362:                                        switch(geo.MediaType) {
                   6363:                                        case F5_360_512:
                   6364:                                        case F5_320_512:
                   6365:                                        case F5_320_1024:
                   6366:                                        case F5_180_512:
                   6367:                                        case F5_160_512:
                   6368:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   6369:                                                break;
                   6370:                                        case F5_1Pt2_512:
                   6371:                                        case F3_1Pt2_512:
                   6372:                                        case F3_1Pt23_1024:
                   6373:                                        case F5_1Pt23_1024:
                   6374:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   6375:                                                break;
                   6376:                                        case F3_720_512:
                   6377:                                        case F3_640_512:
                   6378:                                        case F5_640_512:
                   6379:                                        case F5_720_512:
                   6380:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   6381:                                                break;
                   6382:                                        case F8_256_128:
                   6383:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   6384:                                                break;
                   6385:                                        case FixedMedia:
                   6386:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   6387:                                                break;
                   6388:                                        case F3_1Pt44_512:
                   6389:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   6390:                                                break;
                   6391:                                        case F3_2Pt88_512:
                   6392:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   6393:                                                break;
                   6394:                                        default:
                   6395:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   6396: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   6397:                                                break;
                   6398:                                        }
                   6399:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   6400:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   6401:                                        switch(geo.MediaType) {
                   6402:                                        case F5_360_512:
                   6403:                                        case F5_320_512:
                   6404:                                        case F5_320_1024:
                   6405:                                        case F5_180_512:
                   6406:                                        case F5_160_512:
                   6407:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   6408:                                                break;
                   6409:                                        default:
                   6410:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   6411:                                                break;
                   6412:                                        }
                   6413:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   6414:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   6415:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   6416:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   6417:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   6418:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   6419:                                        switch(geo.MediaType) {
                   6420:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6421:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   6422:                                                break;
                   6423:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6424:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   6425:                                                break;
                   6426:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6427:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   6428:                                                break;
                   6429:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6430:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   6431:                                                break;
                   6432:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6433:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6434:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   6435:                                                break;
                   6436:                                        case FixedMedia:        // hard disk
                   6437:                                        case RemovableMedia:
                   6438:                                        case Unknown:
                   6439:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   6440:                                                break;
                   6441:                                        default:
                   6442:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   6443:                                                break;
                   6444:                                        }
                   6445:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   6446:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   6447:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   6448:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   6449:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   6450:                                        // 21h  BYTE    device type
                   6451:                                        // 22h  WORD    device attributes (removable or not, etc)
                   6452:                                } else {
                   6453:                                        REG16(AX) = 0x0f; // invalid drive
                   6454:                                        m_CF = 1;
                   6455:                                }
                   6456:                                CloseHandle(hFile);
                   6457:                        } else {
                   6458:                                REG16(AX) = 0x0f; // invalid drive
                   6459:                                m_CF = 1;
                   6460:                        }
                   6461:                } else if(REG8(CL) == 0x66) {
                   6462:                        // get volume serial number
                   6463:                        char path[] = "A:\\";
                   6464:                        char volume_label[MAX_PATH];
                   6465:                        DWORD serial_number = 0;
                   6466:                        char file_system[MAX_PATH];
                   6467:                        
                   6468:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   6469:                        
                   6470:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   6471:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   6472:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   6473:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   6474:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   6475:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   6476:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   6477:                        } else {
                   6478:                                REG16(AX) = 0x0f; // invalid drive
                   6479:                                m_CF = 1;
                   6480:                        }
                   6481:                } else if(REG8(CL) == 0x67) {
                   6482:                        // get access flag
                   6483:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   6484:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   6485:                } else if(REG8(CL) == 0x68) {
                   6486:                        // sense media type
                   6487:                        char dev[64];
                   6488:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   6489:                        
                   6490:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   6491:                        if(hFile != INVALID_HANDLE_VALUE) {
                   6492:                                DISK_GEOMETRY geo;
                   6493:                                DWORD dwSize;
                   6494:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6495:                                        switch(geo.MediaType) {
                   6496:                                        case F3_720_512:
                   6497:                                        case F5_720_512:
                   6498:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6499:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   6500:                                                break;
                   6501:                                        case F3_1Pt44_512:
                   6502:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6503:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   6504:                                                break;
                   6505:                                        case F3_2Pt88_512:
                   6506:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   6507:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   6508:                                                break;
                   6509:                                        default:
                   6510:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   6511:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   6512:                                                break;
                   6513:                                        }
                   6514:                                } else {
                   6515:                                        REG16(AX) = 0x0f; // invalid drive
                   6516:                                        m_CF = 1;
                   6517:                                }
                   6518:                                CloseHandle(hFile);
                   6519:                        } else {
                   6520:                                REG16(AX) = 0x0f; // invalid drive
                   6521:                                m_CF = 1;
                   6522:                        }
                   6523:                } else if(REG8(CL) == 0x6a) {
                   6524:                        // unlock logical volume
                   6525:                } else if(REG8(CL) == 0x6b) {
                   6526:                        // unlock physical volume
                   6527:                } else {
                   6528:                        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));
                   6529:                        REG16(AX) = 0x01; // invalid function
                   6530:                        m_CF = 1;
                   6531:                }
                   6532:                break;
                   6533:        case 0x0e: // get logical drive map
                   6534:                {
                   6535:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   6536:                        if(!(GetLogicalDrives() & bits)) {
                   6537:                                REG16(AX) = 0x0f; // invalid drive
                   6538:                                m_CF = 1;
                   6539:                        } else {
                   6540:                                REG8(AL) = 0;
                   6541:                        }
                   6542:                }
                   6543:                break;
                   6544:        case 0x0f: // set logical drive map
                   6545:                {
                   6546:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   6547:                        if(!(GetLogicalDrives() & bits)) {
                   6548:                                REG16(AX) = 0x0f; // invalid drive
                   6549:                                m_CF = 1;
                   6550:                        }
                   6551:                }
                   6552:                break;
                   6553:        case 0x10: // query generic ioctrl capability (handle)
                   6554:                switch(REG8(CL)) {
                   6555:                case 0x45:
                   6556:                case 0x4a:
                   6557:                case 0x65:
                   6558:                case 0x6a:
                   6559:                case 0x7f:
                   6560:                        REG16(AX) = 0x0000; // supported
                   6561:                        break;
                   6562:                default:
                   6563:                        REG8(AL) = 0x01; // ioctl capability not available
                   6564:                        m_CF = 1;
                   6565:                        break;
                   6566:                }
                   6567:                break;
                   6568:        case 0x11: // query generic ioctrl capability (drive)
                   6569:                switch(REG8(CL)) {
                   6570:                case 0x40:
                   6571:                case 0x46:
                   6572:                case 0x4a:
                   6573:                case 0x4b:
                   6574:                case 0x60:
                   6575:                case 0x66:
                   6576:                case 0x67:
                   6577:                case 0x68:
                   6578:                case 0x6a:
                   6579:                case 0x6b:
                   6580:                        REG16(AX) = 0x0000; // supported
                   6581:                        break;
                   6582:                default:
                   6583:                        REG8(AL) = 0x01; // ioctl capability not available
                   6584:                        m_CF = 1;
                   6585:                        break;
                   6586:                }
                   6587:                break;
                   6588:        case 0x12: // determine dos type
                   6589:        case 0x51: // concurrent dos v3.2+ - installation check
                   6590:        case 0x52: // determine dos type/get dr dos versuin
                   6591:                REG16(AX) = 0x01; // this  is not DR-DOS
                   6592:                m_CF = 1;
                   6593:                break;
1.1       root     6594:        default:
1.1.1.22  root     6595:                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     6596:                REG16(AX) = 0x01;
1.1.1.3   root     6597:                m_CF = 1;
1.1       root     6598:                break;
                   6599:        }
                   6600: }
                   6601: 
                   6602: inline void msdos_int_21h_45h()
                   6603: {
                   6604:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6605:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     6606:        
1.1.1.20  root     6607:        if(fd < process->max_files && file_handler[fd].valid) {
                   6608:                int dup_fd = _dup(fd);
                   6609:                if(dup_fd != -1) {
                   6610:                        REG16(AX) = dup_fd;
                   6611:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   6612: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   6613:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     6614:                } else {
                   6615:                        REG16(AX) = errno;
1.1.1.3   root     6616:                        m_CF = 1;
1.1       root     6617:                }
                   6618:        } else {
                   6619:                REG16(AX) = 0x06;
1.1.1.3   root     6620:                m_CF = 1;
1.1       root     6621:        }
                   6622: }
                   6623: 
                   6624: inline void msdos_int_21h_46h()
                   6625: {
                   6626:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     6627:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   6628:        int dup_fd = REG16(CX);
                   6629:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     6630:        
1.1.1.20  root     6631:        if(REG16(BX) == REG16(CX)) {
                   6632:                REG16(AX) = 0x06;
                   6633:                m_CF = 1;
                   6634:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   6635:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   6636:                        _close(tmp_fd);
                   6637:                        msdos_file_handler_close(tmp_fd);
                   6638:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   6639:                }
                   6640:                if(_dup2(fd, dup_fd) != -1) {
                   6641:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   6642: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   6643:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     6644:                } else {
                   6645:                        REG16(AX) = errno;
1.1.1.3   root     6646:                        m_CF = 1;
1.1       root     6647:                }
                   6648:        } else {
                   6649:                REG16(AX) = 0x06;
1.1.1.3   root     6650:                m_CF = 1;
1.1       root     6651:        }
                   6652: }
                   6653: 
                   6654: inline void msdos_int_21h_47h(int lfn)
                   6655: {
                   6656:        char path[MAX_PATH];
                   6657:        
                   6658:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   6659:                if(path[1] == ':') {
                   6660:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     6661:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     6662:                } else {
1.1.1.3   root     6663:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     6664:                }
                   6665:        } else {
                   6666:                REG16(AX) = errno;
1.1.1.3   root     6667:                m_CF = 1;
1.1       root     6668:        }
                   6669: }
                   6670: 
                   6671: inline void msdos_int_21h_48h()
                   6672: {
1.1.1.19  root     6673:        int seg, umb_linked;
1.1       root     6674:        
1.1.1.8   root     6675:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     6676:                // unlink umb not to allocate memory in umb
                   6677:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6678:                        msdos_mem_unlink_umb();
                   6679:                }
1.1.1.8   root     6680:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   6681:                        REG16(AX) = seg;
                   6682:                } else {
                   6683:                        REG16(AX) = 0x08;
                   6684:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   6685:                        m_CF = 1;
                   6686:                }
1.1.1.19  root     6687:                if(umb_linked != 0) {
                   6688:                        msdos_mem_link_umb();
                   6689:                }
1.1.1.8   root     6690:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   6691:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   6692:                        REG16(AX) = seg;
                   6693:                } else {
                   6694:                        REG16(AX) = 0x08;
                   6695:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   6696:                        m_CF = 1;
                   6697:                }
                   6698:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   6699:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   6700:                        REG16(AX) = seg;
                   6701:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   6702:                        REG16(AX) = seg;
                   6703:                } else {
                   6704:                        REG16(AX) = 0x08;
                   6705:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   6706:                        m_CF = 1;
                   6707:                }
1.1       root     6708:        }
                   6709: }
                   6710: 
                   6711: inline void msdos_int_21h_49h()
                   6712: {
1.1.1.14  root     6713:        int mcb_seg = SREG(ES) - 1;
                   6714:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6715:        
                   6716:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   6717:                msdos_mem_free(SREG(ES));
                   6718:        } else {
                   6719:                REG16(AX) = 9;
                   6720:                m_CF = 1;
                   6721:        }
1.1       root     6722: }
                   6723: 
                   6724: inline void msdos_int_21h_4ah()
                   6725: {
1.1.1.14  root     6726:        int mcb_seg = SREG(ES) - 1;
                   6727:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     6728:        int max_paragraphs;
                   6729:        
1.1.1.14  root     6730:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   6731:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   6732:                        REG16(AX) = 0x08;
                   6733:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   6734:                        m_CF = 1;
                   6735:                }
                   6736:        } else {
                   6737:                REG16(AX) = 7;
1.1.1.3   root     6738:                m_CF = 1;
1.1       root     6739:        }
                   6740: }
                   6741: 
                   6742: inline void msdos_int_21h_4bh()
                   6743: {
1.1.1.3   root     6744:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   6745:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     6746:        
                   6747:        switch(REG8(AL)) {
                   6748:        case 0x00:
                   6749:        case 0x01:
                   6750:                if(msdos_process_exec(command, param, REG8(AL))) {
                   6751:                        REG16(AX) = 0x02;
1.1.1.3   root     6752:                        m_CF = 1;
1.1       root     6753:                }
                   6754:                break;
1.1.1.14  root     6755:        case 0x03:
                   6756:                {
                   6757:                        int fd;
                   6758:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   6759:                                REG16(AX) = 0x02;
                   6760:                                m_CF = 1;
                   6761:                                break;
                   6762:                        }
                   6763:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   6764:                        _close(fd);
                   6765:                        
                   6766:                        UINT16 *overlay = (UINT16 *)param;
                   6767:                        
                   6768:                        // check exe header
                   6769:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   6770:                        int header_size = 0;
                   6771:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6772:                                header_size = header->header_size * 16;
                   6773:                                // relocation
                   6774:                                int start_seg = overlay[1];
                   6775:                                for(int i = 0; i < header->relocations; i++) {
                   6776:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6777:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6778:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6779:                                }
                   6780:                        }
                   6781:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   6782:                }
                   6783:                break;
1.1       root     6784:        default:
1.1.1.22  root     6785:                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     6786:                REG16(AX) = 0x01;
1.1.1.3   root     6787:                m_CF = 1;
1.1       root     6788:                break;
                   6789:        }
                   6790: }
                   6791: 
                   6792: inline void msdos_int_21h_4ch()
                   6793: {
                   6794:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   6795: }
                   6796: 
                   6797: inline void msdos_int_21h_4dh()
                   6798: {
                   6799:        REG16(AX) = retval;
                   6800: }
                   6801: 
                   6802: inline void msdos_int_21h_4eh()
                   6803: {
                   6804:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     6805:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6806:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     6807:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     6808:        WIN32_FIND_DATA fd;
                   6809:        
1.1.1.14  root     6810:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   6811:        find->find_magic = FIND_MAGIC;
                   6812:        find->dta_index = dtainfo - dtalist;
1.1       root     6813:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     6814:        dtainfo->allowable_mask = REG8(CL);
                   6815:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     6816:        
1.1.1.14  root     6817:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   6818:                dtainfo->allowable_mask &= ~8;
1.1       root     6819:        }
1.1.1.14  root     6820:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   6821:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     6822:                      !msdos_find_file_has_8dot3name(&fd)) {
                   6823:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   6824:                                FindClose(dtainfo->find_handle);
                   6825:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     6826:                                break;
                   6827:                        }
                   6828:                }
                   6829:        }
1.1.1.13  root     6830:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     6831:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   6832:                msdos_find_file_conv_local_time(&fd);
                   6833:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   6834:                find->size = fd.nFileSizeLow;
1.1.1.13  root     6835:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     6836:                REG16(AX) = 0;
1.1.1.14  root     6837:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     6838:                find->attrib = 8;
                   6839:                find->size = 0;
                   6840:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     6841:                dtainfo->allowable_mask &= ~8;
1.1       root     6842:                REG16(AX) = 0;
                   6843:        } else {
                   6844:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     6845:                m_CF = 1;
1.1       root     6846:        }
                   6847: }
                   6848: 
                   6849: inline void msdos_int_21h_4fh()
                   6850: {
                   6851:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     6852:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   6853:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     6854:        WIN32_FIND_DATA fd;
                   6855:        
1.1.1.14  root     6856:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   6857:                REG16(AX) = 0x12;
                   6858:                m_CF = 1;
                   6859:                return;
                   6860:        }
                   6861:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     6862:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   6863:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     6864:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     6865:                              !msdos_find_file_has_8dot3name(&fd)) {
                   6866:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   6867:                                        FindClose(dtainfo->find_handle);
                   6868:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     6869:                                        break;
                   6870:                                }
                   6871:                        }
                   6872:                } else {
1.1.1.13  root     6873:                        FindClose(dtainfo->find_handle);
                   6874:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     6875:                }
                   6876:        }
1.1.1.13  root     6877:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     6878:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   6879:                msdos_find_file_conv_local_time(&fd);
                   6880:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   6881:                find->size = fd.nFileSizeLow;
1.1.1.13  root     6882:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     6883:                REG16(AX) = 0;
1.1.1.14  root     6884:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     6885:                find->attrib = 8;
                   6886:                find->size = 0;
                   6887:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     6888:                dtainfo->allowable_mask &= ~8;
1.1       root     6889:                REG16(AX) = 0;
                   6890:        } else {
                   6891:                REG16(AX) = 0x12;
1.1.1.3   root     6892:                m_CF = 1;
1.1       root     6893:        }
                   6894: }
                   6895: 
                   6896: inline void msdos_int_21h_50h()
                   6897: {
1.1.1.8   root     6898:        if(current_psp != REG16(BX)) {
                   6899:                process_t *process = msdos_process_info_get(current_psp);
                   6900:                if(process != NULL) {
                   6901:                        process->psp = REG16(BX);
                   6902:                }
                   6903:                current_psp = REG16(BX);
1.1.1.23  root     6904:                msdos_sda_update(current_psp);
1.1.1.8   root     6905:        }
1.1       root     6906: }
                   6907: 
                   6908: inline void msdos_int_21h_51h()
                   6909: {
                   6910:        REG16(BX) = current_psp;
                   6911: }
                   6912: 
                   6913: inline void msdos_int_21h_52h()
                   6914: {
1.1.1.25! root     6915:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     6916:        i386_load_segment_descriptor(ES);
1.1.1.25! root     6917:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     6918: }
                   6919: 
                   6920: inline void msdos_int_21h_54h()
                   6921: {
                   6922:        process_t *process = msdos_process_info_get(current_psp);
                   6923:        
                   6924:        REG8(AL) = process->verify;
                   6925: }
                   6926: 
                   6927: inline void msdos_int_21h_55h()
                   6928: {
                   6929:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   6930:        
                   6931:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   6932:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6933:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6934:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6935:        psp->parent_psp = current_psp;
                   6936: }
                   6937: 
                   6938: inline void msdos_int_21h_56h(int lfn)
                   6939: {
                   6940:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     6941:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   6942:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     6943:        
                   6944:        if(rename(src, dst)) {
                   6945:                REG16(AX) = errno;
1.1.1.3   root     6946:                m_CF = 1;
1.1       root     6947:        }
                   6948: }
                   6949: 
                   6950: inline void msdos_int_21h_57h()
                   6951: {
                   6952:        FILETIME time, local;
1.1.1.14  root     6953:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     6954:        HANDLE hHandle;
1.1       root     6955:        
1.1.1.21  root     6956:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     6957:                REG16(AX) = (UINT16)GetLastError();
                   6958:                m_CF = 1;
                   6959:                return;
                   6960:        }
                   6961:        ctime = atime = mtime = NULL;
                   6962:        
1.1       root     6963:        switch(REG8(AL)) {
                   6964:        case 0x00:
1.1.1.6   root     6965:        case 0x01:
1.1.1.14  root     6966:                mtime = &time;
1.1.1.6   root     6967:                break;
                   6968:        case 0x04:
                   6969:        case 0x05:
1.1.1.14  root     6970:                atime = &time;
1.1       root     6971:                break;
1.1.1.6   root     6972:        case 0x06:
                   6973:        case 0x07:
1.1.1.14  root     6974:                ctime = &time;
                   6975:                break;
                   6976:        default:
1.1.1.22  root     6977:                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     6978:                REG16(AX) = 0x01;
                   6979:                m_CF = 1;
                   6980:                return;
                   6981:        }
                   6982:        if(REG8(AL) & 1) {
1.1       root     6983:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   6984:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     6985:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     6986:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     6987:                        m_CF = 1;
1.1       root     6988:                }
1.1.1.14  root     6989:        } else {
1.1.1.21  root     6990:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     6991:                        // assume a device and use the current time
                   6992:                        GetSystemTimeAsFileTime(&time);
                   6993:                }
                   6994:                FileTimeToLocalFileTime(&time, &local);
                   6995:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     6996:        }
                   6997: }
                   6998: 
                   6999: inline void msdos_int_21h_58h()
                   7000: {
                   7001:        switch(REG8(AL)) {
                   7002:        case 0x00:
1.1.1.7   root     7003:                REG16(AX) = malloc_strategy;
                   7004:                break;
                   7005:        case 0x01:
1.1.1.24  root     7006: //             switch(REG16(BX)) {
                   7007:                switch(REG8(BL)) {
1.1.1.7   root     7008:                case 0x0000:
                   7009:                case 0x0001:
                   7010:                case 0x0002:
                   7011:                case 0x0040:
                   7012:                case 0x0041:
                   7013:                case 0x0042:
                   7014:                case 0x0080:
                   7015:                case 0x0081:
                   7016:                case 0x0082:
                   7017:                        malloc_strategy = REG16(BX);
1.1.1.23  root     7018:                        msdos_sda_update(current_psp);
1.1.1.7   root     7019:                        break;
                   7020:                default:
1.1.1.22  root     7021:                        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     7022:                        REG16(AX) = 0x01;
                   7023:                        m_CF = 1;
                   7024:                        break;
                   7025:                }
                   7026:                break;
                   7027:        case 0x02:
1.1.1.19  root     7028:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     7029:                break;
                   7030:        case 0x03:
1.1.1.24  root     7031: //             switch(REG16(BX)) {
                   7032:                switch(REG8(BL)) {
1.1.1.7   root     7033:                case 0x0000:
1.1.1.19  root     7034:                        msdos_mem_unlink_umb();
                   7035:                        break;
1.1.1.7   root     7036:                case 0x0001:
1.1.1.19  root     7037:                        msdos_mem_link_umb();
1.1.1.7   root     7038:                        break;
                   7039:                default:
1.1.1.22  root     7040:                        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     7041:                        REG16(AX) = 0x01;
                   7042:                        m_CF = 1;
                   7043:                        break;
                   7044:                }
1.1       root     7045:                break;
                   7046:        default:
1.1.1.22  root     7047:                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     7048:                REG16(AX) = 0x01;
1.1.1.3   root     7049:                m_CF = 1;
1.1       root     7050:                break;
                   7051:        }
                   7052: }
                   7053: 
                   7054: inline void msdos_int_21h_59h()
                   7055: {
1.1.1.23  root     7056:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   7057:        
                   7058:        REG16(AX) = sda->extended_error_code;
                   7059:        REG8(BH) = sda->error_class;
                   7060:        REG8(BL) = sda->suggested_action;
                   7061:        REG8(CH) = sda->locus_of_last_error;
1.1       root     7062: }
                   7063: 
                   7064: inline void msdos_int_21h_5ah()
                   7065: {
1.1.1.3   root     7066:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7067:        int len = strlen(path);
                   7068:        char tmp[MAX_PATH];
                   7069:        
                   7070:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   7071:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7072:                
                   7073:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7074:                REG16(AX) = fd;
                   7075:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7076:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7077:                
                   7078:                strcpy(path, tmp);
                   7079:                int dx = REG16(DX) + len;
1.1.1.3   root     7080:                int ds = SREG(DS);
1.1       root     7081:                while(dx > 0xffff) {
                   7082:                        dx -= 0x10;
                   7083:                        ds++;
                   7084:                }
                   7085:                REG16(DX) = dx;
1.1.1.3   root     7086:                SREG(DS) = ds;
                   7087:                i386_load_segment_descriptor(DS);
1.1       root     7088:        } else {
                   7089:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7090:                m_CF = 1;
1.1       root     7091:        }
                   7092: }
                   7093: 
                   7094: inline void msdos_int_21h_5bh()
                   7095: {
1.1.1.3   root     7096:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     7097:        
1.1.1.24  root     7098:        if(msdos_is_existing_file(path)) {
1.1       root     7099:                // already exists
                   7100:                REG16(AX) = 0x50;
1.1.1.3   root     7101:                m_CF = 1;
1.1       root     7102:        } else {
                   7103:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7104:                
                   7105:                if(fd != -1) {
                   7106:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7107:                        REG16(AX) = fd;
                   7108:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7109:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7110:                } else {
                   7111:                        REG16(AX) = errno;
1.1.1.3   root     7112:                        m_CF = 1;
1.1       root     7113:                }
                   7114:        }
                   7115: }
                   7116: 
                   7117: inline void msdos_int_21h_5ch()
                   7118: {
                   7119:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7120:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7121:        
1.1.1.20  root     7122:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     7123:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   7124:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     7125:                        UINT32 pos = _tell(fd);
                   7126:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   7127:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     7128:                                REG16(AX) = errno;
1.1.1.3   root     7129:                                m_CF = 1;
1.1       root     7130:                        }
1.1.1.20  root     7131:                        _lseek(fd, pos, SEEK_SET);
1.1       root     7132:                        // some seconds may be passed in _locking()
                   7133:                        hardware_update();
                   7134:                } else {
                   7135:                        REG16(AX) = 0x01;
1.1.1.3   root     7136:                        m_CF = 1;
1.1       root     7137:                }
                   7138:        } else {
                   7139:                REG16(AX) = 0x06;
1.1.1.3   root     7140:                m_CF = 1;
1.1       root     7141:        }
                   7142: }
                   7143: 
1.1.1.22  root     7144: inline void msdos_int_21h_5dh()
                   7145: {
                   7146:        switch(REG8(AL)) {
                   7147:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     7148:                SREG(DS) = (SDA_TOP >> 4);
                   7149:                i386_load_segment_descriptor(DS);
                   7150:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   7151:                REG16(CX) = 0x80;
                   7152:                REG16(DX) = 0x1a;
                   7153:                break;
                   7154:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     7155:                REG16(AX) = 0x01;
                   7156:                m_CF = 1;
                   7157:                break;
                   7158:        case 0x08: // set redirected printer mode
                   7159:        case 0x09: // flush redirected printer output
                   7160:        case 0x0a: // set extended error information
                   7161:                break;
                   7162:        default:
                   7163:                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));
                   7164:                REG16(AX) = 0x01;
                   7165:                m_CF = 1;
                   7166:                break;
                   7167:        }
                   7168: }
                   7169: 
1.1       root     7170: inline void msdos_int_21h_60h(int lfn)
                   7171: {
1.1.1.14  root     7172:        char full[MAX_PATH], *path;
                   7173:        
1.1       root     7174:        if(lfn) {
1.1.1.14  root     7175:                char *name;
                   7176:                *full = '\0';
1.1.1.3   root     7177:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     7178:                switch(REG8(CL)) {
                   7179:                case 1:
                   7180:                        GetShortPathName(full, full, MAX_PATH);
                   7181:                        my_strupr(full);
                   7182:                        break;
                   7183:                case 2:
                   7184:                        GetLongPathName(full, full, MAX_PATH);
                   7185:                        break;
                   7186:                }
                   7187:                path = full;
                   7188:        } else {
                   7189:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   7190:        }
                   7191:        if(*path != '\0') {
                   7192:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     7193:        } else {
1.1.1.14  root     7194:                REG16(AX) = (UINT16)GetLastError();
                   7195:                m_CF = 1;
1.1       root     7196:        }
                   7197: }
                   7198: 
                   7199: inline void msdos_int_21h_61h()
                   7200: {
                   7201:        REG8(AL) = 0;
                   7202: }
                   7203: 
                   7204: inline void msdos_int_21h_62h()
                   7205: {
                   7206:        REG16(BX) = current_psp;
                   7207: }
                   7208: 
                   7209: inline void msdos_int_21h_63h()
                   7210: {
                   7211:        switch(REG8(AL)) {
                   7212:        case 0x00:
1.1.1.3   root     7213:                SREG(DS) = (DBCS_TABLE >> 4);
                   7214:                i386_load_segment_descriptor(DS);
1.1       root     7215:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   7216:                REG8(AL) = 0x00;
                   7217:                break;
1.1.1.22  root     7218:        case 0x01: // set korean input mode
                   7219:        case 0x02: // get korean input mode
                   7220:                REG8(AL) = 0xff; // not supported
                   7221:                break;
1.1       root     7222:        default:
1.1.1.22  root     7223:                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     7224:                REG16(AX) = 0x01;
1.1.1.3   root     7225:                m_CF = 1;
1.1       root     7226:                break;
                   7227:        }
                   7228: }
                   7229: 
1.1.1.25! root     7230: UINT16 get_extended_country_info(UINT8 func)
1.1       root     7231: {
1.1.1.25! root     7232:        switch(func) {
1.1.1.17  root     7233:        case 0x01:
                   7234:                if(REG16(CX) >= 5) {
1.1.1.19  root     7235:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     7236:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   7237:                                REG16(CX) = sizeof(data);
                   7238:                        ZeroMemory(data, sizeof(data));
                   7239:                        data[0] = 0x01;
                   7240:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     7241:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     7242:                        *(UINT16 *)(data + 5) = active_code_page;
                   7243:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25! root     7244: //                     REG16(AX) = active_code_page;
1.1.1.17  root     7245:                } else {
1.1.1.25! root     7246:                        return(0x08); // insufficient memory
1.1.1.17  root     7247:                }
                   7248:                break;
                   7249:        case 0x02:
                   7250:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   7251:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   7252:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25! root     7253: //             REG16(AX) = active_code_page;
1.1.1.17  root     7254:                REG16(CX) = 0x05;
                   7255:                break;
1.1.1.23  root     7256:        case 0x03:
                   7257:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   7258:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   7259:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25! root     7260: //             REG16(AX) = active_code_page;
1.1.1.23  root     7261:                REG16(CX) = 0x05;
                   7262:                break;
1.1.1.17  root     7263:        case 0x04:
                   7264:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   7265:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   7266:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25! root     7267: //             REG16(AX) = active_code_page;
1.1.1.17  root     7268:                REG16(CX) = 0x05;
                   7269:                break;
                   7270:        case 0x05:
                   7271:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   7272:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   7273:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25! root     7274: //             REG16(AX) = active_code_page;
1.1.1.17  root     7275:                REG16(CX) = 0x05;
                   7276:                break;
                   7277:        case 0x06:
                   7278:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   7279:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   7280:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25! root     7281: //             REG16(AX) = active_code_page;
1.1.1.17  root     7282:                REG16(CX) = 0x05;
                   7283:                break;
1.1       root     7284:        case 0x07:
1.1.1.3   root     7285:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   7286:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   7287:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25! root     7288: //             REG16(AX) = active_code_page;
1.1       root     7289:                REG16(CX) = 0x05;
                   7290:                break;
1.1.1.25! root     7291:        default:
        !          7292:                return(0x01); // function number invalid
        !          7293:        }
        !          7294:        return(0x00);
        !          7295: }
        !          7296: 
        !          7297: inline void msdos_int_21h_65h()
        !          7298: {
        !          7299:        char tmp[0x10000];
        !          7300:        
        !          7301:        switch(REG8(AL)) {
        !          7302:        case 0x01:
        !          7303:        case 0x02:
        !          7304:        case 0x03:
        !          7305:        case 0x04:
        !          7306:        case 0x05:
        !          7307:        case 0x06:
        !          7308:        case 0x07:
        !          7309:                {
        !          7310:                        UINT16 result = get_extended_country_info(REG8(AL));
        !          7311:                        if(result) {
        !          7312:                                REG16(AX) = result;
        !          7313:                                m_CF = 1;
        !          7314:                        } else {
        !          7315:                                REG16(AX) = active_code_page; // FIXME: is this correct???
        !          7316:                        }
        !          7317:                }
        !          7318:                break;
1.1       root     7319:        case 0x20:
1.1.1.25! root     7320:        case 0xa0:
1.1.1.19  root     7321:                memset(tmp, 0, sizeof(tmp));
                   7322:                tmp[0] = REG8(DL);
1.1       root     7323:                my_strupr(tmp);
                   7324:                REG8(DL) = tmp[0];
                   7325:                break;
                   7326:        case 0x21:
1.1.1.25! root     7327:        case 0xa1:
1.1       root     7328:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     7329:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     7330:                my_strupr(tmp);
1.1.1.3   root     7331:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     7332:                break;
                   7333:        case 0x22:
1.1.1.25! root     7334:        case 0xa2:
1.1.1.3   root     7335:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     7336:                break;
1.1.1.25! root     7337:        case 0x23:
        !          7338:                // FIXME: need to check multi-byte (kanji) charactre?
        !          7339:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
        !          7340:                        // 8278h/8299h: multi-byte (kanji) Y and y
        !          7341:                        REG16(AX) = 0x00;
        !          7342:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
        !          7343:                        // 826dh/828eh: multi-byte (kanji) N and n
        !          7344:                        REG16(AX) = 0x01;
        !          7345:                } else {
        !          7346:                        REG16(AX) = 0x02;
        !          7347:                }
        !          7348:                break;
1.1       root     7349:        default:
1.1.1.22  root     7350:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     7351:                REG16(AX) = 0x01;
1.1.1.3   root     7352:                m_CF = 1;
1.1       root     7353:                break;
                   7354:        }
                   7355: }
                   7356: 
                   7357: inline void msdos_int_21h_66h()
                   7358: {
                   7359:        switch(REG8(AL)) {
                   7360:        case 0x01:
                   7361:                REG16(BX) = active_code_page;
                   7362:                REG16(DX) = system_code_page;
                   7363:                break;
                   7364:        case 0x02:
                   7365:                if(active_code_page == REG16(BX)) {
                   7366:                        REG16(AX) = 0xeb41;
                   7367:                } else if(_setmbcp(REG16(BX)) == 0) {
                   7368:                        active_code_page = REG16(BX);
1.1.1.17  root     7369:                        msdos_nls_tables_update();
1.1       root     7370:                        REG16(AX) = 0xeb41;
                   7371:                } else {
                   7372:                        REG16(AX) = 0x25;
1.1.1.3   root     7373:                        m_CF = 1;
1.1       root     7374:                }
                   7375:                break;
                   7376:        default:
1.1.1.22  root     7377:                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     7378:                REG16(AX) = 0x01;
1.1.1.3   root     7379:                m_CF = 1;
1.1       root     7380:                break;
                   7381:        }
                   7382: }
                   7383: 
                   7384: inline void msdos_int_21h_67h()
                   7385: {
                   7386:        process_t *process = msdos_process_info_get(current_psp);
                   7387:        
                   7388:        if(REG16(BX) <= MAX_FILES) {
                   7389:                process->max_files = max(REG16(BX), 20);
                   7390:        } else {
                   7391:                REG16(AX) = 0x08;
1.1.1.3   root     7392:                m_CF = 1;
1.1       root     7393:        }
                   7394: }
                   7395: 
                   7396: inline void msdos_int_21h_68h()
                   7397: {
                   7398:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7399:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     7400:        
1.1.1.20  root     7401:        if(fd < process->max_files && file_handler[fd].valid) {
                   7402:                // fflush(_fdopen(fd, ""));
1.1       root     7403:        } else {
                   7404:                REG16(AX) = 0x06;
1.1.1.3   root     7405:                m_CF = 1;
1.1       root     7406:        }
                   7407: }
                   7408: 
                   7409: inline void msdos_int_21h_69h()
                   7410: {
1.1.1.3   root     7411:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7412:        char path[] = "A:\\";
                   7413:        char volume_label[MAX_PATH];
                   7414:        DWORD serial_number = 0;
                   7415:        char file_system[MAX_PATH];
                   7416:        
                   7417:        if(REG8(BL) == 0) {
                   7418:                path[0] = 'A' + _getdrive() - 1;
                   7419:        } else {
                   7420:                path[0] = 'A' + REG8(BL) - 1;
                   7421:        }
                   7422:        
                   7423:        switch(REG8(AL)) {
                   7424:        case 0x00:
                   7425:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   7426:                        info->info_level = 0;
                   7427:                        info->serial_number = serial_number;
                   7428:                        memset(info->volume_label, 0x20, 11);
                   7429:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   7430:                        memset(info->file_system, 0x20, 8);
                   7431:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   7432:                } else {
                   7433:                        REG16(AX) = errno;
1.1.1.3   root     7434:                        m_CF = 1;
1.1       root     7435:                }
                   7436:                break;
                   7437:        case 0x01:
                   7438:                REG16(AX) = 0x03;
1.1.1.3   root     7439:                m_CF = 1;
1.1       root     7440:        }
                   7441: }
                   7442: 
                   7443: inline void msdos_int_21h_6ah()
                   7444: {
                   7445:        REG8(AH) = 0x68;
                   7446:        msdos_int_21h_68h();
                   7447: }
                   7448: 
                   7449: inline void msdos_int_21h_6bh()
                   7450: {
                   7451:        REG8(AL) = 0;
                   7452: }
                   7453: 
                   7454: inline void msdos_int_21h_6ch(int lfn)
                   7455: {
1.1.1.3   root     7456:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     7457:        int mode = REG8(BL) & 0x03;
                   7458:        
                   7459:        if(mode < 0x03) {
1.1.1.24  root     7460:                if(msdos_is_existing_file(path) || msdos_is_driver_name(path)) {
1.1       root     7461:                        // file exists
                   7462:                        if(REG8(DL) & 1) {
1.1.1.11  root     7463:                                int fd = -1;
                   7464:                                UINT16 info;
1.1       root     7465:                                
1.1.1.11  root     7466:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     7467:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     7468:                                        info = 0x80d3;
1.1.1.14  root     7469:                                } else if(msdos_is_nul_path(path)) {
                   7470:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7471:                                        info = 0x80d3;
1.1.1.24  root     7472:                                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     7473:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7474:                                        info = 0x80d3;
1.1.1.11  root     7475:                                } else {
1.1.1.13  root     7476:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     7477:                                        info = msdos_drive_number(path);
                   7478:                                }
1.1       root     7479:                                if(fd != -1) {
                   7480:                                        REG16(AX) = fd;
                   7481:                                        REG16(CX) = 1;
1.1.1.11  root     7482:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     7483:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7484:                                } else {
                   7485:                                        REG16(AX) = errno;
1.1.1.3   root     7486:                                        m_CF = 1;
1.1       root     7487:                                }
                   7488:                        } else if(REG8(DL) & 2) {
                   7489:                                int attr = GetFileAttributes(path);
                   7490:                                int fd = -1;
1.1.1.11  root     7491:                                UINT16 info;
1.1       root     7492:                                
1.1.1.11  root     7493:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     7494:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     7495:                                        info = 0x80d3;
1.1.1.14  root     7496:                                } else if(msdos_is_nul_path(path)) {
                   7497:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7498:                                        info = 0x80d3;
1.1.1.24  root     7499:                                } else if(msdos_is_driver_name(path)) {
1.1.1.20  root     7500:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   7501:                                        info = 0x80d3;
1.1       root     7502:                                } else {
                   7503:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     7504:                                        info = msdos_drive_number(path);
1.1       root     7505:                                }
                   7506:                                if(fd != -1) {
                   7507:                                        if(attr == -1) {
                   7508:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   7509:                                        }
                   7510:                                        SetFileAttributes(path, attr);
                   7511:                                        REG16(AX) = fd;
                   7512:                                        REG16(CX) = 3;
1.1.1.11  root     7513:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     7514:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7515:                                } else {
                   7516:                                        REG16(AX) = errno;
1.1.1.3   root     7517:                                        m_CF = 1;
1.1       root     7518:                                }
                   7519:                        } else {
                   7520:                                REG16(AX) = 0x50;
1.1.1.3   root     7521:                                m_CF = 1;
1.1       root     7522:                        }
                   7523:                } else {
                   7524:                        // file not exists
                   7525:                        if(REG8(DL) & 0x10) {
                   7526:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   7527:                                
                   7528:                                if(fd != -1) {
                   7529:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   7530:                                        REG16(AX) = fd;
                   7531:                                        REG16(CX) = 2;
                   7532:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     7533:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     7534:                                } else {
                   7535:                                        REG16(AX) = errno;
1.1.1.3   root     7536:                                        m_CF = 1;
1.1       root     7537:                                }
                   7538:                        } else {
                   7539:                                REG16(AX) = 0x02;
1.1.1.3   root     7540:                                m_CF = 1;
1.1       root     7541:                        }
                   7542:                }
                   7543:        } else {
                   7544:                REG16(AX) = 0x0c;
1.1.1.3   root     7545:                m_CF = 1;
1.1       root     7546:        }
                   7547: }
                   7548: 
                   7549: inline void msdos_int_21h_710dh()
                   7550: {
                   7551:        // reset drive
                   7552: }
                   7553: 
1.1.1.17  root     7554: inline void msdos_int_21h_7141h(int lfn)
                   7555: {
                   7556:        if(REG16(SI) == 0) {
                   7557:                msdos_int_21h_41h(lfn);
                   7558:                return;
                   7559:        }
                   7560:        if(REG16(SI) != 1) {
                   7561:                REG16(AX) = 5;
                   7562:                m_CF = 1;
                   7563:        }
                   7564:        /* wild card and matching attributes... */
                   7565:        char tmp[MAX_PATH * 2];
                   7566:        // copy search pathname (and quick check overrun)
                   7567:        ZeroMemory(tmp, sizeof(tmp));
                   7568:        tmp[MAX_PATH - 1] = '\0';
                   7569:        tmp[MAX_PATH] = 1;
                   7570:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   7571:        
                   7572:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   7573:                REG16(AX) = 1;
                   7574:                m_CF = 1;
                   7575:                return;
                   7576:        }
                   7577:        for(char *s = tmp; *s; ++s) {
                   7578:                if(*s == '/') {
                   7579:                        *s = '\\';
                   7580:                }
                   7581:        }
                   7582:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   7583:        if(tmp_name) {
                   7584:                ++tmp_name;
                   7585:        } else {
                   7586:                tmp_name = strchr(tmp, ':');
                   7587:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   7588:        }
                   7589:        
                   7590:        WIN32_FIND_DATAA fd;
                   7591:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   7592:        if(fh == INVALID_HANDLE_VALUE) {
                   7593:                REG16(AX) = 2;
                   7594:                m_CF = 1;
                   7595:                return;
                   7596:        }
                   7597:        do {
                   7598:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   7599:                        strcpy(tmp_name, fd.cFileName);
                   7600:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   7601:                                REG16(AX) = 5;
                   7602:                                m_CF = 1;
                   7603:                                break;
                   7604:                        }
                   7605:                }
                   7606:        } while(FindNextFileA(fh, &fd));
                   7607:        if(!m_CF) {
                   7608:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   7609:                        m_CF = 1;
                   7610:                        REG16(AX) = 2;
                   7611:                }
                   7612:        }
                   7613:        FindClose(fh);
                   7614: }
                   7615: 
1.1       root     7616: inline void msdos_int_21h_714eh()
                   7617: {
                   7618:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     7619:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   7620:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7621:        WIN32_FIND_DATA fd;
                   7622:        
1.1.1.13  root     7623:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   7624:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7625:                FindClose(dtainfo->find_handle);
                   7626:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7627:        }
                   7628:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     7629:        dtainfo->allowable_mask = REG8(CL);
                   7630:        dtainfo->required_mask = REG8(CH);
                   7631:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     7632:        
1.1.1.14  root     7633:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   7634:                dtainfo->allowable_mask &= ~8;
1.1       root     7635:        }
1.1.1.14  root     7636:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   7637:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     7638:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7639:                                FindClose(dtainfo->find_handle);
                   7640:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7641:                                break;
                   7642:                        }
                   7643:                }
                   7644:        }
1.1.1.13  root     7645:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7646:                find->attrib = fd.dwFileAttributes;
                   7647:                msdos_find_file_conv_local_time(&fd);
                   7648:                if(REG16(SI) == 0) {
                   7649:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   7650:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   7651:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   7652:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   7653:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   7654:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   7655:                } else {
                   7656:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   7657:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   7658:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   7659:                }
                   7660:                find->size_hi = fd.nFileSizeHigh;
                   7661:                find->size_lo = fd.nFileSizeLow;
                   7662:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     7663:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     7664:                REG16(AX) = dtainfo - dtalist + 1;
                   7665:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7666:                // volume label
                   7667:                find->attrib = 8;
                   7668:                find->size_hi = find->size_lo = 0;
                   7669:                strcpy(find->full_name, process->volume_label);
                   7670:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7671:                dtainfo->allowable_mask &= ~8;
                   7672:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     7673:        } else {
                   7674:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     7675:                m_CF = 1;
1.1       root     7676:        }
                   7677: }
                   7678: 
                   7679: inline void msdos_int_21h_714fh()
                   7680: {
                   7681:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     7682:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     7683:        WIN32_FIND_DATA fd;
                   7684:        
1.1.1.14  root     7685:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   7686:                REG16(AX) = 6;
1.1.1.13  root     7687:                m_CF = 1;
                   7688:                return;
                   7689:        }
1.1.1.14  root     7690:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     7691:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7692:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     7693:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     7694:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7695:                                        FindClose(dtainfo->find_handle);
                   7696:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7697:                                        break;
                   7698:                                }
                   7699:                        }
                   7700:                } else {
1.1.1.13  root     7701:                        FindClose(dtainfo->find_handle);
                   7702:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7703:                }
                   7704:        }
1.1.1.13  root     7705:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7706:                find->attrib = fd.dwFileAttributes;
                   7707:                msdos_find_file_conv_local_time(&fd);
                   7708:                if(REG16(SI) == 0) {
                   7709:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   7710:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   7711:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   7712:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   7713:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   7714:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   7715:                } else {
                   7716:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   7717:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   7718:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   7719:                }
                   7720:                find->size_hi = fd.nFileSizeHigh;
                   7721:                find->size_lo = fd.nFileSizeLow;
                   7722:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     7723:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     7724:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7725:                // volume label
                   7726:                find->attrib = 8;
                   7727:                find->size_hi = find->size_lo = 0;
                   7728:                strcpy(find->full_name, process->volume_label);
                   7729:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     7730:                dtainfo->allowable_mask &= ~8;
1.1       root     7731:        } else {
                   7732:                REG16(AX) = 0x12;
1.1.1.3   root     7733:                m_CF = 1;
1.1       root     7734:        }
                   7735: }
                   7736: 
                   7737: inline void msdos_int_21h_71a0h()
                   7738: {
                   7739:        DWORD max_component_len, file_sys_flag;
                   7740:        
1.1.1.14  root     7741:        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))) {
                   7742:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   7743:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     7744:                REG16(CX) = (UINT16)max_component_len;          // 255
                   7745:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   7746:        } else {
                   7747:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7748:                m_CF = 1;
1.1       root     7749:        }
                   7750: }
                   7751: 
                   7752: inline void msdos_int_21h_71a1h()
                   7753: {
1.1.1.14  root     7754:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   7755:                REG16(AX) = 6;
1.1.1.13  root     7756:                m_CF = 1;
                   7757:                return;
                   7758:        }
1.1.1.14  root     7759:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     7760:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7761:                FindClose(dtainfo->find_handle);
                   7762:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7763:        }
                   7764: }
                   7765: 
                   7766: inline void msdos_int_21h_71a6h()
                   7767: {
                   7768:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     7769:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   7770:        
1.1.1.3   root     7771:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     7772:        struct _stat64 status;
                   7773:        DWORD serial_number = 0;
                   7774:        
1.1.1.20  root     7775:        if(fd < process->max_files && file_handler[fd].valid) {
                   7776:                if(_fstat64(fd, &status) == 0) {
                   7777:                        if(file_handler[fd].path[1] == ':') {
1.1       root     7778:                                // NOTE: we need to consider the network file path "\\host\share\"
                   7779:                                char volume[] = "A:\\";
1.1.1.20  root     7780:                                volume[0] = file_handler[fd].path[1];
1.1       root     7781:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   7782:                        }
1.1.1.20  root     7783:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     7784:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   7785:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   7786:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   7787:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   7788:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   7789:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   7790:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   7791:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   7792:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   7793:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     7794:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     7795:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     7796:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     7797:                } else {
                   7798:                        REG16(AX) = errno;
1.1.1.3   root     7799:                        m_CF = 1;
1.1       root     7800:                }
                   7801:        } else {
                   7802:                REG16(AX) = 0x06;
1.1.1.3   root     7803:                m_CF = 1;
1.1       root     7804:        }
                   7805: }
                   7806: 
                   7807: inline void msdos_int_21h_71a7h()
                   7808: {
                   7809:        switch(REG8(BL)) {
                   7810:        case 0x00:
1.1.1.3   root     7811:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     7812:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7813:                        m_CF = 1;
1.1       root     7814:                }
                   7815:                break;
                   7816:        case 0x01:
                   7817:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     7818:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     7819:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     7820:                        m_CF = 1;
1.1       root     7821:                }
                   7822:                break;
                   7823:        default:
1.1.1.22  root     7824:                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     7825:                REG16(AX) = 0x01;
1.1.1.3   root     7826:                m_CF = 1;
1.1       root     7827:                break;
                   7828:        }
                   7829: }
                   7830: 
                   7831: inline void msdos_int_21h_71a8h()
                   7832: {
                   7833:        if(REG8(DH) == 0) {
                   7834:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     7835:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     7836:                memset(fcb, 0x20, sizeof(fcb));
                   7837:                int len = strlen(tmp);
1.1.1.21  root     7838:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     7839:                        if(tmp[i] == '.') {
                   7840:                                pos = 8;
                   7841:                        } else {
                   7842:                                if(msdos_lead_byte_check(tmp[i])) {
                   7843:                                        fcb[pos++] = tmp[i++];
                   7844:                                }
                   7845:                                fcb[pos++] = tmp[i];
                   7846:                        }
                   7847:                }
1.1.1.3   root     7848:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     7849:        } else {
1.1.1.3   root     7850:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     7851:        }
                   7852: }
                   7853: 
1.1.1.22  root     7854: inline void msdos_int_21h_71aah()
                   7855: {
                   7856:        char drv[] = "A:", path[MAX_PATH];
                   7857:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7858:        
                   7859:        if(REG8(BL) == 0) {
                   7860:                drv[0] = 'A' + _getdrive() - 1;
                   7861:        } else {
                   7862:                drv[0] = 'A' + REG8(BL) - 1;
                   7863:        }
                   7864:        switch(REG8(BH)) {
                   7865:        case 0x00:
                   7866:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   7867:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   7868:                        if(GetLogicalDrives() & bits) {
                   7869:                                REG16(AX) = 0x0f; // invalid drive
                   7870:                        } else {
                   7871:                                REG16(AX) = 0x03; // path not found
                   7872:                        }
                   7873:                        m_CF = 1;
                   7874:                }
                   7875:                break;
                   7876:        case 0x01:
                   7877:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   7878:                        REG16(AX) = 0x0f; // invalid drive
                   7879:                        m_CF = 1;
                   7880:                }
                   7881:                break;
                   7882:        case 0x02:
                   7883:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   7884:                        REG16(AX) = 0x0f; // invalid drive
                   7885:                        m_CF = 1;
                   7886:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   7887:                        REG16(AX) = 0x0f; // invalid drive
                   7888:                        m_CF = 1;
                   7889:                } else {
                   7890:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   7891:                }
                   7892:                break;
                   7893:        default:
                   7894:                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));
                   7895:                REG16(AX) = 0x01;
                   7896:                m_CF = 1;
                   7897:                break;
                   7898:        }
                   7899: }
                   7900: 
1.1.1.14  root     7901: inline void msdos_int_21h_7300h()
                   7902: {
                   7903:        if(REG8(AL) == 0) {
                   7904:                REG8(AL) = REG8(CL);
                   7905:                REG8(AH) = 0;
                   7906:        } else {
                   7907:                REG16(AX) = 0x01;
                   7908:                m_CF = 1;
                   7909:        }
                   7910: }
                   7911: 
                   7912: inline void msdos_int_21h_7302h()
                   7913: {
                   7914:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   7915:        UINT16 seg, ofs;
                   7916:        
                   7917:        if(REG16(CX) < 0x3f) {
                   7918:                REG8(AL) = 0x18;
                   7919:                m_CF = 1;
                   7920:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   7921:                REG8(AL) = 0xff;
                   7922:                m_CF = 1;
                   7923:        } else {
                   7924:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   7925:        }
                   7926: }
                   7927: 
1.1       root     7928: inline void msdos_int_21h_7303h()
                   7929: {
1.1.1.3   root     7930:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7931:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     7932:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   7933:        
                   7934:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   7935:                info->size_of_structure = sizeof(ext_space_info_t);
                   7936:                info->structure_version = 0;
                   7937:                info->sectors_per_cluster = sectors_per_cluster;
                   7938:                info->bytes_per_sector = bytes_per_sector;
                   7939:                info->available_clusters_on_drive = free_clusters;
                   7940:                info->total_clusters_on_drive = total_clusters;
                   7941:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   7942:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   7943:                info->available_allocation_units = free_clusters;       // ???
                   7944:                info->total_allocation_units = total_clusters;          // ???
                   7945:        } else {
                   7946:                REG16(AX) = errno;
1.1.1.3   root     7947:                m_CF = 1;
1.1       root     7948:        }
                   7949: }
                   7950: 
                   7951: inline void msdos_int_25h()
                   7952: {
                   7953:        UINT16 seg, ofs;
                   7954:        DWORD dwSize;
                   7955:        
1.1.1.3   root     7956: #if defined(HAS_I386)
                   7957:        I386OP(pushf)();
                   7958: #else
                   7959:        PREFIX86(_pushf());
                   7960: #endif
1.1       root     7961:        
                   7962:        if(!(REG8(AL) < 26)) {
                   7963:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     7964:                m_CF = 1;
1.1       root     7965:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   7966:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     7967:                m_CF = 1;
1.1       root     7968:        } else {
                   7969:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   7970:                char dev[64];
                   7971:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   7972:                
                   7973:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7974:                if(hFile == INVALID_HANDLE_VALUE) {
                   7975:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     7976:                        m_CF = 1;
1.1       root     7977:                } else {
1.1.1.19  root     7978:                        UINT32 top_sector  = REG16(DX);
                   7979:                        UINT16 sector_num  = REG16(CX);
                   7980:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   7981:                        
                   7982:                        if(sector_num == 0xffff) {
                   7983:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   7984:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   7985:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   7986:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   7987:                                buffer_addr = (seg << 4) + ofs;
                   7988:                        }
                   7989: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7990: //                             REG8(AL) = 0x02; // drive not ready
                   7991: //                             m_CF = 1;
                   7992: //                     } else 
                   7993:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     7994:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     7995:                                m_CF = 1;
1.1.1.19  root     7996:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     7997:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     7998:                                m_CF = 1;
1.1       root     7999:                        }
                   8000:                        CloseHandle(hFile);
                   8001:                }
                   8002:        }
                   8003: }
                   8004: 
                   8005: inline void msdos_int_26h()
                   8006: {
                   8007:        // this operation may cause serious damage for drives, so always returns error...
                   8008:        UINT16 seg, ofs;
                   8009:        DWORD dwSize;
                   8010:        
1.1.1.3   root     8011: #if defined(HAS_I386)
                   8012:        I386OP(pushf)();
                   8013: #else
                   8014:        PREFIX86(_pushf());
                   8015: #endif
1.1       root     8016:        
                   8017:        if(!(REG8(AL) < 26)) {
                   8018:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     8019:                m_CF = 1;
1.1       root     8020:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   8021:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8022:                m_CF = 1;
1.1       root     8023:        } else {
                   8024:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8025:                char dev[64];
                   8026:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   8027:                
                   8028:                if(dpb->media_type == 0xf8) {
                   8029:                        // this drive is not a floppy
1.1.1.6   root     8030: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   8031: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   8032: //                     }
1.1       root     8033:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8034:                        m_CF = 1;
1.1       root     8035:                } else {
                   8036:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   8037:                        if(hFile == INVALID_HANDLE_VALUE) {
                   8038:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8039:                                m_CF = 1;
1.1       root     8040:                        } else {
1.1.1.19  root     8041:                                UINT32 top_sector  = REG16(DX);
                   8042:                                UINT16 sector_num  = REG16(CX);
                   8043:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   8044:                                
                   8045:                                if(sector_num == 0xffff) {
                   8046:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   8047:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   8048:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   8049:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   8050:                                        buffer_addr = (seg << 4) + ofs;
                   8051:                                }
1.1       root     8052:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8053:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     8054:                                        m_CF = 1;
1.1.1.19  root     8055:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     8056:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     8057:                                        m_CF = 1;
1.1.1.19  root     8058:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     8059:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     8060:                                        m_CF = 1;
1.1       root     8061:                                }
                   8062:                                CloseHandle(hFile);
                   8063:                        }
                   8064:                }
                   8065:        }
                   8066: }
                   8067: 
                   8068: inline void msdos_int_27h()
                   8069: {
1.1.1.14  root     8070:        msdos_mem_realloc(SREG(CS), (REG16(DX) + 15) >> 4, NULL);
1.1.1.3   root     8071:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1.1.14  root     8072:        
                   8073:        // int_21h_4bh succeeded
                   8074:        m_CF = 0;
1.1       root     8075: }
                   8076: 
                   8077: inline void msdos_int_29h()
                   8078: {
1.1.1.14  root     8079: #if 1
                   8080:        // need to check escape sequences
1.1       root     8081:        msdos_putch(REG8(AL));
1.1.1.14  root     8082: #else
                   8083:        DWORD num;
                   8084:        vram_flush();
1.1.1.23  root     8085:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     8086:        cursor_moved = true;
                   8087: #endif
1.1       root     8088: }
                   8089: 
                   8090: inline void msdos_int_2eh()
                   8091: {
                   8092:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   8093:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     8094:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     8095:        char *token = my_strtok(tmp, " ");
                   8096:        strcpy(command, token);
                   8097:        strcpy(opt, token + strlen(token) + 1);
                   8098:        
                   8099:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   8100:        param->env_seg = 0;
                   8101:        param->cmd_line.w.l = 44;
                   8102:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   8103:        param->fcb1.w.l = 24;
                   8104:        param->fcb1.w.h = (WORK_TOP >> 4);
                   8105:        param->fcb2.w.l = 24;
                   8106:        param->fcb2.w.h = (WORK_TOP >> 4);
                   8107:        
                   8108:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   8109:        
                   8110:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   8111:        cmd_line->len = strlen(opt);
                   8112:        strcpy(cmd_line->cmd, opt);
                   8113:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   8114:        
                   8115:        msdos_process_exec(command, param, 0);
                   8116:        REG8(AL) = 0;
                   8117: }
                   8118: 
1.1.1.22  root     8119: inline void msdos_int_2fh_01h()
                   8120: {
                   8121:        switch(REG8(AL)) {
                   8122:        case 0x00:
                   8123:                REG8(AL) = 0x01; // print.com is not installed, can't install
                   8124:                break;
                   8125:        default:
                   8126:                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));
                   8127:                REG16(AX) = 0x01;
                   8128:                m_CF = 1;
                   8129:                break;
                   8130:        }
                   8131: }
                   8132: 
                   8133: inline void msdos_int_2fh_05h()
                   8134: {
                   8135:        switch(REG8(AL)) {
                   8136:        case 0x00:
                   8137:                REG8(AL) = 0x01; // critical error handler is not installed, can't install
                   8138:                break;
                   8139:        default:
                   8140:                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));
                   8141:                REG16(AX) = 0x01;
                   8142:                m_CF = 1;
                   8143:                break;
                   8144:        }
                   8145: }
                   8146: 
                   8147: inline void msdos_int_2fh_06h()
                   8148: {
                   8149:        switch(REG8(AL)) {
                   8150:        case 0x00:
                   8151:                REG8(AL) = 0x01; // assign is not installed, can't install
                   8152:                break;
                   8153:        default:
                   8154:                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));
                   8155:                REG16(AX) = 0x01;
                   8156:                m_CF = 1;
                   8157:                break;
                   8158:        }
                   8159: }
                   8160: 
                   8161: inline void msdos_int_2fh_08h()
                   8162: {
                   8163:        switch(REG8(AL)) {
                   8164:        case 0x00:
                   8165:                REG8(AL) = 0x01; // driver.sys is not installed, can't install
                   8166:                break;
                   8167:        default:
                   8168:                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));
                   8169:                REG16(AX) = 0x01;
                   8170:                m_CF = 1;
                   8171:                break;
                   8172:        }
                   8173: }
                   8174: 
                   8175: inline void msdos_int_2fh_10h()
                   8176: {
                   8177:        switch(REG8(AL)) {
                   8178:        case 0x00:
                   8179:                REG8(AL) = 0x01; // share is not installed, can't install
                   8180:                break;
                   8181:        default:
                   8182:                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));
                   8183:                REG16(AX) = 0x01;
                   8184:                m_CF = 1;
                   8185:                break;
                   8186:        }
                   8187: }
                   8188: 
                   8189: inline void msdos_int_2fh_11h()
                   8190: {
                   8191:        switch(REG8(AL)) {
                   8192:        case 0x00:
                   8193:                REG8(AL) = 0x01; // mscdex is not installed, can't install
                   8194:                break;
                   8195:        default:
                   8196:                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));
                   8197:                REG16(AX) = 0x01;
                   8198:                m_CF = 1;
                   8199:                break;
                   8200:        }
                   8201: }
                   8202: 
1.1.1.21  root     8203: inline void msdos_int_2fh_12h()
                   8204: {
                   8205:        switch(REG8(AL)) {
1.1.1.22  root     8206:        case 0x00:
                   8207:                REG8(AL) = 0xff;
                   8208:                break;
1.1.1.21  root     8209:        case 0x16:
                   8210:                if(REG16(BX) < 20) {
                   8211:                        SREG(ES) = SFT_TOP >> 4;
                   8212:                        i386_load_segment_descriptor(ES);
                   8213:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   8214:                        
                   8215:                        // update system file table
                   8216:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   8217:                        if(file_handler[REG16(BX)].valid) {
                   8218:                                int count = 0;
                   8219:                                for(int i = 0; i < 20; i++) {
                   8220:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   8221:                                                count++;
                   8222:                                        }
                   8223:                                }
                   8224:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   8225:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   8226:                                _lseek(REG16(BX), 0, SEEK_END);
                   8227:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   8228:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   8229:                        } else {
                   8230:                                memset(sft, 0, 0x3b);
                   8231:                        }
                   8232:                } else {
                   8233:                        REG16(AX) = 0x06;
                   8234:                        m_CF = 1;
                   8235:                }
                   8236:                break;
                   8237:        case 0x20:
                   8238:                {
                   8239:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8240:                        
                   8241:                        if(fd < 20) {
                   8242:                                SREG(ES) = current_psp;
                   8243:                                i386_load_segment_descriptor(ES);
                   8244:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   8245:                        } else {
                   8246:                                REG16(AX) = 0x06;
                   8247:                                m_CF = 1;
                   8248:                        }
                   8249:                }
                   8250:                break;
1.1.1.22  root     8251:        case 0x2e:
                   8252:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
                   8253:                        SREG(ES) = ERR_TABLE_TOP >> 4;
                   8254:                        i386_load_segment_descriptor(ES);
                   8255:                        REG16(DI) = 0;
                   8256:                }
                   8257:                break;
                   8258:        default:
                   8259:                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));
                   8260:                REG16(AX) = 0x01;
                   8261:                m_CF = 1;
                   8262:                break;
                   8263:        }
                   8264: }
                   8265: 
                   8266: inline void msdos_int_2fh_14h()
                   8267: {
                   8268:        switch(REG8(AL)) {
                   8269:        case 0x00:
1.1.1.25! root     8270:                REG8(AL) = 0xff; // nlsfunc.com is installed
        !          8271:                break;
        !          8272:        case 0x01:
        !          8273:        case 0x03:
        !          8274:                REG8(AL) = 0x00;
        !          8275:                active_code_page = REG16(BX);
        !          8276:                msdos_nls_tables_update();
        !          8277:                break;
        !          8278:        case 0x02:
        !          8279:                REG8(AL) = get_extended_country_info(REG16(BP));
        !          8280:                break;
        !          8281:        case 0x04:
        !          8282:                REG8(AL) = 0x00;
        !          8283:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     8284:                break;
                   8285:        default:
                   8286:                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));
                   8287:                REG16(AX) = 0x01;
                   8288:                m_CF = 1;
                   8289:                break;
                   8290:        }
                   8291: }
                   8292: 
                   8293: inline void msdos_int_2fh_15h()
                   8294: {
                   8295:        switch(REG8(AL)) {
                   8296:        case 0x00:
                   8297:                // function not supported, do not clear AX
                   8298:                break;
                   8299:        case 0x0b:
                   8300:                // mscdex.exe is not installed
                   8301:                break;
                   8302:        case 0xff:
                   8303:                // corelcdx is not installed
                   8304:                break;
1.1.1.21  root     8305:        default:
1.1.1.22  root     8306:                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     8307:                REG16(AX) = 0x01;
                   8308:                m_CF = 1;
                   8309:                break;
                   8310:        }
                   8311: }
                   8312: 
1.1       root     8313: inline void msdos_int_2fh_16h()
                   8314: {
                   8315:        switch(REG8(AL)) {
                   8316:        case 0x00:
1.1.1.14  root     8317:                if(no_windows) {
                   8318:                        REG8(AL) = 0;
                   8319:                } else {
1.1       root     8320:                        OSVERSIONINFO osvi;
                   8321:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
                   8322:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                   8323:                        GetVersionEx(&osvi);
                   8324:                        REG8(AL) = osvi.dwMajorVersion;
                   8325:                        REG8(AH) = osvi.dwMinorVersion;
                   8326:                }
                   8327:                break;
1.1.1.22  root     8328:        case 0x0a:
                   8329:                if(!no_windows) {
                   8330:                        OSVERSIONINFO osvi;
                   8331:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
                   8332:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                   8333:                        GetVersionEx(&osvi);
                   8334:                        REG16(AX) = 0x0000;
                   8335:                        REG8(BH) = osvi.dwMajorVersion;
                   8336:                        REG8(BL) = osvi.dwMinorVersion;
                   8337:                        REG16(CX) = 0x0003; // enhanced
                   8338:                }
                   8339:                break;
                   8340:        case 0x0e:
                   8341:        case 0x0f:
                   8342:        case 0x11:
                   8343:        case 0x12:
                   8344:        case 0x13:
                   8345:        case 0x14:
                   8346:        case 0x87:
                   8347:                // function not supported, do not clear AX
                   8348:                break;
1.1.1.14  root     8349:        case 0x80:
                   8350:                Sleep(10);
                   8351:                hardware_update();
                   8352:                REG8(AL) = 0;
                   8353:                break;
1.1.1.22  root     8354:        case 0x8e:
                   8355:                REG16(AX) = 0x00; // failed
                   8356:                break;
1.1.1.20  root     8357:        case 0x8f:
                   8358:                switch(REG8(DH)) {
                   8359:                case 0x00:
                   8360:                case 0x02:
                   8361:                case 0x03:
                   8362:                        REG16(AX) = 0x00;
                   8363:                        break;
                   8364:                case 0x01:
                   8365:                        REG16(AX) = 0x168f;
                   8366:                        break;
                   8367:                }
                   8368:                break;
1.1       root     8369:        default:
1.1.1.22  root     8370:                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));
                   8371:                REG16(AX) = 0x01;
                   8372:                m_CF = 1;
                   8373:                break;
                   8374:        }
                   8375: }
                   8376: 
                   8377: inline void msdos_int_2fh_19h()
                   8378: {
                   8379:        switch(REG8(AL)) {
                   8380:        case 0x00:
                   8381:                // shellb.com is not installed
                   8382:                REG8(AL) = 0x00;
                   8383:                break;
                   8384:        case 0x01:
                   8385:        case 0x02:
                   8386:        case 0x03:
                   8387:        case 0x04:
                   8388:                REG16(AX) = 0x01;
                   8389:                m_CF = 1;
                   8390:                break;
                   8391:        default:
                   8392:                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     8393:                REG16(AX) = 0x01;
1.1.1.3   root     8394:                m_CF = 1;
1.1       root     8395:                break;
                   8396:        }
                   8397: }
                   8398: 
                   8399: inline void msdos_int_2fh_1ah()
                   8400: {
                   8401:        switch(REG8(AL)) {
                   8402:        case 0x00:
                   8403:                // ansi.sys is installed
                   8404:                REG8(AL) = 0xff;
                   8405:                break;
                   8406:        default:
1.1.1.22  root     8407:                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));
                   8408:                REG16(AX) = 0x01;
                   8409:                m_CF = 1;
                   8410:                break;
                   8411:        }
                   8412: }
                   8413: 
                   8414: inline void msdos_int_2fh_1bh()
                   8415: {
                   8416:        switch(REG8(AL)) {
                   8417:        case 0x00:
                   8418:                // xma2ems.sys is not installed
                   8419:                REG8(AL) = 0x00;
                   8420:                break;
                   8421:        default:
                   8422:                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     8423:                REG16(AX) = 0x01;
1.1.1.3   root     8424:                m_CF = 1;
1.1       root     8425:                break;
                   8426:        }
                   8427: }
                   8428: 
                   8429: inline void msdos_int_2fh_43h()
                   8430: {
                   8431:        switch(REG8(AL)) {
                   8432:        case 0x00:
1.1.1.19  root     8433:                // xms is installed ?
                   8434: #ifdef SUPPORT_XMS
                   8435:                if(support_xms) {
                   8436:                        REG8(AL) = 0x80;
                   8437:                } else
                   8438: #endif
                   8439:                REG8(AL) = 0x00;
                   8440:                break;
                   8441:        case 0x10:
                   8442:                SREG(ES) = XMS_TOP >> 4;
                   8443:                i386_load_segment_descriptor(ES);
1.1.1.25! root     8444:                REG16(BX) = 0x1c;
1.1       root     8445:                break;
                   8446:        default:
1.1.1.22  root     8447:                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));
                   8448:                REG16(AX) = 0x01;
                   8449:                m_CF = 1;
                   8450:                break;
                   8451:        }
                   8452: }
                   8453: 
                   8454: inline void msdos_int_2fh_46h()
                   8455: {
                   8456:        switch(REG8(AL)) {
                   8457:        case 0x80:
                   8458:                // windows v3.0 is not installed
                   8459:                break;
                   8460:        default:
                   8461:                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));
                   8462:                REG16(AX) = 0x01;
                   8463:                m_CF = 1;
                   8464:                break;
                   8465:        }
                   8466: }
                   8467: 
                   8468: inline void msdos_int_2fh_48h()
                   8469: {
                   8470:        switch(REG8(AL)) {
                   8471:        case 0x00:
                   8472:                // doskey is not installed
                   8473:                break;
                   8474:        case 0x10:
                   8475:                msdos_int_21h_0ah();
                   8476:                REG16(AX) = 0x00;
                   8477:                break;
                   8478:        default:
                   8479:                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     8480:                REG16(AX) = 0x01;
1.1.1.3   root     8481:                m_CF = 1;
1.1       root     8482:                break;
                   8483:        }
                   8484: }
                   8485: 
                   8486: inline void msdos_int_2fh_4ah()
                   8487: {
1.1.1.19  root     8488:        // hma is not installed
1.1       root     8489:        switch(REG8(AL)) {
                   8490:        case 0x01:
                   8491:        case 0x02:
1.1.1.19  root     8492:                // hma is not used
1.1       root     8493:                REG16(BX) = 0;
1.1.1.3   root     8494:                SREG(ES) = 0xffff;
                   8495:                i386_load_segment_descriptor(ES);
1.1       root     8496:                REG16(DI) = 0xffff;
                   8497:                break;
1.1.1.19  root     8498:        case 0x03:
                   8499:                // unable to allocate
                   8500:                REG16(DI) = 0xffff;
                   8501:                break;
                   8502:        case 0x04:
                   8503:                // function not supported, do not clear AX
                   8504:                break;
1.1.1.22  root     8505:        case 0x10: // smartdrv installation check
                   8506:        case 0x11: // dblspace installation check
                   8507:                break;
                   8508:        default:
                   8509:                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));
                   8510:                REG16(AX) = 0x01;
                   8511:                m_CF = 1;
                   8512:                break;
                   8513:        }
                   8514: }
                   8515: 
                   8516: inline void msdos_int_2fh_4bh()
                   8517: {
                   8518:        switch(REG8(AL)) {
1.1.1.24  root     8519:        case 0x01:
1.1.1.22  root     8520:        case 0x02:
1.1.1.24  root     8521:                // task switcher not loaded
                   8522:                break;
                   8523:        case 0x03:
                   8524:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   8525:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     8526:                break;
1.1       root     8527:        default:
1.1.1.22  root     8528:                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     8529:                REG16(AX) = 0x01;
1.1.1.3   root     8530:                m_CF = 1;
1.1       root     8531:                break;
                   8532:        }
                   8533: }
                   8534: 
                   8535: inline void msdos_int_2fh_4fh()
                   8536: {
                   8537:        switch(REG8(AL)) {
                   8538:        case 0x00:
                   8539:                REG16(AX) = 0;
                   8540:                REG8(DL) = 1;   // major version
                   8541:                REG8(DH) = 0;   // minor version
                   8542:                break;
                   8543:        case 0x01:
                   8544:                REG16(AX) = 0;
                   8545:                REG16(BX) = active_code_page;
                   8546:                break;
                   8547:        default:
1.1.1.22  root     8548:                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));
                   8549:                REG16(AX) = 0x01;
                   8550:                m_CF = 1;
                   8551:                break;
                   8552:        }
                   8553: }
                   8554: 
                   8555: inline void msdos_int_2fh_55h()
                   8556: {
                   8557:        switch(REG8(AL)) {
                   8558:        case 0x00:
                   8559:        case 0x01:
                   8560: //             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));
                   8561:                break;
                   8562:        default:
                   8563:                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     8564:                REG16(AX) = 0x01;
1.1.1.3   root     8565:                m_CF = 1;
1.1       root     8566:                break;
                   8567:        }
                   8568: }
                   8569: 
1.1.1.24  root     8570: inline void msdos_int_2fh_adh()
                   8571: {
                   8572:        switch(REG8(AL)) {
                   8573:        case 0x00:
                   8574:                // display.sys is installed
                   8575:                REG8(AL) = 0xff;
                   8576:                REG16(BX) = 0x100; // ???
                   8577:                break;
                   8578:        case 0x01:
                   8579:                active_code_page = REG16(BX);
                   8580:                msdos_nls_tables_update();
                   8581:                REG16(AX) = 0x01;
                   8582:                break;
                   8583:        case 0x02:
                   8584:                REG16(BX) = active_code_page;
                   8585:                break;
                   8586:        case 0x03:
                   8587:                // FIXME
                   8588:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   8589:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   8590:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   8591:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   8592:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   8593:                break;
                   8594:        case 0x80:
                   8595:                break; // keyb.com is not installed
                   8596:        default:
                   8597:                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));
                   8598:                REG16(AX) = 0x01;
                   8599:                m_CF = 1;
                   8600:                break;
                   8601:        }
                   8602: }
                   8603: 
1.1       root     8604: inline void msdos_int_2fh_aeh()
                   8605: {
                   8606:        switch(REG8(AL)) {
                   8607:        case 0x00:
                   8608:                REG8(AL) = 0;
                   8609:                break;
                   8610:        case 0x01:
                   8611:                {
                   8612:                        char command[MAX_PATH];
                   8613:                        memset(command, 0, sizeof(command));
1.1.1.3   root     8614:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     8615:                        
                   8616:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   8617:                        param->env_seg = 0;
                   8618:                        param->cmd_line.w.l = 44;
                   8619:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   8620:                        param->fcb1.w.l = 24;
                   8621:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   8622:                        param->fcb2.w.l = 24;
                   8623:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   8624:                        
                   8625:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   8626:                        
                   8627:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     8628:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   8629:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     8630:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   8631:                        
                   8632:                        if(msdos_process_exec(command, param, 0)) {
                   8633:                                REG16(AX) = 0x02;
1.1.1.3   root     8634:                                m_CF = 1;
1.1       root     8635:                        }
                   8636:                }
                   8637:                break;
                   8638:        default:
1.1.1.22  root     8639:                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     8640:                REG16(AX) = 0x01;
1.1.1.3   root     8641:                m_CF = 1;
1.1       root     8642:                break;
                   8643:        }
                   8644: }
                   8645: 
                   8646: inline void msdos_int_2fh_b7h()
                   8647: {
                   8648:        switch(REG8(AL)) {
                   8649:        case 0x00:
                   8650:                // append is not installed
                   8651:                REG8(AL) = 0;
                   8652:                break;
1.1.1.22  root     8653:        case 0x07:
                   8654:        case 0x11:
                   8655:                break;
1.1       root     8656:        default:
1.1.1.22  root     8657:                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     8658:                REG16(AX) = 0x01;
1.1.1.3   root     8659:                m_CF = 1;
1.1       root     8660:                break;
                   8661:        }
                   8662: }
                   8663: 
1.1.1.24  root     8664: inline void msdos_int_33h_0000h()
                   8665: {
                   8666:        REG16(AX) = 0xffff; // hardware/driver installed
                   8667:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   8668: }
                   8669: 
                   8670: inline void msdos_int_33h_0001h()
                   8671: {
                   8672:        if(!mouse.active) {
                   8673:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   8674:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   8675:                }
                   8676:                mouse.active = true;
                   8677:                pic[1].imr &= ~0x10;    // enable irq12
                   8678:        }
                   8679: }
                   8680: 
                   8681: inline void msdos_int_33h_0002h()
                   8682: {
                   8683:        if(mouse.active) {
                   8684:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   8685:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   8686:                }
                   8687:                mouse.active = false;
                   8688:                pic[1].imr |= 0x10;     // disable irq12
                   8689:        }
                   8690: }
                   8691: 
                   8692: inline void msdos_int_33h_0003h()
                   8693: {
                   8694:        REG16(BX) = mouse.get_buttons();
                   8695:        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   8696:        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   8697: }
                   8698: 
                   8699: inline void msdos_int_33h_0005h()
                   8700: {
                   8701:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   8702:                int idx = REG16(BX);
                   8703:                REG16(BX) = mouse.buttons[idx].pressed_times;
                   8704:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].pressed_position.x));
                   8705:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].pressed_position.y));
                   8706:                mouse.buttons[idx].pressed_times = 0;
                   8707:        } else {
                   8708:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8709:        }
                   8710:        REG16(AX) = mouse.get_buttons();
                   8711: }
                   8712: 
                   8713: inline void msdos_int_33h_0006h()
                   8714: {
                   8715:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   8716:                int idx = REG16(BX);
                   8717:                REG16(BX) = mouse.buttons[idx].released_times;
                   8718:                REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.buttons[idx].released_position.x));
                   8719:                REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.buttons[idx].released_position.y));
                   8720:                mouse.buttons[idx].released_times = 0;
                   8721:        } else {
                   8722:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8723:        }
                   8724:        REG16(AX) = mouse.get_buttons();
                   8725: }
                   8726: 
                   8727: inline void msdos_int_33h_0007h()
                   8728: {
                   8729:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   8730:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   8731: }
                   8732: 
                   8733: inline void msdos_int_33h_0008h()
                   8734: {
                   8735:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   8736:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   8737: }
                   8738: 
                   8739: inline void msdos_int_33h_0009h()
                   8740: {
                   8741:        mouse.hot_spot[0] = REG16(BX);
                   8742:        mouse.hot_spot[1] = REG16(CX);
                   8743: }
                   8744: 
                   8745: inline void msdos_int_33h_000bh()
                   8746: {
                   8747:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   8748:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   8749:        mouse.prev_position.x = mouse.position.x;
                   8750:        mouse.prev_position.y = mouse.position.y;
                   8751:        REG16(CX) = dx;
                   8752:        REG16(DX) = dy;
                   8753: }
                   8754: 
                   8755: inline void msdos_int_33h_000ch()
                   8756: {
                   8757:        mouse.call_mask = REG16(CX);
                   8758:        mouse.call_addr.w.l = REG16(DX);
                   8759:        mouse.call_addr.w.h = SREG(ES);
                   8760: }
                   8761: 
                   8762: inline void msdos_int_33h_000fh()
                   8763: {
                   8764:        mouse.mickey.x = REG16(CX);
                   8765:        mouse.mickey.y = REG16(DX);
                   8766: }
                   8767: 
                   8768: inline void msdos_int_33h_0011h()
                   8769: {
                   8770:        REG16(AX) = 0xffff;
                   8771:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   8772: }
                   8773: 
                   8774: inline void msdos_int_33h_0014h()
                   8775: {
                   8776:        UINT16 old_mask = mouse.call_mask;
                   8777:        UINT16 old_ofs = mouse.call_addr.w.l;
                   8778:        UINT16 old_seg = mouse.call_addr.w.h;
                   8779:        
                   8780:        mouse.call_mask = REG16(CX);
                   8781:        mouse.call_addr.w.l = REG16(DX);
                   8782:        mouse.call_addr.w.h = SREG(ES);
                   8783:        
                   8784:        REG16(CX) = old_mask;
                   8785:        REG16(DX) = old_ofs;
                   8786:        SREG(ES) = old_seg;
                   8787:        i386_load_segment_descriptor(ES);
                   8788: }
                   8789: 
                   8790: inline void msdos_int_33h_0015h()
                   8791: {
                   8792:        REG16(BX) = sizeof(mouse);
                   8793: }
                   8794: 
                   8795: inline void msdos_int_33h_0016h()
                   8796: {
                   8797:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   8798: }
                   8799: 
                   8800: inline void msdos_int_33h_0017h()
                   8801: {
                   8802:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   8803: }
                   8804: 
                   8805: inline void msdos_int_33h_001ah()
                   8806: {
                   8807:        mouse.sensitivity[0] = REG16(BX);
                   8808:        mouse.sensitivity[1] = REG16(CX);
                   8809:        mouse.sensitivity[2] = REG16(DX);
                   8810: }
                   8811: 
                   8812: inline void msdos_int_33h_001bh()
                   8813: {
                   8814:        REG16(BX) = mouse.sensitivity[0];
                   8815:        REG16(CX) = mouse.sensitivity[1];
                   8816:        REG16(DX) = mouse.sensitivity[2];
                   8817: }
                   8818: 
                   8819: inline void msdos_int_33h_001dh()
                   8820: {
                   8821:        mouse.display_page = REG16(BX);
                   8822: }
                   8823: 
                   8824: inline void msdos_int_33h_001eh()
                   8825: {
                   8826:        REG16(BX) = mouse.display_page;
                   8827: }
                   8828: 
                   8829: inline void msdos_int_33h_0021h()
                   8830: {
                   8831:        REG16(AX) = 0xffff;
                   8832:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   8833: }
                   8834: 
                   8835: inline void msdos_int_33h_0022h()
                   8836: {
                   8837:        mouse.language = REG16(BX);
                   8838: }
                   8839: 
                   8840: inline void msdos_int_33h_0023h()
                   8841: {
                   8842:        REG16(BX) = mouse.language;
                   8843: }
                   8844: 
                   8845: inline void msdos_int_33h_0024h()
                   8846: {
                   8847:        REG16(BX) = 0x0805; // V8.05
                   8848:        REG16(CX) = 0x0400; // PS/2
                   8849: }
                   8850: 
                   8851: inline void msdos_int_33h_0026h()
                   8852: {
                   8853:        REG16(BX) = 0x0000;
                   8854:        REG16(CX) = mouse.max_position.x;
                   8855:        REG16(DX) = mouse.max_position.y;
                   8856: }
                   8857: 
                   8858: inline void msdos_int_33h_002ah()
                   8859: {
                   8860:        REG16(AX) = mouse.active ? 0 : 0xffff;
                   8861:        REG16(BX) = mouse.hot_spot[0];
                   8862:        REG16(CX) = mouse.hot_spot[1];
                   8863:        REG16(DX) = 4; // PS/2
                   8864: }
                   8865: 
                   8866: inline void msdos_int_33h_0031h()
                   8867: {
                   8868:        REG16(AX) = mouse.min_position.x;
                   8869:        REG16(BX) = mouse.min_position.y;
                   8870:        REG16(CX) = mouse.max_position.x;
                   8871:        REG16(DX) = mouse.max_position.y;
                   8872: }
                   8873: 
                   8874: inline void msdos_int_33h_0032h()
                   8875: {
                   8876:        REG16(AX) = 0;
                   8877: //     REG16(AX) |= 0x8000; // 0025h
                   8878:        REG16(AX) |= 0x4000; // 0026h
                   8879: //     REG16(AX) |= 0x2000; // 0027h
                   8880: //     REG16(AX) |= 0x1000; // 0028h
                   8881: //     REG16(AX) |= 0x0800; // 0029h
                   8882:        REG16(AX) |= 0x0400; // 002ah
                   8883: //     REG16(AX) |= 0x0200; // 002bh
                   8884: //     REG16(AX) |= 0x0100; // 002ch
                   8885: //     REG16(AX) |= 0x0080; // 002dh
                   8886: //     REG16(AX) |= 0x0040; // 002eh
                   8887:        REG16(AX) |= 0x0020; // 002fh
                   8888: //     REG16(AX) |= 0x0010; // 0030h
                   8889:        REG16(AX) |= 0x0008; // 0031h
                   8890:        REG16(AX) |= 0x0004; // 0032h
                   8891: //     REG16(AX) |= 0x0002; // 0033h
                   8892: //     REG16(AX) |= 0x0001; // 0034h
                   8893: }
                   8894: 
1.1.1.19  root     8895: inline void msdos_int_67h_40h()
                   8896: {
                   8897:        if(!support_ems) {
                   8898:                REG8(AH) = 0x84;
                   8899:        } else {
                   8900:                REG8(AH) = 0x00;
                   8901:        }
                   8902: }
                   8903: 
                   8904: inline void msdos_int_67h_41h()
                   8905: {
                   8906:        if(!support_ems) {
                   8907:                REG8(AH) = 0x84;
                   8908:        } else {
                   8909:                REG8(AH) = 0x00;
                   8910:                REG16(BX) = EMS_TOP >> 4;
                   8911:        }
                   8912: }
                   8913: 
                   8914: inline void msdos_int_67h_42h()
                   8915: {
                   8916:        if(!support_ems) {
                   8917:                REG8(AH) = 0x84;
                   8918:        } else {
                   8919:                REG8(AH) = 0x00;
                   8920:                REG16(BX) = free_ems_pages;
                   8921:                REG16(DX) = MAX_EMS_PAGES;
                   8922:        }
                   8923: }
                   8924: 
                   8925: inline void msdos_int_67h_43h()
                   8926: {
                   8927:        if(!support_ems) {
                   8928:                REG8(AH) = 0x84;
                   8929:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   8930:                REG8(AH) = 0x87;
                   8931:        } else if(REG16(BX) > free_ems_pages) {
                   8932:                REG8(AH) = 0x88;
                   8933:        } else if(REG16(BX) == 0) {
                   8934:                REG8(AH) = 0x89;
                   8935:        } else {
                   8936:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   8937:                        if(!ems_handles[i].allocated) {
                   8938:                                ems_allocate_pages(i, REG16(BX));
                   8939:                                REG8(AH) = 0x00;
                   8940:                                REG16(DX) = i;
                   8941:                                return;
                   8942:                        }
                   8943:                }
                   8944:                REG8(AH) = 0x85;
                   8945:        }
                   8946: }
                   8947: 
                   8948: inline void msdos_int_67h_44h()
                   8949: {
                   8950:        if(!support_ems) {
                   8951:                REG8(AH) = 0x84;
                   8952:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   8953:                REG8(AH) = 0x83;
                   8954:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   8955:                REG8(AH) = 0x8a;
                   8956: //     } else if(!(REG8(AL) < 4)) {
                   8957: //             REG8(AH) = 0x8b;
                   8958:        } else if(REG16(BX) == 0xffff) {
                   8959:                ems_unmap_page(REG8(AL) & 3);
                   8960:                REG8(AH) = 0x00;
                   8961:        } else {
                   8962:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   8963:                REG8(AH) = 0x00;
                   8964:        }
                   8965: }
                   8966: 
                   8967: inline void msdos_int_67h_45h()
                   8968: {
                   8969:        if(!support_ems) {
                   8970:                REG8(AH) = 0x84;
                   8971:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   8972:                REG8(AH) = 0x83;
                   8973:        } else {
                   8974:                ems_release_pages(REG16(DX));
                   8975:                REG8(AH) = 0x00;
                   8976:        }
                   8977: }
                   8978: 
                   8979: inline void msdos_int_67h_46h()
                   8980: {
                   8981:        if(!support_ems) {
                   8982:                REG8(AH) = 0x84;
                   8983:        } else {
                   8984:                REG16(AX) = 0x0032; // EMS 3.2
                   8985: //             REG16(AX) = 0x0040; // EMS 4.0
                   8986:        }
                   8987: }
                   8988: 
                   8989: inline void msdos_int_67h_47h()
                   8990: {
                   8991:        // NOTE: the map data should be stored in the specified ems page, not process data
                   8992:        process_t *process = msdos_process_info_get(current_psp);
                   8993:        
                   8994:        if(!support_ems) {
                   8995:                REG8(AH) = 0x84;
                   8996: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   8997: //             REG8(AH) = 0x83;
                   8998:        } else if(process->ems_pages_stored) {
                   8999:                REG8(AH) = 0x8d;
                   9000:        } else {
                   9001:                for(int i = 0; i < 4; i++) {
                   9002:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   9003:                        process->ems_pages[i].page   = ems_pages[i].page;
                   9004:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   9005:                }
                   9006:                process->ems_pages_stored = true;
                   9007:                REG8(AH) = 0x00;
                   9008:        }
                   9009: }
                   9010: 
                   9011: inline void msdos_int_67h_48h()
                   9012: {
                   9013:        // NOTE: the map data should be restored from the specified ems page, not process data
                   9014:        process_t *process = msdos_process_info_get(current_psp);
                   9015:        
                   9016:        if(!support_ems) {
                   9017:                REG8(AH) = 0x84;
                   9018: //     } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9019: //             REG8(AH) = 0x83;
                   9020:        } else if(!process->ems_pages_stored) {
                   9021:                REG8(AH) = 0x8e;
                   9022:        } else {
                   9023:                for(int i = 0; i < 4; i++) {
                   9024:                        if(process->ems_pages[i].mapped) {
                   9025:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   9026:                        } else {
                   9027:                                ems_unmap_page(i);
                   9028:                        }
                   9029:                }
                   9030:                process->ems_pages_stored = false;
                   9031:                REG8(AH) = 0x00;
                   9032:        }
                   9033: }
                   9034: 
                   9035: inline void msdos_int_67h_4bh()
                   9036: {
                   9037:        if(!support_ems) {
                   9038:                REG8(AH) = 0x84;
                   9039:        } else {
                   9040:                REG8(AH) = 0x00;
                   9041:                REG16(BX) = 0;
                   9042:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9043:                        if(ems_handles[i].allocated) {
                   9044:                                REG16(BX)++;
                   9045:                        }
                   9046:                }
                   9047:        }
                   9048: }
                   9049: 
                   9050: inline void msdos_int_67h_4ch()
                   9051: {
                   9052:        if(!support_ems) {
                   9053:                REG8(AH) = 0x84;
                   9054:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9055:                REG8(AH) = 0x83;
                   9056:        } else {
                   9057:                REG8(AH) = 0x00;
                   9058:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   9059:        }
                   9060: }
                   9061: 
                   9062: inline void msdos_int_67h_4dh()
                   9063: {
                   9064:        if(!support_ems) {
                   9065:                REG8(AH) = 0x84;
                   9066:        } else {
                   9067:                REG8(AH) = 0x00;
                   9068:                REG16(BX) = 0;
                   9069:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9070:                        if(ems_handles[i].allocated) {
                   9071:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   9072:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   9073:                                REG16(BX)++;
                   9074:                        }
                   9075:                }
                   9076:        }
                   9077: }
                   9078: 
1.1.1.20  root     9079: inline void msdos_int_67h_4eh()
                   9080: {
                   9081:        if(!support_ems) {
                   9082:                REG8(AH) = 0x84;
                   9083:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   9084:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   9085:                        // save page map
                   9086:                        for(int i = 0; i < 4; i++) {
                   9087:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   9088:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   9089:                        }
                   9090:                }
                   9091:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   9092:                        // restore page map
                   9093:                        for(int i = 0; i < 4; i++) {
                   9094:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   9095:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   9096:                                
                   9097:                                if(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   9098:                                        ems_map_page(i, handle, page);
                   9099:                                } else {
                   9100:                                        ems_unmap_page(i);
                   9101:                                }
                   9102:                        }
                   9103:                }
                   9104:                REG8(AH) = 0x00;
                   9105:        } else if(REG8(AL) == 0x03) {
                   9106:                REG8(AH) = 0x00;
1.1.1.21  root     9107:                REG8(AL) = 4 * 4;
                   9108:        } else {
1.1.1.22  root     9109:                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     9110:                REG8(AH) = 0x8f;
                   9111:        }
                   9112: }
                   9113: 
                   9114: inline void msdos_int_67h_4fh()
                   9115: {
                   9116:        if(!support_ems) {
                   9117:                REG8(AH) = 0x84;
                   9118:        } else if(REG8(AL) == 0x00) {
                   9119:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   9120:                
                   9121:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   9122:                for(int i = 0; i < count; i++) {
                   9123:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   9124:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   9125:                        
                   9126: //                     if(!(physical < 4)) {
                   9127: //                             REG8(AH) = 0x8b;
                   9128: //                             return;
                   9129: //                     }
                   9130:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   9131:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   9132:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   9133:                }
                   9134:                REG8(AH) = 0x00;
                   9135:        } else if(REG8(AL) == 0x01) {
                   9136:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   9137:                
                   9138:                for(int i = 0; i < count; i++) {
                   9139:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   9140:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   9141:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   9142:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   9143:                        
                   9144: //                     if(!(physical < 4)) {
                   9145: //                             REG8(AH) = 0x8b;
                   9146: //                             return;
                   9147: //                     } else
                   9148:                        if(!(handle < MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
                   9149:                                REG8(AH) = 0x83;
                   9150:                                return;
                   9151:                        } else if(logical == 0xffff) {
                   9152:                                ems_unmap_page(physical & 3);
                   9153:                        } else if(logical < ems_handles[handle].pages) {
                   9154:                                ems_map_page(physical & 3, handle, logical);
                   9155:                        } else {
                   9156:                                REG8(AH) = 0x8a;
                   9157:                                return;
                   9158:                        }
                   9159:                }
                   9160:                REG8(AH) = 0x00;
                   9161:        } else if(REG8(AL) == 0x02) {
                   9162:                REG8(AH) = 0x00;
                   9163:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     9164:        } else {
1.1.1.22  root     9165:                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     9166:                REG8(AH) = 0x8f;
                   9167:        }
                   9168: }
                   9169: 
                   9170: inline void msdos_int_67h_50h()
                   9171: {
                   9172:        if(!support_ems) {
                   9173:                REG8(AH) = 0x84;
                   9174:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9175:                REG8(AH) = 0x83;
                   9176:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9177:                for(int i = 0; i < REG16(CX); i++) {
                   9178:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   9179:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   9180:                        
                   9181:                        if(REG8(AL) == 0x01) {
                   9182:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   9183:                        }
                   9184: //                     if(!(physical < 4)) {
                   9185: //                             REG8(AH) = 0x8b;
                   9186: //                             return;
                   9187: //                     } else
                   9188:                        if(logical == 0xffff) {
                   9189:                                ems_unmap_page(physical & 3);
                   9190:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   9191:                                ems_map_page(physical & 3, REG16(DX), logical);
                   9192:                        } else {
                   9193:                                REG8(AH) = 0x8a;
                   9194:                                return;
                   9195:                        }
                   9196:                }
                   9197:                REG8(AH) = 0x00;
                   9198:        } else {
1.1.1.22  root     9199:                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     9200:                REG8(AH) = 0x8f;
                   9201:        }
                   9202: }
                   9203: 
1.1.1.19  root     9204: inline void msdos_int_67h_51h()
                   9205: {
                   9206:        if(!support_ems) {
                   9207:                REG8(AH) = 0x84;
                   9208:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9209:                REG8(AH) = 0x83;
                   9210:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   9211:                REG8(AH) = 0x87;
                   9212:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   9213:                REG8(AH) = 0x88;
                   9214:        } else {
                   9215:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   9216:                REG8(AH) = 0x00;
                   9217:        }
                   9218: }
                   9219: 
1.1.1.20  root     9220: inline void msdos_int_67h_52h()
                   9221: {
                   9222:        if(!support_ems) {
                   9223:                REG8(AH) = 0x84;
                   9224:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9225:                REG8(AH) = 0x83;
                   9226:        } else if(REG8(AL) == 0x00) {
                   9227:                REG8(AL) = 0x00; // handle is volatile
                   9228:                REG8(AH) = 0x00;
                   9229:        } else if(REG8(AL) == 0x01) {
                   9230:                if(REG8(BL) == 0x00) {
                   9231:                        REG8(AH) = 0x00;
                   9232:                } else {
                   9233:                        REG8(AH) = 0x90; // undefined attribute type
                   9234:                }
                   9235:        } else if(REG8(AL) == 0x02) {
                   9236:                REG8(AL) = 0x00; // only volatile handles supported
                   9237:                REG8(AH) = 0x00;
                   9238:        } else {
1.1.1.22  root     9239:                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     9240:                REG8(AH) = 0x8f;
                   9241:        }
                   9242: }
                   9243: 
1.1.1.19  root     9244: inline void msdos_int_67h_53h()
                   9245: {
                   9246:        if(!support_ems) {
                   9247:                REG8(AH) = 0x84;
                   9248:        } else if(!(REG16(DX) < MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   9249:                REG8(AH) = 0x83;
                   9250:        } else if(REG8(AL) == 0x00) {
                   9251:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   9252:                REG8(AH) = 0x00;
                   9253:        } else if(REG8(AL) == 0x01) {
                   9254:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9255:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   9256:                                REG8(AH) = 0xa1;
                   9257:                                return;
                   9258:                        }
                   9259:                }
                   9260:                REG8(AH) = 0x00;
                   9261:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   9262:        } else {
1.1.1.22  root     9263:                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     9264:                REG8(AH) = 0x8f;
1.1.1.19  root     9265:        }
                   9266: }
                   9267: 
                   9268: inline void msdos_int_67h_54h()
                   9269: {
                   9270:        if(!support_ems) {
                   9271:                REG8(AH) = 0x84;
                   9272:        } else if(REG8(AL) == 0x00) {
                   9273:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9274:                        if(ems_handles[i].allocated) {
                   9275:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   9276:                        } else {
                   9277:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   9278:                        }
                   9279:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   9280:                }
                   9281:                REG8(AH) = 0x00;
                   9282:                REG8(AL) = MAX_EMS_HANDLES;
                   9283:        } else if(REG8(AL) == 0x01) {
                   9284:                REG8(AH) = 0xa0; // not found
                   9285:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9286:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   9287:                                REG8(AH) = 0x00;
                   9288:                                REG16(DX) = i;
                   9289:                                break;
                   9290:                        }
                   9291:                }
                   9292:        } else if(REG8(AL) == 0x02) {
                   9293:                REG8(AH) = 0x00;
                   9294:                REG16(BX) = MAX_EMS_HANDLES;
                   9295:        } else {
1.1.1.22  root     9296:                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     9297:                REG8(AH) = 0x8f;
                   9298:        }
                   9299: }
                   9300: 
                   9301: inline void msdos_int_67h_57h_tmp()
                   9302: {
                   9303:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   9304:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   9305:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   9306:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   9307:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   9308:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   9309:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   9310:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   9311:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   9312:        
                   9313:        UINT8 *src_buffer, *dest_buffer;
                   9314:        UINT32 src_addr, dest_addr;
                   9315:        UINT32 src_addr_max, dest_addr_max;
                   9316:        
                   9317:        if(src_type == 0) {
                   9318:                src_buffer = mem;
                   9319:                src_addr = (src_seg << 4) + src_ofs;
                   9320:                src_addr_max = MAX_MEM;
                   9321:        } else {
                   9322:                if(!(src_handle < MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
                   9323:                        REG8(AH) = 0x83;
                   9324:                        return;
                   9325:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   9326:                        REG8(AH) = 0x8a;
                   9327:                        return;
                   9328:                }
                   9329:                src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   9330:                src_addr = src_ofs;
                   9331:                src_addr_max = 0x4000;
                   9332:        }
                   9333:        if(dest_type == 0) {
                   9334:                dest_buffer = mem;
                   9335:                dest_addr = (dest_seg << 4) + dest_ofs;
                   9336:                dest_addr_max = MAX_MEM;
                   9337:        } else {
                   9338:                if(!(dest_handle < MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
                   9339:                        REG8(AH) = 0x83;
                   9340:                        return;
                   9341:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   9342:                        REG8(AH) = 0x8a;
                   9343:                        return;
                   9344:                }
                   9345:                dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   9346:                dest_addr = dest_ofs;
                   9347:                dest_addr_max = 0x4000;
                   9348:        }
                   9349:        for(int i = 0; i < copy_length; i++) {
                   9350:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   9351:                        if(REG8(AL) == 0x00) {
                   9352:                                dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   9353:                        } else if(REG8(AL) == 0x01) {
                   9354:                                UINT8 tmp = dest_buffer[dest_addr];
                   9355:                                dest_buffer[dest_addr++] = src_buffer[src_addr];
                   9356:                                src_buffer[src_addr++] = tmp;
                   9357:                        }
                   9358:                } else {
                   9359:                        REG8(AH) = 0x93;
                   9360:                        return;
                   9361:                }
                   9362:        }
                   9363:        REG8(AH) = 0x80;
                   9364: }
                   9365: 
                   9366: inline void msdos_int_67h_57h()
                   9367: {
                   9368:        if(!support_ems) {
                   9369:                REG8(AH) = 0x84;
                   9370:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9371:                struct {
                   9372:                        UINT16 handle;
                   9373:                        UINT16 page;
                   9374:                        bool mapped;
                   9375:                } tmp_pages[4];
                   9376:                
                   9377:                // unmap pages to copy memory data to ems buffer
                   9378:                for(int i = 0; i < 4; i++) {
                   9379:                        tmp_pages[i].handle = ems_pages[i].handle;
                   9380:                        tmp_pages[i].page   = ems_pages[i].page;
                   9381:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   9382:                        ems_unmap_page(i);
                   9383:                }
                   9384:                
                   9385:                // run move/exchange operation
                   9386:                msdos_int_67h_57h_tmp();
                   9387:                
                   9388:                // restore unmapped pages
                   9389:                for(int i = 0; i < 4; i++) {
                   9390:                        if(tmp_pages[i].mapped) {
                   9391:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   9392:                        }
                   9393:                }
                   9394:        } else {
1.1.1.22  root     9395:                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     9396:                REG8(AH) = 0x8f;
                   9397:        }
                   9398: }
                   9399: 
                   9400: inline void msdos_int_67h_58h()
                   9401: {
                   9402:        if(!support_ems) {
                   9403:                REG8(AH) = 0x84;
                   9404:        } else if(REG8(AL) == 0x00) {
                   9405:                for(int i = 0; i < 4; i++) {
                   9406:                        *(UINT16 *)(mem +SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   9407:                        *(UINT16 *)(mem +SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
                   9408:                }
                   9409:                REG8(AH) = 0x00;
                   9410:                REG16(CX) = 4;
                   9411:        } else if(REG8(AL) == 0x01) {
                   9412:                REG8(AH) = 0x00;
                   9413:                REG16(CX) = 4;
                   9414:        } else {
1.1.1.22  root     9415:                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     9416:                REG8(AH) = 0x8f;
                   9417:        }
                   9418: }
                   9419: 
                   9420: inline void msdos_int_67h_5ah()
                   9421: {
                   9422:        if(!support_ems) {
1.1.1.19  root     9423:                REG8(AH) = 0x84;
1.1.1.20  root     9424:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   9425:                REG8(AH) = 0x87;
                   9426:        } else if(REG16(BX) > free_ems_pages) {
                   9427:                REG8(AH) = 0x88;
                   9428: //     } else if(REG16(BX) == 0) {
                   9429: //             REG8(AH) = 0x89;
                   9430:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   9431:                for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   9432:                        if(!ems_handles[i].allocated) {
                   9433:                                ems_allocate_pages(i, REG16(BX));
                   9434:                                REG8(AH) = 0x00;
                   9435:                                REG16(DX) = i;
                   9436:                                return;
                   9437:                        }
                   9438:                }
                   9439:                REG8(AH) = 0x85;
                   9440:        } else {
1.1.1.22  root     9441:                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     9442:                REG8(AH) = 0x8f;
1.1.1.19  root     9443:        }
                   9444: }
                   9445: 
                   9446: #ifdef SUPPORT_XMS
                   9447: 
                   9448: inline void msdos_call_xms_00h()
                   9449: {
                   9450:        REG16(AX) = 0x0270; // V2.70
                   9451:        REG16(BX) = 0x0000;
                   9452: //     REG16(DX) = 0x0000; // hma does not exist
                   9453:        REG16(DX) = 0x0001; // hma does exist
                   9454: }
                   9455: 
                   9456: inline void msdos_call_xms_01h()
                   9457: {
                   9458:        REG16(AX) = 0x0000;
                   9459: //     REG8(BL) = 0x90; // hma does not exist
                   9460:        REG8(BL) = 0x91; // hma is already used
                   9461: }
                   9462: 
                   9463: inline void msdos_call_xms_02h()
                   9464: {
                   9465:        REG16(AX) = 0x0000;
                   9466: //     REG8(BL) = 0x90; // hma does not exist
                   9467:        REG8(BL) = 0x91; // hma is already used
                   9468: }
                   9469: 
                   9470: inline void msdos_call_xms_03h()
                   9471: {
                   9472:        i386_set_a20_line(1);
                   9473:        REG16(AX) = 0x0001;
                   9474:        REG8(BL) = 0x00;
                   9475: }
                   9476: 
                   9477: inline void msdos_call_xms_04h()
                   9478: {
1.1.1.21  root     9479:        i386_set_a20_line(0);
                   9480:        REG16(AX) = 0x0001;
                   9481:        REG8(BL) = 0x00;
1.1.1.19  root     9482: }
                   9483: 
                   9484: inline void msdos_call_xms_05h()
                   9485: {
                   9486:        i386_set_a20_line(1);
                   9487:        REG16(AX) = 0x0001;
                   9488:        REG8(BL) = 0x00;
1.1.1.21  root     9489:        xms_a20_local_enb_count++;
1.1.1.19  root     9490: }
                   9491: 
                   9492: void msdos_call_xms_06h()
                   9493: {
1.1.1.21  root     9494:        if(xms_a20_local_enb_count > 0) {
                   9495:                xms_a20_local_enb_count--;
                   9496:        }
                   9497:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     9498:                i386_set_a20_line(0);
1.1.1.21  root     9499:        }
                   9500:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     9501:                REG16(AX) = 0x0000;
                   9502:                REG8(BL) = 0x94;
1.1.1.21  root     9503:        } else {
                   9504:                REG16(AX) = 0x0001;
                   9505:                REG8(BL) = 0x00;
1.1.1.19  root     9506:        }
                   9507: }
                   9508: 
                   9509: inline void msdos_call_xms_07h()
                   9510: {
                   9511:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   9512:        REG8(BL) = 0x00;
                   9513: }
                   9514: 
                   9515: inline void msdos_call_xms_08h()
                   9516: {
                   9517:        REG16(AX) = REG16(DX) = 0x0000;
                   9518:        
                   9519:        int mcb_seg = EMB_TOP >> 4;
                   9520:        
                   9521:        while(1) {
                   9522:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   9523:                
                   9524:                if(mcb->psp == 0) {
                   9525:                        if(REG16(AX) < mcb->size_kb()) {
                   9526:                                REG16(AX) = mcb->size_kb();
                   9527:                        }
                   9528:                        REG16(DX) += mcb->size_kb();
                   9529:                }
                   9530:                if(mcb->mz == 'Z') {
                   9531:                        break;
                   9532:                }
                   9533:                mcb_seg += 1 + mcb->paragraphs();
                   9534:        }
                   9535:        
                   9536:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   9537:                REG8(BL) = 0xa0;
                   9538:        } else {
                   9539:                REG8(BL) = 0x00;
                   9540:        }
                   9541: }
                   9542: 
                   9543: inline void msdos_call_xms_09h()
                   9544: {
                   9545:        for(int i = 1; i <= MAX_XMS_HANDLES; i++) {
                   9546:                if(!xms_handles[i].allocated) {
                   9547:                        if((xms_handles[i].seg = msdos_mem_alloc(EMB_TOP >> 4, REG16(DX) * 64, 0)) != -1) {
                   9548:                                xms_handles[i].size_kb = REG16(DX);
                   9549:                                xms_handles[i].lock = 0;
                   9550:                                xms_handles[i].allocated = true;
                   9551:                                
                   9552:                                REG16(AX) = 0x0001;
                   9553:                                REG16(DX) = i;
                   9554:                                REG8(BL) = 0x00;
                   9555:                        } else {
                   9556:                                REG16(AX) = REG16(DX) = 0x0000;
                   9557:                                REG8(BL) = 0xa0;
                   9558:                        }
                   9559:                        return;
                   9560:                }
                   9561:        }
                   9562:        REG16(AX) = REG16(DX) = 0x0000;
                   9563:        REG8(BL) = 0xa1;
                   9564: }
                   9565: 
                   9566: inline void msdos_call_xms_0ah()
                   9567: {
                   9568:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   9569:                REG16(AX) = 0x0000;
                   9570:                REG8(BL) = 0xa2;
                   9571:        } else if(xms_handles[REG16(DX)].lock > 0) {
                   9572:                REG16(AX) = 0x0000;
                   9573:                REG8(BL) = 0xab;
                   9574:        } else {
                   9575:                msdos_mem_free(xms_handles[REG16(DX)].seg);
                   9576:                xms_handles[REG16(DX)].allocated = false;
                   9577:                
                   9578:                REG16(AX) = 0x0001;
                   9579:                REG8(BL) = 0x00;
                   9580:        }
                   9581: }
                   9582: 
                   9583: inline void msdos_call_xms_0bh()
                   9584: {
                   9585:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   9586:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   9587:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   9588:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   9589:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   9590:        
                   9591:        UINT8 *src_buffer, *dest_buffer;
                   9592:        UINT32 src_addr_max, dest_addr_max;
                   9593:        
                   9594:        if(src_handle == 0) {
                   9595:                src_buffer = mem;
                   9596:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   9597:                src_addr_max = MAX_MEM;
                   9598:        } else {
                   9599:                if(!(src_handle >= 1 && src_handle <= MAX_XMS_HANDLES && xms_handles[src_handle].allocated)) {
                   9600:                        REG16(AX) = 0x0000;
                   9601:                        REG8(BL) = 0xa3;
                   9602:                        return;
                   9603:                }
                   9604:                src_buffer = mem + (xms_handles[src_handle].seg << 4);
                   9605:                src_addr_max = xms_handles[src_handle].size_kb * 1024;
                   9606:        }
                   9607:        if(dest_handle == 0) {
                   9608:                dest_buffer = mem;
                   9609:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   9610:                dest_addr_max = MAX_MEM;
                   9611:        } else {
                   9612:                if(!(dest_handle >= 1 && dest_handle <= MAX_XMS_HANDLES && xms_handles[dest_handle].allocated)) {
                   9613:                        REG16(AX) = 0x0000;
                   9614:                        REG8(BL) = 0xa5;
                   9615:                        return;
                   9616:                }
                   9617:                dest_buffer = mem + (xms_handles[dest_handle].seg << 4);
                   9618:                dest_addr_max = xms_handles[dest_handle].size_kb * 1024;
                   9619:        }
                   9620:        for(int i = 0; i < copy_length; i++) {
                   9621:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   9622:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   9623:                } else {
                   9624:                        break;
                   9625:                }
                   9626:        }
                   9627:        REG16(AX) = 0x0001;
                   9628:        REG8(BL) = 0x00;
                   9629: }
                   9630: 
                   9631: inline void msdos_call_xms_0ch()
                   9632: {
                   9633:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   9634:                REG16(AX) = 0x0000;
                   9635:                REG8(BL) = 0xa2;
                   9636:        } else {
                   9637:                xms_handles[REG16(DX)].lock++;
                   9638:                REG16(AX) = 0x0001;
                   9639:                REG8(BL) = 0x00;
                   9640:                UINT32 addr = xms_handles[REG16(DX)].seg << 4;
                   9641:                REG16(DX) = (addr >> 16) & 0xffff;
                   9642:                REG16(BX) = (addr      ) & 0xffff;
                   9643:        }
                   9644: }
                   9645: 
                   9646: inline void msdos_call_xms_0dh()
                   9647: {
                   9648:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   9649:                REG16(AX) = 0x0000;
                   9650:                REG8(BL) = 0xa2;
                   9651:        } else if(!(xms_handles[REG16(DX)].lock > 0)) {
                   9652:                REG16(AX) = 0x0000;
                   9653:                REG8(BL) = 0xaa;
                   9654:        } else {
                   9655:                xms_handles[REG16(DX)].lock--;
                   9656:                REG16(AX) = 0x0001;
                   9657:                REG8(BL) = 0x00;
                   9658:        }
                   9659: }
                   9660: 
                   9661: inline void msdos_call_xms_0eh()
                   9662: {
                   9663:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   9664:                REG16(AX) = 0x0000;
                   9665:                REG8(BL) = 0xa2;
                   9666:        } else {
                   9667:                REG16(AX) = 0x0001;
                   9668:                REG8(BH) = xms_handles[REG16(DX)].lock;
                   9669:                REG8(BL) = 0x00;
                   9670:                for(int i = 1; i <= MAX_XMS_HANDLES; i++) {
                   9671:                        if(!xms_handles[i].allocated) {
                   9672:                                REG8(BL)++;
                   9673:                        }
                   9674:                }
                   9675:                REG16(DX) = xms_handles[REG16(DX)].size_kb;
                   9676:        }
                   9677: }
                   9678: 
                   9679: inline void msdos_call_xms_0fh()
                   9680: {
                   9681:        if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_XMS_HANDLES && xms_handles[REG16(DX)].allocated)) {
                   9682:                REG16(AX) = 0x0000;
                   9683:                REG8(BL) = 0xa2;
                   9684:        } else if(xms_handles[REG16(DX)].lock > 0) {
                   9685:                REG16(AX) = 0x0000;
                   9686:                REG8(BL) = 0xab;
                   9687:        } else if(msdos_mem_realloc(xms_handles[REG16(DX)].seg, REG16(BX) * 64, NULL)) {
                   9688:                REG16(AX) = 0x0000;
                   9689:                REG8(BL) = 0xa0;
                   9690:        } else {
                   9691:                REG16(AX) = 0x0001;
                   9692:                REG8(BL) = 0x00;
                   9693:        }
                   9694: }
                   9695: 
                   9696: inline void msdos_call_xms_10h()
                   9697: {
                   9698:        int seg;
                   9699:        
                   9700:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   9701:                REG16(AX) = 0x0001;
                   9702:                REG16(BX) = seg;
                   9703:        } else {
                   9704:                REG16(AX) = 0x0000;
                   9705:                REG8(BL) = 0xb0;
                   9706:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   9707:        }
                   9708: }
                   9709: 
                   9710: inline void msdos_call_xms_11h()
                   9711: {
                   9712:        int mcb_seg = REG16(DX) - 1;
                   9713:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   9714:        
                   9715:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   9716:                msdos_mem_free(REG16(DX));
                   9717:                REG16(AX) = 0x0001;
                   9718:                REG8(BL) = 0x00;
                   9719:        } else {
                   9720:                REG16(AX) = 0x0000;
                   9721:                REG8(BL) = 0xb2;
                   9722:        }
                   9723: }
                   9724: 
                   9725: inline void msdos_call_xms_12h()
                   9726: {
                   9727:        int mcb_seg = REG16(DX) - 1;
                   9728:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   9729:        int max_paragraphs;
                   9730:        
                   9731:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   9732:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   9733:                        REG16(AX) = 0x0001;
                   9734:                        REG8(BL) = 0x00;
                   9735:                } else {
                   9736:                        REG16(AX) = 0x0000;
                   9737:                        REG8(BL) = 0xb0;
                   9738:                        REG16(DX) = max_paragraphs;
                   9739:                }
                   9740:        } else {
                   9741:                REG16(AX) = 0x0000;
                   9742:                REG8(BL) = 0xb2;
                   9743:        }
                   9744: }
                   9745: 
                   9746: #endif
                   9747: 
1.1       root     9748: void msdos_syscall(unsigned num)
                   9749: {
1.1.1.22  root     9750: #ifdef ENABLE_DEBUG_SYSCALL
                   9751:        if(num == 0x68) {
                   9752:                // dummy interrupt for EMS (int 67h)
                   9753:                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));
                   9754:        } else if(num == 0x69) {
                   9755:                // dummy interrupt for XMS (call far)
                   9756:                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));
                   9757:        } else if(num == 0x6a) {
                   9758:                // dummy interrupt for case map routine pointed in the country info
                   9759:        } else {
                   9760:                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));
                   9761:        }
                   9762: #endif
                   9763:        
1.1       root     9764:        switch(num) {
                   9765:        case 0x00:
                   9766:                error("division by zero\n");
                   9767:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   9768:                break;
                   9769:        case 0x04:
                   9770:                error("overflow\n");
                   9771:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   9772:                break;
                   9773:        case 0x06:
                   9774:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     9775:                if(!ignore_illegal_insn) {
                   9776:                        error("illegal instruction\n");
                   9777:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   9778:                } else {
                   9779: #if defined(HAS_I386)
                   9780:                        m_eip++;
                   9781: #else
                   9782:                        m_pc++;
                   9783: #endif
                   9784:                }
1.1       root     9785:                break;
1.1.1.8   root     9786:        case 0x08:
1.1.1.14  root     9787: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     9788:        case 0x09:
                   9789:        case 0x0a:
                   9790:        case 0x0b:
                   9791:        case 0x0c:
                   9792:        case 0x0d:
                   9793:        case 0x0e:
                   9794:        case 0x0f:
                   9795:                // EOI
                   9796:                pic[0].isr &= ~(1 << (num - 0x08));
                   9797:                pic_update();
                   9798:                break;
1.1       root     9799:        case 0x10:
                   9800:                // PC BIOS - Video
1.1.1.14  root     9801:                if(!restore_console_on_exit) {
1.1.1.15  root     9802:                        change_console_size(scr_width, scr_height);
1.1       root     9803:                }
1.1.1.3   root     9804:                m_CF = 0;
1.1       root     9805:                switch(REG8(AH)) {
1.1.1.16  root     9806:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     9807:                case 0x01: pcbios_int_10h_01h(); break;
                   9808:                case 0x02: pcbios_int_10h_02h(); break;
                   9809:                case 0x03: pcbios_int_10h_03h(); break;
                   9810:                case 0x05: pcbios_int_10h_05h(); break;
                   9811:                case 0x06: pcbios_int_10h_06h(); break;
                   9812:                case 0x07: pcbios_int_10h_07h(); break;
                   9813:                case 0x08: pcbios_int_10h_08h(); break;
                   9814:                case 0x09: pcbios_int_10h_09h(); break;
                   9815:                case 0x0a: pcbios_int_10h_0ah(); break;
                   9816:                case 0x0b: break;
                   9817:                case 0x0c: break;
                   9818:                case 0x0d: break;
                   9819:                case 0x0e: pcbios_int_10h_0eh(); break;
                   9820:                case 0x0f: pcbios_int_10h_0fh(); break;
                   9821:                case 0x10: break;
1.1.1.14  root     9822:                case 0x11: pcbios_int_10h_11h(); break;
                   9823:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     9824:                case 0x13: pcbios_int_10h_13h(); break;
                   9825:                case 0x18: REG8(AL) = 0x86; break;
1.1.1.14  root     9826:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     9827:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   9828:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     9829:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     9830:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   9831:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     9832:                case 0x4f: pcbios_int_10h_4fh(); break;
                   9833:                case 0x80: m_CF = 1; break; // unknown
                   9834:                case 0x81: m_CF = 1; break; // unknown
1.1       root     9835:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     9836:                case 0x83: pcbios_int_10h_83h(); break;
                   9837:                case 0x8b: break;
                   9838:                case 0x8c: m_CF = 1; break; // unknown
                   9839:                case 0x8d: m_CF = 1; break; // unknown
                   9840:                case 0x8e: m_CF = 1; break; // unknown
                   9841:                case 0x90: pcbios_int_10h_90h(); break;
                   9842:                case 0x91: pcbios_int_10h_91h(); break;
                   9843:                case 0x92: break;
                   9844:                case 0x93: break;
                   9845:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     9846:                case 0xfa: break; // ega register interface library is not installed
1.1       root     9847:                case 0xfe: pcbios_int_10h_feh(); break;
                   9848:                case 0xff: pcbios_int_10h_ffh(); break;
                   9849:                default:
1.1.1.22  root     9850:                        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));
                   9851:                        m_CF = 1;
1.1       root     9852:                        break;
                   9853:                }
                   9854:                break;
                   9855:        case 0x11:
                   9856:                // PC BIOS - Get Equipment List
1.1.1.11  root     9857: #ifdef SUPPORT_FPU
1.1.1.25! root     9858:                REG16(AX) = 0x122;
1.1.1.11  root     9859: #else
1.1.1.25! root     9860:                REG16(AX) = 0x120;
1.1.1.11  root     9861: #endif
1.1       root     9862:                break;
                   9863:        case 0x12:
                   9864:                // PC BIOS - Get Memory Size
                   9865:                REG16(AX) = MEMORY_END / 1024;
                   9866:                break;
                   9867:        case 0x13:
                   9868:                // PC BIOS - Disk
1.1.1.22  root     9869: //             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     9870:                REG8(AH) = 0xff;
1.1.1.3   root     9871:                m_CF = 1;
1.1       root     9872:                break;
                   9873:        case 0x14:
                   9874:                // PC BIOS - Serial I/O
1.1.1.25! root     9875:                switch(REG8(AH)) {
        !          9876:                case 0x00: pcbios_int_14h_00h(); break;
        !          9877:                case 0x01: pcbios_int_14h_01h(); break;
        !          9878:                case 0x02: pcbios_int_14h_02h(); break;
        !          9879:                case 0x03: pcbios_int_14h_03h(); break;
        !          9880:                case 0x04: pcbios_int_14h_04h(); break;
        !          9881:                case 0x05: pcbios_int_14h_05h(); break;
        !          9882:                default:
        !          9883:                        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));
        !          9884:                        break;
        !          9885:                }
1.1       root     9886:                break;
                   9887:        case 0x15:
                   9888:                // PC BIOS
1.1.1.3   root     9889:                m_CF = 0;
1.1       root     9890:                switch(REG8(AH)) {
1.1.1.14  root     9891:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     9892:                case 0x23: pcbios_int_15h_23h(); break;
                   9893:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     9894:                case 0x41: break;
1.1       root     9895:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     9896:                case 0x50: pcbios_int_15h_50h(); break;
1.1       root     9897:                case 0x86: pcbios_int_15h_86h(); break;
                   9898:                case 0x87: pcbios_int_15h_87h(); break;
                   9899:                case 0x88: pcbios_int_15h_88h(); break;
                   9900:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     9901:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     9902:                case 0xc0: // PS/2 ???
                   9903:                case 0xc1:
                   9904:                case 0xc2:
                   9905:                        REG8(AH) = 0x86;
                   9906:                        m_CF = 1;
                   9907:                        break;
1.1.1.3   root     9908: #if defined(HAS_I386)
1.1       root     9909:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     9910: #endif
1.1       root     9911:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     9912:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     9913:                default:
1.1.1.22  root     9914:                        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));
                   9915:                        REG8(AH) = 0x86;
1.1.1.3   root     9916:                        m_CF = 1;
1.1       root     9917:                        break;
                   9918:                }
                   9919:                break;
                   9920:        case 0x16:
                   9921:                // PC BIOS - Keyboard
1.1.1.3   root     9922:                m_CF = 0;
1.1       root     9923:                switch(REG8(AH)) {
                   9924:                case 0x00: pcbios_int_16h_00h(); break;
                   9925:                case 0x01: pcbios_int_16h_01h(); break;
                   9926:                case 0x02: pcbios_int_16h_02h(); break;
                   9927:                case 0x03: pcbios_int_16h_03h(); break;
                   9928:                case 0x05: pcbios_int_16h_05h(); break;
                   9929:                case 0x10: pcbios_int_16h_00h(); break;
                   9930:                case 0x11: pcbios_int_16h_01h(); break;
                   9931:                case 0x12: pcbios_int_16h_12h(); break;
                   9932:                case 0x13: pcbios_int_16h_13h(); break;
                   9933:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     9934:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.22  root     9935:                case 0xda: break; // unknown
                   9936:                case 0xff: break; // unknown
1.1       root     9937:                default:
1.1.1.22  root     9938:                        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     9939:                        break;
                   9940:                }
                   9941:                break;
                   9942:        case 0x17:
                   9943:                // PC BIOS - Printer
1.1.1.22  root     9944: //             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     9945:                break;
                   9946:        case 0x1a:
                   9947:                // PC BIOS - Timer
1.1.1.3   root     9948:                m_CF = 0;
1.1       root     9949:                switch(REG8(AH)) {
                   9950:                case 0x00: pcbios_int_1ah_00h(); break;
                   9951:                case 0x01: break;
                   9952:                case 0x02: pcbios_int_1ah_02h(); break;
                   9953:                case 0x03: break;
                   9954:                case 0x04: pcbios_int_1ah_04h(); break;
                   9955:                case 0x05: break;
                   9956:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   9957:                case 0x0b: break;
1.1.1.14  root     9958:                case 0x35: break; // Word Perfect Third Party Interface?
                   9959:                case 0x36: break; // Word Perfect Third Party Interface
                   9960:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     9961:                default:
1.1.1.22  root     9962:                        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     9963:                        break;
                   9964:                }
                   9965:                break;
                   9966:        case 0x20:
1.1.1.3   root     9967:                msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9968:                break;
                   9969:        case 0x21:
                   9970:                // MS-DOS System Call
1.1.1.3   root     9971:                m_CF = 0;
1.1       root     9972:                switch(REG8(AH)) {
                   9973:                case 0x00: msdos_int_21h_00h(); break;
                   9974:                case 0x01: msdos_int_21h_01h(); break;
                   9975:                case 0x02: msdos_int_21h_02h(); break;
                   9976:                case 0x03: msdos_int_21h_03h(); break;
                   9977:                case 0x04: msdos_int_21h_04h(); break;
                   9978:                case 0x05: msdos_int_21h_05h(); break;
                   9979:                case 0x06: msdos_int_21h_06h(); break;
                   9980:                case 0x07: msdos_int_21h_07h(); break;
                   9981:                case 0x08: msdos_int_21h_08h(); break;
                   9982:                case 0x09: msdos_int_21h_09h(); break;
                   9983:                case 0x0a: msdos_int_21h_0ah(); break;
                   9984:                case 0x0b: msdos_int_21h_0bh(); break;
                   9985:                case 0x0c: msdos_int_21h_0ch(); break;
                   9986:                case 0x0d: msdos_int_21h_0dh(); break;
                   9987:                case 0x0e: msdos_int_21h_0eh(); break;
1.1.1.14  root     9988:                case 0x0f: msdos_int_21h_0fh(); break;
                   9989:                case 0x10: msdos_int_21h_10h(); break;
1.1       root     9990:                case 0x11: msdos_int_21h_11h(); break;
                   9991:                case 0x12: msdos_int_21h_12h(); break;
                   9992:                case 0x13: msdos_int_21h_13h(); break;
1.1.1.16  root     9993:                case 0x14: msdos_int_21h_14h(); break;
                   9994:                case 0x15: msdos_int_21h_15h(); break;
1.1.1.14  root     9995:                case 0x16: msdos_int_21h_16h(); break;
1.1.1.16  root     9996:                case 0x17: msdos_int_21h_17h(); break;
1.1       root     9997:                case 0x18: msdos_int_21h_18h(); break;
                   9998:                case 0x19: msdos_int_21h_19h(); break;
                   9999:                case 0x1a: msdos_int_21h_1ah(); break;
                   10000:                case 0x1b: msdos_int_21h_1bh(); break;
                   10001:                case 0x1c: msdos_int_21h_1ch(); break;
                   10002:                case 0x1d: msdos_int_21h_1dh(); break;
                   10003:                case 0x1e: msdos_int_21h_1eh(); break;
                   10004:                case 0x1f: msdos_int_21h_1fh(); break;
                   10005:                case 0x20: msdos_int_21h_20h(); break;
1.1.1.14  root     10006:                case 0x21: msdos_int_21h_21h(); break;
                   10007:                case 0x22: msdos_int_21h_22h(); break;
1.1.1.16  root     10008:                case 0x23: msdos_int_21h_23h(); break;
                   10009:                case 0x24: msdos_int_21h_24h(); break;
1.1       root     10010:                case 0x25: msdos_int_21h_25h(); break;
                   10011:                case 0x26: msdos_int_21h_26h(); break;
1.1.1.16  root     10012:                case 0x27: msdos_int_21h_27h(); break;
                   10013:                case 0x28: msdos_int_21h_28h(); break;
1.1       root     10014:                case 0x29: msdos_int_21h_29h(); break;
                   10015:                case 0x2a: msdos_int_21h_2ah(); break;
                   10016:                case 0x2b: msdos_int_21h_2bh(); break;
                   10017:                case 0x2c: msdos_int_21h_2ch(); break;
                   10018:                case 0x2d: msdos_int_21h_2dh(); break;
                   10019:                case 0x2e: msdos_int_21h_2eh(); break;
                   10020:                case 0x2f: msdos_int_21h_2fh(); break;
                   10021:                case 0x30: msdos_int_21h_30h(); break;
                   10022:                case 0x31: msdos_int_21h_31h(); break;
                   10023:                case 0x32: msdos_int_21h_32h(); break;
                   10024:                case 0x33: msdos_int_21h_33h(); break;
1.1.1.23  root     10025:                case 0x34: msdos_int_21h_34h(); break;
1.1       root     10026:                case 0x35: msdos_int_21h_35h(); break;
                   10027:                case 0x36: msdos_int_21h_36h(); break;
                   10028:                case 0x37: msdos_int_21h_37h(); break;
1.1.1.14  root     10029:                case 0x38: msdos_int_21h_38h(); break;
1.1       root     10030:                case 0x39: msdos_int_21h_39h(0); break;
                   10031:                case 0x3a: msdos_int_21h_3ah(0); break;
                   10032:                case 0x3b: msdos_int_21h_3bh(0); break;
                   10033:                case 0x3c: msdos_int_21h_3ch(); break;
                   10034:                case 0x3d: msdos_int_21h_3dh(); break;
                   10035:                case 0x3e: msdos_int_21h_3eh(); break;
                   10036:                case 0x3f: msdos_int_21h_3fh(); break;
                   10037:                case 0x40: msdos_int_21h_40h(); break;
                   10038:                case 0x41: msdos_int_21h_41h(0); break;
                   10039:                case 0x42: msdos_int_21h_42h(); break;
                   10040:                case 0x43: msdos_int_21h_43h(0); break;
                   10041:                case 0x44: msdos_int_21h_44h(); break;
                   10042:                case 0x45: msdos_int_21h_45h(); break;
                   10043:                case 0x46: msdos_int_21h_46h(); break;
                   10044:                case 0x47: msdos_int_21h_47h(0); break;
                   10045:                case 0x48: msdos_int_21h_48h(); break;
                   10046:                case 0x49: msdos_int_21h_49h(); break;
                   10047:                case 0x4a: msdos_int_21h_4ah(); break;
                   10048:                case 0x4b: msdos_int_21h_4bh(); break;
                   10049:                case 0x4c: msdos_int_21h_4ch(); break;
                   10050:                case 0x4d: msdos_int_21h_4dh(); break;
                   10051:                case 0x4e: msdos_int_21h_4eh(); break;
                   10052:                case 0x4f: msdos_int_21h_4fh(); break;
                   10053:                case 0x50: msdos_int_21h_50h(); break;
                   10054:                case 0x51: msdos_int_21h_51h(); break;
                   10055:                case 0x52: msdos_int_21h_52h(); break;
                   10056:                // 0x53: translate bios parameter block to drive param bock
                   10057:                case 0x54: msdos_int_21h_54h(); break;
                   10058:                case 0x55: msdos_int_21h_55h(); break;
                   10059:                case 0x56: msdos_int_21h_56h(0); break;
                   10060:                case 0x57: msdos_int_21h_57h(); break;
                   10061:                case 0x58: msdos_int_21h_58h(); break;
                   10062:                case 0x59: msdos_int_21h_59h(); break;
                   10063:                case 0x5a: msdos_int_21h_5ah(); break;
                   10064:                case 0x5b: msdos_int_21h_5bh(); break;
                   10065:                case 0x5c: msdos_int_21h_5ch(); break;
1.1.1.22  root     10066:                case 0x5d: msdos_int_21h_5dh(); break;
1.1       root     10067:                // 0x5e: ms-network
                   10068:                // 0x5f: ms-network
                   10069:                case 0x60: msdos_int_21h_60h(0); break;
                   10070:                case 0x61: msdos_int_21h_61h(); break;
                   10071:                case 0x62: msdos_int_21h_62h(); break;
                   10072:                case 0x63: msdos_int_21h_63h(); break;
                   10073:                // 0x64: set device driver lockahead flag
                   10074:                case 0x65: msdos_int_21h_65h(); break;
                   10075:                case 0x66: msdos_int_21h_66h(); break;
                   10076:                case 0x67: msdos_int_21h_67h(); break;
                   10077:                case 0x68: msdos_int_21h_68h(); break;
                   10078:                case 0x69: msdos_int_21h_69h(); break;
                   10079:                case 0x6a: msdos_int_21h_6ah(); break;
                   10080:                case 0x6b: msdos_int_21h_6bh(); break;
                   10081:                case 0x6c: msdos_int_21h_6ch(0); break;
                   10082:                // 0x6d: find first rom program
                   10083:                // 0x6e: find next rom program
                   10084:                // 0x6f: get/set rom scan start address
                   10085:                // 0x70: windows95 get/set internationalization information
                   10086:                case 0x71:
                   10087:                        // windows95 long filename functions
                   10088:                        switch(REG8(AL)) {
                   10089:                        case 0x0d: msdos_int_21h_710dh(); break;
                   10090:                        case 0x39: msdos_int_21h_39h(1); break;
                   10091:                        case 0x3a: msdos_int_21h_3ah(1); break;
                   10092:                        case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.17  root     10093:                        case 0x41: msdos_int_21h_7141h(1); break;
1.1       root     10094:                        case 0x43: msdos_int_21h_43h(1); break;
                   10095:                        case 0x47: msdos_int_21h_47h(1); break;
                   10096:                        case 0x4e: msdos_int_21h_714eh(); break;
                   10097:                        case 0x4f: msdos_int_21h_714fh(); break;
                   10098:                        case 0x56: msdos_int_21h_56h(1); break;
                   10099:                        case 0x60: msdos_int_21h_60h(1); break;
                   10100:                        case 0x6c: msdos_int_21h_6ch(1); break;
                   10101:                        case 0xa0: msdos_int_21h_71a0h(); break;
                   10102:                        case 0xa1: msdos_int_21h_71a1h(); break;
                   10103:                        case 0xa6: msdos_int_21h_71a6h(); break;
                   10104:                        case 0xa7: msdos_int_21h_71a7h(); break;
                   10105:                        case 0xa8: msdos_int_21h_71a8h(); break;
                   10106:                        // 0xa9: server create/open file
1.1.1.22  root     10107:                        case 0xaa: msdos_int_21h_71aah(); break;
1.1       root     10108:                        default:
1.1.1.22  root     10109:                                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       root     10110:                                REG16(AX) = 0x7100;
1.1.1.3   root     10111:                                m_CF = 1;
1.1       root     10112:                                break;
                   10113:                        }
                   10114:                        break;
                   10115:                // 0x72: Windows95 beta - LFN FindClose
                   10116:                case 0x73:
                   10117:                        // windows95 fat32 functions
                   10118:                        switch(REG8(AL)) {
1.1.1.14  root     10119:                        case 0x00: msdos_int_21h_7300h(); break;
                   10120:                        // 0x01: set drive locking ???
                   10121:                        case 0x02: msdos_int_21h_7302h(); break;
1.1       root     10122:                        case 0x03: msdos_int_21h_7303h(); break;
                   10123:                        // 0x04: set dpb to use for formatting
1.1.1.25! root     10124:                        // 0x05: extended absolute disk read/write
1.1       root     10125:                        default:
1.1.1.22  root     10126:                                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       root     10127:                                REG16(AX) = 0x7300;
1.1.1.3   root     10128:                                m_CF = 1;
1.1       root     10129:                                break;
                   10130:                        }
                   10131:                        break;
                   10132:                default:
1.1.1.22  root     10133:                        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       root     10134:                        REG16(AX) = 0x01;
1.1.1.3   root     10135:                        m_CF = 1;
1.1       root     10136:                        break;
                   10137:                }
1.1.1.3   root     10138:                if(m_CF) {
1.1.1.23  root     10139:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10140:                        sda->extended_error_code = REG16(AX);
                   10141:                        switch(sda->extended_error_code) {
                   10142:                        case  4: // Too many open files
                   10143:                        case  8: // Insufficient memory
                   10144:                                sda->error_class = 1; // Out of resource
                   10145:                                break;
                   10146:                        case  5: // Access denied
                   10147:                                sda->error_class = 3; // Authorization
                   10148:                                break;
                   10149:                        case  7: // Memory control block destroyed
                   10150:                                sda->error_class = 4; // Internal
                   10151:                                break;
                   10152:                        case  2: // File not found
                   10153:                        case  3: // Path not found
                   10154:                        case 15: // Invaid drive specified
                   10155:                        case 18: // No more files
                   10156:                                sda->error_class = 8; // Not found
                   10157:                                break;
                   10158:                        case 32: // Sharing violation
                   10159:                        case 33: // Lock violation
                   10160:                                sda->error_class = 10; // Locked
                   10161:                                break;
                   10162: //                     case 16: // Removal of current directory attempted
                   10163:                        case 19: // Attempted write on protected disk
                   10164:                        case 21: // Drive not ready
                   10165: //                     case 29: // Write failure
                   10166: //                     case 30: // Read failure
                   10167: //                     case 82: // Cannot create subdirectory
                   10168:                                sda->error_class = 11; // Media
                   10169:                                break;
                   10170:                        case 80: // File already exists
                   10171:                                sda->error_class = 12; // Already exist
                   10172:                                break;
                   10173:                        default:
                   10174:                                sda->error_class = 13; // Unknown
                   10175:                                break;
                   10176:                        }
                   10177:                        sda->suggested_action = 1; // Retry
                   10178:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     10179:                }
                   10180:                break;
                   10181:        case 0x22:
                   10182:                fatalerror("int 22h (terminate address)\n");
                   10183:        case 0x23:
                   10184:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   10185:                break;
                   10186:        case 0x24:
                   10187:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   10188:                break;
                   10189:        case 0x25:
                   10190:                msdos_int_25h();
                   10191:                break;
                   10192:        case 0x26:
                   10193:                msdos_int_26h();
                   10194:                break;
                   10195:        case 0x27:
                   10196:                msdos_int_27h();
                   10197:                break;
                   10198:        case 0x28:
                   10199:                Sleep(10);
                   10200:                break;
                   10201:        case 0x29:
                   10202:                msdos_int_29h();
                   10203:                break;
                   10204:        case 0x2e:
                   10205:                msdos_int_2eh();
                   10206:                break;
                   10207:        case 0x2f:
                   10208:                // multiplex interrupt
                   10209:                switch(REG8(AH)) {
1.1.1.22  root     10210:                case 0x00: break;
                   10211:                case 0x01: msdos_int_2fh_01h(); break;
                   10212:                case 0x05: msdos_int_2fh_05h(); break;
                   10213:                case 0x06: msdos_int_2fh_06h(); break;
                   10214:                case 0x08: msdos_int_2fh_08h(); break;
                   10215:                case 0x10: msdos_int_2fh_10h(); break;
                   10216:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     10217:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.22  root     10218:                case 0x14: msdos_int_2fh_14h(); break;
                   10219:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     10220:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     10221:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     10222:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.22  root     10223:                case 0x1b: msdos_int_2fh_1bh(); break;
1.1       root     10224:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     10225:                case 0x46: msdos_int_2fh_46h(); break;
                   10226:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     10227:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     10228:                case 0x4b: msdos_int_2fh_4bh(); break;
                   10229:                case 0x4c: break; // unknown
                   10230:                case 0x4d: break; // unknown
                   10231:                case 0x4e: break; // unknown
1.1       root     10232:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     10233:                case 0x55: msdos_int_2fh_55h(); break;
                   10234:                case 0x58: break; // unknown
                   10235:                case 0x74: break; // unknown
1.1.1.24  root     10236:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     10237:                case 0xae: msdos_int_2fh_aeh(); break;
                   10238:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.22  root     10239:                case 0xe9: break; // unknown
                   10240:                case 0xfe: break; // norton utilities ???
                   10241:                default:
                   10242:                        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));
                   10243:                        break;
1.1       root     10244:                }
                   10245:                break;
1.1.1.24  root     10246:        case 0x33:
                   10247:                switch(REG8(AH)) {
                   10248:                case 0x00:
                   10249:                        // Mouse
                   10250:                        switch(REG8(AL)) {
                   10251:                        case 0x00: msdos_int_33h_0000h(); break;
                   10252:                        case 0x01: msdos_int_33h_0001h(); break;
                   10253:                        case 0x02: msdos_int_33h_0002h(); break;
                   10254:                        case 0x03: msdos_int_33h_0003h(); break;
                   10255:                        case 0x04: break; // position mouse cursor
                   10256:                        case 0x05: msdos_int_33h_0005h(); break;
                   10257:                        case 0x06: msdos_int_33h_0006h(); break;
                   10258:                        case 0x07: msdos_int_33h_0007h(); break;
                   10259:                        case 0x08: msdos_int_33h_0008h(); break;
                   10260:                        case 0x09: msdos_int_33h_0009h(); break;
                   10261:                        case 0x0a: break; // define text cursor
                   10262:                        case 0x0b: msdos_int_33h_000bh(); break;
                   10263:                        case 0x0c: msdos_int_33h_000ch(); break;
                   10264:                        case 0x0d: break; // light pen emulation on
                   10265:                        case 0x0e: break; // light pen emulation off
                   10266:                        case 0x0f: msdos_int_33h_000fh(); break;
                   10267:                        case 0x10: break; // define screen region for updating
                   10268:                        case 0x11: msdos_int_33h_0011h(); break;
                   10269:                        case 0x12: REG16(AX) = 0xffff; break; // set large graphics cursor block
                   10270:                        case 0x13: break; // define double-speed threshold
                   10271:                        case 0x14: msdos_int_33h_0014h(); break;
                   10272:                        case 0x15: msdos_int_33h_0015h(); break;
                   10273:                        case 0x16: msdos_int_33h_0016h(); break;
                   10274:                        case 0x17: msdos_int_33h_0017h(); break;
                   10275:                        case 0x1a: msdos_int_33h_001ah(); break;
                   10276:                        case 0x1b: msdos_int_33h_001bh(); break;
                   10277:                        case 0x1d: msdos_int_33h_001dh(); break;
                   10278:                        case 0x1e: msdos_int_33h_001eh(); break;
                   10279:                        case 0x21: msdos_int_33h_0021h(); break;
                   10280:                        case 0x22: msdos_int_33h_0022h(); break;
                   10281:                        case 0x23: msdos_int_33h_0023h(); break;
                   10282:                        case 0x24: msdos_int_33h_0024h(); break;
                   10283:                        case 0x26: msdos_int_33h_0026h(); break;
                   10284:                        case 0x2a: msdos_int_33h_002ah(); break;
                   10285:                        case 0x2f: break; // mouse hardware reset
                   10286:                        case 0x31: msdos_int_33h_0031h(); break;
                   10287:                        case 0x32: msdos_int_33h_0032h(); break;
                   10288:                        default:
                   10289:                                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));
                   10290:                                break;
                   10291:                        }
                   10292:                        break;
                   10293:                default:
                   10294:                        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));
                   10295:                        break;
                   10296:                }
                   10297:                break;
1.1.1.19  root     10298:        case 0x68:
                   10299:                // dummy interrupt for EMS (int 67h)
                   10300:                switch(REG8(AH)) {
                   10301:                case 0x40: msdos_int_67h_40h(); break;
                   10302:                case 0x41: msdos_int_67h_41h(); break;
                   10303:                case 0x42: msdos_int_67h_42h(); break;
                   10304:                case 0x43: msdos_int_67h_43h(); break;
                   10305:                case 0x44: msdos_int_67h_44h(); break;
                   10306:                case 0x45: msdos_int_67h_45h(); break;
                   10307:                case 0x46: msdos_int_67h_46h(); break;
                   10308:                case 0x47: msdos_int_67h_47h(); break;
                   10309:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     10310:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   10311:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     10312:                case 0x4b: msdos_int_67h_4bh(); break;
                   10313:                case 0x4c: msdos_int_67h_4ch(); break;
                   10314:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     10315:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     10316:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     10317:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     10318:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     10319:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     10320:                case 0x53: msdos_int_67h_53h(); break;
                   10321:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     10322:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   10323:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   10324:                case 0x57: msdos_int_67h_57h(); break;
                   10325:                case 0x58: msdos_int_67h_58h(); break;
                   10326:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   10327:                case 0x5a: msdos_int_67h_5ah(); break;
                   10328:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   10329:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   10330:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.19  root     10331:                default:
1.1.1.22  root     10332:                        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     10333:                        REG8(AH) = 0x84;
                   10334:                        break;
                   10335:                }
                   10336:                break;
                   10337: #ifdef SUPPORT_XMS
                   10338:        case 0x69:
                   10339:                // dummy interrupt for XMS (call far)
                   10340:                switch(REG8(AH)) {
                   10341:                case 0x00: msdos_call_xms_00h(); break;
                   10342:                case 0x01: msdos_call_xms_01h(); break;
                   10343:                case 0x02: msdos_call_xms_02h(); break;
                   10344:                case 0x03: msdos_call_xms_03h(); break;
                   10345:                case 0x04: msdos_call_xms_04h(); break;
                   10346:                case 0x05: msdos_call_xms_05h(); break;
                   10347:                case 0x06: msdos_call_xms_06h(); break;
                   10348:                case 0x07: msdos_call_xms_07h(); break;
                   10349:                case 0x08: msdos_call_xms_08h(); break;
                   10350:                case 0x09: msdos_call_xms_09h(); break;
                   10351:                case 0x0a: msdos_call_xms_0ah(); break;
                   10352:                case 0x0b: msdos_call_xms_0bh(); break;
                   10353:                case 0x0c: msdos_call_xms_0ch(); break;
                   10354:                case 0x0d: msdos_call_xms_0dh(); break;
                   10355:                case 0x0e: msdos_call_xms_0eh(); break;
                   10356:                case 0x0f: msdos_call_xms_0fh(); break;
                   10357:                case 0x10: msdos_call_xms_10h(); break;
                   10358:                case 0x11: msdos_call_xms_11h(); break;
                   10359:                case 0x12: msdos_call_xms_12h(); break;
                   10360:                // 0x88: XMS 3.0 - Query free extended memory
                   10361:                // 0x89: XMS 3.0 - Allocate any extended memory
                   10362:                // 0x8e: XMS 3.0 - Get extended EMB handle information
                   10363:                // 0x8f: XMS 3.0 - Reallocate any extended memory block
                   10364:                default:
1.1.1.22  root     10365:                        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));
1.1.1.19  root     10366:                        REG16(AX) = 0x0000;
                   10367:                        REG8(BL) = 0x80;
                   10368:                        break;
                   10369:                }
                   10370:                break;
                   10371: #endif
                   10372:        case 0x6a:
1.1.1.24  root     10373:                // irq12 (mouse)
                   10374:                mouse_push_ax = REG16(AX);
                   10375:                mouse_push_bx = REG16(BX);
                   10376:                mouse_push_cx = REG16(CX);
                   10377:                mouse_push_dx = REG16(DX);
                   10378:                mouse_push_si = REG16(SI);
                   10379:                mouse_push_di = REG16(DI);
                   10380:                
                   10381:                if(mouse.active && mouse.call_addr.dw != 0) {
                   10382:                        REG16(AX) = mouse.status_irq;
                   10383:                        REG16(BX) = mouse.get_buttons();
                   10384:                        REG16(CX) = max(mouse.min_position.x, min(mouse.max_position.x, mouse.position.x));
                   10385:                        REG16(DX) = max(mouse.min_position.y, min(mouse.max_position.y, mouse.position.y));
                   10386:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   10387:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   10388:                        
                   10389:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   10390:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   10391:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   10392:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   10393:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   10394:                } else {
                   10395:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   10396:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   10397:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   10398:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   10399:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   10400:                }
                   10401:                break;
                   10402:        case 0x6b:
                   10403:                // end of irq12 (mouse)
                   10404:                REG16(AX) = mouse_push_ax;
                   10405:                REG16(BX) = mouse_push_bx;
                   10406:                REG16(CX) = mouse_push_cx;
                   10407:                REG16(DX) = mouse_push_dx;
                   10408:                REG16(SI) = mouse_push_si;
                   10409:                REG16(DI) = mouse_push_di;
                   10410:                
                   10411:                // EOI
                   10412:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   10413:                        pic[0].isr &= ~(1 << 2); // master
                   10414:                }
                   10415:                pic_update();
                   10416:                break;
                   10417:        case 0x6c:
1.1.1.19  root     10418:                // dummy interrupt for case map routine pointed in the country info
                   10419:                if(REG8(AL) >= 0x80) {
                   10420:                        char tmp[2] = {0};
                   10421:                        tmp[0] = REG8(AL);
                   10422:                        my_strupr(tmp);
                   10423:                        REG8(AL) = tmp[0];
                   10424:                }
                   10425:                break;
1.1.1.8   root     10426:        case 0x70:
                   10427:        case 0x71:
                   10428:        case 0x72:
                   10429:        case 0x73:
                   10430:        case 0x74:
                   10431:        case 0x75:
                   10432:        case 0x76:
                   10433:        case 0x77:
                   10434:                // EOI
                   10435:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   10436:                        pic[0].isr &= ~(1 << 2); // master
                   10437:                }
                   10438:                pic_update();
                   10439:                break;
1.1       root     10440:        default:
1.1.1.22  root     10441: //             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     10442:                break;
                   10443:        }
                   10444:        
                   10445:        // update cursor position
                   10446:        if(cursor_moved) {
                   10447:                CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.23  root     10448:                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.15  root     10449:                if(!restore_console_on_exit) {
                   10450:                        scr_top = csbi.srWindow.Top;
                   10451:                }
1.1       root     10452:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
1.1.1.14  root     10453:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
1.1       root     10454:                cursor_moved = false;
                   10455:        }
                   10456: }
                   10457: 
                   10458: // init
                   10459: 
                   10460: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   10461: {
                   10462:        // init file handler
                   10463:        memset(file_handler, 0, sizeof(file_handler));
                   10464:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   10465:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   10466:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     10467: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     10468:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     10469: #else
                   10470:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   10471: #endif
                   10472:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     10473:        }
1.1.1.21  root     10474: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     10475:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.21  root     10476: #else
                   10477:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1       root     10478: #endif
1.1.1.21  root     10479:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   10480:        }
1.1       root     10481:        _dup2(0, DUP_STDIN);
                   10482:        _dup2(1, DUP_STDOUT);
                   10483:        _dup2(2, DUP_STDERR);
1.1.1.21  root     10484:        _dup2(3, DUP_STDAUX);
                   10485:        _dup2(4, DUP_STDPRN);
1.1       root     10486:        
1.1.1.24  root     10487:        // init mouse
                   10488:        memset(&mouse, 0, sizeof(mouse));
                   10489:        mouse.max_position.x = 8 * scr_width  - 1;
                   10490:        mouse.max_position.y = 8 * scr_height - 1;
                   10491:        mouse.mickey.x = 8;
                   10492:        mouse.mickey.y = 16;
                   10493:        
1.1       root     10494:        // init process
                   10495:        memset(process, 0, sizeof(process));
                   10496:        
1.1.1.13  root     10497:        // init dtainfo
                   10498:        msdos_dta_info_init();
                   10499:        
1.1       root     10500:        // init memory
                   10501:        memset(mem, 0, sizeof(mem));
                   10502:        
                   10503:        // bios data area
1.1.1.23  root     10504:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     10505:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   10506:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     10507:        CONSOLE_FONT_INFO cfi;
                   10508:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   10509:        
                   10510:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   10511:        text_vram_top_address = TEXT_VRAM_TOP;
                   10512:        text_vram_end_address = text_vram_top_address + regen;
                   10513:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   10514:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   10515:        
                   10516:        if(regen > 0x4000) {
                   10517:                regen = 0x8000;
                   10518:                vram_pages = 1;
                   10519:        } else if(regen > 0x2000) {
                   10520:                regen = 0x4000;
                   10521:                vram_pages = 2;
                   10522:        } else if(regen > 0x1000) {
                   10523:                regen = 0x2000;
                   10524:                vram_pages = 4;
                   10525:        } else {
                   10526:                regen = 0x1000;
                   10527:                vram_pages = 8;
                   10528:        }
1.1       root     10529:        
1.1.1.25! root     10530:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
        !          10531:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
        !          10532: //     *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
        !          10533: //     *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
        !          10534:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
        !          10535:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
        !          10536: //     *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
        !          10537:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.14  root     10538: #ifdef SUPPORT_FPU
1.1.1.25! root     10539:        *(UINT16 *)(mem + 0x410) = 0x122;
1.1.1.14  root     10540: #else
1.1.1.25! root     10541:        *(UINT16 *)(mem + 0x410) = 0x120;
1.1.1.14  root     10542: #endif
1.1       root     10543:        *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
                   10544:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     10545:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   10546:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     10547:        *(UINT16 *)(mem + 0x44e) = 0;
                   10548:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     10549:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     10550:        *(UINT8  *)(mem + 0x460) = 7;
                   10551:        *(UINT8  *)(mem + 0x461) = 7;
                   10552:        *(UINT8  *)(mem + 0x462) = 0;
                   10553:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     10554:        *(UINT8  *)(mem + 0x465) = 0x09;
                   10555:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     10556:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   10557:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   10558:        *(UINT8  *)(mem + 0x487) = 0x60;
                   10559:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.25! root     10560:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.14  root     10561:        
                   10562:        // initial screen
                   10563:        SMALL_RECT rect;
                   10564:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   10565:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   10566:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   10567:                for(int x = 0; x < scr_width; x++) {
                   10568:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   10569:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   10570:                }
                   10571:        }
1.1       root     10572:        
1.1.1.19  root     10573:        // init mcb
1.1       root     10574:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     10575:        
                   10576:        // iret table
                   10577:        // note: int 2eh vector should address the routine in command.com,
                   10578:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   10579:        // so move iret table into allocated memory block
                   10580:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
                   10581:        msdos_mcb_create(seg++, 'M', -1, IRET_SIZE >> 4);
                   10582:        IRET_TOP = seg << 4;
                   10583:        seg += IRET_SIZE >> 4;
1.1.1.25! root     10584:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     10585:        
                   10586:        // dummy xms/ems device
                   10587:        msdos_mcb_create(seg++, 'M', -1, XMS_SIZE >> 4);
                   10588:        XMS_TOP = seg << 4;
                   10589:        seg += XMS_SIZE >> 4;
                   10590:        
                   10591:        // environment
1.1       root     10592:        msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
                   10593:        int env_seg = seg;
                   10594:        int ofs = 0;
1.1.1.24  root     10595:        char env_path[8192] = "", *path, temp_path[MAX_PATH] = {0};
1.1       root     10596:        
                   10597:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1.1.14  root     10598:                if(_strnicmp(*p, "MSDOS_PATH=", 11) == 0) {
                   10599:                        sprintf(env_path, "%s;", *p + 11);
                   10600:                        break;
                   10601:                }
                   10602:        }
                   10603:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1.1.9   root     10604:                if(_strnicmp(*p, "PATH=", 5) == 0) {
1.1.1.14  root     10605:                        strcat(env_path, *p + 5);
1.1.1.9   root     10606:                        break;
                   10607:                }
                   10608:        }
1.1.1.18  root     10609:        if((path = getenv("MSDOS_COMSPEC")) != NULL ||
                   10610:           (path = msdos_search_command_com(argv[0], env_path)) != NULL) {
1.1.1.15  root     10611:                strcpy(comspec_path, path);
                   10612:        }
1.1.1.24  root     10613:        if((path = getenv("MSDOS_TEMP")) != NULL) {
                   10614:                if(GetShortPathName(path, temp_path, MAX_PATH) == 0) {
                   10615:                        strcpy(temp_path, path);
                   10616:                }
                   10617:        }
1.1.1.15  root     10618:        
1.1.1.9   root     10619:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     10620:                // lower to upper
                   10621:                char tmp[ENV_SIZE], name[ENV_SIZE], value[ENV_SIZE];
                   10622:                int value_pos = 0;
                   10623:                strcpy(tmp, *p);
                   10624:                for(int i = 0;; i++) {
                   10625:                        if(tmp[i] == '=') {
                   10626:                                tmp[i] = '\0';
                   10627:                                sprintf(name, ";%s;", tmp);
1.1.1.25! root     10628:                                my_strupr(name);
1.1       root     10629:                                strcpy(value, tmp + (value_pos = i + 1));
                   10630:                                tmp[i] = '=';
                   10631:                                break;
                   10632:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
                   10633:                                tmp[i] = tmp[i] - 'a' + 'A';
                   10634:                        }
                   10635:                }
1.1.1.18  root     10636:                if(strncmp(tmp, "MSDOS_COMSPEC=", 14) == 0) {
                   10637:                        // ignore MSDOS_COMSPEC
1.1.1.24  root     10638:                } else if(strncmp(tmp, "MSDOS_TEMP=", 11) == 0) {
                   10639:                        // ignore MSDOS_TEMP
1.1.1.18  root     10640:                } else if(standard_env && strstr(";COMSPEC;INCLUDE;LIB;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
                   10641:                        // ignore non standard environments
                   10642:                } else {
1.1       root     10643:                        if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     10644:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.24  root     10645:                        } else if(strncmp(tmp, "TEMP=", 5) == 0 && temp_path[0] != '\0') {
                   10646:                                sprintf(tmp, "TEMP=%s", temp_path);
                   10647:                        } else if(strncmp(tmp, "TMP=",  4) == 0 && temp_path[0] != '\0') {
                   10648:                                sprintf(tmp, "TMP=%s", temp_path);
1.1       root     10649:                        } else if(strncmp(tmp, "PATH=", 5) == 0 || strncmp(tmp, "TEMP=", 5) == 0 || strncmp(tmp, "TMP=", 4) == 0) {
                   10650:                                tmp[value_pos] = '\0';
                   10651:                                char *token = my_strtok(value, ";");
                   10652:                                while(token != NULL) {
                   10653:                                        if(strlen(token) != 0) {
1.1.1.24  root     10654:                                                char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
1.1.1.8   root     10655:                                                if(strlen(path) != 0) {
1.1.1.24  root     10656:                                                        if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
                   10657:                                                                strcat(tmp, path);
                   10658:                                                        } else {
                   10659:                                                                strcat(tmp, short_path);
                   10660:                                                        }
1.1.1.8   root     10661:                                                        strcat(tmp, ";");
1.1       root     10662:                                                }
                   10663:                                        }
                   10664:                                        token = my_strtok(NULL, ";");
                   10665:                                }
                   10666:                                tmp[strlen(tmp) - 1] = '\0';
                   10667:                                my_strupr(tmp);
                   10668:                        }
                   10669:                        int len = strlen(tmp);
1.1.1.14  root     10670:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     10671:                                fatalerror("too many environments\n");
                   10672:                        }
                   10673:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   10674:                        ofs += len + 1;
                   10675:                }
                   10676:        }
                   10677:        seg += (ENV_SIZE >> 4);
                   10678:        
                   10679:        // psp
                   10680:        msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
                   10681:        current_psp = seg;
1.1.1.14  root     10682:        msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
1.1       root     10683:        seg += (PSP_SIZE >> 4);
                   10684:        
1.1.1.19  root     10685:        // first free mcb in conventional memory
                   10686:        msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     10687:        first_mcb = seg;
                   10688:        
1.1.1.19  root     10689:        // dummy mcb to link to umb
                   10690:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', -1, (UMB_TOP >> 4) - (MEMORY_END >> 4));
                   10691:        
                   10692:        // first free mcb in upper memory block
1.1.1.8   root     10693:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     10694:        
1.1.1.19  root     10695: #ifdef SUPPORT_XMS
                   10696:        // first free mcb in extended memory block
                   10697:        msdos_mcb_create(EMB_TOP >> 4, 'Z', 0, (EMB_END >> 4) - (EMB_TOP >> 4) - 1);
                   10698: #endif
                   10699:        
1.1.1.25! root     10700:        // dos info
        !          10701:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
        !          10702:        dos_info->magic_word = 1;
        !          10703:        dos_info->first_mcb = MEMORY_TOP >> 4;
        !          10704:        dos_info->first_dpb.w.l = 0;
        !          10705:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
        !          10706:        dos_info->first_sft.w.l = 0;
        !          10707:        dos_info->first_sft.w.h = SFT_TOP >> 4;
        !          10708:        dos_info->clock_device.w.l = 22 + 18;
        !          10709:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
        !          10710:        dos_info->con_device.w.l = 22;
        !          10711:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
        !          10712:        dos_info->max_sector_len = 512;
        !          10713:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
        !          10714:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
        !          10715:        dos_info->cds.w.l = 0;
        !          10716:        dos_info->cds.w.h = CDS_TOP >> 4;
        !          10717:        dos_info->fcb_table.w.l = 0;
        !          10718:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
        !          10719:        dos_info->last_drive = 'Z' - 'A' + 1;
        !          10720:        dos_info->buffers_x = 20;
        !          10721:        dos_info->buffers_y = 0;
        !          10722:        dos_info->boot_drive = 'C' - 'A' + 1;
        !          10723:        dos_info->nul_device.next_driver.w.l = 0;
        !          10724:        dos_info->nul_device.next_driver.w.h = XMS_TOP >> 4;
        !          10725:        dos_info->nul_device.attributes = 0x8004;
        !          10726:        dos_info->nul_device.strategy = 0xffff;
        !          10727:        dos_info->nul_device.interrupt = 0xffff;
        !          10728:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
        !          10729:        dos_info->disk_buf_heads.w.l = 0;
        !          10730:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
        !          10731:        dos_info->first_umb_fcb = UMB_TOP >> 4;
        !          10732:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
        !          10733:        
        !          10734:        char *env;
        !          10735:        if((env = getenv("LASTDRIVE")) != NULL) {
        !          10736:                if(env[0] >= 'A' && env[0] <= 'Z') {
        !          10737:                        dos_info->last_drive = env[0] - 'A' + 1;
        !          10738:                } else if(env[0] >= 'a' && env[0] <= 'z') {
        !          10739:                        dos_info->last_drive = env[0] - 'a' + 1;
        !          10740:                }
        !          10741:        }
        !          10742:        if((env = getenv("windir")) != NULL) {
        !          10743:                if(env[0] >= 'A' && env[0] <= 'Z') {
        !          10744:                        dos_info->boot_drive = env[0] - 'A' + 1;
        !          10745:                } else if(env[0] >= 'a' && env[0] <= 'z') {
        !          10746:                        dos_info->boot_drive = env[0] - 'a' + 1;
        !          10747:                }
        !          10748:        }
        !          10749: #if defined(HAS_I386)
        !          10750:        dos_info->i386_or_later = 1;
        !          10751: #else
        !          10752:        dos_info->i386_or_later = 0;
        !          10753: #endif
        !          10754:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
        !          10755:        
1.1.1.19  root     10756:        // interrupt vector
                   10757:        for(int i = 0; i < 0x80; i++) {
                   10758:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   10759:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   10760:        }
1.1.1.25! root     10761:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0010;       // fffd:0010 irq0
1.1.1.24  root     10762:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
1.1.1.25! root     10763:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
1.1.1.19  root     10764:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
1.1.1.25! root     10765:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0018;       // xxxx:0018 ems
1.1.1.19  root     10766:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.25! root     10767:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12
1.1.1.24  root     10768:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
1.1.1.19  root     10769:        
1.1.1.25! root     10770:        // dummy devices (CON -> CLOCK$ -> NUL -> EMMXXXX0)
        !          10771:        static const UINT8 device_top[] = {
        !          10772:                // from Windows 98 SE
        !          10773:                0x3f, 0x01, 0x01, 0xc9, 0x00, 0xe9, 0x6a, 0x04,
        !          10774:                0x67, 0x13, 0x00, 0x00, 0xfc, 0x01, 0xcf, 0x00,
        !          10775:                0x0b, 0x02, 0x5a, 0x03, 0xc9, 0x00,
        !          10776:        };
        !          10777:        memcpy(mem + DEVICE_TOP, device_top, sizeof(device_top));
        !          10778:        
        !          10779:        device_t *con_device = (device_t *)(mem + DEVICE_TOP + 22);
        !          10780:        con_device->next_driver.w.l = 22 + 18;
        !          10781:        con_device->next_driver.w.h = DEVICE_TOP >> 4;
        !          10782:        con_device->attributes = 0x8013;
        !          10783:        con_device->strategy = 0xffff;
        !          10784:        con_device->interrupt = 0xffff;
        !          10785:        memcpy(con_device->dev_name, "CON     ", 8);
        !          10786:        
        !          10787:        device_t *clk_device = (device_t *)(mem + DEVICE_TOP + 22 + 18);
        !          10788:        clk_device->next_driver.w.l = offsetof(dos_info_t, nul_device);
        !          10789:        clk_device->next_driver.w.h = DOS_INFO_TOP >> 4;
        !          10790:        clk_device->attributes = 0x8008;
        !          10791:        clk_device->strategy = 0xffff;
        !          10792:        clk_device->interrupt = 0xffff;
        !          10793:        memcpy(clk_device->dev_name, "CLOCK$  ", 8);
        !          10794:        
1.1.1.19  root     10795:        // ems (int 67h) and xms (call far)
1.1.1.25! root     10796:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
        !          10797:        xms_device->next_driver.w.l = 0xffff;
        !          10798:        xms_device->next_driver.w.h = 0xffff;
        !          10799:        xms_device->attributes = 0xc000;
        !          10800:        xms_device->strategy = 0xffff;
        !          10801:        xms_device->interrupt = 0xffff;
        !          10802:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
        !          10803:        
        !          10804:        mem[XMS_TOP + 0x18] = 0xcd;     // int 68h (dummy)
        !          10805:        mem[XMS_TOP + 0x19] = 0x68;
        !          10806:        mem[XMS_TOP + 0x1a] = 0xcf;     // iret
1.1.1.19  root     10807: #ifdef SUPPORT_XMS
                   10808:        if(support_xms) {
1.1.1.25! root     10809:                mem[XMS_TOP + 0x1c] = 0xcd;     // int 69h (dummy)
        !          10810:                mem[XMS_TOP + 0x1d] = 0x69;
        !          10811:                mem[XMS_TOP + 0x1e] = 0xcb;     // retf
1.1.1.19  root     10812:        } else
                   10813: #endif
1.1.1.25! root     10814:        mem[XMS_TOP + 0x1c] = 0xcb;     // retf
1.1.1.19  root     10815:        
1.1.1.24  root     10816:        // irq12 (mouse)
                   10817:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   10818:        mem[0xfffd0 + 0x01] = 0x6a;
                   10819:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   10820:        mem[0xfffd0 + 0x03] = 0xff;
                   10821:        mem[0xfffd0 + 0x04] = 0xff;
                   10822:        mem[0xfffd0 + 0x05] = 0xff;
                   10823:        mem[0xfffd0 + 0x06] = 0xff;
                   10824:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   10825:        mem[0xfffd0 + 0x08] = 0x6b;
                   10826:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   10827:        
1.1.1.19  root     10828:        // case map routine (call far)
1.1.1.24  root     10829:        mem[0xfffd0 + 0x0c] = 0xcd;     // int 6ch (dummy)
                   10830:        mem[0xfffd0 + 0x0d] = 0x6b;
                   10831:        mem[0xfffd0 + 0x0e] = 0xcb;     // retf
1.1.1.19  root     10832:        
1.1.1.14  root     10833:        // have irq0 call system timer tick
1.1.1.24  root     10834:        mem[0xfffd0 + 0x10] = 0xcd;     // int 1ch
                   10835:        mem[0xfffd0 + 0x11] = 0x1c;
                   10836:        mem[0xfffd0 + 0x12] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   10837:        mem[0xfffd0 + 0x13] = 0x08;
                   10838:        mem[0xfffd0 + 0x14] = 0x00;
                   10839:        mem[0xfffd0 + 0x15] = ((IRET_TOP >> 4)     ) & 0xff;
                   10840:        mem[0xfffd0 + 0x16] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     10841:        
1.1       root     10842:        // boot
                   10843:        mem[0xffff0] = 0xf4;    // halt
                   10844:        mem[0xffff1] = 0xcd;    // int 21h
                   10845:        mem[0xffff2] = 0x21;
                   10846:        mem[0xffff3] = 0xcb;    // retf
                   10847:        
1.1.1.24  root     10848:        mem[0xffff5] = '0';     // rom date
                   10849:        mem[0xffff6] = '2';
                   10850:        mem[0xffff7] = '/';
                   10851:        mem[0xffff8] = '2';
                   10852:        mem[0xffff9] = '2';
                   10853:        mem[0xffffa] = '/';
                   10854:        mem[0xffffb] = '0';
                   10855:        mem[0xffffc] = '6';
                   10856:        mem[0xffffe] = 0xfc;    // machine id
                   10857:        mem[0xfffff] = 0x00;
                   10858:        
1.1       root     10859:        // param block
                   10860:        // + 0: param block (22bytes)
                   10861:        // +24: fcb1/2 (20bytes)
                   10862:        // +44: command tail (128bytes)
                   10863:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   10864:        param->env_seg = 0;
                   10865:        param->cmd_line.w.l = 44;
                   10866:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   10867:        param->fcb1.w.l = 24;
                   10868:        param->fcb1.w.h = (WORK_TOP >> 4);
                   10869:        param->fcb2.w.l = 24;
                   10870:        param->fcb2.w.h = (WORK_TOP >> 4);
                   10871:        
                   10872:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   10873:        
                   10874:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   10875:        if(argc > 1) {
                   10876:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   10877:                for(int i = 2; i < argc; i++) {
                   10878:                        char tmp[128];
                   10879:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   10880:                        strcpy(cmd_line->cmd, tmp);
                   10881:                }
                   10882:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   10883:        } else {
                   10884:                cmd_line->len = 0;
                   10885:        }
                   10886:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   10887:        
                   10888:        // system file table
1.1.1.21  root     10889:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   10890:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     10891:        
1.1.1.19  root     10892:        // disk buffer header (from DOSBox)
                   10893:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   10894:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   10895:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   10896:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   10897:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   10898:        
1.1       root     10899:        // current directory structure
                   10900:        msdos_cds_update(_getdrive() - 1);
                   10901:        
                   10902:        // fcb table
                   10903:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     10904:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     10905:        
1.1.1.22  root     10906:        // error table
                   10907:        *(UINT8 *)(mem + ERR_TABLE_TOP + 0) = 0xff;
                   10908:        *(UINT8 *)(mem + ERR_TABLE_TOP + 1) = 0x04;
                   10909:        *(UINT8 *)(mem + ERR_TABLE_TOP + 2) = 0x00;
                   10910:        *(UINT8 *)(mem + ERR_TABLE_TOP + 3) = 0x00;
                   10911:        
1.1.1.17  root     10912:        // nls stuff
                   10913:        msdos_nls_tables_init();
1.1       root     10914:        
                   10915:        // execute command
                   10916:        if(msdos_process_exec(argv[0], param, 0)) {
                   10917:                fatalerror("'%s' not found\n", argv[0]);
                   10918:        }
                   10919:        retval = 0;
                   10920:        return(0);
                   10921: }
                   10922: 
                   10923: #define remove_std_file(path) { \
                   10924:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   10925:        if(fd != -1) { \
                   10926:                _lseek(fd, 0, SEEK_END); \
                   10927:                int size = _tell(fd); \
                   10928:                _close(fd); \
                   10929:                if(size == 0) { \
                   10930:                        remove(path); \
                   10931:                } \
                   10932:        } \
                   10933: }
                   10934: 
                   10935: void msdos_finish()
                   10936: {
                   10937:        for(int i = 0; i < MAX_FILES; i++) {
                   10938:                if(file_handler[i].valid) {
                   10939:                        _close(i);
                   10940:                }
                   10941:        }
1.1.1.21  root     10942: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     10943:        remove_std_file("stdaux.txt");
1.1.1.21  root     10944: #endif
                   10945: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     10946:        remove_std_file("stdprn.txt");
                   10947: #endif
                   10948:        msdos_dbcs_table_finish();
                   10949: }
                   10950: 
                   10951: /* ----------------------------------------------------------------------------
                   10952:        PC/AT hardware emulation
                   10953: ---------------------------------------------------------------------------- */
                   10954: 
                   10955: void hardware_init()
                   10956: {
1.1.1.3   root     10957:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     10958:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     10959:        m_IF = 1;
1.1.1.3   root     10960: #if defined(HAS_I386)
1.1       root     10961:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   10962:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     10963: #endif
                   10964:        i386_set_a20_line(0);
1.1.1.14  root     10965:        
1.1.1.19  root     10966:        ems_init();
1.1.1.25! root     10967:        dma_init();
1.1       root     10968:        pic_init();
1.1.1.25! root     10969:        pio_init();
1.1.1.8   root     10970: #ifdef PIT_ALWAYS_RUNNING
                   10971:        pit_init();
                   10972: #else
1.1       root     10973:        pit_active = 0;
                   10974: #endif
1.1.1.25! root     10975:        sio_init();
1.1.1.8   root     10976:        cmos_init();
                   10977:        kbd_init();
1.1       root     10978: }
                   10979: 
1.1.1.10  root     10980: void hardware_finish()
                   10981: {
                   10982: #if defined(HAS_I386)
                   10983:        vtlb_free(m_vtlb);
                   10984: #endif
1.1.1.19  root     10985:        ems_finish();
1.1.1.25! root     10986:        sio_finish();
1.1.1.10  root     10987: }
                   10988: 
1.1       root     10989: void hardware_run()
                   10990: {
                   10991:        int ops = 0;
                   10992:        
1.1.1.22  root     10993: #ifdef EXPORT_DEBUG_TO_FILE
                   10994:        fdebug = fopen("debug.log", "w");
                   10995: #endif
1.1.1.3   root     10996:        while(!m_halted) {
1.1.1.22  root     10997: #ifdef ENABLE_DEBUG_DASM
                   10998:                if(dasm > 0) {
1.1       root     10999:                        char buffer[256];
1.1.1.3   root     11000: #if defined(HAS_I386)
1.1.1.22  root     11001:                        UINT32 flags = get_flags();
1.1.1.19  root     11002:                        UINT32 eip = m_eip;
1.1.1.3   root     11003: #else
1.1.1.22  root     11004:                        UINT32 flags = CompressFlags();
1.1.1.19  root     11005:                        UINT32 eip = m_pc - SREG_BASE(CS);
1.1.1.3   root     11006: #endif
                   11007:                        UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1       root     11008:                        
1.1.1.3   root     11009: #if defined(HAS_I386)
                   11010:                        if(m_operand_size) {
1.1       root     11011:                                CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3   root     11012:                        } else
                   11013: #endif
                   11014:                        CPU_DISASSEMBLE_CALL(x86_16);
1.1.1.22  root     11015:                        
                   11016:                        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",
                   11017:                        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,
                   11018: #if defined(HAS_I386)
                   11019:                        PROTECTED_MODE ? "PE" : "--",
                   11020: #else
                   11021:                        "--",
                   11022: #endif
                   11023:                        (flags & 0x40000) ? 'A' : '-',
                   11024:                        (flags & 0x20000) ? 'V' : '-',
                   11025:                        (flags & 0x10000) ? 'R' : '-',
                   11026:                        (flags & 0x04000) ? 'N' : '-',
                   11027:                        (flags & 0x02000) ? '1' : '0',
                   11028:                        (flags & 0x01000) ? '1' : '0',
                   11029:                        (flags & 0x00800) ? 'O' : '-',
                   11030:                        (flags & 0x00400) ? 'D' : '-',
                   11031:                        (flags & 0x00200) ? 'I' : '-',
                   11032:                        (flags & 0x00100) ? 'T' : '-',
                   11033:                        (flags & 0x00080) ? 'S' : '-',
                   11034:                        (flags & 0x00040) ? 'Z' : '-',
                   11035:                        (flags & 0x00010) ? 'A' : '-',
                   11036:                        (flags & 0x00004) ? 'P' : '-',
                   11037:                        (flags & 0x00001) ? 'C' : '-');
                   11038:                        fprintf(fdebug, "%04X:%04X\t%s\n", SREG(CS), (unsigned)eip, buffer);
                   11039:                        dasm--;
1.1       root     11040:                }
                   11041: #endif
1.1.1.3   root     11042: #if defined(HAS_I386)
                   11043:                m_cycles = 1;
1.1       root     11044:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     11045: #else
                   11046:                CPU_EXECUTE_CALL(CPU_MODEL);
                   11047: #endif
1.1.1.14  root     11048: #if defined(HAS_I386)
                   11049:                if(m_eip != m_prev_eip) {
                   11050: #else
                   11051:                if(m_pc != m_prevpc) {
                   11052: #endif
                   11053:                        iops++;
                   11054:                }
1.1.1.8   root     11055:                if(++ops == 16384) {
1.1       root     11056:                        hardware_update();
                   11057:                        ops = 0;
                   11058:                }
                   11059:        }
1.1.1.22  root     11060: #ifdef EXPORT_DEBUG_TO_FILE
                   11061:        fclose(fdebug);
                   11062: #endif
1.1       root     11063: }
                   11064: 
                   11065: void hardware_update()
                   11066: {
1.1.1.8   root     11067:        static UINT32 prev_time = 0;
                   11068:        UINT32 cur_time = timeGetTime();
                   11069:        
                   11070:        if(prev_time != cur_time) {
                   11071:                // update pit and raise irq0
                   11072: #ifndef PIT_ALWAYS_RUNNING
                   11073:                if(pit_active)
                   11074: #endif
                   11075:                {
                   11076:                        if(pit_run(0, cur_time)) {
                   11077:                                pic_req(0, 0, 1);
                   11078:                        }
                   11079:                        pit_run(1, cur_time);
                   11080:                        pit_run(2, cur_time);
                   11081:                }
1.1.1.24  root     11082:                
1.1.1.25! root     11083:                // update sio and raise irq4/3
        !          11084:                for(int c = 0; c < 2; c++) {
        !          11085:                        sio_update(c);
        !          11086:                }
        !          11087:                
1.1.1.24  root     11088:                // update keyboard and mouse
1.1.1.14  root     11089:                static UINT32 prev_tick = 0;
                   11090:                UINT32 cur_tick = cur_time / 32;
1.1.1.25! root     11091:                
1.1.1.14  root     11092:                if(prev_tick != cur_tick) {
                   11093:                        // update keyboard flags
                   11094:                        UINT8 state;
1.1.1.24  root     11095:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   11096:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   11097:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   11098:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   11099:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   11100:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   11101:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   11102:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     11103:                        mem[0x417] = state;
                   11104:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   11105:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   11106:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   11107:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     11108: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   11109: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     11110:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   11111:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   11112:                        mem[0x418] = state;
                   11113:                        
1.1.1.24  root     11114:                        // update console input if needed
                   11115:                        if(!key_changed || mouse.active) {
                   11116:                                update_console_input();
                   11117:                        }
                   11118:                        
                   11119:                        // raise irq1 if key is pressed/released
                   11120:                        if(key_changed) {
1.1.1.8   root     11121:                                pic_req(0, 1, 1);
1.1.1.24  root     11122:                                key_changed = false;
                   11123:                        }
                   11124:                        
                   11125:                        // raise irq12 if mouse status is changed
                   11126:                        if(mouse.status & mouse.call_mask) {
                   11127:                                if(mouse.active) {
                   11128:                                        pic_req(1, 4, 1);
                   11129:                                        mouse.status_irq = mouse.status & mouse.call_mask;
                   11130:                                }
                   11131:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     11132:                        }
1.1.1.24  root     11133:                        
1.1.1.14  root     11134:                        prev_tick = cur_tick;
1.1.1.8   root     11135:                }
1.1.1.24  root     11136:                
1.1.1.19  root     11137:                // update daily timer counter
                   11138:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25! root     11139:                
1.1.1.8   root     11140:                prev_time = cur_time;
1.1       root     11141:        }
                   11142: }
                   11143: 
1.1.1.19  root     11144: // ems
                   11145: 
                   11146: void ems_init()
                   11147: {
                   11148:        memset(ems_handles, 0, sizeof(ems_handles));
                   11149:        memset(ems_pages, 0, sizeof(ems_pages));
                   11150:        free_ems_pages = MAX_EMS_PAGES;
                   11151: }
                   11152: 
                   11153: void ems_finish()
                   11154: {
                   11155:        for(int i = 0; i < MAX_EMS_HANDLES; i++) {
                   11156:                if(ems_handles[i].buffer) {
                   11157:                        free(ems_handles[i].buffer);
                   11158:                        ems_handles[i].buffer = NULL;
                   11159:                }
                   11160:        }
                   11161: }
                   11162: 
                   11163: void ems_allocate_pages(int handle, int pages)
                   11164: {
                   11165:        if(pages > 0) {
                   11166:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   11167:        } else {
                   11168:                ems_handles[handle].buffer = NULL;
                   11169:        }
                   11170:        ems_handles[handle].pages = pages;
                   11171:        ems_handles[handle].allocated = true;
                   11172:        free_ems_pages -= pages;
                   11173: }
                   11174: 
                   11175: void ems_reallocate_pages(int handle, int pages)
                   11176: {
                   11177:        if(ems_handles[handle].allocated) {
                   11178:                if(ems_handles[handle].pages != pages) {
                   11179:                        UINT8 *new_buffer = NULL;
                   11180:                        
                   11181:                        if(pages > 0) {
                   11182:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   11183:                        }
                   11184:                        if(ems_handles[handle].buffer) {
                   11185:                                if(new_buffer) {
                   11186:                                        if(pages > ems_handles[handle].pages) {
                   11187:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   11188:                                        } else {
                   11189:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   11190:                                        }
                   11191:                                }
                   11192:                                free(ems_handles[handle].buffer);
                   11193:                                ems_handles[handle].buffer = NULL;
                   11194:                        }
                   11195:                        free_ems_pages += ems_handles[handle].pages;
                   11196:                        
                   11197:                        ems_handles[handle].buffer = new_buffer;
                   11198:                        ems_handles[handle].pages = pages;
                   11199:                        free_ems_pages -= pages;
                   11200:                }
                   11201:        } else {
                   11202:                ems_allocate_pages(handle, pages);
                   11203:        }
                   11204: }
                   11205: 
                   11206: void ems_release_pages(int handle)
                   11207: {
                   11208:        if(ems_handles[handle].allocated) {
                   11209:                if(ems_handles[handle].buffer) {
                   11210:                        free(ems_handles[handle].buffer);
                   11211:                        ems_handles[handle].buffer = NULL;
                   11212:                }
                   11213:                free_ems_pages += ems_handles[handle].pages;
                   11214:                ems_handles[handle].allocated = false;
                   11215:        }
                   11216: }
                   11217: 
                   11218: void ems_map_page(int physical, int handle, int logical)
                   11219: {
                   11220:        if(ems_pages[physical].mapped) {
                   11221:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   11222:                        return;
                   11223:                }
                   11224:                ems_unmap_page(physical);
                   11225:        }
                   11226:        if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   11227:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   11228:        }
                   11229:        ems_pages[physical].handle = handle;
                   11230:        ems_pages[physical].page = logical;
                   11231:        ems_pages[physical].mapped = true;
                   11232: }
                   11233: 
                   11234: void ems_unmap_page(int physical)
                   11235: {
                   11236:        if(ems_pages[physical].mapped) {
                   11237:                int handle = ems_pages[physical].handle;
                   11238:                int logical = ems_pages[physical].page;
                   11239:                
                   11240:                if(ems_handles[handle].allocated && ems_handles[handle].buffer && logical < ems_handles[handle].pages) {
                   11241:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   11242:                }
                   11243:                ems_pages[physical].mapped = false;
                   11244:        }
                   11245: }
                   11246: 
1.1.1.25! root     11247: // dma
1.1       root     11248: 
1.1.1.25! root     11249: void dma_init()
1.1       root     11250: {
1.1.1.25! root     11251:        for(int c = 0; c < 2; c++) {
        !          11252:                dma_reset(c);
        !          11253:        }
1.1       root     11254: }
                   11255: 
1.1.1.25! root     11256: void dma_reset(int c)
1.1       root     11257: {
1.1.1.25! root     11258:        dma[c].low_high = false;
        !          11259:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
        !          11260:        dma[c].mask = 0xff;
        !          11261: }
        !          11262: 
        !          11263: void dma_write(int c, UINT32 addr, UINT8 data)
        !          11264: {
        !          11265:        int ch = (addr >> 1) & 3;
        !          11266:        UINT8 bit = 1 << (data & 3);
        !          11267:        
        !          11268:        switch(addr & 0x0f) {
        !          11269:        case 0x00: case 0x02: case 0x04: case 0x06:
        !          11270:                if(dma[c].low_high) {
        !          11271:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     11272:                } else {
1.1.1.25! root     11273:                        dma[c].ch[ch].bareg.b.l = data;
        !          11274:                }
        !          11275:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
        !          11276:                dma[c].low_high = !dma[c].low_high;
        !          11277:                break;
        !          11278:        case 0x01: case 0x03: case 0x05: case 0x07:
        !          11279:                if(dma[c].low_high) {
        !          11280:                        dma[c].ch[ch].bcreg.b.h = data;
        !          11281:                } else {
        !          11282:                        dma[c].ch[ch].bcreg.b.l = data;
        !          11283:                }
        !          11284:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
        !          11285:                dma[c].low_high = !dma[c].low_high;
        !          11286:                break;
        !          11287:        case 0x08:
        !          11288:                // command register
        !          11289:                dma[c].cmd = data;
        !          11290:                break;
        !          11291:        case 0x09:
        !          11292:                // dma[c].request register
        !          11293:                if(data & 4) {
        !          11294:                        if(!(dma[c].req & bit)) {
        !          11295:                                dma[c].req |= bit;
        !          11296: //                             dma_run(c, ch);
        !          11297:                        }
        !          11298:                } else {
        !          11299:                        dma[c].req &= ~bit;
        !          11300:                }
        !          11301:                break;
        !          11302:        case 0x0a:
        !          11303:                // single mask register
        !          11304:                if(data & 4) {
        !          11305:                        dma[c].mask |= bit;
        !          11306:                } else {
        !          11307:                        dma[c].mask &= ~bit;
        !          11308:                }
        !          11309:                break;
        !          11310:        case 0x0b:
        !          11311:                // mode register
        !          11312:                dma[c].ch[data & 3].mode = data;
        !          11313:                break;
        !          11314:        case 0x0c:
        !          11315:                dma[c].low_high = false;
        !          11316:                break;
        !          11317:        case 0x0d:
        !          11318:                // clear master
        !          11319:                dma_reset(c);
        !          11320:                break;
        !          11321:        case 0x0e:
        !          11322:                // clear mask register
        !          11323:                dma[c].mask = 0;
        !          11324:                break;
        !          11325:        case 0x0f:
        !          11326:                // all mask register
        !          11327:                dma[c].mask = data & 0x0f;
        !          11328:                break;
        !          11329:        }
        !          11330: }
        !          11331: 
        !          11332: UINT8 dma_read(int c, UINT32 addr)
        !          11333: {
        !          11334:        int ch = (addr >> 1) & 3;
        !          11335:        UINT8 val = 0xff;
        !          11336:        
        !          11337:        switch(addr & 0x0f) {
        !          11338:        case 0x00: case 0x02: case 0x04: case 0x06:
        !          11339:                if(dma[c].low_high) {
        !          11340:                        val = dma[c].ch[ch].areg.b.h;
        !          11341:                } else {
        !          11342:                        val = dma[c].ch[ch].areg.b.l;
        !          11343:                }
        !          11344:                dma[c].low_high = !dma[c].low_high;
        !          11345:                return(val);
        !          11346:        case 0x01: case 0x03: case 0x05: case 0x07:
        !          11347:                if(dma[c].low_high) {
        !          11348:                        val = dma[c].ch[ch].creg.b.h;
        !          11349:                } else {
        !          11350:                        val = dma[c].ch[ch].creg.b.l;
        !          11351:                }
        !          11352:                dma[c].low_high = !dma[c].low_high;
        !          11353:                return(val);
        !          11354:        case 0x08:
        !          11355:                // status register
        !          11356:                val = (dma[c].req << 4) | dma[c].tc;
        !          11357:                dma[c].tc = 0;
        !          11358:                return(val);
        !          11359:        case 0x0d:
        !          11360:                // temporary register
        !          11361:                return(dma[c].tmp & 0xff);
        !          11362:        }
        !          11363:        return(0xff);
        !          11364: }
        !          11365: 
        !          11366: void dma_page_write(int c, int ch, UINT8 data)
        !          11367: {
        !          11368:        dma[c].ch[ch].pagereg = data;
        !          11369: }
        !          11370: 
        !          11371: UINT8 dma_page_read(int c, int ch)
        !          11372: {
        !          11373:        return(dma[c].ch[ch].pagereg);
        !          11374: }
        !          11375: 
        !          11376: void dma_run(int c, int ch)
        !          11377: {
        !          11378:        UINT8 bit = 1 << ch;
        !          11379:        
        !          11380:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
        !          11381:                // execute dma
        !          11382:                while(dma[c].req & bit) {
        !          11383:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
        !          11384:                                // memory -> memory
        !          11385:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
        !          11386:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
        !          11387:                                
        !          11388:                                if(c == 0) {
        !          11389:                                        dma[c].tmp = read_byte(saddr);
        !          11390:                                        write_byte(daddr, dma[c].tmp);
        !          11391:                                } else {
        !          11392:                                        dma[c].tmp = read_word(saddr << 1);
        !          11393:                                        write_word(daddr << 1, dma[c].tmp);
        !          11394:                                }
        !          11395:                                if(!(dma[c].cmd & 0x02)) {
        !          11396:                                        if(dma[c].ch[0].mode & 0x20) {
        !          11397:                                                dma[c].ch[0].areg.w--;
        !          11398:                                                if(dma[c].ch[0].areg.w == 0xffff) {
        !          11399:                                                        dma[c].ch[0].pagereg--;
        !          11400:                                                }
        !          11401:                                        } else {
        !          11402:                                                dma[c].ch[0].areg.w++;
        !          11403:                                                if(dma[c].ch[0].areg.w == 0) {
        !          11404:                                                        dma[c].ch[0].pagereg++;
        !          11405:                                                }
        !          11406:                                        }
        !          11407:                                }
        !          11408:                                if(dma[c].ch[1].mode & 0x20) {
        !          11409:                                        dma[c].ch[1].areg.w--;
        !          11410:                                        if(dma[c].ch[1].areg.w == 0xffff) {
        !          11411:                                                dma[c].ch[1].pagereg--;
        !          11412:                                        }
        !          11413:                                } else {
        !          11414:                                        dma[c].ch[1].areg.w++;
        !          11415:                                        if(dma[c].ch[1].areg.w == 0) {
        !          11416:                                                dma[c].ch[1].pagereg++;
        !          11417:                                        }
        !          11418:                                }
        !          11419:                                
        !          11420:                                // check dma condition
        !          11421:                                if(dma[c].ch[0].creg.w-- == 0) {
        !          11422:                                        if(dma[c].ch[0].mode & 0x10) {
        !          11423:                                                // self initialize
        !          11424:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
        !          11425:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
        !          11426:                                        } else {
        !          11427: //                                             dma[c].mask |= bit;
        !          11428:                                        }
        !          11429:                                }
        !          11430:                                if(dma[c].ch[1].creg.w-- == 0) {
        !          11431:                                        // terminal count
        !          11432:                                        if(dma[c].ch[1].mode & 0x10) {
        !          11433:                                                // self initialize
        !          11434:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
        !          11435:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
        !          11436:                                        } else {
        !          11437:                                                dma[c].mask |= bit;
        !          11438:                                        }
        !          11439:                                        dma[c].req &= ~bit;
        !          11440:                                        dma[c].tc |= bit;
        !          11441:                                }
        !          11442:                        } else {
        !          11443:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
        !          11444:                                
        !          11445:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
        !          11446:                                        // verify
        !          11447:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
        !          11448:                                        // io -> memory
        !          11449:                                        if(c == 0) {
        !          11450:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
        !          11451:                                                write_byte(addr, dma[c].tmp);
        !          11452:                                        } else {
        !          11453:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
        !          11454:                                                write_word(addr << 1, dma[c].tmp);
        !          11455:                                        }
        !          11456:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
        !          11457:                                        // memory -> io
        !          11458:                                        if(c == 0) {
        !          11459:                                                dma[c].tmp = read_byte(addr);
        !          11460:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
        !          11461:                                        } else {
        !          11462:                                                dma[c].tmp = read_word(addr << 1);
        !          11463:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
        !          11464:                                        }
        !          11465:                                }
        !          11466:                                if(dma[c].ch[ch].mode & 0x20) {
        !          11467:                                        dma[c].ch[ch].areg.w--;
        !          11468:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
        !          11469:                                                dma[c].ch[ch].pagereg--;
        !          11470:                                        }
        !          11471:                                } else {
        !          11472:                                        dma[c].ch[ch].areg.w++;
        !          11473:                                        if(dma[c].ch[ch].areg.w == 0) {
        !          11474:                                                dma[c].ch[ch].pagereg++;
        !          11475:                                        }
        !          11476:                                }
        !          11477:                                
        !          11478:                                // check dma condition
        !          11479:                                if(dma[c].ch[ch].creg.w-- == 0) {
        !          11480:                                        // terminal count
        !          11481:                                        if(dma[c].ch[ch].mode & 0x10) {
        !          11482:                                                // self initialize
        !          11483:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
        !          11484:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
        !          11485:                                        } else {
        !          11486:                                                dma[c].mask |= bit;
        !          11487:                                        }
        !          11488:                                        dma[c].req &= ~bit;
        !          11489:                                        dma[c].tc |= bit;
        !          11490:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
        !          11491:                                        // single mode
        !          11492:                                        break;
        !          11493:                                }
        !          11494:                        }
        !          11495:                }
        !          11496:        }
        !          11497: }
        !          11498: 
        !          11499: // pic
        !          11500: 
        !          11501: void pic_init()
        !          11502: {
        !          11503:        memset(pic, 0, sizeof(pic));
        !          11504:        pic[0].imr = pic[1].imr = 0xff;
        !          11505:        
        !          11506:        // from bochs bios
        !          11507:        pic_write(0, 0, 0x11);  // icw1 = 11h
        !          11508:        pic_write(0, 1, 0x08);  // icw2 = 08h
        !          11509:        pic_write(0, 1, 0x04);  // icw3 = 04h
        !          11510:        pic_write(0, 1, 0x01);  // icw4 = 01h
        !          11511:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
        !          11512:        pic_write(1, 0, 0x11);  // icw1 = 11h
        !          11513:        pic_write(1, 1, 0x70);  // icw2 = 70h
        !          11514:        pic_write(1, 1, 0x02);  // icw3 = 02h
        !          11515:        pic_write(1, 1, 0x01);  // icw4 = 01h
        !          11516: }
        !          11517: 
        !          11518: void pic_write(int c, UINT32 addr, UINT8 data)
        !          11519: {
        !          11520:        if(addr & 1) {
        !          11521:                if(pic[c].icw2_r) {
        !          11522:                        // icw2
        !          11523:                        pic[c].icw2 = data;
        !          11524:                        pic[c].icw2_r = 0;
        !          11525:                } else if(pic[c].icw3_r) {
        !          11526:                        // icw3
        !          11527:                        pic[c].icw3 = data;
        !          11528:                        pic[c].icw3_r = 0;
        !          11529:                } else if(pic[c].icw4_r) {
        !          11530:                        // icw4
        !          11531:                        pic[c].icw4 = data;
        !          11532:                        pic[c].icw4_r = 0;
        !          11533:                } else {
        !          11534:                        // ocw1
1.1       root     11535:                        pic[c].imr = data;
                   11536:                }
                   11537:        } else {
                   11538:                if(data & 0x10) {
                   11539:                        // icw1
                   11540:                        pic[c].icw1 = data;
                   11541:                        pic[c].icw2_r = 1;
                   11542:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   11543:                        pic[c].icw4_r = data & 1;
                   11544:                        pic[c].irr = 0;
                   11545:                        pic[c].isr = 0;
                   11546:                        pic[c].imr = 0;
                   11547:                        pic[c].prio = 0;
                   11548:                        if(!(pic[c].icw1 & 1)) {
                   11549:                                pic[c].icw4 = 0;
                   11550:                        }
                   11551:                        pic[c].ocw3 = 0;
                   11552:                } else if(data & 8) {
                   11553:                        // ocw3
                   11554:                        if(!(data & 2)) {
                   11555:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   11556:                        }
                   11557:                        if(!(data & 0x40)) {
                   11558:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   11559:                        }
                   11560:                        pic[c].ocw3 = data;
                   11561:                } else {
                   11562:                        // ocw2
                   11563:                        int level = 0;
                   11564:                        if(data & 0x40) {
                   11565:                                level = data & 7;
                   11566:                        } else {
                   11567:                                if(!pic[c].isr) {
                   11568:                                        return;
                   11569:                                }
                   11570:                                level = pic[c].prio;
                   11571:                                while(!(pic[c].isr & (1 << level))) {
                   11572:                                        level = (level + 1) & 7;
                   11573:                                }
                   11574:                        }
                   11575:                        if(data & 0x80) {
                   11576:                                pic[c].prio = (level + 1) & 7;
                   11577:                        }
                   11578:                        if(data & 0x20) {
                   11579:                                pic[c].isr &= ~(1 << level);
                   11580:                        }
                   11581:                }
                   11582:        }
                   11583:        pic_update();
                   11584: }
                   11585: 
                   11586: UINT8 pic_read(int c, UINT32 addr)
                   11587: {
                   11588:        if(addr & 1) {
                   11589:                return(pic[c].imr);
                   11590:        } else {
                   11591:                // polling mode is not supported...
                   11592:                //if(pic[c].ocw3 & 4) {
                   11593:                //      return ???;
                   11594:                //}
                   11595:                if(pic[c].ocw3 & 1) {
                   11596:                        return(pic[c].isr);
                   11597:                } else {
                   11598:                        return(pic[c].irr);
                   11599:                }
                   11600:        }
                   11601: }
                   11602: 
                   11603: void pic_req(int c, int level, int signal)
                   11604: {
                   11605:        if(signal) {
                   11606:                pic[c].irr |= (1 << level);
                   11607:        } else {
                   11608:                pic[c].irr &= ~(1 << level);
                   11609:        }
                   11610:        pic_update();
                   11611: }
                   11612: 
                   11613: int pic_ack()
                   11614: {
                   11615:        // ack (INTA=L)
                   11616:        pic[pic_req_chip].isr |= pic_req_bit;
                   11617:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   11618:        if(pic_req_chip > 0) {
                   11619:                // update isr and irr of master
                   11620:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   11621:                pic[pic_req_chip - 1].isr |= slave;
                   11622:                pic[pic_req_chip - 1].irr &= ~slave;
                   11623:        }
                   11624:        //if(pic[pic_req_chip].icw4 & 1) {
                   11625:                // 8086 mode
                   11626:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   11627:        //} else {
                   11628:        //      // 8080 mode
                   11629:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   11630:        //      if(pic[pic_req_chip].icw1 & 4) {
                   11631:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   11632:        //      } else {
                   11633:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   11634:        //      }
                   11635:        //      vector = 0xcd | (addr << 8);
                   11636:        //}
                   11637:        if(pic[pic_req_chip].icw4 & 2) {
                   11638:                // auto eoi
                   11639:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   11640:        }
                   11641:        return(vector);
                   11642: }
                   11643: 
                   11644: void pic_update()
                   11645: {
                   11646:        for(int c = 0; c < 2; c++) {
                   11647:                UINT8 irr = pic[c].irr;
                   11648:                if(c + 1 < 2) {
                   11649:                        // this is master
                   11650:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   11651:                                // request from slave
                   11652:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   11653:                        }
                   11654:                }
                   11655:                irr &= (~pic[c].imr);
                   11656:                if(!irr) {
                   11657:                        break;
                   11658:                }
                   11659:                if(!(pic[c].ocw3 & 0x20)) {
                   11660:                        irr |= pic[c].isr;
                   11661:                }
                   11662:                int level = pic[c].prio;
                   11663:                UINT8 bit = 1 << level;
                   11664:                while(!(irr & bit)) {
                   11665:                        level = (level + 1) & 7;
                   11666:                        bit = 1 << level;
                   11667:                }
                   11668:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   11669:                        // check slave
                   11670:                        continue;
                   11671:                }
                   11672:                if(pic[c].isr & bit) {
                   11673:                        break;
                   11674:                }
                   11675:                // interrupt request
                   11676:                pic_req_chip = c;
                   11677:                pic_req_level = level;
                   11678:                pic_req_bit = bit;
1.1.1.3   root     11679:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     11680:                return;
                   11681:        }
1.1.1.3   root     11682:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     11683: }
1.1       root     11684: 
1.1.1.25! root     11685: // pio
        !          11686: 
        !          11687: void pio_init()
        !          11688: {
        !          11689:        for(int c = 0; c < 2; c++) {
        !          11690:                pio[c].stat = 0xde;
        !          11691:                pio[c].ctrl = 0x0c;
        !          11692:        }
        !          11693: }
        !          11694: 
        !          11695: void pio_write(int c, UINT32 addr, UINT8 data)
        !          11696: {
        !          11697:        switch(addr & 3) {
        !          11698:        case 0:
        !          11699:                pio[c].data = data;
        !          11700:                break;
        !          11701:        case 2:
        !          11702:                pio[c].ctrl = data;
        !          11703:                break;
        !          11704:        }
        !          11705: }
        !          11706: 
        !          11707: UINT8 pio_read(int c, UINT32 addr)
        !          11708: {
        !          11709:        switch(addr & 3) {
        !          11710:        case 0:
        !          11711:                return(pio[c].data);
        !          11712:        case 1:
        !          11713:                return(pio[c].stat);
        !          11714:        case 2:
        !          11715:                return(pio[c].ctrl);
        !          11716:        }
        !          11717:        return(0xff);
        !          11718: }
        !          11719: 
1.1       root     11720: // pit
                   11721: 
1.1.1.22  root     11722: #define PIT_FREQ 1193182ULL
1.1       root     11723: #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)
                   11724: 
                   11725: void pit_init()
                   11726: {
1.1.1.8   root     11727:        memset(pit, 0, sizeof(pit));
1.1       root     11728:        for(int ch = 0; ch < 3; ch++) {
                   11729:                pit[ch].count = 0x10000;
                   11730:                pit[ch].ctrl_reg = 0x34;
                   11731:                pit[ch].mode = 3;
                   11732:        }
                   11733:        
                   11734:        // from bochs bios
                   11735:        pit_write(3, 0x34);
                   11736:        pit_write(0, 0x00);
                   11737:        pit_write(0, 0x00);
                   11738: }
                   11739: 
                   11740: void pit_write(int ch, UINT8 val)
                   11741: {
1.1.1.8   root     11742: #ifndef PIT_ALWAYS_RUNNING
1.1       root     11743:        if(!pit_active) {
                   11744:                pit_active = 1;
                   11745:                pit_init();
                   11746:        }
1.1.1.8   root     11747: #endif
1.1       root     11748:        switch(ch) {
                   11749:        case 0:
                   11750:        case 1:
                   11751:        case 2:
                   11752:                // write count register
                   11753:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   11754:                        if(pit[ch].ctrl_reg & 0x10) {
                   11755:                                pit[ch].low_write = 1;
                   11756:                        }
                   11757:                        if(pit[ch].ctrl_reg & 0x20) {
                   11758:                                pit[ch].high_write = 1;
                   11759:                        }
                   11760:                }
                   11761:                if(pit[ch].low_write) {
                   11762:                        pit[ch].count_reg = val;
                   11763:                        pit[ch].low_write = 0;
                   11764:                } else if(pit[ch].high_write) {
                   11765:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   11766:                                pit[ch].count_reg = val << 8;
                   11767:                        } else {
                   11768:                                pit[ch].count_reg |= val << 8;
                   11769:                        }
                   11770:                        pit[ch].high_write = 0;
                   11771:                }
                   11772:                // start count
1.1.1.8   root     11773:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   11774:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   11775:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   11776:                                pit[ch].prev_time = timeGetTime();
                   11777:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     11778:                        }
                   11779:                }
                   11780:                break;
                   11781:        case 3: // ctrl reg
                   11782:                if((val & 0xc0) == 0xc0) {
                   11783:                        // i8254 read-back command
                   11784:                        for(ch = 0; ch < 3; ch++) {
                   11785:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   11786:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   11787:                                        pit[ch].status_latched = 1;
                   11788:                                }
                   11789:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   11790:                                        pit_latch_count(ch);
                   11791:                                }
                   11792:                        }
                   11793:                        break;
                   11794:                }
                   11795:                ch = (val >> 6) & 3;
                   11796:                if(val & 0x30) {
                   11797:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   11798:                        pit[ch].mode = modes[(val >> 1) & 7];
                   11799:                        pit[ch].count_latched = 0;
                   11800:                        pit[ch].low_read = pit[ch].high_read = 0;
                   11801:                        pit[ch].low_write = pit[ch].high_write = 0;
                   11802:                        pit[ch].ctrl_reg = val;
                   11803:                        // stop count
1.1.1.8   root     11804:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     11805:                        pit[ch].count_reg = 0;
                   11806:                } else if(!pit[ch].count_latched) {
                   11807:                        pit_latch_count(ch);
                   11808:                }
                   11809:                break;
                   11810:        }
                   11811: }
                   11812: 
                   11813: UINT8 pit_read(int ch)
                   11814: {
1.1.1.8   root     11815: #ifndef PIT_ALWAYS_RUNNING
1.1       root     11816:        if(!pit_active) {
                   11817:                pit_active = 1;
                   11818:                pit_init();
                   11819:        }
1.1.1.8   root     11820: #endif
1.1       root     11821:        switch(ch) {
                   11822:        case 0:
                   11823:        case 1:
                   11824:        case 2:
                   11825:                if(pit[ch].status_latched) {
                   11826:                        pit[ch].status_latched = 0;
                   11827:                        return(pit[ch].status);
                   11828:                }
                   11829:                // if not latched, through current count
                   11830:                if(!pit[ch].count_latched) {
                   11831:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   11832:                                pit_latch_count(ch);
                   11833:                        }
                   11834:                }
                   11835:                // return latched count
                   11836:                if(pit[ch].low_read) {
                   11837:                        pit[ch].low_read = 0;
                   11838:                        if(!pit[ch].high_read) {
                   11839:                                pit[ch].count_latched = 0;
                   11840:                        }
                   11841:                        return(pit[ch].latch & 0xff);
                   11842:                } else if(pit[ch].high_read) {
                   11843:                        pit[ch].high_read = 0;
                   11844:                        pit[ch].count_latched = 0;
                   11845:                        return((pit[ch].latch >> 8) & 0xff);
                   11846:                }
                   11847:        }
                   11848:        return(0xff);
                   11849: }
                   11850: 
1.1.1.8   root     11851: int pit_run(int ch, UINT32 cur_time)
1.1       root     11852: {
1.1.1.8   root     11853:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     11854:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     11855:                pit[ch].prev_time = pit[ch].expired_time;
                   11856:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   11857:                if(cur_time >= pit[ch].expired_time) {
                   11858:                        pit[ch].prev_time = cur_time;
                   11859:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     11860:                }
1.1.1.8   root     11861:                return(1);
1.1       root     11862:        }
1.1.1.8   root     11863:        return(0);
1.1       root     11864: }
                   11865: 
                   11866: void pit_latch_count(int ch)
                   11867: {
1.1.1.8   root     11868:        UINT32 cur_time = timeGetTime();
                   11869:        
                   11870:        if(pit[ch].expired_time != 0) {
                   11871:                pit_run(ch, cur_time);
                   11872:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
                   11873:                pit[ch].latch = (tmp != 0) ? (UINT16)tmp : 1;
                   11874:        } else {
                   11875:                pit[ch].latch = (UINT16)pit[ch].count;
1.1       root     11876:        }
                   11877:        pit[ch].count_latched = 1;
                   11878:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   11879:                // lower byte
                   11880:                pit[ch].low_read = 1;
                   11881:                pit[ch].high_read = 0;
                   11882:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   11883:                // upper byte
                   11884:                pit[ch].low_read = 0;
                   11885:                pit[ch].high_read = 1;
                   11886:        } else {
                   11887:                // lower -> upper
1.1.1.14  root     11888:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     11889:        }
                   11890: }
                   11891: 
1.1.1.8   root     11892: int pit_get_expired_time(int ch)
1.1       root     11893: {
1.1.1.22  root     11894:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   11895:        UINT64 val = pit[ch].accum >> 10;
                   11896:        pit[ch].accum -= val << 10;
                   11897:        return((val != 0) ? val : 1);
1.1.1.8   root     11898: }
                   11899: 
1.1.1.25! root     11900: // sio
        !          11901: 
        !          11902: void sio_init()
        !          11903: {
        !          11904:        for(int c = 0; c < 2; c++) {
        !          11905:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
        !          11906:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
        !          11907:                
        !          11908:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
        !          11909:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
        !          11910:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
        !          11911:                sio[c].irq_identify = 0x01;     // no pending irq
        !          11912:                
        !          11913:                InitializeCriticalSection(&sio_mt[c].csSendData);
        !          11914:                InitializeCriticalSection(&sio_mt[c].csRecvData);
        !          11915:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
        !          11916:                InitializeCriticalSection(&sio_mt[c].csLineStat);
        !          11917:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
        !          11918:                InitializeCriticalSection(&sio_mt[c].csModemStat);
        !          11919:                
        !          11920:                if(sio[c].port_number != 0) {
        !          11921:                        sio[c].channel = c;
        !          11922:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
        !          11923:                }
        !          11924:        }
        !          11925: }
        !          11926: 
        !          11927: void sio_finish()
        !          11928: {
        !          11929:        for(int c = 0; c < 2; c++) {
        !          11930:                if(sio_mt[c].hThread != NULL) {
        !          11931:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
        !          11932:                        CloseHandle(sio_mt[c].hThread);
        !          11933:                }
        !          11934:                DeleteCriticalSection(&sio_mt[c].csSendData);
        !          11935:                DeleteCriticalSection(&sio_mt[c].csRecvData);
        !          11936:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
        !          11937:                DeleteCriticalSection(&sio_mt[c].csLineStat);
        !          11938:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
        !          11939:                DeleteCriticalSection(&sio_mt[c].csModemStat);
        !          11940:                
        !          11941:                sio[c].send_buffer->release();
        !          11942:                delete sio[c].send_buffer;
        !          11943:                sio[c].recv_buffer->release();
        !          11944:                delete sio[c].recv_buffer;
        !          11945:        }
        !          11946: }
        !          11947: 
        !          11948: void sio_write(int c, UINT32 addr, UINT8 data)
        !          11949: {
        !          11950:        switch(addr & 7) {
        !          11951:        case 0:
        !          11952:                if(sio[c].selector & 0x80) {
        !          11953:                        if(sio[c].divisor.b.l != data) {
        !          11954:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
        !          11955:                                sio[c].divisor.b.l = data;
        !          11956:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
        !          11957:                        }
        !          11958:                } else {
        !          11959:                        EnterCriticalSection(&sio_mt[c].csSendData);
        !          11960:                        sio[c].send_buffer->write(data);
        !          11961:                        // transmitter holding/shift registers are not empty
        !          11962:                        sio[c].line_stat_buf &= ~0x60;
        !          11963:                        LeaveCriticalSection(&sio_mt[c].csSendData);
        !          11964:                        
        !          11965:                        if(sio[c].irq_enable & 0x02) {
        !          11966:                                sio_update_irq(c);
        !          11967:                        }
        !          11968:                }
        !          11969:                break;
        !          11970:        case 1:
        !          11971:                if(sio[c].selector & 0x80) {
        !          11972:                        if(sio[c].divisor.b.h != data) {
        !          11973:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
        !          11974:                                sio[c].divisor.b.h = data;
        !          11975:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
        !          11976:                        }
        !          11977:                } else {
        !          11978:                        if(sio[c].irq_enable != data) {
        !          11979:                                sio[c].irq_enable = data;
        !          11980:                                sio_update_irq(c);
        !          11981:                        }
        !          11982:                }
        !          11983:                break;
        !          11984:        case 3:
        !          11985:                {
        !          11986:                        UINT8 line_ctrl = data & 0x3f;
        !          11987:                        bool set_brk = ((data & 0x40) != 0);
        !          11988:                        
        !          11989:                        if(sio[c].line_ctrl != line_ctrl) {
        !          11990:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
        !          11991:                                sio[c].line_ctrl = line_ctrl;
        !          11992:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
        !          11993:                        }
        !          11994:                        if(sio[c].set_brk != set_brk) {
        !          11995:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
        !          11996:                                sio[c].set_brk = set_brk;
        !          11997:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
        !          11998:                        }
        !          11999:                }
        !          12000:                sio[c].selector = data;
        !          12001:                break;
        !          12002:        case 4:
        !          12003:                {
        !          12004:                        bool set_dtr = ((data & 0x01) != 0);
        !          12005:                        bool set_rts = ((data & 0x02) != 0);
        !          12006:                        
        !          12007:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
        !          12008:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
        !          12009:                                sio[c].set_dtr = set_dtr;
        !          12010:                                sio[c].set_rts = set_rts;
        !          12011:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
        !          12012:                        }
        !          12013:                }
        !          12014:                sio[c].modem_ctrl = data;
        !          12015:                break;
        !          12016:        case 7:
        !          12017:                sio[c].scratch = data;
        !          12018:                break;
        !          12019:        }
        !          12020: }
        !          12021: 
        !          12022: UINT8 sio_read(int c, UINT32 addr)
        !          12023: {
        !          12024:        switch(addr & 7) {
        !          12025:        case 0:
        !          12026:                if(sio[c].selector & 0x80) {
        !          12027:                        return(sio[c].divisor.b.l);
        !          12028:                } else {
        !          12029:                        EnterCriticalSection(&sio_mt[c].csRecvData);
        !          12030:                        UINT8 data = sio[c].recv_buffer->read();
        !          12031:                        // data is not ready
        !          12032:                        sio[c].line_stat_buf &= ~0x01;
        !          12033:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
        !          12034:                        
        !          12035:                        if(sio[c].irq_enable & 0x01) {
        !          12036:                                sio_update_irq(c);
        !          12037:                        }
        !          12038:                        return(data);
        !          12039:                }
        !          12040:        case 1:
        !          12041:                if(sio[c].selector & 0x80) {
        !          12042:                        return(sio[c].divisor.b.h);
        !          12043:                } else {
        !          12044:                        return(sio[c].irq_enable);
        !          12045:                }
        !          12046:        case 2:
        !          12047:                return(sio[c].irq_identify);
        !          12048:        case 3:
        !          12049:                return(sio[c].selector);
        !          12050:        case 4:
        !          12051:                return(sio[c].modem_ctrl);
        !          12052:        case 5:
        !          12053:                {
        !          12054:                        EnterCriticalSection(&sio_mt[c].csLineStat);
        !          12055:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
        !          12056:                        sio[c].line_stat_err = 0x00;
        !          12057:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
        !          12058:                        
        !          12059:                        bool state_changed = false;
        !          12060:                        
        !          12061:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
        !          12062:                                EnterCriticalSection(&sio_mt[c].csSendData);
        !          12063:                                if(!sio[c].send_buffer->full()) {
        !          12064:                                        // transmitter holding register will be empty first
        !          12065:                                        if(sio[c].irq_enable & 0x02) {
        !          12066:                                                state_changed = true;
        !          12067:                                        }
        !          12068:                                        sio[c].line_stat_buf |= 0x20;
        !          12069:                                }
        !          12070:                                LeaveCriticalSection(&sio_mt[c].csSendData);
        !          12071:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
        !          12072:                                // transmitter shift register will be empty later
        !          12073:                                sio[c].line_stat_buf |= 0x40;
        !          12074:                        }
        !          12075:                        if(!(sio[c].line_stat_buf & 0x01)) {
        !          12076:                                EnterCriticalSection(&sio_mt[c].csRecvData);
        !          12077:                                if(!sio[c].recv_buffer->empty()) {
        !          12078:                                        // data is ready
        !          12079:                                        if(sio[c].irq_enable & 0x01) {
        !          12080:                                                state_changed = true;
        !          12081:                                        }
        !          12082:                                        sio[c].line_stat_buf |= 0x01;
        !          12083:                                }
        !          12084:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
        !          12085:                        }
        !          12086:                        if(state_changed) {
        !          12087:                                sio_update_irq(c);
        !          12088:                        }
        !          12089:                        return(val);
        !          12090:                }
        !          12091:        case 6:
        !          12092:                {
        !          12093:                        EnterCriticalSection(&sio_mt[c].csModemStat);
        !          12094:                        UINT8 val = sio[c].modem_stat;
        !          12095:                        sio[c].modem_stat &= 0xf0;
        !          12096:                        sio[c].prev_modem_stat = sio[c].modem_stat;
        !          12097:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
        !          12098:                        
        !          12099:                        if(sio[c].modem_ctrl & 0x10) {
        !          12100:                                // loop-back
        !          12101:                                val &= 0x0f;
        !          12102:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
        !          12103:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
        !          12104:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
        !          12105:                        }
        !          12106:                        return(val);
        !          12107:                }
        !          12108:        case 7:
        !          12109:                return(sio[c].scratch);
        !          12110:        }
        !          12111:        return(0xff);
        !          12112: }
        !          12113: 
        !          12114: void sio_update(int c)
        !          12115: {
        !          12116:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
        !          12117:                EnterCriticalSection(&sio_mt[c].csSendData);
        !          12118:                if(!sio[c].send_buffer->full()) {
        !          12119:                        // transmitter holding/shift registers will be empty
        !          12120:                        sio[c].line_stat_buf |= 0x60;
        !          12121:                }
        !          12122:                LeaveCriticalSection(&sio_mt[c].csSendData);
        !          12123:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
        !          12124:                // transmitter shift register will be empty
        !          12125:                sio[c].line_stat_buf |= 0x40;
        !          12126:        }
        !          12127:        if(!(sio[c].line_stat_buf & 0x01)) {
        !          12128:                EnterCriticalSection(&sio_mt[c].csRecvData);
        !          12129:                if(!sio[c].recv_buffer->empty()) {
        !          12130:                        // data is ready
        !          12131:                        sio[c].line_stat_buf |= 0x01;
        !          12132:                }
        !          12133:                LeaveCriticalSection(&sio_mt[c].csRecvData);
        !          12134:        }
        !          12135:        sio_update_irq(c);
        !          12136: }
        !          12137: 
        !          12138: void sio_update_irq(int c)
        !          12139: {
        !          12140:        int level = -1;
        !          12141:        
        !          12142:        if(sio[c].irq_enable & 0x08) {
        !          12143:                EnterCriticalSection(&sio_mt[c].csModemStat);
        !          12144:                if((sio[c].modem_stat & 0x0f) != 0) {
        !          12145:                        level = 0;
        !          12146:                }
        !          12147:                EnterCriticalSection(&sio_mt[c].csModemStat);
        !          12148:        }
        !          12149:        if(sio[c].irq_enable & 0x02) {
        !          12150:                if(sio[c].line_stat_buf & 0x20) {
        !          12151:                        level = 1;
        !          12152:                }
        !          12153:        }
        !          12154:        if(sio[c].irq_enable & 0x01) {
        !          12155:                if(sio[c].line_stat_buf & 0x01) {
        !          12156:                        level = 2;
        !          12157:                }
        !          12158:        }
        !          12159:        if(sio[c].irq_enable & 0x04) {
        !          12160:                EnterCriticalSection(&sio_mt[c].csLineStat);
        !          12161:                if(sio[c].line_stat_err != 0) {
        !          12162:                        level = 3;
        !          12163:                }
        !          12164:                LeaveCriticalSection(&sio_mt[c].csLineStat);
        !          12165:        }
        !          12166:        if(level != -1) {
        !          12167:                sio[c].irq_identify = level << 1;
        !          12168:                pic_req(0, (c == 0) ? 4 : 3, 1);
        !          12169:        } else {
        !          12170:                sio[c].irq_identify = 1;
        !          12171:                pic_req(0, (c == 0) ? 4 : 3, 0);
        !          12172:        }
        !          12173: }
        !          12174: 
        !          12175: DWORD WINAPI sio_thread(void *lpx)
        !          12176: {
        !          12177:        volatile sio_t *p = (sio_t *)lpx;
        !          12178:        sio_mt_t *q = &sio_mt[p->channel];
        !          12179:        
        !          12180:        char name[] = "COM1";
        !          12181:        name[3] = '0' + p->port_number;
        !          12182:        HANDLE hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
        !          12183:        
        !          12184:        if(hComm != INVALID_HANDLE_VALUE) {
        !          12185:                BYTE bytBuffer[SIO_BUFFER_SIZE];
        !          12186:                
        !          12187:                EscapeCommFunction(hComm, CLRBREAK);
        !          12188:                EscapeCommFunction(hComm, CLRDTR);
        !          12189:                EscapeCommFunction(hComm, CLRRTS);
        !          12190:                
        !          12191:                while(!m_halted) {
        !          12192:                        // setup comm port
        !          12193:                        bool comm_state_changed = false;
        !          12194:                        
        !          12195:                        EnterCriticalSection(&q->csLineCtrl);
        !          12196:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
        !          12197:                                p->prev_divisor = p->divisor.w;
        !          12198:                                p->prev_line_ctrl = p->line_ctrl;
        !          12199:                                comm_state_changed = true;
        !          12200:                        }
        !          12201:                        LeaveCriticalSection(&q->csLineCtrl);
        !          12202:                        
        !          12203:                        if(comm_state_changed) {
        !          12204:                                DCB dcb;
        !          12205:                                memset(&dcb, 0, sizeof(DCB));
        !          12206:                                dcb.DCBlength = sizeof(DCB);
        !          12207:                                dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
        !          12208:                                switch(p->prev_line_ctrl & 0x03) {
        !          12209:                                case 0x00: dcb.ByteSize = 5; break;
        !          12210:                                case 0x01: dcb.ByteSize = 6; break;
        !          12211:                                case 0x02: dcb.ByteSize = 7; break;
        !          12212:                                case 0x03: dcb.ByteSize = 8; break;
        !          12213:                                }
        !          12214:                                switch(p->prev_line_ctrl & 0x04) {
        !          12215:                                case 0x00: dcb.StopBits = ONESTOPBIT; break;
        !          12216:                                case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
        !          12217:                                }
        !          12218:                                switch(p->prev_line_ctrl & 0x38) {
        !          12219:                                case 0x08: dcb.Parity = ODDPARITY;   break;
        !          12220:                                case 0x18: dcb.Parity = EVENPARITY;  break;
        !          12221:                                case 0x28: dcb.Parity = MARKPARITY;  break;
        !          12222:                                case 0x38: dcb.Parity = SPACEPARITY; break;
        !          12223:                                default:   dcb.Parity = NOPARITY;    break;
        !          12224:                                }
        !          12225:                                SetCommState(hComm, &dcb);
        !          12226:                                
        !          12227:                                // check again to apply all comm state changes
        !          12228:                                Sleep(10);
        !          12229:                                continue;
        !          12230:                        }
        !          12231:                        
        !          12232:                        // set comm pins
        !          12233:                        bool change_brk = false;
        !          12234:                        bool change_rts = false;
        !          12235:                        bool change_dtr = false;
        !          12236:                        
        !          12237:                        EnterCriticalSection(&q->csModemCtrl);
        !          12238:                        if(p->prev_set_brk != p->set_brk) {
        !          12239:                                p->prev_set_brk = p->set_brk;
        !          12240:                                change_brk = true;
        !          12241:                        }
        !          12242:                        if(p->prev_set_rts != p->set_rts) {
        !          12243:                                p->prev_set_rts = p->set_rts;
        !          12244:                                change_rts = true;
        !          12245:                        }
        !          12246:                        if(p->prev_set_dtr != p->set_dtr) {
        !          12247:                                p->prev_set_dtr = p->set_dtr;
        !          12248:                                change_dtr = true;
        !          12249:                        }
        !          12250:                        LeaveCriticalSection(&q->csModemCtrl);
        !          12251:                        
        !          12252:                        if(change_brk) {
        !          12253:                                EscapeCommFunction(hComm, p->prev_set_brk ? SETBREAK : CLRBREAK);
        !          12254:                        }
        !          12255:                        if(change_rts) {
        !          12256:                                EscapeCommFunction(hComm, p->prev_set_rts ? SETRTS : SETRTS);
        !          12257:                        }
        !          12258:                        if(change_dtr) {
        !          12259:                                EscapeCommFunction(hComm, p->prev_set_dtr ? SETDTR : CLRDTR);
        !          12260:                        }
        !          12261:                        
        !          12262:                        // get comm pins
        !          12263:                        DWORD dwModemStat = 0;
        !          12264:                        
        !          12265:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
        !          12266:                                EnterCriticalSection(&q->csModemStat);
        !          12267:                                if(dwModemStat & MS_RLSD_ON) {
        !          12268:                                        p->modem_stat |= 0x80;
        !          12269:                                } else {
        !          12270:                                        p->modem_stat &= ~0x80;
        !          12271:                                }
        !          12272:                                if(dwModemStat & MS_RING_ON) {
        !          12273:                                        p->modem_stat |= 0x40;
        !          12274:                                } else {
        !          12275:                                        p->modem_stat &= ~0x40;
        !          12276:                                }
        !          12277:                                if(dwModemStat & MS_DSR_ON) {
        !          12278:                                        p->modem_stat |= 0x20;
        !          12279:                                } else {
        !          12280:                                        p->modem_stat &= ~0x20;
        !          12281:                                }
        !          12282:                                if(dwModemStat & MS_CTS_ON) {
        !          12283:                                        p->modem_stat |= 0x10;
        !          12284:                                } else {
        !          12285:                                        p->modem_stat &= ~0x10;
        !          12286:                                }
        !          12287:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
        !          12288:                                        p->modem_stat |= 0x08;
        !          12289:                                }
        !          12290:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
        !          12291:                                        p->modem_stat |= 0x04;
        !          12292:                                }
        !          12293:                                if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
        !          12294:                                        p->modem_stat |= 0x02;
        !          12295:                                }
        !          12296:                                if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
        !          12297:                                        p->modem_stat |= 0x01;
        !          12298:                                }
        !          12299:                                LeaveCriticalSection(&q->csModemStat);
        !          12300:                        }
        !          12301:                        
        !          12302:                        // send data
        !          12303:                        DWORD dwSend = 0;
        !          12304:                        
        !          12305:                        EnterCriticalSection(&q->csSendData);
        !          12306:                        while(!p->send_buffer->empty()) {
        !          12307:                                bytBuffer[dwSend++] = p->send_buffer->read();
        !          12308:                        }
        !          12309:                        LeaveCriticalSection(&q->csSendData);
        !          12310:                        
        !          12311:                        if(dwSend != 0) {
        !          12312:                                DWORD dwWritten = 0;
        !          12313:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
        !          12314:                        }
        !          12315:                        
        !          12316:                        // get line status and recv data
        !          12317:                        DWORD dwLineStat = 0;
        !          12318:                        COMSTAT comStat;
        !          12319:                        
        !          12320:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
        !          12321:                                EnterCriticalSection(&q->csLineStat);
        !          12322:                                if(dwLineStat & CE_BREAK) {
        !          12323:                                        p->line_stat_err |= 0x10;
        !          12324:                                }
        !          12325:                                if(dwLineStat & CE_FRAME) {
        !          12326:                                        p->line_stat_err |= 0x08;
        !          12327:                                }
        !          12328:                                if(dwLineStat & CE_RXPARITY) {
        !          12329:                                        p->line_stat_err |= 0x04;
        !          12330:                                }
        !          12331:                                if(dwLineStat & CE_OVERRUN) {
        !          12332:                                        p->line_stat_err |= 0x02;
        !          12333:                                }
        !          12334:                                LeaveCriticalSection(&q->csLineStat);
        !          12335:                                
        !          12336:                                if(comStat.cbInQue != 0) {
        !          12337:                                        EnterCriticalSection(&q->csRecvData);
        !          12338:                                        DWORD dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
        !          12339:                                        LeaveCriticalSection(&q->csRecvData);
        !          12340:                                        
        !          12341:                                        if(dwRecv != 0) {
        !          12342:                                                DWORD dwRead = 0;
        !          12343:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
        !          12344:                                                        EnterCriticalSection(&q->csRecvData);
        !          12345:                                                        for(int i = 0; i < dwRead; i++) {
        !          12346:                                                                p->recv_buffer->write(bytBuffer[i]);
        !          12347:                                                        }
        !          12348:                                                        LeaveCriticalSection(&q->csRecvData);
        !          12349:                                                }
        !          12350:                                        }
        !          12351:                                }
        !          12352:                        }
        !          12353:                        Sleep(10);
        !          12354:                }
        !          12355:                CloseHandle(hComm);
        !          12356:        }
        !          12357:        return 0;
        !          12358: }
        !          12359: 
1.1.1.8   root     12360: // cmos
                   12361: 
                   12362: void cmos_init()
                   12363: {
                   12364:        memset(cmos, 0, sizeof(cmos));
                   12365:        cmos_addr = 0;
1.1       root     12366:        
1.1.1.8   root     12367:        // from DOSBox
                   12368:        cmos_write(0x0a, 0x26);
                   12369:        cmos_write(0x0b, 0x02);
                   12370:        cmos_write(0x0d, 0x80);
1.1       root     12371: }
                   12372: 
1.1.1.8   root     12373: void cmos_write(int addr, UINT8 val)
1.1       root     12374: {
1.1.1.8   root     12375:        cmos[addr & 0x7f] = val;
                   12376: }
                   12377: 
                   12378: #define CMOS_GET_TIME() { \
                   12379:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   12380:        if(prev_sec != cur_sec) { \
                   12381:                GetLocalTime(&time); \
                   12382:                prev_sec = cur_sec; \
                   12383:        } \
1.1       root     12384: }
1.1.1.8   root     12385: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     12386: 
1.1.1.8   root     12387: UINT8 cmos_read(int addr)
1.1       root     12388: {
1.1.1.8   root     12389:        static SYSTEMTIME time;
                   12390:        static UINT32 prev_sec = 0;
1.1       root     12391:        
1.1.1.8   root     12392:        switch(addr & 0x7f) {
                   12393:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   12394:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   12395:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   12396:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   12397:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   12398:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   12399:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   12400: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   12401:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   12402:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   12403:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   12404:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   12405:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   12406:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   12407:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   12408:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     12409:        }
1.1.1.8   root     12410:        return(cmos[addr & 0x7f]);
1.1       root     12411: }
                   12412: 
1.1.1.7   root     12413: // kbd (a20)
                   12414: 
                   12415: void kbd_init()
                   12416: {
1.1.1.8   root     12417:        kbd_data = kbd_command = 0;
1.1.1.7   root     12418:        kbd_status = 0x18;
                   12419: }
                   12420: 
                   12421: UINT8 kbd_read_data()
                   12422: {
1.1.1.8   root     12423:        kbd_status &= ~1;
1.1.1.7   root     12424:        return(kbd_data);
                   12425: }
                   12426: 
                   12427: void kbd_write_data(UINT8 val)
                   12428: {
                   12429:        switch(kbd_command) {
                   12430:        case 0xd1:
                   12431:                i386_set_a20_line((val >> 1) & 1);
                   12432:                break;
                   12433:        }
                   12434:        kbd_command = 0;
1.1.1.8   root     12435:        kbd_status &= ~8;
1.1.1.7   root     12436: }
                   12437: 
                   12438: UINT8 kbd_read_status()
                   12439: {
                   12440:        return(kbd_status);
                   12441: }
                   12442: 
                   12443: void kbd_write_command(UINT8 val)
                   12444: {
                   12445:        switch(val) {
                   12446:        case 0xd0:
                   12447:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     12448:                kbd_status |= 1;
1.1.1.7   root     12449:                break;
                   12450:        case 0xdd:
                   12451:                i386_set_a20_line(0);
                   12452:                break;
                   12453:        case 0xdf:
                   12454:                i386_set_a20_line(1);
                   12455:                break;
                   12456:        case 0xf0:
                   12457:        case 0xf1:
                   12458:        case 0xf2:
                   12459:        case 0xf3:
                   12460:        case 0xf4:
                   12461:        case 0xf5:
                   12462:        case 0xf6:
                   12463:        case 0xf7:
                   12464:        case 0xf8:
                   12465:        case 0xf9:
                   12466:        case 0xfa:
                   12467:        case 0xfb:
                   12468:        case 0xfc:
                   12469:        case 0xfd:
                   12470:        case 0xfe:
                   12471:        case 0xff:
                   12472:                if(!(val & 1)) {
1.1.1.8   root     12473:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     12474:                                // reset pic
                   12475:                                pic_init();
                   12476:                                pic[0].irr = pic[1].irr = 0x00;
                   12477:                                pic[0].imr = pic[1].imr = 0xff;
                   12478:                        }
                   12479:                        CPU_RESET_CALL(CPU_MODEL);
                   12480:                        i386_jmp_far(0x40, 0x67);
                   12481:                }
                   12482:                i386_set_a20_line((val >> 1) & 1);
                   12483:                break;
                   12484:        }
                   12485:        kbd_command = val;
1.1.1.8   root     12486:        kbd_status |= 8;
1.1.1.7   root     12487: }
                   12488: 
1.1.1.9   root     12489: // vga
                   12490: 
                   12491: UINT8 vga_read_status()
                   12492: {
                   12493:        // 60hz
                   12494:        static const int period[3] = {16, 17, 17};
                   12495:        static int index = 0;
                   12496:        UINT32 time = timeGetTime() % period[index];
                   12497:        
                   12498:        index = (index + 1) % 3;
1.1.1.14  root     12499:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     12500: }
                   12501: 
1.1       root     12502: // i/o bus
                   12503: 
1.1.1.25! root     12504: #ifdef ENABLE_DEBUG_IOPORT
        !          12505: UINT8 read_io_byte_debug(offs_t addr);
        !          12506: 
        !          12507: UINT8 read_io_byte(offs_t addr)
        !          12508: {
        !          12509:        UINT8 val = read_io_byte_debug(addr);
        !          12510:        if(fdebug != NULL) {
        !          12511:                fprintf(fdebug, "inb %04X, %02X\n", addr, val);
        !          12512:        }
        !          12513:        return(val);
        !          12514: }
        !          12515: 
        !          12516: UINT8 read_io_byte_debug(offs_t addr)
        !          12517: #else
1.1       root     12518: UINT8 read_io_byte(offs_t addr)
1.1.1.25! root     12519: #endif
1.1       root     12520: {
                   12521:        switch(addr) {
1.1.1.25! root     12522:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
        !          12523:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
        !          12524:                return(dma_read(0, addr));
        !          12525:        case 0x20: case 0x21:
1.1       root     12526:                return(pic_read(0, addr));
1.1.1.25! root     12527:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     12528:                return(pit_read(addr & 0x03));
1.1.1.7   root     12529:        case 0x60:
                   12530:                return(kbd_read_data());
1.1.1.9   root     12531:        case 0x61:
                   12532:                return(system_port);
1.1.1.7   root     12533:        case 0x64:
                   12534:                return(kbd_read_status());
1.1       root     12535:        case 0x71:
1.1.1.8   root     12536:                return(cmos_read(cmos_addr));
1.1.1.25! root     12537:        case 0x81:
        !          12538:                return(dma_page_read(0, 2));
        !          12539:        case 0x82:
        !          12540:                return(dma_page_read(0, 3));
        !          12541:        case 0x83:
        !          12542:                return(dma_page_read(0, 1));
        !          12543:        case 0x87:
        !          12544:                return(dma_page_read(0, 0));
        !          12545:        case 0x89:
        !          12546:                return(dma_page_read(1, 2));
        !          12547:        case 0x8a:
        !          12548:                return(dma_page_read(1, 3));
        !          12549:        case 0x8b:
        !          12550:                return(dma_page_read(1, 1));
        !          12551:        case 0x8f:
        !          12552:                return(dma_page_read(1, 0));
1.1       root     12553:        case 0x92:
1.1.1.3   root     12554:                return((m_a20_mask >> 19) & 2);
1.1.1.25! root     12555:        case 0xa0: case 0xa1:
1.1       root     12556:                return(pic_read(1, addr));
1.1.1.25! root     12557:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
        !          12558:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
        !          12559:                return(dma_read(1, addr >> 1));
        !          12560:        case 0x278: case 0x279: case 0x27a:
        !          12561:                return(pio_read(1, addr));
        !          12562:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
        !          12563:                return(sio_read(1, addr));
        !          12564:        case 0x378: case 0x379: case 0x37a:
        !          12565:                return(pio_read(0, addr));
        !          12566:        case 0x3ba: case 0x3da:
1.1.1.9   root     12567:                return(vga_read_status());
1.1.1.25! root     12568:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
        !          12569:                return(sio_read(0, addr));
1.1       root     12570:        default:
                   12571: //             error("inb %4x\n", addr);
                   12572:                break;
                   12573:        }
                   12574:        return(0xff);
                   12575: }
                   12576: 
                   12577: UINT16 read_io_word(offs_t addr)
                   12578: {
                   12579:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   12580: }
                   12581: 
                   12582: UINT32 read_io_dword(offs_t addr)
                   12583: {
                   12584:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   12585: }
                   12586: 
                   12587: void write_io_byte(offs_t addr, UINT8 val)
                   12588: {
1.1.1.25! root     12589: #ifdef ENABLE_DEBUG_IOPORT
        !          12590:        if(fdebug != NULL) {
        !          12591:                fprintf(fdebug, "outb %04X, %02X\n", addr, val);
        !          12592:        }
        !          12593: #endif
1.1       root     12594:        switch(addr) {
1.1.1.25! root     12595:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
        !          12596:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
        !          12597:                dma_write(0, addr, val);
        !          12598:                break;
        !          12599:        case 0x20: case 0x21:
1.1       root     12600:                pic_write(0, addr, val);
                   12601:                break;
1.1.1.25! root     12602:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     12603:                pit_write(addr & 0x03, val);
                   12604:                break;
1.1.1.7   root     12605:        case 0x60:
                   12606:                kbd_write_data(val);
                   12607:                break;
1.1.1.9   root     12608:        case 0x61:
                   12609:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   12610:                        // beep on
                   12611: //                     MessageBeep(-1);
                   12612:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   12613:                        // beep off
                   12614:                }
                   12615:                system_port = val;
                   12616:                break;
1.1       root     12617:        case 0x64:
1.1.1.7   root     12618:                kbd_write_command(val);
1.1       root     12619:                break;
                   12620:        case 0x70:
                   12621:                cmos_addr = val;
                   12622:                break;
                   12623:        case 0x71:
1.1.1.8   root     12624:                cmos_write(cmos_addr, val);
1.1       root     12625:                break;
1.1.1.25! root     12626:        case 0x81:
        !          12627:                dma_page_write(0, 2, val);
        !          12628:        case 0x82:
        !          12629:                dma_page_write(0, 3, val);
        !          12630:        case 0x83:
        !          12631:                dma_page_write(0, 1, val);
        !          12632:        case 0x87:
        !          12633:                dma_page_write(0, 0, val);
        !          12634:        case 0x89:
        !          12635:                dma_page_write(1, 2, val);
        !          12636:        case 0x8a:
        !          12637:                dma_page_write(1, 3, val);
        !          12638:        case 0x8b:
        !          12639:                dma_page_write(1, 1, val);
        !          12640:        case 0x8f:
        !          12641:                dma_page_write(1, 0, val);
1.1       root     12642:        case 0x92:
1.1.1.7   root     12643:                i386_set_a20_line((val >> 1) & 1);
1.1       root     12644:                break;
1.1.1.25! root     12645:        case 0xa0: case 0xa1:
1.1       root     12646:                pic_write(1, addr, val);
                   12647:                break;
1.1.1.25! root     12648:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
        !          12649:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
        !          12650:                dma_write(1, addr >> 1, val);
        !          12651:                break;
        !          12652:        case 0x278: case 0x279: case 0x27a:
        !          12653:                pio_write(1, addr, val);
        !          12654:                break;
        !          12655:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
        !          12656:                sio_write(1, addr, val);
        !          12657:                break;
        !          12658:        case 0x378: case 0x379: case 0x37a:
        !          12659:                pio_write(0, addr, val);
        !          12660:                break;
        !          12661:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
        !          12662:                sio_write(0, addr, val);
        !          12663:                break;
1.1       root     12664:        default:
                   12665: //             error("outb %4x,%2x\n", addr, val);
                   12666:                break;
                   12667:        }
                   12668: }
                   12669: 
                   12670: void write_io_word(offs_t addr, UINT16 val)
                   12671: {
                   12672:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   12673:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   12674: }
                   12675: 
                   12676: void write_io_dword(offs_t addr, UINT32 val)
                   12677: {
                   12678:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   12679:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   12680:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   12681:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   12682: }

unix.superglobalmegacorp.com

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