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

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

unix.superglobalmegacorp.com

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