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

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 my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     11: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     12: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
                     13: 
                     14: #define fatalerror(...) { \
                     15:        fprintf(stderr, __VA_ARGS__); \
                     16:        exit(1); \
                     17: }
                     18: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
                     19: 
                     20: /* ----------------------------------------------------------------------------
1.1.1.3   root       21:        MAME i86/i386
1.1       root       22: ---------------------------------------------------------------------------- */
                     23: 
                     24: //#define SUPPORT_DISASSEMBLER
                     25: 
1.1.1.7   root       26: #if defined(HAS_I86)
                     27:        #define CPU_MODEL i8086
1.1.1.9   root       28: #elif defined(HAS_I186)
                     29:        #define CPU_MODEL i80186
1.1.1.7   root       30: #elif defined(HAS_I286)
                     31:        #define CPU_MODEL i80286
                     32: #elif defined(HAS_I386)
1.1.1.3   root       33:        #define CPU_MODEL i386
1.1.1.9   root       34: #else
                     35:        #if defined(HAS_I386SX)
                     36:                #define CPU_MODEL i386SX
                     37:        #else
1.1.1.11! root       38:                #if defined(HAS_I486)
        !            39:                        #define CPU_MODEL i486
        !            40:                #else
        !            41:                        #if defined(HAS_PENTIUM)
        !            42:                                #define CPU_MODEL pentium
        !            43:                        #elif defined(HAS_MEDIAGX)
        !            44:                                #define CPU_MODEL mediagx
        !            45:                        #elif defined(HAS_PENTIUM_PRO)
        !            46:                                #define CPU_MODEL pentium_pro
        !            47:                        #elif defined(HAS_PENTIUM_MMX)
        !            48:                                #define CPU_MODEL pentium_mmx
        !            49:                        #elif defined(HAS_PENTIUM2)
        !            50:                                #define CPU_MODEL pentium2
        !            51:                        #elif defined(HAS_PENTIUM3)
        !            52:                                #define CPU_MODEL pentium3
        !            53:                        #elif defined(HAS_PENTIUM4)
        !            54:                                #define CPU_MODEL pentium4
        !            55:                        #endif
        !            56:                        #define SUPPORT_RDTSC
1.1.1.9   root       57:                #endif
1.1.1.11! root       58:                #define SUPPORT_FPU
1.1.1.9   root       59:        #endif
1.1.1.7   root       60:        #define HAS_I386
1.1.1.3   root       61: #endif
1.1       root       62: 
1.1.1.10  root       63: #ifndef __BIG_ENDIAN__
1.1       root       64: #define LSB_FIRST
1.1.1.10  root       65: #endif
1.1       root       66: 
                     67: #ifndef INLINE
                     68: #define INLINE inline
                     69: #endif
                     70: #define U64(v) UINT64(v)
                     71: 
                     72: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                     73: #define logerror(...)
                     74: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                     75: #define popmessage(...)
                     76: 
                     77: /*****************************************************************************/
1.1.1.10  root       78: /* src/emu/devcpu.h */
                     79: 
                     80: // CPU interface functions
                     81: #define CPU_INIT_NAME(name)                    cpu_init_##name
                     82: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                     83: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                     84: 
                     85: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                     86: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                     87: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                     88: 
                     89: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                     90: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                     91: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                     92: 
                     93: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                     94: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                     95: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                     96: 
                     97: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                     98: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                     99: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    100: 
                    101: /*****************************************************************************/
                    102: /* src/emu/didisasm.h */
                    103: 
                    104: // Disassembler constants
                    105: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    106: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    107: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    108: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    109: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    110: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    111: 
                    112: /*****************************************************************************/
1.1       root      113: /* src/emu/diexec.h */
                    114: 
                    115: // I/O line states
                    116: enum line_state
                    117: {
                    118:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    119:        ASSERT_LINE,                            // assert an interrupt immediately
                    120:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    121:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    122: };
                    123: 
                    124: // I/O line definitions
                    125: enum
                    126: {
                    127:        INPUT_LINE_IRQ = 0,
                    128:        INPUT_LINE_NMI
                    129: };
                    130: 
                    131: /*****************************************************************************/
1.1.1.10  root      132: /* src/emu/dimemory.h */
1.1       root      133: 
1.1.1.10  root      134: // Translation intentions
                    135: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    136: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    137: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    138: 
                    139: const int TRANSLATE_READ            = 0;        // translate for read
                    140: const int TRANSLATE_WRITE           = 1;        // translate for write
                    141: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    142: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    143: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    144: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    145: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    146: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    147: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      148: 
1.1.1.10  root      149: /*****************************************************************************/
                    150: /* src/emu/emucore.h */
1.1       root      151: 
1.1.1.10  root      152: // constants for expression endianness
                    153: enum endianness_t
                    154: {
                    155:        ENDIANNESS_LITTLE,
                    156:        ENDIANNESS_BIG
                    157: };
1.1       root      158: 
1.1.1.10  root      159: // declare native endianness to be one or the other
                    160: #ifdef LSB_FIRST
                    161: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    162: #else
                    163: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    164: #endif
                    165: 
                    166: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    167: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    168: 
                    169: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    170: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    171: 
                    172: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    173: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      174: 
                    175: /*****************************************************************************/
                    176: /* src/emu/memory.h */
                    177: 
1.1.1.10  root      178: // address spaces
                    179: enum address_spacenum
                    180: {
                    181:        AS_0,                           // first address space
                    182:        AS_1,                           // second address space
                    183:        AS_2,                           // third address space
                    184:        AS_3,                           // fourth address space
                    185:        ADDRESS_SPACES,                 // maximum number of address spaces
                    186: 
                    187:        // alternate address space names for common use
                    188:        AS_PROGRAM = AS_0,              // program address space
                    189:        AS_DATA = AS_1,                 // data address space
                    190:        AS_IO = AS_2                    // I/O address space
                    191: };
                    192: 
1.1       root      193: // offsets and addresses are 32-bit (for now...)
                    194: typedef UINT32 offs_t;
                    195: 
                    196: // read accessors
                    197: UINT8 read_byte(offs_t byteaddress)
                    198: {
1.1.1.4   root      199: #if defined(HAS_I386)
1.1       root      200:        if(byteaddress < MAX_MEM) {
                    201:                return mem[byteaddress];
1.1.1.3   root      202: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    203: //             return read_byte(byteaddress & 0xfffff);
1.1       root      204:        }
                    205:        return 0;
1.1.1.4   root      206: #else
                    207:        return mem[byteaddress];
                    208: #endif
1.1       root      209: }
                    210: 
                    211: UINT16 read_word(offs_t byteaddress)
                    212: {
1.1.1.4   root      213: #if defined(HAS_I386)
1.1       root      214:        if(byteaddress < MAX_MEM - 1) {
                    215:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      216: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    217: //             return read_word(byteaddress & 0xfffff);
1.1       root      218:        }
                    219:        return 0;
1.1.1.4   root      220: #else
                    221:        return *(UINT16 *)(mem + byteaddress);
                    222: #endif
1.1       root      223: }
                    224: 
                    225: UINT32 read_dword(offs_t byteaddress)
                    226: {
1.1.1.4   root      227: #if defined(HAS_I386)
1.1       root      228:        if(byteaddress < MAX_MEM - 3) {
                    229:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      230: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    231: //             return read_dword(byteaddress & 0xfffff);
1.1       root      232:        }
                    233:        return 0;
1.1.1.4   root      234: #else
                    235:        return *(UINT32 *)(mem + byteaddress);
                    236: #endif
1.1       root      237: }
                    238: 
                    239: // write accessors
1.1.1.8   root      240: void write_text_vram_byte(offs_t offset, UINT8 data)
                    241: {
                    242:        // XXX: we need to consider a multi-byte character
                    243:        COORD co;
                    244:        DWORD num;
                    245:        
                    246:        co.X = (offset >> 1) % 80;
                    247:        co.Y = (offset >> 1) / 80;
                    248:        
                    249:        if(offset & 1) {
                    250:                scr_attr[0] = data;
                    251:                WriteConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                    252:        } else {
                    253:                scr_char[0] = data;
                    254:                WriteConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                    255:        }
                    256: }
                    257: 
                    258: void write_text_vram_word(offs_t offset, UINT16 data)
                    259: {
                    260:        // XXX: we need to consider a multi-byte character
                    261:        if(offset & 1) {
                    262:                // Attr, Char
                    263:                write_text_vram_byte(offset    , (data     ) & 0xff);
                    264:                write_text_vram_byte(offset + 1, (data >> 8) & 0xff);
                    265:        } else {
                    266:                // Char, Attr
                    267:                COORD co;
                    268:                DWORD num;
                    269:                
                    270:                co.X = (offset >> 1) % 80;
                    271:                co.Y = (offset >> 1) / 80;
                    272:                
                    273:                scr_char[0] = (data     ) & 0xff;
                    274:                scr_attr[0] = (data >> 8) & 0xff;
                    275:                
                    276:                WriteConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                    277:                WriteConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                    278:        }
                    279: }
                    280: 
                    281: void write_text_vram_dword(offs_t offset, UINT32 data)
                    282: {
                    283:        // XXX: we need to consider a multi-byte character
                    284:        if(offset & 1) {
                    285:                // Attr, Char, Attr, Char
                    286:                write_text_vram_byte(offset    , (data      ) & 0x00ff);
                    287:                write_text_vram_word(offset + 1, (data >>  8) & 0xffff);
                    288:                write_text_vram_byte(offset + 3, (data >> 24) & 0x00ff);
                    289:        } else {
                    290:                // Char, Attr, Char, Attr
                    291:                COORD co;
                    292:                DWORD num;
                    293:                
                    294:                co.X = (offset >> 1) % 80;
                    295:                co.Y = (offset >> 1) / 80;
                    296:                
                    297:                scr_char[0] = (data      ) & 0xff;
                    298:                scr_attr[0] = (data >>  8) & 0xff;
                    299:                scr_char[1] = (data >> 16) & 0xff;
                    300:                scr_attr[1] = (data >> 24) & 0xff;
                    301:                
                    302:                WriteConsoleOutputCharacter(hStdout, scr_char, 2, co, &num);
                    303:                WriteConsoleOutputAttribute(hStdout, scr_attr, 2, co, &num);
                    304:        }
                    305: }
                    306: 
1.1       root      307: void write_byte(offs_t byteaddress, UINT8 data)
                    308: {
1.1.1.8   root      309:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      310:                mem[byteaddress] = data;
1.1.1.8   root      311:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
                    312:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    313:                mem[byteaddress] = data;
                    314:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    315:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    316:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      317:                }
                    318:                mem[byteaddress] = data;
1.1.1.4   root      319: #if defined(HAS_I386)
1.1.1.3   root      320:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      321: #else
                    322:        } else {
                    323: #endif
1.1.1.3   root      324:                mem[byteaddress] = data;
1.1       root      325:        }
                    326: }
                    327: 
                    328: void write_word(offs_t byteaddress, UINT16 data)
                    329: {
1.1.1.8   root      330:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      331:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      332:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
                    333:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    334:                *(UINT16 *)(mem + byteaddress) = data;
                    335:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    336:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    337:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      338:                }
                    339:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      340: #if defined(HAS_I386)
1.1.1.3   root      341:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      342: #else
                    343:        } else {
                    344: #endif
1.1.1.3   root      345:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      346:        }
                    347: }
                    348: 
                    349: void write_dword(offs_t byteaddress, UINT32 data)
                    350: {
1.1.1.8   root      351:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      352:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      353:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
                    354:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    355:                *(UINT32 *)(mem + byteaddress) = data;
                    356:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    357:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    358:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      359:                }
                    360:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      361: #if defined(HAS_I386)
1.1.1.3   root      362:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      363: #else
                    364:        } else {
                    365: #endif
1.1.1.3   root      366:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      367:        }
                    368: }
                    369: 
                    370: #define read_decrypted_byte read_byte
                    371: #define read_decrypted_word read_word
                    372: #define read_decrypted_dword read_dword
                    373: 
1.1.1.3   root      374: #define read_raw_byte read_byte
                    375: #define write_raw_byte write_byte
                    376: 
                    377: #define read_word_unaligned read_word
                    378: #define write_word_unaligned write_word
                    379: 
                    380: #define read_io_word_unaligned read_io_word
                    381: #define write_io_word_unaligned write_io_word
                    382: 
1.1       root      383: UINT8 read_io_byte(offs_t byteaddress);
                    384: UINT16 read_io_word(offs_t byteaddress);
                    385: UINT32 read_io_dword(offs_t byteaddress);
                    386: 
                    387: void write_io_byte(offs_t byteaddress, UINT8 data);
                    388: void write_io_word(offs_t byteaddress, UINT16 data);
                    389: void write_io_dword(offs_t byteaddress, UINT32 data);
                    390: 
                    391: /*****************************************************************************/
                    392: /* src/osd/osdcomm.h */
                    393: 
                    394: /* Highly useful macro for compile-time knowledge of an array size */
                    395: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    396: 
1.1.1.3   root      397: #if defined(HAS_I386)
1.1.1.10  root      398:        static CPU_TRANSLATE(i386);
                    399:        #include "mame/lib/softfloat/softfloat.c"
                    400:        #include "mame/emu/cpu/vtlb.c"
                    401:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.3   root      402: #elif defined(HAS_I286)
1.1.1.10  root      403:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      404: #else
1.1.1.10  root      405:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      406: #endif
1.1       root      407: #ifdef SUPPORT_DISASSEMBLER
1.1.1.10  root      408:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1.1.3   root      409:        bool dasm = false;
1.1       root      410: #endif
                    411: 
1.1.1.3   root      412: #if defined(HAS_I386)
                    413:        #define SREG(x)                         m_sreg[x].selector
                    414:        #define SREG_BASE(x)                    m_sreg[x].base
                    415: 
                    416:        int cpu_type, cpu_step;
                    417: #else
                    418:        #define REG8(x)                         m_regs.b[x]
                    419:        #define REG16(x)                        m_regs.w[x]
                    420:        #define SREG(x)                         m_sregs[x]
                    421:        #define SREG_BASE(x)                    m_base[x]
                    422:        #define m_CF                            m_CarryVal
                    423:        #define m_a20_mask                      AMASK
                    424:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    425:        #if defined(HAS_I286)
                    426:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    427:        #else
                    428:                #define i386_set_a20_line(x)
                    429:        #endif
                    430:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    431: #endif
1.1       root      432: 
                    433: void i386_jmp_far(UINT16 selector, UINT32 address)
                    434: {
1.1.1.3   root      435: #if defined(HAS_I386)
1.1       root      436:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      437:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      438:        } else {
1.1.1.3   root      439:                SREG(CS) = selector;
                    440:                m_performed_intersegment_jump = 1;
                    441:                i386_load_segment_descriptor(CS);
                    442:                m_eip = address;
                    443:                CHANGE_PC(m_eip);
1.1       root      444:        }
1.1.1.3   root      445: #elif defined(HAS_I286)
                    446:        i80286_code_descriptor(selector, address, 1);
                    447: #else
                    448:        SREG(CS) = selector;
                    449:        i386_load_segment_descriptor(CS);
                    450:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    451: #endif
1.1       root      452: }
                    453: 
                    454: /* ----------------------------------------------------------------------------
                    455:        main
                    456: ---------------------------------------------------------------------------- */
                    457: 
1.1.1.10  root      458: bool is_started_from_command_prompt()
                    459: {
                    460:        bool ret = false;
                    461:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                    462:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                    463:                DWORD dwParentProcessID = 0;
                    464:                PROCESSENTRY32 pe32;
                    465:                pe32.dwSize = sizeof(PROCESSENTRY32);
                    466:                if(Process32First(hSnapshot, &pe32)) {
                    467:                        do {
                    468:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                    469:                                        dwParentProcessID = pe32.th32ParentProcessID;
                    470:                                        break;
                    471:                                }
                    472:                        } while(Process32Next(hSnapshot, &pe32));
                    473:                }
                    474:                CloseHandle(hSnapshot);
                    475:                if(dwParentProcessID != 0) {
                    476:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                    477:                        if(hProcess != NULL) {
                    478:                                HMODULE hMod;
                    479:                                DWORD cbNeeded;
                    480:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                    481:                                        char module_name[MAX_PATH];
                    482:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                    483:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                    484:                                        }
                    485:                                }
                    486:                                CloseHandle(hProcess);
                    487:                        }
                    488:                }
                    489:        }
                    490:        return(ret);
                    491: }
                    492: 
1.1.1.9   root      493: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                    494: 
1.1       root      495: int main(int argc, char *argv[], char *envp[])
                    496: {
1.1.1.9   root      497:        int arg_offset = 0;
                    498:        int standard_env = 0;
1.1       root      499:        
1.1.1.9   root      500:        for(int i = 1; i < argc; i++) {
                    501:                if(_strnicmp(argv[i], "-e", 2) == 0) {
                    502:                        standard_env = 1;
                    503:                        arg_offset++;
                    504:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
                    505:                        if(strlen(argv[i]) >= 6 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && IS_NUMERIC(argv[i][5])) {
                    506:                                major_version = argv[i][2] - '0';
                    507:                                minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] - '0');
                    508:                        }
                    509:                        arg_offset++;
                    510:                } else {
                    511:                        break;
                    512:                }
                    513:        }
                    514:        
                    515:        if(argc < 2 + arg_offset) {
1.1       root      516: #ifdef _WIN64
                    517:                fprintf(stderr, "MS-DOS Player for Win32-x64 console\n\n");
                    518: #else
                    519:                fprintf(stderr, "MS-DOS Player for Win32 console\n\n");
                    520: #endif
1.1.1.9   root      521:                fprintf(stderr, "Usage: MSDOS [-e] [-vX.XX] (command file) [opions]\n");
1.1.1.10  root      522:                
                    523:                if(!is_started_from_command_prompt()) {
                    524:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                    525:                        while(!_kbhit()) {
                    526:                                Sleep(10);
                    527:                        }
                    528:                }
1.1       root      529:                return(EXIT_FAILURE);
                    530:        }
                    531:        
                    532:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                    533:        hStdin = GetStdHandle(STD_INPUT_HANDLE);
                    534:        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                    535:        GetConsoleScreenBufferInfo(hStdout, &csbi);
                    536:        
                    537:        for(int y = 0; y < SCR_BUF_SIZE; y++) {
                    538:                for(int x = 0; x < 80; x++) {
                    539:                        scr_buf[y][x].Char.AsciiChar = ' ';
                    540:                        scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                    541:                }
                    542:        }
                    543:        scr_buf_size.X = 80;
                    544:        scr_buf_size.Y = SCR_BUF_SIZE;
                    545:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                    546:        scr_width = csbi.dwSize.X;
                    547:        scr_height = csbi.dwSize.Y;
                    548:        cursor_moved = false;
                    549:        
                    550:        key_buf_char = new FIFO();
                    551:        key_buf_scan = new FIFO();
                    552:        
                    553:        hardware_init();
                    554:        
1.1.1.9   root      555:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root      556:                retval = EXIT_FAILURE;
                    557:        } else {
1.1.1.8   root      558:                TIMECAPS caps;
                    559:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                    560:                timeBeginPeriod(caps.wPeriodMin);
1.1       root      561:                hardware_run();
                    562:                msdos_finish();
1.1.1.8   root      563:                timeEndPeriod(caps.wPeriodMin);
1.1       root      564:        }
                    565:        
1.1.1.10  root      566:        hardware_finish();
                    567:        
1.1       root      568:        delete key_buf_char;
                    569:        delete key_buf_scan;
                    570:        
                    571:        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                    572:        
                    573:        return(retval);
                    574: }
                    575: 
                    576: /* ----------------------------------------------------------------------------
                    577:        MS-DOS virtual machine
                    578: ---------------------------------------------------------------------------- */
                    579: 
1.1.1.8   root      580: void update_key_buffer_tmp()
1.1       root      581: {
1.1.1.8   root      582:        DWORD dwNumberOfEvents = 0;
1.1       root      583:        DWORD dwRead;
                    584:        INPUT_RECORD ir[16];
                    585:        
1.1.1.8   root      586:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                    587:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                    588:                        for(int i = 0; i < dwRead; i++) {
                    589:                                if((ir[i].EventType & KEY_EVENT) && ir[i].Event.KeyEvent.bKeyDown) {
                    590:                                        if(ir[i].Event.KeyEvent.uChar.AsciiChar == 0) {
                    591:                                                // ignore shift, ctrl and alt keys
                    592:                                                if(ir[i].Event.KeyEvent.wVirtualScanCode != 0x1d &&
                    593:                                                   ir[i].Event.KeyEvent.wVirtualScanCode != 0x2a &&
                    594:                                                   ir[i].Event.KeyEvent.wVirtualScanCode != 0x36 &&
                    595:                                                   ir[i].Event.KeyEvent.wVirtualScanCode != 0x38) {
                    596:                                                        key_buf_char->write(0x00);
                    597:                                                        key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                    598:                                                        key_buf_char->write(0x00);
                    599:                                                        key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
                    600:                                                }
                    601:                                        } else {
                    602:                                                key_buf_char->write(ir[i].Event.KeyEvent.uChar.AsciiChar & 0xff);
1.1       root      603:                                                key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
                    604:                                        }
                    605:                                }
                    606:                        }
                    607:                }
                    608:        }
1.1.1.8   root      609: }
                    610: 
                    611: void update_key_buffer()
                    612: {
                    613:        int prev_count = key_buf_char->count();
                    614:        update_key_buffer_tmp();
                    615:        key_input += key_buf_char->count() - prev_count;
                    616:        
1.1       root      617:        if(key_buf_char->count() == 0) {
                    618:                Sleep(10);
                    619:        }
                    620: }
                    621: 
1.1.1.8   root      622: int check_key_input()
                    623: {
                    624:        if(key_input == 0) {
                    625:                int prev_count = key_buf_char->count();
                    626:                update_key_buffer_tmp();
                    627:                key_input = key_buf_char->count() - prev_count;
                    628:        }
                    629:        int val = key_input;
                    630:        key_input = 0;
                    631:        return(val);
                    632: }
                    633: 
1.1       root      634: // process info
                    635: 
                    636: process_t *msdos_process_info_create(UINT16 psp_seg)
                    637: {
                    638:        for(int i = 0; i < MAX_PROCESS; i++) {
                    639:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                    640:                        memset(&process[i], 0, sizeof(process_t));
                    641:                        process[i].psp = psp_seg;
                    642:                        return(&process[i]);
                    643:                }
                    644:        }
                    645:        fatalerror("too many processes\n");
                    646:        return(NULL);
                    647: }
                    648: 
                    649: process_t *msdos_process_info_get(UINT16 psp_seg)
                    650: {
                    651:        for(int i = 0; i < MAX_PROCESS; i++) {
                    652:                if(process[i].psp == psp_seg) {
                    653:                        return(&process[i]);
                    654:                }
                    655:        }
                    656:        fatalerror("invalid psp address\n");
                    657:        return(NULL);
                    658: }
                    659: 
                    660: void msdos_cds_update(int drv)
                    661: {
                    662:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                    663:        
                    664:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                    665:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                    666:        cds->drive_attrib = 0x4000;     // physical drive
                    667:        cds->physical_drive_number = drv;
                    668: }
                    669: 
                    670: // dbcs
                    671: 
                    672: void msdos_dbcs_table_update()
                    673: {
                    674:        UINT8 dbcs_data[DBCS_SIZE];
                    675:        memset(dbcs_data, 0, sizeof(dbcs_data));
                    676:        
                    677:        CPINFO info;
                    678:        GetCPInfo(active_code_page, &info);
                    679:        
                    680:        if(info.MaxCharSize != 1) {
                    681:                for(int i = 0;; i += 2) {
                    682:                        UINT8 lo = info.LeadByte[i + 0];
                    683:                        UINT8 hi = info.LeadByte[i + 1];
                    684:                        dbcs_data[2 + i + 0] = lo;
                    685:                        dbcs_data[2 + i + 1] = hi;
                    686:                        if(lo == 0 && hi == 0) {
                    687:                                dbcs_data[0] = i + 2;
                    688:                                break;
                    689:                        }
                    690:                }
                    691:        } else {
                    692:                dbcs_data[0] = 2;       // ???
                    693:        }
                    694:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                    695: }
                    696: 
                    697: void msdos_dbcs_table_init()
                    698: {
                    699:        system_code_page = active_code_page = _getmbcp();
                    700:        msdos_dbcs_table_update();
                    701: }
                    702: 
                    703: void msdos_dbcs_table_finish()
                    704: {
                    705:        if(active_code_page != system_code_page) {
                    706:                _setmbcp(system_code_page);
                    707:        }
                    708: }
                    709: 
                    710: int msdos_lead_byte_check(UINT8 code)
                    711: {
                    712:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                    713:        
                    714:        for(int i = 0;; i += 2) {
                    715:                UINT8 lo = dbcs_table[i + 0];
                    716:                UINT8 hi = dbcs_table[i + 1];
                    717:                if(lo == 0 && hi == 0) {
                    718:                        break;
                    719:                }
                    720:                if(lo <= code && code <= hi) {
                    721:                        return(1);
                    722:                }
                    723:        }
                    724:        return(0);
                    725: }
                    726: 
                    727: // file control
                    728: 
                    729: char *msdos_trimmed_path(char *path, int lfn)
                    730: {
                    731:        static char tmp[MAX_PATH];
                    732:        
                    733:        if(lfn) {
                    734:                strcpy(tmp, path);
                    735:        } else {
                    736:                // remove space in the path
                    737:                char *src = path, *dst = tmp;
                    738:                
                    739:                while(*src != '\0') {
                    740:                        if(msdos_lead_byte_check(*src)) {
                    741:                                *dst++ = *src++;
                    742:                                *dst++ = *src++;
                    743:                        } else if(*src != ' ') {
                    744:                                *dst++ = *src++;
                    745:                        } else {
                    746:                                src++;  // skip space
                    747:                        }
                    748:                }
                    749:                *dst = '\0';
                    750:        }
                    751:        return(tmp);
                    752: }
                    753: 
                    754: bool match(char *text, char *pattern)
                    755: {
                    756:        //http://www.prefield.com/algorithm/string/wildcard.html
                    757:        switch (*pattern) {
                    758:        case '\0':
                    759:                return !*text;
                    760:        case '*':
                    761:                return match(text, pattern + 1) || *text && match(text + 1, pattern);
                    762:        case '?':
                    763:                return *text && match(text + 1, pattern + 1);
                    764:        default:
                    765:                return (*text == *pattern) && match(text + 1, pattern + 1);
                    766:        }
                    767: }
                    768: 
                    769: bool msdos_match_volume_label(char *path, char *volume)
                    770: {
                    771:        char *p;
                    772:        
                    773:        if((p = my_strchr(path, ':')) != NULL) {
                    774:                return msdos_match_volume_label(p + 1, volume);
                    775:        } else if((p = my_strchr(path, '\\')) != NULL) {
                    776:                return msdos_match_volume_label(p + 1, volume);
                    777:        } else if((p = my_strchr(path, '.')) != NULL) {
                    778:                *p = '\0';
                    779:                bool result = match(volume, path);
                    780:                *p = '.';
                    781:                return result;
                    782:        } else {
                    783:                return match(volume, path);
                    784:        }
                    785: }
                    786: 
                    787: char *msdos_fcb_path(fcb_t *fcb)
                    788: {
                    789:        static char tmp[MAX_PATH];
                    790:        char name[9], ext[4];
                    791:        
                    792:        memset(name, 0, sizeof(name));
                    793:        memcpy(name, fcb->file_name, 8);
                    794:        strcpy(name, msdos_trimmed_path(name, 0));
                    795:        
                    796:        memset(ext, 0, sizeof(ext));
                    797:        memcpy(ext, fcb->file_name + 8, 3);
                    798:        strcpy(ext, msdos_trimmed_path(ext, 0));
                    799:        
                    800:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                    801:                strcpy(name, "*");
                    802:        }
                    803:        if(ext[0] == '\0') {
                    804:                strcpy(tmp, name);
                    805:        } else {
                    806:                if(strcmp(ext, "???") == 0) {
                    807:                        strcpy(ext, "*");
                    808:                }
                    809:                sprintf(tmp, "%s.%s", name, ext);
                    810:        }
                    811:        return(tmp);
                    812: }
                    813: 
                    814: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                    815: {
                    816:        char *ext = my_strchr(path, '.');
                    817:        
                    818:        memset(fcb->file_name, 0x20, 8 + 3);
                    819:        if(ext != NULL && path[0] != '.') {
                    820:                *ext = '\0';
                    821:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                    822:        }
                    823:        memcpy(fcb->file_name, path, strlen(path));
                    824: }
                    825: 
                    826: char *msdos_short_path(char *path)
                    827: {
                    828:        static char tmp[MAX_PATH];
                    829:        
                    830:        GetShortPathName(path, tmp, MAX_PATH);
                    831:        my_strupr(tmp);
                    832:        return(tmp);
                    833: }
                    834: 
                    835: char *msdos_short_full_path(char *path)
                    836: {
                    837:        static char tmp[MAX_PATH];
                    838:        char full[MAX_PATH], *name;
                    839:        
                    840:        GetFullPathName(path, MAX_PATH, full, &name);
                    841:        GetShortPathName(full, tmp, MAX_PATH);
                    842:        my_strupr(tmp);
                    843:        return(tmp);
                    844: }
                    845: 
                    846: char *msdos_short_full_dir(char *path)
                    847: {
                    848:        static char tmp[MAX_PATH];
                    849:        char full[MAX_PATH], *name;
                    850:        
                    851:        GetFullPathName(path, MAX_PATH, full, &name);
                    852:        name[-1] = '\0';
                    853:        GetShortPathName(full, tmp, MAX_PATH);
                    854:        my_strupr(tmp);
                    855:        return(tmp);
                    856: }
                    857: 
                    858: char *msdos_local_file_path(char *path, int lfn)
                    859: {
                    860:        char *trimmed = msdos_trimmed_path(path, lfn);
                    861:        
                    862:        if(_access(trimmed, 0) != 0) {
                    863:                process_t *process = msdos_process_info_get(current_psp);
                    864:                static char tmp[MAX_PATH];
                    865:                
                    866:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                    867:                if(_access(tmp, 0) == 0) {
                    868:                        return(tmp);
                    869:                }
                    870:        }
                    871:        return(trimmed);
                    872: }
                    873: 
1.1.1.11! root      874: bool msdos_is_con_path(char *path)
        !           875: {
        !           876:        char full[MAX_PATH], *name;
        !           877:        
        !           878:        GetFullPathName(path, MAX_PATH, full, &name);
        !           879:        return(_stricmp(full, "\\\\.\\CON") == 0);
        !           880: }
        !           881: 
1.1.1.8   root      882: char *msdos_remove_double_quote(char *path)
                    883: {
                    884:        static char tmp[MAX_PATH];
                    885:        
                    886:        memset(tmp, 0, sizeof(tmp));
                    887:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                    888:                memcpy(tmp, path + 1, strlen(path) - 2);
                    889:        } else {
                    890:                sprintf(tmp, path);
                    891:        }
                    892:        return(tmp);
                    893: }
                    894: 
                    895: char *msdos_combine_path(char *dir, char *file)
                    896: {
                    897:        static char tmp[MAX_PATH];
                    898:        char *tmp_dir = msdos_remove_double_quote(dir);
                    899:        
                    900:        if(strlen(tmp_dir) == 0) {
                    901:                strcpy(tmp, file);
                    902:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                    903:                sprintf(tmp, "%s%s", tmp_dir, file);
                    904:        } else {
                    905:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                    906:        }
                    907:        return(tmp);
                    908: }
                    909: 
1.1.1.9   root      910: char *msdos_search_command_com(char *command_path, char *env_path)
                    911: {
                    912:        static char tmp[MAX_PATH];
                    913:        char path[MAX_PATH], *file_name;
                    914:        
                    915:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                    916:                sprintf(file_name, "COMMAND.COM");
                    917:                if(_access(tmp, 0) == 0) {
                    918:                        return(tmp);
                    919:                }
                    920:        }
                    921:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                    922:                sprintf(file_name, "COMMAND.COM");
                    923:                if(_access(tmp, 0) == 0) {
                    924:                        return(tmp);
                    925:                }
                    926:        }
                    927:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                    928:                if(_access(tmp, 0) == 0) {
                    929:                        return(tmp);
                    930:                }
                    931:        }
                    932:        char *token = my_strtok(env_path, ";");
                    933:        while(token != NULL) {
                    934:                if(strlen(token) != 0) {
                    935:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                    936:                        if(_access(tmp, 0) == 0) {
                    937:                                return(tmp);
                    938:                        }
                    939:                }
                    940:                token = my_strtok(NULL, ";");
                    941:        }
                    942:        return(NULL);
                    943: }
                    944: 
1.1       root      945: int msdos_drive_number(char *path)
                    946: {
                    947:        char tmp[MAX_PATH], *name;
                    948:        
                    949:        GetFullPathName(path, MAX_PATH, tmp, &name);
                    950:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                    951:                return(tmp[0] - 'a');
                    952:        } else {
                    953:                return(tmp[0] - 'A');
                    954:        }
                    955: }
                    956: 
                    957: char *msdos_volume_label(char *path)
                    958: {
                    959:        static char tmp[MAX_PATH];
                    960:        char volume[] = "A:\\";
                    961:        
                    962:        if(path[1] == ':') {
                    963:                volume[0] = path[0];
                    964:        } else {
                    965:                volume[0] = 'A' + _getdrive() - 1;
                    966:        }
                    967:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                    968:                memset(tmp, 0, sizeof(tmp));
                    969:        }
                    970:        return(tmp);
                    971: }
                    972: 
                    973: char *msdos_short_volume_label(char *label)
                    974: {
                    975:        static char tmp[(8 + 1 + 3) + 1];
                    976:        char *src = label;
                    977:        int remain = strlen(label);
                    978:        char *dst_n = tmp;
                    979:        char *dst_e = tmp + 9;
                    980:        
                    981:        strcpy(tmp, "        .   ");
                    982:        for(int i = 0; i < 8 && remain > 0; i++) {
                    983:                if(msdos_lead_byte_check(*src)) {
                    984:                        if(++i == 8) {
                    985:                                break;
                    986:                        }
                    987:                        *dst_n++ = *src++;
                    988:                        remain--;
                    989:                }
                    990:                *dst_n++ = *src++;
                    991:                remain--;
                    992:        }
                    993:        if(remain > 0) {
                    994:                for(int i = 0; i < 3 && remain > 0; i++) {
                    995:                        if(msdos_lead_byte_check(*src)) {
                    996:                                if(++i == 3) {
                    997:                                        break;
                    998:                                }
                    999:                                *dst_e++ = *src++;
                   1000:                                remain--;
                   1001:                        }
                   1002:                        *dst_e++ = *src++;
                   1003:                        remain--;
                   1004:                }
                   1005:                *dst_e = '\0';
                   1006:        } else {
                   1007:                *dst_n = '\0';
                   1008:        }
                   1009:        my_strupr(tmp);
                   1010:        return(tmp);
                   1011: }
                   1012: 
                   1013: void msdos_file_handler_open(int fd, char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   1014: {
                   1015:        static int id = 0;
                   1016:        char full[MAX_PATH], *name;
                   1017:        
                   1018:        if(psp_seg && fd < 20) {
                   1019:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1020:                psp->file_table[fd] = fd;
                   1021:        }
                   1022:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   1023:                strcpy(file_handler[fd].path, full);
                   1024:        } else {
                   1025:                strcpy(file_handler[fd].path, path);
                   1026:        }
                   1027:        file_handler[fd].valid = 1;
                   1028:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
                   1029:        file_handler[fd].atty = atty;
                   1030:        file_handler[fd].mode = mode;
                   1031:        file_handler[fd].info = info;
                   1032:        file_handler[fd].psp = psp_seg;
                   1033: }
                   1034: 
                   1035: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   1036: {
                   1037:        if(psp_seg && dst < 20) {
                   1038:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1039:                psp->file_table[dst] = dst;
                   1040:        }
                   1041:        strcpy(file_handler[dst].path, file_handler[src].path);
                   1042:        file_handler[dst].valid = 1;
                   1043:        file_handler[dst].id = file_handler[src].id;
                   1044:        file_handler[dst].atty = file_handler[src].atty;
                   1045:        file_handler[dst].mode = file_handler[src].mode;
                   1046:        file_handler[dst].info = file_handler[src].info;
                   1047:        file_handler[dst].psp = psp_seg;
                   1048: }
                   1049: 
                   1050: void msdos_file_handler_close(int fd, UINT16 psp_seg)
                   1051: {
                   1052:        if(psp_seg && fd < 20) {
                   1053:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1054:                psp->file_table[fd] = 0xff;
                   1055:        }
                   1056:        file_handler[fd].valid = 0;
                   1057: }
                   1058: 
                   1059: int msdos_file_attribute_create(UINT16 new_attr)
                   1060: {
                   1061:        int attr = 0;
                   1062:        
                   1063:        if(REG16(CX) & 0x01) {
                   1064:                attr |= FILE_ATTRIBUTE_READONLY;
                   1065:        }
                   1066:        if(REG16(CX) & 0x02) {
                   1067:                attr |= FILE_ATTRIBUTE_HIDDEN;
                   1068:        }
                   1069:        if(REG16(CX) & 0x04) {
                   1070:                attr |= FILE_ATTRIBUTE_SYSTEM;
                   1071:        }
                   1072:        if(REG16(CX) & 0x20) {
                   1073:                attr |= FILE_ATTRIBUTE_ARCHIVE;
                   1074:        }
                   1075:        return(attr);
                   1076: }
                   1077: 
                   1078: // find file
                   1079: 
                   1080: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   1081: {
                   1082:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   1083:                return(0);      // search directory only !!!
                   1084:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   1085:                return(0);
                   1086:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   1087:                return(0);
                   1088:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   1089:                return(0);
                   1090:        } else if((attribute & required_mask) != required_mask) {
                   1091:                return(0);
                   1092:        } else {
                   1093:                return(1);
                   1094:        }
                   1095: }
                   1096: 
                   1097: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   1098: {
                   1099:        FILETIME local;
                   1100:        
                   1101:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   1102:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   1103:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   1104:        
                   1105:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   1106:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   1107:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   1108:        
                   1109:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   1110:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   1111:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   1112: }
                   1113: 
                   1114: // i/o
                   1115: 
                   1116: void msdos_putch(UINT8 data);
                   1117: 
                   1118: void msdos_stdio_reopen()
                   1119: {
                   1120:        if(!file_handler[0].valid) {
                   1121:                _dup2(DUP_STDIN, 0);
                   1122:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   1123:        }
                   1124:        if(!file_handler[1].valid) {
                   1125:                _dup2(DUP_STDOUT, 1);
                   1126:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   1127:        }
                   1128:        if(!file_handler[2].valid) {
                   1129:                _dup2(DUP_STDERR, 2);
                   1130:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   1131:        }
                   1132: }
                   1133: 
                   1134: int msdos_kbhit()
                   1135: {
                   1136:        msdos_stdio_reopen();
                   1137:        
                   1138:        if(!file_handler[0].atty) {
                   1139:                // stdin is redirected to file
                   1140:                return(eof(0) == 0);
                   1141:        }
                   1142:        
                   1143:        // check keyboard status
1.1.1.5   root     1144:        if(key_buf_char->count() != 0 || key_code != 0) {
1.1       root     1145:                return(1);
                   1146:        } else {
                   1147:                return(_kbhit());
                   1148:        }
                   1149: }
                   1150: 
                   1151: int msdos_getch_ex(int echo)
                   1152: {
                   1153:        static char prev = 0;
                   1154:        
                   1155:        msdos_stdio_reopen();
                   1156:        
                   1157:        if(!file_handler[0].atty) {
                   1158:                // stdin is redirected to file
                   1159: retry:
                   1160:                char data;
                   1161:                if(_read(0, &data, 1) == 1) {
                   1162:                        char tmp = data;
                   1163:                        if(data == 0x0a) {
                   1164:                                if(prev == 0x0d) {
                   1165:                                        goto retry; // CRLF -> skip LF
                   1166:                                } else {
                   1167:                                        data = 0x0d; // LF only -> CR
                   1168:                                }
                   1169:                        }
                   1170:                        prev = tmp;
                   1171:                        return(data);
                   1172:                }
                   1173:                return(EOF);
                   1174:        }
                   1175:        
                   1176:        // input from console
1.1.1.5   root     1177:        int key_char, key_scan;
                   1178:        if(key_code != 0) {
                   1179:                key_char = (key_code >> 0) & 0xff;
                   1180:                key_scan = (key_code >> 8) & 0xff;
                   1181:                key_code >>= 16;
                   1182:        } else {
                   1183:                while(key_buf_char->count() == 0) {
                   1184:                        update_key_buffer();
                   1185:                }
                   1186:                key_char = key_buf_char->read();
                   1187:                key_scan = key_buf_scan->read();
1.1       root     1188:        }
                   1189:        if(echo && key_char) {
                   1190:                msdos_putch(key_char);
                   1191:        }
                   1192:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   1193: }
                   1194: 
                   1195: inline int msdos_getch()
                   1196: {
                   1197:        return(msdos_getch_ex(0));
                   1198: }
                   1199: 
                   1200: inline int msdos_getche()
                   1201: {
                   1202:        return(msdos_getch_ex(1));
                   1203: }
                   1204: 
                   1205: int msdos_write(int fd, const void *buffer, unsigned int count)
                   1206: {
                   1207:        static int is_cr = 0;
                   1208:        
                   1209:        if(fd == 1 && !file_handler[1].atty) {
                   1210:                // CR+LF -> LF
                   1211:                UINT8 *buf = (UINT8 *)buffer;
                   1212:                for(unsigned int i = 0; i < count; i++) {
                   1213:                        UINT8 data = buf[i];
                   1214:                        if(is_cr) {
                   1215:                                if(data != 0x0a) {
                   1216:                                        UINT8 tmp = 0x0d;
                   1217:                                        _write(1, &tmp, 1);
                   1218:                                }
                   1219:                                _write(1, &data, 1);
                   1220:                                is_cr = 0;
                   1221:                        } else if(data == 0x0d) {
                   1222:                                is_cr = 1;
                   1223:                        } else {
                   1224:                                _write(1, &data, 1);
                   1225:                        }
                   1226:                }
                   1227:                return(count);
                   1228:        }
                   1229:        return(_write(fd, buffer, count));
                   1230: }
                   1231: 
                   1232: void msdos_putch(UINT8 data)
                   1233: {
                   1234:        static int p = 0;
                   1235:        static int is_kanji = 0;
                   1236:        static int is_esc = 0;
                   1237:        static int stored_x;
                   1238:        static int stored_y;
                   1239:        static WORD stored_a;
                   1240:        static char tmp[64];
                   1241:        
                   1242:        msdos_stdio_reopen();
                   1243:        
                   1244:        if(!file_handler[1].atty) {
                   1245:                // stdout is redirected to file
                   1246:                msdos_write(1, &data, 1);
                   1247:                return;
                   1248:        }
                   1249:        
                   1250:        // output to console
                   1251:        tmp[p++] = data;
                   1252:        
                   1253:        if(is_kanji) {
                   1254:                // kanji character
                   1255:                is_kanji = 0;
                   1256:        } else if(is_esc) {
                   1257:                // escape sequense
                   1258:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   1259:                        p = is_esc = 0;
                   1260:                } else if(tmp[1] == '=' && p == 4) {
                   1261:                        COORD co;
                   1262:                        co.X = tmp[3] - 0x20;
                   1263:                        co.Y = tmp[2] - 0x20;
                   1264:                        SetConsoleCursorPosition(hStdout, co);
                   1265:                        mem[0x450 + mem[0x462] * 2] = co.X;
                   1266:                        mem[0x451 + mem[0x462] * 2] = co.Y;
                   1267:                        cursor_moved = false;
                   1268:                        p = is_esc = 0;
                   1269:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   1270:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   1271:                        COORD co;
                   1272:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   1273:                        co.X = csbi.dwCursorPosition.X;
                   1274:                        co.Y = csbi.dwCursorPosition.Y;
                   1275:                        WORD wAttributes = csbi.wAttributes;
                   1276:                        
                   1277:                        if(tmp[1] == 'D') {
                   1278:                                co.Y++;
                   1279:                        } else if(tmp[1] == 'E') {
                   1280:                                co.X = 0;
                   1281:                                co.Y++;
                   1282:                        } else if(tmp[1] == 'M') {
                   1283:                                co.Y--;
                   1284:                        } else if(tmp[1] == '*') {
                   1285:                                SMALL_RECT rect;
                   1286:                                SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1287:                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1288:                                co.X = co.Y = 0;
                   1289:                        } else if(tmp[1] == '[') {
                   1290:                                int param[256], params = 0;
                   1291:                                memset(param, 0, sizeof(param));
                   1292:                                for(int i = 2; i < p; i++) {
                   1293:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   1294:                                                param[params] *= 10;
                   1295:                                                param[params] += tmp[i] - '0';
                   1296:                                        } else {
                   1297:                                                params++;
                   1298:                                        }
                   1299:                                }
                   1300:                                if(data == 'A') {
                   1301:                                        co.Y -= param[0];
                   1302:                                } else if(data == 'B') {
                   1303:                                        co.Y += param[0];
                   1304:                                } else if(data == 'C') {
                   1305:                                        co.X += param[0];
                   1306:                                } else if(data == 'D') {
                   1307:                                        co.X -= param[0];
                   1308:                                } else if(data == 'H' || data == 'f') {
                   1309:                                        co.X = param[1] - 1;
                   1310:                                        co.Y = param[0] - 1;
                   1311:                                } else if(data == 'J') {
                   1312:                                        SMALL_RECT rect;
                   1313:                                        if(param[0] == 0) {
                   1314:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
                   1315:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1316:                                                if(co.Y < csbi.dwSize.Y - 1) {
                   1317:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1318:                                                        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1319:                                                }
                   1320:                                        } else if(param[0] == 1) {
                   1321:                                                if(co.Y > 0) {
                   1322:                                                        SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, co.Y - 1);
                   1323:                                                        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1324:                                                }
                   1325:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
                   1326:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1327:                                        } else if(param[0] == 2) {
                   1328:                                                SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1329:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1330:                                                co.X = co.Y = 0;
                   1331:                                        }
                   1332:                                } else if(data == 'K') {
                   1333:                                        SMALL_RECT rect;
                   1334:                                        if(param[0] == 0) {
                   1335:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
                   1336:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1337:                                        } else if(param[0] == 1) {
                   1338:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
                   1339:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1340:                                        } else if(param[0] == 2) {
                   1341:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
                   1342:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1343:                                        }
                   1344:                                } else if(data == 'L') {
                   1345:                                        SMALL_RECT rect;
                   1346:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1347:                                        ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1348:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1349:                                        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1350:                                        // clear buffer
                   1351:                                        for(int y = 0; y < SCR_BUF_SIZE; y++) {
                   1352:                                                for(int x = 0; x < 80; x++) {
                   1353:                                                        scr_buf[y][x].Char.AsciiChar = ' ';
                   1354:                                                        scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   1355:                                                }
                   1356:                                        }
                   1357:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
                   1358:                                        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1359:                                        co.X = 0;
                   1360:                                } else if(data == 'M') {
                   1361:                                        SMALL_RECT rect;
                   1362:                                        if(co.Y + param[0] > csbi.dwSize.Y - 1) {
                   1363:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1364:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1365:                                        } else {
                   1366:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1367:                                                ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1368:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
                   1369:                                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   1370:                                                // clear buffer
                   1371:                                                for(int y = 0; y < SCR_BUF_SIZE; y++) {
                   1372:                                                        for(int x = 0; x < 80; x++) {
                   1373:                                                                scr_buf[y][x].Char.AsciiChar = ' ';
                   1374:                                                                scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   1375:                                                        }
                   1376:                                                }
                   1377:                                        }
                   1378:                                        co.X = 0;
                   1379:                                } else if(data == 'h') {
                   1380:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   1381:                                                CONSOLE_CURSOR_INFO cur;
                   1382:                                                GetConsoleCursorInfo(hStdout, &cur);
                   1383:                                                if(cur.bVisible) {
                   1384:                                                        cur.bVisible = FALSE;
                   1385:                                                        GetConsoleCursorInfo(hStdout, &cur);
                   1386:                                                }
                   1387:                                        }
                   1388:                                } else if(data == 'l') {
                   1389:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   1390:                                                CONSOLE_CURSOR_INFO cur;
                   1391:                                                GetConsoleCursorInfo(hStdout, &cur);
                   1392:                                                if(!cur.bVisible) {
                   1393:                                                        cur.bVisible = TRUE;
                   1394:                                                        GetConsoleCursorInfo(hStdout, &cur);
                   1395:                                                }
                   1396:                                        }
                   1397:                                } else if(data == 'm') {
                   1398:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   1399:                                        int reverse = 0, hidden = 0;
                   1400:                                        for(int i = 0; i < params; i++) {
                   1401:                                                if(param[i] == 1) {
                   1402:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   1403:                                                } else if(param[i] == 4) {
                   1404:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   1405:                                                } else if(param[i] == 7) {
                   1406:                                                        reverse = 1;
                   1407:                                                } else if(param[i] == 8 || param[i] == 16) {
                   1408:                                                        hidden = 1;
                   1409:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   1410:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   1411:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   1412:                                                                param[i] -= 16;
                   1413:                                                        } else {
                   1414:                                                                param[i] -= 30;
                   1415:                                                        }
                   1416:                                                        if(param[i] & 1) {
                   1417:                                                                wAttributes |= FOREGROUND_RED;
                   1418:                                                        }
                   1419:                                                        if(param[i] & 2) {
                   1420:                                                                wAttributes |= FOREGROUND_GREEN;
                   1421:                                                        }
                   1422:                                                        if(param[i] & 4) {
                   1423:                                                                wAttributes |= FOREGROUND_BLUE;
                   1424:                                                        }
                   1425:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   1426:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   1427:                                                        if((param[i] - 40) & 1) {
                   1428:                                                                wAttributes |= BACKGROUND_RED;
                   1429:                                                        }
                   1430:                                                        if((param[i] - 40) & 2) {
                   1431:                                                                wAttributes |= BACKGROUND_GREEN;
                   1432:                                                        }
                   1433:                                                        if((param[i] - 40) & 4) {
                   1434:                                                                wAttributes |= BACKGROUND_BLUE;
                   1435:                                                        }
                   1436:                                                }
                   1437:                                        }
                   1438:                                        if(reverse) {
                   1439:                                                wAttributes &= ~0xff;
                   1440:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   1441:                                        }
                   1442:                                        if(hidden) {
                   1443:                                                wAttributes &= ~0x0f;
                   1444:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   1445:                                        }
                   1446:                                } else if(data == 'n') {
                   1447:                                        if(param[0] == 6) {
                   1448:                                                char tmp[16];
                   1449:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   1450:                                                int len = strlen(tmp);
                   1451:                                                for(int i = 0; i < len; i++) {
                   1452:                                                        key_buf_char->write(tmp[i]);
                   1453:                                                        key_buf_scan->write(0x00);
                   1454:                                                }
                   1455:                                        }
                   1456:                                } else if(data == 's') {
                   1457:                                        stored_x = co.X;
                   1458:                                        stored_y = co.Y;
                   1459:                                        stored_a = wAttributes;
                   1460:                                } else if(data == 'u') {
                   1461:                                        co.X = stored_x;
                   1462:                                        co.Y = stored_y;
                   1463:                                        wAttributes = stored_a;
                   1464:                                }
                   1465:                        }
                   1466:                        if(co.X < 0) {
                   1467:                                co.X = 0;
                   1468:                        } else if(co.X >= csbi.dwSize.X) {
                   1469:                                co.X = csbi.dwSize.X - 1;
                   1470:                        }
                   1471:                        if(co.Y < 0) {
                   1472:                                co.Y = 0;
                   1473:                        } else if(co.Y >= csbi.dwSize.Y) {
                   1474:                                co.Y = csbi.dwSize.Y - 1;
                   1475:                        }
                   1476:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   1477:                                SetConsoleCursorPosition(hStdout, co);
                   1478:                                mem[0x450 + mem[0x462] * 2] = co.X;
                   1479:                                mem[0x451 + mem[0x462] * 2] = co.Y;
                   1480:                                cursor_moved = false;
                   1481:                        }
                   1482:                        if(wAttributes != csbi.wAttributes) {
                   1483:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   1484:                        }
                   1485:                        p = is_esc = 0;
                   1486:                }
                   1487:                return;
                   1488:        } else {
                   1489:                if(msdos_lead_byte_check(data)) {
                   1490:                        is_kanji = 1;
                   1491:                        return;
                   1492:                } else if(data == 0x1b) {
                   1493:                        is_esc = 1;
                   1494:                        return;
                   1495:                }
                   1496:        }
                   1497:        tmp[p++] = '\0';
                   1498:        p = 0;
                   1499:        printf("%s", tmp);
                   1500:        cursor_moved = true;
                   1501: }
                   1502: 
                   1503: int msdos_aux_in()
                   1504: {
                   1505: #ifdef SUPPORT_AUX_PRN
                   1506:        if(file_handler[3].valid && !eof(3)) {
                   1507:                char data = 0;
                   1508:                _read(3, &data, 1);
                   1509:                return(data);
                   1510:        } else {
                   1511:                return(EOF);
                   1512:        }
                   1513: #else
                   1514:        return(0);
                   1515: #endif
                   1516: }
                   1517: 
                   1518: void msdos_aux_out(char data)
                   1519: {
                   1520: #ifdef SUPPORT_AUX_PRN
                   1521:        if(file_handler[3].valid) {
                   1522:                msdos_write(3, &data, 1);
                   1523:        }
                   1524: #endif
                   1525: }
                   1526: 
                   1527: void msdos_prn_out(char data)
                   1528: {
                   1529: #ifdef SUPPORT_AUX_PRN
                   1530:        if(file_handler[4].valid) {
                   1531:                msdos_write(4, &data, 1);
                   1532:        }
                   1533: #endif
                   1534: }
                   1535: 
                   1536: // memory control
                   1537: 
                   1538: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, UINT16 paragraphs)
                   1539: {
                   1540:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1541:        
                   1542:        mcb->mz = mz;
                   1543:        mcb->psp = psp;
                   1544:        mcb->paragraphs = paragraphs;
                   1545:        return(mcb);
                   1546: }
                   1547: 
                   1548: void msdos_mcb_check(mcb_t *mcb)
                   1549: {
                   1550:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
                   1551:                fatalerror("broken mcb\n");
                   1552:        }
                   1553: }
                   1554: 
                   1555: int msdos_mem_split(int seg, int paragraphs)
                   1556: {
                   1557:        int mcb_seg = seg - 1;
                   1558:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1559:        msdos_mcb_check(mcb);
                   1560:        
                   1561:        if(mcb->paragraphs > paragraphs) {
                   1562:                int new_seg = mcb_seg + 1 + paragraphs;
                   1563:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
                   1564:                
                   1565:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   1566:                mcb->mz = 'M';
                   1567:                mcb->paragraphs = paragraphs;
                   1568:                return(0);
                   1569:        }
                   1570:        return(-1);
                   1571: }
                   1572: 
                   1573: void msdos_mem_merge(int seg)
                   1574: {
                   1575:        int mcb_seg = seg - 1;
                   1576:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1577:        msdos_mcb_check(mcb);
                   1578:        
                   1579:        while(1) {
                   1580:                if(mcb->mz == 'Z') {
                   1581:                        break;
                   1582:                }
                   1583:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   1584:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   1585:                msdos_mcb_check(next_mcb);
                   1586:                
                   1587:                if(next_mcb->psp != 0) {
                   1588:                        break;
                   1589:                }
                   1590:                mcb->mz = next_mcb->mz;
                   1591:                mcb->paragraphs += 1 + next_mcb->paragraphs;
                   1592:        }
                   1593: }
                   1594: 
1.1.1.8   root     1595: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     1596: {
                   1597:        while(1) {
                   1598:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1599:                msdos_mcb_check(mcb);
                   1600:                
1.1.1.8   root     1601:                if(!(new_process && mcb->mz != 'Z')) {
1.1       root     1602:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
                   1603:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   1604:                                mcb->psp = current_psp;
                   1605:                                return(mcb_seg + 1);
                   1606:                        }
                   1607:                }
                   1608:                if(mcb->mz == 'Z') {
                   1609:                        break;
                   1610:                }
                   1611:                mcb_seg += 1 + mcb->paragraphs;
                   1612:        }
                   1613:        return(-1);
                   1614: }
                   1615: 
                   1616: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   1617: {
                   1618:        int mcb_seg = seg - 1;
                   1619:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1620:        msdos_mcb_check(mcb);
                   1621:        int current_paragraphs = mcb->paragraphs;
                   1622:        
                   1623:        msdos_mem_merge(seg);
                   1624:        if(paragraphs > mcb->paragraphs) {
                   1625:                *max_paragraphs = mcb->paragraphs;
                   1626:                msdos_mem_split(seg, current_paragraphs);
                   1627:                return(-1);
                   1628:        }
                   1629:        msdos_mem_split(seg, paragraphs);
                   1630:        return(0);
                   1631: }
                   1632: 
                   1633: void msdos_mem_free(int seg)
                   1634: {
                   1635:        int mcb_seg = seg - 1;
                   1636:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1637:        msdos_mcb_check(mcb);
                   1638:        
                   1639:        mcb->psp = 0;
                   1640:        msdos_mem_merge(seg);
                   1641: }
                   1642: 
1.1.1.8   root     1643: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     1644: {
                   1645:        int max_paragraphs = 0;
                   1646:        
                   1647:        while(1) {
                   1648:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1649:                msdos_mcb_check(mcb);
                   1650:                
1.1.1.8   root     1651:                if(!(new_process && mcb->mz != 'Z')) {
1.1       root     1652:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   1653:                                max_paragraphs = mcb->paragraphs;
                   1654:                        }
                   1655:                }
                   1656:                if(mcb->mz == 'Z') {
                   1657:                        break;
                   1658:                }
                   1659:                mcb_seg += 1 + mcb->paragraphs;
                   1660:        }
                   1661:        return(max_paragraphs);
                   1662: }
                   1663: 
1.1.1.8   root     1664: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   1665: {
                   1666:        int last_seg = -1;
                   1667:        
                   1668:        while(1) {
                   1669:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   1670:                msdos_mcb_check(mcb);
                   1671:                
                   1672:                if(mcb->mz == 'Z') {
                   1673:                        break;
                   1674:                } else if(mcb->psp == psp) {
                   1675:                        last_seg = mcb_seg;
                   1676:                }
                   1677:                mcb_seg += 1 + mcb->paragraphs;
                   1678:        }
                   1679:        return(last_seg);
                   1680: }
                   1681: 
1.1       root     1682: // environment
                   1683: 
                   1684: void msdos_env_set_argv(int env_seg, char *argv)
                   1685: {
                   1686:        char *dst = (char *)(mem + (env_seg << 4));
                   1687:        
                   1688:        while(1) {
                   1689:                if(dst[0] == 0) {
                   1690:                        break;
                   1691:                }
                   1692:                dst += strlen(dst) + 1;
                   1693:        }
                   1694:        *dst++ = 0; // end of environment
                   1695:        *dst++ = 1; // top of argv[0]
                   1696:        *dst++ = 0;
                   1697:        memcpy(dst, argv, strlen(argv));
                   1698:        dst += strlen(argv);
                   1699:        *dst++ = 0;
                   1700:        *dst++ = 0;
                   1701: }
                   1702: 
                   1703: char *msdos_env_get_argv(int env_seg)
                   1704: {
                   1705:        static char env[ENV_SIZE];
                   1706:        char *src = env;
                   1707:        
                   1708:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   1709:        while(1) {
                   1710:                if(src[0] == 0) {
                   1711:                        if(src[1] == 1) {
                   1712:                                return(src + 3);
                   1713:                        }
                   1714:                        break;
                   1715:                }
                   1716:                src += strlen(src) + 1;
                   1717:        }
                   1718:        return(NULL);
                   1719: }
                   1720: 
                   1721: char *msdos_env_get(int env_seg, const char *name)
                   1722: {
                   1723:        static char env[ENV_SIZE];
                   1724:        char *src = env;
                   1725:        
                   1726:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   1727:        while(1) {
                   1728:                if(src[0] == 0) {
                   1729:                        break;
                   1730:                }
                   1731:                int len = strlen(src);
                   1732:                char *n = my_strtok(src, "=");
                   1733:                char *v = src + strlen(n) + 1;
                   1734:                
                   1735:                if(_stricmp(name, n) == 0) {
                   1736:                        return(v);
                   1737:                }
                   1738:                src += len + 1;
                   1739:        }
                   1740:        return(NULL);
                   1741: }
                   1742: 
                   1743: void msdos_env_set(int env_seg, char *name, char *value)
                   1744: {
                   1745:        char env[ENV_SIZE];
                   1746:        char *src = env;
                   1747:        char *dst = (char *)(mem + (env_seg << 4));
                   1748:        char *argv = msdos_env_get_argv(env_seg);
                   1749:        int done = 0;
                   1750:        
                   1751:        memcpy(src, dst, ENV_SIZE);
                   1752:        memset(dst, 0, ENV_SIZE);
                   1753:        while(1) {
                   1754:                if(src[0] == 0) {
                   1755:                        break;
                   1756:                }
                   1757:                int len = strlen(src);
                   1758:                char *n = my_strtok(src, "=");
                   1759:                char *v = src + strlen(n) + 1;
                   1760:                char tmp[1024];
                   1761:                
                   1762:                if(_stricmp(name, n) == 0) {
                   1763:                        sprintf(tmp, "%s=%s", n, value);
                   1764:                        done = 1;
                   1765:                } else {
                   1766:                        sprintf(tmp, "%s=%s", n, v);
                   1767:                }
                   1768:                memcpy(dst, tmp, strlen(tmp));
                   1769:                dst += strlen(tmp) + 1;
                   1770:                src += len + 1;
                   1771:        }
                   1772:        if(!done) {
                   1773:                char tmp[1024];
                   1774:                
                   1775:                sprintf(tmp, "%s=%s", name, value);
                   1776:                memcpy(dst, tmp, strlen(tmp));
                   1777:                dst += strlen(tmp) + 1;
                   1778:        }
                   1779:        if(argv) {
                   1780:                *dst++ = 0; // end of environment
                   1781:                *dst++ = 1; // top of argv[0]
                   1782:                *dst++ = 0;
                   1783:                memcpy(dst, argv, strlen(argv));
                   1784:                dst += strlen(argv);
                   1785:                *dst++ = 0;
                   1786:                *dst++ = 0;
                   1787:        }
                   1788: }
                   1789: 
                   1790: // process
                   1791: 
1.1.1.8   root     1792: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     1793: {
                   1794:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1795:        
                   1796:        memset(psp, 0, PSP_SIZE);
                   1797:        psp->exit[0] = 0xcd;
                   1798:        psp->exit[1] = 0x20;
1.1.1.8   root     1799:        psp->first_mcb = mcb_seg;
1.1       root     1800:        psp->far_call = 0xea;
                   1801:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   1802:        psp->cpm_entry.w.h = 0xf000;
                   1803:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   1804:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   1805:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   1806:        psp->parent_psp = parent_psp;
                   1807:        for(int i = 0; i < 20; i++) {
                   1808:                if(file_handler[i].valid) {
                   1809:                        psp->file_table[i] = i;
                   1810:                } else {
                   1811:                        psp->file_table[i] = 0xff;
                   1812:                }
                   1813:        }
                   1814:        psp->env_seg = env_seg;
                   1815:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     1816:        psp->stack.w.h = SREG(SS);
1.1       root     1817:        psp->service[0] = 0xcd;
                   1818:        psp->service[1] = 0x21;
                   1819:        psp->service[2] = 0xcb;
                   1820:        return(psp);
                   1821: }
                   1822: 
                   1823: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   1824: {
                   1825:        // load command file
                   1826:        int fd = -1;
                   1827:        int dos_command = 0;
1.1.1.4   root     1828:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name, name_tmp[MAX_PATH];
1.1       root     1829:        
                   1830:        strcpy(command, cmd);
                   1831:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   1832:        int opt_len = mem[opt_ofs];
                   1833:        memset(opt, 0, sizeof(opt));
                   1834:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   1835:        
                   1836:        // check command.com
                   1837:        GetFullPathName(command, MAX_PATH, path, &name);
1.1.1.4   root     1838:        memset(name_tmp, 0, sizeof(name_tmp));
                   1839:        strcpy(name_tmp, name);
                   1840:        
1.1       root     1841:        if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "CMD.EXE") == 0) {
                   1842:                for(int i = 0; i < opt_len; i++) {
                   1843:                        if(opt[i] == ' ') {
                   1844:                                continue;
                   1845:                        }
                   1846:                        if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   1847:                                dos_command = 1;
                   1848:                                for(int j = i + 3; j < opt_len; j++) {
                   1849:                                        if(opt[j] == ' ') {
                   1850:                                                continue;
                   1851:                                        }
                   1852:                                        char *token = my_strtok(opt + j, " ");
                   1853:                                        strcpy(command, token);
                   1854:                                        char tmp[MAX_PATH];
                   1855:                                        strcpy(tmp, token + strlen(token) + 1);
                   1856:                                        strcpy(opt, tmp);
                   1857:                                        opt_len = strlen(opt);
                   1858:                                        mem[opt_ofs] = opt_len;
                   1859:                                        strcpy((char *)(mem + opt_ofs + 1), opt);
                   1860:                                        break;
                   1861:                                }
                   1862:                        }
                   1863:                        break;
                   1864:                }
                   1865:        }
                   1866:        
                   1867:        // load command file
                   1868:        strcpy(path, command);
                   1869:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   1870:                sprintf(path, "%s.COM", command);
                   1871:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   1872:                        sprintf(path, "%s.EXE", command);
                   1873:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   1874:                                // search path in parent environments
                   1875:                                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   1876:                                char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   1877:                                if(env != NULL) {
                   1878:                                        char env_path[1024];
                   1879:                                        strcpy(env_path, env);
                   1880:                                        char *token = my_strtok(env_path, ";");
                   1881:                                        
                   1882:                                        while(token != NULL) {
1.1.1.8   root     1883:                                                if(strlen(token) != 0) {
                   1884:                                                        sprintf(path, "%s", msdos_combine_path(token, command));
                   1885:                                                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   1886:                                                                break;
                   1887:                                                        }
                   1888:                                                        sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   1889:                                                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   1890:                                                                break;
                   1891:                                                        }
                   1892:                                                        sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   1893:                                                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   1894:                                                                break;
                   1895:                                                        }
1.1       root     1896:                                                }
                   1897:                                                token = my_strtok(NULL, ";");
                   1898:                                        }
                   1899:                                }
                   1900:                        }
                   1901:                }
                   1902:        }
                   1903:        if(fd == -1) {
                   1904:                if(dos_command) {
                   1905:                        // may be dos command
                   1906:                        char tmp[MAX_PATH];
                   1907:                        sprintf(tmp, "%s %s", command, opt);
                   1908:                        system(tmp);
                   1909:                        return(0);
                   1910:                } else {
                   1911:                        return(-1);
                   1912:                }
                   1913:        }
                   1914:        _read(fd, file_buffer, sizeof(file_buffer));
                   1915:        _close(fd);
                   1916:        
                   1917:        // copy environment
                   1918:        int env_seg, psp_seg;
                   1919:        
1.1.1.8   root     1920:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1       root     1921:                return(-1);
                   1922:        }
                   1923:        if(param->env_seg == 0) {
                   1924:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   1925:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   1926:        } else {
                   1927:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   1928:        }
                   1929:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   1930:        
                   1931:        // check exe header
                   1932:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     1933:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     1934:        UINT16 cs, ss, ip, sp;
                   1935:        
                   1936:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   1937:                // memory allocation
                   1938:                int header_size = header->header_size * 16;
                   1939:                int load_size = header->pages * 512 - header_size;
                   1940:                if(header_size + load_size < 512) {
                   1941:                        load_size = 512 - header_size;
                   1942:                }
                   1943:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   1944:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   1945:                        msdos_mem_free(env_seg);
                   1946:                        return(-1);
                   1947:                }
                   1948:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   1949:                if(paragraphs > free_paragraphs) {
                   1950:                        paragraphs = free_paragraphs;
                   1951:                }
1.1.1.8   root     1952:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     1953:                        msdos_mem_free(env_seg);
                   1954:                        return(-1);
                   1955:                }
                   1956:                // relocation
                   1957:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   1958:                for(int i = 0; i < header->relocations; i++) {
                   1959:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   1960:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   1961:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   1962:                }
                   1963:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   1964:                // segments
                   1965:                cs = header->init_cs + start_seg;
                   1966:                ss = header->init_ss + start_seg;
                   1967:                ip = header->init_ip;
                   1968:                sp = header->init_sp - 2; // for symdeb
                   1969:        } else {
                   1970:                // memory allocation
                   1971:                paragraphs = free_paragraphs;
1.1.1.8   root     1972:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1       root     1973:                        msdos_mem_free(env_seg);
                   1974:                        return(-1);
                   1975:                }
                   1976:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   1977:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   1978:                // segments
                   1979:                cs = ss = psp_seg;
                   1980:                ip = 0x100;
                   1981:                sp = 0xfffe;
                   1982:        }
                   1983:        
                   1984:        // create psp
1.1.1.3   root     1985: #if defined(HAS_I386)
                   1986:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   1987: #else
                   1988:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_pc - SREG_BASE(CS);
                   1989: #endif
                   1990:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     1991:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   1992:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   1993:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   1994:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   1995:        
                   1996:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   1997:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   1998:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   1999:        
1.1.1.4   root     2000:        for(int i = 0; i < 8; i++) {
                   2001:                if(name_tmp[i] == '.') {
                   2002:                        mcb_psp->prog_name[i] = '\0';
                   2003:                        break;
                   2004:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   2005:                        mcb_psp->prog_name[i] = name_tmp[i];
                   2006:                        i++;
                   2007:                        mcb_psp->prog_name[i] = name_tmp[i];
                   2008:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   2009:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   2010:                } else {
                   2011:                        mcb_psp->prog_name[i] = name_tmp[i];
                   2012:                }
                   2013:        }
                   2014:        
1.1       root     2015:        // process info
                   2016:        process_t *process = msdos_process_info_create(psp_seg);
                   2017:        strcpy(process->module_dir, msdos_short_full_dir(path));
                   2018:        process->dta.w.l = 0x80;
                   2019:        process->dta.w.h = psp_seg;
                   2020:        process->switchar = '/';
                   2021:        process->max_files = 20;
                   2022:        process->find_handle = INVALID_HANDLE_VALUE;
                   2023:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   2024:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
                   2025:        
                   2026:        current_psp = psp_seg;
                   2027:        
                   2028:        if(al == 0x00) {
                   2029:                int_10h_feh_called = int_10h_ffh_called = false;
                   2030:                
                   2031:                // registers and segments
                   2032:                REG16(AX) = REG16(BX) = 0x00;
                   2033:                REG16(CX) = 0xff;
                   2034:                REG16(DX) = psp_seg;
                   2035:                REG16(SI) = ip;
                   2036:                REG16(DI) = sp;
                   2037:                REG16(SP) = sp;
1.1.1.3   root     2038:                SREG(DS) = SREG(ES) = psp_seg;
                   2039:                SREG(SS) = ss;
                   2040:                i386_load_segment_descriptor(DS);
                   2041:                i386_load_segment_descriptor(ES);
                   2042:                i386_load_segment_descriptor(SS);
1.1       root     2043:                
                   2044:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   2045:                i386_jmp_far(cs, ip);
                   2046:        } else if(al == 0x01) {
                   2047:                // copy ss:sp and cs:ip to param block
                   2048:                param->sp = sp;
                   2049:                param->ss = ss;
                   2050:                param->ip = ip;
                   2051:                param->cs = cs;
                   2052:        }
                   2053:        return(0);
                   2054: }
                   2055: 
                   2056: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   2057: {
                   2058:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   2059:        
                   2060:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   2061:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   2062:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   2063:        
1.1.1.3   root     2064:        SREG(SS) = psp->stack.w.h;
                   2065:        i386_load_segment_descriptor(SS);
1.1       root     2066:        REG16(SP) = psp->stack.w.l;
                   2067:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   2068:        
                   2069:        process_t *process = msdos_process_info_get(psp_seg);
                   2070:        int_10h_feh_called = process->parent_int_10h_feh_called;
                   2071:        int_10h_ffh_called = process->parent_int_10h_ffh_called;
                   2072:        
                   2073:        if(mem_free) {
1.1.1.8   root     2074:                int mcb_seg;
                   2075:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   2076:                        msdos_mem_free(mcb_seg + 1);
                   2077:                }
                   2078:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   2079:                        msdos_mem_free(mcb_seg + 1);
                   2080:                }
1.1       root     2081:                
                   2082:                for(int i = 0; i < MAX_FILES; i++) {
                   2083:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   2084:                                _close(i);
                   2085:                                msdos_file_handler_close(i, psp_seg);
                   2086:                        }
                   2087:                }
                   2088:                msdos_stdio_reopen();
                   2089:                
                   2090:                if(process->find_handle != INVALID_HANDLE_VALUE) {
                   2091:                        FindClose(process->find_handle);
                   2092:                        process->find_handle = INVALID_HANDLE_VALUE;
                   2093:                }
                   2094:        }
                   2095:        
                   2096:        memset(process, 0, sizeof(process_t));
                   2097:        
                   2098:        current_psp = psp->parent_psp;
                   2099:        retval = ret;
                   2100: }
                   2101: 
                   2102: // drive
                   2103: 
                   2104: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   2105: {
                   2106:        *seg = DPB_TOP >> 4;
                   2107:        *ofs = sizeof(dpb_t) * drive_num;
                   2108:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   2109:        
                   2110:        if(!force_update && dpb->free_clusters != 0) {
                   2111:                return(dpb->bytes_per_sector ? 1 : 0);
                   2112:        }
                   2113:        memset(dpb, 0, sizeof(dpb_t));
                   2114:        
                   2115:        int res = 0;
                   2116:        char dev[64];
                   2117:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   2118:        
                   2119:        HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   2120:        if(hFile != INVALID_HANDLE_VALUE) {
                   2121:                DISK_GEOMETRY geo;
                   2122:                DWORD dwSize;
                   2123:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   2124:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   2125:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   2126:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
                   2127:                        switch(geo.MediaType) {
                   2128:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   2129:                                dpb->media_type = 0xff;
                   2130:                                break;
                   2131:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   2132:                                dpb->media_type = 0xfe;
                   2133:                                break;
                   2134:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   2135:                                dpb->media_type = 0xfd;
                   2136:                                break;
                   2137:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   2138:                                dpb->media_type = 0xfc;
                   2139:                                break;
                   2140:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   2141:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   2142:                                dpb->media_type = 0xf9;
                   2143:                                break;
                   2144:                        case FixedMedia:        // hard disk
                   2145:                        case RemovableMedia:
                   2146:                                dpb->media_type = 0xf8;
                   2147:                                break;
                   2148:                        default:
                   2149:                                dpb->media_type = 0xf0;
                   2150:                                break;
                   2151:                        }
                   2152:                        res = 1;
                   2153:                }
                   2154:                dpb->drive_num = drive_num;
                   2155:                dpb->unit_num = drive_num;
                   2156:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   2157:                dpb->next_dpb_seg = *seg;
                   2158:                dpb->free_clusters = 0xffff;
                   2159:                CloseHandle(hFile);
                   2160:        }
                   2161:        return(res);
                   2162: }
                   2163: 
                   2164: // pc bios
                   2165: 
1.1.1.8   root     2166: int get_text_vram_address(int page)
1.1       root     2167: {
                   2168:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     2169:                return TEXT_VRAM_TOP;
1.1       root     2170:        } else {
1.1.1.8   root     2171:                return TEXT_VRAM_TOP + 0x1000 * (page & 7);
1.1       root     2172:        }
                   2173: }
                   2174: 
1.1.1.8   root     2175: int get_shadow_buffer_address(int page)
1.1       root     2176: {
1.1.1.8   root     2177:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
                   2178:                return SHADOW_BUF_TOP;
                   2179:        } else {
                   2180:                return SHADOW_BUF_TOP + 0x1000 * (page & 7);
                   2181:        }
                   2182: }
                   2183: 
                   2184: inline int get_shadow_buffer_address(int page, int x, int y)
                   2185: {
                   2186:        return get_shadow_buffer_address(page) + (x + y * 80) * 2;
1.1       root     2187: }
                   2188: 
                   2189: inline void pcbios_int_10h_00h()
                   2190: {
                   2191:        mem[0x449] = REG8(AL) & 0x7f;
1.1.1.8   root     2192:        text_vram_top_address = get_text_vram_address(mem[0x462]);
                   2193:        text_vram_end_address = text_vram_top_address + 4000;
                   2194:        shadow_buffer_top_address = get_shadow_buffer_address(mem[0x462]);
                   2195:        shadow_buffer_end_address = shadow_buffer_top_address + 4000;
1.1       root     2196:        
                   2197:        if(REG8(AL) & 0x80) {
                   2198:                mem[0x487] |= 0x80;
                   2199:        } else {
1.1.1.8   root     2200:                for(int y = 0, ofs = get_shadow_buffer_address(mem[0x462]); y < 25; y++) {
1.1       root     2201:                        for(int x = 0; x < 80; x++) {
                   2202:                                scr_buf[y][x].Char.AsciiChar = ' ';
                   2203:                                scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   2204:                                mem[ofs++] = 0x20;
                   2205:                                mem[ofs++] = 0x07;
                   2206:                        }
                   2207:                }
                   2208:                SMALL_RECT rect;
                   2209:                SET_RECT(rect, 0, 0, 79, 24);
                   2210:                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2211:                mem[0x487] &= ~0x80;
                   2212:        }
                   2213: }
                   2214: 
                   2215: inline void pcbios_int_10h_01h()
                   2216: {
                   2217:         mem[0x460] = REG8(CL);
                   2218:         mem[0x461] = REG8(CH);
                   2219: }
                   2220: 
                   2221: inline void pcbios_int_10h_02h()
                   2222: {
                   2223:        if(mem[0x462] == REG8(BH)) {
                   2224:                COORD co;
                   2225:                co.X = REG8(DL);
                   2226:                co.Y = REG8(DH);
                   2227:                SetConsoleCursorPosition(hStdout, co);
                   2228:        }
                   2229:        mem[0x450 + (REG8(BH) & 7) * 2] = REG8(DL);
                   2230:        mem[0x451 + (REG8(BH) & 7) * 2] = REG8(DH);
                   2231: }
                   2232: 
                   2233: inline void pcbios_int_10h_03h()
                   2234: {
                   2235:        REG8(DL) = mem[0x450 + (REG8(BH) & 7) * 2];
                   2236:        REG8(DH) = mem[0x451 + (REG8(BH) & 7) * 2];
                   2237:        REG8(CL) = mem[0x460];
                   2238:        REG8(CH) = mem[0x461];
                   2239: }
                   2240: 
                   2241: inline void pcbios_int_10h_05h()
                   2242: {
                   2243:        if(mem[0x462] != REG8(BH)) {
                   2244:                SMALL_RECT rect;
                   2245:                SET_RECT(rect, 0, 0, 79, 24);
                   2246:                ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2247:                
1.1.1.8   root     2248:                for(int y = 0, ofs = get_shadow_buffer_address(mem[0x462]); y < 25; y++) {
1.1       root     2249:                        for(int x = 0; x < 80; x++) {
                   2250:                                mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
                   2251:                                mem[ofs++] = scr_buf[y][x].Attributes;
                   2252:                        }
                   2253:                }
1.1.1.8   root     2254:                for(int y = 0, ofs = get_shadow_buffer_address(REG8(BH)); y < 25; y++) {
1.1       root     2255:                        for(int x = 0; x < 80; x++) {
                   2256:                                scr_buf[y][x].Char.AsciiChar = mem[ofs++];
                   2257:                                scr_buf[y][x].Attributes = mem[ofs++];
                   2258:                        }
                   2259:                }
                   2260:                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2261:                
                   2262:                COORD co;
                   2263:                co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2264:                co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2265:                SetConsoleCursorPosition(hStdout, co);
                   2266:        }
                   2267:        mem[0x462] = REG8(BH) & 7;
                   2268:        mem[0x44e] = 0;
                   2269:        mem[0x44f] = REG8(BH) << 4;
1.1.1.8   root     2270:        text_vram_top_address = get_text_vram_address(mem[0x462]);
                   2271:        text_vram_end_address = text_vram_top_address + 4000;
                   2272:        shadow_buffer_top_address = get_shadow_buffer_address(mem[0x462]);
                   2273:        shadow_buffer_end_address = shadow_buffer_top_address + 4000;
1.1       root     2274: }
                   2275: 
                   2276: inline void pcbios_int_10h_06h()
                   2277: {
                   2278:        SMALL_RECT rect;
                   2279:        SET_RECT(rect, 0, 0, 79, 24);
                   2280:        ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2281:        
                   2282:        if(REG8(AL) == 0) {
                   2283:                for(int y = REG8(CH); y <= REG8(DH); y++) {
1.1.1.8   root     2284:                        for(int x = REG8(CL), ofs = get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1.1       root     2285:                                scr_buf[y][x].Char.AsciiChar = ' ';
                   2286:                                scr_buf[y][x].Attributes = REG8(BH);
                   2287:                                mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
                   2288:                                mem[ofs++] = scr_buf[y][x].Attributes;
                   2289:                        }
                   2290:                }
                   2291:        } else {
                   2292:                ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2293:                for(int y = REG8(CH), y2 = REG8(CH) + REG8(AL); y <= REG8(DH); y++, y2++) {
1.1.1.8   root     2294:                        for(int x = REG8(CL), ofs = get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1.1       root     2295:                                if(y2 <= REG8(DH) && y2 >= 0 && y2 < SCR_BUF_SIZE) {
                   2296:                                        scr_buf[y][x] = scr_buf[y2][x];
                   2297:                                } else {
                   2298:                                        scr_buf[y][x].Char.AsciiChar = ' ';
                   2299:                                        scr_buf[y][x].Attributes = REG8(BH);
                   2300:                                }
                   2301:                                mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
                   2302:                                mem[ofs++] = scr_buf[y][x].Attributes;
                   2303:                        }
                   2304:                }
                   2305:        }
                   2306:        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2307: }
                   2308: 
                   2309: inline void pcbios_int_10h_07h()
                   2310: {
                   2311:        SMALL_RECT rect;
                   2312:        SET_RECT(rect, 0, 0, 79, 24);
                   2313:        ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2314:        
                   2315:        if(REG8(AL) == 0) {
                   2316:                for(int y = REG8(CH); y <= REG8(DH); y++) {
1.1.1.8   root     2317:                        for(int x = REG8(CL), ofs = get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1.1       root     2318:                                scr_buf[y][x].Char.AsciiChar = ' ';
                   2319:                                scr_buf[y][x].Attributes = REG8(BH);
                   2320:                                mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
                   2321:                                mem[ofs++] = scr_buf[y][x].Attributes;
                   2322:                        }
                   2323:                }
                   2324:        } else {
                   2325:                ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2326:                for(int y = REG8(DH), y2 = REG8(DH) - REG8(AL); y >= REG8(CH); y--, y2--) {
1.1.1.8   root     2327:                        for(int x = REG8(CL), ofs = get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1.1       root     2328:                                if(y2 >= REG8(CH) && y2 >= 0 && y2 < SCR_BUF_SIZE) {
                   2329:                                        scr_buf[y][x] = scr_buf[y2][x];
                   2330:                                } else {
                   2331:                                        scr_buf[y][x].Char.AsciiChar = ' ';
                   2332:                                        scr_buf[y][x].Attributes = REG8(BH);
                   2333:                                }
                   2334:                                mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
                   2335:                                mem[ofs++] = scr_buf[y][x].Attributes;
                   2336:                        }
                   2337:                }
                   2338:        }
                   2339:        WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   2340: }
                   2341: 
                   2342: inline void pcbios_int_10h_08h()
                   2343: {
                   2344:        COORD co;
                   2345:        DWORD num;
                   2346:        
                   2347:        co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2348:        co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2349:        
                   2350:        if(mem[0x462] == REG8(BH)) {
                   2351:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   2352:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   2353:                REG8(AL) = scr_char[0];
                   2354:                REG8(AH) = scr_attr[0];
                   2355:        } else {
1.1.1.8   root     2356:                REG16(AX) = *(UINT16 *)(mem + get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     2357:        }
                   2358: }
                   2359: 
                   2360: inline void pcbios_int_10h_09h()
                   2361: {
                   2362:        COORD co;
                   2363:        DWORD num;
                   2364:        
                   2365:        co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2366:        co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2367:        
                   2368:        if(mem[0x462] == REG8(BH)) {
                   2369:                for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
                   2370:                        scr_char[i] = REG8(AL);
                   2371:                        scr_attr[i] = REG8(BL);
                   2372:                }
                   2373:                WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   2374:                WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   2375:        } else {
1.1.1.8   root     2376:                for(int i = 0, dest = get_shadow_buffer_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
1.1       root     2377:                        mem[dest++] = REG8(AL);
                   2378:                        mem[dest++] = REG8(BL);
                   2379:                        if(++co.X == 80) {
                   2380:                                if(++co.Y == 25) {
                   2381:                                        break;
                   2382:                                }
                   2383:                                co.X = 0;
                   2384:                        }
                   2385:                }
                   2386:        }
                   2387: }
                   2388: 
                   2389: inline void pcbios_int_10h_0ah()
                   2390: {
                   2391:        COORD co;
                   2392:        DWORD num;
                   2393:        
                   2394:        co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2395:        co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2396:        
                   2397:        if(mem[0x462] == REG8(BH)) {
                   2398:                for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
                   2399:                        scr_char[i] = REG8(AL);
                   2400: //                     scr_attr[i] = REG8(BL);
                   2401:                }
                   2402:                WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   2403: //             WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   2404:        } else {
1.1.1.8   root     2405:                for(int i = 0, dest = get_shadow_buffer_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
1.1       root     2406:                        mem[dest++] = REG8(AL);
                   2407: //                     mem[dest++] = REG8(BL);
                   2408:                        dest++;
                   2409:                        if(++co.X == 80) {
                   2410:                                if(++co.Y == 25) {
                   2411:                                        break;
                   2412:                                }
                   2413:                                co.X = 0;
                   2414:                        }
                   2415:                }
                   2416:        }
                   2417: }
                   2418: 
                   2419: inline void pcbios_int_10h_0eh()
                   2420: {
                   2421:        msdos_putch(REG8(AL));
                   2422: }
                   2423: 
                   2424: inline void pcbios_int_10h_0fh()
                   2425: {
                   2426:        REG8(AL) = mem[0x449];
                   2427:        REG8(AH) = mem[0x44a];
                   2428:        REG8(BH) = mem[0x462];
                   2429: }
                   2430: 
                   2431: inline void pcbios_int_10h_13h()
                   2432: {
1.1.1.3   root     2433:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     2434:        COORD co;
                   2435:        DWORD num;
                   2436:        
                   2437:        co.X = REG8(DL);
                   2438:        co.Y = REG8(DH);
                   2439:        
                   2440:        switch(REG8(AL)) {
                   2441:        case 0x00:
                   2442:        case 0x01:
                   2443:                if(mem[0x462] == REG8(BH)) {
                   2444:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2445:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2446:                        SetConsoleCursorPosition(hStdout, co);
                   2447:                        
                   2448:                        if(csbi.wAttributes != REG8(BL)) {
                   2449:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   2450:                        }
                   2451:                        for(int i = 0; i < REG16(CX); i++) {
                   2452:                                msdos_putch(mem[ofs++]);
                   2453:                        }
                   2454:                        if(csbi.wAttributes != REG8(BL)) {
                   2455:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2456:                        }
                   2457:                        if(REG8(AL) == 0x00) {
                   2458:                                co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2459:                                co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2460:                                SetConsoleCursorPosition(hStdout, co);
                   2461:                        } else {
                   2462:                                cursor_moved = true;
                   2463:                        }
                   2464:                } else {
1.1.1.3   root     2465:                        m_CF = 1;
1.1       root     2466:                }
                   2467:                break;
                   2468:        case 0x02:
                   2469:        case 0x03:
                   2470:                if(mem[0x462] == REG8(BH)) {
                   2471:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2472:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2473:                        SetConsoleCursorPosition(hStdout, co);
                   2474:                        
                   2475:                        WORD wAttributes = csbi.wAttributes;
                   2476:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   2477:                                if(wAttributes != mem[ofs + 1]) {
                   2478:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   2479:                                        wAttributes = mem[ofs + 1];
                   2480:                                }
                   2481:                                msdos_putch(mem[ofs]);
                   2482:                        }
                   2483:                        if(csbi.wAttributes != wAttributes) {
                   2484:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2485:                        }
                   2486:                        if(REG8(AL) == 0x02) {
                   2487:                                co.X = mem[0x450 + (REG8(BH) & 7) * 2];
                   2488:                                co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
                   2489:                                SetConsoleCursorPosition(hStdout, co);
                   2490:                        } else {
                   2491:                                cursor_moved = true;
                   2492:                        }
                   2493:                } else {
1.1.1.3   root     2494:                        m_CF = 1;
1.1       root     2495:                }
                   2496:                break;
                   2497:        case 0x10:
                   2498:        case 0x11:
                   2499:                if(mem[0x462] == REG8(BH)) {
                   2500:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   2501:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   2502:                        for(int i = 0; i < num; i++) {
                   2503:                                mem[ofs++] = scr_char[i];
                   2504:                                mem[ofs++] = scr_attr[i];
                   2505:                                if(REG8(AL) == 0x11) {
                   2506:                                        mem[ofs++] = 0;
                   2507:                                        mem[ofs++] = 0;
                   2508:                                }
                   2509:                        }
                   2510:                } else {
1.1.1.8   root     2511:                        for(int i = 0, src = get_shadow_buffer_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
1.1       root     2512:                                mem[ofs++] = mem[src++];
                   2513:                                mem[ofs++] = mem[src++];
                   2514:                                if(REG8(AL) == 0x11) {
                   2515:                                        mem[ofs++] = 0;
                   2516:                                        mem[ofs++] = 0;
                   2517:                                }
                   2518:                                if(++co.X == 80) {
                   2519:                                        if(++co.Y == 25) {
                   2520:                                                break;
                   2521:                                        }
                   2522:                                        co.X = 0;
                   2523:                                }
                   2524:                        }
                   2525:                }
                   2526:                break;
                   2527:        case 0x20:
                   2528:        case 0x21:
                   2529:                if(mem[0x462] == REG8(BH)) {
                   2530:                        for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
                   2531:                                scr_char[i] = mem[ofs++];
                   2532:                                scr_attr[i] = mem[ofs++];
                   2533:                                if(REG8(AL) == 0x21) {
                   2534:                                        ofs += 2;
                   2535:                                }
                   2536:                        }
                   2537:                        WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   2538:                        WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   2539:                } else {
1.1.1.8   root     2540:                        for(int i = 0, dest = get_shadow_buffer_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
1.1       root     2541:                                mem[dest++] = mem[ofs++];
                   2542:                                mem[dest++] = mem[ofs++];
                   2543:                                if(REG8(AL) == 0x21) {
                   2544:                                        ofs += 2;
                   2545:                                }
                   2546:                                if(++co.X == 80) {
                   2547:                                        if(++co.Y == 25) {
                   2548:                                                break;
                   2549:                                        }
                   2550:                                        co.X = 0;
                   2551:                                }
                   2552:                        }
                   2553:                }
                   2554:                break;
                   2555:        default:
1.1.1.3   root     2556:                m_CF = 1;
1.1       root     2557:                break;
                   2558:        }
                   2559: }
                   2560: 
                   2561: inline void pcbios_int_10h_1dh()
                   2562: {
                   2563:        switch(REG8(AL)) {
                   2564:        case 0x01:
                   2565:                break;
                   2566:        case 0x02:
                   2567:                REG16(BX) = 0;
                   2568:                break;
                   2569:        default:
1.1.1.3   root     2570:                m_CF = 1;
1.1       root     2571:                break;
                   2572:        }
                   2573: }
                   2574: 
                   2575: inline void pcbios_int_10h_82h()
                   2576: {
                   2577:        static UINT8 mode = 0;
                   2578:        
                   2579:        switch(REG8(AL)) {
                   2580:        case 0:
                   2581:                if(REG8(BL) != 0xff) {
                   2582:                        mode = REG8(BL);
                   2583:                }
                   2584:                REG8(AL) = mode;
                   2585:                break;
                   2586:        default:
1.1.1.3   root     2587:                m_CF = 1;
1.1       root     2588:                break;
                   2589:        }
                   2590: }
                   2591: 
                   2592: inline void pcbios_int_10h_feh()
                   2593: {
                   2594:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     2595:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     2596:                i386_load_segment_descriptor(ES);
1.1.1.8   root     2597:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     2598:        }
                   2599:        int_10h_feh_called = true;
                   2600: }
                   2601: 
                   2602: inline void pcbios_int_10h_ffh()
                   2603: {
                   2604:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
                   2605:                COORD co;
                   2606:                DWORD num;
                   2607:                
                   2608:                co.X = (REG16(DI) >> 1) % 80;
                   2609:                co.Y = (REG16(DI) >> 1) / 80;
1.1.1.8   root     2610:                for(int i = 0, ofs = get_shadow_buffer_address(0, co.X, co.Y); i < REG16(CX) && i < 80 * 25; i++) {
1.1       root     2611:                        scr_char[i] = mem[ofs++];
                   2612:                        scr_attr[i] = mem[ofs++];
                   2613:                }
                   2614:                WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   2615:                WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   2616:        }
                   2617:        int_10h_ffh_called = true;
                   2618: }
                   2619: 
                   2620: inline void pcbios_int_15h_23h()
                   2621: {
                   2622:        switch(REG8(AL)) {
                   2623:        case 0:
1.1.1.8   root     2624:                REG8(CL) = cmos_read(0x2d);
                   2625:                REG8(CH) = cmos_read(0x2e);
1.1       root     2626:                break;
                   2627:        case 1:
1.1.1.8   root     2628:                cmos_write(0x2d, REG8(CL));
                   2629:                cmos_write(0x2e, REG8(CH));
1.1       root     2630:                break;
                   2631:        default:
                   2632:                REG8(AH) = 0x86;
1.1.1.3   root     2633:                m_CF = 1;
1.1       root     2634:                break;
                   2635:        }
                   2636: }
                   2637: 
                   2638: inline void pcbios_int_15h_24h()
                   2639: {
                   2640:        switch(REG8(AL)) {
                   2641:        case 0:
1.1.1.3   root     2642:                i386_set_a20_line(0);
1.1       root     2643:                REG8(AH) = 0;
                   2644:                break;
                   2645:        case 1:
1.1.1.3   root     2646:                i386_set_a20_line(1);
1.1       root     2647:                REG8(AH) = 0;
                   2648:                break;
                   2649:        case 2:
                   2650:                REG8(AH) = 0;
1.1.1.3   root     2651:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     2652:                REG16(CX) = 0;
                   2653:                break;
                   2654:        case 3:
                   2655:                REG16(AX) = 0;
                   2656:                REG16(BX) = 0;
                   2657:                break;
                   2658:        }
                   2659: }
                   2660: 
                   2661: inline void pcbios_int_15h_49h()
                   2662: {
                   2663:        REG8(AL) = 0;
                   2664:        REG8(BL) = 0;   // DOS/V;
                   2665: }
                   2666: 
                   2667: inline void pcbios_int_15h_86h()
                   2668: {
                   2669:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
                   2670:        Sleep(usec / 1000);
                   2671: }
                   2672: 
                   2673: inline void pcbios_int_15h_87h()
                   2674: {
                   2675:        // copy extended memory (from DOSBox)
                   2676:        int len = REG16(CX) * 2;
1.1.1.3   root     2677:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     2678:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   2679:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   2680:        memcpy(mem + dst, mem + src, len);
                   2681:        REG16(AX) = 0x00;
                   2682: }
                   2683: 
                   2684: inline void pcbios_int_15h_88h()
                   2685: {
                   2686:        REG16(AX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   2687: }
                   2688: 
                   2689: inline void pcbios_int_15h_89h()
                   2690: {
1.1.1.3   root     2691: #if defined(HAS_I386) || defined(HAS_I286)
1.1       root     2692:        // switch to protected mode (from DOSBox)
                   2693:        write_io_byte(0x20, 0x10);
                   2694:        write_io_byte(0x21, REG8(BH));
                   2695:        write_io_byte(0x21, 0x00);
                   2696:        write_io_byte(0xa0, 0x10);
                   2697:        write_io_byte(0xa1, REG8(BL));
                   2698:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     2699:        i386_set_a20_line(1);
                   2700:        int ofs = SREG_BASE(ES) + REG16(SI);
                   2701:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   2702:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   2703:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   2704:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   2705: #if defined(HAS_I386)
                   2706:        m_cr[0] |= 1;
                   2707: #else
                   2708:        m_msw |= 1;
                   2709: #endif
                   2710:        SREG(DS) = 0x18;
                   2711:        SREG(ES) = 0x20;
                   2712:        SREG(SS) = 0x28;
                   2713:        i386_load_segment_descriptor(DS);
                   2714:        i386_load_segment_descriptor(ES);
                   2715:        i386_load_segment_descriptor(SS);
1.1       root     2716:        REG16(SP) += 6;
1.1.1.3   root     2717: #if defined(HAS_I386)
                   2718:        set_flags(0);   // ???
                   2719: #else
                   2720:        m_flags = 2;
                   2721:        ExpandFlags(m_flags);
                   2722: #endif
1.1       root     2723:        REG16(AX) = 0x00;
                   2724:        i386_jmp_far(0x30, REG16(CX));
                   2725: #else
                   2726:        REG8(AH) = 0x86;
1.1.1.3   root     2727:        m_CF = 1;
1.1       root     2728: #endif
                   2729: }
                   2730: 
1.1.1.3   root     2731: #if defined(HAS_I386)
1.1       root     2732: inline void pcbios_int_15h_c9h()
                   2733: {
                   2734:        REG8(AH) = 0x00;
                   2735:        REG8(CH) = cpu_type;
                   2736:        REG8(CL) = cpu_step;
                   2737: }
1.1.1.3   root     2738: #endif
1.1       root     2739: 
                   2740: inline void pcbios_int_15h_cah()
                   2741: {
                   2742:        switch(REG8(AL)) {
                   2743:        case 0:
                   2744:                if(REG8(BL) > 0x3f) {
                   2745:                        REG8(AH) = 0x03;
1.1.1.3   root     2746:                        m_CF = 1;
1.1       root     2747:                } else if(REG8(BL) < 0x0e) {
                   2748:                        REG8(AH) = 0x04;
1.1.1.3   root     2749:                        m_CF = 1;
1.1       root     2750:                } else {
1.1.1.8   root     2751:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     2752:                }
                   2753:                break;
                   2754:        case 1:
                   2755:                if(REG8(BL) > 0x3f) {
                   2756:                        REG8(AH) = 0x03;
1.1.1.3   root     2757:                        m_CF = 1;
1.1       root     2758:                } else if(REG8(BL) < 0x0e) {
                   2759:                        REG8(AH) = 0x04;
1.1.1.3   root     2760:                        m_CF = 1;
1.1       root     2761:                } else {
1.1.1.8   root     2762:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     2763:                }
                   2764:                break;
                   2765:        default:
                   2766:                REG8(AH) = 0x86;
1.1.1.3   root     2767:                m_CF = 1;
1.1       root     2768:                break;
                   2769:        }
                   2770: }
                   2771: 
                   2772: UINT32 get_key_code(bool clear_buffer)
                   2773: {
                   2774:        UINT32 code = 0;
                   2775:        
                   2776:        if(key_buf_char->count() == 0) {
                   2777:                update_key_buffer();
                   2778:        }
                   2779:        if(!clear_buffer) {
                   2780:                key_buf_char->store_buffer();
                   2781:                key_buf_scan->store_buffer();
                   2782:        }
                   2783:        if(key_buf_char->count() != 0) {
                   2784:                code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   2785:        }
                   2786:        if(key_buf_char->count() != 0) {
                   2787:                code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   2788:        }
                   2789:        if(!clear_buffer) {
                   2790:                key_buf_char->restore_buffer();
                   2791:                key_buf_scan->restore_buffer();
                   2792:        }
                   2793:        return code;
                   2794: }
                   2795: 
                   2796: inline void pcbios_int_16h_00h()
                   2797: {
                   2798:        while(key_code == 0) {
                   2799:                key_code = get_key_code(true);
                   2800:        }
                   2801:        if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   2802:                if(REG8(AH) == 0x10) {
                   2803:                        key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   2804:                } else {
                   2805:                        key_code = ((key_code >> 16) & 0xff00);
                   2806:                }
                   2807:        }
                   2808:        REG16(AX) = key_code & 0xffff;
                   2809:        key_code >>= 16;
                   2810: }
                   2811: 
                   2812: inline void pcbios_int_16h_01h()
                   2813: {
1.1.1.5   root     2814:        static UINT32 key_code_prev = 0;
                   2815:        UINT32 key_code_tmp = key_code;
1.1       root     2816:        
1.1.1.5   root     2817:        if(key_code_tmp == 0) {
                   2818:                key_code_tmp = get_key_code(false);
                   2819:        }
                   2820:        if(key_code_prev == key_code_tmp && (key_code_tmp & 0xffffff) == 0x00e000 && (key_code_tmp & 0xff000000) != 0) {
                   2821:                key_code_prev = key_code_tmp = 0;
                   2822:        } else {
                   2823:                key_code_prev = key_code_tmp;
                   2824:        }
                   2825:        if(key_code_tmp != 0) {
                   2826:                if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
1.1       root     2827:                        if(REG8(AH) == 0x11) {
1.1.1.5   root     2828:                                key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
1.1       root     2829:                        } else {
1.1.1.5   root     2830:                                key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1       root     2831:                        }
                   2832:                }
                   2833:        }
1.1.1.5   root     2834:        if(key_code_tmp != 0) {
                   2835:                REG16(AX) = key_code_tmp & 0xffff;
1.1       root     2836:        }
1.1.1.3   root     2837: #if defined(HAS_I386)
1.1.1.5   root     2838:        m_ZF = (key_code_tmp == 0);
1.1.1.3   root     2839: #else
1.1.1.5   root     2840:        m_ZeroVal = (key_code_tmp != 0);
1.1.1.3   root     2841: #endif
1.1       root     2842: }
                   2843: 
                   2844: inline void pcbios_int_16h_02h()
                   2845: {
                   2846:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   2847:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   2848:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   2849:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   2850:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   2851:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   2852:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   2853:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   2854: }
                   2855: 
                   2856: inline void pcbios_int_16h_03h()
                   2857: {
                   2858:        static UINT16 status = 0;
                   2859:        
                   2860:        switch(REG8(AL)) {
                   2861:        case 0x05:
                   2862:                status = REG16(BX);
                   2863:                break;
                   2864:        case 0x06:
                   2865:                REG16(BX) = status;
                   2866:                break;
                   2867:        default:
1.1.1.3   root     2868:                m_CF = 1;
1.1       root     2869:                break;
                   2870:        }
                   2871: }
                   2872: 
                   2873: inline void pcbios_int_16h_05h()
                   2874: {
                   2875:        _ungetch(REG8(CL));
                   2876:        REG8(AL) = 0x00;
                   2877: }
                   2878: 
                   2879: inline void pcbios_int_16h_12h()
                   2880: {
                   2881:        pcbios_int_16h_02h();
                   2882:        
                   2883:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   2884:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   2885:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   2886:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   2887:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   2888:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   2889:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   2890:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   2891: }
                   2892: 
                   2893: inline void pcbios_int_16h_13h()
                   2894: {
                   2895:        static UINT16 status = 0;
                   2896:        
                   2897:        switch(REG8(AL)) {
                   2898:        case 0x00:
                   2899:                status = REG16(DX);
                   2900:                break;
                   2901:        case 0x01:
                   2902:                REG16(DX) = status;
                   2903:                break;
                   2904:        default:
1.1.1.3   root     2905:                m_CF = 1;
1.1       root     2906:                break;
                   2907:        }
                   2908: }
                   2909: 
                   2910: inline void pcbios_int_16h_14h()
                   2911: {
                   2912:        static UINT8 status = 0;
                   2913:        
                   2914:        switch(REG8(AL)) {
                   2915:        case 0x00:
                   2916:        case 0x01:
                   2917:                status = REG8(AL);
                   2918:                break;
                   2919:        case 0x02:
                   2920:                REG8(AL) = status;
                   2921:                break;
                   2922:        default:
1.1.1.3   root     2923:                m_CF = 1;
1.1       root     2924:                break;
                   2925:        }
                   2926: }
                   2927: 
                   2928: inline void pcbios_int_1ah_00h()
                   2929: {
                   2930:        static WORD prev_day = 0;
                   2931:        SYSTEMTIME time;
                   2932:        
                   2933:        GetLocalTime(&time);
                   2934:        unsigned __int64 msec = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   2935:        unsigned __int64 tick = msec * 0x1800b0 / 24 / 60 / 60 / 1000;
                   2936:        REG16(CX) = (tick >> 16) & 0xffff;
                   2937:        REG16(DX) = (tick      ) & 0xffff;
                   2938:        REG8(AL) = (prev_day != 0 && prev_day != time.wDay) ? 1 : 0;
                   2939:        prev_day = time.wDay;
                   2940: }
                   2941: 
                   2942: inline int to_bcd(int t)
                   2943: {
                   2944:        int u = (t % 100) / 10;
                   2945:        return (u << 4) | (t % 10);
                   2946: }
                   2947: 
                   2948: inline void pcbios_int_1ah_02h()
                   2949: {
                   2950:        SYSTEMTIME time;
                   2951:        
                   2952:        GetLocalTime(&time);
                   2953:        REG8(CH) = to_bcd(time.wHour);
                   2954:        REG8(CL) = to_bcd(time.wMinute);
                   2955:        REG8(DH) = to_bcd(time.wSecond);
                   2956:        REG8(DL) = 0x00;
                   2957: }
                   2958: 
                   2959: inline void pcbios_int_1ah_04h()
                   2960: {
                   2961:        SYSTEMTIME time;
                   2962:        
                   2963:        GetLocalTime(&time);
                   2964:        REG8(CH) = to_bcd(time.wYear / 100);
                   2965:        REG8(CL) = to_bcd(time.wYear);
                   2966:        REG8(DH) = to_bcd(time.wMonth);
                   2967:        REG8(DL) = to_bcd(time.wDay);
                   2968: }
                   2969: 
                   2970: inline void pcbios_int_1ah_0ah()
                   2971: {
                   2972:        SYSTEMTIME time;
                   2973:        FILETIME file_time;
                   2974:        WORD dos_date, dos_time;
                   2975:        
                   2976:        GetLocalTime(&time);
                   2977:        SystemTimeToFileTime(&time, &file_time);
                   2978:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   2979:        REG16(CX) = dos_date;
                   2980: }
                   2981: 
                   2982: // msdos system call
                   2983: 
                   2984: inline void msdos_int_21h_00h()
                   2985: {
1.1.1.3   root     2986:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     2987: }
                   2988: 
                   2989: inline void msdos_int_21h_01h()
                   2990: {
                   2991:        REG8(AL) = msdos_getche();
1.1.1.8   root     2992:        // some seconds may be passed in console
1.1       root     2993:        hardware_update();
                   2994: }
                   2995: 
                   2996: inline void msdos_int_21h_02h()
                   2997: {
                   2998:        msdos_putch(REG8(DL));
                   2999: }
                   3000: 
                   3001: inline void msdos_int_21h_03h()
                   3002: {
                   3003:        REG8(AL) = msdos_aux_in();
                   3004: }
                   3005: 
                   3006: inline void msdos_int_21h_04h()
                   3007: {
                   3008:        msdos_aux_out(REG8(DL));
                   3009: }
                   3010: 
                   3011: inline void msdos_int_21h_05h()
                   3012: {
                   3013:        msdos_prn_out(REG8(DL));
                   3014: }
                   3015: 
                   3016: inline void msdos_int_21h_06h()
                   3017: {
                   3018:        if(REG8(DL) == 0xff) {
                   3019:                if(msdos_kbhit()) {
                   3020:                        REG8(AL) = msdos_getch();
1.1.1.3   root     3021: #if defined(HAS_I386)
                   3022:                        m_ZF = 0;
                   3023: #else
                   3024:                        m_ZeroVal = 1;
                   3025: #endif
1.1       root     3026:                } else {
                   3027:                        REG8(AL) = 0;
1.1.1.3   root     3028: #if defined(HAS_I386)
                   3029:                        m_ZF = 1;
                   3030: #else
                   3031:                        m_ZeroVal = 0;
                   3032: #endif
1.1       root     3033:                        Sleep(10);
                   3034:                }
                   3035:        } else {
                   3036:                msdos_putch(REG8(DL));
                   3037:        }
                   3038: }
                   3039: 
                   3040: inline void msdos_int_21h_07h()
                   3041: {
                   3042:        REG8(AL) = msdos_getch();
1.1.1.8   root     3043:        // some seconds may be passed in console
1.1       root     3044:        hardware_update();
                   3045: }
                   3046: 
                   3047: inline void msdos_int_21h_08h()
                   3048: {
                   3049:        REG8(AL) = msdos_getch();
1.1.1.8   root     3050:        // some seconds may be passed in console
1.1       root     3051:        hardware_update();
                   3052: }
                   3053: 
                   3054: inline void msdos_int_21h_09h()
                   3055: {
                   3056:        char tmp[0x10000];
1.1.1.3   root     3057:        memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), sizeof(tmp));
1.1       root     3058:        tmp[sizeof(tmp) - 1] = '\0';
                   3059:        int len = strlen(my_strtok(tmp, "$"));
                   3060:        
                   3061:        if(file_handler[1].valid && !file_handler[1].atty) {
                   3062:                // stdout is redirected to file
                   3063:                msdos_write(1, tmp, len);
                   3064:        } else {
                   3065:                for(int i = 0; i < len; i++) {
                   3066:                        msdos_putch(tmp[i]);
                   3067:                }
                   3068:        }
                   3069: }
                   3070: 
                   3071: inline void msdos_int_21h_0ah()
                   3072: {
1.1.1.3   root     3073:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     3074:        int max = mem[ofs] - 1;
                   3075:        UINT8 *buf = mem + ofs + 2;
                   3076:        int chr, p = 0;
                   3077:        
                   3078:        while((chr = msdos_getch()) != 0x0d) {
                   3079:                if(chr == 0x00) {
                   3080:                        // skip 2nd byte
                   3081:                        msdos_getch();
                   3082:                } else if(chr == 0x08) {
                   3083:                        // back space
                   3084:                        if(p > 0) {
                   3085:                                p--;
                   3086:                                msdos_putch(chr);
                   3087:                                msdos_putch(' ');
                   3088:                                msdos_putch(chr);
                   3089:                        }
                   3090:                } else if(p < max) {
                   3091:                        buf[p++] = chr;
                   3092:                        msdos_putch(chr);
                   3093:                }
                   3094:        }
                   3095:        buf[p] = 0x0d;
                   3096:        mem[ofs + 1] = p;
1.1.1.8   root     3097:        // some seconds may be passed in console
1.1       root     3098:        hardware_update();
                   3099: }
                   3100: 
                   3101: inline void msdos_int_21h_0bh()
                   3102: {
                   3103:        if(msdos_kbhit()) {
                   3104:                REG8(AL) = 0xff;
                   3105:        } else {
                   3106:                REG8(AL) = 0x00;
                   3107:                Sleep(10);
                   3108:        }
                   3109: }
                   3110: 
                   3111: inline void msdos_int_21h_0ch()
                   3112: {
                   3113:        // clear key buffer
                   3114:        if(file_handler[0].valid && !file_handler[0].atty) {
                   3115:                // stdin is redirected to file
                   3116:        } else {
                   3117:                while(msdos_kbhit()) {
                   3118:                        msdos_getch();
                   3119:                }
                   3120:        }
                   3121:        
                   3122:        switch(REG8(AL)) {
                   3123:        case 0x01:
                   3124:                msdos_int_21h_01h();
                   3125:                break;
                   3126:        case 0x06:
                   3127:                msdos_int_21h_06h();
                   3128:                break;
                   3129:        case 0x07:
                   3130:                msdos_int_21h_07h();
                   3131:                break;
                   3132:        case 0x08:
                   3133:                msdos_int_21h_08h();
                   3134:                break;
                   3135:        case 0x0a:
                   3136:                msdos_int_21h_0ah();
                   3137:                break;
                   3138:        default:
                   3139:                REG16(AX) = 0x01;
1.1.1.3   root     3140:                m_CF = 1;
1.1       root     3141:                break;
                   3142:        }
                   3143: }
                   3144: 
                   3145: inline void msdos_int_21h_0dh()
                   3146: {
                   3147: }
                   3148: 
                   3149: inline void msdos_int_21h_0eh()
                   3150: {
                   3151:        if(REG8(DL) < 26) {
                   3152:                _chdrive(REG8(DL) + 1);
                   3153:                msdos_cds_update(REG8(DL));
                   3154:        }
                   3155:        REG8(AL) = 26; // zdrive
                   3156: }
                   3157: 
                   3158: inline void msdos_int_21h_11h()
                   3159: {
1.1.1.3   root     3160:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   3161:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     3162:        
                   3163:        process_t *process = msdos_process_info_get(current_psp);
                   3164:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
                   3165:        find_fcb_t *find = (find_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l + (ext_fcb->flag == 0xff ? 7 : 0));
                   3166:        char *path = msdos_fcb_path(fcb);
                   3167:        WIN32_FIND_DATA fd;
                   3168:        
                   3169:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   3170:                FindClose(process->find_handle);
                   3171:                process->find_handle = INVALID_HANDLE_VALUE;
                   3172:        }
                   3173:        strcpy(process->volume_label, msdos_volume_label(path));
                   3174:        process->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   3175:        
                   3176:        if((process->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   3177:                process->allowable_mask &= ~8;
                   3178:        }
                   3179:        if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   3180:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
                   3181:                        if(!FindNextFile(process->find_handle, &fd)) {
                   3182:                                FindClose(process->find_handle);
                   3183:                                process->find_handle = INVALID_HANDLE_VALUE;
                   3184:                                break;
                   3185:                        }
                   3186:                }
                   3187:        }
                   3188:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   3189:                if(ext_fcb->flag == 0xff) {
                   3190:                        ext_find->flag = 0xff;
                   3191:                        memset(ext_find->reserved, 0, 5);
                   3192:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   3193:                }
                   3194:                find->drive = _getdrive();
                   3195:                msdos_set_fcb_path((fcb_t *)find, msdos_short_path(fd.cFileName));
                   3196:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   3197:                find->nt_res = 0;
                   3198:                msdos_find_file_conv_local_time(&fd);
                   3199:                find->create_time_ms = 0;
                   3200:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   3201:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   3202:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   3203:                find->cluster_hi = find->cluster_lo = 0;
                   3204:                find->file_size = fd.nFileSizeLow;
                   3205:                REG8(AL) = 0x00;
                   3206:        } else if(process->allowable_mask & 8) {
                   3207:                if(ext_fcb->flag == 0xff) {
                   3208:                        ext_find->flag = 0xff;
                   3209:                        memset(ext_find->reserved, 0, 5);
                   3210:                        ext_find->attribute = 8;
                   3211:                }
                   3212:                find->drive = _getdrive();
                   3213:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   3214:                find->attribute = 8;
                   3215:                find->nt_res = 0;
                   3216:                msdos_find_file_conv_local_time(&fd);
                   3217:                find->create_time_ms = 0;
                   3218:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   3219:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   3220:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   3221:                find->cluster_hi = find->cluster_lo = 0;
                   3222:                find->file_size = 0;
                   3223:                process->allowable_mask &= ~8;
                   3224:                REG8(AL) = 0x00;
                   3225:        } else {
                   3226:                REG8(AL) = 0xff;
                   3227:        }
                   3228: }
                   3229: 
                   3230: inline void msdos_int_21h_12h()
                   3231: {
1.1.1.3   root     3232:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   3233:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     3234:        
                   3235:        process_t *process = msdos_process_info_get(current_psp);
                   3236:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
                   3237:        find_fcb_t *find = (find_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l + (ext_fcb->flag == 0xff ? 7 : 0));
                   3238:        WIN32_FIND_DATA fd;
                   3239:        
                   3240:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   3241:                if(FindNextFile(process->find_handle, &fd)) {
                   3242:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
                   3243:                                if(!FindNextFile(process->find_handle, &fd)) {
                   3244:                                        FindClose(process->find_handle);
                   3245:                                        process->find_handle = INVALID_HANDLE_VALUE;
                   3246:                                        break;
                   3247:                                }
                   3248:                        }
                   3249:                } else {
                   3250:                        FindClose(process->find_handle);
                   3251:                        process->find_handle = INVALID_HANDLE_VALUE;
                   3252:                }
                   3253:        }
                   3254:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   3255:                if(ext_fcb->flag == 0xff) {
                   3256:                        ext_find->flag = 0xff;
                   3257:                        memset(ext_find->reserved, 0, 5);
                   3258:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   3259:                }
                   3260:                find->drive = _getdrive();
                   3261:                msdos_set_fcb_path((fcb_t *)find, msdos_short_path(fd.cFileName));
                   3262:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   3263:                find->nt_res = 0;
                   3264:                msdos_find_file_conv_local_time(&fd);
                   3265:                find->create_time_ms = 0;
                   3266:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   3267:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   3268:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   3269:                find->cluster_hi = find->cluster_lo = 0;
                   3270:                find->file_size = fd.nFileSizeLow;
                   3271:                REG8(AL) = 0x00;
                   3272:        } else if(process->allowable_mask & 8) {
                   3273:                if(ext_fcb->flag == 0xff) {
                   3274:                        ext_find->flag = 0xff;
                   3275:                        memset(ext_find->reserved, 0, 5);
                   3276:                        ext_find->attribute = 8;
                   3277:                }
                   3278:                find->drive = _getdrive();
                   3279:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   3280:                find->attribute = 8;
                   3281:                find->nt_res = 0;
                   3282:                msdos_find_file_conv_local_time(&fd);
                   3283:                find->create_time_ms = 0;
                   3284:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   3285:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   3286:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   3287:                find->cluster_hi = find->cluster_lo = 0;
                   3288:                find->file_size = 0;
                   3289:                process->allowable_mask &= ~8;
                   3290:                REG8(AL) = 0x00;
                   3291:        } else {
                   3292:                REG8(AL) = 0xff;
                   3293:        }
                   3294: }
                   3295: 
                   3296: inline void msdos_int_21h_13h()
                   3297: {
1.1.1.3   root     3298:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     3299:                REG8(AL) = 0xff;
                   3300:        } else {
                   3301:                REG8(AL) = 0x00;
                   3302:        }
                   3303: }
                   3304: 
                   3305: inline void msdos_int_21h_18h()
                   3306: {
                   3307:        REG8(AL) = 0x00;
                   3308: }
                   3309: 
                   3310: inline void msdos_int_21h_19h()
                   3311: {
                   3312:        REG8(AL) = _getdrive() - 1;
                   3313: }
                   3314: 
                   3315: inline void msdos_int_21h_1ah()
                   3316: {
                   3317:        process_t *process = msdos_process_info_get(current_psp);
                   3318:        
                   3319:        process->dta.w.l = REG16(DX);
1.1.1.3   root     3320:        process->dta.w.h = SREG(DS);
1.1       root     3321: }
                   3322: 
                   3323: inline void msdos_int_21h_1bh()
                   3324: {
                   3325:        int drive_num = _getdrive() - 1;
                   3326:        UINT16 seg, ofs;
                   3327:        
                   3328:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   3329:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   3330:                REG8(AL) = dpb->highest_sector_num + 1;
                   3331:                REG16(CX) = dpb->bytes_per_sector;
                   3332:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     3333:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     3334:        } else {
                   3335:                REG8(AL) = 0xff;
1.1.1.3   root     3336:                m_CF = 1;
1.1       root     3337:        }
                   3338: 
                   3339: }
                   3340: 
                   3341: inline void msdos_int_21h_1ch()
                   3342: {
                   3343:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   3344:        UINT16 seg, ofs;
                   3345:        
                   3346:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   3347:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   3348:                REG8(AL) = dpb->highest_sector_num + 1;
                   3349:                REG16(CX) = dpb->bytes_per_sector;
                   3350:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     3351:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     3352:        } else {
                   3353:                REG8(AL) = 0xff;
1.1.1.3   root     3354:                m_CF = 1;
1.1       root     3355:        }
                   3356: 
                   3357: }
                   3358: 
                   3359: inline void msdos_int_21h_1dh()
                   3360: {
                   3361:        REG8(AL) = 0;
                   3362: }
                   3363: 
                   3364: inline void msdos_int_21h_1eh()
                   3365: {
                   3366:        REG8(AL) = 0;
                   3367: }
                   3368: 
                   3369: inline void msdos_int_21h_1fh()
                   3370: {
                   3371:        int drive_num = _getdrive() - 1;
                   3372:        UINT16 seg, ofs;
                   3373:        
                   3374:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   3375:                REG8(AL) = 0;
1.1.1.3   root     3376:                SREG(DS) = seg;
                   3377:                i386_load_segment_descriptor(DS);
1.1       root     3378:                REG16(BX) = ofs;
                   3379:        } else {
                   3380:                REG8(AL) = 0xff;
1.1.1.3   root     3381:                m_CF = 1;
1.1       root     3382:        }
                   3383: }
                   3384: 
                   3385: inline void msdos_int_21h_20h()
                   3386: {
                   3387:        REG8(AL) = 0;
                   3388: }
                   3389: 
                   3390: inline void msdos_int_21h_25h()
                   3391: {
                   3392:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     3393:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     3394: }
                   3395: 
                   3396: inline void msdos_int_21h_26h()
                   3397: {
                   3398:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   3399:        
                   3400:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   3401:        psp->first_mcb = REG16(DX) + 16;
                   3402:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   3403:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   3404:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   3405:        psp->parent_psp = 0;
                   3406: }
                   3407: 
                   3408: inline void msdos_int_21h_29h()
                   3409: {
1.1.1.3   root     3410:        int ofs = SREG_BASE(DS) + REG16(SI);
1.1       root     3411:        char name[MAX_PATH], ext[MAX_PATH];
                   3412:        UINT8 drv = 0;
                   3413:        char sep_chars[] = ":.;,=+";
                   3414:        char end_chars[] = "\\<>|/\"[]";
                   3415:        char spc_chars[] = " \t";
                   3416:        
                   3417:        if(REG8(AL) & 1) {
                   3418:                ofs += strspn((char *)&mem[ofs], spc_chars);
                   3419:                if(my_strchr(sep_chars, mem[ofs]) && mem[ofs] != '\0') {
                   3420:                        ofs++;
                   3421:                }
                   3422:        }
                   3423:        ofs += strspn((char *)&mem[ofs], spc_chars);
                   3424:        
                   3425:        if(mem[ofs + 1] == ':') {
                   3426:                UINT8 c = mem[ofs];
                   3427:                if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
                   3428:                        ; // invalid drive letter
                   3429:                } else {
                   3430:                        if(c >= 'a' && c <= 'z') {
                   3431:                                drv = c - 'a' + 1;
                   3432:                        } else {
                   3433:                                drv = c - 'A' + 1;
                   3434:                        }
                   3435:                        ofs += 2;
                   3436:                }
                   3437:        }
                   3438:        memset(name, 0x20, sizeof(name));
                   3439:        memset(ext, 0x20, sizeof(ext));
                   3440:        for(int i = 0; i < MAX_PATH; i++) {
                   3441:                UINT8 c = mem[ofs];
                   3442:                if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
                   3443:                        break;
                   3444:                } else if(c >= 'a' && c <= 'z') {
                   3445:                        c -= 0x20;
                   3446:                }
                   3447:                ofs++;
                   3448:                name[i] = c;
                   3449:        }
                   3450:        if(mem[ofs] == '.') {
                   3451:                ofs++;
                   3452:                for(int i = 0; i < MAX_PATH; i++) {
                   3453:                        UINT8 c = mem[ofs];
                   3454:                        if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
                   3455:                                break;
                   3456:                        } else if(c >= 'a' && c <= 'z') {
                   3457:                                c -= 0x20;
                   3458:                        }
                   3459:                        ofs++;
                   3460:                        ext[i] = c;
                   3461:                }
                   3462:        }
1.1.1.3   root     3463:        int si = ofs - SREG_BASE(DS);
                   3464:        int ds = SREG(DS);
1.1       root     3465:        while(si > 0xffff) {
                   3466:                si -= 0x10;
                   3467:                ds++;
                   3468:        }
                   3469:        REG16(SI) = si;
1.1.1.3   root     3470:        SREG(DS) = ds;
                   3471:        i386_load_segment_descriptor(DS);
1.1       root     3472:        
1.1.1.3   root     3473:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1       root     3474:        fcb[0] = drv;
                   3475:        memcpy(fcb + 1, name, 8);
                   3476:        int found_star = 0;
                   3477:        for(int i = 1; i < 1 + 8; i++) {
                   3478:                if(fcb[i] == '*') {
                   3479:                        found_star = 1;
                   3480:                }
                   3481:                if(found_star) {
                   3482:                        fcb[i] = '?';
                   3483:                }
                   3484:        }
                   3485:        memcpy(fcb + 9, ext, 3);
                   3486:        found_star = 0;
                   3487:        for(int i = 9; i < 9 + 3; i++) {
                   3488:                if(fcb[i] == '*') {
                   3489:                        found_star = 1;
                   3490:                }
                   3491:                if(found_star) {
                   3492:                        fcb[i] = '?';
                   3493:                }
                   3494:        }
                   3495:        
                   3496:        REG8(AL) = 0x00;
                   3497:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   3498:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   3499:                        REG8(AL) = 0x01;
                   3500:                }
                   3501:        } else {
                   3502:                REG8(AL) = 0xff;
                   3503:        }
                   3504: }
                   3505: 
                   3506: inline void msdos_int_21h_2ah()
                   3507: {
                   3508:        SYSTEMTIME sTime;
                   3509:        
                   3510:        GetLocalTime(&sTime);
                   3511:        REG16(CX) = sTime.wYear;
                   3512:        REG8(DH) = (UINT8)sTime.wMonth;
                   3513:        REG8(DL) = (UINT8)sTime.wDay;
                   3514:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   3515: }
                   3516: 
                   3517: inline void msdos_int_21h_2bh()
                   3518: {
                   3519:        REG8(AL) = 0x00;
                   3520: }
                   3521: 
                   3522: inline void msdos_int_21h_2ch()
                   3523: {
                   3524:        SYSTEMTIME sTime;
                   3525:        
                   3526:        GetLocalTime(&sTime);
                   3527:        REG8(CH) = (UINT8)sTime.wHour;
                   3528:        REG8(CL) = (UINT8)sTime.wMinute;
                   3529:        REG8(DH) = (UINT8)sTime.wSecond;
                   3530: }
                   3531: 
                   3532: inline void msdos_int_21h_2dh()
                   3533: {
                   3534:        REG8(AL) = 0x00;
                   3535: }
                   3536: 
                   3537: inline void msdos_int_21h_2eh()
                   3538: {
                   3539:        process_t *process = msdos_process_info_get(current_psp);
                   3540:        
                   3541:        process->verify = REG8(AL);
                   3542: }
                   3543: 
                   3544: inline void msdos_int_21h_2fh()
                   3545: {
                   3546:        process_t *process = msdos_process_info_get(current_psp);
                   3547:        
                   3548:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     3549:        SREG(ES) = process->dta.w.h;
                   3550:        i386_load_segment_descriptor(ES);
1.1       root     3551: }
                   3552: 
                   3553: inline void msdos_int_21h_30h()
                   3554: {
                   3555:        // Version Flag / OEM
                   3556:        if(REG8(AL) == 1) {
                   3557:                REG8(BH) = 0x00;        // not in ROM
                   3558:        } else {
                   3559:                REG8(BH) = 0xff;        // OEM = Microsoft
                   3560:        }
1.1.1.9   root     3561:        REG8(AL) = major_version;       // 7
                   3562:        REG8(AH) = minor_version;       // 10
1.1       root     3563: }
                   3564: 
                   3565: inline void msdos_int_21h_31h()
                   3566: {
                   3567:        int mcb_seg = current_psp - 1;
                   3568:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   3569:        
                   3570:        mcb->paragraphs = REG16(DX);
                   3571:        mcb_seg += mcb->paragraphs + 1;
                   3572:        msdos_mcb_create(mcb_seg, 'Z', 0, (MEMORY_END >> 4) - mcb_seg - 1);
                   3573:        
                   3574:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   3575: }
                   3576: 
                   3577: inline void msdos_int_21h_32h()
                   3578: {
                   3579:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   3580:        UINT16 seg, ofs;
                   3581:        
                   3582:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   3583:                REG8(AL) = 0;
1.1.1.3   root     3584:                SREG(DS) = seg;
                   3585:                i386_load_segment_descriptor(DS);
1.1       root     3586:                REG16(BX) = ofs;
                   3587:        } else {
                   3588:                REG8(AL) = 0xff;
1.1.1.3   root     3589:                m_CF = 1;
1.1       root     3590:        }
                   3591: }
                   3592: 
                   3593: inline void msdos_int_21h_33h()
                   3594: {
                   3595:        static UINT8 state = 0x00;
                   3596:        char path[MAX_PATH];
                   3597:        
                   3598:        switch(REG8(AL)) {
                   3599:        case 0x00:
                   3600:                REG8(DL) = state;
                   3601:                break;
                   3602:        case 0x01:
                   3603:                state = REG8(DL);
                   3604:                break;
                   3605:        case 0x05:
                   3606:                GetSystemDirectory(path, MAX_PATH);
                   3607:                if(path[0] >= 'a' && path[0] <= 'z') {
                   3608:                        REG8(DL) = path[0] - 'a' + 1;
                   3609:                } else {
                   3610:                        REG8(DL) = path[0] - 'A' + 1;
                   3611:                }
                   3612:                break;
                   3613:        case 0x06:
1.1.1.2   root     3614:                // MS-DOS version (7.10)
1.1       root     3615:                REG8(BL) = 7;
1.1.1.2   root     3616:                REG8(BH) = 10;
1.1       root     3617:                REG8(DL) = 0;
                   3618:                REG8(DH) = 0x10; // in HMA
                   3619:                break;
1.1.1.6   root     3620:        case 0x07:
                   3621:                if(REG8(DL) == 0) {
                   3622:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   3623:                } else if(REG8(DL) == 1) {
                   3624:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   3625:                }
                   3626:                break;
1.1       root     3627:        default:
                   3628:                REG16(AX) = 0x01;
1.1.1.3   root     3629:                m_CF = 1;
1.1       root     3630:                break;
                   3631:        }
                   3632: }
                   3633: 
                   3634: inline void msdos_int_21h_35h()
                   3635: {
                   3636:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     3637:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   3638:        i386_load_segment_descriptor(ES);
1.1       root     3639: }
                   3640: 
                   3641: inline void msdos_int_21h_36h()
                   3642: {
                   3643:        struct _diskfree_t df = {0};
                   3644:        
                   3645:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   3646:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   3647:                REG16(CX) = (UINT16)df.bytes_per_sector;
                   3648:                REG16(BX) = (UINT16)df.avail_clusters;
                   3649:                REG16(DX) = (UINT16)df.total_clusters;
                   3650:        } else {
                   3651:                REG16(AX) = 0xffff;
                   3652:        }
                   3653: }
                   3654: 
                   3655: inline void msdos_int_21h_37h()
                   3656: {
                   3657:        process_t *process = msdos_process_info_get(current_psp);
                   3658:        
                   3659:        switch(REG8(AL)) {
                   3660:        case 0x00:
                   3661:                REG8(AL) = 0x00;
                   3662:                REG8(DL) = process->switchar;
                   3663:                break;
                   3664:        case 0x01:
                   3665:                REG8(AL) = 0x00;
                   3666:                process->switchar = REG8(DL);
                   3667:                break;
                   3668:        default:
                   3669:                REG16(AX) = 1;
                   3670:                break;
                   3671:        }
                   3672: }
                   3673: 
                   3674: inline void msdos_int_21h_39h(int lfn)
                   3675: {
1.1.1.3   root     3676:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     3677:                REG16(AX) = errno;
1.1.1.3   root     3678:                m_CF = 1;
1.1       root     3679:        }
                   3680: }
                   3681: 
                   3682: inline void msdos_int_21h_3ah(int lfn)
                   3683: {
1.1.1.3   root     3684:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     3685:                REG16(AX) = errno;
1.1.1.3   root     3686:                m_CF = 1;
1.1       root     3687:        }
                   3688: }
                   3689: 
                   3690: inline void msdos_int_21h_3bh(int lfn)
                   3691: {
1.1.1.3   root     3692:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     3693:                REG16(AX) = errno;
1.1.1.3   root     3694:                m_CF = 1;
1.1       root     3695:        }
                   3696: }
                   3697: 
                   3698: inline void msdos_int_21h_3ch()
                   3699: {
1.1.1.3   root     3700:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     3701:        int attr = GetFileAttributes(path);
                   3702:        int fd = -1;
1.1.1.11! root     3703:        UINT16 info;
1.1       root     3704:        
1.1.1.11! root     3705:        if(msdos_is_con_path(path)) {
        !          3706:                fd = _open("CON", _O_WRONLY | _O_BINARY);
        !          3707:                info = 0x80d3;
1.1       root     3708:        } else {
                   3709:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11! root     3710:                info = msdos_drive_number(path);
1.1       root     3711:        }
                   3712:        if(fd != -1) {
                   3713:                if(attr == -1) {
                   3714:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   3715:                }
                   3716:                SetFileAttributes(path, attr);
                   3717:                REG16(AX) = fd;
1.1.1.11! root     3718:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1       root     3719:        } else {
                   3720:                REG16(AX) = errno;
1.1.1.3   root     3721:                m_CF = 1;
1.1       root     3722:        }
                   3723: }
                   3724: 
                   3725: inline void msdos_int_21h_3dh()
                   3726: {
1.1.1.3   root     3727:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     3728:        int mode = REG8(AL) & 0x03;
1.1.1.11! root     3729:        int fd = -1;
        !          3730:        UINT16 info;
1.1       root     3731:        
                   3732:        if(mode < 0x03) {
1.1.1.11! root     3733:                if(msdos_is_con_path(path)) {
        !          3734:                        fd = _open("CON", file_mode[mode].mode);
        !          3735:                        info = 0x80d3;
        !          3736:                } else {
        !          3737:                        fd = _open(path, file_mode[mode].mode);
        !          3738:                        info = msdos_drive_number(path);
        !          3739:                }
1.1       root     3740:                if(fd != -1) {
                   3741:                        REG16(AX) = fd;
1.1.1.11! root     3742:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1       root     3743:                } else {
                   3744:                        REG16(AX) = errno;
1.1.1.3   root     3745:                        m_CF = 1;
1.1       root     3746:                }
                   3747:        } else {
                   3748:                REG16(AX) = 0x0c;
1.1.1.3   root     3749:                m_CF = 1;
1.1       root     3750:        }
                   3751: }
                   3752: 
                   3753: inline void msdos_int_21h_3eh()
                   3754: {
                   3755:        process_t *process = msdos_process_info_get(current_psp);
                   3756:        
                   3757:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3758:                _close(REG16(BX));
                   3759:                msdos_file_handler_close(REG16(BX), current_psp);
                   3760:        } else {
                   3761:                REG16(AX) = 0x06;
1.1.1.3   root     3762:                m_CF = 1;
1.1       root     3763:        }
                   3764: }
                   3765: 
                   3766: inline void msdos_int_21h_3fh()
                   3767: {
                   3768:        process_t *process = msdos_process_info_get(current_psp);
                   3769:        
                   3770:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3771:                if(file_mode[file_handler[REG16(BX)].mode].in) {
                   3772:                        if(file_handler[REG16(BX)].atty) {
                   3773:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     3774:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     3775:                                int max = REG16(CX);
                   3776:                                int p = 0;
                   3777:                                
                   3778:                                while(max > p) {
                   3779:                                        int chr = msdos_getch();
                   3780:                                        
                   3781:                                        if(chr == 0x00) {
                   3782:                                                // skip 2nd byte
                   3783:                                                msdos_getch();
                   3784:                                        } else if(chr == 0x0d) {
                   3785:                                                // carriage return
                   3786:                                                buf[p++] = 0x0d;
                   3787:                                                if(max > p) {
                   3788:                                                        buf[p++] = 0x0a;
                   3789:                                                }
                   3790:                                                break;
                   3791:                                        } else if(chr == 0x08) {
                   3792:                                                // back space
                   3793:                                                if(p > 0) {
                   3794:                                                        p--;
                   3795:                                                        msdos_putch(chr);
                   3796:                                                        msdos_putch(' ');
                   3797:                                                        msdos_putch(chr);
                   3798:                                                }
                   3799:                                        } else {
                   3800:                                                buf[p++] = chr;
                   3801:                                                msdos_putch(chr);
                   3802:                                        }
                   3803:                                }
                   3804:                                REG16(AX) = p;
1.1.1.8   root     3805:                                // some seconds may be passed in console
1.1       root     3806:                                hardware_update();
                   3807:                        } else {
1.1.1.3   root     3808:                                REG16(AX) = _read(REG16(BX), mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     3809:                        }
                   3810:                } else {
                   3811:                        REG16(AX) = 0x05;
1.1.1.3   root     3812:                        m_CF = 1;
1.1       root     3813:                }
                   3814:        } else {
                   3815:                REG16(AX) = 0x06;
1.1.1.3   root     3816:                m_CF = 1;
1.1       root     3817:        }
                   3818: }
                   3819: 
                   3820: inline void msdos_int_21h_40h()
                   3821: {
                   3822:        process_t *process = msdos_process_info_get(current_psp);
                   3823:        
                   3824:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3825:                if(file_mode[file_handler[REG16(BX)].mode].out) {
                   3826:                        if(REG16(CX)) {
                   3827:                                if(file_handler[REG16(BX)].atty) {
                   3828:                                        // BX is stdout/stderr or is redirected to stdout
                   3829:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     3830:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     3831:                                        }
                   3832:                                        REG16(AX) = REG16(CX);
                   3833:                                } else {
1.1.1.3   root     3834:                                        REG16(AX) = msdos_write(REG16(BX), mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     3835:                                }
                   3836:                        } else {
                   3837:                                UINT32 pos = _tell(REG16(BX));
                   3838:                                _lseek(REG16(BX), 0, SEEK_END);
                   3839:                                UINT32 size = _tell(REG16(BX));
                   3840:                                REG16(AX) = 0;
                   3841:                                for(UINT32 i = size; i < pos; i++) {
                   3842:                                        UINT8 tmp = 0;
                   3843:                                        REG16(AX) += msdos_write(REG16(BX), &tmp, 1);
                   3844:                                }
                   3845:                                _lseek(REG16(BX), pos, SEEK_SET);
                   3846:                        }
                   3847:                } else {
                   3848:                        REG16(AX) = 0x05;
1.1.1.3   root     3849:                        m_CF = 1;
1.1       root     3850:                }
                   3851:        } else {
                   3852:                REG16(AX) = 0x06;
1.1.1.3   root     3853:                m_CF = 1;
1.1       root     3854:        }
                   3855: }
                   3856: 
                   3857: inline void msdos_int_21h_41h(int lfn)
                   3858: {
1.1.1.3   root     3859:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     3860:                REG16(AX) = errno;
1.1.1.3   root     3861:                m_CF = 1;
1.1       root     3862:        }
                   3863: }
                   3864: 
                   3865: inline void msdos_int_21h_42h()
                   3866: {
                   3867:        process_t *process = msdos_process_info_get(current_psp);
                   3868:        
                   3869:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3870:                if(REG8(AL) < 0x03) {
                   3871:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
                   3872:                        _lseek(REG16(BX), (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   3873:                        UINT32 pos = _tell(REG16(BX));
                   3874:                        REG16(AX) = pos & 0xffff;
                   3875:                        REG16(DX) = (pos >> 16);
                   3876:                } else {
                   3877:                        REG16(AX) = 0x01;
1.1.1.3   root     3878:                        m_CF = 1;
1.1       root     3879:                }
                   3880:        } else {
                   3881:                REG16(AX) = 0x06;
1.1.1.3   root     3882:                m_CF = 1;
1.1       root     3883:        }
                   3884: }
                   3885: 
                   3886: inline void msdos_int_21h_43h(int lfn)
                   3887: {
1.1.1.3   root     3888:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     3889:        int attr;
                   3890:        
                   3891:        switch(REG8(AL)) {
                   3892:        case 0x00:
                   3893:                if((attr = GetFileAttributes(path)) != -1) {
                   3894:                        REG16(CX) = 0;
                   3895:                        if(attr & FILE_ATTRIBUTE_READONLY) {
                   3896:                                REG16(CX) |= 0x01;
                   3897:                        }
                   3898:                        if(attr & FILE_ATTRIBUTE_HIDDEN) {
                   3899:                                REG16(CX) |= 0x02;
                   3900:                        }
                   3901:                        if(attr & FILE_ATTRIBUTE_SYSTEM) {
                   3902:                                REG16(CX) |= 0x04;
                   3903:                        }
                   3904:                        if(attr & FILE_ATTRIBUTE_ARCHIVE) {
                   3905:                                REG16(CX) |= 0x20;
                   3906:                        }
                   3907:                } else {
                   3908:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     3909:                        m_CF = 1;
1.1       root     3910:                }
                   3911:                break;
                   3912:        case 0x01:
                   3913:                if(SetFileAttributes(path, msdos_file_attribute_create(REG16(CX))) != 0) {
                   3914:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     3915:                        m_CF = 1;
1.1       root     3916:                }
                   3917:                break;
                   3918:        case 0x02:
                   3919:                break;
                   3920:        case 0x03:
                   3921:                REG16(CX) = 0x00;
                   3922:                break;
                   3923:        default:
                   3924:                REG16(AX) = 0x01;
1.1.1.3   root     3925:                m_CF = 1;
1.1       root     3926:                break;
                   3927:        }
                   3928: }
                   3929: 
                   3930: inline void msdos_int_21h_44h()
                   3931: {
                   3932:        switch(REG8(AL)) {
                   3933:        case 0x00: // get ioctrl data
                   3934:                REG16(DX) = file_handler[REG16(BX)].info;
                   3935:                break;
                   3936:        case 0x01: // set ioctrl data
                   3937:                file_handler[REG16(BX)].info |= REG8(DL);
                   3938:                break;
                   3939:        case 0x02: // recv from character device
                   3940:        case 0x03: // send to character device
                   3941:                REG16(AX) = 0x06;
1.1.1.3   root     3942:                m_CF = 1;
1.1       root     3943:                break;
                   3944:        case 0x04: // recv from block device
                   3945:        case 0x05: // send to block device
                   3946:                REG16(AX) = 0x05;
1.1.1.3   root     3947:                m_CF = 1;
1.1       root     3948:                break;
                   3949:        case 0x06: // get read status
                   3950:                {
                   3951:                        process_t *process = msdos_process_info_get(current_psp);
                   3952:                        
                   3953:                        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3954:                                if(file_mode[file_handler[REG16(BX)].mode].in) {
                   3955:                                        if(file_handler[REG16(BX)].atty) {
                   3956:                                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
                   3957:                                        } else {
                   3958:                                                REG8(AL) = eof(REG16(BX)) ? 0x00 : 0xff;
                   3959:                                        }
                   3960:                                } else {
                   3961:                                        REG8(AL) = 0x00;
                   3962:                                }
                   3963:                        } else {
                   3964:                                REG16(AX) = 0x06;
1.1.1.3   root     3965:                                m_CF = 1;
1.1       root     3966:                        }
                   3967:                }
                   3968:                break;
                   3969:        case 0x07: // get write status
                   3970:                {
                   3971:                        process_t *process = msdos_process_info_get(current_psp);
                   3972:                        
                   3973:                        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   3974:                                if(file_mode[file_handler[REG16(BX)].mode].out) {
                   3975:                                        REG8(AL) = 0xff;
                   3976:                                } else {
                   3977:                                        REG8(AL) = 0x00;
                   3978:                                }
                   3979:                        } else {
                   3980:                                REG16(AX) = 0x06;
1.1.1.3   root     3981:                                m_CF = 1;
1.1       root     3982:                        }
                   3983:                }
                   3984:                break;
                   3985:        case 0x08: // check removable drive
                   3986:                if(REG8(BL) < ('Z' - 'A' + 1)) {
                   3987:                        UINT32 val;
                   3988:                        if(REG8(BL) == 0) {
                   3989:                                val = GetDriveType(NULL);
                   3990:                        } else if(REG8(BL) < ('Z' - 'A' + 1)) {
                   3991:                                char tmp[8];
                   3992:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   3993:                                val = GetDriveType(tmp);
                   3994:                        }
                   3995:                        if(val == DRIVE_NO_ROOT_DIR) {
                   3996:                                // no drive
                   3997:                                REG16(AX) = 0x0f;
1.1.1.3   root     3998:                                m_CF = 1;
1.1       root     3999:                        } else if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   4000:                                // removable drive
                   4001:                                REG16(AX) = 0x00;
                   4002:                        } else {
                   4003:                                // fixed drive
                   4004:                                REG16(AX) = 0x01;
                   4005:                        }
                   4006:                } else {
                   4007:                        // invalid drive number
                   4008:                        REG16(AX) = 0x0f;
1.1.1.3   root     4009:                        m_CF = 1;
1.1       root     4010:                }
                   4011:                break;
                   4012:        case 0x09: // check remote drive
                   4013:                if(REG8(BL) < ('Z' - 'A' + 1)) {
                   4014:                        UINT32 val;
                   4015:                        if(REG8(BL) == 0) {
                   4016:                                val = GetDriveType(NULL);
                   4017:                        } else if(REG8(BL) < ('Z' - 'A' + 1)) {
                   4018:                                char tmp[8];
                   4019:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   4020:                                val = GetDriveType(tmp);
                   4021:                        }
                   4022:                        if(val == DRIVE_NO_ROOT_DIR) {
                   4023:                                // no drive
                   4024:                                REG16(AX) = 0x0f;
1.1.1.3   root     4025:                                m_CF = 1;
1.1       root     4026:                        } else if(val == DRIVE_REMOTE) {
                   4027:                                // remote drive
                   4028:                                REG16(DX) = 0x1000;
                   4029:                        } else {
                   4030:                                // local drive
                   4031:                                REG16(DX) = 0x00;
                   4032:                        }
                   4033:                } else {
                   4034:                        // invalid drive number
                   4035:                        REG16(AX) = 0x0f;
1.1.1.3   root     4036:                        m_CF = 1;
1.1       root     4037:                }
                   4038:                break;
                   4039:        case 0x0b: // set retry count
                   4040:                break;
                   4041:        default:
                   4042:                REG16(AX) = 0x01;
1.1.1.3   root     4043:                m_CF = 1;
1.1       root     4044:                break;
                   4045:        }
                   4046: }
                   4047: 
                   4048: inline void msdos_int_21h_45h()
                   4049: {
                   4050:        process_t *process = msdos_process_info_get(current_psp);
                   4051:        
                   4052:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   4053:                int fd = _dup(REG16(BX));
                   4054:                if(fd != -1) {
                   4055:                        REG16(AX) = fd;
                   4056:                        msdos_file_handler_dup(REG16(AX), REG16(BX), current_psp);
                   4057:                } else {
                   4058:                        REG16(AX) = errno;
1.1.1.3   root     4059:                        m_CF = 1;
1.1       root     4060:                }
                   4061:        } else {
                   4062:                REG16(AX) = 0x06;
1.1.1.3   root     4063:                m_CF = 1;
1.1       root     4064:        }
                   4065: }
                   4066: 
                   4067: inline void msdos_int_21h_46h()
                   4068: {
                   4069:        process_t *process = msdos_process_info_get(current_psp);
                   4070:        
                   4071:        if(REG16(BX) < process->max_files && REG16(CX) < process->max_files && file_handler[REG16(BX)].valid) {
                   4072:                if(_dup2(REG16(BX), REG16(CX)) != -1) {
                   4073:                        msdos_file_handler_dup(REG16(CX), REG16(BX), current_psp);
                   4074:                } else {
                   4075:                        REG16(AX) = errno;
1.1.1.3   root     4076:                        m_CF = 1;
1.1       root     4077:                }
                   4078:        } else {
                   4079:                REG16(AX) = 0x06;
1.1.1.3   root     4080:                m_CF = 1;
1.1       root     4081:        }
                   4082: }
                   4083: 
                   4084: inline void msdos_int_21h_47h(int lfn)
                   4085: {
                   4086:        char path[MAX_PATH];
                   4087:        
                   4088:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   4089:                if(path[1] == ':') {
                   4090:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     4091:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     4092:                } else {
1.1.1.3   root     4093:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     4094:                }
                   4095:        } else {
                   4096:                REG16(AX) = errno;
1.1.1.3   root     4097:                m_CF = 1;
1.1       root     4098:        }
                   4099: }
                   4100: 
                   4101: inline void msdos_int_21h_48h()
                   4102: {
                   4103:        int seg;
                   4104:        
1.1.1.8   root     4105:        if((malloc_strategy & 0xf0) == 0x00) {
                   4106:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   4107:                        REG16(AX) = seg;
                   4108:                } else {
                   4109:                        REG16(AX) = 0x08;
                   4110:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   4111:                        m_CF = 1;
                   4112:                }
                   4113:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   4114:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   4115:                        REG16(AX) = seg;
                   4116:                } else {
                   4117:                        REG16(AX) = 0x08;
                   4118:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   4119:                        m_CF = 1;
                   4120:                }
                   4121:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   4122:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   4123:                        REG16(AX) = seg;
                   4124:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   4125:                        REG16(AX) = seg;
                   4126:                } else {
                   4127:                        REG16(AX) = 0x08;
                   4128:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   4129:                        m_CF = 1;
                   4130:                }
1.1       root     4131:        }
                   4132: }
                   4133: 
                   4134: inline void msdos_int_21h_49h()
                   4135: {
1.1.1.3   root     4136:        msdos_mem_free(SREG(ES));
1.1       root     4137: }
                   4138: 
                   4139: inline void msdos_int_21h_4ah()
                   4140: {
                   4141:        int max_paragraphs;
                   4142:        
1.1.1.3   root     4143:        if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
1.1       root     4144:                REG16(AX) = 0x08;
                   4145:                REG16(BX) = max_paragraphs;
1.1.1.3   root     4146:                m_CF = 1;
1.1       root     4147:        }
                   4148: }
                   4149: 
                   4150: inline void msdos_int_21h_4bh()
                   4151: {
1.1.1.3   root     4152:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   4153:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     4154:        
                   4155:        switch(REG8(AL)) {
                   4156:        case 0x00:
                   4157:        case 0x01:
                   4158:                if(msdos_process_exec(command, param, REG8(AL))) {
                   4159:                        REG16(AX) = 0x02;
1.1.1.3   root     4160:                        m_CF = 1;
1.1       root     4161:                }
                   4162:                break;
                   4163:        default:
                   4164:                REG16(AX) = 0x01;
1.1.1.3   root     4165:                m_CF = 1;
1.1       root     4166:                break;
                   4167:        }
                   4168: }
                   4169: 
                   4170: inline void msdos_int_21h_4ch()
                   4171: {
                   4172:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   4173: }
                   4174: 
                   4175: inline void msdos_int_21h_4dh()
                   4176: {
                   4177:        REG16(AX) = retval;
                   4178: }
                   4179: 
                   4180: inline void msdos_int_21h_4eh()
                   4181: {
                   4182:        process_t *process = msdos_process_info_get(current_psp);
                   4183:        find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
1.1.1.3   root     4184:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     4185:        WIN32_FIND_DATA fd;
                   4186:        
                   4187:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4188:                FindClose(process->find_handle);
                   4189:                process->find_handle = INVALID_HANDLE_VALUE;
                   4190:        }
                   4191:        strcpy(process->volume_label, msdos_volume_label(path));
                   4192:        process->allowable_mask = REG8(CL);
                   4193:        
                   4194:        if((process->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   4195:                process->allowable_mask &= ~8;
                   4196:        }
                   4197:        if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   4198:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
                   4199:                        if(!FindNextFile(process->find_handle, &fd)) {
                   4200:                                FindClose(process->find_handle);
                   4201:                                process->find_handle = INVALID_HANDLE_VALUE;
                   4202:                                break;
                   4203:                        }
                   4204:                }
                   4205:        }
                   4206:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4207:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   4208:                msdos_find_file_conv_local_time(&fd);
                   4209:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   4210:                find->size = fd.nFileSizeLow;
                   4211:                strcpy(find->name, msdos_short_path(fd.cFileName));
                   4212:                REG16(AX) = 0;
                   4213:        } else if(process->allowable_mask & 8) {
                   4214:                find->attrib = 8;
                   4215:                find->size = 0;
                   4216:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
                   4217:                process->allowable_mask &= ~8;
                   4218:                REG16(AX) = 0;
                   4219:        } else {
                   4220:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     4221:                m_CF = 1;
1.1       root     4222:        }
                   4223: }
                   4224: 
                   4225: inline void msdos_int_21h_4fh()
                   4226: {
                   4227:        process_t *process = msdos_process_info_get(current_psp);
                   4228:        find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
                   4229:        WIN32_FIND_DATA fd;
                   4230:        
                   4231:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4232:                if(FindNextFile(process->find_handle, &fd)) {
                   4233:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
                   4234:                                if(!FindNextFile(process->find_handle, &fd)) {
                   4235:                                        FindClose(process->find_handle);
                   4236:                                        process->find_handle = INVALID_HANDLE_VALUE;
                   4237:                                        break;
                   4238:                                }
                   4239:                        }
                   4240:                } else {
                   4241:                        FindClose(process->find_handle);
                   4242:                        process->find_handle = INVALID_HANDLE_VALUE;
                   4243:                }
                   4244:        }
                   4245:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4246:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   4247:                msdos_find_file_conv_local_time(&fd);
                   4248:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   4249:                find->size = fd.nFileSizeLow;
                   4250:                strcpy(find->name, msdos_short_path(fd.cFileName));
                   4251:                REG16(AX) = 0;
                   4252:        } else if(process->allowable_mask & 8) {
                   4253:                find->attrib = 8;
                   4254:                find->size = 0;
                   4255:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
                   4256:                process->allowable_mask &= ~8;
                   4257:                REG16(AX) = 0;
                   4258:        } else {
                   4259:                REG16(AX) = 0x12;
1.1.1.3   root     4260:                m_CF = 1;
1.1       root     4261:        }
                   4262: }
                   4263: 
                   4264: inline void msdos_int_21h_50h()
                   4265: {
1.1.1.8   root     4266:        if(current_psp != REG16(BX)) {
                   4267:                process_t *process = msdos_process_info_get(current_psp);
                   4268:                if(process != NULL) {
                   4269:                        process->psp = REG16(BX);
                   4270:                }
                   4271:                current_psp = REG16(BX);
                   4272:        }
1.1       root     4273: }
                   4274: 
                   4275: inline void msdos_int_21h_51h()
                   4276: {
                   4277:        REG16(BX) = current_psp;
                   4278: }
                   4279: 
                   4280: inline void msdos_int_21h_52h()
                   4281: {
1.1.1.3   root     4282:        SREG(ES) = (DOS_INFO_BASE >> 4);
                   4283:        i386_load_segment_descriptor(ES);
1.1       root     4284:        REG16(BX) = (DOS_INFO_BASE & 0x0f);
                   4285: }
                   4286: 
                   4287: inline void msdos_int_21h_54h()
                   4288: {
                   4289:        process_t *process = msdos_process_info_get(current_psp);
                   4290:        
                   4291:        REG8(AL) = process->verify;
                   4292: }
                   4293: 
                   4294: inline void msdos_int_21h_55h()
                   4295: {
                   4296:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   4297:        
                   4298:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   4299:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   4300:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   4301:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   4302:        psp->parent_psp = current_psp;
                   4303: }
                   4304: 
                   4305: inline void msdos_int_21h_56h(int lfn)
                   4306: {
                   4307:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     4308:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   4309:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     4310:        
                   4311:        if(rename(src, dst)) {
                   4312:                REG16(AX) = errno;
1.1.1.3   root     4313:                m_CF = 1;
1.1       root     4314:        }
                   4315: }
                   4316: 
                   4317: inline void msdos_int_21h_57h()
                   4318: {
                   4319:        FILETIME time, local;
1.1.1.6   root     4320:        HANDLE handle;
1.1       root     4321:        
                   4322:        switch(REG8(AL)) {
                   4323:        case 0x00:
1.1.1.6   root     4324:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4325:                        REG16(AX) = (UINT16)GetLastError();
                   4326:                        m_CF = 1;
                   4327:                } if(!GetFileTime(handle, NULL, NULL, &time)) {
                   4328:                        REG16(AX) = (UINT16)GetLastError();
                   4329:                        m_CF = 1;
                   4330:                } else {
1.1       root     4331:                        FileTimeToLocalFileTime(&time, &local);
                   4332:                        FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1.1.6   root     4333:                }
                   4334:                break;
                   4335:        case 0x01:
                   4336:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   4337:                LocalFileTimeToFileTime(&local, &time);
                   4338:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4339:                        REG16(AX) = (UINT16)GetLastError();
                   4340:                        m_CF = 1;
                   4341:                } else if(!SetFileTime(handle, NULL, NULL, &time)) {
                   4342:                        REG16(AX) = (UINT16)GetLastError();
                   4343:                        m_CF = 1;
                   4344:                }
                   4345:                break;
                   4346:        case 0x04:
                   4347:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4348:                        REG16(AX) = (UINT16)GetLastError();
                   4349:                        m_CF = 1;
                   4350:                } if(!GetFileTime(handle, NULL, &time, NULL)) {
                   4351:                        REG16(AX) = (UINT16)GetLastError();
                   4352:                        m_CF = 1;
1.1       root     4353:                } else {
1.1.1.6   root     4354:                        FileTimeToLocalFileTime(&time, &local);
                   4355:                        FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
                   4356:                }
                   4357:                break;
                   4358:        case 0x05:
                   4359:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   4360:                LocalFileTimeToFileTime(&local, &time);
                   4361:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4362:                        REG16(AX) = (UINT16)GetLastError();
                   4363:                        m_CF = 1;
                   4364:                } else if(!SetFileTime(handle, NULL, &time, NULL)) {
1.1       root     4365:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     4366:                        m_CF = 1;
1.1       root     4367:                }
                   4368:                break;
1.1.1.6   root     4369:        case 0x06:
                   4370:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4371:                        REG16(AX) = (UINT16)GetLastError();
                   4372:                        m_CF = 1;
                   4373:                } if(!GetFileTime(handle, &time, NULL, NULL)) {
                   4374:                        REG16(AX) = (UINT16)GetLastError();
                   4375:                        m_CF = 1;
                   4376:                } else {
                   4377:                        FileTimeToLocalFileTime(&time, &local);
                   4378:                        FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
                   4379:                }
                   4380:                break;
                   4381:        case 0x07:
1.1       root     4382:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   4383:                LocalFileTimeToFileTime(&local, &time);
1.1.1.6   root     4384:                if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
                   4385:                        REG16(AX) = (UINT16)GetLastError();
                   4386:                        m_CF = 1;
                   4387:                } else if(!SetFileTime(handle, &time, NULL, NULL)) {
1.1       root     4388:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     4389:                        m_CF = 1;
1.1       root     4390:                }
                   4391:                break;
                   4392:        default:
                   4393:                REG16(AX) = 0x01;
1.1.1.3   root     4394:                m_CF = 1;
1.1       root     4395:                break;
                   4396:        }
                   4397: }
                   4398: 
                   4399: inline void msdos_int_21h_58h()
                   4400: {
                   4401:        switch(REG8(AL)) {
                   4402:        case 0x00:
1.1.1.7   root     4403:                REG16(AX) = malloc_strategy;
                   4404:                break;
                   4405:        case 0x01:
                   4406:                switch(REG16(BX)) {
                   4407:                case 0x0000:
                   4408:                case 0x0001:
                   4409:                case 0x0002:
                   4410:                case 0x0040:
                   4411:                case 0x0041:
                   4412:                case 0x0042:
                   4413:                case 0x0080:
                   4414:                case 0x0081:
                   4415:                case 0x0082:
                   4416:                        malloc_strategy = REG16(BX);
                   4417:                        break;
                   4418:                default:
                   4419:                        REG16(AX) = 0x01;
                   4420:                        m_CF = 1;
                   4421:                        break;
                   4422:                }
                   4423:                break;
                   4424:        case 0x02:
                   4425:                REG8(AL) = umb_linked;
                   4426:                break;
                   4427:        case 0x03:
                   4428:                switch(REG16(BX)) {
                   4429:                case 0x0000:
                   4430:                case 0x0001:
                   4431:                        umb_linked = REG8(BL);
                   4432:                        break;
                   4433:                default:
                   4434:                        REG16(AX) = 0x01;
                   4435:                        m_CF = 1;
                   4436:                        break;
                   4437:                }
1.1       root     4438:                break;
                   4439:        default:
                   4440:                REG16(AX) = 0x01;
1.1.1.3   root     4441:                m_CF = 1;
1.1       root     4442:                break;
                   4443:        }
                   4444: }
                   4445: 
                   4446: inline void msdos_int_21h_59h()
                   4447: {
                   4448:        REG16(AX) = error_code;
                   4449:        switch(error_code) {
                   4450:        case  4: // Too many open files
                   4451:        case  8: // Insufficient memory
                   4452:                REG8(BH) = 1; // Out of resource
                   4453:                break;
                   4454:        case  5: // Access denied
                   4455:                REG8(BH) = 3; // Authorization
                   4456:                break;
                   4457:        case  7: // Memory control block destroyed
                   4458:                REG8(BH) = 4; // Internal
                   4459:                break;
                   4460:        case  2: // File not found
                   4461:        case  3: // Path not found
                   4462:        case 15: // Invaid drive specified
                   4463:        case 18: // No more files
                   4464:                REG8(BH) = 8; // Not found
                   4465:                break;
                   4466:        case 32: // Sharing violation
                   4467:        case 33: // Lock violation
                   4468:                REG8(BH) = 10; // Locked
                   4469:                break;
                   4470: //     case 16: // Removal of current directory attempted
                   4471:        case 19: // Attempted write on protected disk
                   4472:        case 21: // Drive not ready
                   4473: //     case 29: // Write failure
                   4474: //     case 30: // Read failure
                   4475: //     case 82: // Cannot create subdirectory
                   4476:                REG8(BH) = 11; // Media
                   4477:                break;
                   4478:        case 80: // File already exists
                   4479:                REG8(BH) = 12; // Already exist
                   4480:                break;
                   4481:        default:
                   4482:                REG8(BH) = 13; // Unknown
                   4483:                break;
                   4484:        }
                   4485:        REG8(BL) = 1; // Retry
                   4486:        REG8(CH) = 1; // Unknown
                   4487: }
                   4488: 
                   4489: inline void msdos_int_21h_5ah()
                   4490: {
1.1.1.3   root     4491:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     4492:        int len = strlen(path);
                   4493:        char tmp[MAX_PATH];
                   4494:        
                   4495:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   4496:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   4497:                
                   4498:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   4499:                REG16(AX) = fd;
                   4500:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
                   4501:                
                   4502:                strcpy(path, tmp);
                   4503:                int dx = REG16(DX) + len;
1.1.1.3   root     4504:                int ds = SREG(DS);
1.1       root     4505:                while(dx > 0xffff) {
                   4506:                        dx -= 0x10;
                   4507:                        ds++;
                   4508:                }
                   4509:                REG16(DX) = dx;
1.1.1.3   root     4510:                SREG(DS) = ds;
                   4511:                i386_load_segment_descriptor(DS);
1.1       root     4512:        } else {
                   4513:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     4514:                m_CF = 1;
1.1       root     4515:        }
                   4516: }
                   4517: 
                   4518: inline void msdos_int_21h_5bh()
                   4519: {
1.1.1.3   root     4520:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     4521:        
                   4522:        if(_access(path, 0) == 0) {
                   4523:                // already exists
                   4524:                REG16(AX) = 0x50;
1.1.1.3   root     4525:                m_CF = 1;
1.1       root     4526:        } else {
                   4527:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   4528:                
                   4529:                if(fd != -1) {
                   4530:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   4531:                        REG16(AX) = fd;
                   4532:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
                   4533:                } else {
                   4534:                        REG16(AX) = errno;
1.1.1.3   root     4535:                        m_CF = 1;
1.1       root     4536:                }
                   4537:        }
                   4538: }
                   4539: 
                   4540: inline void msdos_int_21h_5ch()
                   4541: {
                   4542:        process_t *process = msdos_process_info_get(current_psp);
                   4543:        
                   4544:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   4545:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   4546:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
                   4547:                        UINT32 pos = _tell(REG16(BX));
                   4548:                        _lseek(REG16(BX), (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   4549:                        if(_locking(REG16(BX), modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
                   4550:                                REG16(AX) = errno;
1.1.1.3   root     4551:                                m_CF = 1;
1.1       root     4552:                        }
                   4553:                        _lseek(REG16(BX), pos, SEEK_SET);
                   4554:                        // some seconds may be passed in _locking()
                   4555:                        hardware_update();
                   4556:                } else {
                   4557:                        REG16(AX) = 0x01;
1.1.1.3   root     4558:                        m_CF = 1;
1.1       root     4559:                }
                   4560:        } else {
                   4561:                REG16(AX) = 0x06;
1.1.1.3   root     4562:                m_CF = 1;
1.1       root     4563:        }
                   4564: }
                   4565: 
                   4566: inline void msdos_int_21h_60h(int lfn)
                   4567: {
                   4568:        if(lfn) {
                   4569:                char full[MAX_PATH], *name;
1.1.1.3   root     4570:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
                   4571:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), full);
1.1       root     4572:        } else {
1.1.1.3   root     4573:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     4574:        }
                   4575: }
                   4576: 
                   4577: inline void msdos_int_21h_61h()
                   4578: {
                   4579:        REG8(AL) = 0;
                   4580: }
                   4581: 
                   4582: inline void msdos_int_21h_62h()
                   4583: {
                   4584:        REG16(BX) = current_psp;
                   4585: }
                   4586: 
                   4587: inline void msdos_int_21h_63h()
                   4588: {
                   4589:        switch(REG8(AL)) {
                   4590:        case 0x00:
1.1.1.3   root     4591:                SREG(DS) = (DBCS_TABLE >> 4);
                   4592:                i386_load_segment_descriptor(DS);
1.1       root     4593:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   4594:                REG8(AL) = 0x00;
                   4595:                break;
                   4596:        default:
                   4597:                REG16(AX) = 0x01;
1.1.1.3   root     4598:                m_CF = 1;
1.1       root     4599:                break;
                   4600:        }
                   4601: }
                   4602: 
                   4603: inline void msdos_int_21h_65h()
                   4604: {
                   4605:        char tmp[0x10000];
                   4606:        
                   4607:        switch(REG8(AL)) {
                   4608:        case 0x07:
1.1.1.3   root     4609:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   4610:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   4611:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1       root     4612:                REG16(CX) = 0x05;
                   4613:                break;
                   4614:        case 0x20:
                   4615:                sprintf(tmp, "%c", REG8(DL));
                   4616:                my_strupr(tmp);
                   4617:                REG8(DL) = tmp[0];
                   4618:                break;
                   4619:        case 0x21:
                   4620:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     4621:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     4622:                my_strupr(tmp);
1.1.1.3   root     4623:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     4624:                break;
                   4625:        case 0x22:
1.1.1.3   root     4626:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     4627:                break;
                   4628:        default:
                   4629:                REG16(AX) = 0x01;
1.1.1.3   root     4630:                m_CF = 1;
1.1       root     4631:                break;
                   4632:        }
                   4633: }
                   4634: 
                   4635: inline void msdos_int_21h_66h()
                   4636: {
                   4637:        switch(REG8(AL)) {
                   4638:        case 0x01:
                   4639:                REG16(BX) = active_code_page;
                   4640:                REG16(DX) = system_code_page;
                   4641:                break;
                   4642:        case 0x02:
                   4643:                if(active_code_page == REG16(BX)) {
                   4644:                        REG16(AX) = 0xeb41;
                   4645:                } else if(_setmbcp(REG16(BX)) == 0) {
                   4646:                        active_code_page = REG16(BX);
                   4647:                        msdos_dbcs_table_update();
                   4648:                        REG16(AX) = 0xeb41;
                   4649:                } else {
                   4650:                        REG16(AX) = 0x25;
1.1.1.3   root     4651:                        m_CF = 1;
1.1       root     4652:                }
                   4653:                break;
                   4654:        default:
                   4655:                REG16(AX) = 0x01;
1.1.1.3   root     4656:                m_CF = 1;
1.1       root     4657:                break;
                   4658:        }
                   4659: }
                   4660: 
                   4661: inline void msdos_int_21h_67h()
                   4662: {
                   4663:        process_t *process = msdos_process_info_get(current_psp);
                   4664:        
                   4665:        if(REG16(BX) <= MAX_FILES) {
                   4666:                process->max_files = max(REG16(BX), 20);
                   4667:        } else {
                   4668:                REG16(AX) = 0x08;
1.1.1.3   root     4669:                m_CF = 1;
1.1       root     4670:        }
                   4671: }
                   4672: 
                   4673: inline void msdos_int_21h_68h()
                   4674: {
                   4675:        process_t *process = msdos_process_info_get(current_psp);
                   4676:        
                   4677:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   4678:                // fflush(_fdopen(REG16(BX), ""));
                   4679:        } else {
                   4680:                REG16(AX) = 0x06;
1.1.1.3   root     4681:                m_CF = 1;
1.1       root     4682:        }
                   4683: }
                   4684: 
                   4685: inline void msdos_int_21h_69h()
                   4686: {
1.1.1.3   root     4687:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     4688:        char path[] = "A:\\";
                   4689:        char volume_label[MAX_PATH];
                   4690:        DWORD serial_number = 0;
                   4691:        char file_system[MAX_PATH];
                   4692:        
                   4693:        if(REG8(BL) == 0) {
                   4694:                path[0] = 'A' + _getdrive() - 1;
                   4695:        } else {
                   4696:                path[0] = 'A' + REG8(BL) - 1;
                   4697:        }
                   4698:        
                   4699:        switch(REG8(AL)) {
                   4700:        case 0x00:
                   4701:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   4702:                        info->info_level = 0;
                   4703:                        info->serial_number = serial_number;
                   4704:                        memset(info->volume_label, 0x20, 11);
                   4705:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   4706:                        memset(info->file_system, 0x20, 8);
                   4707:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   4708:                } else {
                   4709:                        REG16(AX) = errno;
1.1.1.3   root     4710:                        m_CF = 1;
1.1       root     4711:                }
                   4712:                break;
                   4713:        case 0x01:
                   4714:                REG16(AX) = 0x03;
1.1.1.3   root     4715:                m_CF = 1;
1.1       root     4716:        }
                   4717: }
                   4718: 
                   4719: inline void msdos_int_21h_6ah()
                   4720: {
                   4721:        REG8(AH) = 0x68;
                   4722:        msdos_int_21h_68h();
                   4723: }
                   4724: 
                   4725: inline void msdos_int_21h_6bh()
                   4726: {
                   4727:        REG8(AL) = 0;
                   4728: }
                   4729: 
                   4730: inline void msdos_int_21h_6ch(int lfn)
                   4731: {
1.1.1.3   root     4732:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     4733:        int mode = REG8(BL) & 0x03;
                   4734:        
                   4735:        if(mode < 0x03) {
                   4736:                if(_access(path, 0) == 0) {
                   4737:                        // file exists
                   4738:                        if(REG8(DL) & 1) {
1.1.1.11! root     4739:                                int fd = -1;
        !          4740:                                UINT16 info;
1.1       root     4741:                                
1.1.1.11! root     4742:                                if(msdos_is_con_path(path)) {
        !          4743:                                        fd = _open("CON", file_mode[mode].mode);
        !          4744:                                        info = 0x80d3;
        !          4745:                                } else {
        !          4746:                                        fd = _open(path, file_mode[mode].mode);
        !          4747:                                        info = msdos_drive_number(path);
        !          4748:                                }
1.1       root     4749:                                if(fd != -1) {
                   4750:                                        REG16(AX) = fd;
                   4751:                                        REG16(CX) = 1;
1.1.1.11! root     4752:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1       root     4753:                                } else {
                   4754:                                        REG16(AX) = errno;
1.1.1.3   root     4755:                                        m_CF = 1;
1.1       root     4756:                                }
                   4757:                        } else if(REG8(DL) & 2) {
                   4758:                                int attr = GetFileAttributes(path);
                   4759:                                int fd = -1;
1.1.1.11! root     4760:                                UINT16 info;
1.1       root     4761:                                
1.1.1.11! root     4762:                                if(msdos_is_con_path(path)) {
        !          4763:                                        fd = _open("CON", file_mode[mode].mode);
        !          4764:                                        info = 0x80d3;
1.1       root     4765:                                } else {
                   4766:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11! root     4767:                                        info = msdos_drive_number(path);
1.1       root     4768:                                }
                   4769:                                if(fd != -1) {
                   4770:                                        if(attr == -1) {
                   4771:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   4772:                                        }
                   4773:                                        SetFileAttributes(path, attr);
                   4774:                                        REG16(AX) = fd;
                   4775:                                        REG16(CX) = 3;
1.1.1.11! root     4776:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1       root     4777:                                } else {
                   4778:                                        REG16(AX) = errno;
1.1.1.3   root     4779:                                        m_CF = 1;
1.1       root     4780:                                }
                   4781:                        } else {
                   4782:                                REG16(AX) = 0x50;
1.1.1.3   root     4783:                                m_CF = 1;
1.1       root     4784:                        }
                   4785:                } else {
                   4786:                        // file not exists
                   4787:                        if(REG8(DL) & 0x10) {
                   4788:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   4789:                                
                   4790:                                if(fd != -1) {
                   4791:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   4792:                                        REG16(AX) = fd;
                   4793:                                        REG16(CX) = 2;
                   4794:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
                   4795:                                } else {
                   4796:                                        REG16(AX) = errno;
1.1.1.3   root     4797:                                        m_CF = 1;
1.1       root     4798:                                }
                   4799:                        } else {
                   4800:                                REG16(AX) = 0x02;
1.1.1.3   root     4801:                                m_CF = 1;
1.1       root     4802:                        }
                   4803:                }
                   4804:        } else {
                   4805:                REG16(AX) = 0x0c;
1.1.1.3   root     4806:                m_CF = 1;
1.1       root     4807:        }
                   4808: }
                   4809: 
                   4810: inline void msdos_int_21h_710dh()
                   4811: {
                   4812:        // reset drive
                   4813: }
                   4814: 
                   4815: inline void msdos_int_21h_714eh()
                   4816: {
                   4817:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     4818:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   4819:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     4820:        WIN32_FIND_DATA fd;
                   4821:        
                   4822:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4823:                FindClose(process->find_handle);
                   4824:                process->find_handle = INVALID_HANDLE_VALUE;
                   4825:        }
                   4826:        strcpy(process->volume_label, msdos_volume_label(path));
                   4827:        process->allowable_mask = REG8(CL);
                   4828:        process->required_mask = REG8(CH);
                   4829:        
                   4830:        if((process->allowable_mask & 8) && !msdos_match_volume_label(path, process->volume_label) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   4831:                process->allowable_mask &= ~8;
                   4832:        }
                   4833:        if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   4834:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, process->required_mask)) {
                   4835:                        if(!FindNextFile(process->find_handle, &fd)) {
                   4836:                                FindClose(process->find_handle);
                   4837:                                process->find_handle = INVALID_HANDLE_VALUE;
                   4838:                                break;
                   4839:                        }
                   4840:                }
                   4841:        }
                   4842:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4843:                find->attrib = fd.dwFileAttributes;
                   4844:                msdos_find_file_conv_local_time(&fd);
                   4845:                if(REG16(SI) == 0) {
                   4846:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   4847:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   4848:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   4849:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   4850:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   4851:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   4852:                } else {
                   4853:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   4854:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   4855:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   4856:                }
                   4857:                find->size_hi = fd.nFileSizeHigh;
                   4858:                find->size_lo = fd.nFileSizeLow;
                   4859:                strcpy(find->full_name, fd.cFileName);
                   4860:                strcpy(find->short_name, msdos_short_path(fd.cFileName));
                   4861:        } else if(process->allowable_mask & 8) {
                   4862:                // volume label
                   4863:                find->attrib = 8;
                   4864:                find->size_hi = find->size_lo = 0;
                   4865:                strcpy(find->full_name, process->volume_label);
                   4866:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
                   4867:                process->allowable_mask &= ~8;
                   4868:        } else {
                   4869:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     4870:                m_CF = 1;
1.1       root     4871:        }
                   4872: }
                   4873: 
                   4874: inline void msdos_int_21h_714fh()
                   4875: {
                   4876:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     4877:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     4878:        WIN32_FIND_DATA fd;
                   4879:        
                   4880:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4881:                if(FindNextFile(process->find_handle, &fd)) {
                   4882:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, process->required_mask)) {
                   4883:                                if(!FindNextFile(process->find_handle, &fd)) {
                   4884:                                        FindClose(process->find_handle);
                   4885:                                        process->find_handle = INVALID_HANDLE_VALUE;
                   4886:                                        break;
                   4887:                                }
                   4888:                        }
                   4889:                } else {
                   4890:                        FindClose(process->find_handle);
                   4891:                        process->find_handle = INVALID_HANDLE_VALUE;
                   4892:                }
                   4893:        }
                   4894:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4895:                find->attrib = fd.dwFileAttributes;
                   4896:                msdos_find_file_conv_local_time(&fd);
                   4897:                if(REG16(SI) == 0) {
                   4898:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   4899:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   4900:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   4901:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   4902:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   4903:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   4904:                } else {
                   4905:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   4906:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   4907:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   4908:                }
                   4909:                find->size_hi = fd.nFileSizeHigh;
                   4910:                find->size_lo = fd.nFileSizeLow;
                   4911:                strcpy(find->full_name, fd.cFileName);
                   4912:                strcpy(find->short_name, msdos_short_path(fd.cFileName));
                   4913:        } else if(process->allowable_mask & 8) {
                   4914:                // volume label
                   4915:                find->attrib = 8;
                   4916:                find->size_hi = find->size_lo = 0;
                   4917:                strcpy(find->full_name, process->volume_label);
                   4918:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
                   4919:                process->allowable_mask &= ~8;
                   4920:        } else {
                   4921:                REG16(AX) = 0x12;
1.1.1.3   root     4922:                m_CF = 1;
1.1       root     4923:        }
                   4924: }
                   4925: 
                   4926: inline void msdos_int_21h_71a0h()
                   4927: {
                   4928:        DWORD max_component_len, file_sys_flag;
                   4929:        
1.1.1.3   root     4930:        if(GetVolumeInformation((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
1.1       root     4931:                REG16(BX) = (UINT16)file_sys_flag;
                   4932:                REG16(CX) = (UINT16)max_component_len;          // 255
                   4933:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   4934:        } else {
                   4935:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     4936:                m_CF = 1;
1.1       root     4937:        }
                   4938: }
                   4939: 
                   4940: inline void msdos_int_21h_71a1h()
                   4941: {
                   4942:        process_t *process = msdos_process_info_get(current_psp);
                   4943:        find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
                   4944:        
                   4945:        if(process->find_handle != INVALID_HANDLE_VALUE) {
                   4946:                FindClose(process->find_handle);
                   4947:                process->find_handle = INVALID_HANDLE_VALUE;
                   4948:        }
                   4949: }
                   4950: 
                   4951: inline void msdos_int_21h_71a6h()
                   4952: {
                   4953:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     4954:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     4955:        struct _stat64 status;
                   4956:        DWORD serial_number = 0;
                   4957:        
                   4958:        if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
                   4959:                if(_fstat64(REG16(BX), &status) == 0) {
                   4960:                        if(file_handler[REG16(BX)].path[1] == ':') {
                   4961:                                // NOTE: we need to consider the network file path "\\host\share\"
                   4962:                                char volume[] = "A:\\";
                   4963:                                volume[0] = file_handler[REG16(BX)].path[1];
                   4964:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   4965:                        }
                   4966:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[REG16(BX)].path);
                   4967:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   4968:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   4969:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   4970:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   4971:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   4972:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   4973:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   4974:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   4975:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   4976:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
                   4977:                        // this is dummy id and it will be changed when it is reopend...
                   4978:                        *(UINT32 *)(buffer + 0x2c) = 0;
                   4979:                        *(UINT32 *)(buffer + 0x30) = file_handler[REG16(BX)].id;
                   4980:                } else {
                   4981:                        REG16(AX) = errno;
1.1.1.3   root     4982:                        m_CF = 1;
1.1       root     4983:                }
                   4984:        } else {
                   4985:                REG16(AX) = 0x06;
1.1.1.3   root     4986:                m_CF = 1;
1.1       root     4987:        }
                   4988: }
                   4989: 
                   4990: inline void msdos_int_21h_71a7h()
                   4991: {
                   4992:        switch(REG8(BL)) {
                   4993:        case 0x00:
1.1.1.3   root     4994:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     4995:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     4996:                        m_CF = 1;
1.1       root     4997:                }
                   4998:                break;
                   4999:        case 0x01:
                   5000:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     5001:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     5002:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     5003:                        m_CF = 1;
1.1       root     5004:                }
                   5005:                break;
                   5006:        default:
                   5007:                REG16(AX) = 0x01;
1.1.1.3   root     5008:                m_CF = 1;
1.1       root     5009:                break;
                   5010:        }
                   5011: }
                   5012: 
                   5013: inline void msdos_int_21h_71a8h()
                   5014: {
                   5015:        if(REG8(DH) == 0) {
                   5016:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     5017:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     5018:                memset(fcb, 0x20, sizeof(fcb));
                   5019:                int len = strlen(tmp);
                   5020:                int pos = 0;
                   5021:                for(int i = 0; i < len; i++) {
                   5022:                        if(tmp[i] == '.') {
                   5023:                                pos = 8;
                   5024:                        } else {
                   5025:                                if(msdos_lead_byte_check(tmp[i])) {
                   5026:                                        fcb[pos++] = tmp[i++];
                   5027:                                }
                   5028:                                fcb[pos++] = tmp[i];
                   5029:                        }
                   5030:                }
1.1.1.3   root     5031:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     5032:        } else {
1.1.1.3   root     5033:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     5034:        }
                   5035: }
                   5036: 
                   5037: inline void msdos_int_21h_7303h()
                   5038: {
1.1.1.3   root     5039:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   5040:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     5041:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   5042:        
                   5043:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   5044:                info->size_of_structure = sizeof(ext_space_info_t);
                   5045:                info->structure_version = 0;
                   5046:                info->sectors_per_cluster = sectors_per_cluster;
                   5047:                info->bytes_per_sector = bytes_per_sector;
                   5048:                info->available_clusters_on_drive = free_clusters;
                   5049:                info->total_clusters_on_drive = total_clusters;
                   5050:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   5051:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   5052:                info->available_allocation_units = free_clusters;       // ???
                   5053:                info->total_allocation_units = total_clusters;          // ???
                   5054:        } else {
                   5055:                REG16(AX) = errno;
1.1.1.3   root     5056:                m_CF = 1;
1.1       root     5057:        }
                   5058: }
                   5059: 
                   5060: inline void msdos_int_25h()
                   5061: {
                   5062:        UINT16 seg, ofs;
                   5063:        DWORD dwSize;
                   5064:        
1.1.1.3   root     5065: #if defined(HAS_I386)
                   5066:        I386OP(pushf)();
                   5067: #else
                   5068:        PREFIX86(_pushf());
                   5069: #endif
1.1       root     5070:        
                   5071:        if(!(REG8(AL) < 26)) {
                   5072:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     5073:                m_CF = 1;
1.1       root     5074:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   5075:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5076:                m_CF = 1;
1.1       root     5077:        } else {
                   5078:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5079:                char dev[64];
                   5080:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   5081:                
                   5082:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   5083:                if(hFile == INVALID_HANDLE_VALUE) {
                   5084:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5085:                        m_CF = 1;
1.1       root     5086:                } else {
                   5087:                        if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   5088:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5089:                                m_CF = 1;
1.1       root     5090:                        } else if(SetFilePointer(hFile, REG16(DX) * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5091:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     5092:                                m_CF = 1;
                   5093:                        } else if(ReadFile(hFile, mem + SREG_BASE(DS) + REG16(BX), REG16(CX) * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     5094:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     5095:                                m_CF = 1;
1.1       root     5096:                        }
                   5097:                        CloseHandle(hFile);
                   5098:                }
                   5099:        }
                   5100: }
                   5101: 
                   5102: inline void msdos_int_26h()
                   5103: {
                   5104:        // this operation may cause serious damage for drives, so always returns error...
                   5105:        UINT16 seg, ofs;
                   5106:        DWORD dwSize;
                   5107:        
1.1.1.3   root     5108: #if defined(HAS_I386)
                   5109:        I386OP(pushf)();
                   5110: #else
                   5111:        PREFIX86(_pushf());
                   5112: #endif
1.1       root     5113:        
                   5114:        if(!(REG8(AL) < 26)) {
                   5115:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     5116:                m_CF = 1;
1.1       root     5117:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   5118:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5119:                m_CF = 1;
1.1       root     5120:        } else {
                   5121:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   5122:                char dev[64];
                   5123:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   5124:                
                   5125:                if(dpb->media_type == 0xf8) {
                   5126:                        // this drive is not a floppy
1.1.1.6   root     5127: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   5128: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   5129: //                     }
1.1       root     5130:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5131:                        m_CF = 1;
1.1       root     5132:                } else {
                   5133:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   5134:                        if(hFile == INVALID_HANDLE_VALUE) {
                   5135:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5136:                                m_CF = 1;
1.1       root     5137:                        } else {
                   5138:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   5139:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     5140:                                        m_CF = 1;
1.1       root     5141:                                } else if(SetFilePointer(hFile, REG16(DX) * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   5142:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     5143:                                        m_CF = 1;
                   5144:                                } else if(WriteFile(hFile, mem + SREG_BASE(DS) + REG16(BX), REG16(CX) * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     5145:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     5146:                                        m_CF = 1;
1.1       root     5147:                                }
                   5148:                                CloseHandle(hFile);
                   5149:                        }
                   5150:                }
                   5151:        }
                   5152: }
                   5153: 
                   5154: inline void msdos_int_27h()
                   5155: {
1.1.1.3   root     5156:        int mcb_seg = SREG(CS) - 1;
1.1       root     5157:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5158:        
                   5159:        mcb->paragraphs = (REG16(DX) >> 4);
                   5160:        mcb_seg += mcb->paragraphs + 1;
                   5161:        msdos_mcb_create(mcb_seg, 'Z', 0, (MEMORY_END >> 4) - mcb_seg - 1);
                   5162:        
1.1.1.3   root     5163:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     5164: }
                   5165: 
                   5166: inline void msdos_int_29h()
                   5167: {
                   5168:        msdos_putch(REG8(AL));
                   5169: }
                   5170: 
                   5171: inline void msdos_int_2eh()
                   5172: {
                   5173:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   5174:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     5175:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     5176:        char *token = my_strtok(tmp, " ");
                   5177:        strcpy(command, token);
                   5178:        strcpy(opt, token + strlen(token) + 1);
                   5179:        
                   5180:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   5181:        param->env_seg = 0;
                   5182:        param->cmd_line.w.l = 44;
                   5183:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   5184:        param->fcb1.w.l = 24;
                   5185:        param->fcb1.w.h = (WORK_TOP >> 4);
                   5186:        param->fcb2.w.l = 24;
                   5187:        param->fcb2.w.h = (WORK_TOP >> 4);
                   5188:        
                   5189:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   5190:        
                   5191:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   5192:        cmd_line->len = strlen(opt);
                   5193:        strcpy(cmd_line->cmd, opt);
                   5194:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   5195:        
                   5196:        msdos_process_exec(command, param, 0);
                   5197:        REG8(AL) = 0;
                   5198: }
                   5199: 
                   5200: inline void msdos_int_2fh_16h()
                   5201: {
                   5202:        switch(REG8(AL)) {
                   5203:        case 0x00:
                   5204:                {
                   5205:                        OSVERSIONINFO osvi;
                   5206:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
                   5207:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                   5208:                        GetVersionEx(&osvi);
                   5209:                        REG8(AL) = osvi.dwMajorVersion;
                   5210:                        REG8(AH) = osvi.dwMinorVersion;
                   5211:                }
                   5212:                break;
                   5213:        default:
                   5214:                REG16(AX) = 0x01;
1.1.1.3   root     5215:                m_CF = 1;
1.1       root     5216:                break;
                   5217:        }
                   5218: }
                   5219: 
                   5220: inline void msdos_int_2fh_1ah()
                   5221: {
                   5222:        switch(REG8(AL)) {
                   5223:        case 0x00:
                   5224:                // ansi.sys is installed
                   5225:                REG8(AL) = 0xff;
                   5226:                break;
                   5227:        default:
                   5228:                REG16(AX) = 0x01;
1.1.1.3   root     5229:                m_CF = 1;
1.1       root     5230:                break;
                   5231:        }
                   5232: }
                   5233: 
                   5234: inline void msdos_int_2fh_43h()
                   5235: {
                   5236:        switch(REG8(AL)) {
                   5237:        case 0x00:
                   5238:                // xms is not installed
                   5239:                REG8(AL) = 0;
                   5240:                break;
                   5241:        default:
                   5242:                REG16(AX) = 0x01;
1.1.1.3   root     5243:                m_CF = 1;
1.1       root     5244:                break;
                   5245:        }
                   5246: }
                   5247: 
                   5248: inline void msdos_int_2fh_4ah()
                   5249: {
                   5250:        switch(REG8(AL)) {
                   5251:        case 0x01:
                   5252:        case 0x02:
                   5253:                // hma is not installed
                   5254:                REG16(BX) = 0;
1.1.1.3   root     5255:                SREG(ES) = 0xffff;
                   5256:                i386_load_segment_descriptor(ES);
1.1       root     5257:                REG16(DI) = 0xffff;
                   5258:                break;
                   5259:        default:
                   5260:                REG16(AX) = 0x01;
1.1.1.3   root     5261:                m_CF = 1;
1.1       root     5262:                break;
                   5263:        }
                   5264: }
                   5265: 
                   5266: inline void msdos_int_2fh_4fh()
                   5267: {
                   5268:        switch(REG8(AL)) {
                   5269:        case 0x00:
                   5270:                REG16(AX) = 0;
                   5271:                REG8(DL) = 1;   // major version
                   5272:                REG8(DH) = 0;   // minor version
                   5273:                break;
                   5274:        case 0x01:
                   5275:                REG16(AX) = 0;
                   5276:                REG16(BX) = active_code_page;
                   5277:                break;
                   5278:        default:
                   5279:                REG16(AX) = 0x01;
1.1.1.3   root     5280:                m_CF = 1;
1.1       root     5281:                break;
                   5282:        }
                   5283: }
                   5284: 
                   5285: inline void msdos_int_2fh_aeh()
                   5286: {
                   5287:        switch(REG8(AL)) {
                   5288:        case 0x00:
                   5289:                REG8(AL) = 0;
                   5290:                break;
                   5291:        case 0x01:
                   5292:                {
                   5293:                        char command[MAX_PATH];
                   5294:                        memset(command, 0, sizeof(command));
1.1.1.3   root     5295:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     5296:                        
                   5297:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   5298:                        param->env_seg = 0;
                   5299:                        param->cmd_line.w.l = 44;
                   5300:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   5301:                        param->fcb1.w.l = 24;
                   5302:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   5303:                        param->fcb2.w.l = 24;
                   5304:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   5305:                        
                   5306:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   5307:                        
                   5308:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     5309:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   5310:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     5311:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   5312:                        
                   5313:                        if(msdos_process_exec(command, param, 0)) {
                   5314:                                REG16(AX) = 0x02;
1.1.1.3   root     5315:                                m_CF = 1;
1.1       root     5316:                        }
                   5317:                }
                   5318:                break;
                   5319:        default:
                   5320:                REG16(AX) = 0x01;
1.1.1.3   root     5321:                m_CF = 1;
1.1       root     5322:                break;
                   5323:        }
                   5324: }
                   5325: 
                   5326: inline void msdos_int_2fh_b7h()
                   5327: {
                   5328:        switch(REG8(AL)) {
                   5329:        case 0x00:
                   5330:                // append is not installed
                   5331:                REG8(AL) = 0;
                   5332:                break;
                   5333:        default:
                   5334:                REG16(AX) = 0x01;
1.1.1.3   root     5335:                m_CF = 1;
1.1       root     5336:                break;
                   5337:        }
                   5338: }
                   5339: 
                   5340: void msdos_syscall(unsigned num)
                   5341: {
                   5342:        switch(num) {
                   5343:        case 0x00:
                   5344:                error("division by zero\n");
                   5345:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   5346:                break;
                   5347:        case 0x04:
                   5348:                error("overflow\n");
                   5349:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   5350:                break;
                   5351:        case 0x06:
                   5352:                // NOTE: ish.com has illegal instruction...
                   5353: //             error("illegal instruction\n");
                   5354: //             msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   5355:                break;
1.1.1.8   root     5356:        case 0x08:
                   5357:        case 0x09:
                   5358:        case 0x0a:
                   5359:        case 0x0b:
                   5360:        case 0x0c:
                   5361:        case 0x0d:
                   5362:        case 0x0e:
                   5363:        case 0x0f:
                   5364:                // EOI
                   5365:                pic[0].isr &= ~(1 << (num - 0x08));
                   5366:                pic_update();
                   5367:                break;
1.1       root     5368:        case 0x10:
                   5369:                // PC BIOS - Video
                   5370:                if(scr_width != 80 || scr_height != 25) {
                   5371:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5372:                        SMALL_RECT rect;
                   5373:                        COORD co;
                   5374:                        
                   5375:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5376:                        if(csbi.dwCursorPosition.Y > 24) {
                   5377:                                SET_RECT(rect, 0, 0, scr_width - 1, scr_height - 1);
                   5378:                                ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   5379:                                for(int y = 0, y2 = csbi.dwCursorPosition.Y - 24; y < 25; y++, y2++) {
                   5380:                                        for(int x = 0; x < 80; x++) {
                   5381:                                                scr_buf[y][x] = scr_buf[y2][x];
                   5382:                                        }
                   5383:                                }
                   5384:                                WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
                   5385:                                
                   5386:                                co.X = csbi.dwCursorPosition.X;
                   5387:                                co.Y = 24;
                   5388:                                SetConsoleCursorPosition(hStdout, co);
                   5389:                                cursor_moved = true;
                   5390:                        }
                   5391:                        SET_RECT(rect, 0, 0, 79, 24);
                   5392:                        co.X = 80;
                   5393:                        co.Y = 25;
                   5394:                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   5395:                        SetConsoleScreenBufferSize(hStdout, co);
                   5396:                        scr_width = 80;
                   5397:                        scr_height = 25;
                   5398:                }
1.1.1.3   root     5399:                m_CF = 0;
1.1       root     5400:                switch(REG8(AH)) {
                   5401:                case 0x00: pcbios_int_10h_00h(); break;
                   5402:                case 0x01: pcbios_int_10h_01h(); break;
                   5403:                case 0x02: pcbios_int_10h_02h(); break;
                   5404:                case 0x03: pcbios_int_10h_03h(); break;
                   5405:                case 0x05: pcbios_int_10h_05h(); break;
                   5406:                case 0x06: pcbios_int_10h_06h(); break;
                   5407:                case 0x07: pcbios_int_10h_07h(); break;
                   5408:                case 0x08: pcbios_int_10h_08h(); break;
                   5409:                case 0x09: pcbios_int_10h_09h(); break;
                   5410:                case 0x0a: pcbios_int_10h_0ah(); break;
                   5411:                case 0x0b: break;
                   5412:                case 0x0c: break;
                   5413:                case 0x0d: break;
                   5414:                case 0x0e: pcbios_int_10h_0eh(); break;
                   5415:                case 0x0f: pcbios_int_10h_0fh(); break;
                   5416:                case 0x10: break;
                   5417:                case 0x11: break;
                   5418:                case 0x12: REG8(AL) = 0x00; break;
                   5419:                case 0x13: pcbios_int_10h_13h(); break;
                   5420:                case 0x18: REG8(AL) = 0x86; break;
                   5421:                case 0x1a: REG8(AL) = 0x00; break;
1.1.1.11! root     5422:                case 0x1b: break;
1.1       root     5423:                case 0x1c: REG8(AL) = 0x00; break;
                   5424:                case 0x1d: pcbios_int_10h_1dh(); break;
                   5425:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.11! root     5426:                case 0xef: REG16(DX) = 0xffff; break;
1.1       root     5427:                case 0xfe: pcbios_int_10h_feh(); break;
                   5428:                case 0xff: pcbios_int_10h_ffh(); break;
                   5429:                default:
                   5430:                        fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5431:                        break;
                   5432:                }
                   5433:                break;
                   5434:        case 0x11:
                   5435:                // PC BIOS - Get Equipment List
1.1.1.11! root     5436: #ifdef SUPPORT_FPU
        !          5437:                REG16(AX) = 0x22;
        !          5438: #else
1.1       root     5439:                REG16(AX) = 0x20;
1.1.1.11! root     5440: #endif
1.1       root     5441:                break;
                   5442:        case 0x12:
                   5443:                // PC BIOS - Get Memory Size
                   5444:                REG16(AX) = MEMORY_END / 1024;
                   5445:                break;
                   5446:        case 0x13:
                   5447:                // PC BIOS - Disk
                   5448: //             fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5449:                REG8(AH) = 0xff;
1.1.1.3   root     5450:                m_CF = 1;
1.1       root     5451:                break;
                   5452:        case 0x14:
                   5453:                // PC BIOS - Serial I/O
                   5454: //             fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5455:                REG8(AH) = 0xff;
1.1.1.3   root     5456:                m_CF = 1;
1.1       root     5457:                break;
                   5458:        case 0x15:
                   5459:                // PC BIOS
1.1.1.3   root     5460:                m_CF = 0;
1.1       root     5461:                switch(REG8(AH)) {
                   5462:                case 0x23: pcbios_int_15h_23h(); break;
                   5463:                case 0x24: pcbios_int_15h_24h(); break;
                   5464:                case 0x49: pcbios_int_15h_49h(); break;
                   5465:                case 0x86: pcbios_int_15h_86h(); break;
                   5466:                case 0x87: pcbios_int_15h_87h(); break;
                   5467:                case 0x88: pcbios_int_15h_88h(); break;
                   5468:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.3   root     5469: #if defined(HAS_I386)
1.1       root     5470:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     5471: #endif
1.1       root     5472:                case 0xca: pcbios_int_15h_cah(); break;
                   5473:                default:
                   5474: //                     fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5475:                        REG8(AH)=0x86;
1.1.1.3   root     5476:                        m_CF = 1;
1.1       root     5477:                        break;
                   5478:                }
                   5479:                break;
                   5480:        case 0x16:
                   5481:                // PC BIOS - Keyboard
1.1.1.3   root     5482:                m_CF = 0;
1.1       root     5483:                switch(REG8(AH)) {
                   5484:                case 0x00: pcbios_int_16h_00h(); break;
                   5485:                case 0x01: pcbios_int_16h_01h(); break;
                   5486:                case 0x02: pcbios_int_16h_02h(); break;
                   5487:                case 0x03: pcbios_int_16h_03h(); break;
                   5488:                case 0x05: pcbios_int_16h_05h(); break;
                   5489:                case 0x10: pcbios_int_16h_00h(); break;
                   5490:                case 0x11: pcbios_int_16h_01h(); break;
                   5491:                case 0x12: pcbios_int_16h_12h(); break;
                   5492:                case 0x13: pcbios_int_16h_13h(); break;
                   5493:                case 0x14: pcbios_int_16h_14h(); break;
                   5494:                default:
                   5495:                        fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5496:                        break;
                   5497:                }
                   5498:                break;
                   5499:        case 0x17:
                   5500:                // PC BIOS - Printer
                   5501:                fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5502:                break;
                   5503:        case 0x1a:
                   5504:                // PC BIOS - Timer
1.1.1.3   root     5505:                m_CF = 0;
1.1       root     5506:                switch(REG8(AH)) {
                   5507:                case 0x00: pcbios_int_1ah_00h(); break;
                   5508:                case 0x01: break;
                   5509:                case 0x02: pcbios_int_1ah_02h(); break;
                   5510:                case 0x03: break;
                   5511:                case 0x04: pcbios_int_1ah_04h(); break;
                   5512:                case 0x05: break;
                   5513:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   5514:                case 0x0b: break;
                   5515:                default:
                   5516:                        fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5517:                        break;
                   5518:                }
                   5519:                break;
                   5520:        case 0x20:
1.1.1.3   root     5521:                msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     5522:                break;
                   5523:        case 0x21:
                   5524:                // MS-DOS System Call
1.1.1.3   root     5525:                m_CF = 0;
1.1       root     5526:                switch(REG8(AH)) {
                   5527:                case 0x00: msdos_int_21h_00h(); break;
                   5528:                case 0x01: msdos_int_21h_01h(); break;
                   5529:                case 0x02: msdos_int_21h_02h(); break;
                   5530:                case 0x03: msdos_int_21h_03h(); break;
                   5531:                case 0x04: msdos_int_21h_04h(); break;
                   5532:                case 0x05: msdos_int_21h_05h(); break;
                   5533:                case 0x06: msdos_int_21h_06h(); break;
                   5534:                case 0x07: msdos_int_21h_07h(); break;
                   5535:                case 0x08: msdos_int_21h_08h(); break;
                   5536:                case 0x09: msdos_int_21h_09h(); break;
                   5537:                case 0x0a: msdos_int_21h_0ah(); break;
                   5538:                case 0x0b: msdos_int_21h_0bh(); break;
                   5539:                case 0x0c: msdos_int_21h_0ch(); break;
                   5540:                case 0x0d: msdos_int_21h_0dh(); break;
                   5541:                case 0x0e: msdos_int_21h_0eh(); break;
                   5542:                // 0x0f: open file with fcb
                   5543:                // 0x10: close file with fcb
                   5544:                case 0x11: msdos_int_21h_11h(); break;
                   5545:                case 0x12: msdos_int_21h_12h(); break;
                   5546:                case 0x13: msdos_int_21h_13h(); break;
                   5547:                // 0x14: sequential read with fcb
                   5548:                // 0x15: sequential write with fcb
                   5549:                // 0x16: create new file with fcb
                   5550:                // 0x17: rename file with fcb
                   5551:                case 0x18: msdos_int_21h_18h(); break;
                   5552:                case 0x19: msdos_int_21h_19h(); break;
                   5553:                case 0x1a: msdos_int_21h_1ah(); break;
                   5554:                case 0x1b: msdos_int_21h_1bh(); break;
                   5555:                case 0x1c: msdos_int_21h_1ch(); break;
                   5556:                case 0x1d: msdos_int_21h_1dh(); break;
                   5557:                case 0x1e: msdos_int_21h_1eh(); break;
                   5558:                case 0x1f: msdos_int_21h_1fh(); break;
                   5559:                case 0x20: msdos_int_21h_20h(); break;
                   5560:                // 0x21: random read with fcb
                   5561:                // 0x22: randome write with fcb
                   5562:                // 0x23: get file size with fcb
                   5563:                // 0x24: set relative record field with fcb
                   5564:                case 0x25: msdos_int_21h_25h(); break;
                   5565:                case 0x26: msdos_int_21h_26h(); break;
                   5566:                // 0x27: random block read with fcb
                   5567:                // 0x28: random block write with fcb
                   5568:                case 0x29: msdos_int_21h_29h(); break;
                   5569:                case 0x2a: msdos_int_21h_2ah(); break;
                   5570:                case 0x2b: msdos_int_21h_2bh(); break;
                   5571:                case 0x2c: msdos_int_21h_2ch(); break;
                   5572:                case 0x2d: msdos_int_21h_2dh(); break;
                   5573:                case 0x2e: msdos_int_21h_2eh(); break;
                   5574:                case 0x2f: msdos_int_21h_2fh(); break;
                   5575:                case 0x30: msdos_int_21h_30h(); break;
                   5576:                case 0x31: msdos_int_21h_31h(); break;
                   5577:                case 0x32: msdos_int_21h_32h(); break;
                   5578:                case 0x33: msdos_int_21h_33h(); break;
                   5579:                // 0x34: get address of indos flag
                   5580:                case 0x35: msdos_int_21h_35h(); break;
                   5581:                case 0x36: msdos_int_21h_36h(); break;
                   5582:                case 0x37: msdos_int_21h_37h(); break;
                   5583:                // 0x38: get country-specific information
                   5584:                case 0x39: msdos_int_21h_39h(0); break;
                   5585:                case 0x3a: msdos_int_21h_3ah(0); break;
                   5586:                case 0x3b: msdos_int_21h_3bh(0); break;
                   5587:                case 0x3c: msdos_int_21h_3ch(); break;
                   5588:                case 0x3d: msdos_int_21h_3dh(); break;
                   5589:                case 0x3e: msdos_int_21h_3eh(); break;
                   5590:                case 0x3f: msdos_int_21h_3fh(); break;
                   5591:                case 0x40: msdos_int_21h_40h(); break;
                   5592:                case 0x41: msdos_int_21h_41h(0); break;
                   5593:                case 0x42: msdos_int_21h_42h(); break;
                   5594:                case 0x43: msdos_int_21h_43h(0); break;
                   5595:                case 0x44: msdos_int_21h_44h(); break;
                   5596:                case 0x45: msdos_int_21h_45h(); break;
                   5597:                case 0x46: msdos_int_21h_46h(); break;
                   5598:                case 0x47: msdos_int_21h_47h(0); break;
                   5599:                case 0x48: msdos_int_21h_48h(); break;
                   5600:                case 0x49: msdos_int_21h_49h(); break;
                   5601:                case 0x4a: msdos_int_21h_4ah(); break;
                   5602:                case 0x4b: msdos_int_21h_4bh(); break;
                   5603:                case 0x4c: msdos_int_21h_4ch(); break;
                   5604:                case 0x4d: msdos_int_21h_4dh(); break;
                   5605:                case 0x4e: msdos_int_21h_4eh(); break;
                   5606:                case 0x4f: msdos_int_21h_4fh(); break;
                   5607:                case 0x50: msdos_int_21h_50h(); break;
                   5608:                case 0x51: msdos_int_21h_51h(); break;
                   5609:                case 0x52: msdos_int_21h_52h(); break;
                   5610:                // 0x53: translate bios parameter block to drive param bock
                   5611:                case 0x54: msdos_int_21h_54h(); break;
                   5612:                case 0x55: msdos_int_21h_55h(); break;
                   5613:                case 0x56: msdos_int_21h_56h(0); break;
                   5614:                case 0x57: msdos_int_21h_57h(); break;
                   5615:                case 0x58: msdos_int_21h_58h(); break;
                   5616:                case 0x59: msdos_int_21h_59h(); break;
                   5617:                case 0x5a: msdos_int_21h_5ah(); break;
                   5618:                case 0x5b: msdos_int_21h_5bh(); break;
                   5619:                case 0x5c: msdos_int_21h_5ch(); break;
                   5620:                // 0x5e: ms-network
                   5621:                // 0x5f: ms-network
                   5622:                case 0x60: msdos_int_21h_60h(0); break;
                   5623:                case 0x61: msdos_int_21h_61h(); break;
                   5624:                case 0x62: msdos_int_21h_62h(); break;
                   5625:                case 0x63: msdos_int_21h_63h(); break;
                   5626:                // 0x64: set device driver lockahead flag
                   5627:                case 0x65: msdos_int_21h_65h(); break;
                   5628:                case 0x66: msdos_int_21h_66h(); break;
                   5629:                case 0x67: msdos_int_21h_67h(); break;
                   5630:                case 0x68: msdos_int_21h_68h(); break;
                   5631:                case 0x69: msdos_int_21h_69h(); break;
                   5632:                case 0x6a: msdos_int_21h_6ah(); break;
                   5633:                case 0x6b: msdos_int_21h_6bh(); break;
                   5634:                case 0x6c: msdos_int_21h_6ch(0); break;
                   5635:                // 0x6d: find first rom program
                   5636:                // 0x6e: find next rom program
                   5637:                // 0x6f: get/set rom scan start address
                   5638:                // 0x70: windows95 get/set internationalization information
                   5639:                case 0x71:
                   5640:                        // windows95 long filename functions
                   5641:                        switch(REG8(AL)) {
                   5642:                        case 0x0d: msdos_int_21h_710dh(); break;
                   5643:                        case 0x39: msdos_int_21h_39h(1); break;
                   5644:                        case 0x3a: msdos_int_21h_3ah(1); break;
                   5645:                        case 0x3b: msdos_int_21h_3bh(1); break;
                   5646:                        case 0x41: msdos_int_21h_41h(1); break;
                   5647:                        case 0x43: msdos_int_21h_43h(1); break;
                   5648:                        case 0x47: msdos_int_21h_47h(1); break;
                   5649:                        case 0x4e: msdos_int_21h_714eh(); break;
                   5650:                        case 0x4f: msdos_int_21h_714fh(); break;
                   5651:                        case 0x56: msdos_int_21h_56h(1); break;
                   5652:                        case 0x60: msdos_int_21h_60h(1); break;
                   5653:                        case 0x6c: msdos_int_21h_6ch(1); break;
                   5654:                        case 0xa0: msdos_int_21h_71a0h(); break;
                   5655:                        case 0xa1: msdos_int_21h_71a1h(); break;
                   5656:                        case 0xa6: msdos_int_21h_71a6h(); break;
                   5657:                        case 0xa7: msdos_int_21h_71a7h(); break;
                   5658:                        case 0xa8: msdos_int_21h_71a8h(); break;
                   5659:                        // 0xa9: server create/open file
                   5660:                        // 0xaa: create/terminate SUBST
                   5661:                        default:
                   5662: //                             fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5663:                                REG16(AX) = 0x7100;
1.1.1.3   root     5664:                                m_CF = 1;
1.1       root     5665:                                break;
                   5666:                        }
                   5667:                        break;
                   5668:                // 0x72: Windows95 beta - LFN FindClose
                   5669:                case 0x73:
                   5670:                        // windows95 fat32 functions
                   5671:                        switch(REG8(AL)) {
                   5672:                        // 0x00: drive locking ???
                   5673:                        // 0x01: drive locking ???
                   5674:                        // 0x02: get extended dpb
                   5675:                        case 0x03: msdos_int_21h_7303h(); break;
                   5676:                        // 0x04: set dpb to use for formatting
                   5677:                        default:
                   5678: //                             fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5679:                                REG16(AX) = 0x7300;
1.1.1.3   root     5680:                                m_CF = 1;
1.1       root     5681:                                break;
                   5682:                        }
                   5683:                        break;
                   5684:                default:
                   5685: //                     fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5686:                        REG16(AX) = 0x01;
1.1.1.3   root     5687:                        m_CF = 1;
1.1       root     5688:                        break;
                   5689:                }
1.1.1.3   root     5690:                if(m_CF) {
1.1       root     5691:                        error_code = REG16(AX);
                   5692:                }
                   5693:                break;
                   5694:        case 0x22:
                   5695:                fatalerror("int 22h (terminate address)\n");
                   5696:        case 0x23:
                   5697:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   5698:                break;
                   5699:        case 0x24:
                   5700:                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   5701:                break;
                   5702:        case 0x25:
                   5703:                msdos_int_25h();
                   5704:                break;
                   5705:        case 0x26:
                   5706:                msdos_int_26h();
                   5707:                break;
                   5708:        case 0x27:
                   5709:                msdos_int_27h();
                   5710:                break;
                   5711:        case 0x28:
                   5712:                Sleep(10);
                   5713:                break;
                   5714:        case 0x29:
                   5715:                msdos_int_29h();
                   5716:                break;
                   5717:        case 0x2e:
                   5718:                msdos_int_2eh();
                   5719:                break;
                   5720:        case 0x2f:
                   5721:                // multiplex interrupt
1.1.1.3   root     5722:                m_CF = 0;
1.1       root     5723:                switch(REG8(AH)) {
                   5724:                case 0x16: msdos_int_2fh_16h(); break;
                   5725:                case 0x1a: msdos_int_2fh_1ah(); break;
                   5726:                case 0x43: msdos_int_2fh_43h(); break;
                   5727:                case 0x4a: msdos_int_2fh_4ah(); break;
                   5728:                case 0x4f: msdos_int_2fh_4fh(); break;
                   5729:                case 0xae: msdos_int_2fh_aeh(); break;
                   5730:                case 0xb7: msdos_int_2fh_b7h(); break;
                   5731:                default:
                   5732: //                     fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5733:                        REG16(AX) = 0x01; // ???
1.1.1.3   root     5734:                        m_CF = 1;
1.1       root     5735:                        break;
                   5736:                }
1.1.1.3   root     5737:                if(m_CF) {
1.1       root     5738:                        error_code = REG16(AX);
                   5739:                }
                   5740:                break;
1.1.1.8   root     5741:        case 0x70:
                   5742:        case 0x71:
                   5743:        case 0x72:
                   5744:        case 0x73:
                   5745:        case 0x74:
                   5746:        case 0x75:
                   5747:        case 0x76:
                   5748:        case 0x77:
                   5749:                // EOI
                   5750:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   5751:                        pic[0].isr &= ~(1 << 2); // master
                   5752:                }
                   5753:                pic_update();
                   5754:                break;
1.1       root     5755:        default:
                   5756: //             fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
                   5757:                break;
                   5758:        }
                   5759:        
                   5760:        // update cursor position
                   5761:        if(cursor_moved) {
                   5762:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5763:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5764:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   5765:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y;
                   5766:                cursor_moved = false;
                   5767:        }
                   5768: }
                   5769: 
                   5770: // init
                   5771: 
                   5772: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   5773: {
                   5774:        // init file handler
                   5775:        memset(file_handler, 0, sizeof(file_handler));
                   5776:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5777:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5778:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5779: #ifdef SUPPORT_AUX_PRN
                   5780:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   5781:                msdos_file_handler_open(3, 0, 2, 0x80c0, 0);
                   5782:        }
                   5783:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
                   5784:                msdos_file_handler_open(4, 0, 1, 0xa8c0, 0);
                   5785:        }
                   5786: #endif
                   5787:        _dup2(0, DUP_STDIN);
                   5788:        _dup2(1, DUP_STDOUT);
                   5789:        _dup2(2, DUP_STDERR);
                   5790:        
                   5791:        // init process
                   5792:        memset(process, 0, sizeof(process));
                   5793:        
                   5794:        // init memory
                   5795:        memset(mem, 0, sizeof(mem));
                   5796:        for(int i = 0; i < 0x100; i++) {
                   5797:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   5798:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   5799:        }
                   5800:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0xfff0;
                   5801:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xf000;
                   5802:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE);
                   5803:        
                   5804:        // bios data area
                   5805:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5806:        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5807:        
                   5808:        *(UINT8  *)(mem + 0x411) = 0x20;
                   5809:        *(UINT16 *)(mem + 0x410) = 0x20;
                   5810:        *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
                   5811:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
                   5812:        *(UINT16 *)(mem + 0x44a) = 80;
                   5813:        *(UINT16 *)(mem + 0x44c) = 4096;
                   5814:        *(UINT16 *)(mem + 0x44e) = 0;
                   5815:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
                   5816:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y;
                   5817:        *(UINT8  *)(mem + 0x460) = 7;
                   5818:        *(UINT8  *)(mem + 0x461) = 7;
                   5819:        *(UINT8  *)(mem + 0x462) = 0;
                   5820:        *(UINT16 *)(mem + 0x463) = 0x3d4;
                   5821:        *(UINT8  *)(mem + 0x484) = 24;
                   5822:        *(UINT8  *)(mem + 0x485) = 19;
                   5823:        *(UINT8  *)(mem + 0x487) = 0;   // is this okay?
                   5824:        
                   5825:        // dos info
                   5826:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   5827:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   5828:        dos_info->first_dpb.w.l = 0;
                   5829:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   5830:        dos_info->first_sft.w.l = 0;
                   5831:        dos_info->first_sft.w.h = FILE_TABLE_TOP >> 4;
                   5832:        dos_info->cds.w.l = 0;
                   5833:        dos_info->cds.w.h = CDS_TOP >> 4;
                   5834:        dos_info->fcb_table.w.l = 0;
                   5835:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   5836:        dos_info->last_drive = 'Z' - 'A' + 1;
                   5837:        dos_info->buffers_x = 20;
                   5838:        dos_info->buffers_y = 0;
                   5839:        char *env;
                   5840:        if((env = getenv("LASTDRIVE")) != NULL) {
                   5841:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   5842:                        dos_info->last_drive = env[0] - 'A' + 1;
                   5843:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   5844:                        dos_info->last_drive = env[0] - 'a' + 1;
                   5845:                }
                   5846:        }
                   5847:        if((env = getenv("windir")) != NULL) {
                   5848:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   5849:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   5850:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   5851:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   5852:                }
                   5853:        }
1.1.1.3   root     5854: #if defined(HAS_I386)
1.1       root     5855:        dos_info->i386_or_later = 1;
1.1.1.3   root     5856: #else
                   5857:        dos_info->i386_or_later = 0;
                   5858: #endif
1.1       root     5859:        dos_info->ext_mem_size = (MAX_MEM - 0x100000) >> 10;
                   5860:        
                   5861:        // environment
                   5862:        int seg = MEMORY_TOP >> 4;
                   5863:        msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
                   5864:        int env_seg = seg;
                   5865:        int ofs = 0;
1.1.1.9   root     5866:        char env_path[4096];
1.1       root     5867:        
                   5868:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1.1.9   root     5869:                if(_strnicmp(*p, "PATH=", 5) == 0) {
                   5870:                        strcpy(env_path, *p + 5);
                   5871:                        break;
                   5872:                }
                   5873:        }
                   5874:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     5875:                // lower to upper
                   5876:                char tmp[ENV_SIZE], name[ENV_SIZE], value[ENV_SIZE];
                   5877:                int value_pos = 0;
                   5878:                strcpy(tmp, *p);
                   5879:                for(int i = 0;; i++) {
                   5880:                        if(tmp[i] == '=') {
                   5881:                                tmp[i] = '\0';
                   5882:                                sprintf(name, ";%s;", tmp);
                   5883:                                strcpy(value, tmp + (value_pos = i + 1));
                   5884:                                tmp[i] = '=';
                   5885:                                break;
                   5886:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
                   5887:                                tmp[i] = tmp[i] - 'a' + 'A';
                   5888:                        }
                   5889:                }
                   5890:                if(!(standard_env && strstr(";COMSPEC;INCLUDE;LIB;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL)) {
                   5891:                        if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.9   root     5892:                                char *path = msdos_search_command_com(argv[0], env_path), tmp_path[MAX_PATH];
                   5893:                                if(path == NULL) {
                   5894:                                        path = msdos_remove_double_quote(tmp + 8);
1.1       root     5895:                                }
1.1.1.8   root     5896:                                GetShortPathName(path, tmp_path, MAX_PATH);
                   5897:                                my_strupr(tmp_path);
                   5898:                                sprintf(tmp, "COMSPEC=%s", tmp_path);
1.1       root     5899:                        } else if(strncmp(tmp, "PATH=", 5) == 0 || strncmp(tmp, "TEMP=", 5) == 0 || strncmp(tmp, "TMP=", 4) == 0) {
                   5900:                                tmp[value_pos] = '\0';
                   5901:                                char *token = my_strtok(value, ";");
                   5902:                                while(token != NULL) {
                   5903:                                        if(strlen(token) != 0) {
1.1.1.8   root     5904:                                                char *path = msdos_remove_double_quote(token), tmp_path[MAX_PATH];
                   5905:                                                if(strlen(path) != 0) {
                   5906:                                                        GetShortPathName(path, tmp_path, MAX_PATH);
                   5907:                                                        strcat(tmp, tmp_path);
                   5908:                                                        strcat(tmp, ";");
1.1       root     5909:                                                }
                   5910:                                        }
                   5911:                                        token = my_strtok(NULL, ";");
                   5912:                                }
                   5913:                                tmp[strlen(tmp) - 1] = '\0';
                   5914:                                my_strupr(tmp);
                   5915:                        }
                   5916:                        int len = strlen(tmp);
                   5917:                        if (ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
                   5918:                                fatalerror("too many environments\n");
                   5919:                        }
                   5920:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   5921:                        ofs += len + 1;
                   5922:                }
                   5923:        }
                   5924:        seg += (ENV_SIZE >> 4);
                   5925:        
                   5926:        // psp
                   5927:        msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
                   5928:        current_psp = seg;
                   5929:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   5930:        seg += (PSP_SIZE >> 4);
                   5931:        
1.1.1.8   root     5932:        // first mcb in conventional memory
1.1       root     5933:        msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 1);
1.1.1.8   root     5934:        first_mcb = seg;
                   5935:        
                   5936:        // first mcb in upper memory block
                   5937:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     5938:        
                   5939:        // boot
                   5940:        mem[0xffff0] = 0xf4;    // halt
                   5941:        mem[0xffff1] = 0xcd;    // int 21h
                   5942:        mem[0xffff2] = 0x21;
                   5943:        mem[0xffff3] = 0xcb;    // retf
                   5944:        
                   5945:        // param block
                   5946:        // + 0: param block (22bytes)
                   5947:        // +24: fcb1/2 (20bytes)
                   5948:        // +44: command tail (128bytes)
                   5949:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   5950:        param->env_seg = 0;
                   5951:        param->cmd_line.w.l = 44;
                   5952:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   5953:        param->fcb1.w.l = 24;
                   5954:        param->fcb1.w.h = (WORK_TOP >> 4);
                   5955:        param->fcb2.w.l = 24;
                   5956:        param->fcb2.w.h = (WORK_TOP >> 4);
                   5957:        
                   5958:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   5959:        
                   5960:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   5961:        if(argc > 1) {
                   5962:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   5963:                for(int i = 2; i < argc; i++) {
                   5964:                        char tmp[128];
                   5965:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   5966:                        strcpy(cmd_line->cmd, tmp);
                   5967:                }
                   5968:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   5969:        } else {
                   5970:                cmd_line->len = 0;
                   5971:        }
                   5972:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   5973:        
                   5974:        // system file table
                   5975:        *(UINT16 *)(mem + FILE_TABLE_TOP +  0) = 6;
                   5976:        *(UINT16 *)(mem + FILE_TABLE_TOP +  2) = FILE_TABLE_TOP >> 4;
                   5977:        *(UINT16 *)(mem + FILE_TABLE_TOP +  4) = 100;
                   5978:        *(UINT32 *)(mem + FILE_TABLE_TOP +  6) = 0xffffffff;
                   5979:        *(UINT16 *)(mem + FILE_TABLE_TOP + 10) = 100;
                   5980:        
                   5981:        // current directory structure
                   5982:        msdos_cds_update(_getdrive() - 1);
                   5983:        
                   5984:        // fcb table
                   5985:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
                   5986:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 100;
                   5987:        
                   5988:        // dbcs table
                   5989:        msdos_dbcs_table_init();
                   5990:        
                   5991:        // execute command
                   5992:        if(msdos_process_exec(argv[0], param, 0)) {
                   5993:                fatalerror("'%s' not found\n", argv[0]);
                   5994:        }
                   5995:        retval = 0;
                   5996:        return(0);
                   5997: }
                   5998: 
                   5999: #define remove_std_file(path) { \
                   6000:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   6001:        if(fd != -1) { \
                   6002:                _lseek(fd, 0, SEEK_END); \
                   6003:                int size = _tell(fd); \
                   6004:                _close(fd); \
                   6005:                if(size == 0) { \
                   6006:                        remove(path); \
                   6007:                } \
                   6008:        } \
                   6009: }
                   6010: 
                   6011: void msdos_finish()
                   6012: {
                   6013:        for(int i = 0; i < MAX_FILES; i++) {
                   6014:                if(file_handler[i].valid) {
                   6015:                        _close(i);
                   6016:                }
                   6017:        }
                   6018: #ifdef SUPPORT_AUX_PRN
                   6019:        remove_std_file("stdaux.txt");
                   6020:        remove_std_file("stdprn.txt");
                   6021: #endif
                   6022:        msdos_dbcs_table_finish();
                   6023: }
                   6024: 
                   6025: /* ----------------------------------------------------------------------------
                   6026:        PC/AT hardware emulation
                   6027: ---------------------------------------------------------------------------- */
                   6028: 
                   6029: void hardware_init()
                   6030: {
1.1.1.3   root     6031:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     6032:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.3   root     6033: #if defined(HAS_I386)
1.1       root     6034:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   6035:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     6036: #endif
                   6037:        i386_set_a20_line(0);
1.1       root     6038:        pic_init();
1.1.1.8   root     6039: #ifdef PIT_ALWAYS_RUNNING
                   6040:        pit_init();
                   6041: #else
1.1       root     6042:        pit_active = 0;
                   6043: #endif
1.1.1.8   root     6044:        cmos_init();
                   6045:        kbd_init();
1.1       root     6046: }
                   6047: 
1.1.1.10  root     6048: void hardware_finish()
                   6049: {
                   6050: #if defined(HAS_I386)
                   6051:        vtlb_free(m_vtlb);
                   6052: #endif
                   6053: }
                   6054: 
1.1       root     6055: void hardware_run()
                   6056: {
                   6057:        int ops = 0;
                   6058:        
1.1.1.3   root     6059:        while(!m_halted) {
1.1       root     6060: #ifdef SUPPORT_DISASSEMBLER
                   6061:                if(dasm) {
                   6062:                        char buffer[256];
1.1.1.3   root     6063: #if defined(HAS_I386)
                   6064:                        UINT64 eip = m_eip;
                   6065: #else
                   6066:                        UINT64 eip = m_pc - SREG_BASE(CS);
                   6067: #endif
                   6068:                        UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1       root     6069:                        
1.1.1.3   root     6070: #if defined(HAS_I386)
                   6071:                        if(m_operand_size) {
1.1       root     6072:                                CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3   root     6073:                        } else
                   6074: #endif
                   6075:                        CPU_DISASSEMBLE_CALL(x86_16);
                   6076:                        fprintf(stderr, "%04x:%04x\t%s\n", SREG(CS), eip, buffer);
1.1       root     6077:                }
                   6078: #endif
1.1.1.3   root     6079: #if defined(HAS_I386)
                   6080:                m_cycles = 1;
1.1       root     6081:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     6082: #else
                   6083:                CPU_EXECUTE_CALL(CPU_MODEL);
                   6084: #endif
1.1.1.8   root     6085:                if(++ops == 16384) {
1.1       root     6086:                        hardware_update();
                   6087:                        ops = 0;
                   6088:                }
                   6089:        }
                   6090: }
                   6091: 
                   6092: void hardware_update()
                   6093: {
1.1.1.8   root     6094:        static UINT32 prev_time = 0;
                   6095:        UINT32 cur_time = timeGetTime();
                   6096:        
                   6097:        if(prev_time != cur_time) {
                   6098:                // update pit and raise irq0
                   6099: #ifndef PIT_ALWAYS_RUNNING
                   6100:                if(pit_active)
                   6101: #endif
                   6102:                {
                   6103:                        if(pit_run(0, cur_time)) {
                   6104:                                pic_req(0, 0, 1);
                   6105:                        }
                   6106:                        pit_run(1, cur_time);
                   6107:                        pit_run(2, cur_time);
                   6108:                }
                   6109:                // check key input and raise irq1
                   6110:                static UINT32 prev_100ms = 0;
                   6111:                UINT32 cur_100ms = cur_time / 100;
                   6112:                if(prev_100ms != cur_100ms) {
                   6113:                        if(check_key_input()) {
                   6114:                                pic_req(0, 1, 1);
                   6115:                        }
                   6116:                        prev_100ms = cur_100ms;
                   6117:                }
                   6118:                prev_time = cur_time;
1.1       root     6119:        }
                   6120: }
                   6121: 
                   6122: // pic
                   6123: 
                   6124: void pic_init()
                   6125: {
1.1.1.8   root     6126:        memset(pic, 0, sizeof(pic));
                   6127:        pic[0].imr = pic[1].imr = 0xff;
1.1       root     6128:        
                   6129:        // from bochs bios
                   6130:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   6131:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   6132:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   6133:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   6134:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   6135:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   6136:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   6137:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   6138:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   6139: }
                   6140: 
                   6141: void pic_write(int c, UINT32 addr, UINT8 data)
                   6142: {
                   6143:        if(addr & 1) {
                   6144:                if(pic[c].icw2_r) {
                   6145:                        // icw2
                   6146:                        pic[c].icw2 = data;
                   6147:                        pic[c].icw2_r = 0;
                   6148:                } else if(pic[c].icw3_r) {
                   6149:                        // icw3
                   6150:                        pic[c].icw3 = data;
                   6151:                        pic[c].icw3_r = 0;
                   6152:                } else if(pic[c].icw4_r) {
                   6153:                        // icw4
                   6154:                        pic[c].icw4 = data;
                   6155:                        pic[c].icw4_r = 0;
                   6156:                } else {
                   6157:                        // ocw1
                   6158:                        pic[c].imr = data;
                   6159:                }
                   6160:        } else {
                   6161:                if(data & 0x10) {
                   6162:                        // icw1
                   6163:                        pic[c].icw1 = data;
                   6164:                        pic[c].icw2_r = 1;
                   6165:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   6166:                        pic[c].icw4_r = data & 1;
                   6167:                        pic[c].irr = 0;
                   6168:                        pic[c].isr = 0;
                   6169:                        pic[c].imr = 0;
                   6170:                        pic[c].prio = 0;
                   6171:                        if(!(pic[c].icw1 & 1)) {
                   6172:                                pic[c].icw4 = 0;
                   6173:                        }
                   6174:                        pic[c].ocw3 = 0;
                   6175:                } else if(data & 8) {
                   6176:                        // ocw3
                   6177:                        if(!(data & 2)) {
                   6178:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   6179:                        }
                   6180:                        if(!(data & 0x40)) {
                   6181:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   6182:                        }
                   6183:                        pic[c].ocw3 = data;
                   6184:                } else {
                   6185:                        // ocw2
                   6186:                        int level = 0;
                   6187:                        if(data & 0x40) {
                   6188:                                level = data & 7;
                   6189:                        } else {
                   6190:                                if(!pic[c].isr) {
                   6191:                                        return;
                   6192:                                }
                   6193:                                level = pic[c].prio;
                   6194:                                while(!(pic[c].isr & (1 << level))) {
                   6195:                                        level = (level + 1) & 7;
                   6196:                                }
                   6197:                        }
                   6198:                        if(data & 0x80) {
                   6199:                                pic[c].prio = (level + 1) & 7;
                   6200:                        }
                   6201:                        if(data & 0x20) {
                   6202:                                pic[c].isr &= ~(1 << level);
                   6203:                        }
                   6204:                }
                   6205:        }
                   6206:        pic_update();
                   6207: }
                   6208: 
                   6209: UINT8 pic_read(int c, UINT32 addr)
                   6210: {
                   6211:        if(addr & 1) {
                   6212:                return(pic[c].imr);
                   6213:        } else {
                   6214:                // polling mode is not supported...
                   6215:                //if(pic[c].ocw3 & 4) {
                   6216:                //      return ???;
                   6217:                //}
                   6218:                if(pic[c].ocw3 & 1) {
                   6219:                        return(pic[c].isr);
                   6220:                } else {
                   6221:                        return(pic[c].irr);
                   6222:                }
                   6223:        }
                   6224: }
                   6225: 
                   6226: void pic_req(int c, int level, int signal)
                   6227: {
                   6228:        if(signal) {
                   6229:                pic[c].irr |= (1 << level);
                   6230:        } else {
                   6231:                pic[c].irr &= ~(1 << level);
                   6232:        }
                   6233:        pic_update();
                   6234: }
                   6235: 
                   6236: int pic_ack()
                   6237: {
                   6238:        // ack (INTA=L)
                   6239:        pic[pic_req_chip].isr |= pic_req_bit;
                   6240:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   6241:        if(pic_req_chip > 0) {
                   6242:                // update isr and irr of master
                   6243:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   6244:                pic[pic_req_chip - 1].isr |= slave;
                   6245:                pic[pic_req_chip - 1].irr &= ~slave;
                   6246:        }
                   6247:        //if(pic[pic_req_chip].icw4 & 1) {
                   6248:                // 8086 mode
                   6249:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   6250:        //} else {
                   6251:        //      // 8080 mode
                   6252:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   6253:        //      if(pic[pic_req_chip].icw1 & 4) {
                   6254:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   6255:        //      } else {
                   6256:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   6257:        //      }
                   6258:        //      vector = 0xcd | (addr << 8);
                   6259:        //}
                   6260:        if(pic[pic_req_chip].icw4 & 2) {
                   6261:                // auto eoi
                   6262:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   6263:        }
                   6264:        return(vector);
                   6265: }
                   6266: 
                   6267: void pic_update()
                   6268: {
                   6269:        for(int c = 0; c < 2; c++) {
                   6270:                UINT8 irr = pic[c].irr;
                   6271:                if(c + 1 < 2) {
                   6272:                        // this is master
                   6273:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   6274:                                // request from slave
                   6275:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   6276:                        }
                   6277:                }
                   6278:                irr &= (~pic[c].imr);
                   6279:                if(!irr) {
                   6280:                        break;
                   6281:                }
                   6282:                if(!(pic[c].ocw3 & 0x20)) {
                   6283:                        irr |= pic[c].isr;
                   6284:                }
                   6285:                int level = pic[c].prio;
                   6286:                UINT8 bit = 1 << level;
                   6287:                while(!(irr & bit)) {
                   6288:                        level = (level + 1) & 7;
                   6289:                        bit = 1 << level;
                   6290:                }
                   6291:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   6292:                        // check slave
                   6293:                        continue;
                   6294:                }
                   6295:                if(pic[c].isr & bit) {
                   6296:                        break;
                   6297:                }
                   6298:                // interrupt request
                   6299:                pic_req_chip = c;
                   6300:                pic_req_level = level;
                   6301:                pic_req_bit = bit;
1.1.1.3   root     6302:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     6303:                return;
                   6304:        }
1.1.1.3   root     6305:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     6306: }
1.1       root     6307: 
                   6308: // pit
                   6309: 
                   6310: #define PIT_FREQ 1193182
                   6311: #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)
                   6312: 
                   6313: void pit_init()
                   6314: {
1.1.1.8   root     6315:        memset(pit, 0, sizeof(pit));
1.1       root     6316:        for(int ch = 0; ch < 3; ch++) {
                   6317:                pit[ch].count = 0x10000;
                   6318:                pit[ch].ctrl_reg = 0x34;
                   6319:                pit[ch].mode = 3;
                   6320:        }
                   6321:        
                   6322:        // from bochs bios
                   6323:        pit_write(3, 0x34);
                   6324:        pit_write(0, 0x00);
                   6325:        pit_write(0, 0x00);
                   6326: }
                   6327: 
                   6328: void pit_write(int ch, UINT8 val)
                   6329: {
1.1.1.8   root     6330: #ifndef PIT_ALWAYS_RUNNING
1.1       root     6331:        if(!pit_active) {
                   6332:                pit_active = 1;
                   6333:                pit_init();
                   6334:        }
1.1.1.8   root     6335: #endif
1.1       root     6336:        switch(ch) {
                   6337:        case 0:
                   6338:        case 1:
                   6339:        case 2:
                   6340:                // write count register
                   6341:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   6342:                        if(pit[ch].ctrl_reg & 0x10) {
                   6343:                                pit[ch].low_write = 1;
                   6344:                        }
                   6345:                        if(pit[ch].ctrl_reg & 0x20) {
                   6346:                                pit[ch].high_write = 1;
                   6347:                        }
                   6348:                }
                   6349:                if(pit[ch].low_write) {
                   6350:                        pit[ch].count_reg = val;
                   6351:                        pit[ch].low_write = 0;
                   6352:                } else if(pit[ch].high_write) {
                   6353:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   6354:                                pit[ch].count_reg = val << 8;
                   6355:                        } else {
                   6356:                                pit[ch].count_reg |= val << 8;
                   6357:                        }
                   6358:                        pit[ch].high_write = 0;
                   6359:                }
                   6360:                // start count
1.1.1.8   root     6361:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   6362:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   6363:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   6364:                                pit[ch].prev_time = timeGetTime();
                   6365:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     6366:                        }
                   6367:                }
                   6368:                break;
                   6369:        case 3: // ctrl reg
                   6370:                if((val & 0xc0) == 0xc0) {
                   6371:                        // i8254 read-back command
                   6372:                        for(ch = 0; ch < 3; ch++) {
                   6373:                                UINT8 bit = 2 << ch;
                   6374:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   6375:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   6376:                                        pit[ch].status_latched = 1;
                   6377:                                }
                   6378:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   6379:                                        pit_latch_count(ch);
                   6380:                                }
                   6381:                        }
                   6382:                        break;
                   6383:                }
                   6384:                ch = (val >> 6) & 3;
                   6385:                if(val & 0x30) {
                   6386:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   6387:                        pit[ch].mode = modes[(val >> 1) & 7];
                   6388:                        pit[ch].count_latched = 0;
                   6389:                        pit[ch].low_read = pit[ch].high_read = 0;
                   6390:                        pit[ch].low_write = pit[ch].high_write = 0;
                   6391:                        pit[ch].ctrl_reg = val;
                   6392:                        // stop count
1.1.1.8   root     6393:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     6394:                        pit[ch].count_reg = 0;
                   6395:                } else if(!pit[ch].count_latched) {
                   6396:                        pit_latch_count(ch);
                   6397:                }
                   6398:                break;
                   6399:        }
                   6400: }
                   6401: 
                   6402: UINT8 pit_read(int ch)
                   6403: {
1.1.1.8   root     6404: #ifndef PIT_ALWAYS_RUNNING
1.1       root     6405:        if(!pit_active) {
                   6406:                pit_active = 1;
                   6407:                pit_init();
                   6408:        }
1.1.1.8   root     6409: #endif
1.1       root     6410:        switch(ch) {
                   6411:        case 0:
                   6412:        case 1:
                   6413:        case 2:
                   6414:                if(pit[ch].status_latched) {
                   6415:                        pit[ch].status_latched = 0;
                   6416:                        return(pit[ch].status);
                   6417:                }
                   6418:                // if not latched, through current count
                   6419:                if(!pit[ch].count_latched) {
                   6420:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   6421:                                pit_latch_count(ch);
                   6422:                        }
                   6423:                }
                   6424:                // return latched count
                   6425:                if(pit[ch].low_read) {
                   6426:                        pit[ch].low_read = 0;
                   6427:                        if(!pit[ch].high_read) {
                   6428:                                pit[ch].count_latched = 0;
                   6429:                        }
                   6430:                        return(pit[ch].latch & 0xff);
                   6431:                } else if(pit[ch].high_read) {
                   6432:                        pit[ch].high_read = 0;
                   6433:                        pit[ch].count_latched = 0;
                   6434:                        return((pit[ch].latch >> 8) & 0xff);
                   6435:                }
                   6436:        }
                   6437:        return(0xff);
                   6438: }
                   6439: 
1.1.1.8   root     6440: int pit_run(int ch, UINT32 cur_time)
1.1       root     6441: {
1.1.1.8   root     6442:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     6443:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     6444:                pit[ch].prev_time = pit[ch].expired_time;
                   6445:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   6446:                if(cur_time >= pit[ch].expired_time) {
                   6447:                        pit[ch].prev_time = cur_time;
                   6448:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     6449:                }
1.1.1.8   root     6450:                return(1);
1.1       root     6451:        }
1.1.1.8   root     6452:        return(0);
1.1       root     6453: }
                   6454: 
                   6455: void pit_latch_count(int ch)
                   6456: {
1.1.1.8   root     6457:        UINT32 cur_time = timeGetTime();
                   6458:        
                   6459:        if(pit[ch].expired_time != 0) {
                   6460:                pit_run(ch, cur_time);
                   6461:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
                   6462:                pit[ch].latch = (tmp != 0) ? (UINT16)tmp : 1;
                   6463:        } else {
                   6464:                pit[ch].latch = (UINT16)pit[ch].count;
1.1       root     6465:        }
                   6466:        pit[ch].count_latched = 1;
                   6467:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   6468:                // lower byte
                   6469:                pit[ch].low_read = 1;
                   6470:                pit[ch].high_read = 0;
                   6471:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   6472:                // upper byte
                   6473:                pit[ch].low_read = 0;
                   6474:                pit[ch].high_read = 1;
                   6475:        } else {
                   6476:                // lower -> upper
                   6477:                pit[ch].low_read = pit[ch].low_read = 1;
                   6478:        }
                   6479: }
                   6480: 
1.1.1.8   root     6481: int pit_get_expired_time(int ch)
1.1       root     6482: {
1.1.1.8   root     6483:        UINT32 val = (1000 * pit[ch].count) / PIT_FREQ;
                   6484:        return((val > 0) ? val : 1);
                   6485: }
                   6486: 
                   6487: // cmos
                   6488: 
                   6489: void cmos_init()
                   6490: {
                   6491:        memset(cmos, 0, sizeof(cmos));
                   6492:        cmos_addr = 0;
1.1       root     6493:        
1.1.1.8   root     6494:        // from DOSBox
                   6495:        cmos_write(0x0a, 0x26);
                   6496:        cmos_write(0x0b, 0x02);
                   6497:        cmos_write(0x0d, 0x80);
1.1       root     6498: }
                   6499: 
1.1.1.8   root     6500: void cmos_write(int addr, UINT8 val)
1.1       root     6501: {
1.1.1.8   root     6502:        cmos[addr & 0x7f] = val;
                   6503: }
                   6504: 
                   6505: #define CMOS_GET_TIME() { \
                   6506:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   6507:        if(prev_sec != cur_sec) { \
                   6508:                GetLocalTime(&time); \
                   6509:                prev_sec = cur_sec; \
                   6510:        } \
1.1       root     6511: }
1.1.1.8   root     6512: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     6513: 
1.1.1.8   root     6514: UINT8 cmos_read(int addr)
1.1       root     6515: {
1.1.1.8   root     6516:        static SYSTEMTIME time;
                   6517:        static UINT32 prev_sec = 0;
1.1       root     6518:        
1.1.1.8   root     6519:        switch(addr & 0x7f) {
                   6520:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   6521:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   6522:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   6523:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   6524:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   6525:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   6526:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   6527: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   6528:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   6529:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   6530:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   6531:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   6532:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   6533:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   6534:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   6535:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     6536:        }
1.1.1.8   root     6537:        return(cmos[addr & 0x7f]);
1.1       root     6538: }
                   6539: 
1.1.1.7   root     6540: // kbd (a20)
                   6541: 
                   6542: void kbd_init()
                   6543: {
1.1.1.8   root     6544:        kbd_data = kbd_command = 0;
1.1.1.7   root     6545:        kbd_status = 0x18;
                   6546: }
                   6547: 
                   6548: UINT8 kbd_read_data()
                   6549: {
1.1.1.8   root     6550:        kbd_status &= ~1;
1.1.1.7   root     6551:        return(kbd_data);
                   6552: }
                   6553: 
                   6554: void kbd_write_data(UINT8 val)
                   6555: {
                   6556:        switch(kbd_command) {
                   6557:        case 0xd1:
                   6558:                i386_set_a20_line((val >> 1) & 1);
                   6559:                break;
                   6560:        }
                   6561:        kbd_command = 0;
1.1.1.8   root     6562:        kbd_status &= ~8;
1.1.1.7   root     6563: }
                   6564: 
                   6565: UINT8 kbd_read_status()
                   6566: {
                   6567:        return(kbd_status);
                   6568: }
                   6569: 
                   6570: void kbd_write_command(UINT8 val)
                   6571: {
                   6572:        switch(val) {
                   6573:        case 0xd0:
                   6574:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     6575:                kbd_status |= 1;
1.1.1.7   root     6576:                break;
                   6577:        case 0xdd:
                   6578:                i386_set_a20_line(0);
                   6579:                break;
                   6580:        case 0xdf:
                   6581:                i386_set_a20_line(1);
                   6582:                break;
                   6583:        case 0xf0:
                   6584:        case 0xf1:
                   6585:        case 0xf2:
                   6586:        case 0xf3:
                   6587:        case 0xf4:
                   6588:        case 0xf5:
                   6589:        case 0xf6:
                   6590:        case 0xf7:
                   6591:        case 0xf8:
                   6592:        case 0xf9:
                   6593:        case 0xfa:
                   6594:        case 0xfb:
                   6595:        case 0xfc:
                   6596:        case 0xfd:
                   6597:        case 0xfe:
                   6598:        case 0xff:
                   6599:                if(!(val & 1)) {
1.1.1.8   root     6600:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     6601:                                // reset pic
                   6602:                                pic_init();
                   6603:                                pic[0].irr = pic[1].irr = 0x00;
                   6604:                                pic[0].imr = pic[1].imr = 0xff;
                   6605:                        }
                   6606:                        CPU_RESET_CALL(CPU_MODEL);
                   6607:                        i386_jmp_far(0x40, 0x67);
                   6608:                }
                   6609:                i386_set_a20_line((val >> 1) & 1);
                   6610:                break;
                   6611:        }
                   6612:        kbd_command = val;
1.1.1.8   root     6613:        kbd_status |= 8;
1.1.1.7   root     6614: }
                   6615: 
1.1.1.9   root     6616: // vga
                   6617: 
                   6618: UINT8 vga_read_status()
                   6619: {
                   6620:        // 60hz
                   6621:        static const int period[3] = {16, 17, 17};
                   6622:        static int index = 0;
                   6623:        UINT32 time = timeGetTime() % period[index];
                   6624:        
                   6625:        index = (index + 1) % 3;
                   6626:        return((time < 4 ? 0x08 : 0) | 0x01);
                   6627: }
                   6628: 
1.1       root     6629: // i/o bus
                   6630: 
                   6631: UINT8 read_io_byte(offs_t addr)
                   6632: {
                   6633:        switch(addr) {
                   6634:        case 0x20:
                   6635:        case 0x21:
                   6636:                return(pic_read(0, addr));
                   6637:        case 0x40:
                   6638:        case 0x41:
                   6639:        case 0x42:
                   6640:        case 0x43:
                   6641:                return(pit_read(addr & 0x03));
1.1.1.7   root     6642:        case 0x60:
                   6643:                return(kbd_read_data());
1.1.1.9   root     6644:        case 0x61:
                   6645:                return(system_port);
1.1.1.7   root     6646:        case 0x64:
                   6647:                return(kbd_read_status());
1.1       root     6648:        case 0x71:
1.1.1.8   root     6649:                return(cmos_read(cmos_addr));
1.1       root     6650:        case 0x92:
1.1.1.3   root     6651:                return((m_a20_mask >> 19) & 2);
1.1       root     6652:        case 0xa0:
                   6653:        case 0xa1:
                   6654:                return(pic_read(1, addr));
1.1.1.9   root     6655:        case 0x3ba:
                   6656:        case 0x3da:
                   6657:                return(vga_read_status());
1.1       root     6658:        default:
                   6659: //             error("inb %4x\n", addr);
                   6660:                break;
                   6661:        }
                   6662:        return(0xff);
                   6663: }
                   6664: 
                   6665: UINT16 read_io_word(offs_t addr)
                   6666: {
                   6667:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   6668: }
                   6669: 
                   6670: UINT32 read_io_dword(offs_t addr)
                   6671: {
                   6672:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   6673: }
                   6674: 
                   6675: void write_io_byte(offs_t addr, UINT8 val)
                   6676: {
                   6677:        switch(addr) {
                   6678:        case 0x20:
                   6679:        case 0x21:
                   6680:                pic_write(0, addr, val);
                   6681:                break;
                   6682:        case 0x40:
                   6683:        case 0x41:
                   6684:        case 0x42:
                   6685:        case 0x43:
                   6686:                pit_write(addr & 0x03, val);
                   6687:                break;
1.1.1.7   root     6688:        case 0x60:
                   6689:                kbd_write_data(val);
                   6690:                break;
1.1.1.9   root     6691:        case 0x61:
                   6692:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   6693:                        // beep on
                   6694: //                     MessageBeep(-1);
                   6695:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   6696:                        // beep off
                   6697:                }
                   6698:                system_port = val;
                   6699:                break;
1.1       root     6700:        case 0x64:
1.1.1.7   root     6701:                kbd_write_command(val);
1.1       root     6702:                break;
                   6703:        case 0x70:
                   6704:                cmos_addr = val;
                   6705:                break;
                   6706:        case 0x71:
1.1.1.8   root     6707:                cmos_write(cmos_addr, val);
1.1       root     6708:                break;
                   6709:        case 0x92:
1.1.1.7   root     6710:                i386_set_a20_line((val >> 1) & 1);
1.1       root     6711:                break;
                   6712:        case 0xa0:
                   6713:        case 0xa1:
                   6714:                pic_write(1, addr, val);
                   6715:                break;
                   6716:        default:
                   6717: //             error("outb %4x,%2x\n", addr, val);
                   6718:                break;
                   6719:        }
                   6720: }
                   6721: 
                   6722: void write_io_word(offs_t addr, UINT16 val)
                   6723: {
                   6724:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   6725:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   6726: }
                   6727: 
                   6728: void write_io_dword(offs_t addr, UINT32 val)
                   6729: {
                   6730:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   6731:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   6732:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   6733:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   6734: }

unix.superglobalmegacorp.com

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