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

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

unix.superglobalmegacorp.com

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