|
|
1.1 root 1: /*
2: MS-DOS Player for Win32 console
3:
4: Author : Takeda.Toshiya
5: Date : 2009.11.09-
6: */
7:
8: #include "msdos.h"
9:
1.1.1.28 root 10: void exit_handler();
11:
1.1 root 12: #define fatalerror(...) { \
13: fprintf(stderr, __VA_ARGS__); \
1.1.1.28 root 14: exit_handler(); \
1.1 root 15: exit(1); \
16: }
17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22 root 18: #define nolog(...)
19:
1.1.1.33 root 20: //#define ENABLE_DEBUG_LOG
21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22 root 22: #define EXPORT_DEBUG_TO_FILE
23: #define ENABLE_DEBUG_SYSCALL
24: #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25 root 25: #define ENABLE_DEBUG_IOPORT
1.1.1.22 root 26:
27: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 28: FILE* fp_debug_log = NULL;
1.1.1.22 root 29: #else
1.1.1.33 root 30: #define fp_debug_log stderr
1.1.1.22 root 31: #endif
32: #ifdef ENABLE_DEBUG_UNIMPLEMENTED
33: #define unimplemented_10h fatalerror
1.1.1.42 root 34: #define unimplemented_13h fatalerror
1.1.1.25 root 35: #define unimplemented_14h fatalerror
1.1.1.22 root 36: #define unimplemented_15h fatalerror
37: #define unimplemented_16h fatalerror
1.1.1.37 root 38: #define unimplemented_17h fatalerror
1.1.1.22 root 39: #define unimplemented_1ah fatalerror
40: #define unimplemented_21h fatalerror
41: #define unimplemented_2fh fatalerror
1.1.1.24 root 42: #define unimplemented_33h fatalerror
1.1.1.22 root 43: #define unimplemented_67h fatalerror
44: #define unimplemented_xms fatalerror
45: #endif
46: #endif
47: #ifndef unimplemented_10h
48: #define unimplemented_10h nolog
49: #endif
1.1.1.42 root 50: #ifndef unimplemented_13h
51: #define unimplemented_13h nolog
52: #endif
1.1.1.25 root 53: #ifndef unimplemented_14h
54: #define unimplemented_14h nolog
55: #endif
1.1.1.22 root 56: #ifndef unimplemented_15h
57: #define unimplemented_15h nolog
58: #endif
59: #ifndef unimplemented_16h
60: #define unimplemented_16h nolog
61: #endif
1.1.1.37 root 62: #ifndef unimplemented_17h
63: #define unimplemented_17h nolog
64: #endif
1.1.1.22 root 65: #ifndef unimplemented_1ah
66: #define unimplemented_1ah nolog
67: #endif
68: #ifndef unimplemented_21h
69: #define unimplemented_21h nolog
70: #endif
71: #ifndef unimplemented_2fh
72: #define unimplemented_2fh nolog
73: #endif
1.1.1.24 root 74: #ifndef unimplemented_33h
75: #define unimplemented_33h nolog
76: #endif
1.1.1.22 root 77: #ifndef unimplemented_67h
78: #define unimplemented_67h nolog
79: #endif
80: #ifndef unimplemented_xms
81: #define unimplemented_xms nolog
82: #endif
83:
1.1.1.60! root 84: #ifdef _MBCS
! 85: inline char *my_strchr(char *str, int chr)
! 86: {
! 87: return (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr));
! 88: }
! 89: inline const char *my_strchr(const char *str, int chr)
! 90: {
! 91: return (const char *)_mbschr((const unsigned char *)(str), (unsigned int)(chr));
! 92: }
! 93: inline char *my_strrchr(char *str, int chr)
! 94: {
! 95: return (char *)_mbsrchr((unsigned char *)(str), (unsigned int)(chr));
! 96: }
! 97: inline const char *my_strrchr(const char *str, int chr)
! 98: {
! 99: return (const char *)_mbsrchr((const unsigned char *)(str), (unsigned int)(chr));
! 100: }
! 101: inline char *my_strtok(char *tok, const char *del)
! 102: {
! 103: return (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del));
! 104: }
! 105: inline char *my_strupr(char *str)
! 106: {
! 107: return (char *)_mbsupr((unsigned char *)(str));
! 108: }
! 109: #else
! 110: #define my_strchr(str, chr) strchr((str), (chr))
! 111: #define my_strrchr(str, chr) strrchr((str), (chr))
! 112: #define my_strtok(tok, del) strtok((tok), (del))
! 113: #define my_strupr(str) _strupr((str))
! 114: #endif
1.1.1.32 root 115: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1 root 116:
1.1.1.12 root 117: #if defined(__MINGW32__)
118: extern "C" int _CRT_glob = 0;
119: #endif
120:
121: /*
122: kludge for "more-standardized" C++
123: */
124: #if !defined(_MSC_VER)
125: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
126: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
127: #define min(a,b) kludge_min(a,b)
128: #define max(a,b) kludge_max(a,b)
1.1.1.14 root 129: #elif _MSC_VER >= 1400
130: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
131: {
132: }
133: #endif
134:
1.1.1.35 root 135: #define USE_VRAM_THREAD
1.1.1.14 root 136:
1.1.1.35 root 137: #ifdef USE_VRAM_THREAD
1.1.1.14 root 138: static CRITICAL_SECTION vram_crit_sect;
139: #else
140: #define vram_flush()
1.1.1.12 root 141: #endif
142:
1.1.1.14 root 143: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
144: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
145:
146: void change_console_size(int width, int height);
147: void clear_scr_buffer(WORD attr);
148:
149: static UINT32 vram_length_char = 0, vram_length_attr = 0;
150: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
151: static COORD vram_coord_char, vram_coord_attr;
152:
1.1.1.28 root 153: char temp_file_path[MAX_PATH];
154: bool temp_file_created = false;
155:
1.1.1.14 root 156: bool ignore_illegal_insn = false;
157: bool limit_max_memory = false;
158: bool no_windows = false;
159: bool stay_busy = false;
1.1.1.19 root 160: bool support_ems = false;
161: #ifdef SUPPORT_XMS
162: bool support_xms = false;
163: #endif
1.1.1.29 root 164: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14 root 165:
1.1.1.54 root 166: BOOL is_xp_64_or_later;
1.1.1.14 root 167: BOOL is_vista_or_later;
168:
1.1.1.35 root 169: #define UPDATE_OPS 16384
170: #define REQUEST_HARDWRE_UPDATE() { \
171: update_ops = UPDATE_OPS - 1; \
172: }
173: UINT32 update_ops = 0;
174: UINT32 idle_ops = 0;
175:
1.1.1.54 root 176: inline BOOL is_sse2_ready()
177: {
178: static int result = -1;
179: int cpu_info[4];
180:
181: if(result == -1) {
182: result = 0;
183: __cpuid(cpu_info, 0);
184: if(cpu_info[0] >= 1){
185: __cpuid(cpu_info, 1);
186: if(cpu_info[3] & (1 << 26)) {
187: result = 1;
188: }
189: }
190: }
191: return(result == 1);
192: }
193:
1.1.1.14 root 194: inline void maybe_idle()
195: {
196: // if it appears to be in a tight loop, assume waiting for input
197: // allow for one updated video character, for a spinning cursor
1.1.1.35 root 198: if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54 root 199: if(is_sse2_ready()) {
200: _mm_pause(); // SSE2 pause
201: } else if(is_xp_64_or_later) {
202: Sleep(0); // switch to other thread that is ready to run, without checking priority
203: } else {
204: Sleep(1);
205: REQUEST_HARDWRE_UPDATE();
206: }
1.1.1.14 root 207: }
1.1.1.35 root 208: idle_ops = 0;
1.1.1.14 root 209: }
1.1.1.12 root 210:
1.1 root 211: /* ----------------------------------------------------------------------------
1.1.1.3 root 212: MAME i86/i386
1.1 root 213: ---------------------------------------------------------------------------- */
214:
1.1.1.10 root 215: #ifndef __BIG_ENDIAN__
1.1 root 216: #define LSB_FIRST
1.1.1.10 root 217: #endif
1.1 root 218:
219: #ifndef INLINE
220: #define INLINE inline
221: #endif
222: #define U64(v) UINT64(v)
223:
224: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
225: #define logerror(...)
226: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
227: #define popmessage(...)
228:
229: /*****************************************************************************/
1.1.1.10 root 230: /* src/emu/devcpu.h */
231:
232: // CPU interface functions
233: #define CPU_INIT_NAME(name) cpu_init_##name
234: #define CPU_INIT(name) void CPU_INIT_NAME(name)()
235: #define CPU_INIT_CALL(name) CPU_INIT_NAME(name)()
236:
237: #define CPU_RESET_NAME(name) cpu_reset_##name
238: #define CPU_RESET(name) void CPU_RESET_NAME(name)()
239: #define CPU_RESET_CALL(name) CPU_RESET_NAME(name)()
240:
241: #define CPU_EXECUTE_NAME(name) cpu_execute_##name
242: #define CPU_EXECUTE(name) void CPU_EXECUTE_NAME(name)()
243: #define CPU_EXECUTE_CALL(name) CPU_EXECUTE_NAME(name)()
244:
245: #define CPU_TRANSLATE_NAME(name) cpu_translate_##name
246: #define CPU_TRANSLATE(name) int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
247: #define CPU_TRANSLATE_CALL(name) CPU_TRANSLATE_NAME(name)(space, intention, address)
248:
249: #define CPU_DISASSEMBLE_NAME(name) cpu_disassemble_##name
250: #define CPU_DISASSEMBLE(name) int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
251: #define CPU_DISASSEMBLE_CALL(name) CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
252:
1.1.1.14 root 253: #define CPU_MODEL_STR(name) #name
254: #define CPU_MODEL_NAME(name) CPU_MODEL_STR(name)
255:
1.1.1.10 root 256: /*****************************************************************************/
257: /* src/emu/didisasm.h */
258:
259: // Disassembler constants
260: const UINT32 DASMFLAG_SUPPORTED = 0x80000000; // are disassembly flags supported?
261: const UINT32 DASMFLAG_STEP_OUT = 0x40000000; // this instruction should be the end of a step out sequence
262: const UINT32 DASMFLAG_STEP_OVER = 0x20000000; // this instruction should be stepped over by setting a breakpoint afterwards
263: const UINT32 DASMFLAG_OVERINSTMASK = 0x18000000; // number of extra instructions to skip when stepping over
264: const UINT32 DASMFLAG_OVERINSTSHIFT = 27; // bits to shift after masking to get the value
265: const UINT32 DASMFLAG_LENGTHMASK = 0x0000ffff; // the low 16-bits contain the actual length
266:
267: /*****************************************************************************/
1.1 root 268: /* src/emu/diexec.h */
269:
270: // I/O line states
271: enum line_state
272: {
273: CLEAR_LINE = 0, // clear (a fired or held) line
274: ASSERT_LINE, // assert an interrupt immediately
275: HOLD_LINE, // hold interrupt line until acknowledged
276: PULSE_LINE // pulse interrupt line instantaneously (only for NMI, RESET)
277: };
278:
279: // I/O line definitions
280: enum
281: {
282: INPUT_LINE_IRQ = 0,
283: INPUT_LINE_NMI
284: };
285:
286: /*****************************************************************************/
1.1.1.10 root 287: /* src/emu/dimemory.h */
1.1 root 288:
1.1.1.10 root 289: // Translation intentions
290: const int TRANSLATE_TYPE_MASK = 0x03; // read write or fetch
291: const int TRANSLATE_USER_MASK = 0x04; // user mode or fully privileged
292: const int TRANSLATE_DEBUG_MASK = 0x08; // debug mode (no side effects)
293:
294: const int TRANSLATE_READ = 0; // translate for read
295: const int TRANSLATE_WRITE = 1; // translate for write
296: const int TRANSLATE_FETCH = 2; // translate for instruction fetch
297: const int TRANSLATE_READ_USER = (TRANSLATE_READ | TRANSLATE_USER_MASK);
298: const int TRANSLATE_WRITE_USER = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
299: const int TRANSLATE_FETCH_USER = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
300: const int TRANSLATE_READ_DEBUG = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
301: const int TRANSLATE_WRITE_DEBUG = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
302: const int TRANSLATE_FETCH_DEBUG = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1 root 303:
1.1.1.10 root 304: /*****************************************************************************/
305: /* src/emu/emucore.h */
1.1 root 306:
1.1.1.10 root 307: // constants for expression endianness
308: enum endianness_t
309: {
310: ENDIANNESS_LITTLE,
311: ENDIANNESS_BIG
312: };
1.1 root 313:
1.1.1.10 root 314: // declare native endianness to be one or the other
315: #ifdef LSB_FIRST
316: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
317: #else
318: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
319: #endif
320:
321: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
322: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
323:
324: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
325: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
326:
327: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
328: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval) (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1 root 329:
330: /*****************************************************************************/
331: /* src/emu/memory.h */
332:
1.1.1.10 root 333: // address spaces
334: enum address_spacenum
335: {
336: AS_0, // first address space
337: AS_1, // second address space
338: AS_2, // third address space
339: AS_3, // fourth address space
340: ADDRESS_SPACES, // maximum number of address spaces
341:
342: // alternate address space names for common use
343: AS_PROGRAM = AS_0, // program address space
344: AS_DATA = AS_1, // data address space
345: AS_IO = AS_2 // I/O address space
346: };
347:
1.1 root 348: // offsets and addresses are 32-bit (for now...)
1.1.1.30 root 349: //typedef UINT32 offs_t;
1.1 root 350:
351: // read accessors
352: UINT8 read_byte(offs_t byteaddress)
1.1.1.33 root 353: #ifdef USE_DEBUGGER
354: {
355: if(now_debugging) {
356: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
357: if(rd_break_point.table[i].status == 1) {
358: if(byteaddress == rd_break_point.table[i].addr) {
359: rd_break_point.hit = i + 1;
360: now_suspended = true;
361: break;
362: }
363: }
364: }
365: }
366: return(debugger_read_byte(byteaddress));
367: }
368: UINT8 debugger_read_byte(offs_t byteaddress)
369: #endif
1.1 root 370: {
1.1.1.4 root 371: #if defined(HAS_I386)
1.1 root 372: if(byteaddress < MAX_MEM) {
373: return mem[byteaddress];
1.1.1.3 root 374: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
375: // return read_byte(byteaddress & 0xfffff);
1.1 root 376: }
377: return 0;
1.1.1.4 root 378: #else
379: return mem[byteaddress];
380: #endif
1.1 root 381: }
382:
383: UINT16 read_word(offs_t byteaddress)
1.1.1.33 root 384: #ifdef USE_DEBUGGER
385: {
386: if(now_debugging) {
387: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
388: if(rd_break_point.table[i].status == 1) {
389: if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
390: rd_break_point.hit = i + 1;
391: now_suspended = true;
392: break;
393: }
394: }
395: }
396: }
397: return(debugger_read_word(byteaddress));
398: }
399: UINT16 debugger_read_word(offs_t byteaddress)
400: #endif
1.1 root 401: {
1.1.1.14 root 402: if(byteaddress == 0x41c) {
403: // pointer to first free slot in keyboard buffer
1.1.1.35 root 404: if(key_buf_char != NULL && key_buf_scan != NULL) {
405: #ifdef USE_SERVICE_THREAD
406: EnterCriticalSection(&key_buf_crit_sect);
407: #endif
1.1.1.55 root 408: bool empty = pcbios_is_key_buffer_empty();
1.1.1.35 root 409: #ifdef USE_SERVICE_THREAD
410: LeaveCriticalSection(&key_buf_crit_sect);
411: #endif
1.1.1.51 root 412: if(empty) maybe_idle();
1.1.1.14 root 413: }
414: }
1.1.1.4 root 415: #if defined(HAS_I386)
1.1 root 416: if(byteaddress < MAX_MEM - 1) {
417: return *(UINT16 *)(mem + byteaddress);
1.1.1.3 root 418: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
419: // return read_word(byteaddress & 0xfffff);
1.1 root 420: }
421: return 0;
1.1.1.4 root 422: #else
423: return *(UINT16 *)(mem + byteaddress);
424: #endif
1.1 root 425: }
426:
427: UINT32 read_dword(offs_t byteaddress)
1.1.1.33 root 428: #ifdef USE_DEBUGGER
429: {
430: if(now_debugging) {
431: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
432: if(rd_break_point.table[i].status == 1) {
433: if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
434: rd_break_point.hit = i + 1;
435: now_suspended = true;
436: break;
437: }
438: }
439: }
440: }
441: return(debugger_read_dword(byteaddress));
442: }
443: UINT32 debugger_read_dword(offs_t byteaddress)
444: #endif
1.1 root 445: {
1.1.1.4 root 446: #if defined(HAS_I386)
1.1 root 447: if(byteaddress < MAX_MEM - 3) {
448: return *(UINT32 *)(mem + byteaddress);
1.1.1.3 root 449: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
450: // return read_dword(byteaddress & 0xfffff);
1.1 root 451: }
452: return 0;
1.1.1.4 root 453: #else
454: return *(UINT32 *)(mem + byteaddress);
455: #endif
1.1 root 456: }
457:
458: // write accessors
1.1.1.35 root 459: #ifdef USE_VRAM_THREAD
1.1.1.14 root 460: void vram_flush_char()
461: {
462: if(vram_length_char != 0) {
463: DWORD num;
1.1.1.60! root 464: WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14 root 465: vram_length_char = vram_last_length_char = 0;
466: }
467: }
468:
469: void vram_flush_attr()
470: {
471: if(vram_length_attr != 0) {
472: DWORD num;
1.1.1.23 root 473: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14 root 474: vram_length_attr = vram_last_length_attr = 0;
475: }
476: }
477:
478: void vram_flush()
479: {
480: if(vram_length_char != 0 || vram_length_attr != 0) {
481: EnterCriticalSection(&vram_crit_sect);
482: vram_flush_char();
483: vram_flush_attr();
484: LeaveCriticalSection(&vram_crit_sect);
485: }
486: }
487: #endif
488:
489: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8 root 490: {
1.1.1.35 root 491: #ifdef USE_VRAM_THREAD
1.1.1.14 root 492: static offs_t first_offset_char, last_offset_char;
493:
494: if(vram_length_char != 0) {
495: if(offset <= last_offset_char && offset >= first_offset_char) {
496: scr_char[(offset - first_offset_char) >> 1] = data;
497: return;
498: }
499: if(offset != last_offset_char + 2) {
500: vram_flush_char();
501: }
502: }
503: if(vram_length_char == 0) {
504: first_offset_char = offset;
505: vram_coord_char.X = (offset >> 1) % scr_width;
506: vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
507: }
508: scr_char[vram_length_char++] = data;
509: last_offset_char = offset;
510: #else
1.1.1.8 root 511: COORD co;
512: DWORD num;
513:
1.1.1.14 root 514: co.X = (offset >> 1) % scr_width;
515: co.Y = (offset >> 1) / scr_width;
516: scr_char[0] = data;
1.1.1.60! root 517: WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14 root 518: #endif
519: }
520:
521: void write_text_vram_attr(offs_t offset, UINT8 data)
522: {
1.1.1.35 root 523: #ifdef USE_VRAM_THREAD
1.1.1.14 root 524: static offs_t first_offset_attr, last_offset_attr;
525:
526: if(vram_length_attr != 0) {
527: if(offset <= last_offset_attr && offset >= first_offset_attr) {
528: scr_attr[(offset - first_offset_attr) >> 1] = data;
529: return;
530: }
531: if(offset != last_offset_attr + 2) {
532: vram_flush_attr();
533: }
534: }
535: if(vram_length_attr == 0) {
536: first_offset_attr = offset;
537: vram_coord_attr.X = (offset >> 1) % scr_width;
538: vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
539: }
540: scr_attr[vram_length_attr++] = data;
541: last_offset_attr = offset;
542: #else
543: COORD co;
544: DWORD num;
1.1.1.8 root 545:
1.1.1.14 root 546: co.X = (offset >> 1) % scr_width;
547: co.Y = (offset >> 1) / scr_width;
548: scr_attr[0] = data;
1.1.1.23 root 549: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14 root 550: #endif
551: }
552:
553: void write_text_vram_byte(offs_t offset, UINT8 data)
554: {
1.1.1.35 root 555: #ifdef USE_VRAM_THREAD
1.1.1.14 root 556: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 557: #endif
1.1.1.8 root 558: if(offset & 1) {
1.1.1.14 root 559: write_text_vram_attr(offset, data);
1.1.1.8 root 560: } else {
1.1.1.14 root 561: write_text_vram_char(offset, data);
1.1.1.8 root 562: }
1.1.1.35 root 563: #ifdef USE_VRAM_THREAD
1.1.1.14 root 564: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 565: #endif
1.1.1.8 root 566: }
567:
568: void write_text_vram_word(offs_t offset, UINT16 data)
569: {
1.1.1.35 root 570: #ifdef USE_VRAM_THREAD
1.1.1.14 root 571: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 572: #endif
1.1.1.8 root 573: if(offset & 1) {
1.1.1.14 root 574: write_text_vram_attr(offset , (data ) & 0xff);
575: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 576: } else {
1.1.1.14 root 577: write_text_vram_char(offset , (data ) & 0xff);
578: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 579: }
1.1.1.35 root 580: #ifdef USE_VRAM_THREAD
1.1.1.14 root 581: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 582: #endif
1.1.1.8 root 583: }
584:
585: void write_text_vram_dword(offs_t offset, UINT32 data)
586: {
1.1.1.35 root 587: #ifdef USE_VRAM_THREAD
1.1.1.14 root 588: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 589: #endif
1.1.1.8 root 590: if(offset & 1) {
1.1.1.14 root 591: write_text_vram_attr(offset , (data ) & 0xff);
592: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
593: write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
594: write_text_vram_char(offset + 3, (data >> 24) & 0xff);
595: } else {
596: write_text_vram_char(offset , (data ) & 0xff);
597: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
598: write_text_vram_char(offset + 2, (data >> 16) & 0xff);
599: write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8 root 600: }
1.1.1.35 root 601: #ifdef USE_VRAM_THREAD
1.1.1.14 root 602: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 603: #endif
1.1.1.8 root 604: }
605:
1.1 root 606: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33 root 607: #ifdef USE_DEBUGGER
608: {
609: if(now_debugging) {
610: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
611: if(wr_break_point.table[i].status == 1) {
612: if(byteaddress == wr_break_point.table[i].addr) {
613: wr_break_point.hit = i + 1;
614: now_suspended = true;
615: break;
616: }
617: }
618: }
619: }
620: debugger_write_byte(byteaddress, data);
621: }
622: void debugger_write_byte(offs_t byteaddress, UINT8 data)
623: #endif
1.1 root 624: {
1.1.1.8 root 625: if(byteaddress < MEMORY_END) {
1.1.1.3 root 626: mem[byteaddress] = data;
1.1.1.8 root 627: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 628: if(!restore_console_on_exit) {
629: change_console_size(scr_width, scr_height);
1.1.1.12 root 630: }
1.1.1.8 root 631: write_text_vram_byte(byteaddress - text_vram_top_address, data);
632: mem[byteaddress] = data;
633: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
634: if(int_10h_feh_called && !int_10h_ffh_called) {
635: write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1 root 636: }
637: mem[byteaddress] = data;
1.1.1.4 root 638: #if defined(HAS_I386)
1.1.1.3 root 639: } else if(byteaddress < MAX_MEM) {
1.1.1.4 root 640: #else
641: } else {
642: #endif
1.1.1.3 root 643: mem[byteaddress] = data;
1.1 root 644: }
645: }
646:
647: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33 root 648: #ifdef USE_DEBUGGER
649: {
650: if(now_debugging) {
651: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
652: if(wr_break_point.table[i].status == 1) {
653: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
654: wr_break_point.hit = i + 1;
655: now_suspended = true;
656: break;
657: }
658: }
659: }
660: }
661: debugger_write_word(byteaddress, data);
662: }
663: void debugger_write_word(offs_t byteaddress, UINT16 data)
664: #endif
1.1 root 665: {
1.1.1.8 root 666: if(byteaddress < MEMORY_END) {
1.1.1.51 root 667: if(byteaddress == cursor_position_address) {
668: if(*(UINT16 *)(mem + byteaddress) != data) {
669: COORD co;
670: co.X = data & 0xff;
671: co.Y = (data >> 8) + scr_top;
672: SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.59 root 673: cursor_moved = false;
674: cursor_moved_by_crtc = false;
1.1.1.51 root 675: }
1.1.1.14 root 676: }
1.1.1.3 root 677: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8 root 678: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 679: if(!restore_console_on_exit) {
680: change_console_size(scr_width, scr_height);
1.1.1.12 root 681: }
1.1.1.8 root 682: write_text_vram_word(byteaddress - text_vram_top_address, data);
683: *(UINT16 *)(mem + byteaddress) = data;
684: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
685: if(int_10h_feh_called && !int_10h_ffh_called) {
686: write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1 root 687: }
688: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4 root 689: #if defined(HAS_I386)
1.1.1.3 root 690: } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4 root 691: #else
692: } else {
693: #endif
1.1.1.3 root 694: *(UINT16 *)(mem + byteaddress) = data;
1.1 root 695: }
696: }
697:
698: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33 root 699: #ifdef USE_DEBUGGER
700: {
701: if(now_debugging) {
702: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
703: if(wr_break_point.table[i].status == 1) {
704: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
705: wr_break_point.hit = i + 1;
706: now_suspended = true;
707: break;
708: }
709: }
710: }
711: }
712: debugger_write_dword(byteaddress, data);
713: }
714: void debugger_write_dword(offs_t byteaddress, UINT32 data)
715: #endif
1.1 root 716: {
1.1.1.8 root 717: if(byteaddress < MEMORY_END) {
1.1.1.3 root 718: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8 root 719: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 720: if(!restore_console_on_exit) {
721: change_console_size(scr_width, scr_height);
1.1.1.12 root 722: }
1.1.1.8 root 723: write_text_vram_dword(byteaddress - text_vram_top_address, data);
724: *(UINT32 *)(mem + byteaddress) = data;
725: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
726: if(int_10h_feh_called && !int_10h_ffh_called) {
727: write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1 root 728: }
729: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4 root 730: #if defined(HAS_I386)
1.1.1.3 root 731: } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4 root 732: #else
733: } else {
734: #endif
1.1.1.3 root 735: *(UINT32 *)(mem + byteaddress) = data;
1.1 root 736: }
737: }
738:
739: #define read_decrypted_byte read_byte
740: #define read_decrypted_word read_word
741: #define read_decrypted_dword read_dword
742:
1.1.1.3 root 743: #define read_raw_byte read_byte
744: #define write_raw_byte write_byte
745:
746: #define read_word_unaligned read_word
747: #define write_word_unaligned write_word
748:
749: #define read_io_word_unaligned read_io_word
750: #define write_io_word_unaligned write_io_word
751:
1.1 root 752: UINT8 read_io_byte(offs_t byteaddress);
753: UINT16 read_io_word(offs_t byteaddress);
754: UINT32 read_io_dword(offs_t byteaddress);
755:
756: void write_io_byte(offs_t byteaddress, UINT8 data);
757: void write_io_word(offs_t byteaddress, UINT16 data);
758: void write_io_dword(offs_t byteaddress, UINT32 data);
759:
760: /*****************************************************************************/
761: /* src/osd/osdcomm.h */
762:
763: /* Highly useful macro for compile-time knowledge of an array size */
764: #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
765:
1.1.1.54 root 766: // flag to exit MS-DOS Player
767: // this is set when the first process is terminated and jump to FFFF:0000 HALT
768: int m_exit = 0;
769:
1.1.1.3 root 770: #if defined(HAS_I386)
1.1.1.10 root 771: static CPU_TRANSLATE(i386);
772: #include "mame/lib/softfloat/softfloat.c"
773: #include "mame/emu/cpu/i386/i386.c"
1.1.1.12 root 774: #include "mame/emu/cpu/vtlb.c"
1.1.1.3 root 775: #elif defined(HAS_I286)
1.1.1.10 root 776: #include "mame/emu/cpu/i86/i286.c"
1.1.1.3 root 777: #else
1.1.1.10 root 778: #include "mame/emu/cpu/i86/i86.c"
1.1.1.3 root 779: #endif
1.1.1.33 root 780: #ifdef USE_DEBUGGER
1.1.1.10 root 781: #include "mame/emu/cpu/i386/i386dasm.c"
1.1 root 782: #endif
783:
1.1.1.3 root 784: #if defined(HAS_I386)
785: #define SREG(x) m_sreg[x].selector
786: #define SREG_BASE(x) m_sreg[x].base
787: int cpu_type, cpu_step;
788: #else
789: #define REG8(x) m_regs.b[x]
790: #define REG16(x) m_regs.w[x]
791: #define SREG(x) m_sregs[x]
792: #define SREG_BASE(x) m_base[x]
1.1.1.33 root 793: #define m_eip (m_pc - m_base[CS])
1.1.1.3 root 794: #define m_CF m_CarryVal
795: #define m_a20_mask AMASK
796: #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
797: #if defined(HAS_I286)
798: #define i386_set_a20_line(x) i80286_set_a20_line(x)
799: #else
800: #define i386_set_a20_line(x)
801: #endif
802: #define i386_set_irq_line(x, y) set_irq_line(x, y)
803: #endif
1.1 root 804:
805: void i386_jmp_far(UINT16 selector, UINT32 address)
806: {
1.1.1.3 root 807: #if defined(HAS_I386)
1.1 root 808: if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3 root 809: i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1 root 810: } else {
1.1.1.3 root 811: SREG(CS) = selector;
812: m_performed_intersegment_jump = 1;
813: i386_load_segment_descriptor(CS);
814: m_eip = address;
815: CHANGE_PC(m_eip);
1.1 root 816: }
1.1.1.3 root 817: #elif defined(HAS_I286)
818: i80286_code_descriptor(selector, address, 1);
819: #else
820: SREG(CS) = selector;
821: i386_load_segment_descriptor(CS);
822: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
823: #endif
1.1 root 824: }
825:
1.1.1.24 root 826: void i386_call_far(UINT16 selector, UINT32 address)
827: {
828: #if defined(HAS_I386)
829: if(PROTECTED_MODE && !V8086_MODE) {
830: i386_protected_mode_call(selector, address, 1, m_operand_size);
831: } else {
832: PUSH16(SREG(CS));
833: PUSH16(m_eip);
834: SREG(CS) = selector;
835: m_performed_intersegment_jump = 1;
836: i386_load_segment_descriptor(CS);
837: m_eip = address;
838: CHANGE_PC(m_eip);
839: }
840: #else
841: UINT16 ip = m_pc - SREG_BASE(CS);
842: UINT16 cs = SREG(CS);
843: #if defined(HAS_I286)
844: i80286_code_descriptor(selector, address, 2);
845: #else
846: SREG(CS) = selector;
847: i386_load_segment_descriptor(CS);
848: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
849: #endif
850: PUSH(cs);
851: PUSH(ip);
852: CHANGE_PC(m_pc);
853: #endif
854: }
1.1.1.49 root 855:
856: void i386_push16(UINT16 value)
857: {
858: #if defined(HAS_I386)
859: PUSH16(value);
860: #else
861: PUSH(value);
1.1.1.35 root 862: #endif
1.1.1.49 root 863: }
864:
865: UINT16 i386_pop16()
866: {
867: #if defined(HAS_I386)
868: return POP16();
869: #else
870: UINT16 value;
871: POP(value);
872: return value;
873: #endif
874: }
1.1.1.24 root 875:
1.1.1.29 root 876: UINT16 i386_read_stack()
877: {
878: #if defined(HAS_I386)
879: UINT32 ea, new_esp;
880: if( STACK_32BIT ) {
881: new_esp = REG32(ESP) + 2;
882: ea = i386_translate(SS, new_esp - 2, 0);
883: } else {
884: new_esp = REG16(SP) + 2;
885: ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
886: }
887: return READ16(ea);
888: #else
889: UINT16 sp = m_regs.w[SP] + 2;
890: return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
891: #endif
892: }
893:
1.1.1.53 root 894: void i386_write_stack(UINT16 value)
895: {
896: #if defined(HAS_I386)
897: UINT32 ea, new_esp;
898: if( STACK_32BIT ) {
899: new_esp = REG32(ESP) + 2;
900: ea = i386_translate(SS, new_esp - 2, 0);
901: } else {
902: new_esp = REG16(SP) + 2;
903: ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
904: }
905: WRITE16(ea, value);
906: #else
907: UINT16 sp = m_regs.w[SP] + 2;
908: WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
909: #endif
910: }
911:
1.1 root 912: /* ----------------------------------------------------------------------------
1.1.1.33 root 913: debugger
914: ---------------------------------------------------------------------------- */
915:
916: #ifdef USE_DEBUGGER
917: #define TELNET_BLUE 0x0004 // text color contains blue.
918: #define TELNET_GREEN 0x0002 // text color contains green.
919: #define TELNET_RED 0x0001 // text color contains red.
920: #define TELNET_INTENSITY 0x0008 // text color is intensified.
921:
922: int svr_socket = 0;
923: int cli_socket = 0;
924:
1.1.1.55 root 925: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33 root 926:
927: void debugger_init()
928: {
929: now_debugging = false;
930: now_going = false;
931: now_suspended = false;
932: force_suspend = false;
933:
934: memset(&break_point, 0, sizeof(break_point_t));
935: memset(&rd_break_point, 0, sizeof(break_point_t));
936: memset(&wr_break_point, 0, sizeof(break_point_t));
937: memset(&in_break_point, 0, sizeof(break_point_t));
938: memset(&out_break_point, 0, sizeof(break_point_t));
939: memset(&int_break_point, 0, sizeof(int_break_point_t));
940: }
941:
1.1.1.45 root 942: void telnet_send(const char *string)
1.1.1.33 root 943: {
944: char buffer[8192], *ptr;
945: strcpy(buffer, string);
946: while((ptr = strstr(buffer, "\n")) != NULL) {
947: char tmp[8192];
948: *ptr = '\0';
949: sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
950: strcpy(buffer, tmp);
951: }
952:
953: int len = strlen(buffer), res;
954: ptr = buffer;
955: while(len > 0) {
956: if((res = send(cli_socket, ptr, len, 0)) > 0) {
957: len -= res;
958: ptr += res;
959: }
960: }
961: }
962:
963: void telnet_command(const char *format, ...)
964: {
965: char buffer[1024];
966: va_list ap;
967: va_start(ap, format);
968: vsprintf(buffer, format, ap);
969: va_end(ap);
970:
971: telnet_send(buffer);
972: }
973:
974: void telnet_printf(const char *format, ...)
975: {
976: char buffer[1024];
977: va_list ap;
978: va_start(ap, format);
979: vsprintf(buffer, format, ap);
980: va_end(ap);
981:
982: if(fp_debugger != NULL) {
983: fprintf(fp_debugger, "%s", buffer);
984: }
985: telnet_send(buffer);
986: }
987:
988: bool telnet_gets(char *str, int n)
989: {
990: char buffer[1024];
991: int ptr = 0;
992:
993: telnet_command("\033[12l"); // local echo on
994: telnet_command("\033[2l"); // key unlock
995:
1.1.1.54 root 996: while(!m_exit) {
1.1.1.33 root 997: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
998:
999: if(len > 0 && buffer[0] != 0xff) {
1000: for(int i = 0; i < len; i++) {
1001: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
1002: str[ptr] = 0;
1003: telnet_command("\033[2h"); // key lock
1004: telnet_command("\033[12h"); // local echo off
1.1.1.54 root 1005: return(!m_exit);
1.1.1.33 root 1006: } else if(buffer[i] == 0x08) {
1007: if(ptr > 0) {
1008: telnet_command("\033[0K"); // erase from cursor position
1009: ptr--;
1010: } else {
1011: telnet_command("\033[1C"); // move cursor forward
1012: }
1013: } else if(ptr < n - 1) {
1.1.1.37 root 1014: if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33 root 1015: str[ptr++] = buffer[i];
1016: }
1017: } else {
1018: telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
1019: }
1020: }
1021: } else if(len == -1) {
1022: if(WSAGetLastError() != WSAEWOULDBLOCK) {
1023: return(false);
1024: }
1025: } else if(len == 0) {
1026: return(false);
1027: }
1028: Sleep(10);
1029: }
1.1.1.54 root 1030: return(!m_exit);
1.1.1.33 root 1031: }
1032:
1033: bool telnet_kbhit()
1034: {
1035: char buffer[1024];
1036:
1.1.1.54 root 1037: if(!m_exit) {
1.1.1.33 root 1038: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
1039:
1040: if(len > 0) {
1041: for(int i = 0; i < len; i++) {
1042: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
1043: return(true);
1044: }
1045: }
1046: } else if(len == 0) {
1047: return(true); // disconnected
1048: }
1049: }
1050: return(false);
1051: }
1052:
1053: bool telnet_disconnected()
1054: {
1055: char buffer[1024];
1056: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
1057:
1058: if(len == 0) {
1059: return(true);
1060: } else if(len == -1) {
1061: if(WSAGetLastError() != WSAEWOULDBLOCK) {
1062: return(true);
1063: }
1064: }
1065: return(false);
1066: }
1067:
1068: void telnet_set_color(int color)
1069: {
1070: telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
1071: }
1072:
1073: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
1074: {
1075: // UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
1076: UINT8 ops[16];
1077: for(int i = 0; i < 16; i++) {
1078: ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
1079: }
1080: UINT8 *oprom = ops;
1081:
1082: #if defined(HAS_I386)
1083: if(m_operand_size) {
1084: return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
1085: } else
1086: #endif
1087: return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
1088: }
1089:
1090: void debugger_regs_info(char *buffer)
1091: {
1092: #if defined(HAS_I386)
1093: UINT32 flags = get_flags();
1094: #else
1095: UINT32 flags = CompressFlags();
1096: #endif
1097: #if defined(HAS_I386)
1098: if(m_operand_size) {
1099: sprintf(buffer, "EAX=%08X EBX=%08X ECX=%08X EDX=%08X\nESP=%08X EBP=%08X ESI=%08X EDI=%08X\nEIP=%08X DS=%04X ES=%04X SS=%04X CS=%04X FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
1100: REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
1101: PROTECTED_MODE ? "PE" : "--",
1102: (flags & 0x40000) ? 'A' : '-',
1103: (flags & 0x20000) ? 'V' : '-',
1104: (flags & 0x10000) ? 'R' : '-',
1105: (flags & 0x04000) ? 'N' : '-',
1106: (flags & 0x02000) ? '1' : '0',
1107: (flags & 0x01000) ? '1' : '0',
1108: (flags & 0x00800) ? 'O' : '-',
1109: (flags & 0x00400) ? 'D' : '-',
1110: (flags & 0x00200) ? 'I' : '-',
1111: (flags & 0x00100) ? 'T' : '-',
1112: (flags & 0x00080) ? 'S' : '-',
1113: (flags & 0x00040) ? 'Z' : '-',
1114: (flags & 0x00010) ? 'A' : '-',
1115: (flags & 0x00004) ? 'P' : '-',
1116: (flags & 0x00001) ? 'C' : '-');
1117: } else {
1118: #endif
1119: sprintf(buffer, "AX=%04X BX=%04X CX=%04X DX=%04X SP=%04X BP=%04X SI=%04X DI=%04X\nIP=%04X DS=%04X ES=%04X SS=%04X CS=%04X FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
1120: REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
1121: #if defined(HAS_I386)
1122: PROTECTED_MODE ? "PE" : "--",
1123: #else
1124: "--",
1125: #endif
1126: (flags & 0x40000) ? 'A' : '-',
1127: (flags & 0x20000) ? 'V' : '-',
1128: (flags & 0x10000) ? 'R' : '-',
1129: (flags & 0x04000) ? 'N' : '-',
1130: (flags & 0x02000) ? '1' : '0',
1131: (flags & 0x01000) ? '1' : '0',
1132: (flags & 0x00800) ? 'O' : '-',
1133: (flags & 0x00400) ? 'D' : '-',
1134: (flags & 0x00200) ? 'I' : '-',
1135: (flags & 0x00100) ? 'T' : '-',
1136: (flags & 0x00080) ? 'S' : '-',
1137: (flags & 0x00040) ? 'Z' : '-',
1138: (flags & 0x00010) ? 'A' : '-',
1139: (flags & 0x00004) ? 'P' : '-',
1140: (flags & 0x00001) ? 'C' : '-');
1141: #if defined(HAS_I386)
1142: }
1143: #endif
1144: }
1145:
1146: void debugger_process_info(char *buffer)
1147: {
1148: UINT16 psp_seg = current_psp;
1149: process_t *process;
1150: bool check[0x10000] = {0};
1151:
1152: buffer[0] = '\0';
1153:
1154: while(!check[psp_seg] && (process = msdos_process_info_get(psp_seg, false)) != NULL) {
1155: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
1156: char *file = process->module_path, *s;
1157: char tmp[8192];
1158:
1159: while((s = strstr(file, "\\")) != NULL) {
1160: file = s + 1;
1161: }
1162: sprintf(tmp, "PSP=%04X ENV=%04X RETURN=%04X:%04X PROGRAM=%s\n", psp_seg, psp->env_seg, psp->int_22h.w.h, psp->int_22h.w.l, my_strupr(file));
1163: strcat(tmp, buffer);
1164: strcpy(buffer, tmp);
1165:
1166: check[psp_seg] = true;
1167: psp_seg = psp->parent_psp;
1168: }
1169: }
1170:
1171: UINT32 debugger_get_val(const char *str)
1172: {
1173: char tmp[1024];
1174:
1175: if(str == NULL || strlen(str) == 0) {
1176: return(0);
1177: }
1178: strcpy(tmp, str);
1179:
1180: if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
1181: // ank
1182: return(tmp[1] & 0xff);
1183: } else if(tmp[0] == '%') {
1184: // decimal
1185: return(strtoul(tmp + 1, NULL, 10));
1186: }
1187: return(strtoul(tmp, NULL, 16));
1188: }
1189:
1190: UINT32 debugger_get_seg(const char *str, UINT32 val)
1191: {
1192: char tmp[1024], *s;
1193:
1194: if(str == NULL || strlen(str) == 0) {
1195: return(val);
1196: }
1197: strcpy(tmp, str);
1198:
1199: if((s = strstr(tmp, ":")) != NULL) {
1200: // 0000:0000
1201: *s = '\0';
1202: return(debugger_get_val(tmp));
1203: }
1204: return(val);
1205: }
1206:
1207: UINT32 debugger_get_ofs(const char *str)
1208: {
1209: char tmp[1024], *s;
1210:
1211: if(str == NULL || strlen(str) == 0) {
1212: return(0);
1213: }
1214: strcpy(tmp, str);
1215:
1216: if((s = strstr(tmp, ":")) != NULL) {
1217: // 0000:0000
1218: return(debugger_get_val(s + 1));
1219: }
1220: return(debugger_get_val(tmp));
1221: }
1222:
1223: void debugger_main()
1224: {
1225: telnet_command("\033[20h"); // cr-lf
1226:
1227: force_suspend = true;
1228: now_going = false;
1229: now_debugging = true;
1230: Sleep(100);
1231:
1.1.1.54 root 1232: if(!m_exit && !now_suspended) {
1.1.1.33 root 1233: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1234: telnet_printf("waiting until cpu is suspended...\n");
1235: }
1.1.1.54 root 1236: while(!m_exit && !now_suspended) {
1.1.1.33 root 1237: if(telnet_disconnected()) {
1238: break;
1239: }
1240: Sleep(10);
1241: }
1242:
1243: char buffer[8192];
1244:
1245: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1246: debugger_process_info(buffer);
1247: telnet_printf("%s", buffer);
1248: debugger_regs_info(buffer);
1249: telnet_printf("%s", buffer);
1250: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1251: telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
1252: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1253: debugger_dasm(buffer, SREG(CS), m_eip);
1254: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1255: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1256:
1257: #define MAX_COMMAND_LEN 64
1258:
1259: char command[MAX_COMMAND_LEN + 1];
1260: char prev_command[MAX_COMMAND_LEN + 1] = {0};
1261:
1262: UINT32 data_seg = SREG(DS);
1263: UINT32 data_ofs = 0;
1264: UINT32 dasm_seg = SREG(CS);
1265: UINT32 dasm_ofs = m_eip;
1266:
1.1.1.54 root 1267: while(!m_exit) {
1.1.1.33 root 1268: telnet_printf("- ");
1269: command[0] = '\0';
1270:
1271: if(fi_debugger != NULL) {
1272: while(command[0] == '\0') {
1273: if(fgets(command, sizeof(command), fi_debugger) == NULL) {
1274: break;
1275: }
1276: while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
1277: command[strlen(command) - 1] = '\0';
1278: }
1279: }
1280: if(command[0] != '\0') {
1281: telnet_command("%s\n", command);
1282: }
1283: }
1284: if(command[0] == '\0') {
1285: if(!telnet_gets(command, sizeof(command))) {
1286: break;
1287: }
1288: }
1289: if(command[0] == '\0') {
1290: strcpy(command, prev_command);
1291: } else {
1292: strcpy(prev_command, command);
1293: }
1294: if(fp_debugger != NULL) {
1295: fprintf(fp_debugger, "%s\n", command);
1296: }
1297:
1.1.1.54 root 1298: if(!m_exit && command[0] != 0) {
1.1.1.33 root 1299: char *params[32], *token = NULL;
1300: int num = 0;
1301:
1302: if((token = strtok(command, " ")) != NULL) {
1303: params[num++] = token;
1304: while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
1305: params[num++] = token;
1306: }
1307: }
1308: if(stricmp(params[0], "D") == 0) {
1309: if(num <= 3) {
1310: if(num >= 2) {
1311: data_seg = debugger_get_seg(params[1], data_seg);
1312: data_ofs = debugger_get_ofs(params[1]);
1313: }
1314: UINT32 end_seg = data_seg;
1315: UINT32 end_ofs = data_ofs + 8 * 16 - 1;
1316: if(num == 3) {
1317: end_seg = debugger_get_seg(params[2], data_seg);
1318: end_ofs = debugger_get_ofs(params[2]);
1319: }
1320: UINT64 start_addr = (data_seg << 4) + data_ofs;
1321: UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37 root 1322: // bool is_sjis = false;
1.1.1.33 root 1323:
1324: for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
1325: if((addr & 0x0f) == 0) {
1326: if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
1327: data_seg += 0x1000;
1328: data_ofs -= 0x10000;
1329: }
1330: telnet_printf("%06X:%04X ", data_seg, data_ofs);
1331: memset(buffer, 0, sizeof(buffer));
1332: }
1333: if(addr < start_addr || addr > end_addr) {
1334: telnet_printf(" ");
1335: buffer[addr & 0x0f] = ' ';
1336: } else {
1337: UINT8 data = debugger_read_byte(addr & ADDR_MASK);
1338: telnet_printf(" %02X", data);
1.1.1.37 root 1339: // if(is_sjis) {
1.1.1.33 root 1340: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1341: // is_sjis = false;
1.1.1.33 root 1342: // } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
1343: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1344: // is_sjis = true;
1.1.1.33 root 1345: // } else
1346: if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
1347: buffer[addr & 0x0f] = data;
1348: } else {
1349: buffer[addr & 0x0f] = '.';
1350: }
1351: }
1352: if((addr & 0x0f) == 0x0f) {
1353: telnet_printf(" %s\n", buffer);
1354: }
1355: }
1356: if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
1357: data_seg += 0x1000;
1358: data_ofs -= 0x10000;
1359: }
1360: prev_command[1] = '\0'; // remove parameters to dump continuously
1361: } else {
1362: telnet_printf("invalid parameter number\n");
1363: }
1364: } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
1365: if(num >= 3) {
1366: UINT32 seg = debugger_get_seg(params[1], data_seg);
1367: UINT32 ofs = debugger_get_ofs(params[1]);
1368: for(int i = 2, j = 0; i < num; i++, j++) {
1369: debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
1370: }
1371: } else {
1372: telnet_printf("invalid parameter number\n");
1373: }
1374: } else if(stricmp(params[0], "EW") == 0) {
1375: if(num >= 3) {
1376: UINT32 seg = debugger_get_seg(params[1], data_seg);
1377: UINT32 ofs = debugger_get_ofs(params[1]);
1378: for(int i = 2, j = 0; i < num; i++, j += 2) {
1379: debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
1380: }
1381: } else {
1382: telnet_printf("invalid parameter number\n");
1383: }
1384: } else if(stricmp(params[0], "ED") == 0) {
1385: if(num >= 3) {
1386: UINT32 seg = debugger_get_seg(params[1], data_seg);
1387: UINT32 ofs = debugger_get_ofs(params[1]);
1388: for(int i = 2, j = 0; i < num; i++, j += 4) {
1389: debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
1390: }
1391: } else {
1392: telnet_printf("invalid parameter number\n");
1393: }
1394: } else if(stricmp(params[0], "EA") == 0) {
1395: if(num >= 3) {
1396: UINT32 seg = debugger_get_seg(params[1], data_seg);
1397: UINT32 ofs = debugger_get_ofs(params[1]);
1398: strcpy(buffer, prev_command);
1399: if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
1400: int len = strlen(token);
1401: for(int i = 0; i < len; i++) {
1402: debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
1403: }
1404: } else {
1405: telnet_printf("invalid parameter\n");
1406: }
1407: } else {
1408: telnet_printf("invalid parameter number\n");
1409: }
1410: } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
1411: if(num == 2) {
1412: telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
1413: } else {
1414: telnet_printf("invalid parameter number\n");
1415: }
1416: } else if(stricmp(params[0], "IW") == 0) {
1417: if(num == 2) {
1418: telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
1419: } else {
1420: telnet_printf("invalid parameter number\n");
1421: }
1422: } else if(stricmp(params[0], "ID") == 0) {
1423: if(num == 2) {
1424: telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
1425: } else {
1426: telnet_printf("invalid parameter number\n");
1427: }
1428: } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
1429: if(num == 3) {
1430: debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
1431: } else {
1432: telnet_printf("invalid parameter number\n");
1433: }
1434: } else if(stricmp(params[0], "OW") == 0) {
1435: if(num == 3) {
1436: debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
1437: } else {
1438: telnet_printf("invalid parameter number\n");
1439: }
1440: } else if(stricmp(params[0], "OD") == 0) {
1441: if(num == 3) {
1442: debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
1443: } else {
1444: telnet_printf("invalid parameter number\n");
1445: }
1446: } else if(stricmp(params[0], "R") == 0) {
1447: if(num == 1) {
1448: debugger_regs_info(buffer);
1449: telnet_printf("%s", buffer);
1450: } else if(num == 3) {
1451: #if defined(HAS_I386)
1452: if(stricmp(params[1], "EAX") == 0) {
1453: REG32(EAX) = debugger_get_val(params[2]);
1454: } else if(stricmp(params[1], "EBX") == 0) {
1455: REG32(EBX) = debugger_get_val(params[2]);
1456: } else if(stricmp(params[1], "ECX") == 0) {
1457: REG32(ECX) = debugger_get_val(params[2]);
1458: } else if(stricmp(params[1], "EDX") == 0) {
1459: REG32(EDX) = debugger_get_val(params[2]);
1460: } else if(stricmp(params[1], "ESP") == 0) {
1461: REG32(ESP) = debugger_get_val(params[2]);
1462: } else if(stricmp(params[1], "EBP") == 0) {
1463: REG32(EBP) = debugger_get_val(params[2]);
1464: } else if(stricmp(params[1], "ESI") == 0) {
1465: REG32(ESI) = debugger_get_val(params[2]);
1466: } else if(stricmp(params[1], "EDI") == 0) {
1467: REG32(EDI) = debugger_get_val(params[2]);
1468: } else
1469: #endif
1470: if(stricmp(params[1], "AX") == 0) {
1471: REG16(AX) = debugger_get_val(params[2]);
1472: } else if(stricmp(params[1], "BX") == 0) {
1473: REG16(BX) = debugger_get_val(params[2]);
1474: } else if(stricmp(params[1], "CX") == 0) {
1475: REG16(CX) = debugger_get_val(params[2]);
1476: } else if(stricmp(params[1], "DX") == 0) {
1477: REG16(DX) = debugger_get_val(params[2]);
1478: } else if(stricmp(params[1], "SP") == 0) {
1479: REG16(SP) = debugger_get_val(params[2]);
1480: } else if(stricmp(params[1], "BP") == 0) {
1481: REG16(BP) = debugger_get_val(params[2]);
1482: } else if(stricmp(params[1], "SI") == 0) {
1483: REG16(SI) = debugger_get_val(params[2]);
1484: } else if(stricmp(params[1], "DI") == 0) {
1485: REG16(DI) = debugger_get_val(params[2]);
1486: } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
1487: #if defined(HAS_I386)
1488: if(m_operand_size) {
1489: m_eip = debugger_get_val(params[2]);
1490: } else {
1491: m_eip = debugger_get_val(params[2]) & 0xffff;
1492: }
1493: CHANGE_PC(m_eip);
1494: #else
1495: m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
1496: CHANGE_PC(m_pc);
1497: #endif
1498: } else if(stricmp(params[1], "AL") == 0) {
1499: REG8(AL) = debugger_get_val(params[2]);
1500: } else if(stricmp(params[1], "AH") == 0) {
1501: REG8(AH) = debugger_get_val(params[2]);
1502: } else if(stricmp(params[1], "BL") == 0) {
1503: REG8(BL) = debugger_get_val(params[2]);
1504: } else if(stricmp(params[1], "BH") == 0) {
1505: REG8(BH) = debugger_get_val(params[2]);
1506: } else if(stricmp(params[1], "CL") == 0) {
1507: REG8(CL) = debugger_get_val(params[2]);
1508: } else if(stricmp(params[1], "CH") == 0) {
1509: REG8(CH) = debugger_get_val(params[2]);
1510: } else if(stricmp(params[1], "DL") == 0) {
1511: REG8(DL) = debugger_get_val(params[2]);
1512: } else if(stricmp(params[1], "DH") == 0) {
1513: REG8(DH) = debugger_get_val(params[2]);
1514: } else {
1515: telnet_printf("unknown register %s\n", params[1]);
1516: }
1517: } else {
1518: telnet_printf("invalid parameter number\n");
1519: }
1.1.1.60! root 1520: } else if(stricmp(params[0], "S") == 0) {
1.1.1.33 root 1521: if(num >= 4) {
1522: UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
1523: UINT32 cur_ofs = debugger_get_ofs(params[1]);
1524: UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
1525: UINT32 end_ofs = debugger_get_ofs(params[2]);
1526: UINT8 list[32];
1527:
1528: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1529: list[j] = debugger_get_val(params[i]);
1530: }
1531: while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
1532: bool found = true;
1533: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1534: if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
1535: found = false;
1536: break;
1537: }
1538: }
1539: if(found) {
1540: telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
1541: }
1542: if((cur_ofs += 1) > 0xffff) {
1543: cur_seg += 0x1000;
1544: cur_ofs -= 0x10000;
1545: }
1546: }
1547: } else {
1548: telnet_printf("invalid parameter number\n");
1549: }
1550: } else if(stricmp(params[0], "U") == 0) {
1551: if(num <= 3) {
1552: if(num >= 2) {
1553: dasm_seg = debugger_get_seg(params[1], dasm_seg);
1554: dasm_ofs = debugger_get_ofs(params[1]);
1555: }
1556: if(num == 3) {
1557: UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
1558: UINT32 end_ofs = debugger_get_ofs(params[2]);
1559:
1560: while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
1561: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1562: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1563: for(int i = 0; i < len; i++) {
1564: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1565: }
1566: for(int i = len; i < 8; i++) {
1567: telnet_printf(" ");
1568: }
1569: telnet_printf(" %s\n", buffer);
1570: if((dasm_ofs += len) > 0xffff) {
1571: dasm_seg += 0x1000;
1572: dasm_ofs -= 0x10000;
1573: }
1574: }
1575: } else {
1576: for(int i = 0; i < 16; i++) {
1577: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1578: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1579: for(int i = 0; i < len; i++) {
1580: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1581: }
1582: for(int i = len; i < 8; i++) {
1583: telnet_printf(" ");
1584: }
1585: telnet_printf(" %s\n", buffer);
1586: if((dasm_ofs += len) > 0xffff) {
1587: dasm_seg += 0x1000;
1588: dasm_ofs -= 0x10000;
1589: }
1590: }
1591: }
1592: prev_command[1] = '\0'; // remove parameters to disassemble continuously
1593: } else {
1594: telnet_printf("invalid parameter number\n");
1595: }
1596: } else if(stricmp(params[0], "H") == 0) {
1597: if(num == 3) {
1598: UINT32 l = debugger_get_val(params[1]);
1599: UINT32 r = debugger_get_val(params[2]);
1600: telnet_printf("%08X %08X\n", l + r, l - r);
1601: } else {
1602: telnet_printf("invalid parameter number\n");
1603: }
1604: } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
1605: break_point_t *break_point_ptr;
1606: #define GET_BREAK_POINT_PTR() { \
1.1.1.58 root 1607: if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33 root 1608: break_point_ptr = &rd_break_point; \
1.1.1.58 root 1609: } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33 root 1610: break_point_ptr = &wr_break_point; \
1.1.1.58 root 1611: } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33 root 1612: break_point_ptr = &in_break_point; \
1.1.1.58 root 1613: } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33 root 1614: break_point_ptr = &out_break_point; \
1615: } else { \
1616: break_point_ptr = &break_point; \
1617: } \
1618: }
1619: GET_BREAK_POINT_PTR();
1620: if(num == 2) {
1.1.1.58 root 1621: UINT32 seg = 0;
1622: if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
1623: seg = debugger_get_seg(params[1], data_seg);
1624: } else {
1625: seg = debugger_get_seg(params[1], SREG(CS));
1626: }
1.1.1.33 root 1627: UINT32 ofs = debugger_get_ofs(params[1]);
1628: bool found = false;
1629: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1630: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
1631: break_point_ptr->table[i].addr = (seg << 4) + ofs;
1632: break_point_ptr->table[i].seg = seg;
1633: break_point_ptr->table[i].ofs = ofs;
1634: break_point_ptr->table[i].status = 1;
1635: found = true;
1636: }
1637: }
1638: if(!found) {
1639: telnet_printf("too many break points\n");
1640: }
1641: } else {
1642: telnet_printf("invalid parameter number\n");
1643: }
1644: } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
1645: break_point_t *break_point_ptr;
1646: GET_BREAK_POINT_PTR();
1647: if(num == 2) {
1648: UINT32 addr = debugger_get_val(params[1]);
1649: bool found = false;
1650: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1651: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
1652: break_point_ptr->table[i].addr = addr;
1653: break_point_ptr->table[i].status = 1;
1654: found = true;
1655: }
1656: }
1657: if(!found) {
1658: telnet_printf("too many break points\n");
1659: }
1660: } else {
1661: telnet_printf("invalid parameter number\n");
1662: }
1663: } else if(stricmp(params[0], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
1664: break_point_t *break_point_ptr;
1665: GET_BREAK_POINT_PTR();
1666: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1667: memset(break_point_ptr, 0, sizeof(break_point_t));
1668: } else if(num >= 2) {
1669: for(int i = 1; i < num; i++) {
1670: int index = debugger_get_val(params[i]);
1671: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1672: telnet_printf("invalid index %x\n", index);
1673: } else {
1674: break_point_ptr->table[index - 1].addr = 0;
1675: break_point_ptr->table[index - 1].seg = 0;
1676: break_point_ptr->table[index - 1].ofs = 0;
1677: break_point_ptr->table[index - 1].status = 0;
1678: }
1679: }
1680: } else {
1681: telnet_printf("invalid parameter number\n");
1682: }
1683: } else if(stricmp(params[0], "BD") == 0 || stricmp(params[0], "RBD") == 0 || stricmp(params[0], "WBD") == 0 || stricmp(params[0], "IBD") == 0 || stricmp(params[0], "OBD") == 0 ||
1684: stricmp(params[0], "BE") == 0 || stricmp(params[0], "RBE") == 0 || stricmp(params[0], "WBE") == 0 || stricmp(params[0], "IBE") == 0 || stricmp(params[0], "OBE") == 0) {
1685: break_point_t *break_point_ptr;
1686: GET_BREAK_POINT_PTR();
1687: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1688: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1689: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1690: if(break_point_ptr->table[i].status != 0) {
1691: break_point_ptr->table[i].status = enabled ? 1 : -1;
1692: }
1693: }
1694: } else if(num >= 2) {
1695: for(int i = 1; i < num; i++) {
1696: int index = debugger_get_val(params[i]);
1697: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1698: telnet_printf("invalid index %x\n", index);
1699: } else if(break_point_ptr->table[index - 1].status == 0) {
1700: telnet_printf("break point %x is null\n", index);
1701: } else {
1702: break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
1703: }
1704: }
1705: } else {
1706: telnet_printf("invalid parameter number\n");
1707: }
1708: } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
1709: break_point_t *break_point_ptr;
1710: GET_BREAK_POINT_PTR();
1711: if(num == 1) {
1712: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1713: if(break_point_ptr->table[i].status) {
1714: telnet_printf("%d %c %04X:%04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].seg, break_point_ptr->table[i].ofs);
1715: }
1716: }
1717: } else {
1718: telnet_printf("invalid parameter number\n");
1719: }
1720: } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
1721: break_point_t *break_point_ptr;
1722: GET_BREAK_POINT_PTR();
1723: if(num == 1) {
1724: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1725: if(break_point_ptr->table[i].status) {
1726: telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
1727: }
1728: }
1729: } else {
1730: telnet_printf("invalid parameter number\n");
1731: }
1732: } else if(stricmp(params[0], "INTBP") == 0) {
1733: if(num >= 2 && num <= 4) {
1734: int int_num = debugger_get_val(params[1]);
1735: UINT8 ah = 0, ah_registered = 0;
1736: UINT8 al = 0, al_registered = 0;
1737: if(num >= 3) {
1738: ah = debugger_get_val(params[2]);
1739: ah_registered = 1;
1740: }
1741: if(num == 4) {
1742: al = debugger_get_val(params[3]);
1743: al_registered = 1;
1744: }
1745: bool found = false;
1746: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1747: if(int_break_point.table[i].status == 0 || (
1748: int_break_point.table[i].int_num == int_num &&
1749: int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
1750: int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
1751: int_break_point.table[i].int_num = int_num;
1752: int_break_point.table[i].ah = ah;
1753: int_break_point.table[i].ah_registered = ah_registered;
1754: int_break_point.table[i].al = al;
1755: int_break_point.table[i].al_registered = al_registered;
1756: int_break_point.table[i].status = 1;
1757: found = true;
1758: }
1759: }
1760: if(!found) {
1761: telnet_printf("too many break points\n");
1762: }
1763: } else {
1764: telnet_printf("invalid parameter number\n");
1765: }
1766: } else if(stricmp(params[0], "INTBC") == 0) {
1767: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1768: memset(&int_break_point, 0, sizeof(int_break_point_t));
1769: } else if(num >= 2) {
1770: for(int i = 1; i < num; i++) {
1771: int index = debugger_get_val(params[i]);
1772: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1773: telnet_printf("invalid index %x\n", index);
1774: } else {
1775: int_break_point.table[index - 1].int_num = 0;
1776: int_break_point.table[index - 1].ah = 0;
1777: int_break_point.table[index - 1].ah_registered = 0;
1778: int_break_point.table[index - 1].al = 0;
1779: int_break_point.table[index - 1].al_registered = 0;
1780: int_break_point.table[index - 1].status = 0;
1781: }
1782: }
1783: } else {
1784: telnet_printf("invalid parameter number\n");
1785: }
1786: } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
1787: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1788: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1789: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1790: if(int_break_point.table[i].status != 0) {
1791: int_break_point.table[i].status = enabled ? 1 : -1;
1792: }
1793: }
1794: } else if(num >= 2) {
1795: for(int i = 1; i < num; i++) {
1796: int index = debugger_get_val(params[i]);
1797: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1798: telnet_printf("invalid index %x\n", index);
1799: } else if(int_break_point.table[index - 1].status == 0) {
1800: telnet_printf("break point %x is null\n", index);
1801: } else {
1802: int_break_point.table[index - 1].status = enabled ? 1 : -1;
1803: }
1804: }
1805: } else {
1806: telnet_printf("invalid parameter number\n");
1807: }
1808: } else if(stricmp(params[0], "INTBL") == 0) {
1809: if(num == 1) {
1810: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1811: if(int_break_point.table[i].status) {
1812: telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
1813: if(int_break_point.table[i].ah_registered) {
1814: telnet_printf(" %02X", int_break_point.table[i].ah);
1815: }
1816: if(int_break_point.table[i].al_registered) {
1817: telnet_printf(" %02X", int_break_point.table[i].al);
1818: }
1819: telnet_printf("\n");
1820: }
1821: }
1822: } else {
1823: telnet_printf("invalid parameter number\n");
1824: }
1825: } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
1826: if(num == 1 || num == 2) {
1827: break_point_t break_point_stored;
1828: bool break_points_stored = false;
1829:
1830: if(stricmp(params[0], "P") == 0) {
1831: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1832: memset(&break_point, 0, sizeof(break_point_t));
1833: break_points_stored = true;
1834:
1835: break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
1836: break_point.table[0].status = 1;
1837: } else if(num >= 2) {
1838: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1839: memset(&break_point, 0, sizeof(break_point_t));
1840: break_points_stored = true;
1841:
1842: UINT32 seg = debugger_get_seg(params[1], SREG(CS));
1843: UINT32 ofs = debugger_get_ofs(params[1]);
1844: break_point.table[0].addr = (seg << 4) + ofs;
1845: break_point.table[0].seg = seg;
1846: break_point.table[0].ofs = ofs;
1847: break_point.table[0].status = 1;
1848: }
1849: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1850: now_going = true;
1851: now_suspended = false;
1852:
1853: telnet_command("\033[2l"); // key unlock
1.1.1.54 root 1854: while(!m_exit && !now_suspended) {
1.1.1.33 root 1855: if(telnet_kbhit()) {
1856: break;
1857: }
1858: Sleep(10);
1859: }
1860: now_going = false;
1861: telnet_command("\033[2h"); // key lock
1862:
1863: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1864: Sleep(100);
1.1.1.54 root 1865: if(!m_exit && !now_suspended) {
1.1.1.33 root 1866: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1867: telnet_printf("waiting until cpu is suspended...\n");
1868: }
1869: }
1.1.1.54 root 1870: while(!m_exit && !now_suspended) {
1.1.1.33 root 1871: if(telnet_disconnected()) {
1872: break;
1873: }
1874: Sleep(10);
1875: }
1876: dasm_seg = SREG(CS);
1877: dasm_ofs = m_eip;
1878:
1879: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1880: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1881: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1882:
1883: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1884: debugger_regs_info(buffer);
1885: telnet_printf("%s", buffer);
1886:
1887: if(break_point.hit) {
1888: if(stricmp(params[0], "G") == 0 && num == 1) {
1889: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1890: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1891: }
1892: } else if(rd_break_point.hit) {
1893: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1894: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1895: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1896: m_prev_cs, m_prev_eip);
1897: } else if(wr_break_point.hit) {
1898: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1899: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1900: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1901: m_prev_cs, m_prev_eip);
1902: } else if(in_break_point.hit) {
1903: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1904: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
1905: in_break_point.table[in_break_point.hit - 1].addr,
1906: m_prev_cs, m_prev_eip);
1907: } else if(out_break_point.hit) {
1908: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1909: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
1910: out_break_point.table[out_break_point.hit - 1].addr,
1911: m_prev_cs, m_prev_eip);
1912: } else if(int_break_point.hit) {
1913: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1914: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
1915: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
1916: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
1917: }
1918: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
1919: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
1920: }
1921: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
1922: } else {
1923: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1924: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
1925: }
1926: if(break_points_stored) {
1927: memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
1928: }
1929: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1930: debugger_dasm(buffer, SREG(CS), m_eip);
1931: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1932: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1933: } else {
1934: telnet_printf("invalid parameter number\n");
1935: }
1936: } else if(stricmp(params[0], "T") == 0) {
1937: if(num == 1 || num == 2) {
1938: int steps = 1;
1939: if(num >= 2) {
1940: steps = debugger_get_val(params[1]);
1941: }
1942:
1943: telnet_command("\033[2l"); // key unlock
1944: while(steps-- > 0) {
1945: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1946: now_going = false;
1947: now_suspended = false;
1948:
1.1.1.54 root 1949: while(!m_exit && !now_suspended) {
1.1.1.33 root 1950: if(telnet_disconnected()) {
1951: break;
1952: }
1953: Sleep(10);
1954: }
1955: dasm_seg = SREG(CS);
1956: dasm_ofs = m_eip;
1957:
1958: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1959: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1960: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1961:
1962: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1963: debugger_regs_info(buffer);
1964: telnet_printf("%s", buffer);
1965:
1966: if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
1967: break;
1968: }
1969: }
1970: telnet_command("\033[2h"); // key lock
1971:
1972: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1973: Sleep(100);
1.1.1.54 root 1974: if(!m_exit && !now_suspended) {
1.1.1.33 root 1975: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1976: telnet_printf("waiting until cpu is suspended...\n");
1977: }
1978: }
1.1.1.54 root 1979: while(!m_exit && !now_suspended) {
1.1.1.33 root 1980: if(telnet_disconnected()) {
1981: break;
1982: }
1983: Sleep(10);
1984: }
1985: if(break_point.hit) {
1986: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1987: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1988: } else if(rd_break_point.hit) {
1989: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1990: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1991: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1992: m_prev_cs, m_prev_eip);
1993: } else if(wr_break_point.hit) {
1994: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1995: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1996: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1997: m_prev_cs, m_prev_eip);
1998: } else if(in_break_point.hit) {
1999: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
2000: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
2001: in_break_point.table[in_break_point.hit - 1].addr,
2002: m_prev_cs, m_prev_eip);
2003: } else if(out_break_point.hit) {
2004: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
2005: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
2006: out_break_point.table[out_break_point.hit - 1].addr,
2007: m_prev_cs, m_prev_eip);
2008: } else if(int_break_point.hit) {
2009: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
2010: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
2011: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
2012: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
2013: }
2014: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
2015: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
2016: }
2017: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
2018: } else if(steps > 0) {
2019: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
2020: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
2021: }
2022: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
2023: debugger_dasm(buffer, SREG(CS), m_eip);
2024: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
2025: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
2026: } else {
2027: telnet_printf("invalid parameter number\n");
2028: }
2029: } else if(stricmp(params[0], "Q") == 0) {
2030: break;
2031: } else if(stricmp(params[0], "X") == 0) {
2032: debugger_process_info(buffer);
2033: telnet_printf("%s", buffer);
2034: } else if(stricmp(params[0], ">") == 0) {
2035: if(num == 2) {
2036: if(fp_debugger != NULL) {
2037: fclose(fp_debugger);
2038: fp_debugger = NULL;
2039: }
2040: fp_debugger = fopen(params[1], "w");
2041: } else {
2042: telnet_printf("invalid parameter number\n");
2043: }
2044: } else if(stricmp(params[0], "<") == 0) {
2045: if(num == 2) {
2046: if(fi_debugger != NULL) {
2047: fclose(fi_debugger);
2048: fi_debugger = NULL;
2049: }
2050: fi_debugger = fopen(params[1], "r");
2051: } else {
2052: telnet_printf("invalid parameter number\n");
2053: }
2054: } else if(stricmp(params[0], "?") == 0) {
2055: telnet_printf("D [<start> [<end>]] - dump memory\n");
2056: telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
2057: telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
2058: telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
2059: telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
2060:
2061: telnet_printf("R - show registers\n");
2062: telnet_printf("R <reg> <value> - edit register\n");
2063: telnet_printf("S <start> <end> <list> - search\n");
2064: telnet_printf("U [<start> [<end>]] - unassemble\n");
2065:
2066: telnet_printf("H <value> <value> - hexadd\n");
2067:
2068: telnet_printf("BP <address> - set breakpoint\n");
2069: telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
2070: telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
2071: telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
2072: telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
2073: telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
2074:
2075: telnet_printf("G - go (press enter key to break)\n");
2076: telnet_printf("G <address> - go and break at address\n");
2077: telnet_printf("P - trace one opcode (step over)\n");
2078: telnet_printf("T [<count>] - trace (step in)\n");
2079: telnet_printf("Q - quit\n");
2080: telnet_printf("X - show dos process info\n");
2081:
2082: telnet_printf("> <filename> - output logfile\n");
2083: telnet_printf("< <filename> - input commands from file\n");
2084:
2085: telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
2086: telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
2087: } else {
2088: telnet_printf("unknown command %s\n", params[0]);
2089: }
2090: }
2091: }
2092: if(fp_debugger != NULL) {
2093: fclose(fp_debugger);
2094: fp_debugger = NULL;
2095: }
2096: if(fi_debugger != NULL) {
2097: fclose(fi_debugger);
2098: fi_debugger = NULL;
2099: }
2100: now_debugging = now_going = now_suspended = force_suspend = false;
2101: closesocket(cli_socket);
2102: }
2103:
2104: const char *debugger_get_ttermpro_path()
2105: {
2106: static char path[MAX_PATH] = {0};
2107:
2108: if(getenv("ProgramFiles")) {
2109: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
2110: }
2111: return(path);
2112: }
2113:
2114: const char *debugger_get_ttermpro_x86_path()
2115: {
2116: static char path[MAX_PATH] = {0};
2117:
2118: if(getenv("ProgramFiles(x86)")) {
2119: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
2120: }
2121: return(path);
2122: }
2123:
2124: const char *debugger_get_putty_path()
2125: {
2126: static char path[MAX_PATH] = {0};
2127:
2128: if(getenv("ProgramFiles")) {
2129: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
2130: }
2131: return(path);
2132: }
2133:
2134: const char *debugger_get_putty_x86_path()
2135: {
2136: static char path[MAX_PATH] = {0};
2137:
2138: if(getenv("ProgramFiles(x86)")) {
2139: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
2140: }
2141: return(path);
2142: }
2143:
2144: const char *debugger_get_telnet_path()
2145: {
2146: // NOTE: When you run 32bit version of msdos.exe on Windows x64,
2147: // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
2148: // But 32bit version of telnet.exe will not be installed in SysWOW64
2149: // and 64bit version of telnet.exe will be installed in System32.
2150: static char path[MAX_PATH] = {0};
2151:
2152: if(getenv("windir") != NULL) {
2153: sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
2154: }
2155: return(path);
2156: }
2157:
2158: DWORD WINAPI debugger_thread(LPVOID)
2159: {
2160: WSADATA was_data;
2161: struct sockaddr_in svr_addr;
2162: struct sockaddr_in cli_addr;
2163: int cli_addr_len = sizeof(cli_addr);
2164: int port = 23;
2165: int bind_stat = SOCKET_ERROR;
2166: struct timeval timeout;
2167:
2168: WSAStartup(MAKEWORD(2,0), &was_data);
2169:
2170: if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
2171: memset(&svr_addr, 0, sizeof(svr_addr));
2172: svr_addr.sin_family = AF_INET;
2173: svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
2174:
1.1.1.54 root 2175: while(!m_exit && port < 10000) {
1.1.1.33 root 2176: svr_addr.sin_port = htons(port);
2177: if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
2178: break;
2179: } else {
2180: port = (port == 23) ? 9000 : (port + 1);
2181: }
2182: }
2183: if(bind_stat == 0) {
2184: timeout.tv_sec = 1;
2185: timeout.tv_usec = 0;
1.1.1.45 root 2186: setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33 root 2187:
2188: listen(svr_socket, 1);
2189:
2190: char command[MAX_PATH] = {0};
1.1.1.60! root 2191: STARTUPINFOA si;
1.1.1.33 root 2192: PROCESS_INFORMATION pi;
2193:
2194: if(_access(debugger_get_ttermpro_path(), 0) == 0) {
2195: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
2196: } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
2197: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
2198: } else if(_access(debugger_get_putty_path(), 0) == 0) {
2199: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
2200: } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
2201: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
2202: } else if(_access(debugger_get_telnet_path(), 0) == 0) {
2203: sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
2204: }
2205: if(command[0] != '\0') {
1.1.1.60! root 2206: memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33 root 2207: memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60! root 2208: CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33 root 2209: }
2210:
1.1.1.54 root 2211: while(!m_exit) {
1.1.1.33 root 2212: if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
2213: u_long val = 1;
2214: ioctlsocket(cli_socket, FIONBIO, &val);
2215: debugger_main();
2216: }
2217: }
2218: }
2219: }
2220: WSACleanup();
2221: return(0);
2222: }
2223: #endif
2224:
2225: /* ----------------------------------------------------------------------------
1.1 root 2226: main
2227: ---------------------------------------------------------------------------- */
2228:
1.1.1.28 root 2229: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
2230: {
2231: if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33 root 2232: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 2233: #ifdef USE_SERVICE_THREAD
2234: EnterCriticalSection(&key_buf_crit_sect);
2235: #endif
1.1.1.51 root 2236: pcbios_clear_key_buffer();
1.1.1.35 root 2237: #ifdef USE_SERVICE_THREAD
2238: LeaveCriticalSection(&key_buf_crit_sect);
2239: #endif
1.1.1.33 root 2240: }
2241: // key_code = key_recv = 0;
1.1.1.28 root 2242: return TRUE;
2243: } else if(dwCtrlType == CTRL_C_EVENT) {
2244: return TRUE;
2245: } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
2246: // this program will be terminated abnormally, do minimum end process
2247: exit_handler();
2248: exit(1);
2249: }
2250: return FALSE;
2251: }
2252:
2253: void exit_handler()
2254: {
2255: if(temp_file_created) {
1.1.1.60! root 2256: DeleteFileA(temp_file_path);
1.1.1.28 root 2257: temp_file_created = false;
2258: }
2259: if(key_buf_char != NULL) {
2260: key_buf_char->release();
2261: delete key_buf_char;
2262: key_buf_char = NULL;
2263: }
2264: if(key_buf_scan != NULL) {
2265: key_buf_scan->release();
2266: delete key_buf_scan;
2267: key_buf_scan = NULL;
2268: }
1.1.1.57 root 2269: if(key_buf_data != NULL) {
2270: key_buf_data->release();
2271: delete key_buf_data;
2272: key_buf_data = NULL;
2273: }
1.1.1.32 root 2274: #ifdef SUPPORT_XMS
2275: msdos_xms_release();
2276: #endif
1.1.1.28 root 2277: hardware_release();
2278: }
2279:
1.1.1.35 root 2280: #ifdef USE_VRAM_THREAD
1.1.1.28 root 2281: DWORD WINAPI vram_thread(LPVOID)
2282: {
1.1.1.54 root 2283: while(!m_exit) {
1.1.1.28 root 2284: EnterCriticalSection(&vram_crit_sect);
2285: if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
2286: vram_flush_char();
2287: }
2288: if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
2289: vram_flush_attr();
2290: }
2291: vram_last_length_char = vram_length_char;
2292: vram_last_length_attr = vram_length_attr;
2293: LeaveCriticalSection(&vram_crit_sect);
2294: // this is about half the maximum keyboard repeat rate - any
2295: // lower tends to be jerky, any higher misses updates
2296: Sleep(15);
2297: }
2298: return 0;
2299: }
2300: #endif
2301:
1.1.1.45 root 2302: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28 root 2303: {
2304: UINT8 header[0x400];
2305:
2306: long position = ftell(fp);
2307: fseek(fp, 0, SEEK_SET);
2308: fread(header, sizeof(header), 1, fp);
2309: fseek(fp, position, SEEK_SET);
2310:
2311: try {
2312: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2313: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2314: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2315: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2316: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2317: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2318:
2319: for(int i = 0; i < coffHeader->NumberOfSections; i++) {
2320: _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
2321: if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
2322: return(sectionHeader->PointerToRawData);
2323: }
2324: }
2325: } catch(...) {
2326: }
2327: return(0);
2328: }
2329:
1.1.1.10 root 2330: bool is_started_from_command_prompt()
2331: {
1.1.1.58 root 2332: bool result = false;
1.1.1.60! root 2333: HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58 root 2334:
1.1.1.18 root 2335: if(hLibrary) {
2336: typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
2337: GetConsoleProcessListFunction lpfnGetConsoleProcessList;
2338: lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58 root 2339: if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18 root 2340: DWORD pl;
1.1.1.58 root 2341: result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18 root 2342: FreeLibrary(hLibrary);
1.1.1.58 root 2343: return(result);
1.1.1.18 root 2344: }
2345: FreeLibrary(hLibrary);
2346: }
2347:
2348: HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2349: if(hSnapshot != INVALID_HANDLE_VALUE) {
2350: DWORD dwParentProcessID = 0;
2351: PROCESSENTRY32 pe32;
2352: pe32.dwSize = sizeof(PROCESSENTRY32);
2353: if(Process32First(hSnapshot, &pe32)) {
2354: do {
2355: if(pe32.th32ProcessID == GetCurrentProcessId()) {
2356: dwParentProcessID = pe32.th32ParentProcessID;
2357: break;
2358: }
2359: } while(Process32Next(hSnapshot, &pe32));
2360: }
2361: CloseHandle(hSnapshot);
2362: if(dwParentProcessID != 0) {
2363: HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
2364: if(hProcess != NULL) {
2365: HMODULE hMod;
2366: DWORD cbNeeded;
2367: if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
2368: char module_name[MAX_PATH];
1.1.1.60! root 2369: if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58 root 2370: result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18 root 2371: }
2372: }
2373: CloseHandle(hProcess);
2374: }
2375: }
2376: }
1.1.1.58 root 2377: return(result);
1.1.1.14 root 2378: }
2379:
2380: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
2381: {
1.1.1.60! root 2382: HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14 root 2383:
1.1.1.60! root 2384: if(hLibrary) {
! 2385: typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
! 2386: typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
! 2387:
! 2388: VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
! 2389: VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
! 2390:
! 2391: if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
! 2392: // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
! 2393: OSVERSIONINFOEXA osvi;
! 2394: DWORDLONG dwlConditionMask = 0;
! 2395: int op = VER_GREATER_EQUAL;
! 2396:
! 2397: // Initialize the OSVERSIONINFOEXA structure.
! 2398: ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
! 2399: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
! 2400: osvi.dwMajorVersion = dwMajorVersion;
! 2401: osvi.dwMinorVersion = dwMinorVersion;
! 2402: osvi.wServicePackMajor = wServicePackMajor;
! 2403: osvi.wServicePackMinor = wServicePackMinor;
! 2404:
! 2405: // Initialize the condition mask.
! 2406: #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
! 2407:
! 2408: MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
! 2409: MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
! 2410: MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
! 2411: MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
! 2412:
! 2413: // Perform the test.
! 2414: BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
! 2415: FreeLibrary(hLibrary);
! 2416: return(result);
! 2417: }
! 2418: FreeLibrary(hLibrary);
! 2419: }
! 2420:
! 2421: OSVERSIONINFOA osvi;
! 2422: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
! 2423:
! 2424: if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
! 2425: if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
! 2426: return(false);
! 2427: } else if(osvi.dwMajorVersion > dwMajorVersion) {
! 2428: return(true);
! 2429: } else if(osvi.dwMajorVersion < dwMajorVersion) {
! 2430: return(false);
! 2431: } else if(osvi.dwMinorVersion > dwMinorVersion) {
! 2432: return(true);
! 2433: } else if(osvi.dwMinorVersion < dwMinorVersion) {
! 2434: return(false);
! 2435: }
! 2436: // FIXME: check wServicePackMajor and wServicePackMinor :-(
! 2437: return(true);
! 2438: }
! 2439: return(false);
1.1.1.14 root 2440: }
2441:
1.1.1.58 root 2442: bool get_console_font_size(HANDLE hStdout, int *width, int *height)
2443: {
2444: bool result = false;
1.1.1.60! root 2445: HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58 root 2446:
2447: if(hLibrary) {
2448: typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
2449: GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
2450: if(lpfnGetCurrentConsoleFont) { // Windows XP or later
2451: CONSOLE_FONT_INFO fi;
2452: if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
2453: *width = fi.dwFontSize.X;
2454: *height = fi.dwFontSize.Y;
2455: result = true;
2456: }
2457: }
2458: FreeLibrary(hLibrary);
2459: }
2460: return(result);
2461: }
2462:
1.1.1.56 root 2463: bool set_console_font_size(HANDLE hStdout, int width, int height)
2464: {
2465: // http://d.hatena.ne.jp/aharisu/20090427/1240852598
2466: bool result = false;
1.1.1.60! root 2467: HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56 root 2468:
2469: if(hLibrary) {
2470: typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
2471: typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60! root 2472: typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56 root 2473: typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
2474: typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
2475:
2476: GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
2477: GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60! root 2478: GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56 root 2479: SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
2480: GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
2481:
2482: if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) {
1.1.1.58 root 2483: static const int offsets[] = {0, +1, -1, +2, -2, +3, -3};
1.1.1.56 root 2484: DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
2485: CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
2486: lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
2487: for(int i = 0; i < dwFontNum; i++) {
1.1.1.60! root 2488: fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
1.1.1.56 root 2489: }
1.1.1.58 root 2490: for(int h = 0; h < 5; h++) { // 0, +1, -1, +2, -2
2491: int height_tmp = height + offsets[h];
2492: for(int w = 0; w < 7; w++) { // 0, +1, -1, +2, -2, +3, -3
2493: int width_tmp = width + offsets[w];
2494: for(int i = 0; i < dwFontNum; i++) {
2495: if(fonts[i].dwFontSize.X == width_tmp && fonts[i].dwFontSize.Y == height_tmp) {
2496: lpfnSetConsoleFont(hStdout, fonts[i].nFont);
2497: if(lpfnGetCurrentConsoleFont) { // Windows XP or later
2498: CONSOLE_FONT_INFO fi;
2499: if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
2500: if(fi.dwFontSize.X == width_tmp && fi.dwFontSize.Y == height_tmp) {
2501: result = true;
2502: }
2503: }
2504: } else {
2505: result = true;
2506: }
2507: }
2508: if(result) {
2509: *(UINT16 *)(mem + 0x485) = height_tmp;
2510: goto exit_loop;
2511: }
2512: }
1.1.1.57 root 2513: }
1.1.1.56 root 2514: }
1.1.1.58 root 2515: exit_loop:
2516: free(fonts);
1.1.1.56 root 2517: }
2518: FreeLibrary(hLibrary);
2519: }
2520: return(result);
2521: }
2522:
1.1.1.59 root 2523: bool is_cursor_blink_off()
2524: {
2525: static int result = -1;
2526: HKEY hKey;
2527: char chData[64];
2528: DWORD dwSize = sizeof(chData);
2529:
2530: if(result == -1) {
2531: result = 0;
1.1.1.60! root 2532: if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
! 2533: if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59 root 2534: if(strncmp(chData, "-1", 2) == 0) {
2535: result = 1;
2536: }
2537: }
2538: RegCloseKey(hKey);
2539: }
2540: }
2541: return(result != 0);
2542: }
2543:
1.1.1.27 root 2544: void get_sio_port_numbers()
2545: {
2546: SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
2547: HDEVINFO hDevInfo = 0;
2548: HKEY hKey = 0;
1.1.1.60! root 2549: if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27 root 2550: for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
2551: if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
2552: char chData[256];
2553: DWORD dwType = 0;
2554: DWORD dwSize = sizeof(chData);
2555: int port_number = 0;
2556:
1.1.1.60! root 2557: if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27 root 2558: if(_strnicmp(chData, "COM", 3) == 0) {
2559: port_number = atoi(chData + 3);
2560: }
2561: }
2562: RegCloseKey(hKey);
2563:
1.1.1.29 root 2564: if(sio_port_number[0] == port_number || sio_port_number[1] == port_number || sio_port_number[2] == port_number || sio_port_number[3] == port_number) {
1.1.1.27 root 2565: continue;
2566: }
2567: if(sio_port_number[0] == 0) {
2568: sio_port_number[0] = port_number;
2569: } else if(sio_port_number[1] == 0) {
2570: sio_port_number[1] = port_number;
1.1.1.29 root 2571: } else if(sio_port_number[2] == 0) {
2572: sio_port_number[2] = port_number;
2573: } else if(sio_port_number[3] == 0) {
2574: sio_port_number[3] = port_number;
1.1.1.27 root 2575: }
1.1.1.29 root 2576: if(sio_port_number[0] != 0 && sio_port_number[1] != 0 && sio_port_number[2] != 0 && sio_port_number[3] != 0) {
1.1.1.27 root 2577: break;
2578: }
2579: }
2580: }
2581: }
2582: }
2583:
1.1.1.28 root 2584: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
2585:
1.1 root 2586: int main(int argc, char *argv[], char *envp[])
2587: {
1.1.1.9 root 2588: int arg_offset = 0;
2589: int standard_env = 0;
1.1.1.14 root 2590: int buf_width = 0, buf_height = 0;
1.1.1.28 root 2591: bool get_console_info_success = false;
1.1.1.56 root 2592: bool get_console_font_success = false;
1.1.1.28 root 2593: bool screen_size_changed = false;
2594:
1.1.1.60! root 2595: char path[MAX_PATH], full[MAX_PATH], *name = NULL;
! 2596: GetModuleFileNameA(NULL, path, MAX_PATH);
! 2597: GetFullPathNameA(path, MAX_PATH, full, &name);
1.1 root 2598:
1.1.1.27 root 2599: char dummy_argv_0[] = "msdos.exe";
2600: char dummy_argv_1[MAX_PATH];
2601: char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
2602: char new_exec_file[MAX_PATH];
2603: bool convert_cmd_file = false;
1.1.1.28 root 2604: unsigned int code_page = 0;
1.1.1.27 root 2605:
2606: if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28 root 2607: // check if command file is embedded to this execution file
2608: // if this execution file name is msdos.exe, don't check
1.1.1.27 root 2609: FILE* fp = fopen(full, "rb");
1.1.1.28 root 2610: long offset = get_section_in_exec_file(fp, ".msdos");
2611: if(offset != 0) {
1.1.1.30 root 2612: UINT8 buffer[16];
1.1.1.28 root 2613: fseek(fp, offset, SEEK_SET);
2614: fread(buffer, sizeof(buffer), 1, fp);
2615:
2616: // restore flags
2617: stay_busy = ((buffer[0] & 0x01) != 0);
2618: no_windows = ((buffer[0] & 0x02) != 0);
2619: standard_env = ((buffer[0] & 0x04) != 0);
2620: ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
2621: limit_max_memory = ((buffer[0] & 0x10) != 0);
2622: if((buffer[0] & 0x20) != 0) {
2623: get_sio_port_numbers();
2624: }
2625: if((buffer[0] & 0x40) != 0) {
2626: UMB_TOP = EMS_TOP + EMS_SIZE;
2627: support_ems = true;
1.1.1.30 root 2628: }
1.1.1.27 root 2629: #ifdef SUPPORT_XMS
1.1.1.30 root 2630: if((buffer[0] & 0x80) != 0) {
1.1.1.28 root 2631: support_xms = true;
2632: }
1.1.1.30 root 2633: #endif
1.1.1.28 root 2634: if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
2635: buf_width = buffer[1] | (buffer[2] << 8);
2636: buf_height = buffer[3] | (buffer[4] << 8);
2637: }
2638: if(buffer[5] != 0) {
1.1.1.30 root 2639: dos_major_version = buffer[5];
2640: dos_minor_version = buffer[6];
2641: }
2642: if(buffer[7] != 0) {
2643: win_major_version = buffer[7];
2644: win_minor_version = buffer[8];
1.1.1.28 root 2645: }
1.1.1.30 root 2646: if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28 root 2647: SetConsoleCP(code_page);
2648: SetConsoleOutputCP(code_page);
2649: }
1.1.1.30 root 2650: int name_len = buffer[11];
2651: int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28 root 2652:
2653: // restore command file name
2654: memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
2655: fread(dummy_argv_1, name_len, 1, fp);
2656:
2657: if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
2658: // if original command file exists, create a temporary file name
1.1.1.60! root 2659: if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28 root 2660: // create a temporary command file in the current director
1.1.1.60! root 2661: DeleteFileA(dummy_argv_1);
1.1.1.27 root 2662: } else {
1.1.1.28 root 2663: // create a temporary command file in the temporary folder
1.1.1.60! root 2664: GetTempPathA(MAX_PATH, path);
! 2665: if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
! 2666: DeleteFileA(dummy_argv_1);
1.1.1.28 root 2667: } else {
2668: sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
2669: }
1.1.1.27 root 2670: }
1.1.1.28 root 2671: // check the command file type
2672: fread(buffer, 2, 1, fp);
2673: fseek(fp, -2, SEEK_CUR);
2674: if(memcmp(buffer, "MZ", 2) != 0) {
2675: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
2676: } else {
2677: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27 root 2678: }
2679: }
1.1.1.28 root 2680:
2681: // restore command file
2682: FILE* fo = fopen(dummy_argv_1, "wb");
2683: for(int i = 0; i < file_len; i++) {
2684: fputc(fgetc(fp), fo);
2685: }
2686: fclose(fo);
2687:
1.1.1.60! root 2688: GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28 root 2689: temp_file_created = true;
1.1.1.60! root 2690: SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28 root 2691:
2692: // adjust argc/argv
2693: for(int i = 1; i < argc && (i + 1) < 256; i++) {
2694: dummy_argv[i + 1] = argv[i];
2695: }
2696: argc++;
2697: argv = dummy_argv;
1.1.1.27 root 2698: }
2699: fclose(fp);
2700: }
1.1.1.9 root 2701: for(int i = 1; i < argc; i++) {
1.1.1.25 root 2702: if(_strnicmp(argv[i], "-b", 2) == 0) {
2703: stay_busy = true;
2704: arg_offset++;
1.1.1.27 root 2705: } else if(_strnicmp(argv[i], "-c", 2) == 0) {
2706: if(argv[i][2] != '\0') {
2707: strcpy(new_exec_file, &argv[i][2]);
2708: } else {
2709: strcpy(new_exec_file, "new_exec_file.exe");
2710: }
2711: convert_cmd_file = true;
2712: arg_offset++;
1.1.1.28 root 2713: } else if(_strnicmp(argv[i], "-p", 2) == 0) {
2714: if(IS_NUMERIC(argv[i][2])) {
2715: code_page = atoi(&argv[i][2]);
2716: } else {
2717: code_page = GetConsoleCP();
2718: }
2719: arg_offset++;
1.1.1.25 root 2720: } else if(_strnicmp(argv[i], "-d", 2) == 0) {
2721: no_windows = true;
2722: arg_offset++;
2723: } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9 root 2724: standard_env = 1;
2725: arg_offset++;
1.1.1.14 root 2726: } else if(_strnicmp(argv[i], "-i", 2) == 0) {
2727: ignore_illegal_insn = true;
2728: arg_offset++;
2729: } else if(_strnicmp(argv[i], "-m", 2) == 0) {
2730: limit_max_memory = true;
2731: arg_offset++;
2732: } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51 root 2733: int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
2734: if(result == 1) {
2735: buf_width = 0;
2736: } else if(result != 2) {
1.1.1.17 root 2737: buf_width = buf_height = 0;
2738: }
2739: if(buf_width <= 0 || buf_width > 0x7fff) {
2740: buf_width = 80;
2741: }
2742: if(buf_height <= 0 || buf_height > 0x7fff) {
2743: buf_height = 25;
2744: }
1.1.1.14 root 2745: arg_offset++;
1.1.1.25 root 2746: } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28 root 2747: if(IS_NUMERIC(argv[i][2])) {
1.1.1.29 root 2748: char *p0 = &argv[i][2], *p1, *p2, *p3;
2749: if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
2750: sio_port_number[1] = atoi(p1 + 1);
2751: if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
2752: sio_port_number[2] = atoi(p2 + 1);
2753: if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
2754: sio_port_number[3] = atoi(p3 + 1);
2755: }
2756: }
1.1.1.25 root 2757: }
1.1.1.29 root 2758: sio_port_number[0] = atoi(p0);
1.1.1.25 root 2759: }
1.1.1.29 root 2760: if(sio_port_number[0] == 0 || sio_port_number[1] == 0 || sio_port_number[2] == 0 || sio_port_number[3] == 0) {
1.1.1.27 root 2761: get_sio_port_numbers();
1.1.1.25 root 2762: }
2763: arg_offset++;
1.1.1.9 root 2764: } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17 root 2765: if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
1.1.1.30 root 2766: dos_major_version = argv[i][2] - '0';
2767: dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
2768: }
2769: arg_offset++;
2770: } else if(_strnicmp(argv[i], "-w", 2) == 0) {
2771: if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
2772: win_major_version = argv[i][2] - '0';
2773: win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9 root 2774: }
2775: arg_offset++;
1.1.1.25 root 2776: } else if(_strnicmp(argv[i], "-x", 2) == 0) {
2777: UMB_TOP = EMS_TOP + EMS_SIZE;
2778: support_ems = true;
2779: #ifdef SUPPORT_XMS
2780: support_xms = true;
2781: #endif
2782: arg_offset++;
1.1.1.9 root 2783: } else {
2784: break;
2785: }
2786: }
2787: if(argc < 2 + arg_offset) {
1.1 root 2788: #ifdef _WIN64
1.1.1.14 root 2789: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1 root 2790: #else
1.1.1.14 root 2791: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1 root 2792: #endif
1.1.1.25 root 2793: fprintf(stderr,
1.1.1.28 root 2794: "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30 root 2795: " [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25 root 2796: "\n"
2797: "\t-b\tstay busy during keyboard polling\n"
1.1.1.28 root 2798: #ifdef _WIN64
1.1.1.27 root 2799: "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28 root 2800: #else
1.1.1.27 root 2801: "\t-c\tconvert command file to 32bit execution file\n"
2802: #endif
1.1.1.28 root 2803: "\t-p\trecord current code page when convert command file\n"
1.1.1.25 root 2804: "\t-d\tpretend running under straight DOS, not Windows\n"
2805: "\t-e\tuse a reduced environment block\n"
2806: "\t-i\tignore invalid instructions\n"
2807: "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
2808: "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
2809: "\t-s\tenable serial I/O and set host's COM port numbers\n"
2810: "\t-v\tset the DOS version\n"
1.1.1.30 root 2811: "\t-w\tset the Windows version\n"
1.1.1.19 root 2812: #ifdef SUPPORT_XMS
1.1.1.28 root 2813: "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19 root 2814: #else
1.1.1.28 root 2815: "\t-x\tenable LIM EMS\n"
1.1.1.19 root 2816: #endif
2817: );
1.1.1.10 root 2818:
2819: if(!is_started_from_command_prompt()) {
2820: fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
2821: while(!_kbhit()) {
2822: Sleep(10);
2823: }
2824: }
1.1.1.20 root 2825: #ifdef _DEBUG
2826: _CrtDumpMemoryLeaks();
2827: #endif
1.1 root 2828: return(EXIT_FAILURE);
2829: }
1.1.1.27 root 2830: if(convert_cmd_file) {
2831: retval = EXIT_FAILURE;
1.1.1.28 root 2832: if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27 root 2833: FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28 root 2834: int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27 root 2835:
1.1.1.28 root 2836: if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
2837: fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
2838: } else if((fp = fopen(full, "rb")) == NULL) {
2839: fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27 root 2840: } else {
1.1.1.28 root 2841: long offset = get_section_in_exec_file(fp, ".msdos");
2842: if(offset != 0) {
2843: UINT8 buffer[14];
2844: fseek(fp, offset, SEEK_SET);
2845: fread(buffer, sizeof(buffer), 1, fp);
2846: memset(path, 0, sizeof(path));
2847: fread(path, buffer[9], 1, fp);
2848: fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
2849: } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
2850: fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
2851: } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
2852: fprintf(stderr, "Can't open '%s'\n", new_exec_file);
2853: } else {
2854: // read pe header of msdos.exe
2855: UINT8 header[0x400];
2856: fseek(fp, 0, SEEK_SET);
2857: fread(header, sizeof(header), 1, fp);
2858:
2859: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2860: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2861: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2862: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2863: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2864: _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
2865: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2866:
2867: _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
2868: DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
2869: DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
2870: DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
2871: if(dwExtraLastSectionBytes != 0) {
2872: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
2873: dwLastSectionSize += dwRemain;
2874: }
2875: DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
2876:
2877: // store msdos.exe
2878: fseek(fp, 0, SEEK_SET);
2879: for(int i = 0; i < dwEndOfFile; i++) {
2880: if((data = fgetc(fp)) != EOF) {
2881: fputc(data, fo);
2882: } else {
2883: // we should not reach here :-(
2884: fputc(0, fo);
2885: }
2886: }
2887:
2888: // store options
2889: UINT8 flags = 0;
2890: if(stay_busy) {
2891: flags |= 0x01;
2892: }
2893: if(no_windows) {
2894: flags |= 0x02;
2895: }
2896: if(standard_env) {
2897: flags |= 0x04;
2898: }
2899: if(ignore_illegal_insn) {
2900: flags |= 0x08;
2901: }
2902: if(limit_max_memory) {
2903: flags |= 0x10;
2904: }
1.1.1.29 root 2905: if(sio_port_number[0] != 0 || sio_port_number[1] != 0 || sio_port_number[2] != 0 || sio_port_number[3] != 0) {
1.1.1.28 root 2906: flags |= 0x20;
2907: }
2908: if(support_ems) {
2909: flags |= 0x40;
2910: }
1.1.1.30 root 2911: #ifdef SUPPORT_XMS
2912: if(support_xms) {
2913: flags |= 0x80;
2914: }
2915: #endif
1.1.1.28 root 2916:
2917: fputc(flags, fo);
2918: fputc((buf_width >> 0) & 0xff, fo);
2919: fputc((buf_width >> 8) & 0xff, fo);
2920: fputc((buf_height >> 0) & 0xff, fo);
2921: fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30 root 2922: fputc(dos_major_version, fo);
2923: fputc(dos_minor_version, fo);
2924: fputc(win_major_version, fo);
2925: fputc(win_minor_version, fo);
1.1.1.28 root 2926: fputc((code_page >> 0) & 0xff, fo);
2927: fputc((code_page >> 8) & 0xff, fo);
2928:
2929: // store command file info
1.1.1.60! root 2930: GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28 root 2931: int name_len = strlen(name);
2932: fseek(fs, 0, SEEK_END);
2933: long file_size = ftell(fs);
2934:
2935: fputc(name_len, fo);
2936: fputc((file_size >> 0) & 0xff, fo);
2937: fputc((file_size >> 8) & 0xff, fo);
2938: fputc((file_size >> 16) & 0xff, fo);
2939: fputc((file_size >> 24) & 0xff, fo);
2940: fwrite(name, name_len, 1, fo);
2941:
2942: // store command file
2943: fseek(fs, 0, SEEK_SET);
2944: for(int i = 0; i < file_size; i++) {
2945: if((data = fgetc(fs)) != EOF) {
2946: fputc(data, fo);
2947: } else {
2948: // we should not reach here :-(
2949: fputc(0, fo);
2950: }
2951: }
2952:
2953: // store padding data and update pe header
1.1.1.29 root 2954: _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
2955: coffHeader->NumberOfSections++;
2956: memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
2957: memcpy(newSectionHeader->Name, ".msdos", 6);
2958: newSectionHeader->VirtualAddress = dwVirtualAddress;
2959: newSectionHeader->PointerToRawData = dwEndOfFile;
2960: newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28 root 2961: newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
2962: DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
2963: if(dwExtraRawBytes != 0) {
1.1.1.29 root 2964: static const char padding[] = "PADDINGXXPADDING";
1.1.1.28 root 2965: DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
2966: for(int i = 0; i < dwRemain; i++) {
1.1.1.29 root 2967: if(i < 2) {
2968: fputc(padding[i & 15], fo);
2969: } else {
2970: fputc(padding[(i - 2) & 15], fo);
2971: }
1.1.1.28 root 2972: }
2973: newSectionHeader->SizeOfRawData += dwRemain;
2974: }
2975: newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
2976:
2977: DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
2978: DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
2979: if(dwExtraNewSectionBytes != 0) {
2980: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
2981: dwNewSectionSize += dwRemain;
2982: }
2983: optionalHeader->SizeOfImage += dwNewSectionSize;
2984:
2985: fseek(fo, 0, SEEK_SET);
2986: fwrite(header, sizeof(header), 1, fo);
2987:
2988: fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
2989: retval = EXIT_SUCCESS;
1.1.1.27 root 2990: }
2991: }
2992: if(fp != NULL) {
2993: fclose(fp);
2994: }
2995: if(fs != NULL) {
2996: fclose(fs);
2997: }
2998: if(fo != NULL) {
2999: fclose(fo);
3000: }
3001: }
3002: #ifdef _DEBUG
3003: _CrtDumpMemoryLeaks();
3004: #endif
3005: return(retval);
3006: }
1.1 root 3007:
1.1.1.54 root 3008: is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14 root 3009: is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
3010:
1.1.1.23 root 3011: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 3012: CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14 root 3013: CONSOLE_CURSOR_INFO ci;
1.1.1.58 root 3014: int font_width = 10, font_height = 18; // default in english mode
1.1.1.23 root 3015:
1.1.1.28 root 3016: get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14 root 3017: GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59 root 3018: ci_old = ci_new = ci;
1.1.1.24 root 3019: GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.58 root 3020: get_console_font_success = get_console_font_size(hStdout, &font_width, &font_height);
1.1 root 3021:
1.1.1.14 root 3022: for(int y = 0; y < SCR_BUF_WIDTH; y++) {
3023: for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
3024: SCR_BUF(y,x).Char.AsciiChar = ' ';
3025: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 3026: }
3027: }
1.1.1.28 root 3028: if(get_console_info_success) {
1.1.1.12 root 3029: scr_width = csbi.dwSize.X;
1.1.1.14 root 3030: scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3031:
1.1.1.28 root 3032: // v-text shadow buffer size must be lesser than 0x7fd0
3033: if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14 root 3034: (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
3035: scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
3036: scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28 root 3037: if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14 root 3038: scr_width = 80;
3039: scr_height = 25;
3040: }
1.1.1.28 root 3041: screen_size_changed = true;
1.1.1.14 root 3042: }
1.1.1.12 root 3043: } else {
3044: // for a proof (not a console)
3045: scr_width = 80;
3046: scr_height = 25;
3047: }
1.1.1.14 root 3048: scr_buf_size.X = scr_width;
3049: scr_buf_size.Y = scr_height;
3050: scr_buf_pos.X = scr_buf_pos.Y = 0;
3051: scr_top = csbi.srWindow.Top;
1.1 root 3052: cursor_moved = false;
1.1.1.59 root 3053: cursor_moved_by_crtc = false;
1.1 root 3054:
1.1.1.54 root 3055: SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
3056:
1.1.1.35 root 3057: #ifdef USE_SERVICE_THREAD
3058: InitializeCriticalSection(&input_crit_sect);
3059: InitializeCriticalSection(&key_buf_crit_sect);
3060: InitializeCriticalSection(&putch_crit_sect);
1.1.1.50 root 3061: main_thread_id = GetCurrentThreadId();
1.1.1.35 root 3062: #endif
1.1.1.50 root 3063:
1.1.1.25 root 3064: key_buf_char = new FIFO(256);
3065: key_buf_scan = new FIFO(256);
1.1.1.57 root 3066: key_buf_data = new FIFO(256);
1.1 root 3067:
3068: hardware_init();
3069:
1.1.1.33 root 3070: #ifdef USE_DEBUGGER
3071: debugger_init();
3072: #endif
3073:
1.1.1.9 root 3074: if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1 root 3075: retval = EXIT_FAILURE;
3076: } else {
1.1.1.27 root 3077: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14 root 3078: _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
3079: #endif
3080: SetConsoleCtrlHandler(ctrl_handler, TRUE);
3081:
1.1.1.28 root 3082: if(screen_size_changed) {
1.1.1.24 root 3083: change_console_size(scr_width, scr_height);
3084: }
1.1.1.8 root 3085: TIMECAPS caps;
3086: timeGetDevCaps(&caps, sizeof(TIMECAPS));
3087: timeBeginPeriod(caps.wPeriodMin);
1.1.1.35 root 3088: #ifdef USE_VRAM_THREAD
1.1.1.14 root 3089: InitializeCriticalSection(&vram_crit_sect);
3090: CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
3091: #endif
1.1.1.33 root 3092: #ifdef USE_DEBUGGER
3093: CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
3094: // wait until telnet client starts and connects to me
3095: if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
3096: _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
3097: _access(debugger_get_putty_path(), 0) == 0 ||
3098: _access(debugger_get_putty_x86_path(), 0) == 0 ||
3099: _access(debugger_get_telnet_path(), 0) == 0) {
3100: for(int i = 0; i < 100 && cli_socket == 0; i++) {
3101: Sleep(100);
3102: }
3103: }
3104: #endif
1.1 root 3105: hardware_run();
1.1.1.35 root 3106: #ifdef USE_VRAM_THREAD
1.1.1.14 root 3107: vram_flush();
3108: DeleteCriticalSection(&vram_crit_sect);
3109: #endif
1.1.1.24 root 3110: timeEndPeriod(caps.wPeriodMin);
1.1.1.14 root 3111:
1.1.1.24 root 3112: // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.56 root 3113: if(get_console_font_success) {
3114: hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58 root 3115: set_console_font_size(hStdout, font_width, font_height);
1.1.1.56 root 3116: }
1.1.1.28 root 3117: if(get_console_info_success) {
1.1.1.23 root 3118: hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 3119: if(restore_console_on_exit) {
1.1.1.14 root 3120: // window can't be bigger than buffer,
3121: // buffer can't be smaller than window,
3122: // so make a tiny window,
3123: // set the required buffer,
3124: // then set the required window
3125: SMALL_RECT rect;
3126: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
3127: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12 root 3128: SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14 root 3129: SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12 root 3130: SetConsoleWindowInfo(hStdout, TRUE, &rect);
3131: }
1.1.1.14 root 3132: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
3133: SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12 root 3134: }
1.1.1.24 root 3135: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
3136:
1.1 root 3137: msdos_finish();
1.1.1.14 root 3138:
3139: SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1 root 3140: }
1.1.1.35 root 3141: if(temp_file_created) {
1.1.1.60! root 3142: DeleteFileA(temp_file_path);
1.1.1.35 root 3143: temp_file_created = false;
3144: }
1.1.1.10 root 3145: hardware_finish();
3146:
1.1.1.28 root 3147: if(key_buf_char != NULL) {
3148: key_buf_char->release();
3149: delete key_buf_char;
3150: key_buf_char = NULL;
3151: }
3152: if(key_buf_scan != NULL) {
3153: key_buf_scan->release();
3154: delete key_buf_scan;
3155: key_buf_scan = NULL;
3156: }
1.1.1.57 root 3157: if(key_buf_data != NULL) {
3158: key_buf_data->release();
3159: delete key_buf_data;
3160: key_buf_data = NULL;
3161: }
1.1.1.35 root 3162: #ifdef USE_SERVICE_THREAD
3163: DeleteCriticalSection(&input_crit_sect);
3164: DeleteCriticalSection(&key_buf_crit_sect);
3165: DeleteCriticalSection(&putch_crit_sect);
3166: #endif
1.1.1.20 root 3167: #ifdef _DEBUG
3168: _CrtDumpMemoryLeaks();
3169: #endif
1.1 root 3170: return(retval);
3171: }
3172:
1.1.1.20 root 3173: /* ----------------------------------------------------------------------------
3174: console
3175: ---------------------------------------------------------------------------- */
3176:
1.1.1.14 root 3177: void change_console_size(int width, int height)
1.1.1.12 root 3178: {
1.1.1.23 root 3179: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 3180: CONSOLE_SCREEN_BUFFER_INFO csbi;
3181: SMALL_RECT rect;
3182: COORD co;
3183:
3184: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14 root 3185: if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
3186: if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60! root 3187: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14 root 3188: SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60! root 3189: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 3190: } else if(csbi.dwCursorPosition.Y > height - 1) {
3191: SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60! root 3192: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 3193: SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60! root 3194: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12 root 3195: }
3196: }
1.1.1.14 root 3197: if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12 root 3198: co.X = csbi.dwCursorPosition.X;
1.1.1.14 root 3199: co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12 root 3200: SetConsoleCursorPosition(hStdout, co);
3201: cursor_moved = true;
1.1.1.59 root 3202: cursor_moved_by_crtc = false;
1.1.1.12 root 3203: }
1.1.1.14 root 3204:
3205: // window can't be bigger than buffer,
3206: // buffer can't be smaller than window,
3207: // so make a tiny window,
3208: // set the required buffer,
3209: // then set the required window
3210: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12 root 3211: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14 root 3212: co.X = width;
3213: co.Y = height;
1.1.1.12 root 3214: SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14 root 3215: SET_RECT(rect, 0, 0, width - 1, height - 1);
3216: SetConsoleWindowInfo(hStdout, TRUE, &rect);
3217:
3218: scr_width = scr_buf_size.X = width;
3219: scr_height = scr_buf_size.Y = height;
3220: scr_top = 0;
3221:
3222: clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
3223:
3224: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15 root 3225: text_vram_end_address = text_vram_top_address + regen;
3226: shadow_buffer_end_address = shadow_buffer_top_address + regen;
3227:
1.1.1.14 root 3228: if(regen > 0x4000) {
3229: regen = 0x8000;
3230: vram_pages = 1;
3231: } else if(regen > 0x2000) {
3232: regen = 0x4000;
3233: vram_pages = 2;
3234: } else if(regen > 0x1000) {
3235: regen = 0x2000;
3236: vram_pages = 4;
3237: } else {
3238: regen = 0x1000;
3239: vram_pages = 8;
3240: }
1.1.1.15 root 3241: *(UINT16 *)(mem + 0x44a) = scr_width;
3242: *(UINT16 *)(mem + 0x44c) = regen;
3243: *(UINT8 *)(mem + 0x484) = scr_height - 1;
3244:
1.1.1.24 root 3245: mouse.min_position.x = 0;
3246: mouse.min_position.y = 0;
1.1.1.34 root 3247: mouse.max_position.x = 8 * (scr_width - 1);
3248: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 3249:
1.1.1.15 root 3250: restore_console_on_exit = true;
1.1.1.14 root 3251: }
3252:
3253: void clear_scr_buffer(WORD attr)
3254: {
3255: for(int y = 0; y < scr_height; y++) {
3256: for(int x = 0; x < scr_width; x++) {
3257: SCR_BUF(y,x).Char.AsciiChar = ' ';
3258: SCR_BUF(y,x).Attributes = attr;
3259: }
3260: }
1.1.1.12 root 3261: }
3262:
1.1.1.24 root 3263: bool update_console_input()
1.1 root 3264: {
1.1.1.35 root 3265: #ifdef USE_SERVICE_THREAD
3266: EnterCriticalSection(&input_crit_sect);
3267: #endif
1.1.1.23 root 3268: HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8 root 3269: DWORD dwNumberOfEvents = 0;
1.1 root 3270: DWORD dwRead;
3271: INPUT_RECORD ir[16];
1.1.1.24 root 3272: CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
3273: bool result = false;
1.1 root 3274:
1.1.1.8 root 3275: if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
3276: if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
3277: for(int i = 0; i < dwRead; i++) {
1.1.1.24 root 3278: if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59 root 3279: if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
3280: if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
3281: // NOTE: if restore_console_on_exit, console is not scrolled
3282: if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
3283: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
3284: }
3285: // FIXME: character size is always 8x8 ???
3286: int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
3287: int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
3288:
3289: if(mouse.position.x != x || mouse.position.y != y) {
3290: mouse.position.x = x;
3291: mouse.position.y = y;
3292: mouse.status |= 1;
3293: mouse.status_alt |= 1;
3294: }
1.1.1.34 root 3295: }
1.1.1.59 root 3296: } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
3297: for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34 root 3298: static const DWORD bits[] = {
3299: FROM_LEFT_1ST_BUTTON_PRESSED, // left
3300: RIGHTMOST_BUTTON_PRESSED, // right
3301: FROM_LEFT_2ND_BUTTON_PRESSED, // middle
3302: };
1.1.1.59 root 3303: bool prev_status = mouse.buttons[j].status;
3304: mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34 root 3305:
1.1.1.59 root 3306: if(!prev_status && mouse.buttons[j].status) {
3307: mouse.buttons[j].pressed_times++;
3308: mouse.buttons[j].pressed_position.x = mouse.position.x;
3309: mouse.buttons[j].pressed_position.y = mouse.position.x;
3310: if(j < 2) {
3311: mouse.status_alt |= 2 << (j * 2);
1.1.1.43 root 3312: }
1.1.1.59 root 3313: mouse.status |= 2 << (j * 2);
3314: } else if(prev_status && !mouse.buttons[j].status) {
3315: mouse.buttons[j].released_times++;
3316: mouse.buttons[j].released_position.x = mouse.position.x;
3317: mouse.buttons[j].released_position.y = mouse.position.x;
3318: if(j < 2) {
3319: mouse.status_alt |= 4 << (j * 2);
1.1.1.43 root 3320: }
1.1.1.59 root 3321: mouse.status |= 4 << (j * 2);
1.1.1.14 root 3322: }
3323: }
3324: }
1.1.1.24 root 3325: } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33 root 3326: // update keyboard flags in bios data area
1.1.1.35 root 3327: if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
3328: mem[0x417] |= 0x40;
1.1.1.33 root 3329: } else {
1.1.1.35 root 3330: mem[0x417] &= ~0x40;
1.1.1.33 root 3331: }
1.1.1.35 root 3332: if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
3333: mem[0x417] |= 0x20;
1.1.1.33 root 3334: } else {
1.1.1.35 root 3335: mem[0x417] &= ~0x20;
3336: }
3337: if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
3338: mem[0x417] |= 0x10;
3339: } else {
3340: mem[0x417] &= ~0x10;
1.1.1.33 root 3341: }
3342: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43 root 3343: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3344: mouse.status_alt |= 0x80;
3345: }
1.1.1.33 root 3346: mem[0x417] |= 0x08;
3347: } else {
3348: mem[0x417] &= ~0x08;
3349: }
1.1.1.35 root 3350: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43 root 3351: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3352: mouse.status_alt |= 0x40;
3353: }
1.1.1.35 root 3354: mem[0x417] |= 0x04;
1.1.1.33 root 3355: } else {
1.1.1.35 root 3356: mem[0x417] &= ~0x04;
1.1.1.33 root 3357: }
3358: if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43 root 3359: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3360: mouse.status_alt |= 0x20;
3361: }
1.1.1.33 root 3362: if(!(mem[0x417] & 0x03)) {
3363: mem[0x417] |= 0x02; // left shift
3364: }
3365: } else {
3366: mem[0x417] &= ~0x03;
3367: }
1.1.1.35 root 3368: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
3369: mem[0x418] |= 0x02;
3370: } else {
3371: mem[0x418] &= ~0x02;
3372: }
3373: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
3374: mem[0x418] |= 0x01;
3375: } else {
3376: mem[0x418] &= ~0x01;
3377: }
1.1.1.33 root 3378:
1.1.1.28 root 3379: // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57 root 3380: // kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
3381: // kbd_status |= 1;
3382: UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33 root 3383:
3384: // update dos key buffer
3385: UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
3386: UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51 root 3387: UINT8 scn_old = scn;
1.1.1.33 root 3388:
3389: if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28 root 3390: // make
1.1.1.57 root 3391: tmp_data &= 0x7f;
1.1.1.24 root 3392:
1.1.1.33 root 3393: if(chr == 0x00) {
1.1.1.24 root 3394: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3395: if(scn >= 0x3b && scn <= 0x44) {
3396: scn += 0x68 - 0x3b; // F1 to F10
3397: } else if(scn == 0x57 || scn == 0x58) {
3398: scn += 0x8b - 0x57; // F11 & F12
3399: } else if(scn >= 0x47 && scn <= 0x53) {
3400: scn += 0x97 - 0x47; // edit/arrow clusters
3401: } else if(scn == 0x35) {
3402: scn = 0xa4; // keypad /
3403: }
3404: } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
3405: if(scn == 0x07) {
3406: chr = 0x1e; // Ctrl+^
3407: } else if(scn == 0x0c) {
3408: chr = 0x1f; // Ctrl+_
3409: } else if(scn >= 0x35 && scn <= 0x58) {
3410: static const UINT8 ctrl_map[] = {
3411: 0x95, // keypad /
3412: 0,
3413: 0x96, // keypad *
3414: 0, 0, 0,
3415: 0x5e, // F1
3416: 0x5f, // F2
3417: 0x60, // F3
3418: 0x61, // F4
3419: 0x62, // F5
3420: 0x63, // F6
3421: 0x64, // F7
3422: 0x65, // F8
3423: 0x66, // F9
3424: 0x67, // F10
3425: 0,
3426: 0,
3427: 0x77, // Home
3428: 0x8d, // Up
3429: 0x84, // PgUp
3430: 0x8e, // keypad -
3431: 0x73, // Left
3432: 0x8f, // keypad center
3433: 0x74, // Right
3434: 0x90, // keyapd +
3435: 0x75, // End
3436: 0x91, // Down
3437: 0x76, // PgDn
3438: 0x92, // Insert
3439: 0x93, // Delete
3440: 0, 0, 0,
3441: 0x89, // F11
3442: 0x8a, // F12
3443: };
3444: scn = ctrl_map[scn - 0x35];
3445: }
3446: } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
3447: if(scn >= 0x3b && scn <= 0x44) {
3448: scn += 0x54 - 0x3b; // F1 to F10
3449: } else if(scn == 0x57 || scn == 0x58) {
3450: scn += 0x87 - 0x57; // F11 & F12
3451: }
3452: } else if(scn == 0x57 || scn == 0x58) {
3453: scn += 0x85 - 0x57;
3454: }
3455: // ignore shift, ctrl, alt, win and menu keys
1.1.1.51 root 3456: if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32 root 3457: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3458: #ifdef USE_SERVICE_THREAD
3459: EnterCriticalSection(&key_buf_crit_sect);
3460: #endif
1.1.1.32 root 3461: if(chr == 0) {
1.1.1.51 root 3462: pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32 root 3463: }
1.1.1.51 root 3464: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3465: #ifdef USE_SERVICE_THREAD
3466: LeaveCriticalSection(&key_buf_crit_sect);
3467: #endif
1.1.1.24 root 3468: }
3469: }
3470: } else {
3471: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3472: chr = 0;
3473: if(scn >= 0x02 && scn <= 0x0e) {
3474: scn += 0x78 - 0x02; // 1 to 0 - =
3475: }
3476: }
1.1.1.32 root 3477: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3478: #ifdef USE_SERVICE_THREAD
3479: EnterCriticalSection(&key_buf_crit_sect);
3480: #endif
1.1.1.51 root 3481: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3482: #ifdef USE_SERVICE_THREAD
3483: LeaveCriticalSection(&key_buf_crit_sect);
3484: #endif
1.1.1.32 root 3485: }
1.1.1.24 root 3486: }
1.1.1.57 root 3487: } else {
3488: if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
3489: // ctrl-break, ctrl-c
3490: if(scn == 0x46) {
3491: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3492: #ifdef USE_SERVICE_THREAD
1.1.1.57 root 3493: EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35 root 3494: #endif
1.1.1.57 root 3495: pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35 root 3496: #ifdef USE_SERVICE_THREAD
1.1.1.57 root 3497: LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35 root 3498: #endif
1.1.1.57 root 3499: }
3500: ctrl_break_pressed = true;
3501: mem[0x471] = 0x80;
3502: raise_int_1bh = true;
3503: } else {
3504: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3505: #ifdef USE_SERVICE_THREAD
1.1.1.57 root 3506: EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35 root 3507: #endif
1.1.1.57 root 3508: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3509: #ifdef USE_SERVICE_THREAD
1.1.1.57 root 3510: LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35 root 3511: #endif
1.1.1.57 root 3512: }
3513: ctrl_c_pressed = (scn == 0x2e);
1.1.1.33 root 3514: }
3515: }
3516: // break
1.1.1.57 root 3517: tmp_data |= 0x80;
3518: }
3519: if(!(kbd_status & 1)) {
3520: kbd_data = tmp_data;
3521: kbd_status |= 1;
3522: } else {
3523: if(key_buf_data != NULL) {
3524: #ifdef USE_SERVICE_THREAD
3525: EnterCriticalSection(&key_buf_crit_sect);
3526: #endif
3527: key_buf_data->write(tmp_data);
3528: #ifdef USE_SERVICE_THREAD
3529: LeaveCriticalSection(&key_buf_crit_sect);
3530: #endif
3531: }
1.1 root 3532: }
1.1.1.24 root 3533: result = key_changed = true;
1.1.1.36 root 3534: // IME may be on and it may causes screen scroll up and cursor position change
3535: cursor_moved = true;
1.1 root 3536: }
3537: }
3538: }
3539: }
1.1.1.35 root 3540: #ifdef USE_SERVICE_THREAD
3541: LeaveCriticalSection(&input_crit_sect);
3542: #endif
1.1.1.24 root 3543: return(result);
1.1.1.8 root 3544: }
3545:
1.1.1.14 root 3546: bool update_key_buffer()
1.1.1.8 root 3547: {
1.1.1.35 root 3548: if(update_console_input()) {
3549: return(true);
3550: }
3551: if(key_buf_char != NULL && key_buf_scan != NULL) {
3552: #ifdef USE_SERVICE_THREAD
3553: EnterCriticalSection(&key_buf_crit_sect);
3554: #endif
1.1.1.55 root 3555: bool empty = pcbios_is_key_buffer_empty();
1.1.1.35 root 3556: #ifdef USE_SERVICE_THREAD
3557: LeaveCriticalSection(&key_buf_crit_sect);
3558: #endif
3559: if(!empty) return(true);
3560: }
3561: return(false);
1.1.1.8 root 3562: }
3563:
1.1.1.20 root 3564: /* ----------------------------------------------------------------------------
3565: MS-DOS virtual machine
3566: ---------------------------------------------------------------------------- */
3567:
1.1.1.32 root 3568: static const struct {
1.1.1.33 root 3569: char *name;
3570: DWORD lcid;
3571: char *std;
3572: char *dlt;
3573: } tz_table[] = {
3574: // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
3575: // 0 GMT Greenwich Mean Time GMT0
3576: {"GMT Standard Time", 0x0809, "GMT", "BST"}, // (UTC+00:00) GB London (en-gb)
3577: {"GMT Standard Time", 0x1809, "GMT", "IST"}, // (UTC+00:00) IE Dublin (en-ie)
3578: {"GMT Standard Time", 0x0000, "WET", "WES"}, // (UTC+00:00) PT Lisbon
3579: {"Greenwich Standard Time", 0x0000, "GMT", "GST"}, // (UTC+00:00) IS Reykjavik
3580: // 2 FST FDT Fernando De Noronha Std FST2FDT
3581: {"Mid-Atlantic Standard Time", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3582: {"UTC-02", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3583: // 3 BST Brazil Standard Time BST3
3584: {"Bahia Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Bahia
3585: {"SA Eastern Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Fortaleza
3586: {"Tocantins Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Palmas
3587: // 3 EST EDT Eastern Standard (Brazil) EST3EDT
3588: {"E. South America Standard Time", 0x0000, "EST", "EDT"}, // (UTC-03:00) BR Sao Paulo
3589: // 3 GST Greenland Standard Time GST3
3590: {"Greenland Standard Time", 0x0000, "GST", "GDT"}, // (UTC-03:00) GL Godthab
3591: // 3:30 NST NDT Newfoundland Standard Time NST3:30NDT
3592: {"Newfoundland Standard Time", 0x0000, "NST", "NDT"}, // (UTC-03:30) CA St.Johns
3593: // 4 AST ADT Atlantic Standard Time AST4ADT
3594: {"Atlantic Standard Time", 0x0000, "AST", "ADT"}, // (UTC-04:00) CA Halifax
3595: // 4 WST WDT Western Standard (Brazil) WST4WDT
3596: {"Central Brazilian Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Cuiaba
3597: {"SA Western Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Manaus
3598: // 5 EST EDT Eastern Standard Time EST5EDT
3599: {"Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US New York
3600: {"Eastern Standard Time (Mexico)", 0x0000, "EST", "EDT"}, // (UTC-05:00) MX Cancun
3601: {"US Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US Indianapolis
3602: // 5 CST CDT Chile Standard Time CST5CDT
3603: {"Pacific SA Standard Time", 0x0000, "CST", "CDT"}, // (UTC-04:00) CL Santiago
3604: // 5 AST ADT Acre Standard Time AST5ADT
3605: {"SA Pacific Standard Time", 0x0000, "AST", "ADT"}, // (UTC-05:00) BR Rio Branco
3606: // 5 CST CDT Cuba Standard Time CST5CDT
3607: {"Cuba Standard Time", 0x0000, "CST", "CDT"}, // (UTC-05:00) CU Havana
3608: // 6 CST CDT Central Standard Time CST6CDT
3609: {"Canada Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) CA Regina
3610: {"Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) US Chicago
3611: {"Central Standard Time (Mexico)", 0x0000, "CST", "CDT"}, // (UTC-06:00) MX Mexico City
3612: // 6 EST EDT Easter Island Standard EST6EDT
3613: {"Easter Island Standard Time", 0x0000, "EST", "EDT"}, // (UTC-06:00) CL Easter
3614: // 7 MST MDT Mountain Standard Time MST7MDT
3615: {"Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Denver
3616: {"Mountain Standard Time (Mexico)", 0x0000, "MST", "MDT"}, // (UTC-07:00) MX Chihuahua
3617: {"US Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Phoenix
3618: // 8 PST PDT Pacific Standard Time PST8PDT
3619: {"Pacific Standard Time", 0x0000, "PST", "PDT"}, // (UTC-08:00) US Los Angeles
3620: {"Pacific Standard Time (Mexico)", 0x0000, "PST", "PDT"}, // (UTC-08:00) MX Tijuana
3621: // 9 AKS AKD Alaska Standard Time AKS9AKD
3622: // 9 YST YDT Yukon Standard Time YST9YST
3623: {"Alaskan Standard Time", 0x0000, "AKS", "AKD"}, // (UTC-09:00) US Anchorage
3624: // 10 HST HDT Hawaii Standard Time HST10HDT
3625: {"Aleutian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Aleutian
3626: {"Hawaiian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Honolulu
3627: // 11 SST Samoa Standard Time SST11
3628: {"Samoa Standard Time", 0x0000, "SST", "SDT"}, // (UTC-11:00) US Samoa
3629: // -12 NZS NZD New Zealand Standard Time NZS-12NZD
3630: {"New Zealand Standard Time", 0x0000, "NZS", "NZD"}, // (UTC+12:00) NZ Auckland
3631: // -10 GST Guam Standard Time GST-10
3632: {"West Pacific Standard Time", 0x0000, "GST", "GDT"}, // (UTC+10:00) GU Guam
3633: // -10 EAS EAD Eastern Australian Standard EAS-10EAD
3634: {"AUS Eastern Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Sydney
3635: {"E. Australia Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Brisbane
3636: {"Tasmania Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Hobart
3637: // -9:30 CAS CAD Central Australian Standard CAS-9:30CAD
3638: {"AUS Central Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Darwin
3639: {"Cen. Australia Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Adelaide
3640: // -9 JST Japan Standard Time JST-9
3641: {"Tokyo Standard Time", 0x0000, "JST", "JDT"}, // (UTC+09:00) JP Tokyo
3642: // -9 KST KDT Korean Standard Time KST-9KDT
3643: {"Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+09:00) KR Seoul
3644: {"North Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+08:30) KP Pyongyang
3645: // -8 HKT Hong Kong Time HKT-8
3646: {"China Standard Time", 0x0C04, "HKT", "HKS"}, // (UTC+08:00) HK Hong Kong (zh-hk)
3647: // -8 CCT China Coast Time CCT-8
3648: {"China Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) CN Shanghai
3649: {"Taipei Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) TW Taipei
3650: // -8 SST Singapore Standard Time SST-8
3651: {"Singapore Standard Time", 0x0000, "SST", "SDT"}, // (UTC+08:00) SG Singapore
3652: // -8 WAS WAD Western Australian Standard WAS-8WAD
3653: {"Aus Central W. Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:45) AU Eucla
3654: {"W. Australia Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:00) AU Perth
3655: // -7:30 JT Java Standard Time JST-7:30
3656: // -7 NST North Sumatra Time NST-7
3657: {"SE Asia Standard Time", 0x0000, "NST", "NDT"}, // (UTC+07:00) ID Jakarta
3658: // -5:30 IST Indian Standard Time IST-5:30
3659: {"India Standard Time", 0x0000, "IST", "IDT"}, // (UTC+05:30) IN Calcutta
3660: // -3:30 IST IDT Iran Standard Time IST-3:30IDT
3661: {"Iran Standard Time", 0x0000, "IST", "IDT"}, // (UTC+03:30) IR Tehran
3662: // -3 MSK MSD Moscow Winter Time MSK-3MSD
3663: {"Belarus Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) BY Minsk
3664: {"Russian Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) RU Moscow
3665: // -2 EET Eastern Europe Time EET-2
3666: {"E. Europe Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) MD Chisinau
3667: {"FLE Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) UA Kiev
3668: {"GTB Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RO Bucharest
3669: {"Kaliningrad Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RU Kaliningrad
3670: // -2 IST IDT Israel Standard Time IST-2IDT
3671: {"Israel Standard Time", 0x0000, "IST", "IDT"}, // (UTC+02:00) IL Jerusalem
3672: // -1 MEZ MES Middle European Time MEZ-1MES
3673: // -1 SWT SST Swedish Winter Time SWT-1SST
3674: // -1 FWT FST French Winter Time FWT-1FST
3675: // -1 CET CES Central European Time CET-1CES
3676: {"Central Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) HU Budapest
3677: {"Central European Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) PL Warsaw
3678: {"Romance Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) FR Paris
3679: {"W. Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) DE Berlin
3680: // -1 WAT West African Time WAT-1
3681: {"Namibia Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NA Windhoek
3682: {"W. Central Africa Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NG Lagos
3683: // 0 UTC Universal Coordinated Time UTC0
3684: {"UTC", 0x0000, "UTC", "" }, // (UTC+00:00) GMT+0
3685: {"UTC-02", 0x0000, "UTC", "" }, // (UTC-02:00) GMT+2
3686: {"UTC-08", 0x0000, "UTC", "" }, // (UTC-08:00) GMT+8
3687: {"UTC-09", 0x0000, "UTC", "" }, // (UTC-09:00) GMT+9
3688: {"UTC-11", 0x0000, "UTC", "" }, // (UTC-11:00) GMT+11
3689: {"UTC+12", 0x0000, "UTC", "" }, // (UTC+12:00) GMT-12
3690: };
3691:
1.1.1.53 root 3692: // FIXME: consider to build on non-Japanese environment :-(
3693: // message_japanese string must be in shift-jis
3694:
1.1.1.33 root 3695: static const struct {
1.1.1.32 root 3696: UINT16 code;
3697: char *message_english;
3698: char *message_japanese;
3699: } standard_error_table[] = {
3700: {0x01, "Invalid function", "�����ȃt�@���N�V�����ł�."},
3701: {0x02, "File not found", "�t�@�C����������܂���."},
3702: {0x03, "Path not found", "�p�X��������܂���."},
3703: {0x04, "Too many open files", "�J����Ă���t�@�C�����������܂�."},
3704: {0x05, "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
3705: {0x06, "Invalid handle", "�����ȃn���h���ł�."},
3706: {0x07, "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
3707: {0x08, "Insufficient memory", "������������܂���."},
3708: {0x09, "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
3709: {0x0A, "Invalid Environment", "�����Ȋ��ł�."},
3710: {0x0B, "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
3711: {0x0C, "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
3712: {0x0D, "Invalid data", "�����ȃf�[�^�ł�."},
3713: {0x0F, "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
3714: {0x10, "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
3715: {0x11, "Not same device", "�����f�o�C�X�ł͂���܂���."},
3716: {0x12, "No more files", "�t�@�C���͂���ȏ゠��܂���."},
3717: {0x13, "Write protect error", "�������ݕی�G���[�ł�."},
3718: {0x14, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3719: {0x15, "Not ready", "�������ł��Ă��܂���."},
3720: {0x16, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3721: {0x17, "Data error", "�f�[�^�G���[�ł�."},
3722: {0x18, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3723: {0x19, "Seek error", "�V�[�N�G���[�ł�."},
3724: {0x1A, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3725: {0x1B, "Sector not found", "�Z�N�^��������܂���."},
3726: {0x1C, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3727: {0x1D, "Write fault error", "�������݃G���[�ł�."},
3728: {0x1E, "Read fault error", "�ǂݎ��G���[�ł�."},
3729: {0x1F, "General failure", "�G���[�ł�."},
3730: {0x20, "Sharing violation", "���L�ᔽ�ł�."},
3731: {0x21, "Lock violation", "���b�N�ᔽ�ł�."},
3732: {0x22, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3733: {0x23, "FCB unavailable", "FCB �͎g���܂���."},
3734: {0x24, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3735: {0x25, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3736: {0x26, "Out of input", "���͂��I���܂���."},
3737: {0x27, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3738: /*
3739: {0x32, "Network request not supported", NULL},
3740: {0x33, "Remote computer not listening", NULL},
3741: {0x34, "Duplicate name on network", NULL},
3742: {0x35, "Network name not found", NULL},
3743: {0x36, "Network busy", NULL},
3744: {0x37, "Network device no longer exists", NULL},
3745: {0x38, "Network BIOS command limit exceeded", NULL},
3746: {0x39, "Network adapter hardware error", NULL},
3747: {0x3A, "Incorrect response from network", NULL},
3748: {0x3B, "Unexpected network error", NULL},
3749: {0x3C, "Incompatible remote adapter", NULL},
3750: {0x3D, "Print queue full", NULL},
3751: {0x3E, "Queue not full", NULL},
3752: {0x3F, "Not enough space to print file", NULL},
3753: {0x40, "Network name was deleted", NULL},
3754: {0x41, "Network: Access denied", NULL},
3755: {0x42, "Network device type incorrect", NULL},
3756: {0x43, "Network name not found", NULL},
3757: {0x44, "Network name limit exceeded", NULL},
3758: {0x45, "Network BIOS session limit exceeded", NULL},
3759: {0x46, "Temporarily paused", NULL},
3760: {0x47, "Network request not accepted", NULL},
3761: {0x48, "Network print/disk redirection paused", NULL},
3762: {0x49, "Network software not installed", NULL},
3763: {0x4A, "Unexpected adapter close", NULL},
3764: */
3765: {0x50, "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33 root 3766: {0x52, "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32 root 3767: {0x53, "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
3768: {0x54, "Too many redirections", "���_�C���N�g���������܂�."},
3769: {0x55, "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
3770: {0x56, "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
3771: {0x57, "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
3772: {0x58, "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
3773: {0x59, "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
3774: {0x5A, "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53 root 3775: #ifdef SUPPORT_MSCDEX
1.1.1.32 root 3776: {0x64, "Unknown error", "�s���ȃG���[�ł�."},
3777: {0x65, "Not ready", "�������ł��Ă��܂���."},
3778: {0x66, "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
3779: {0x67, "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
3780: {0x68, "Door open", "���o�[���܂��Ă��܂���."
1.1.1.53 root 3781: #endif
1.1.1.32 root 3782: {0xB0, "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
3783: {0xB1, "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
3784: {0xB2, "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
3785: {0xB4, "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
3786: {0xB5, "A valid eject request failed", "���o���Ɏ��s���܂���."},
3787: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3788: };
3789:
3790: static const struct {
3791: UINT16 code;
3792: char *message_english;
3793: char *message_japanese;
3794: } param_error_table[] = {
3795: {0x01, "Too many parameters", "�p�����[�^���������܂�."},
3796: {0x02, "Required parameter missing", "�p�����[�^������܂���."},
3797: {0x03, "Invalid switch", "�����ȃX�C�b�`�ł�."},
3798: {0x04, "Invalid keyword", "�����ȃL�[���[�h�ł�."},
3799: {0x06, "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂��Ă��܂�."},
3800: {0x07, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3801: {0x08, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3802: {0x09, "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
3803: {0x0A, "Invalid parameter", "�����ȃp�����[�^�ł�."},
3804: {0x0B, "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
3805: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3806: };
3807:
3808: static const struct {
3809: UINT16 code;
3810: char *message_english;
3811: char *message_japanese;
3812: } critical_error_table[] = {
3813: {0x00, "Write protect error", "�������ݕی�G���[�ł�."},
3814: {0x01, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3815: {0x02, "Not ready", "�������ł��Ă��܂���."},
3816: {0x03, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3817: {0x04, "Data error", "�f�[�^�G���[�ł�."},
3818: {0x05, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3819: {0x06, "Seek error", "�V�[�N�G���[�ł�."},
3820: {0x07, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3821: {0x08, "Sector not found", "�Z�N�^��������܂���."},
3822: {0x09, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3823: {0x0A, "Write fault error", "�������݃G���[�ł�."},
3824: {0x0B, "Read fault error", "�ǂݎ��G���[�ł�."},
3825: {0x0C, "General failure", "�G���[�ł�."},
3826: {0x0D, "Sharing violation", "���L�ᔽ�ł�."},
3827: {0x0E, "Lock violation", "���b�N�ᔽ�ł�."},
3828: {0x0F, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3829: {0x10, "FCB unavailable", "FCB �͎g���܂���."},
3830: {0x11, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3831: {0x12, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3832: {0x13, "Out of input", "���͂��I���܂���."},
3833: {0x14, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3834: {-1 , "Critical error", "�v���I�ȃG���[�ł�."},
3835: };
3836:
1.1.1.20 root 3837: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
3838: int msdos_psp_get_file_table(int fd, int psp_seg);
3839: void msdos_putch(UINT8 data);
1.1.1.50 root 3840: void msdos_putch_fast(UINT8 data);
1.1.1.35 root 3841: #ifdef USE_SERVICE_THREAD
3842: void msdos_putch_tmp(UINT8 data);
3843: #endif
1.1.1.45 root 3844: const char *msdos_short_path(const char *path);
1.1.1.44 root 3845: bool msdos_is_valid_drive(int drv);
3846: bool msdos_is_removable_drive(int drv);
3847: bool msdos_is_cdrom_drive(int drv);
3848: bool msdos_is_remote_drive(int drv);
3849: bool msdos_is_subst_drive(int drv);
1.1.1.20 root 3850:
1.1 root 3851: // process info
3852:
3853: process_t *msdos_process_info_create(UINT16 psp_seg)
3854: {
3855: for(int i = 0; i < MAX_PROCESS; i++) {
3856: if(process[i].psp == 0 || process[i].psp == psp_seg) {
3857: memset(&process[i], 0, sizeof(process_t));
3858: process[i].psp = psp_seg;
3859: return(&process[i]);
3860: }
3861: }
3862: fatalerror("too many processes\n");
3863: return(NULL);
3864: }
3865:
1.1.1.52 root 3866: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1 root 3867: {
3868: for(int i = 0; i < MAX_PROCESS; i++) {
3869: if(process[i].psp == psp_seg) {
3870: return(&process[i]);
3871: }
3872: }
1.1.1.33 root 3873: if(show_error) {
3874: fatalerror("invalid psp address\n");
3875: }
1.1 root 3876: return(NULL);
3877: }
3878:
1.1.1.23 root 3879: void msdos_sda_update(int psp_seg)
3880: {
3881: sda_t *sda = (sda_t *)(mem + SDA_TOP);
3882:
3883: for(int i = 0; i < MAX_PROCESS; i++) {
3884: if(process[i].psp == psp_seg) {
3885: sda->switchar = process[i].switchar;
3886: sda->current_dta.w.l = process[i].dta.w.l;
3887: sda->current_dta.w.h = process[i].dta.w.h;
3888: sda->current_psp = process[i].psp;
3889: break;
3890: }
3891: }
3892: sda->malloc_strategy = malloc_strategy;
3893: sda->return_code = retval;
3894: sda->current_drive = _getdrive();
3895: }
3896:
1.1.1.13 root 3897: // dta info
3898:
3899: void msdos_dta_info_init()
3900: {
1.1.1.14 root 3901: for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13 root 3902: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3903: }
3904: }
3905:
3906: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
3907: {
3908: dtainfo_t *free_dta = NULL;
1.1.1.14 root 3909: for(int i = 0; i < MAX_DTAINFO; i++) {
3910: if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
3911: if(free_dta == NULL) {
1.1.1.13 root 3912: free_dta = &dtalist[i];
3913: }
1.1.1.14 root 3914: } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13 root 3915: return(&dtalist[i]);
3916: }
3917: }
1.1.1.14 root 3918: if(free_dta) {
1.1.1.13 root 3919: free_dta->psp = psp_seg;
3920: free_dta->dta = dta_laddr;
3921: return(free_dta);
3922: }
3923: fatalerror("too many dta\n");
3924: return(NULL);
3925: }
3926:
3927: void msdos_dta_info_free(UINT16 psp_seg)
3928: {
1.1.1.14 root 3929: for(int i = 0; i < MAX_DTAINFO; i++) {
3930: if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13 root 3931: FindClose(dtalist[i].find_handle);
3932: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3933: }
3934: }
3935: }
3936:
1.1 root 3937: void msdos_cds_update(int drv)
3938: {
1.1.1.44 root 3939: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1 root 3940:
1.1.1.44 root 3941: memset(cds, 0, 88);
3942:
3943: if(msdos_is_valid_drive(drv)) {
3944: char path[MAX_PATH];
3945: if(msdos_is_remote_drive(drv)) {
3946: cds->drive_attrib = 0xc000; // network drive
3947: } else if(msdos_is_subst_drive(drv)) {
3948: cds->drive_attrib = 0x5000; // subst drive
3949: } else {
3950: cds->drive_attrib = 0x4000; // physical drive
3951: }
3952: if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
3953: strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
3954: }
3955: }
3956: if(cds->path_name[0] == '\0') {
3957: sprintf(cds->path_name, "%c:\\", 'A' + drv);
3958: }
3959: cds->dpb_ptr.w.h = DPB_TOP >> 4;
3960: cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
3961: cds->word_1 = cds->word_2 = 0xffff;
3962: cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
3963: cds->bs_offset = 2;
3964: }
3965:
1.1.1.45 root 3966: void msdos_cds_update(int drv, const char *path)
1.1.1.44 root 3967: {
3968: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
3969:
3970: strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1 root 3971: }
3972:
1.1.1.17 root 3973: // nls information tables
3974:
3975: // uppercase table (func 6502h)
3976: void msdos_upper_table_update()
3977: {
3978: *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 3979: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 3980: UINT8 c[4];
1.1.1.33 root 3981: *(UINT32 *)c = 0; // reset internal conversion state
3982: CharUpperBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.17 root 3983: c[0] = 0x80 + i;
3984: DWORD rc = CharUpperBuffA((LPSTR)c, 1);
3985: mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
3986: }
3987: }
3988:
1.1.1.23 root 3989: // lowercase table (func 6503h)
3990: void msdos_lower_table_update()
3991: {
3992: *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
3993: for(unsigned i = 0; i < 0x80; ++i) {
3994: UINT8 c[4];
1.1.1.33 root 3995: *(UINT32 *)c = 0; // reset internal conversion state
3996: CharLowerBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.23 root 3997: c[0] = 0x80 + i;
3998: DWORD rc = CharLowerBuffA((LPSTR)c, 1);
3999: mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
4000: }
4001: }
4002:
1.1.1.17 root 4003: // filename uppercase table (func 6504h)
4004: void msdos_filename_upper_table_init()
4005: {
4006: // depended on (file)system, not on active codepage
4007: // temporary solution: just filling data
4008: *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 4009: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 4010: mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
4011: }
4012: }
4013:
4014: // filaname terminator table (func 6505h)
4015: void msdos_filename_terminator_table_init()
4016: {
4017: const char illegal_chars[] = ".\"/\\[]:|<>+=;,"; // for standard MS-DOS fs.
4018: UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
4019:
4020: data[2] = 1; // marker? (permissible character value)
4021: data[3] = 0x00; // 00h...FFh
4022: data[4] = 0xff;
4023: data[5] = 0; // marker? (excluded character)
4024: data[6] = 0x00; // 00h...20h
4025: data[7] = 0x20;
4026: data[8] = 2; // marker? (illegal characters for filename)
4027: data[9] = (UINT8)strlen(illegal_chars);
4028: memcpy(data + 10, illegal_chars, data[9]);
4029:
4030: // total length
4031: *(UINT16 *)data = (10 - 2) + data[9];
4032: }
4033:
4034: // collating table (func 6506h)
4035: void msdos_collating_table_update()
4036: {
4037: // temporary solution: just filling data
4038: *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22 root 4039: for(unsigned i = 0; i < 256; ++i) {
1.1.1.17 root 4040: mem[COLLATING_TABLE_TOP + 2 + i] = i;
4041: }
4042: }
4043:
1.1 root 4044: // dbcs
4045:
4046: void msdos_dbcs_table_update()
4047: {
4048: UINT8 dbcs_data[DBCS_SIZE];
4049: memset(dbcs_data, 0, sizeof(dbcs_data));
4050:
4051: CPINFO info;
4052: GetCPInfo(active_code_page, &info);
4053:
4054: if(info.MaxCharSize != 1) {
4055: for(int i = 0;; i += 2) {
4056: UINT8 lo = info.LeadByte[i + 0];
4057: UINT8 hi = info.LeadByte[i + 1];
4058: dbcs_data[2 + i + 0] = lo;
4059: dbcs_data[2 + i + 1] = hi;
4060: if(lo == 0 && hi == 0) {
4061: dbcs_data[0] = i + 2;
4062: break;
4063: }
4064: }
4065: } else {
4066: dbcs_data[0] = 2; // ???
4067: }
4068: memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
4069: }
4070:
1.1.1.17 root 4071: void msdos_dbcs_table_finish()
4072: {
1.1.1.32 root 4073: if(system_code_page != _getmbcp()) {
1.1.1.17 root 4074: _setmbcp(system_code_page);
4075: }
1.1.1.32 root 4076: if(console_code_page != GetConsoleCP()) {
4077: SetConsoleCP(console_code_page);
4078: SetConsoleOutputCP(console_code_page);
4079: }
1.1.1.17 root 4080: }
4081:
4082: void msdos_nls_tables_init()
1.1 root 4083: {
1.1.1.32 root 4084: active_code_page = console_code_page = GetConsoleCP();
4085: system_code_page = _getmbcp();
4086:
4087: if(active_code_page != system_code_page) {
4088: if(_setmbcp(active_code_page) != 0) {
4089: active_code_page = system_code_page;
4090: }
4091: }
4092:
1.1.1.17 root 4093: msdos_upper_table_update();
1.1.1.23 root 4094: msdos_lower_table_update();
1.1.1.17 root 4095: msdos_filename_terminator_table_init();
4096: msdos_filename_upper_table_init();
4097: msdos_collating_table_update();
1.1 root 4098: msdos_dbcs_table_update();
4099: }
4100:
1.1.1.17 root 4101: void msdos_nls_tables_update()
1.1 root 4102: {
1.1.1.17 root 4103: msdos_dbcs_table_update();
4104: msdos_upper_table_update();
1.1.1.23 root 4105: msdos_lower_table_update();
4106: // msdos_collating_table_update();
1.1 root 4107: }
4108:
4109: int msdos_lead_byte_check(UINT8 code)
4110: {
4111: UINT8 *dbcs_table = mem + DBCS_TABLE;
4112:
4113: for(int i = 0;; i += 2) {
4114: UINT8 lo = dbcs_table[i + 0];
4115: UINT8 hi = dbcs_table[i + 1];
4116: if(lo == 0 && hi == 0) {
4117: break;
4118: }
4119: if(lo <= code && code <= hi) {
4120: return(1);
4121: }
4122: }
4123: return(0);
4124: }
4125:
1.1.1.20 root 4126: int msdos_ctrl_code_check(UINT8 code)
4127: {
1.1.1.22 root 4128: return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20 root 4129: }
4130:
1.1.1.36 root 4131: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
4132: {
4133: int is_kanji_1st = 0;
4134: int is_kanji_2nd = 0;
4135:
4136: for(int p = 0;; p++) {
4137: if(is_kanji_1st) {
4138: is_kanji_1st = 0;
4139: is_kanji_2nd = 1;
4140: } else if(msdos_lead_byte_check(buf[p])) {
4141: is_kanji_1st = 1;
4142: }
4143: if(p == n) {
4144: return(is_kanji_2nd);
4145: }
4146: is_kanji_2nd = 0;
4147: }
4148: }
4149:
1.1 root 4150: // file control
4151:
1.1.1.45 root 4152: const char *msdos_remove_double_quote(const char *path)
1.1.1.14 root 4153: {
4154: static char tmp[MAX_PATH];
4155:
4156: if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45 root 4157: memset(tmp, 0, sizeof(tmp));
1.1.1.14 root 4158: memcpy(tmp, path + 1, strlen(path) - 2);
4159: } else {
4160: strcpy(tmp, path);
4161: }
4162: return(tmp);
4163: }
4164:
1.1.1.45 root 4165: const char *msdos_remove_end_separator(const char *path)
1.1.1.32 root 4166: {
4167: static char tmp[MAX_PATH];
4168:
4169: strcpy(tmp, path);
1.1.1.45 root 4170:
4171: // for example "C:\" case, the end separator should not be removed
4172: if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
4173: tmp[strlen(tmp) - 1] = '\0';
1.1.1.32 root 4174: }
4175: return(tmp);
4176: }
4177:
1.1.1.45 root 4178: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14 root 4179: {
4180: static char tmp[MAX_PATH];
1.1.1.45 root 4181: const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14 root 4182:
4183: if(strlen(tmp_dir) == 0) {
4184: strcpy(tmp, file);
4185: } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
4186: sprintf(tmp, "%s%s", tmp_dir, file);
4187: } else {
4188: sprintf(tmp, "%s\\%s", tmp_dir, file);
4189: }
4190: return(tmp);
4191: }
4192:
1.1.1.45 root 4193: const char *msdos_trimmed_path(const char *path, int lfn)
1.1 root 4194: {
4195: static char tmp[MAX_PATH];
4196:
4197: if(lfn) {
4198: strcpy(tmp, path);
4199: } else {
4200: // remove space in the path
1.1.1.45 root 4201: const char *src = path;
4202: char *dst = tmp;
1.1 root 4203:
4204: while(*src != '\0') {
4205: if(msdos_lead_byte_check(*src)) {
4206: *dst++ = *src++;
4207: *dst++ = *src++;
4208: } else if(*src != ' ') {
4209: *dst++ = *src++;
4210: } else {
4211: src++; // skip space
4212: }
4213: }
4214: *dst = '\0';
4215: }
1.1.1.14 root 4216: if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
4217: // redirect C:\COMMAND.COM to comspec_path
4218: strcpy(tmp, comspec_path);
4219: } else if(is_vista_or_later && _access(tmp, 0) != 0) {
4220: // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
4221: static int root_drive_protected = -1;
4222: char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
4223: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
4224:
1.1.1.60! root 4225: if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14 root 4226: name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
4227: strcpy(name, name_temp);
4228: name_temp[0] = '\0';
4229:
4230: if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
4231: (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
4232: if(root_drive_protected == -1) {
4233: FILE *fp = NULL;
4234:
4235: sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
4236: root_drive_protected = 1;
4237: try {
4238: if((fp = fopen(temp, "w")) != NULL) {
4239: if(fprintf(fp, "TEST") == 4) {
4240: root_drive_protected = 0;
4241: }
4242: }
4243: } catch(...) {
4244: }
4245: if(fp != NULL) {
4246: fclose(fp);
4247: }
4248: if(_access(temp, 0) == 0) {
4249: remove(temp);
4250: }
4251: }
4252: if(root_drive_protected == 1) {
1.1.1.60! root 4253: if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
! 4254: GetEnvironmentVariableA("TMP", temp, MAX_PATH) != 0) {
1.1.1.14 root 4255: strcpy(tmp, msdos_combine_path(temp, name));
4256: }
4257: }
4258: }
4259: }
4260: }
1.1 root 4261: return(tmp);
4262: }
4263:
1.1.1.45 root 4264: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28 root 4265: {
1.1.1.32 root 4266: // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28 root 4267: static char env_path[ENV_SIZE];
4268: char tmp[ENV_SIZE], *token;
4269:
4270: memset(env_path, 0, sizeof(env_path));
4271: strcpy(tmp, src);
4272: token = my_strtok(tmp, ";");
4273:
4274: while(token != NULL) {
4275: if(token[0] != '\0') {
1.1.1.45 root 4276: const char *path = msdos_remove_double_quote(token);
4277: char short_path[MAX_PATH];
1.1.1.32 root 4278: if(path != NULL && strlen(path) != 0) {
4279: if(env_path[0] != '\0') {
4280: strcat(env_path, ";");
4281: }
1.1.1.60! root 4282: if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32 root 4283: strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28 root 4284: } else {
4285: my_strupr(short_path);
1.1.1.32 root 4286: strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28 root 4287: }
4288: }
4289: }
4290: token = my_strtok(NULL, ";");
4291: }
4292: return(env_path);
4293: }
4294:
1.1.1.45 root 4295: bool match(const char *text, const char *pattern)
1.1 root 4296: {
1.1.1.24 root 4297: // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14 root 4298: switch(*pattern) {
1.1 root 4299: case '\0':
4300: return !*text;
4301: case '*':
1.1.1.14 root 4302: return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1 root 4303: case '?':
4304: return *text && match(text + 1, pattern + 1);
4305: default:
4306: return (*text == *pattern) && match(text + 1, pattern + 1);
4307: }
4308: }
4309:
1.1.1.45 root 4310: bool msdos_match_volume_label(const char *path, const char *volume)
1.1 root 4311: {
1.1.1.45 root 4312: const char *p = NULL;
1.1 root 4313:
1.1.1.14 root 4314: if(!*volume) {
4315: return false;
4316: } else if((p = my_strchr(path, ':')) != NULL) {
1.1 root 4317: return msdos_match_volume_label(p + 1, volume);
4318: } else if((p = my_strchr(path, '\\')) != NULL) {
4319: return msdos_match_volume_label(p + 1, volume);
4320: } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14 root 4321: char tmp[MAX_PATH];
4322: sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
4323: return match(volume, tmp);
1.1 root 4324: } else {
4325: return match(volume, path);
4326: }
4327: }
4328:
1.1.1.45 root 4329: const char *msdos_fcb_path(fcb_t *fcb)
1.1 root 4330: {
4331: static char tmp[MAX_PATH];
4332: char name[9], ext[4];
4333:
4334: memset(name, 0, sizeof(name));
4335: memcpy(name, fcb->file_name, 8);
4336: strcpy(name, msdos_trimmed_path(name, 0));
4337:
4338: memset(ext, 0, sizeof(ext));
4339: memcpy(ext, fcb->file_name + 8, 3);
4340: strcpy(ext, msdos_trimmed_path(ext, 0));
4341:
4342: if(name[0] == '\0' || strcmp(name, "????????") == 0) {
4343: strcpy(name, "*");
4344: }
4345: if(ext[0] == '\0') {
4346: strcpy(tmp, name);
4347: } else {
4348: if(strcmp(ext, "???") == 0) {
4349: strcpy(ext, "*");
4350: }
4351: sprintf(tmp, "%s.%s", name, ext);
4352: }
4353: return(tmp);
4354: }
4355:
1.1.1.45 root 4356: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1 root 4357: {
1.1.1.60! root 4358: char tmp[MAX_PATH];
! 4359: strcpy(tmp, path);
! 4360: char *ext = my_strchr(tmp, '.');
1.1 root 4361:
4362: memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60! root 4363: if(ext != NULL && tmp[0] != '.') {
1.1 root 4364: *ext = '\0';
4365: memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
4366: }
1.1.1.60! root 4367: memcpy(fcb->file_name, tmp, strlen(tmp));
1.1 root 4368: }
4369:
1.1.1.45 root 4370: const char *msdos_short_path(const char *path)
1.1 root 4371: {
4372: static char tmp[MAX_PATH];
4373:
1.1.1.60! root 4374: if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24 root 4375: strcpy(tmp, path);
4376: }
1.1 root 4377: my_strupr(tmp);
4378: return(tmp);
4379: }
4380:
1.1.1.60! root 4381: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13 root 4382: {
4383: static char tmp[MAX_PATH];
1.1.1.45 root 4384:
1.1.1.14 root 4385: if(fd->cAlternateFileName[0]) {
1.1.1.13 root 4386: strcpy(tmp, fd->cAlternateFileName);
4387: } else {
4388: strcpy(tmp, fd->cFileName);
4389: }
4390: my_strupr(tmp);
4391: return(tmp);
4392: }
4393:
1.1.1.45 root 4394: const char *msdos_short_full_path(const char *path)
1.1 root 4395: {
4396: static char tmp[MAX_PATH];
4397: char full[MAX_PATH], *name;
4398:
1.1.1.14 root 4399: // Full works with non-existent files, but Short does not
1.1.1.60! root 4400: GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14 root 4401: *tmp = '\0';
1.1.1.60! root 4402: if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14 root 4403: name[-1] = '\0';
1.1.1.60! root 4404: DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14 root 4405: if(len == 0) {
4406: strcpy(tmp, full);
4407: } else {
4408: tmp[len++] = '\\';
4409: strcpy(tmp + len, name);
4410: }
4411: }
1.1 root 4412: my_strupr(tmp);
4413: return(tmp);
4414: }
4415:
1.1.1.45 root 4416: const char *msdos_short_full_dir(const char *path)
1.1 root 4417: {
4418: static char tmp[MAX_PATH];
4419: char full[MAX_PATH], *name;
4420:
1.1.1.60! root 4421: GetFullPathNameA(path, MAX_PATH, full, &name);
1.1 root 4422: name[-1] = '\0';
1.1.1.60! root 4423: if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24 root 4424: strcpy(tmp, full);
4425: }
1.1 root 4426: my_strupr(tmp);
4427: return(tmp);
4428: }
4429:
1.1.1.45 root 4430: const char *msdos_local_file_path(const char *path, int lfn)
1.1 root 4431: {
1.1.1.45 root 4432: static char trimmed[MAX_PATH];
4433:
4434: strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14 root 4435: #if 0
4436: // I have forgotten the reason of this routine... :-(
1.1 root 4437: if(_access(trimmed, 0) != 0) {
4438: process_t *process = msdos_process_info_get(current_psp);
4439: static char tmp[MAX_PATH];
4440:
4441: sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
4442: if(_access(tmp, 0) == 0) {
4443: return(tmp);
4444: }
4445: }
1.1.1.14 root 4446: #endif
1.1 root 4447: return(trimmed);
4448: }
4449:
1.1.1.45 root 4450: bool msdos_is_device_path(const char *path)
1.1.1.11 root 4451: {
4452: char full[MAX_PATH], *name;
4453:
1.1.1.60! root 4454: if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4455: if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
4456: _stricmp(full, "\\\\.\\CON" ) == 0 ||
4457: _stricmp(full, "\\\\.\\NUL" ) == 0 ||
4458: _stricmp(full, "\\\\.\\PRN" ) == 0 ||
4459: _stricmp(full, "\\\\.\\COM1") == 0 ||
4460: _stricmp(full, "\\\\.\\COM2") == 0 ||
4461: _stricmp(full, "\\\\.\\COM3") == 0 ||
4462: _stricmp(full, "\\\\.\\COM4") == 0 ||
4463: _stricmp(full, "\\\\.\\COM5") == 0 ||
4464: _stricmp(full, "\\\\.\\COM6") == 0 ||
4465: _stricmp(full, "\\\\.\\COM7") == 0 ||
4466: _stricmp(full, "\\\\.\\COM8") == 0 ||
4467: _stricmp(full, "\\\\.\\COM9") == 0 ||
4468: _stricmp(full, "\\\\.\\LPT1") == 0 ||
4469: _stricmp(full, "\\\\.\\LPT2") == 0 ||
4470: _stricmp(full, "\\\\.\\LPT3") == 0 ||
4471: _stricmp(full, "\\\\.\\LPT4") == 0 ||
4472: _stricmp(full, "\\\\.\\LPT5") == 0 ||
4473: _stricmp(full, "\\\\.\\LPT6") == 0 ||
4474: _stricmp(full, "\\\\.\\LPT7") == 0 ||
4475: _stricmp(full, "\\\\.\\LPT8") == 0 ||
4476: _stricmp(full, "\\\\.\\LPT9") == 0) {
4477: return(true);
4478: } else if(name != NULL) {
4479: if(_stricmp(name, "CLOCK$" ) == 0 ||
4480: _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30 root 4481: _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43 root 4482: // _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30 root 4483: _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29 root 4484: return(true);
4485: }
4486: }
1.1.1.24 root 4487: }
4488: return(false);
1.1.1.11 root 4489: }
4490:
1.1.1.45 root 4491: bool msdos_is_con_path(const char *path)
1.1.1.8 root 4492: {
1.1.1.14 root 4493: char full[MAX_PATH], *name;
1.1.1.8 root 4494:
1.1.1.60! root 4495: if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4496: return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24 root 4497: }
4498: return(false);
4499: }
4500:
1.1.1.45 root 4501: int msdos_is_comm_path(const char *path)
1.1.1.24 root 4502: {
4503: char full[MAX_PATH], *name;
4504:
1.1.1.60! root 4505: if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4506: if(_stricmp(full, "\\\\.\\COM1") == 0) {
4507: return(1);
4508: } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
4509: return(2);
4510: } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
4511: return(3);
4512: } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
4513: return(4);
1.1.1.24 root 4514: }
4515: }
1.1.1.29 root 4516: return(0);
4517: }
4518:
1.1.1.45 root 4519: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37 root 4520: {
4521: // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
1.1.1.45 root 4522: const char *p = NULL;
1.1.1.37 root 4523:
4524: if((p = strstr(path, ":")) != NULL) {
4525: UINT8 selector = sio_read(sio_port - 1, 3);
4526:
4527: // baud rate
4528: int baud = max(110, min(9600, atoi(p + 1)));
4529: UINT16 divisor = 115200 / baud;
4530:
4531: if((p = strstr(p + 1, ",")) != NULL) {
4532: // parity
4533: if(p[1] == 'N' || p[1] == 'n') {
4534: selector = (selector & ~0x38) | 0x00;
4535: } else if(p[1] == 'O' || p[1] == 'o') {
4536: selector = (selector & ~0x38) | 0x08;
4537: } else if(p[1] == 'E' || p[1] == 'e') {
4538: selector = (selector & ~0x38) | 0x18;
4539: } else if(p[1] == 'M' || p[1] == 'm') {
4540: selector = (selector & ~0x38) | 0x28;
4541: } else if(p[1] == 'S' || p[1] == 's') {
4542: selector = (selector & ~0x38) | 0x38;
4543: }
4544: if((p = strstr(p + 1, ",")) != NULL) {
4545: // word length
4546: if(p[1] == '8') {
4547: selector = (selector & ~0x03) | 0x03;
4548: } else if(p[1] == '7') {
4549: selector = (selector & ~0x03) | 0x02;
4550: } else if(p[1] == '6') {
4551: selector = (selector & ~0x03) | 0x01;
4552: } else if(p[1] == '5') {
4553: selector = (selector & ~0x03) | 0x00;
4554: }
4555: if((p = strstr(p + 1, ",")) != NULL) {
4556: // stop bits
4557: float bits = atof(p + 1);
4558: if(bits > 1.0F) {
4559: selector |= 0x04;
4560: } else {
4561: selector &= ~0x04;
4562: }
4563: }
4564: }
4565: }
4566: sio_write(sio_port - 1, 3, selector | 0x80);
4567: sio_write(sio_port - 1, 0, divisor & 0xff);
4568: sio_write(sio_port - 1, 1, divisor >> 8);
4569: sio_write(sio_port - 1, 3, selector);
4570: }
4571: }
4572:
1.1.1.45 root 4573: int msdos_is_prn_path(const char *path)
1.1.1.30 root 4574: {
4575: char full[MAX_PATH], *name;
4576:
1.1.1.60! root 4577: if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30 root 4578: if(_stricmp(full, "\\\\.\\PRN") == 0) {
4579: return(1);
4580: } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
4581: return(1);
4582: } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
4583: return(2);
4584: } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
4585: return(3);
4586: }
4587: }
4588: return(0);
4589: }
4590:
1.1.1.44 root 4591: bool msdos_is_valid_drive(int drv)
4592: {
4593: return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
4594: }
4595:
4596: bool msdos_is_removable_drive(int drv)
4597: {
4598: char volume[] = "A:\\";
4599:
4600: volume[0] = 'A' + drv;
4601:
1.1.1.60! root 4602: return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44 root 4603: }
4604:
4605: bool msdos_is_cdrom_drive(int drv)
4606: {
4607: char volume[] = "A:\\";
4608:
4609: volume[0] = 'A' + drv;
4610:
1.1.1.60! root 4611: return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44 root 4612: }
4613:
4614: bool msdos_is_remote_drive(int drv)
4615: {
4616: char volume[] = "A:\\";
4617:
4618: volume[0] = 'A' + drv;
4619:
1.1.1.60! root 4620: return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44 root 4621: }
4622:
4623: bool msdos_is_subst_drive(int drv)
4624: {
4625: char device[] = "A:", path[MAX_PATH];
4626:
4627: device[0] = 'A' + drv;
4628:
1.1.1.60! root 4629: if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44 root 4630: if(strncmp(path, "\\??\\", 4) == 0) {
4631: return(true);
4632: }
4633: }
4634: return(false);
4635: }
4636:
1.1.1.45 root 4637: bool msdos_is_existing_file(const char *path)
1.1.1.24 root 4638: {
4639: // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60! root 4640: WIN32_FIND_DATAA fd;
1.1.1.24 root 4641: HANDLE hFind;
4642:
1.1.1.60! root 4643: if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24 root 4644: FindClose(hFind);
1.1.1.60! root 4645: return(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1.1.1.24 root 4646: }
4647: return(false);
1.1.1.8 root 4648: }
4649:
1.1.1.45 root 4650: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9 root 4651: {
4652: static char tmp[MAX_PATH];
1.1.1.28 root 4653: char path[ENV_SIZE], *file_name;
1.1.1.9 root 4654:
1.1.1.28 root 4655: // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60! root 4656: if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9 root 4657: sprintf(file_name, "COMMAND.COM");
4658: if(_access(tmp, 0) == 0) {
4659: return(tmp);
4660: }
4661: }
1.1.1.28 root 4662:
4663: // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60! root 4664: if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9 root 4665: sprintf(file_name, "COMMAND.COM");
4666: if(_access(tmp, 0) == 0) {
4667: return(tmp);
4668: }
4669: }
1.1.1.28 root 4670:
4671: // check if COMMAND.COM is in the current directory
1.1.1.60! root 4672: if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9 root 4673: if(_access(tmp, 0) == 0) {
4674: return(tmp);
4675: }
4676: }
1.1.1.28 root 4677:
4678: // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
4679: strcpy(path, env_path);
4680: char *token = my_strtok(path, ";");
1.1.1.9 root 4681: while(token != NULL) {
1.1.1.14 root 4682: if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9 root 4683: strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
4684: if(_access(tmp, 0) == 0) {
4685: return(tmp);
4686: }
4687: }
4688: token = my_strtok(NULL, ";");
4689: }
4690: return(NULL);
4691: }
4692:
1.1.1.14 root 4693: int msdos_drive_number(const char *path)
1.1 root 4694: {
4695: char tmp[MAX_PATH], *name;
4696:
1.1.1.60! root 4697: if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45 root 4698: if(tmp[0] >= 'a' && tmp[0] <= 'z') {
4699: return(tmp[0] - 'a');
4700: } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
4701: return(tmp[0] - 'A');
4702: }
1.1 root 4703: }
1.1.1.45 root 4704: // return(msdos_drive_number("."));
4705: return(_getdrive() - 1);
1.1 root 4706: }
4707:
1.1.1.45 root 4708: const char *msdos_volume_label(const char *path)
1.1 root 4709: {
4710: static char tmp[MAX_PATH];
4711: char volume[] = "A:\\";
4712:
4713: if(path[1] == ':') {
4714: volume[0] = path[0];
4715: } else {
4716: volume[0] = 'A' + _getdrive() - 1;
4717: }
1.1.1.60! root 4718: if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1 root 4719: memset(tmp, 0, sizeof(tmp));
4720: }
4721: return(tmp);
4722: }
4723:
1.1.1.45 root 4724: const char *msdos_short_volume_label(const char *label)
1.1 root 4725: {
4726: static char tmp[(8 + 1 + 3) + 1];
1.1.1.45 root 4727: const char *src = label;
1.1 root 4728: int remain = strlen(label);
4729: char *dst_n = tmp;
4730: char *dst_e = tmp + 9;
4731:
4732: strcpy(tmp, " . ");
4733: for(int i = 0; i < 8 && remain > 0; i++) {
4734: if(msdos_lead_byte_check(*src)) {
4735: if(++i == 8) {
4736: break;
4737: }
4738: *dst_n++ = *src++;
4739: remain--;
4740: }
4741: *dst_n++ = *src++;
4742: remain--;
4743: }
4744: if(remain > 0) {
4745: for(int i = 0; i < 3 && remain > 0; i++) {
4746: if(msdos_lead_byte_check(*src)) {
4747: if(++i == 3) {
4748: break;
4749: }
4750: *dst_e++ = *src++;
4751: remain--;
4752: }
4753: *dst_e++ = *src++;
4754: remain--;
4755: }
4756: *dst_e = '\0';
4757: } else {
4758: *dst_n = '\0';
4759: }
4760: my_strupr(tmp);
4761: return(tmp);
4762: }
4763:
1.1.1.13 root 4764: errno_t msdos_maperr(unsigned long oserrno)
4765: {
4766: _doserrno = oserrno;
1.1.1.14 root 4767: switch(oserrno) {
1.1.1.13 root 4768: case ERROR_FILE_NOT_FOUND: // 2
4769: case ERROR_PATH_NOT_FOUND: // 3
4770: case ERROR_INVALID_DRIVE: // 15
4771: case ERROR_NO_MORE_FILES: // 18
4772: case ERROR_BAD_NETPATH: // 53
4773: case ERROR_BAD_NET_NAME: // 67
4774: case ERROR_BAD_PATHNAME: // 161
4775: case ERROR_FILENAME_EXCED_RANGE: // 206
4776: return ENOENT;
4777: case ERROR_TOO_MANY_OPEN_FILES: // 4
4778: return EMFILE;
4779: case ERROR_ACCESS_DENIED: // 5
4780: case ERROR_CURRENT_DIRECTORY: // 16
4781: case ERROR_NETWORK_ACCESS_DENIED: // 65
4782: case ERROR_CANNOT_MAKE: // 82
4783: case ERROR_FAIL_I24: // 83
4784: case ERROR_DRIVE_LOCKED: // 108
4785: case ERROR_SEEK_ON_DEVICE: // 132
4786: case ERROR_NOT_LOCKED: // 158
4787: case ERROR_LOCK_FAILED: // 167
4788: return EACCES;
4789: case ERROR_INVALID_HANDLE: // 6
4790: case ERROR_INVALID_TARGET_HANDLE: // 114
4791: case ERROR_DIRECT_ACCESS_HANDLE: // 130
4792: return EBADF;
4793: case ERROR_ARENA_TRASHED: // 7
4794: case ERROR_NOT_ENOUGH_MEMORY: // 8
4795: case ERROR_INVALID_BLOCK: // 9
4796: case ERROR_NOT_ENOUGH_QUOTA: // 1816
4797: return ENOMEM;
4798: case ERROR_BAD_ENVIRONMENT: // 10
4799: return E2BIG;
4800: case ERROR_BAD_FORMAT: // 11
4801: return ENOEXEC;
4802: case ERROR_NOT_SAME_DEVICE: // 17
4803: return EXDEV;
4804: case ERROR_FILE_EXISTS: // 80
4805: case ERROR_ALREADY_EXISTS: // 183
4806: return EEXIST;
4807: case ERROR_NO_PROC_SLOTS: // 89
4808: case ERROR_MAX_THRDS_REACHED: // 164
4809: case ERROR_NESTING_NOT_ALLOWED: // 215
4810: return EAGAIN;
4811: case ERROR_BROKEN_PIPE: // 109
4812: return EPIPE;
4813: case ERROR_DISK_FULL: // 112
4814: return ENOSPC;
4815: case ERROR_WAIT_NO_CHILDREN: // 128
4816: case ERROR_CHILD_NOT_COMPLETE: // 129
4817: return ECHILD;
4818: case ERROR_DIR_NOT_EMPTY: // 145
4819: return ENOTEMPTY;
4820: }
1.1.1.14 root 4821: if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13 root 4822: oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
4823: return EACCES;
4824: }
1.1.1.14 root 4825: if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13 root 4826: oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
4827: return ENOEXEC;
4828: }
4829: return EINVAL;
4830: }
4831:
1.1.1.45 root 4832: int msdos_open(const char *path, int oflag)
1.1.1.13 root 4833: {
1.1.1.14 root 4834: if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45 root 4835: return(_open(path, oflag));
1.1.1.13 root 4836: }
1.1.1.14 root 4837:
4838: SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13 root 4839: DWORD disposition;
1.1.1.14 root 4840: switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
4841: default:
1.1.1.13 root 4842: case _O_EXCL:
4843: disposition = OPEN_EXISTING;
4844: break;
4845: case _O_CREAT:
4846: disposition = OPEN_ALWAYS;
4847: break;
4848: case _O_CREAT | _O_EXCL:
4849: case _O_CREAT | _O_TRUNC | _O_EXCL:
4850: disposition = CREATE_NEW;
4851: break;
4852: case _O_TRUNC:
4853: case _O_TRUNC | _O_EXCL:
4854: disposition = TRUNCATE_EXISTING;
4855: break;
4856: case _O_CREAT | _O_TRUNC:
4857: disposition = CREATE_ALWAYS;
4858: break;
4859: }
1.1.1.14 root 4860:
1.1.1.60! root 4861: HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13 root 4862: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4863: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4864: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4865: // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
4866: // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60! root 4867: h = CreateFileA(path, GENERIC_READ,
1.1.1.13 root 4868: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4869: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4870: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4871: errno = msdos_maperr(GetLastError());
1.1.1.45 root 4872: return(-1);
1.1.1.13 root 4873: }
4874: }
1.1.1.14 root 4875:
1.1.1.13 root 4876: int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14 root 4877: if(fd == -1) {
1.1.1.13 root 4878: CloseHandle(h);
4879: }
1.1.1.45 root 4880: return(fd);
4881: }
4882:
4883: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
4884: {
4885: int fd = -1;
4886:
4887: *sio_port = *lpt_port = 0;
4888:
4889: if(msdos_is_con_path(path)) {
4890: // MODE.COM opens CON device with read/write mode :-(
4891: if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
4892: oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
4893: oflag |= _O_RDONLY;
4894: }
4895: if((fd = msdos_open("CON", oflag)) == -1) {
4896: // fd = msdos_open("NUL", oflag);
4897: }
4898: } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
4899: fd = msdos_open("NUL", oflag);
4900: msdos_set_comm_params(*sio_port, path);
4901: } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
4902: fd = msdos_open("NUL", oflag);
4903: } else if(msdos_is_device_path(path)) {
4904: fd = msdos_open("NUL", oflag);
4905: // } else if(oflag & _O_CREAT) {
4906: // fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
4907: // } else {
4908: // fd = _open(path, oflag);
4909: }
4910: return(fd);
4911: }
4912:
4913: UINT16 msdos_device_info(const char *path)
4914: {
4915: if(msdos_is_con_path(path)) {
4916: return(0x80d3);
4917: } else if(msdos_is_comm_path(path)) {
4918: return(0x80a0);
4919: } else if(msdos_is_prn_path(path)) {
4920: // return(0xa8c0);
4921: return(0x80a0);
4922: } else if(msdos_is_device_path(path)) {
4923: if(strstr(path, "EMMXXXX0") != NULL) {
4924: return(0xc0c0);
4925: } else if(strstr(path, "MSCD001") != NULL) {
4926: return(0xc880);
4927: } else {
4928: return(0x8084);
4929: }
4930: } else {
4931: return(msdos_drive_number(path));
4932: }
1.1.1.13 root 4933: }
4934:
1.1.1.52 root 4935: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port = 0, int lpt_port = 0)
1.1 root 4936: {
4937: static int id = 0;
4938: char full[MAX_PATH], *name;
4939:
1.1.1.60! root 4940: if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1 root 4941: strcpy(file_handler[fd].path, full);
4942: } else {
4943: strcpy(file_handler[fd].path, path);
4944: }
1.1.1.14 root 4945: // isatty makes no distinction between CON & NUL
4946: // GetFileSize fails on CON, succeeds on NUL
4947: if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45 root 4948: if(info == 0x80d3) {
4949: info = 0x8084;
4950: }
1.1.1.14 root 4951: atty = 0;
4952: } else if(!atty && info == 0x80d3) {
1.1.1.45 root 4953: // info = msdos_drive_number(".");
4954: info = msdos_drive_number(path);
1.1.1.14 root 4955: }
1.1 root 4956: file_handler[fd].valid = 1;
4957: file_handler[fd].id = id++; // dummy id for int 21h ax=71a6h
1.1.1.37 root 4958: file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1 root 4959: file_handler[fd].mode = mode;
4960: file_handler[fd].info = info;
4961: file_handler[fd].psp = psp_seg;
1.1.1.37 root 4962: file_handler[fd].sio_port = sio_port;
4963: file_handler[fd].lpt_port = lpt_port;
1.1.1.21 root 4964:
4965: // init system file table
4966: if(fd < 20) {
4967: UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
4968:
4969: memset(sft, 0, 0x3b);
4970:
4971: *(UINT16 *)(sft + 0x00) = 1;
4972: *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60! root 4973: *(UINT8 *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21 root 4974: *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
4975:
4976: if(!(file_handler[fd].info & 0x80)) {
4977: *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
4978: *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
4979:
4980: FILETIME time, local;
4981: HANDLE hHandle;
4982: WORD dos_date = 0, dos_time = 0;
4983: DWORD file_size = 0;
4984: if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
4985: if(GetFileTime(hHandle, NULL, NULL, &time)) {
4986: FileTimeToLocalFileTime(&time, &local);
4987: FileTimeToDosDateTime(&local, &dos_date, &dos_time);
4988: }
4989: file_size = GetFileSize(hHandle, NULL);
4990: }
4991: *(UINT16 *)(sft + 0x0d) = dos_time;
4992: *(UINT16 *)(sft + 0x0f) = dos_date;
4993: *(UINT32 *)(sft + 0x11) = file_size;
4994: }
4995:
4996: char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
4997: _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
4998: my_strupr(fname);
4999: my_strupr(ext);
5000: memset(sft + 0x20, 0x20, 11);
5001: memcpy(sft + 0x20, fname, min(strlen(fname), 8));
5002: memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
5003:
5004: *(UINT16 *)(sft + 0x31) = psp_seg;
5005: }
1.1 root 5006: }
5007:
5008: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
5009: {
5010: strcpy(file_handler[dst].path, file_handler[src].path);
5011: file_handler[dst].valid = 1;
5012: file_handler[dst].id = file_handler[src].id;
5013: file_handler[dst].atty = file_handler[src].atty;
5014: file_handler[dst].mode = file_handler[src].mode;
5015: file_handler[dst].info = file_handler[src].info;
5016: file_handler[dst].psp = psp_seg;
1.1.1.37 root 5017: file_handler[dst].sio_port = file_handler[src].sio_port;
5018: file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1 root 5019: }
5020:
1.1.1.20 root 5021: void msdos_file_handler_close(int fd)
1.1 root 5022: {
5023: file_handler[fd].valid = 0;
1.1.1.21 root 5024:
5025: if(fd < 20) {
5026: memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
5027: }
1.1 root 5028: }
5029:
1.1.1.14 root 5030: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1 root 5031: {
1.1.1.14 root 5032: return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
5033: FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
5034: FILE_ATTRIBUTE_DIRECTORY));
1.1 root 5035: }
5036:
5037: // find file
5038:
5039: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
5040: {
5041: if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
5042: return(0); // search directory only !!!
5043: } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
5044: return(0);
5045: } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
5046: return(0);
5047: } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
5048: return(0);
5049: } else if((attribute & required_mask) != required_mask) {
5050: return(0);
5051: } else {
5052: return(1);
5053: }
5054: }
5055:
1.1.1.60! root 5056: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13 root 5057: {
1.1.1.14 root 5058: if(fd->cAlternateFileName[0]) {
1.1.1.42 root 5059: return(1);
1.1.1.13 root 5060: }
5061: size_t len = strlen(fd->cFileName);
1.1.1.14 root 5062: if(len > 12) {
1.1.1.42 root 5063: return(0);
1.1.1.13 root 5064: }
5065: const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14 root 5066: if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42 root 5067: return(0);
1.1.1.13 root 5068: }
1.1.1.42 root 5069: return(1);
1.1.1.13 root 5070: }
5071:
1.1.1.60! root 5072: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1 root 5073: {
5074: FILETIME local;
5075:
5076: FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
5077: fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
5078: fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
5079:
5080: FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
5081: fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
5082: fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
5083:
5084: FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
5085: fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
5086: fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
5087: }
5088:
5089: // i/o
5090:
5091: void msdos_stdio_reopen()
5092: {
5093: if(!file_handler[0].valid) {
5094: _dup2(DUP_STDIN, 0);
5095: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
5096: }
5097: if(!file_handler[1].valid) {
5098: _dup2(DUP_STDOUT, 1);
5099: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
5100: }
5101: if(!file_handler[2].valid) {
5102: _dup2(DUP_STDERR, 2);
5103: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
5104: }
1.1.1.21 root 5105: if(!file_handler[3].valid) {
5106: _dup2(DUP_STDAUX, 3);
5107: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
5108: }
5109: if(!file_handler[4].valid) {
5110: _dup2(DUP_STDPRN, 4);
1.1.1.45 root 5111: // msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
5112: msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21 root 5113: }
5114: for(int i = 0; i < 5; i++) {
5115: if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
5116: msdos_psp_set_file_table(i, i, current_psp);
5117: }
5118: }
1.1 root 5119: }
5120:
1.1.1.37 root 5121: int msdos_read(int fd, void *buffer, unsigned int count)
5122: {
5123: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
5124: // read from serial port
5125: int read = 0;
5126: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
5127: UINT8 *buf = (UINT8 *)buffer;
5128: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
5129: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 5130: DWORD timeout = timeGetTime() + 1000;
5131: while(read < count) {
5132: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
5133: buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
5134: timeout = timeGetTime() + 1000;
5135: } else {
5136: if(timeGetTime() > timeout) {
5137: break;
5138: }
5139: Sleep(10);
1.1.1.37 root 5140: }
5141: }
5142: sio_write(file_handler[fd].sio_port - 1, 3, selector);
5143: }
5144: return(read);
5145: }
5146: return(_read(fd, buffer, count));
5147: }
5148:
1.1 root 5149: int msdos_kbhit()
5150: {
5151: msdos_stdio_reopen();
5152:
1.1.1.20 root 5153: process_t *process = msdos_process_info_get(current_psp);
5154: int fd = msdos_psp_get_file_table(0, current_psp);
5155:
5156: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 5157: // stdin is redirected to file
1.1.1.20 root 5158: return(eof(fd) == 0);
1.1 root 5159: }
5160:
5161: // check keyboard status
1.1.1.35 root 5162: if(key_recv != 0) {
1.1 root 5163: return(1);
5164: }
1.1.1.35 root 5165: if(key_buf_char != NULL && key_buf_scan != NULL) {
5166: #ifdef USE_SERVICE_THREAD
5167: EnterCriticalSection(&key_buf_crit_sect);
5168: #endif
1.1.1.55 root 5169: bool empty = pcbios_is_key_buffer_empty();
1.1.1.35 root 5170: #ifdef USE_SERVICE_THREAD
5171: LeaveCriticalSection(&key_buf_crit_sect);
5172: #endif
5173: if(!empty) return(1);
5174: }
5175: return(_kbhit());
1.1 root 5176: }
5177:
5178: int msdos_getch_ex(int echo)
5179: {
5180: static char prev = 0;
5181:
5182: msdos_stdio_reopen();
5183:
1.1.1.20 root 5184: process_t *process = msdos_process_info_get(current_psp);
5185: int fd = msdos_psp_get_file_table(0, current_psp);
5186:
5187: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 5188: // stdin is redirected to file
5189: retry:
5190: char data;
1.1.1.37 root 5191: if(msdos_read(fd, &data, 1) == 1) {
1.1 root 5192: char tmp = data;
5193: if(data == 0x0a) {
5194: if(prev == 0x0d) {
5195: goto retry; // CRLF -> skip LF
5196: } else {
5197: data = 0x0d; // LF only -> CR
5198: }
5199: }
5200: prev = tmp;
5201: return(data);
5202: }
5203: return(EOF);
5204: }
5205:
5206: // input from console
1.1.1.5 root 5207: int key_char, key_scan;
1.1.1.33 root 5208: if(key_recv != 0) {
1.1.1.5 root 5209: key_char = (key_code >> 0) & 0xff;
5210: key_scan = (key_code >> 8) & 0xff;
5211: key_code >>= 16;
1.1.1.33 root 5212: key_recv >>= 16;
1.1.1.5 root 5213: } else {
1.1.1.54 root 5214: while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35 root 5215: if(key_buf_char != NULL && key_buf_scan != NULL) {
5216: #ifdef USE_SERVICE_THREAD
5217: EnterCriticalSection(&key_buf_crit_sect);
5218: #endif
1.1.1.55 root 5219: bool empty = pcbios_is_key_buffer_empty();
1.1.1.35 root 5220: #ifdef USE_SERVICE_THREAD
5221: LeaveCriticalSection(&key_buf_crit_sect);
5222: #endif
5223: if(!empty) break;
5224: }
1.1.1.23 root 5225: if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
5226: // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
5227: if(_kbhit()) {
1.1.1.32 root 5228: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 5229: #ifdef USE_SERVICE_THREAD
5230: EnterCriticalSection(&key_buf_crit_sect);
5231: #endif
1.1.1.51 root 5232: pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35 root 5233: #ifdef USE_SERVICE_THREAD
5234: LeaveCriticalSection(&key_buf_crit_sect);
5235: #endif
1.1.1.32 root 5236: }
1.1.1.23 root 5237: } else {
5238: Sleep(10);
5239: }
5240: } else {
5241: if(!update_key_buffer()) {
5242: Sleep(10);
5243: }
1.1.1.14 root 5244: }
5245: }
1.1.1.54 root 5246: if(m_exit) {
1.1.1.33 root 5247: // insert CR to terminate input loops
1.1.1.14 root 5248: key_char = 0x0d;
5249: key_scan = 0;
1.1.1.32 root 5250: } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 5251: #ifdef USE_SERVICE_THREAD
5252: EnterCriticalSection(&key_buf_crit_sect);
5253: #endif
1.1.1.51 root 5254: pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35 root 5255: #ifdef USE_SERVICE_THREAD
5256: LeaveCriticalSection(&key_buf_crit_sect);
5257: #endif
1.1.1.5 root 5258: }
1.1 root 5259: }
5260: if(echo && key_char) {
5261: msdos_putch(key_char);
5262: }
5263: return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
5264: }
5265:
5266: inline int msdos_getch()
5267: {
5268: return(msdos_getch_ex(0));
5269: }
5270:
5271: inline int msdos_getche()
5272: {
5273: return(msdos_getch_ex(1));
5274: }
5275:
5276: int msdos_write(int fd, const void *buffer, unsigned int count)
5277: {
1.1.1.37 root 5278: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
5279: // write to serial port
1.1.1.38 root 5280: int written = 0;
1.1.1.37 root 5281: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
5282: UINT8 *buf = (UINT8 *)buffer;
5283: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
5284: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 5285: DWORD timeout = timeGetTime() + 1000;
5286: while(written < count) {
5287: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
5288: sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
5289: timeout = timeGetTime() + 1000;
5290: } else {
5291: if(timeGetTime() > timeout) {
5292: break;
5293: }
5294: Sleep(10);
5295: }
1.1.1.37 root 5296: }
5297: sio_write(file_handler[fd].sio_port - 1, 3, selector);
5298: }
1.1.1.38 root 5299: return(written);
1.1.1.37 root 5300: } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
5301: // write to printer port
5302: UINT8 *buf = (UINT8 *)buffer;
5303: for(unsigned int i = 0; i < count; i++) {
5304: // printer_out(file_handler[fd].lpt_port - 1, buf[i]);
5305: pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
5306: }
5307: return(count);
5308: } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1 root 5309: // CR+LF -> LF
1.1.1.37 root 5310: static int is_cr = 0;
1.1 root 5311: UINT8 *buf = (UINT8 *)buffer;
5312: for(unsigned int i = 0; i < count; i++) {
5313: UINT8 data = buf[i];
5314: if(is_cr) {
5315: if(data != 0x0a) {
5316: UINT8 tmp = 0x0d;
5317: _write(1, &tmp, 1);
5318: }
5319: _write(1, &data, 1);
5320: is_cr = 0;
5321: } else if(data == 0x0d) {
5322: is_cr = 1;
5323: } else {
5324: _write(1, &data, 1);
5325: }
5326: }
5327: return(count);
5328: }
1.1.1.14 root 5329: vram_flush();
1.1 root 5330: return(_write(fd, buffer, count));
5331: }
5332:
5333: void msdos_putch(UINT8 data)
1.1.1.50 root 5334: {
5335: msdos_stdio_reopen();
5336:
5337: process_t *process = msdos_process_info_get(current_psp);
5338: int fd = msdos_psp_get_file_table(1, current_psp);
5339:
5340: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
5341: // stdout is redirected to file
5342: msdos_write(fd, &data, 1);
5343: return;
5344: }
5345:
5346: // call int 29h ?
1.1.1.58 root 5347: if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50 root 5348: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
5349: // int 29h is not hooked, no need to call int 29h
5350: msdos_putch_fast(data);
5351: #ifdef USE_SERVICE_THREAD
5352: } else if(in_service && main_thread_id != GetCurrentThreadId()) {
5353: // XXX: in usually we should not reach here
5354: // this is called from service thread to echo the input
5355: // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
5356: msdos_putch_fast(data);
5357: #endif
1.1.1.51 root 5358: } else if(in_service_29h) {
1.1.1.50 root 5359: // disallow reentering call int 29h routine to prevent an infinite loop :-(
5360: msdos_putch_fast(data);
5361: } else {
5362: // this is called from main thread, so we can call int 29h :-)
1.1.1.51 root 5363: in_service_29h = true;
1.1.1.50 root 5364: try {
5365: UINT32 tmp_pc = m_pc;
5366: UINT16 tmp_ax = REG16(AX);
5367: UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
5368:
5369: // call int 29h routine is at fffc:0027
5370: i386_call_far(DUMMY_TOP >> 4, 0x0027);
5371: REG8(AL) = data;
5372:
5373: // run cpu until call int 29h routine is done
1.1.1.54 root 5374: while(!m_exit && tmp_pc != m_pc) {
1.1.1.50 root 5375: try {
5376: hardware_run_cpu();
5377: } catch(...) {
5378: }
5379: }
5380: REG16(AX) = tmp_ax;
5381: REG16(BX) = tmp_bx;
5382: } catch(...) {
5383: }
1.1.1.51 root 5384: in_service_29h = false;
1.1.1.50 root 5385: }
5386: }
5387:
5388: void msdos_putch_fast(UINT8 data)
1.1.1.35 root 5389: #ifdef USE_SERVICE_THREAD
5390: {
5391: EnterCriticalSection(&putch_crit_sect);
5392: msdos_putch_tmp(data);
5393: LeaveCriticalSection(&putch_crit_sect);
5394: }
5395: void msdos_putch_tmp(UINT8 data)
5396: #endif
1.1 root 5397: {
1.1.1.34 root 5398: CONSOLE_SCREEN_BUFFER_INFO csbi;
5399: SMALL_RECT rect;
5400: COORD co;
1.1 root 5401: static int p = 0;
5402: static int is_kanji = 0;
5403: static int is_esc = 0;
5404: static int stored_x;
5405: static int stored_y;
5406: static WORD stored_a;
1.1.1.20 root 5407: static char tmp[64], out[64];
1.1 root 5408:
1.1.1.23 root 5409: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 5410:
5411: // output to console
5412: tmp[p++] = data;
5413:
1.1.1.14 root 5414: vram_flush();
5415:
1.1 root 5416: if(is_kanji) {
5417: // kanji character
5418: is_kanji = 0;
5419: } else if(is_esc) {
5420: // escape sequense
5421: if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
5422: p = is_esc = 0;
5423: } else if(tmp[1] == '=' && p == 4) {
5424: co.X = tmp[3] - 0x20;
1.1.1.14 root 5425: co.Y = tmp[2] - 0x20 + scr_top;
1.1 root 5426: SetConsoleCursorPosition(hStdout, co);
5427: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 5428: mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1 root 5429: cursor_moved = false;
1.1.1.59 root 5430: cursor_moved_by_crtc = false;
1.1 root 5431: p = is_esc = 0;
5432: } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59 root 5433: if(cursor_moved_by_crtc) {
5434: if(!restore_console_on_exit) {
5435: GetConsoleScreenBufferInfo(hStdout, &csbi);
5436: scr_top = csbi.srWindow.Top;
5437: }
5438: co.X = mem[0x450 + REG8(BH) * 2];
5439: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
5440: SetConsoleCursorPosition(hStdout, co);
5441: cursor_moved_by_crtc = false;
5442: }
1.1 root 5443: GetConsoleScreenBufferInfo(hStdout, &csbi);
5444: co.X = csbi.dwCursorPosition.X;
5445: co.Y = csbi.dwCursorPosition.Y;
5446: WORD wAttributes = csbi.wAttributes;
5447:
5448: if(tmp[1] == 'D') {
5449: co.Y++;
5450: } else if(tmp[1] == 'E') {
5451: co.X = 0;
5452: co.Y++;
5453: } else if(tmp[1] == 'M') {
5454: co.Y--;
5455: } else if(tmp[1] == '*') {
1.1.1.14 root 5456: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5457: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5458: co.X = 0;
5459: co.Y = csbi.srWindow.Top;
1.1 root 5460: } else if(tmp[1] == '[') {
5461: int param[256], params = 0;
5462: memset(param, 0, sizeof(param));
5463: for(int i = 2; i < p; i++) {
5464: if(tmp[i] >= '0' && tmp[i] <= '9') {
5465: param[params] *= 10;
5466: param[params] += tmp[i] - '0';
5467: } else {
5468: params++;
5469: }
5470: }
5471: if(data == 'A') {
1.1.1.14 root 5472: co.Y -= (params == 0) ? 1 : param[0];
1.1 root 5473: } else if(data == 'B') {
1.1.1.14 root 5474: co.Y += (params == 0) ? 1 : param[0];
1.1 root 5475: } else if(data == 'C') {
1.1.1.14 root 5476: co.X += (params == 0) ? 1 : param[0];
1.1 root 5477: } else if(data == 'D') {
1.1.1.14 root 5478: co.X -= (params == 0) ? 1 : param[0];
1.1 root 5479: } else if(data == 'H' || data == 'f') {
1.1.1.14 root 5480: co.X = (param[1] == 0 ? 1 : param[1]) - 1;
5481: co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1 root 5482: } else if(data == 'J') {
1.1.1.14 root 5483: clear_scr_buffer(csbi.wAttributes);
1.1 root 5484: if(param[0] == 0) {
5485: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60! root 5486: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5487: if(co.Y < csbi.srWindow.Bottom) {
5488: SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5489: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5490: }
5491: } else if(param[0] == 1) {
1.1.1.14 root 5492: if(co.Y > csbi.srWindow.Top) {
5493: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60! root 5494: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5495: }
5496: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60! root 5497: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5498: } else if(param[0] == 2) {
1.1.1.14 root 5499: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5500: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5501: co.X = co.Y = 0;
5502: }
5503: } else if(data == 'K') {
1.1.1.14 root 5504: clear_scr_buffer(csbi.wAttributes);
1.1 root 5505: if(param[0] == 0) {
5506: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60! root 5507: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5508: } else if(param[0] == 1) {
5509: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60! root 5510: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5511: } else if(param[0] == 2) {
5512: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60! root 5513: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5514: }
5515: } else if(data == 'L') {
1.1.1.14 root 5516: if(params == 0) {
5517: param[0] = 1;
1.1 root 5518: }
1.1.1.14 root 5519: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5520: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5521: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5522: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5523: clear_scr_buffer(csbi.wAttributes);
1.1 root 5524: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60! root 5525: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5526: co.X = 0;
5527: } else if(data == 'M') {
1.1.1.14 root 5528: if(params == 0) {
5529: param[0] = 1;
5530: }
5531: if(co.Y + param[0] > csbi.srWindow.Bottom) {
5532: clear_scr_buffer(csbi.wAttributes);
5533: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5534: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5535: } else {
1.1.1.14 root 5536: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5537: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5538: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 5539: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 5540: clear_scr_buffer(csbi.wAttributes);
1.1 root 5541: }
5542: co.X = 0;
5543: } else if(data == 'h') {
5544: if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60! root 5545: ci_new.bVisible = FALSE;
1.1 root 5546: }
5547: } else if(data == 'l') {
5548: if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60! root 5549: ci_new.bVisible = TRUE;
1.1 root 5550: }
5551: } else if(data == 'm') {
5552: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
5553: int reverse = 0, hidden = 0;
5554: for(int i = 0; i < params; i++) {
5555: if(param[i] == 1) {
5556: wAttributes |= FOREGROUND_INTENSITY;
5557: } else if(param[i] == 4) {
5558: wAttributes |= COMMON_LVB_UNDERSCORE;
5559: } else if(param[i] == 7) {
5560: reverse = 1;
5561: } else if(param[i] == 8 || param[i] == 16) {
5562: hidden = 1;
5563: } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
5564: wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
5565: if(param[i] >= 17 && param[i] <= 23) {
5566: param[i] -= 16;
5567: } else {
5568: param[i] -= 30;
5569: }
5570: if(param[i] & 1) {
5571: wAttributes |= FOREGROUND_RED;
5572: }
5573: if(param[i] & 2) {
5574: wAttributes |= FOREGROUND_GREEN;
5575: }
5576: if(param[i] & 4) {
5577: wAttributes |= FOREGROUND_BLUE;
5578: }
5579: } else if(param[i] >= 40 && param[i] <= 47) {
5580: wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
5581: if((param[i] - 40) & 1) {
5582: wAttributes |= BACKGROUND_RED;
5583: }
5584: if((param[i] - 40) & 2) {
5585: wAttributes |= BACKGROUND_GREEN;
5586: }
5587: if((param[i] - 40) & 4) {
5588: wAttributes |= BACKGROUND_BLUE;
5589: }
5590: }
5591: }
5592: if(reverse) {
5593: wAttributes &= ~0xff;
5594: wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
5595: }
5596: if(hidden) {
5597: wAttributes &= ~0x0f;
5598: wAttributes |= (wAttributes >> 4) & 0x0f;
5599: }
5600: } else if(data == 'n') {
5601: if(param[0] == 6) {
5602: char tmp[16];
5603: sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
5604: int len = strlen(tmp);
1.1.1.32 root 5605: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 5606: #ifdef USE_SERVICE_THREAD
5607: EnterCriticalSection(&key_buf_crit_sect);
5608: #endif
1.1.1.32 root 5609: for(int i = 0; i < len; i++) {
1.1.1.51 root 5610: pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32 root 5611: }
1.1.1.35 root 5612: #ifdef USE_SERVICE_THREAD
5613: LeaveCriticalSection(&key_buf_crit_sect);
5614: #endif
1.1 root 5615: }
5616: }
5617: } else if(data == 's') {
5618: stored_x = co.X;
5619: stored_y = co.Y;
5620: stored_a = wAttributes;
5621: } else if(data == 'u') {
5622: co.X = stored_x;
5623: co.Y = stored_y;
5624: wAttributes = stored_a;
5625: }
5626: }
5627: if(co.X < 0) {
5628: co.X = 0;
5629: } else if(co.X >= csbi.dwSize.X) {
5630: co.X = csbi.dwSize.X - 1;
5631: }
1.1.1.14 root 5632: if(co.Y < csbi.srWindow.Top) {
5633: co.Y = csbi.srWindow.Top;
5634: } else if(co.Y > csbi.srWindow.Bottom) {
5635: co.Y = csbi.srWindow.Bottom;
1.1 root 5636: }
5637: if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
5638: SetConsoleCursorPosition(hStdout, co);
5639: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 5640: mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1 root 5641: cursor_moved = false;
5642: }
5643: if(wAttributes != csbi.wAttributes) {
5644: SetConsoleTextAttribute(hStdout, wAttributes);
5645: }
5646: p = is_esc = 0;
5647: }
5648: return;
5649: } else {
5650: if(msdos_lead_byte_check(data)) {
5651: is_kanji = 1;
5652: return;
5653: } else if(data == 0x1b) {
5654: is_esc = 1;
5655: return;
5656: }
5657: }
1.1.1.20 root 5658:
5659: DWORD q = 0, num;
5660: is_kanji = 0;
5661: for(int i = 0; i < p; i++) {
5662: UINT8 c = tmp[i];
5663: if(is_kanji) {
5664: is_kanji = 0;
5665: } else if(msdos_lead_byte_check(data)) {
5666: is_kanji = 1;
5667: } else if(msdos_ctrl_code_check(data)) {
5668: out[q++] = '^';
5669: c += 'A' - 1;
5670: }
5671: out[q++] = c;
5672: }
1.1.1.59 root 5673: if(cursor_moved_by_crtc) {
5674: if(!restore_console_on_exit) {
5675: GetConsoleScreenBufferInfo(hStdout, &csbi);
5676: scr_top = csbi.srWindow.Top;
5677: }
5678: co.X = mem[0x450 + REG8(BH) * 2];
5679: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
5680: SetConsoleCursorPosition(hStdout, co);
5681: cursor_moved_by_crtc = false;
5682: }
1.1.1.34 root 5683: if(q == 1 && out[0] == 0x08) {
5684: // back space
5685: GetConsoleScreenBufferInfo(hStdout, &csbi);
5686: if(csbi.dwCursorPosition.X > 0) {
5687: co.X = csbi.dwCursorPosition.X - 1;
5688: co.Y = csbi.dwCursorPosition.Y;
5689: SetConsoleCursorPosition(hStdout, co);
5690: } else if(csbi.dwCursorPosition.Y > 0) {
5691: co.X = csbi.dwSize.X - 1;
5692: co.Y = csbi.dwCursorPosition.Y - 1;
5693: SetConsoleCursorPosition(hStdout, co);
5694: } else {
1.1.1.60! root 5695: WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34 root 5696: }
5697: } else {
1.1.1.60! root 5698: WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34 root 5699: }
1.1 root 5700: p = 0;
1.1.1.14 root 5701:
1.1.1.15 root 5702: if(!restore_console_on_exit) {
5703: GetConsoleScreenBufferInfo(hStdout, &csbi);
5704: scr_top = csbi.srWindow.Top;
5705: }
1.1 root 5706: cursor_moved = true;
5707: }
5708:
5709: int msdos_aux_in()
5710: {
1.1.1.21 root 5711: msdos_stdio_reopen();
5712:
1.1.1.20 root 5713: process_t *process = msdos_process_info_get(current_psp);
5714: int fd = msdos_psp_get_file_table(3, current_psp);
5715:
5716: if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1 root 5717: char data = 0;
1.1.1.37 root 5718: msdos_read(fd, &data, 1);
1.1 root 5719: return(data);
5720: } else {
5721: return(EOF);
5722: }
5723: }
5724:
5725: void msdos_aux_out(char data)
5726: {
1.1.1.21 root 5727: msdos_stdio_reopen();
5728:
1.1.1.20 root 5729: process_t *process = msdos_process_info_get(current_psp);
5730: int fd = msdos_psp_get_file_table(3, current_psp);
5731:
5732: if(fd < process->max_files && file_handler[fd].valid) {
5733: msdos_write(fd, &data, 1);
1.1 root 5734: }
5735: }
5736:
5737: void msdos_prn_out(char data)
5738: {
1.1.1.21 root 5739: msdos_stdio_reopen();
5740:
1.1.1.20 root 5741: process_t *process = msdos_process_info_get(current_psp);
5742: int fd = msdos_psp_get_file_table(4, current_psp);
5743:
5744: if(fd < process->max_files && file_handler[fd].valid) {
5745: msdos_write(fd, &data, 1);
1.1 root 5746: }
5747: }
5748:
5749: // memory control
5750:
1.1.1.52 root 5751: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1 root 5752: {
5753: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5754:
5755: mcb->mz = mz;
5756: mcb->psp = psp;
1.1.1.30 root 5757: mcb->paragraphs = paragraphs;
1.1.1.39 root 5758:
5759: if(prog_name != NULL) {
5760: memset(mcb->prog_name, 0, 8);
5761: memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
5762: }
1.1 root 5763: return(mcb);
5764: }
5765:
5766: void msdos_mcb_check(mcb_t *mcb)
5767: {
5768: if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28 root 5769: #if 0
5770: // shutdown now !!!
5771: fatalerror("broken memory control block\n");
5772: #else
5773: // return error code and continue
5774: throw(0x07); // broken memory control block
5775: #endif
1.1 root 5776: }
5777: }
5778:
1.1.1.39 root 5779: void msdos_mem_split(int seg, int paragraphs)
1.1 root 5780: {
5781: int mcb_seg = seg - 1;
5782: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5783: msdos_mcb_check(mcb);
5784:
1.1.1.30 root 5785: if(mcb->paragraphs > paragraphs) {
1.1 root 5786: int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30 root 5787: int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1 root 5788:
5789: msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
5790: mcb->mz = 'M';
1.1.1.30 root 5791: mcb->paragraphs = paragraphs;
1.1 root 5792: }
5793: }
5794:
5795: void msdos_mem_merge(int seg)
5796: {
5797: int mcb_seg = seg - 1;
5798: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5799: msdos_mcb_check(mcb);
5800:
5801: while(1) {
5802: if(mcb->mz == 'Z') {
5803: break;
5804: }
1.1.1.30 root 5805: int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1 root 5806: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5807: msdos_mcb_check(next_mcb);
5808:
5809: if(next_mcb->psp != 0) {
5810: break;
5811: }
5812: mcb->mz = next_mcb->mz;
1.1.1.30 root 5813: mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1 root 5814: }
5815: }
5816:
1.1.1.8 root 5817: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1 root 5818: {
5819: while(1) {
5820: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5821: bool last_block;
1.1 root 5822:
1.1.1.14 root 5823: if(mcb->psp == 0) {
5824: msdos_mem_merge(mcb_seg + 1);
5825: } else {
5826: msdos_mcb_check(mcb);
5827: }
1.1.1.33 root 5828: if(!(last_block = (mcb->mz == 'Z'))) {
5829: // check if the next is dummy mcb to link to umb
5830: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5831: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5832: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5833: }
5834: if(!(new_process && !last_block)) {
1.1.1.30 root 5835: if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1 root 5836: msdos_mem_split(mcb_seg + 1, paragraphs);
5837: mcb->psp = current_psp;
5838: return(mcb_seg + 1);
5839: }
5840: }
5841: if(mcb->mz == 'Z') {
5842: break;
5843: }
1.1.1.30 root 5844: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5845: }
5846: return(-1);
5847: }
5848:
5849: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
5850: {
5851: int mcb_seg = seg - 1;
5852: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5853: msdos_mcb_check(mcb);
1.1.1.30 root 5854: int current_paragraphs = mcb->paragraphs;
1.1 root 5855:
5856: msdos_mem_merge(seg);
1.1.1.30 root 5857: if(paragraphs > mcb->paragraphs) {
1.1.1.14 root 5858: if(max_paragraphs) {
1.1.1.30 root 5859: *max_paragraphs = mcb->paragraphs;
1.1.1.14 root 5860: }
1.1 root 5861: msdos_mem_split(seg, current_paragraphs);
5862: return(-1);
5863: }
5864: msdos_mem_split(seg, paragraphs);
5865: return(0);
5866: }
5867:
5868: void msdos_mem_free(int seg)
5869: {
5870: int mcb_seg = seg - 1;
5871: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5872: msdos_mcb_check(mcb);
5873:
5874: mcb->psp = 0;
5875: msdos_mem_merge(seg);
5876: }
5877:
1.1.1.8 root 5878: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1 root 5879: {
5880: int max_paragraphs = 0;
5881:
5882: while(1) {
5883: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5884: bool last_block;
5885:
1.1 root 5886: msdos_mcb_check(mcb);
5887:
1.1.1.33 root 5888: if(!(last_block = (mcb->mz == 'Z'))) {
5889: // check if the next is dummy mcb to link to umb
5890: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5891: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5892: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5893: }
5894: if(!(new_process && !last_block)) {
1.1.1.30 root 5895: if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
5896: max_paragraphs = mcb->paragraphs;
1.1 root 5897: }
5898: }
5899: if(mcb->mz == 'Z') {
5900: break;
5901: }
1.1.1.30 root 5902: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5903: }
1.1.1.14 root 5904: return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1 root 5905: }
5906:
1.1.1.8 root 5907: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
5908: {
5909: int last_seg = -1;
5910:
5911: while(1) {
5912: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5913: msdos_mcb_check(mcb);
5914:
1.1.1.14 root 5915: if(mcb->psp == psp) {
1.1.1.8 root 5916: last_seg = mcb_seg;
5917: }
1.1.1.14 root 5918: if(mcb->mz == 'Z') {
5919: break;
5920: }
1.1.1.30 root 5921: mcb_seg += 1 + mcb->paragraphs;
1.1.1.8 root 5922: }
5923: return(last_seg);
5924: }
5925:
1.1.1.19 root 5926: int msdos_mem_get_umb_linked()
5927: {
1.1.1.33 root 5928: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5929: msdos_mcb_check(mcb);
1.1.1.19 root 5930:
1.1.1.33 root 5931: if(mcb->mz == 'M') {
5932: return(-1);
1.1.1.19 root 5933: }
5934: return(0);
5935: }
5936:
1.1.1.33 root 5937: void msdos_mem_link_umb()
1.1.1.19 root 5938: {
1.1.1.33 root 5939: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5940: msdos_mcb_check(mcb);
1.1.1.19 root 5941:
1.1.1.33 root 5942: mcb->mz = 'M';
5943: mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39 root 5944:
5945: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19 root 5946: }
5947:
1.1.1.33 root 5948: void msdos_mem_unlink_umb()
1.1.1.19 root 5949: {
1.1.1.33 root 5950: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5951: msdos_mcb_check(mcb);
1.1.1.19 root 5952:
1.1.1.33 root 5953: mcb->mz = 'Z';
5954: mcb->paragraphs = 0;
1.1.1.39 root 5955:
5956: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19 root 5957: }
5958:
1.1.1.29 root 5959: #ifdef SUPPORT_HMA
5960:
5961: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
5962: {
5963: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5964:
5965: mcb->ms[0] = 'M';
5966: mcb->ms[1] = 'S';
5967: mcb->owner = owner;
5968: mcb->size = size;
5969: mcb->next = next;
5970: return(mcb);
5971: }
5972:
5973: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
5974: {
5975: return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
5976: }
5977:
5978: int msdos_hma_mem_split(int offset, int size)
5979: {
5980: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5981:
5982: if(!msdos_is_hma_mcb_valid(mcb)) {
5983: return(-1);
5984: }
5985: if(mcb->size >= size + 0x10) {
5986: int new_offset = offset + 0x10 + size;
5987: int new_size = mcb->size - 0x10 - size;
5988:
5989: msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
5990: mcb->size = size;
5991: mcb->next = new_offset;
5992: return(0);
5993: }
5994: return(-1);
5995: }
5996:
5997: void msdos_hma_mem_merge(int offset)
5998: {
5999: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
6000:
6001: if(!msdos_is_hma_mcb_valid(mcb)) {
6002: return;
6003: }
6004: while(1) {
6005: if(mcb->next == 0) {
6006: break;
6007: }
6008: hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
6009:
6010: if(!msdos_is_hma_mcb_valid(next_mcb)) {
6011: return;
6012: }
6013: if(next_mcb->owner != 0) {
6014: break;
6015: }
6016: mcb->size += 0x10 + next_mcb->size;
6017: mcb->next = next_mcb->next;
6018: }
6019: }
6020:
6021: int msdos_hma_mem_alloc(int size, UINT16 owner)
6022: {
6023: int offset = 0x10; // first mcb in HMA
6024:
6025: while(1) {
6026: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
6027:
6028: if(!msdos_is_hma_mcb_valid(mcb)) {
6029: return(-1);
6030: }
6031: if(mcb->owner == 0) {
6032: msdos_hma_mem_merge(offset);
6033: }
6034: if(mcb->owner == 0 && mcb->size >= size) {
6035: msdos_hma_mem_split(offset, size);
6036: mcb->owner = owner;
6037: return(offset);
6038: }
6039: if(mcb->next == 0) {
6040: break;
6041: }
6042: offset = mcb->next;
6043: }
6044: return(-1);
6045: }
6046:
6047: int msdos_hma_mem_realloc(int offset, int size)
6048: {
6049: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
6050:
6051: if(!msdos_is_hma_mcb_valid(mcb)) {
6052: return(-1);
6053: }
6054: if(mcb->size < size) {
6055: return(-1);
6056: }
6057: msdos_hma_mem_split(offset, size);
6058: return(0);
6059: }
6060:
6061: void msdos_hma_mem_free(int offset)
6062: {
6063: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
6064:
6065: if(!msdos_is_hma_mcb_valid(mcb)) {
6066: return;
6067: }
6068: mcb->owner = 0;
6069: msdos_hma_mem_merge(offset);
6070: }
6071:
6072: int msdos_hma_mem_get_free(int *available_offset)
6073: {
6074: int offset = 0x10; // first mcb in HMA
6075: int size = 0;
6076:
6077: while(1) {
6078: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
6079:
6080: if(!msdos_is_hma_mcb_valid(mcb)) {
6081: return(0);
6082: }
6083: if(mcb->owner == 0 && size < mcb->size) {
6084: if(available_offset != NULL) {
6085: *available_offset = offset;
6086: }
6087: size = mcb->size;
6088: }
6089: if(mcb->next == 0) {
6090: break;
6091: }
6092: offset = mcb->next;
6093: }
6094: return(size);
6095: }
6096:
6097: #endif
6098:
1.1 root 6099: // environment
6100:
1.1.1.45 root 6101: void msdos_env_set_argv(int env_seg, const char *argv)
1.1 root 6102: {
6103: char *dst = (char *)(mem + (env_seg << 4));
6104:
6105: while(1) {
6106: if(dst[0] == 0) {
6107: break;
6108: }
6109: dst += strlen(dst) + 1;
6110: }
6111: *dst++ = 0; // end of environment
6112: *dst++ = 1; // top of argv[0]
6113: *dst++ = 0;
6114: memcpy(dst, argv, strlen(argv));
6115: dst += strlen(argv);
6116: *dst++ = 0;
6117: *dst++ = 0;
6118: }
6119:
1.1.1.45 root 6120: const char *msdos_env_get_argv(int env_seg)
1.1 root 6121: {
6122: static char env[ENV_SIZE];
6123: char *src = env;
6124:
6125: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
6126: while(1) {
6127: if(src[0] == 0) {
6128: if(src[1] == 1) {
6129: return(src + 3);
6130: }
6131: break;
6132: }
6133: src += strlen(src) + 1;
6134: }
6135: return(NULL);
6136: }
6137:
1.1.1.45 root 6138: const char *msdos_env_get(int env_seg, const char *name)
1.1 root 6139: {
6140: static char env[ENV_SIZE];
6141: char *src = env;
6142:
6143: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
6144: while(1) {
6145: if(src[0] == 0) {
6146: break;
6147: }
6148: int len = strlen(src);
6149: char *n = my_strtok(src, "=");
6150: char *v = src + strlen(n) + 1;
6151:
6152: if(_stricmp(name, n) == 0) {
6153: return(v);
6154: }
6155: src += len + 1;
6156: }
6157: return(NULL);
6158: }
6159:
1.1.1.45 root 6160: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1 root 6161: {
6162: char env[ENV_SIZE];
6163: char *src = env;
6164: char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45 root 6165: const char *argv = msdos_env_get_argv(env_seg);
1.1 root 6166: int done = 0;
6167:
6168: memcpy(src, dst, ENV_SIZE);
6169: memset(dst, 0, ENV_SIZE);
6170: while(1) {
6171: if(src[0] == 0) {
6172: break;
6173: }
6174: int len = strlen(src);
6175: char *n = my_strtok(src, "=");
6176: char *v = src + strlen(n) + 1;
6177: char tmp[1024];
6178:
6179: if(_stricmp(name, n) == 0) {
6180: sprintf(tmp, "%s=%s", n, value);
6181: done = 1;
6182: } else {
6183: sprintf(tmp, "%s=%s", n, v);
6184: }
6185: memcpy(dst, tmp, strlen(tmp));
6186: dst += strlen(tmp) + 1;
6187: src += len + 1;
6188: }
6189: if(!done) {
6190: char tmp[1024];
6191:
6192: sprintf(tmp, "%s=%s", name, value);
6193: memcpy(dst, tmp, strlen(tmp));
6194: dst += strlen(tmp) + 1;
6195: }
6196: if(argv) {
6197: *dst++ = 0; // end of environment
6198: *dst++ = 1; // top of argv[0]
6199: *dst++ = 0;
6200: memcpy(dst, argv, strlen(argv));
6201: dst += strlen(argv);
6202: *dst++ = 0;
6203: *dst++ = 0;
6204: }
6205: }
6206:
6207: // process
6208:
1.1.1.8 root 6209: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1 root 6210: {
6211: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6212:
6213: memset(psp, 0, PSP_SIZE);
6214: psp->exit[0] = 0xcd;
6215: psp->exit[1] = 0x20;
1.1.1.8 root 6216: psp->first_mcb = mcb_seg;
1.1.1.46 root 6217: #if 1
1.1.1.49 root 6218: psp->call5[0] = 0xcd; // int 30h
6219: psp->call5[1] = 0x30;
1.1.1.46 root 6220: psp->call5[2] = 0xc3; // ret
6221: #else
6222: psp->call5[0] = 0x8a; // mov ah, cl
6223: psp->call5[1] = 0xe1;
6224: psp->call5[2] = 0xcd; // int 21h
6225: psp->call5[3] = 0x21;
6226: psp->call5[4] = 0xc3; // ret
6227: #endif
1.1 root 6228: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
6229: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
6230: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
6231: psp->parent_psp = parent_psp;
1.1.1.20 root 6232: if(parent_psp == (UINT16)-1) {
6233: for(int i = 0; i < 20; i++) {
6234: if(file_handler[i].valid) {
6235: psp->file_table[i] = i;
6236: } else {
6237: psp->file_table[i] = 0xff;
6238: }
1.1 root 6239: }
1.1.1.20 root 6240: } else {
6241: memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1 root 6242: }
6243: psp->env_seg = env_seg;
6244: psp->stack.w.l = REG16(SP);
1.1.1.3 root 6245: psp->stack.w.h = SREG(SS);
1.1.1.14 root 6246: psp->file_table_size = 20;
6247: psp->file_table_ptr.w.l = 0x18;
6248: psp->file_table_ptr.w.h = psp_seg;
1.1 root 6249: psp->service[0] = 0xcd;
6250: psp->service[1] = 0x21;
6251: psp->service[2] = 0xcb;
6252: return(psp);
6253: }
6254:
1.1.1.20 root 6255: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
6256: {
6257: if(psp_seg && fd < 20) {
6258: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6259: psp->file_table[fd] = value;
6260: }
6261: }
6262:
6263: int msdos_psp_get_file_table(int fd, int psp_seg)
6264: {
6265: if(psp_seg && fd < 20) {
6266: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6267: fd = psp->file_table[fd];
6268: }
6269: return fd;
6270: }
6271:
1.1.1.52 root 6272: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1 root 6273: {
6274: // load command file
6275: int fd = -1;
1.1.1.45 root 6276: int sio_port = 0;
6277: int lpt_port = 0;
1.1 root 6278: int dos_command = 0;
1.1.1.24 root 6279: char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38 root 6280: char pipe_stdin_path[MAX_PATH] = {0};
6281: char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39 root 6282: char pipe_stderr_path[MAX_PATH] = {0};
1.1 root 6283:
6284: int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
6285: int opt_len = mem[opt_ofs];
6286: memset(opt, 0, sizeof(opt));
6287: memcpy(opt, mem + opt_ofs + 1, opt_len);
6288:
1.1.1.14 root 6289: if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
6290: // this is a batch file, run command.com
6291: char tmp[MAX_PATH];
6292: if(opt_len != 0) {
6293: sprintf(tmp, "/C %s %s", cmd, opt);
6294: } else {
6295: sprintf(tmp, "/C %s", cmd);
6296: }
6297: strcpy(opt, tmp);
6298: opt_len = strlen(opt);
6299: mem[opt_ofs] = opt_len;
6300: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6301: strcpy(command, comspec_path);
6302: strcpy(name_tmp, "COMMAND.COM");
6303: } else {
6304: if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
6305: // redirect C:\COMMAND.COM to comspec_path
6306: strcpy(command, comspec_path);
6307: } else {
6308: strcpy(command, cmd);
6309: }
1.1.1.60! root 6310: if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24 root 6311: return(-1);
6312: }
1.1.1.14 root 6313: memset(name_tmp, 0, sizeof(name_tmp));
6314: strcpy(name_tmp, name);
6315:
6316: // check command.com
1.1.1.38 root 6317: if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
6318: // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14 root 6319: if(opt_len == 0) {
6320: // process_t *current_process = msdos_process_info_get(current_psp);
6321: process_t *current_process = NULL;
6322: for(int i = 0; i < MAX_PROCESS; i++) {
6323: if(process[i].psp == current_psp) {
6324: current_process = &process[i];
6325: break;
6326: }
6327: }
6328: if(current_process != NULL) {
6329: param->cmd_line.dw = current_process->dta.dw;
6330: opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
6331: opt_len = mem[opt_ofs];
6332: memset(opt, 0, sizeof(opt));
6333: memcpy(opt, mem + opt_ofs + 1, opt_len);
6334: }
6335: }
6336: for(int i = 0; i < opt_len; i++) {
6337: if(opt[i] == ' ') {
6338: continue;
6339: }
6340: if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
6341: for(int j = i + 3; j < opt_len; j++) {
6342: if(opt[j] == ' ') {
6343: continue;
6344: }
6345: char *token = my_strtok(opt + j, " ");
6346:
1.1.1.38 root 6347: strcpy(command, token);
6348: char tmp[MAX_PATH];
6349: strcpy(tmp, token + strlen(token) + 1);
1.1.1.39 root 6350: strcpy(opt, "");
6351: for(int i = 0; i < strlen(tmp); i++) {
6352: if(tmp[i] != ' ') {
6353: strcpy(opt, tmp + i);
6354: break;
6355: }
6356: }
6357: strcpy(tmp, opt);
1.1.1.38 root 6358:
6359: if(al == 0x00) {
1.1.1.39 root 6360: #define GET_FILE_PATH() { \
6361: if(token[0] != '>' && token[0] != '<') { \
6362: token++; \
6363: } \
6364: token++; \
6365: while(*token == ' ') { \
6366: token++; \
6367: } \
6368: char *ptr = token; \
6369: while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
6370: ptr++; \
6371: } \
6372: *ptr = '\0'; \
6373: }
6374: if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
6375: GET_FILE_PATH();
1.1.1.38 root 6376: strcpy(pipe_stdin_path, token);
6377: strcpy(opt, tmp);
6378: }
1.1.1.39 root 6379: if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
6380: GET_FILE_PATH();
1.1.1.38 root 6381: strcpy(pipe_stdout_path, token);
6382: strcpy(opt, tmp);
6383: }
1.1.1.39 root 6384: if((token = strstr(opt, "2>")) != NULL) {
6385: GET_FILE_PATH();
6386: strcpy(pipe_stderr_path, token);
6387: strcpy(opt, tmp);
6388: }
6389: #undef GET_FILE_PATH
6390:
6391: if((token = strstr(opt, "0<")) != NULL) {
6392: *token = '\0';
6393: }
6394: if((token = strstr(opt, "1>")) != NULL) {
6395: *token = '\0';
6396: }
6397: if((token = strstr(opt, "2>")) != NULL) {
6398: *token = '\0';
6399: }
1.1.1.38 root 6400: if((token = strstr(opt, "<")) != NULL) {
6401: *token = '\0';
6402: }
6403: if((token = strstr(opt, ">")) != NULL) {
6404: *token = '\0';
6405: }
1.1.1.14 root 6406: }
1.1.1.39 root 6407: for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
6408: opt[i] = '\0';
6409: }
1.1.1.38 root 6410: opt_len = strlen(opt);
6411: mem[opt_ofs] = opt_len;
6412: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6413: dos_command = 1;
1.1.1.14 root 6414: break;
1.1 root 6415: }
6416: }
1.1.1.14 root 6417: break;
1.1 root 6418: }
6419: }
6420: }
6421:
6422: // load command file
6423: strcpy(path, command);
6424: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
6425: sprintf(path, "%s.COM", command);
6426: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
6427: sprintf(path, "%s.EXE", command);
6428: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14 root 6429: sprintf(path, "%s.BAT", command);
6430: if(_access(path, 0) == 0) {
6431: // this is a batch file, run command.com
6432: char tmp[MAX_PATH];
6433: if(opt_len != 0) {
6434: sprintf(tmp, "/C %s %s", path, opt);
6435: } else {
6436: sprintf(tmp, "/C %s", path);
6437: }
6438: strcpy(opt, tmp);
6439: opt_len = strlen(opt);
6440: mem[opt_ofs] = opt_len;
6441: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6442: strcpy(path, comspec_path);
6443: strcpy(name_tmp, "COMMAND.COM");
6444: fd = _open(path, _O_RDONLY | _O_BINARY);
6445: } else {
6446: // search path in parent environments
6447: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45 root 6448: const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14 root 6449: if(env != NULL) {
6450: char env_path[4096];
6451: strcpy(env_path, env);
6452: char *token = my_strtok(env_path, ";");
6453:
6454: while(token != NULL) {
6455: if(strlen(token) != 0) {
6456: sprintf(path, "%s", msdos_combine_path(token, command));
6457: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6458: break;
6459: }
6460: sprintf(path, "%s.COM", msdos_combine_path(token, command));
6461: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6462: break;
6463: }
6464: sprintf(path, "%s.EXE", msdos_combine_path(token, command));
6465: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6466: break;
6467: }
6468: sprintf(path, "%s.BAT", msdos_combine_path(token, command));
6469: if(_access(path, 0) == 0) {
6470: // this is a batch file, run command.com
6471: char tmp[MAX_PATH];
6472: if(opt_len != 0) {
6473: sprintf(tmp, "/C %s %s", path, opt);
6474: } else {
6475: sprintf(tmp, "/C %s", path);
6476: }
6477: strcpy(opt, tmp);
6478: opt_len = strlen(opt);
6479: mem[opt_ofs] = opt_len;
6480: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6481: strcpy(path, comspec_path);
6482: strcpy(name_tmp, "COMMAND.COM");
6483: fd = _open(path, _O_RDONLY | _O_BINARY);
6484: break;
6485: }
1.1.1.8 root 6486: }
1.1.1.14 root 6487: token = my_strtok(NULL, ";");
1.1 root 6488: }
6489: }
6490: }
6491: }
6492: }
6493: }
6494: if(fd == -1) {
1.1.1.38 root 6495: // we can not find command.com in the path, so open comspec_path
6496: if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
6497: strcpy(command, comspec_path);
6498: strcpy(path, command);
6499: fd = _open(path, _O_RDONLY | _O_BINARY);
6500: }
6501: }
6502: if(fd == -1) {
1.1.1.52 root 6503: if(!first_process && al == 0 && dos_command) {
1.1 root 6504: // may be dos command
6505: char tmp[MAX_PATH];
1.1.1.52 root 6506: if(opt_len != 0) {
6507: sprintf(tmp, "%s %s", command, opt);
6508: } else {
6509: sprintf(tmp, "%s", command);
6510: }
6511: retval = system(tmp);
1.1 root 6512: return(0);
6513: } else {
6514: return(-1);
6515: }
6516: }
1.1.1.52 root 6517: memset(file_buffer, 0, sizeof(file_buffer));
1.1 root 6518: _read(fd, file_buffer, sizeof(file_buffer));
6519: _close(fd);
6520:
1.1.1.52 root 6521: // check if this is win32 program
6522: if(!first_process && al == 0) {
6523: UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
6524: UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
6525: if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
6526: UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
6527: UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
6528: if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
6529: char tmp[MAX_PATH];
6530: if(opt_len != 0) {
6531: sprintf(tmp, "\"%s\" %s", path, opt);
6532: } else {
6533: sprintf(tmp, "\"%s\"", path);
6534: }
6535: retval = system(tmp);
6536: return(0);
6537: }
6538: }
6539: }
6540:
1.1 root 6541: // copy environment
1.1.1.29 root 6542: int umb_linked, env_seg, psp_seg;
1.1 root 6543:
1.1.1.29 root 6544: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
6545: msdos_mem_unlink_umb();
6546: }
1.1.1.8 root 6547: if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29 root 6548: if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
6549: if(umb_linked != 0) {
6550: msdos_mem_link_umb();
6551: }
6552: return(-1);
6553: }
1.1 root 6554: }
6555: if(param->env_seg == 0) {
6556: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
6557: memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
6558: } else {
6559: memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
6560: }
6561: msdos_env_set_argv(env_seg, msdos_short_full_path(path));
6562:
6563: // check exe header
6564: exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8 root 6565: int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1 root 6566: UINT16 cs, ss, ip, sp;
6567:
6568: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
6569: // memory allocation
6570: int header_size = header->header_size * 16;
6571: int load_size = header->pages * 512 - header_size;
6572: if(header_size + load_size < 512) {
6573: load_size = 512 - header_size;
6574: }
6575: paragraphs = (PSP_SIZE + load_size) >> 4;
6576: if(paragraphs + header->min_alloc > free_paragraphs) {
6577: msdos_mem_free(env_seg);
6578: return(-1);
6579: }
6580: paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
6581: if(paragraphs > free_paragraphs) {
6582: paragraphs = free_paragraphs;
6583: }
1.1.1.8 root 6584: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6585: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6586: if(umb_linked != 0) {
6587: msdos_mem_link_umb();
6588: }
6589: msdos_mem_free(env_seg);
6590: return(-1);
6591: }
1.1 root 6592: }
6593: // relocation
6594: int start_seg = psp_seg + (PSP_SIZE >> 4);
6595: for(int i = 0; i < header->relocations; i++) {
6596: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
6597: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
6598: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
6599: }
6600: memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
6601: // segments
6602: cs = header->init_cs + start_seg;
6603: ss = header->init_ss + start_seg;
6604: ip = header->init_ip;
6605: sp = header->init_sp - 2; // for symdeb
6606: } else {
6607: // memory allocation
6608: paragraphs = free_paragraphs;
1.1.1.8 root 6609: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6610: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6611: if(umb_linked != 0) {
6612: msdos_mem_link_umb();
6613: }
6614: msdos_mem_free(env_seg);
6615: return(-1);
6616: }
1.1 root 6617: }
6618: int start_seg = psp_seg + (PSP_SIZE >> 4);
6619: memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
6620: // segments
6621: cs = ss = psp_seg;
6622: ip = 0x100;
6623: sp = 0xfffe;
6624: }
1.1.1.29 root 6625: if(umb_linked != 0) {
6626: msdos_mem_link_umb();
6627: }
1.1 root 6628:
6629: // create psp
1.1.1.3 root 6630: *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
6631: *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1 root 6632: psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
6633: memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
6634: memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
6635: memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
6636:
6637: mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
6638: mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
6639: mcb_psp->psp = mcb_env->psp = psp_seg;
6640:
1.1.1.4 root 6641: for(int i = 0; i < 8; i++) {
6642: if(name_tmp[i] == '.') {
6643: mcb_psp->prog_name[i] = '\0';
6644: break;
6645: } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
6646: mcb_psp->prog_name[i] = name_tmp[i];
6647: i++;
6648: mcb_psp->prog_name[i] = name_tmp[i];
6649: } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
6650: mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
6651: } else {
6652: mcb_psp->prog_name[i] = name_tmp[i];
6653: }
6654: }
6655:
1.1 root 6656: // process info
6657: process_t *process = msdos_process_info_create(psp_seg);
6658: strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33 root 6659: #ifdef USE_DEBUGGER
6660: strcpy(process->module_path, path);
6661: #endif
1.1 root 6662: process->dta.w.l = 0x80;
6663: process->dta.w.h = psp_seg;
6664: process->switchar = '/';
6665: process->max_files = 20;
6666: process->parent_int_10h_feh_called = int_10h_feh_called;
6667: process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14 root 6668: process->parent_ds = SREG(DS);
1.1.1.31 root 6669: process->parent_es = SREG(ES);
1.1 root 6670:
6671: current_psp = psp_seg;
1.1.1.23 root 6672: msdos_sda_update(current_psp);
1.1 root 6673:
6674: if(al == 0x00) {
6675: int_10h_feh_called = int_10h_ffh_called = false;
6676:
1.1.1.38 root 6677: // pipe
6678: if(pipe_stdin_path[0] != '\0') {
1.1.1.45 root 6679: // if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
6680: if(msdos_is_device_path(pipe_stdin_path)) {
6681: fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
6682: } else {
6683: fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
6684: }
6685: if(fd != -1) {
6686: msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, msdos_device_info(pipe_stdin_path), current_psp, sio_port, lpt_port);
1.1.1.38 root 6687: psp->file_table[0] = fd;
6688: msdos_psp_set_file_table(fd, fd, current_psp);
6689: }
6690: }
6691: if(pipe_stdout_path[0] != '\0') {
6692: if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60! root 6693: SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
! 6694: DeleteFileA(pipe_stdout_path);
1.1.1.38 root 6695: }
1.1.1.45 root 6696: // if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
6697: if(msdos_is_device_path(pipe_stdout_path)) {
6698: fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
6699: } else {
6700: fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
6701: }
6702: if(fd != -1) {
6703: msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, msdos_device_info(pipe_stdout_path), current_psp, sio_port, lpt_port);
1.1.1.38 root 6704: psp->file_table[1] = fd;
6705: msdos_psp_set_file_table(fd, fd, current_psp);
6706: }
6707: }
1.1.1.39 root 6708: if(pipe_stderr_path[0] != '\0') {
6709: if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60! root 6710: SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
! 6711: DeleteFileA(pipe_stderr_path);
1.1.1.39 root 6712: }
1.1.1.45 root 6713: // if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
6714: if(msdos_is_device_path(pipe_stderr_path)) {
6715: fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
6716: } else {
6717: fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
6718: }
6719: if(fd != -1) {
6720: msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 1, msdos_device_info(pipe_stderr_path), current_psp, sio_port, lpt_port);
1.1.1.39 root 6721: psp->file_table[2] = fd;
6722: msdos_psp_set_file_table(fd, fd, current_psp);
6723: }
6724: }
1.1.1.38 root 6725:
1.1 root 6726: // registers and segments
6727: REG16(AX) = REG16(BX) = 0x00;
6728: REG16(CX) = 0xff;
6729: REG16(DX) = psp_seg;
6730: REG16(SI) = ip;
6731: REG16(DI) = sp;
6732: REG16(SP) = sp;
1.1.1.3 root 6733: SREG(DS) = SREG(ES) = psp_seg;
6734: SREG(SS) = ss;
6735: i386_load_segment_descriptor(DS);
6736: i386_load_segment_descriptor(ES);
6737: i386_load_segment_descriptor(SS);
1.1 root 6738:
6739: *(UINT16 *)(mem + (ss << 4) + sp) = 0;
6740: i386_jmp_far(cs, ip);
6741: } else if(al == 0x01) {
6742: // copy ss:sp and cs:ip to param block
6743: param->sp = sp;
6744: param->ss = ss;
6745: param->ip = ip;
6746: param->cs = cs;
1.1.1.31 root 6747:
6748: // the AX value to be passed to the child program is put on top of the child's stack
6749: *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1 root 6750: }
6751: return(0);
6752: }
6753:
6754: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
6755: {
6756: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6757:
6758: *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
6759: *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
6760: *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
6761:
1.1.1.3 root 6762: SREG(SS) = psp->stack.w.h;
6763: i386_load_segment_descriptor(SS);
1.1 root 6764: REG16(SP) = psp->stack.w.l;
6765: i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
6766:
1.1.1.28 root 6767: // process_t *current_process = msdos_process_info_get(psp_seg);
6768: process_t *current_process = NULL;
6769: for(int i = 0; i < MAX_PROCESS; i++) {
6770: if(process[i].psp == psp_seg) {
6771: current_process = &process[i];
6772: break;
6773: }
6774: }
6775: if(current_process == NULL) {
6776: throw(0x1f); // general failure
6777: }
6778: int_10h_feh_called = current_process->parent_int_10h_feh_called;
6779: int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
6780: if(current_process->called_by_int2eh) {
6781: REG16(AX) = ret;
6782: }
6783: SREG(DS) = current_process->parent_ds;
1.1.1.31 root 6784: SREG(ES) = current_process->parent_es;
1.1.1.14 root 6785: i386_load_segment_descriptor(DS);
1.1.1.31 root 6786: i386_load_segment_descriptor(ES);
1.1 root 6787:
6788: if(mem_free) {
1.1.1.8 root 6789: int mcb_seg;
6790: while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
6791: msdos_mem_free(mcb_seg + 1);
6792: }
6793: while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
6794: msdos_mem_free(mcb_seg + 1);
6795: }
1.1 root 6796:
6797: for(int i = 0; i < MAX_FILES; i++) {
6798: if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
6799: _close(i);
1.1.1.20 root 6800: msdos_file_handler_close(i);
6801: msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1 root 6802: }
6803: }
1.1.1.13 root 6804: msdos_dta_info_free(psp_seg);
1.1 root 6805: }
1.1.1.14 root 6806: msdos_stdio_reopen();
1.1 root 6807:
1.1.1.28 root 6808: memset(current_process, 0, sizeof(process_t));
1.1 root 6809:
6810: current_psp = psp->parent_psp;
6811: retval = ret;
1.1.1.23 root 6812: msdos_sda_update(current_psp);
1.1 root 6813: }
6814:
6815: // drive
6816:
1.1.1.42 root 6817: int pcbios_update_drive_param(int drive_num, int force_update);
6818:
1.1 root 6819: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
6820: {
1.1.1.41 root 6821: if(!(drive_num >= 0 && drive_num < 26)) {
6822: return(0);
6823: }
1.1.1.42 root 6824: pcbios_update_drive_param(drive_num, force_update);
6825:
6826: drive_param_t *drive_param = &drive_params[drive_num];
1.1 root 6827: *seg = DPB_TOP >> 4;
6828: *ofs = sizeof(dpb_t) * drive_num;
6829: dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
6830:
6831: memset(dpb, 0, sizeof(dpb_t));
6832:
1.1.1.41 root 6833: dpb->drive_num = drive_num;
6834: dpb->unit_num = drive_num;
1.1.1.42 root 6835:
6836: if(drive_param->valid) {
6837: DISK_GEOMETRY *geo = &drive_param->geometry;
6838:
6839: dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
6840: dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
6841: dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
6842: dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
6843: switch(geo->MediaType) {
6844: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
6845: dpb->media_type = 0xff;
6846: break;
6847: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
6848: dpb->media_type = 0xfe;
6849: break;
6850: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
6851: dpb->media_type = 0xfd;
6852: break;
6853: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
6854: dpb->media_type = 0xfc;
6855: break;
6856: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
6857: case F3_1Pt2_512:
6858: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
6859: case F5_720_512:
6860: dpb->media_type = 0xf9;
6861: break;
6862: case FixedMedia: // hard disk
6863: case RemovableMedia:
6864: case Unknown:
6865: dpb->media_type = 0xf8;
6866: break;
6867: default:
6868: dpb->media_type = 0xf0;
6869: break;
6870: }
6871: }
1.1.1.41 root 6872: dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
6873: dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42 root 6874: dpb->info_sector = 0xffff;
6875: dpb->backup_boot_sector = 0xffff;
6876: dpb->free_clusters = 0xffff;
6877: dpb->free_search_cluster = 0xffffffff;
6878:
6879: return(drive_param->valid);
1.1 root 6880: }
6881:
6882: // pc bios
6883:
1.1.1.35 root 6884: #ifdef USE_SERVICE_THREAD
6885: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
6886: {
6887: #if defined(HAS_I386)
6888: if(m_SF != 0) {
6889: m_SF = 0;
1.1.1.49 root 6890: mem[DUMMY_TOP + 0x15] = 0x79; // jns -4
1.1.1.35 root 6891: } else {
6892: m_SF = 1;
1.1.1.49 root 6893: mem[DUMMY_TOP + 0x15] = 0x78; // js -4
1.1.1.35 root 6894: }
6895: #else
6896: if(m_SignVal < 0) {
6897: m_SignVal = 0;
1.1.1.49 root 6898: mem[DUMMY_TOP + 0x15] = 0x79; // jns -4
1.1.1.35 root 6899: } else {
6900: m_SignVal = -1;
1.1.1.49 root 6901: mem[DUMMY_TOP + 0x15] = 0x78; // js -4
1.1.1.35 root 6902: }
6903: #endif
1.1.1.59 root 6904: // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49 root 6905: i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35 root 6906: in_service = true;
6907: service_exit = false;
6908: CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
6909: }
6910:
6911: void finish_service_loop()
6912: {
6913: if(in_service && service_exit) {
6914: #if defined(HAS_I386)
6915: if(m_SF != 0) {
6916: m_SF = 0;
6917: } else {
6918: m_SF = 1;
6919: }
6920: #else
6921: if(m_SignVal < 0) {
6922: m_SignVal = 0;
6923: } else {
6924: m_SignVal = -1;
6925: }
6926: #endif
6927: in_service = false;
6928: }
6929: }
6930: #endif
6931:
1.1.1.19 root 6932: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
6933: {
6934: static unsigned __int64 start_msec_since_midnight = 0;
6935: static unsigned __int64 start_msec_since_hostboot = 0;
6936:
6937: if(start_msec_since_midnight == 0) {
6938: SYSTEMTIME time;
6939: GetLocalTime(&time);
6940: start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
6941: start_msec_since_hostboot = cur_msec;
6942: }
6943: unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
6944: unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
6945: return (UINT32)tick;
6946: }
6947:
6948: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
6949: {
6950: UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
6951: UINT32 next_tick = get_ticks_since_midnight(cur_msec);
6952:
6953: if(prev_tick > next_tick) {
6954: mem[0x470] = 1;
6955: }
6956: *(UINT32 *)(mem + 0x46c) = next_tick;
6957: }
6958:
1.1.1.14 root 6959: inline void pcbios_irq0()
6960: {
6961: //++*(UINT32 *)(mem + 0x46c);
1.1.1.19 root 6962: pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14 root 6963: }
6964:
1.1.1.16 root 6965: int pcbios_get_text_vram_address(int page)
1.1 root 6966: {
6967: if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6968: return TEXT_VRAM_TOP;
1.1 root 6969: } else {
1.1.1.14 root 6970: return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1 root 6971: }
6972: }
6973:
1.1.1.16 root 6974: int pcbios_get_shadow_buffer_address(int page)
1.1 root 6975: {
1.1.1.14 root 6976: if(!int_10h_feh_called) {
1.1.1.16 root 6977: return pcbios_get_text_vram_address(page);
1.1.1.14 root 6978: } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6979: return SHADOW_BUF_TOP;
6980: } else {
1.1.1.14 root 6981: return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8 root 6982: }
6983: }
6984:
1.1.1.16 root 6985: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8 root 6986: {
1.1.1.16 root 6987: return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1 root 6988: }
6989:
1.1.1.56 root 6990: bool pcbios_set_font_size(int width, int height)
6991: {
6992: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
6993: return(set_console_font_size(hStdout, width, height));
6994: }
6995:
1.1.1.16 root 6996: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1 root 6997: {
1.1.1.14 root 6998: // clear the existing screen, not just the new one
6999: int clr_height = max(height, scr_height);
7000:
1.1.1.16 root 7001: if(scr_width != width || scr_height != height) {
7002: change_console_size(width, height);
1.1.1.14 root 7003: }
7004: mem[0x462] = 0;
7005: *(UINT16 *)(mem + 0x44e) = 0;
7006:
1.1.1.16 root 7007: text_vram_top_address = pcbios_get_text_vram_address(0);
7008: text_vram_end_address = text_vram_top_address + width * height * 2;
7009: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
7010: shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51 root 7011: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1 root 7012:
1.1.1.23 root 7013: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16 root 7014: if(clr_screen) {
1.1.1.14 root 7015: for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
7016: mem[ofs++] = 0x20;
7017: mem[ofs++] = 0x07;
7018: }
7019:
1.1.1.35 root 7020: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7021: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7022: #endif
1.1.1.14 root 7023: for(int y = 0; y < clr_height; y++) {
7024: for(int x = 0; x < scr_width; x++) {
7025: SCR_BUF(y,x).Char.AsciiChar = ' ';
7026: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 7027: }
7028: }
7029: SMALL_RECT rect;
1.1.1.14 root 7030: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60! root 7031: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 7032: vram_length_char = vram_last_length_char = 0;
7033: vram_length_attr = vram_last_length_attr = 0;
1.1.1.35 root 7034: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7035: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7036: #endif
1.1 root 7037: }
1.1.1.14 root 7038: COORD co;
7039: co.X = 0;
7040: co.Y = scr_top;
7041: SetConsoleCursorPosition(hStdout, co);
7042: cursor_moved = true;
1.1.1.59 root 7043: cursor_moved_by_crtc = false;
1.1.1.14 root 7044: SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1 root 7045: }
7046:
1.1.1.36 root 7047: void pcbios_update_cursor_position()
7048: {
7049: CONSOLE_SCREEN_BUFFER_INFO csbi;
7050: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
7051: if(!restore_console_on_exit) {
7052: scr_top = csbi.srWindow.Top;
7053: }
7054: mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
7055: mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
7056: }
7057:
1.1.1.16 root 7058: inline void pcbios_int_10h_00h()
7059: {
7060: switch(REG8(AL) & 0x7f) {
7061: case 0x70: // v-text mode
7062: case 0x71: // extended cga v-text mode
7063: pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
7064: break;
7065: default:
7066: pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
7067: break;
7068: }
7069: if(REG8(AL) & 0x80) {
7070: mem[0x487] |= 0x80;
7071: } else {
7072: mem[0x487] &= ~0x80;
7073: }
7074: mem[0x449] = REG8(AL) & 0x7f;
7075: }
7076:
1.1 root 7077: inline void pcbios_int_10h_01h()
7078: {
1.1.1.13 root 7079: mem[0x460] = REG8(CL);
7080: mem[0x461] = REG8(CH);
1.1.1.14 root 7081:
1.1.1.60! root 7082: int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
! 7083:
! 7084: if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
! 7085: ci_new.bVisible = TRUE;
! 7086: ci_new.dwSize = (size + 2) * 100 / (8 + 2);
! 7087: } else {
! 7088: ci_new.bVisible = FALSE;
! 7089: }
1.1 root 7090: }
7091:
7092: inline void pcbios_int_10h_02h()
7093: {
1.1.1.14 root 7094: // continuously setting the cursor effectively stops it blinking
1.1.1.59 root 7095: if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2] || cursor_moved_by_crtc)) {
1.1 root 7096: COORD co;
7097: co.X = REG8(DL);
1.1.1.14 root 7098: co.Y = REG8(DH) + scr_top;
7099:
7100: // some programs hide the cursor by moving it off screen
7101: static bool hidden = false;
1.1.1.23 root 7102: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7103:
7104: if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59 root 7105: if(ci_new.bVisible) {
7106: ci_new.bVisible = FALSE;
1.1.1.14 root 7107: hidden = true;
7108: }
7109: } else if(hidden) {
1.1.1.59 root 7110: if(!ci_new.bVisible) {
7111: ci_new.bVisible = TRUE;
1.1.1.14 root 7112: }
7113: hidden = false;
7114: }
1.1.1.59 root 7115: cursor_moved_by_crtc = false;
1.1 root 7116: }
1.1.1.14 root 7117: mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
7118: mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1 root 7119: }
7120:
7121: inline void pcbios_int_10h_03h()
7122: {
1.1.1.14 root 7123: REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7124: REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 7125: REG8(CL) = mem[0x460];
7126: REG8(CH) = mem[0x461];
7127: }
7128:
7129: inline void pcbios_int_10h_05h()
7130: {
1.1.1.14 root 7131: if(REG8(AL) >= vram_pages) {
7132: return;
7133: }
7134: if(mem[0x462] != REG8(AL)) {
7135: vram_flush();
7136:
1.1.1.23 root 7137: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7138: SMALL_RECT rect;
1.1.1.14 root 7139: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60! root 7140: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 7141:
1.1.1.16 root 7142: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14 root 7143: for(int x = 0; x < scr_width; x++) {
7144: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
7145: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 7146: }
7147: }
1.1.1.16 root 7148: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14 root 7149: for(int x = 0; x < scr_width; x++) {
7150: SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
7151: SCR_BUF(y,x).Attributes = mem[ofs++];
1.1 root 7152: }
7153: }
1.1.1.60! root 7154: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 7155:
7156: COORD co;
1.1.1.14 root 7157: co.X = mem[0x450 + REG8(AL) * 2];
7158: co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
7159: if(co.Y < scr_top + scr_height) {
7160: SetConsoleCursorPosition(hStdout, co);
7161: }
1.1.1.59 root 7162: cursor_moved_by_crtc = false;
1.1 root 7163: }
1.1.1.14 root 7164: mem[0x462] = REG8(AL);
7165: *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
7166: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16 root 7167: text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14 root 7168: text_vram_end_address = text_vram_top_address + regen;
1.1.1.16 root 7169: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14 root 7170: shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51 root 7171: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1 root 7172: }
7173:
7174: inline void pcbios_int_10h_06h()
7175: {
1.1.1.14 root 7176: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
7177: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
7178: return;
7179: }
7180: vram_flush();
7181:
1.1.1.23 root 7182: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7183: SMALL_RECT rect;
1.1.1.14 root 7184: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60! root 7185: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 7186:
7187: int right = min(REG8(DL), scr_width - 1);
7188: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 7189:
7190: if(REG8(AL) == 0) {
1.1.1.14 root 7191: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 7192: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 7193: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
7194: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 7195: }
7196: }
7197: } else {
1.1.1.14 root 7198: for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16 root 7199: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 7200: if(y2 <= bottom) {
7201: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 7202: } else {
1.1.1.14 root 7203: SCR_BUF(y,x).Char.AsciiChar = ' ';
7204: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 7205: }
1.1.1.14 root 7206: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
7207: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 7208: }
7209: }
7210: }
1.1.1.60! root 7211: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 7212: }
7213:
7214: inline void pcbios_int_10h_07h()
7215: {
1.1.1.14 root 7216: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
7217: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
7218: return;
7219: }
7220: vram_flush();
7221:
1.1.1.23 root 7222: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7223: SMALL_RECT rect;
1.1.1.14 root 7224: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60! root 7225: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 7226:
7227: int right = min(REG8(DL), scr_width - 1);
7228: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 7229:
7230: if(REG8(AL) == 0) {
1.1.1.14 root 7231: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 7232: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 7233: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
7234: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 7235: }
7236: }
7237: } else {
1.1.1.14 root 7238: for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16 root 7239: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 7240: if(y2 >= REG8(CH)) {
7241: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 7242: } else {
1.1.1.14 root 7243: SCR_BUF(y,x).Char.AsciiChar = ' ';
7244: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 7245: }
1.1.1.14 root 7246: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
7247: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 7248: }
7249: }
7250: }
1.1.1.60! root 7251: WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 7252: }
7253:
7254: inline void pcbios_int_10h_08h()
7255: {
7256: COORD co;
7257: DWORD num;
7258:
1.1.1.14 root 7259: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7260: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 7261:
7262: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7263: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7264: co.Y += scr_top;
7265: vram_flush();
1.1.1.60! root 7266: ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1 root 7267: ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
7268: REG8(AL) = scr_char[0];
7269: REG8(AH) = scr_attr[0];
7270: } else {
1.1.1.16 root 7271: REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1 root 7272: }
7273: }
7274:
7275: inline void pcbios_int_10h_09h()
7276: {
7277: COORD co;
7278:
1.1.1.14 root 7279: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7280: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
7281:
1.1.1.16 root 7282: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
7283: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 7284:
7285: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 7286: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7287: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7288: #endif
1.1.1.16 root 7289: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 7290: while(dest < end) {
7291: write_text_vram_char(dest - vram, REG8(AL));
7292: mem[dest++] = REG8(AL);
7293: write_text_vram_attr(dest - vram, REG8(BL));
7294: mem[dest++] = REG8(BL);
1.1 root 7295: }
1.1.1.35 root 7296: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7297: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7298: #endif
1.1 root 7299: } else {
1.1.1.14 root 7300: while(dest < end) {
1.1 root 7301: mem[dest++] = REG8(AL);
7302: mem[dest++] = REG8(BL);
7303: }
7304: }
7305: }
7306:
7307: inline void pcbios_int_10h_0ah()
7308: {
7309: COORD co;
7310:
1.1.1.14 root 7311: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7312: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
7313:
1.1.1.16 root 7314: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
7315: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 7316:
7317: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 7318: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7319: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7320: #endif
1.1.1.16 root 7321: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 7322: while(dest < end) {
7323: write_text_vram_char(dest - vram, REG8(AL));
7324: mem[dest++] = REG8(AL);
7325: dest++;
1.1 root 7326: }
1.1.1.35 root 7327: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7328: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7329: #endif
1.1 root 7330: } else {
1.1.1.14 root 7331: while(dest < end) {
1.1 root 7332: mem[dest++] = REG8(AL);
7333: dest++;
7334: }
7335: }
7336: }
7337:
1.1.1.40 root 7338: HDC get_console_window_device_context()
7339: {
7340: static HWND hwndFound = 0;
7341:
7342: if(hwndFound == 0) {
7343: // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
7344: char pszNewWindowTitle[1024];
7345: char pszOldWindowTitle[1024];
7346:
1.1.1.60! root 7347: GetConsoleTitleA(pszOldWindowTitle, 1024);
! 7348: wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
! 7349: SetConsoleTitleA(pszNewWindowTitle);
1.1.1.40 root 7350: Sleep(100);
1.1.1.60! root 7351: hwndFound = FindWindowA(NULL, pszNewWindowTitle);
! 7352: SetConsoleTitleA(pszOldWindowTitle);
1.1.1.40 root 7353: }
7354: return GetDC(hwndFound);
7355: }
7356:
7357: inline void pcbios_int_10h_0ch()
7358: {
7359: HDC hdc = get_console_window_device_context();
7360:
7361: if(hdc != NULL) {
7362: BYTE r = (REG8(AL) & 2) ? 255 : 0;
7363: BYTE g = (REG8(AL) & 4) ? 255 : 0;
7364: BYTE b = (REG8(AL) & 1) ? 255 : 0;
7365:
7366: if(REG8(AL) & 0x80) {
7367: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
7368: if(color != CLR_INVALID) {
7369: r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
7370: g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
7371: b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
7372: }
7373: }
7374: SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
7375: }
7376: }
7377:
7378: inline void pcbios_int_10h_0dh()
7379: {
7380: HDC hdc = get_console_window_device_context();
7381: BYTE r = 0;
7382: BYTE g = 0;
7383: BYTE b = 0;
7384:
7385: if(hdc != NULL) {
7386: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
7387: if(color != CLR_INVALID) {
7388: r = ((DWORD)color & 0x0000ff) ? 255 : 0;
7389: g = ((DWORD)color & 0x00ff00) ? 255 : 0;
7390: b = ((DWORD)color & 0xff0000) ? 255 : 0;
7391: }
7392: }
7393: REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
7394: }
7395:
1.1 root 7396: inline void pcbios_int_10h_0eh()
7397: {
1.1.1.59 root 7398: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
7399: CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14 root 7400: DWORD num;
7401: COORD co;
7402:
1.1.1.59 root 7403: if(cursor_moved_by_crtc) {
7404: if(!restore_console_on_exit) {
7405: GetConsoleScreenBufferInfo(hStdout, &csbi);
7406: scr_top = csbi.srWindow.Top;
7407: }
7408: co.X = mem[0x450 + REG8(BH) * 2];
7409: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
7410: SetConsoleCursorPosition(hStdout, co);
7411: cursor_moved_by_crtc = false;
7412: }
1.1.1.54 root 7413: co.X = mem[0x450 + mem[0x462] * 2];
7414: co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14 root 7415:
7416: if(REG8(AL) == 7) {
7417: //MessageBeep(-1);
7418: } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
7419: if(REG8(AL) == 10) {
7420: vram_flush();
7421: }
1.1.1.60! root 7422: WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), ®8(AL), 1, &num, NULL);
1.1.1.14 root 7423: cursor_moved = true;
7424: } else {
1.1.1.54 root 7425: int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35 root 7426: #ifdef USE_VRAM_THREAD
1.1.1.54 root 7427: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7428: #endif
1.1.1.54 root 7429: int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
7430: write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35 root 7431: #ifdef USE_VRAM_THREAD
1.1.1.54 root 7432: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7433: #endif
1.1.1.54 root 7434:
7435: if(++co.X == scr_width) {
7436: co.X = 0;
7437: if(++co.Y == scr_height) {
7438: vram_flush();
1.1.1.60! root 7439: WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14 root 7440: cursor_moved = true;
7441: }
7442: }
1.1.1.54 root 7443: if(!cursor_moved) {
7444: co.Y += scr_top;
7445: SetConsoleCursorPosition(hStdout, co);
7446: cursor_moved = true;
7447: }
1.1.1.14 root 7448: mem[dest] = REG8(AL);
7449: }
1.1 root 7450: }
7451:
7452: inline void pcbios_int_10h_0fh()
7453: {
7454: REG8(AL) = mem[0x449];
7455: REG8(AH) = mem[0x44a];
7456: REG8(BH) = mem[0x462];
7457: }
7458:
1.1.1.14 root 7459: inline void pcbios_int_10h_11h()
7460: {
7461: switch(REG8(AL)) {
1.1.1.58 root 7462: case 0x00:
7463: case 0x10:
7464: if(REG8(BH) != 0 && pcbios_set_font_size(8, REG8(BH))) {
7465: pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
7466: } else {
7467: m_CF = 1; // never failed in real PC BIOS
7468: }
7469: break;
1.1.1.16 root 7470: case 0x01:
1.1.1.14 root 7471: case 0x11:
1.1.1.58 root 7472: // if(pcbios_set_font_size(8, 14)) {
7473: if(pcbios_set_font_size(8, 12)) {
7474: pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
7475: } else {
7476: m_CF = 1; // never failed in real PC BIOS
1.1.1.56 root 7477: }
1.1.1.14 root 7478: break;
1.1.1.16 root 7479: case 0x02:
1.1.1.14 root 7480: case 0x12:
1.1.1.58 root 7481: if(pcbios_set_font_size(8, 8)) {
7482: pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
7483: } else {
7484: m_CF = 1; // never failed in real PC BIOS
1.1.1.56 root 7485: }
1.1.1.14 root 7486: break;
1.1.1.16 root 7487: case 0x04:
1.1.1.14 root 7488: case 0x14:
1.1.1.58 root 7489: // if(pcbios_set_font_size(8, 16)) {
7490: if(pcbios_set_font_size(8, 18)) {
7491: pcbios_set_console_size(80, 25, true);
7492: } else {
7493: m_CF = 1; // never failed in real PC BIOS
1.1.1.56 root 7494: }
1.1.1.58 root 7495: break;
7496: case 0x18:
7497: // if(pcbios_set_font_size(8, 16)) {
7498: if(pcbios_set_font_size(8, 18)) {
1.1.1.56 root 7499: pcbios_set_console_size(80, 50, true);
7500: } else {
1.1.1.58 root 7501: m_CF = 1; // never failed in real PC BIOS
1.1.1.56 root 7502: }
1.1.1.14 root 7503: break;
7504: case 0x30:
7505: SREG(ES) = 0;
7506: i386_load_segment_descriptor(ES);
7507: REG16(BP) = 0;
7508: REG16(CX) = mem[0x485];
7509: REG8(DL) = mem[0x484];
7510: break;
1.1.1.54 root 7511: default:
7512: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
7513: m_CF = 1;
7514: break;
1.1.1.14 root 7515: }
7516: }
7517:
7518: inline void pcbios_int_10h_12h()
7519: {
1.1.1.16 root 7520: switch(REG8(BL)) {
7521: case 0x10:
1.1.1.14 root 7522: REG16(BX) = 0x0003;
7523: REG16(CX) = 0x0009;
1.1.1.16 root 7524: break;
1.1.1.14 root 7525: }
7526: }
7527:
1.1 root 7528: inline void pcbios_int_10h_13h()
7529: {
1.1.1.3 root 7530: int ofs = SREG_BASE(ES) + REG16(BP);
1.1 root 7531: COORD co;
7532: DWORD num;
7533:
7534: co.X = REG8(DL);
1.1.1.14 root 7535: co.Y = REG8(DH) + scr_top;
7536:
7537: vram_flush();
1.1 root 7538:
7539: switch(REG8(AL)) {
7540: case 0x00:
7541: case 0x01:
7542: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7543: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7544: CONSOLE_SCREEN_BUFFER_INFO csbi;
7545: GetConsoleScreenBufferInfo(hStdout, &csbi);
7546: SetConsoleCursorPosition(hStdout, co);
7547:
7548: if(csbi.wAttributes != REG8(BL)) {
7549: SetConsoleTextAttribute(hStdout, REG8(BL));
7550: }
1.1.1.60! root 7551: WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14 root 7552:
1.1 root 7553: if(csbi.wAttributes != REG8(BL)) {
7554: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
7555: }
7556: if(REG8(AL) == 0x00) {
1.1.1.15 root 7557: if(!restore_console_on_exit) {
7558: GetConsoleScreenBufferInfo(hStdout, &csbi);
7559: scr_top = csbi.srWindow.Top;
7560: }
1.1.1.14 root 7561: co.X = mem[0x450 + REG8(BH) * 2];
7562: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 7563: SetConsoleCursorPosition(hStdout, co);
7564: } else {
7565: cursor_moved = true;
7566: }
1.1.1.59 root 7567: cursor_moved_by_crtc = false;
1.1 root 7568: } else {
1.1.1.3 root 7569: m_CF = 1;
1.1 root 7570: }
7571: break;
7572: case 0x02:
7573: case 0x03:
7574: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7575: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7576: CONSOLE_SCREEN_BUFFER_INFO csbi;
7577: GetConsoleScreenBufferInfo(hStdout, &csbi);
7578: SetConsoleCursorPosition(hStdout, co);
7579:
7580: WORD wAttributes = csbi.wAttributes;
7581: for(int i = 0; i < REG16(CX); i++, ofs += 2) {
7582: if(wAttributes != mem[ofs + 1]) {
7583: SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
7584: wAttributes = mem[ofs + 1];
7585: }
1.1.1.60! root 7586: WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1 root 7587: }
7588: if(csbi.wAttributes != wAttributes) {
7589: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
7590: }
7591: if(REG8(AL) == 0x02) {
1.1.1.14 root 7592: co.X = mem[0x450 + REG8(BH) * 2];
7593: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 7594: SetConsoleCursorPosition(hStdout, co);
7595: } else {
7596: cursor_moved = true;
7597: }
1.1.1.59 root 7598: cursor_moved_by_crtc = false;
1.1 root 7599: } else {
1.1.1.3 root 7600: m_CF = 1;
1.1 root 7601: }
7602: break;
7603: case 0x10:
7604: case 0x11:
7605: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7606: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60! root 7607: ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1 root 7608: ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
7609: for(int i = 0; i < num; i++) {
7610: mem[ofs++] = scr_char[i];
7611: mem[ofs++] = scr_attr[i];
1.1.1.45 root 7612: if(REG8(AL) & 0x01) {
1.1 root 7613: mem[ofs++] = 0;
7614: mem[ofs++] = 0;
7615: }
7616: }
7617: } else {
1.1.1.16 root 7618: for(int i = 0, src = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1 root 7619: mem[ofs++] = mem[src++];
7620: mem[ofs++] = mem[src++];
1.1.1.45 root 7621: if(REG8(AL) & 0x01) {
1.1 root 7622: mem[ofs++] = 0;
7623: mem[ofs++] = 0;
7624: }
1.1.1.14 root 7625: if(++co.X == scr_width) {
7626: if(++co.Y == scr_height) {
1.1 root 7627: break;
7628: }
7629: co.X = 0;
7630: }
7631: }
7632: }
7633: break;
1.1.1.45 root 7634: case 0x12: // ???
7635: case 0x13: // ???
1.1 root 7636: case 0x20:
7637: case 0x21:
7638: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7639: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7640: int len = min(REG16(CX), scr_width * scr_height);
7641: for(int i = 0; i < len; i++) {
1.1 root 7642: scr_char[i] = mem[ofs++];
7643: scr_attr[i] = mem[ofs++];
1.1.1.45 root 7644: if(REG8(AL) & 0x01) {
1.1 root 7645: ofs += 2;
7646: }
7647: }
1.1.1.60! root 7648: WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14 root 7649: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7650: } else {
1.1.1.16 root 7651: for(int i = 0, dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1 root 7652: mem[dest++] = mem[ofs++];
7653: mem[dest++] = mem[ofs++];
1.1.1.45 root 7654: if(REG8(AL) & 0x01) {
1.1 root 7655: ofs += 2;
7656: }
1.1.1.14 root 7657: if(++co.X == scr_width) {
7658: if(++co.Y == scr_height) {
1.1 root 7659: break;
7660: }
7661: co.X = 0;
7662: }
7663: }
7664: }
7665: break;
7666: default:
1.1.1.22 root 7667: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3 root 7668: m_CF = 1;
1.1 root 7669: break;
7670: }
7671: }
7672:
1.1.1.30 root 7673: inline void pcbios_int_10h_18h()
7674: {
7675: switch(REG8(AL)) {
7676: case 0x00:
7677: case 0x01:
7678: // REG8(AL) = 0x86;
7679: REG8(AL) = 0x00;
7680: break;
7681: default:
7682: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
7683: m_CF = 1;
7684: break;
7685: }
7686: }
7687:
1.1.1.14 root 7688: inline void pcbios_int_10h_1ah()
7689: {
7690: switch(REG8(AL)) {
7691: case 0x00:
7692: REG8(AL) = 0x1a;
7693: REG8(BL) = 0x08;
7694: REG8(BH) = 0x00;
7695: break;
7696: default:
1.1.1.22 root 7697: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14 root 7698: m_CF = 1;
7699: break;
7700: }
7701: }
7702:
1.1 root 7703: inline void pcbios_int_10h_1dh()
7704: {
7705: switch(REG8(AL)) {
1.1.1.43 root 7706: case 0x00:
7707: // DOS/V Shift Status Line Control is not supported
7708: m_CF = 1;
7709: break;
1.1 root 7710: case 0x01:
7711: break;
7712: case 0x02:
7713: REG16(BX) = 0;
7714: break;
7715: default:
1.1.1.22 root 7716: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
7717: m_CF = 1;
7718: break;
7719: }
7720: }
7721:
7722: inline void pcbios_int_10h_4fh()
7723: {
7724: switch(REG8(AL)) {
7725: case 0x00:
7726: REG8(AH) = 0x02; // not supported
7727: break;
7728: case 0x01:
7729: case 0x02:
7730: case 0x03:
7731: case 0x04:
7732: case 0x05:
7733: case 0x06:
7734: case 0x07:
7735: case 0x08:
7736: case 0x09:
7737: case 0x0a:
7738: case 0x0b:
7739: case 0x0c:
7740: REG8(AH) = 0x01; // failed
7741: break;
7742: default:
7743: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3 root 7744: m_CF = 1;
1.1 root 7745: break;
7746: }
7747: }
7748:
7749: inline void pcbios_int_10h_82h()
7750: {
7751: static UINT8 mode = 0;
7752:
7753: switch(REG8(AL)) {
1.1.1.22 root 7754: case 0x00:
1.1 root 7755: if(REG8(BL) != 0xff) {
7756: mode = REG8(BL);
7757: }
7758: REG8(AL) = mode;
7759: break;
7760: default:
1.1.1.22 root 7761: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3 root 7762: m_CF = 1;
1.1 root 7763: break;
7764: }
7765: }
7766:
1.1.1.22 root 7767: inline void pcbios_int_10h_83h()
7768: {
7769: static UINT8 mode = 0;
7770:
7771: switch(REG8(AL)) {
7772: case 0x00:
7773: REG16(AX) = 0; // offset???
7774: SREG(ES) = (SHADOW_BUF_TOP >> 4);
7775: i386_load_segment_descriptor(ES);
7776: REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
7777: break;
7778: default:
7779: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
7780: m_CF = 1;
7781: break;
7782: }
7783: }
7784:
7785: inline void pcbios_int_10h_90h()
7786: {
7787: REG8(AL) = mem[0x449];
7788: }
7789:
7790: inline void pcbios_int_10h_91h()
7791: {
7792: REG8(AL) = 0x04; // VGA
7793: }
7794:
7795: inline void pcbios_int_10h_efh()
7796: {
7797: REG16(DX) = 0xffff;
7798: }
7799:
1.1 root 7800: inline void pcbios_int_10h_feh()
7801: {
7802: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 7803: SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3 root 7804: i386_load_segment_descriptor(ES);
1.1.1.8 root 7805: REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1 root 7806: }
7807: int_10h_feh_called = true;
7808: }
7809:
7810: inline void pcbios_int_10h_ffh()
7811: {
7812: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23 root 7813: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7814: COORD co;
7815: DWORD num;
7816:
1.1.1.14 root 7817: vram_flush();
7818:
7819: co.X = (REG16(DI) >> 1) % scr_width;
7820: co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16 root 7821: int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
7822: int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14 root 7823: int len;
7824: for(len = 0; ofs < end; len++) {
7825: scr_char[len] = mem[ofs++];
7826: scr_attr[len] = mem[ofs++];
7827: }
7828: co.Y += scr_top;
1.1.1.60! root 7829: WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14 root 7830: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7831: }
7832: int_10h_ffh_called = true;
7833: }
7834:
1.1.1.42 root 7835: int pcbios_update_drive_param(int drive_num, int force_update)
7836: {
7837: if(drive_num >= 0 && drive_num < 26) {
7838: drive_param_t *drive_param = &drive_params[drive_num];
7839:
7840: if(force_update || !drive_param->initialized) {
7841: drive_param->valid = 0;
7842: char dev[64];
7843: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7844:
1.1.1.60! root 7845: HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42 root 7846: if(hFile != INVALID_HANDLE_VALUE) {
7847: DWORD dwSize;
7848: if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
7849: drive_param->valid = 1;
7850: }
7851: CloseHandle(hFile);
7852: }
7853: drive_param->initialized = 1;
7854: }
7855: return(drive_param->valid);
7856: }
7857: return(0);
7858: }
7859:
7860: inline void pcbios_int_13h_00h()
7861: {
7862: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7863:
7864: if(pcbios_update_drive_param(drive_num, 1)) {
7865: REG8(AH) = 0x00; // successful completion
7866: } else {
7867: if(REG8(DL) & 0x80) {
7868: REG8(AH) = 0x05; // reset failed (hard disk)
7869: } else {
7870: REG8(AH) = 0x80; // timeout (not ready)
7871: }
7872: m_CF = 1;
7873: }
7874: }
7875:
7876: inline void pcbios_int_13h_02h()
7877: {
7878: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7879:
7880: if(REG8(AL) == 0) {
7881: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7882: m_CF = 1;
7883: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7884: REG8(AH) = 0xff; // sense operation failed (hard disk)
7885: m_CF = 1;
7886: } else {
7887: drive_param_t *drive_param = &drive_params[drive_num];
7888: DISK_GEOMETRY *geo = &drive_param->geometry;
7889: char dev[64];
7890: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7891:
1.1.1.60! root 7892: HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42 root 7893: if(hFile == INVALID_HANDLE_VALUE) {
7894: REG8(AH) = 0xff; // sense operation failed (hard disk)
7895: m_CF = 1;
7896: } else {
7897: UINT32 sector_num = REG8(AL);
7898: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7899: UINT32 head = REG8(DH);
7900: UINT32 sector = REG8(CL) & 0x3f;
7901: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
7902: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
7903: DWORD dwSize;
7904:
7905: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
7906: // REG8(AH) = 0xff; // sense operation failed (hard disk)
7907: // m_CF = 1;
7908: // } else
7909: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
7910: REG8(AH) = 0x04; // sector not found/read error
7911: m_CF = 1;
7912: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
7913: REG8(AH) = 0x04; // sector not found/read error
7914: m_CF = 1;
7915: } else {
7916: REG8(AH) = 0x00; // successful completion
7917: }
7918: CloseHandle(hFile);
7919: }
7920: }
7921: }
7922:
7923: inline void pcbios_int_13h_03h()
7924: {
7925: // this operation may cause serious damage for drives, so support only floppy disk...
7926: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7927:
7928: if(REG8(AL) == 0) {
7929: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7930: m_CF = 1;
7931: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7932: REG8(AH) = 0xff; // sense operation failed (hard disk)
7933: m_CF = 1;
7934: } else if(!drive_params[drive_num].is_fdd()) {
7935: REG8(AH) = 0xff; // sense operation failed (hard disk)
7936: m_CF = 1;
7937: } else {
7938: drive_param_t *drive_param = &drive_params[drive_num];
7939: DISK_GEOMETRY *geo = &drive_param->geometry;
7940: char dev[64];
7941: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7942:
1.1.1.60! root 7943: HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42 root 7944: if(hFile == INVALID_HANDLE_VALUE) {
7945: REG8(AH) = 0xff; // sense operation failed (hard disk)
7946: m_CF = 1;
7947: } else {
7948: UINT32 sector_num = REG8(AL);
7949: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7950: UINT32 head = REG8(DH);
7951: UINT32 sector = REG8(CL) & 0x3f;
7952: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
7953: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
7954: DWORD dwSize;
7955:
7956: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
7957: // REG8(AH) = 0xff; // sense operation failed (hard disk)
7958: // m_CF = 1;
7959: // } else
7960: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
7961: REG8(AH) = 0x04; // sector not found/read error
7962: m_CF = 1;
7963: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
7964: REG8(AH) = 0x04; // sector not found/read error
7965: m_CF = 1;
7966: } else {
7967: REG8(AH) = 0x00; // successful completion
7968: }
7969: CloseHandle(hFile);
7970: }
7971: }
7972: }
7973:
7974: inline void pcbios_int_13h_04h()
7975: {
7976: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7977:
7978: if(REG8(AL) == 0) {
7979: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7980: m_CF = 1;
7981: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7982: REG8(AH) = 0xff; // sense operation failed (hard disk)
7983: m_CF = 1;
7984: } else {
7985: drive_param_t *drive_param = &drive_params[drive_num];
7986: DISK_GEOMETRY *geo = &drive_param->geometry;
7987: char dev[64];
7988: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7989:
1.1.1.60! root 7990: HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42 root 7991: if(hFile == INVALID_HANDLE_VALUE) {
7992: REG8(AH) = 0xff; // sense operation failed (hard disk)
7993: m_CF = 1;
7994: } else {
7995: UINT32 sector_num = REG8(AL);
7996: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7997: UINT32 head = REG8(DH);
7998: UINT32 sector = REG8(CL) & 0x3f;
7999: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
8000: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
8001: DWORD dwSize;
8002: UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
8003:
8004: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
8005: // REG8(AH) = 0xff; // sense operation failed (hard disk)
8006: // m_CF = 1;
8007: // } else
8008: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
8009: REG8(AH) = 0x04; // sector not found/read error
8010: m_CF = 1;
8011: } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
8012: REG8(AH) = 0x04; // sector not found/read error
8013: m_CF = 1;
8014: } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
8015: REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
8016: m_CF = 1;
8017: } else {
8018: REG8(AH) = 0x00; // successful completion
8019: }
8020: free(tmp_buffer);
8021: CloseHandle(hFile);
8022: }
8023: }
8024: }
8025:
8026: inline void pcbios_int_13h_08h()
8027: {
8028: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
8029:
8030: if(pcbios_update_drive_param(drive_num, 1)) {
8031: drive_param_t *drive_param = &drive_params[drive_num];
8032: DISK_GEOMETRY *geo = &drive_param->geometry;
8033:
8034: REG16(AX) = 0x0000;
8035: switch(geo->MediaType) {
8036: case F5_360_512:
8037: case F5_320_512:
8038: case F5_320_1024:
8039: case F5_180_512:
8040: case F5_160_512:
8041: REG8(BL) = 0x01; // 320K/360K disk
8042: break;
8043: case F5_1Pt2_512:
8044: case F3_1Pt2_512:
8045: case F3_1Pt23_1024:
8046: case F5_1Pt23_1024:
8047: REG8(BL) = 0x02; // 1.2M disk
8048: break;
8049: case F3_720_512:
8050: case F3_640_512:
8051: case F5_640_512:
8052: case F5_720_512:
8053: REG8(BL) = 0x03; // 720K disk
8054: break;
8055: case F3_1Pt44_512:
8056: REG8(BL) = 0x04; // 1.44M disk
8057: break;
8058: case F3_2Pt88_512:
8059: REG8(BL) = 0x06; // 2.88M disk
8060: break;
8061: case RemovableMedia:
8062: REG8(BL) = 0x10; // ATAPI Removable Media Device
8063: break;
8064: default:
8065: REG8(BL) = 0x00; // unknown
8066: break;
8067: }
8068: if(REG8(DL) & 0x80) {
8069: switch(GetLogicalDrives() & 0x0c) {
8070: case 0x00: REG8(DL) = 0x00; break;
8071: case 0x04:
8072: case 0x08: REG8(DL) = 0x01; break;
8073: case 0x0c: REG8(DL) = 0x02; break;
8074: }
8075: } else {
8076: switch(GetLogicalDrives() & 0x03) {
8077: case 0x00: REG8(DL) = 0x00; break;
8078: case 0x01:
8079: case 0x02: REG8(DL) = 0x01; break;
8080: case 0x03: REG8(DL) = 0x02; break;
8081: }
8082: }
8083: REG8(DH) = drive_param->head_num();
8084: int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
8085: int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
8086: REG8(CH) = cyl & 0xff;
8087: REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
8088: } else {
8089: REG8(AH) = 0x07;
8090: m_CF = 1;
8091: }
8092: }
8093:
8094: inline void pcbios_int_13h_10h()
8095: {
8096: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
8097:
8098: if(pcbios_update_drive_param(drive_num, 1)) {
8099: REG8(AH) = 0x00; // successful completion
8100: } else {
8101: if(REG8(DL) & 0x80) {
8102: REG8(AH) = 0xaa; // drive not ready (hard disk)
8103: } else {
8104: REG8(AH) = 0x80; // timeout (not ready)
8105: }
8106: m_CF = 1;
8107: }
8108: }
8109:
8110: inline void pcbios_int_13h_15h()
8111: {
8112: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
8113:
8114: if(pcbios_update_drive_param(drive_num, 1)) {
8115: if(REG8(DL) & 0x80) {
8116: REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
8117: } else {
8118: REG8(AH) = 0x03; // hard disk
8119: }
8120: } else {
8121: REG8(AH) = 0x00; // no such drive
8122: }
8123: }
8124:
1.1.1.43 root 8125: inline void pcbios_int_13h_41h()
8126: {
8127: if(REG16(BX) == 0x55aa) {
8128: // IBM/MS INT 13 Extensions is not installed
8129: REG8(AH) = 0x01;
8130: m_CF = 1;
8131: } else {
8132: unimplemented_13h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x13, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8133: REG8(AH) = 0x01;
8134: m_CF = 1;
8135: }
8136: }
8137:
1.1.1.25 root 8138: inline void pcbios_int_14h_00h()
8139: {
1.1.1.29 root 8140: if(REG16(DX) < 4) {
1.1.1.25 root 8141: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
8142: UINT8 selector = sio_read(REG16(DX), 3);
8143: selector &= ~0x3f;
8144: selector |= REG8(AL) & 0x1f;
8145: UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
8146: sio_write(REG16(DX), 3, selector | 0x80);
8147: sio_write(REG16(DX), 0, divisor & 0xff);
8148: sio_write(REG16(DX), 1, divisor >> 8);
8149: sio_write(REG16(DX), 3, selector);
8150: REG8(AH) = sio_read(REG16(DX), 5);
8151: REG8(AL) = sio_read(REG16(DX), 6);
8152: } else {
8153: REG8(AH) = 0x80;
8154: }
8155: }
8156:
8157: inline void pcbios_int_14h_01h()
8158: {
1.1.1.29 root 8159: if(REG16(DX) < 4) {
1.1.1.25 root 8160: UINT8 selector = sio_read(REG16(DX), 3);
8161: sio_write(REG16(DX), 3, selector & ~0x80);
8162: sio_write(REG16(DX), 0, REG8(AL));
8163: sio_write(REG16(DX), 3, selector);
8164: REG8(AH) = sio_read(REG16(DX), 5);
8165: } else {
8166: REG8(AH) = 0x80;
8167: }
8168: }
8169:
8170: inline void pcbios_int_14h_02h()
8171: {
1.1.1.29 root 8172: if(REG16(DX) < 4) {
1.1.1.25 root 8173: UINT8 selector = sio_read(REG16(DX), 3);
8174: sio_write(REG16(DX), 3, selector & ~0x80);
8175: REG8(AL) = sio_read(REG16(DX), 0);
8176: sio_write(REG16(DX), 3, selector);
8177: REG8(AH) = sio_read(REG16(DX), 5);
8178: } else {
8179: REG8(AH) = 0x80;
8180: }
8181: }
8182:
8183: inline void pcbios_int_14h_03h()
8184: {
1.1.1.29 root 8185: if(REG16(DX) < 4) {
1.1.1.25 root 8186: REG8(AH) = sio_read(REG16(DX), 5);
8187: REG8(AL) = sio_read(REG16(DX), 6);
8188: } else {
8189: REG8(AH) = 0x80;
8190: }
8191: }
8192:
8193: inline void pcbios_int_14h_04h()
8194: {
1.1.1.29 root 8195: if(REG16(DX) < 4) {
1.1.1.25 root 8196: UINT8 selector = sio_read(REG16(DX), 3);
8197: if(REG8(CH) <= 0x03) {
8198: selector = (selector & ~0x03) | REG8(CH);
8199: }
8200: if(REG8(BL) == 0x00) {
8201: selector &= ~0x04;
8202: } else if(REG8(BL) == 0x01) {
8203: selector |= 0x04;
8204: }
8205: if(REG8(BH) == 0x00) {
8206: selector = (selector & ~0x38) | 0x00;
8207: } else if(REG8(BH) == 0x01) {
8208: selector = (selector & ~0x38) | 0x08;
8209: } else if(REG8(BH) == 0x02) {
8210: selector = (selector & ~0x38) | 0x18;
8211: } else if(REG8(BH) == 0x03) {
8212: selector = (selector & ~0x38) | 0x28;
8213: } else if(REG8(BH) == 0x04) {
8214: selector = (selector & ~0x38) | 0x38;
8215: }
8216: if(REG8(AL) == 0x00) {
8217: selector |= 0x40;
8218: } else if(REG8(AL) == 0x01) {
8219: selector &= ~0x40;
8220: }
8221: if(REG8(CL) <= 0x0b) {
8222: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
8223: UINT16 divisor = 115200 / rate[REG8(CL)];
8224: sio_write(REG16(DX), 3, selector | 0x80);
8225: sio_write(REG16(DX), 0, divisor & 0xff);
8226: sio_write(REG16(DX), 1, divisor >> 8);
8227: }
8228: sio_write(REG16(DX), 3, selector);
8229: REG8(AH) = sio_read(REG16(DX), 5);
8230: REG8(AL) = sio_read(REG16(DX), 6);
8231: } else {
8232: REG8(AH) = 0x80;
8233: }
8234: }
8235:
8236: inline void pcbios_int_14h_05h()
8237: {
1.1.1.29 root 8238: if(REG16(DX) < 4) {
1.1.1.25 root 8239: if(REG8(AL) == 0x00) {
8240: REG8(BL) = sio_read(REG16(DX), 4);
8241: REG8(AH) = sio_read(REG16(DX), 5);
8242: REG8(AL) = sio_read(REG16(DX), 6);
8243: } else if(REG8(AL) == 0x01) {
8244: sio_write(REG16(DX), 4, REG8(BL));
8245: REG8(AH) = sio_read(REG16(DX), 5);
8246: REG8(AL) = sio_read(REG16(DX), 6);
8247: } else {
8248: unimplemented_14h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x14, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8249: }
8250: } else {
8251: REG8(AH) = 0x80;
8252: }
8253: }
8254:
1.1.1.14 root 8255: inline void pcbios_int_15h_10h()
8256: {
1.1.1.22 root 8257: switch(REG8(AL)) {
8258: case 0x00:
1.1.1.14 root 8259: Sleep(10);
1.1.1.35 root 8260: REQUEST_HARDWRE_UPDATE();
1.1.1.22 root 8261: break;
8262: default:
8263: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14 root 8264: REG8(AH) = 0x86;
8265: m_CF = 1;
8266: }
8267: }
8268:
1.1 root 8269: inline void pcbios_int_15h_23h()
8270: {
8271: switch(REG8(AL)) {
1.1.1.22 root 8272: case 0x00:
1.1.1.8 root 8273: REG8(CL) = cmos_read(0x2d);
8274: REG8(CH) = cmos_read(0x2e);
1.1 root 8275: break;
1.1.1.22 root 8276: case 0x01:
1.1.1.8 root 8277: cmos_write(0x2d, REG8(CL));
8278: cmos_write(0x2e, REG8(CH));
1.1 root 8279: break;
8280: default:
1.1.1.22 root 8281: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 8282: REG8(AH) = 0x86;
1.1.1.3 root 8283: m_CF = 1;
1.1 root 8284: break;
8285: }
8286: }
8287:
8288: inline void pcbios_int_15h_24h()
8289: {
8290: switch(REG8(AL)) {
1.1.1.22 root 8291: case 0x00:
1.1.1.3 root 8292: i386_set_a20_line(0);
1.1 root 8293: REG8(AH) = 0;
8294: break;
1.1.1.22 root 8295: case 0x01:
1.1.1.3 root 8296: i386_set_a20_line(1);
1.1 root 8297: REG8(AH) = 0;
8298: break;
1.1.1.22 root 8299: case 0x02:
1.1 root 8300: REG8(AH) = 0;
1.1.1.3 root 8301: REG8(AL) = (m_a20_mask >> 20) & 1;
1.1 root 8302: REG16(CX) = 0;
8303: break;
1.1.1.22 root 8304: case 0x03:
1.1 root 8305: REG16(AX) = 0;
8306: REG16(BX) = 0;
8307: break;
1.1.1.22 root 8308: default:
8309: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8310: REG8(AH) = 0x86;
8311: m_CF = 1;
8312: break;
1.1 root 8313: }
8314: }
8315:
8316: inline void pcbios_int_15h_49h()
8317: {
1.1.1.27 root 8318: REG8(AH) = 0x00;
8319: REG8(BL) = 0x00; // DOS/V
1.1 root 8320: }
8321:
1.1.1.22 root 8322: inline void pcbios_int_15h_50h()
8323: {
8324: switch(REG8(AL)) {
8325: case 0x00:
8326: case 0x01:
8327: if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
8328: REG8(AH) = 0x01; // invalid font type in bh
8329: m_CF = 1;
1.1.1.27 root 8330: } else if(REG8(BL) != 0x00) {
1.1.1.22 root 8331: REG8(AH) = 0x02; // bl not zero
8332: m_CF = 1;
8333: } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
8334: REG8(AH) = 0x04; // invalid code page
8335: m_CF = 1;
1.1.1.27 root 8336: } else if(REG8(AL) == 0x01) {
8337: REG8(AH) = 0x06; // font is read only
1.1.1.22 root 8338: m_CF = 1;
1.1.1.27 root 8339: } else {
1.1.1.49 root 8340: // dummy font read routine is at fffc:000d
8341: SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27 root 8342: i386_load_segment_descriptor(ES);
1.1.1.32 root 8343: REG16(BX) = 0x000d;
1.1.1.27 root 8344: REG8(AH) = 0x00; // success
1.1.1.22 root 8345: }
8346: break;
8347: default:
8348: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8349: REG8(AH) = 0x86;
8350: m_CF = 1;
8351: break;
8352: }
8353: }
8354:
1.1.1.30 root 8355: inline void pcbios_int_15h_53h()
8356: {
8357: switch(REG8(AL)) {
8358: case 0x00:
8359: // APM is not installed
8360: REG8(AH) = 0x86;
8361: m_CF = 1;
8362: break;
8363: default:
8364: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8365: REG8(AH) = 0x86;
8366: m_CF = 1;
8367: break;
8368: }
8369: }
8370:
1.1.1.43 root 8371: inline void pcbios_int_15h_84h()
8372: {
8373: // joystick support (from DOSBox)
8374: switch(REG16(DX)) {
8375: case 0x00:
8376: REG16(AX) = 0x00f0;
8377: REG16(DX) = 0x0201;
8378: m_CF = 1;
8379: break;
8380: case 0x01:
8381: REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
8382: m_CF = 1;
8383: break;
8384: default:
8385: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8386: REG8(AH) = 0x86;
8387: m_CF = 1;
8388: break;
8389: }
8390: }
1.1.1.35 root 8391:
8392: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1 root 8393: {
8394: UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14 root 8395: UINT32 msec = usec / 1000;
8396:
1.1.1.54 root 8397: while(msec && !m_exit) {
1.1.1.14 root 8398: UINT32 tmp = min(msec, 100);
8399: if(msec - tmp < 10) {
8400: tmp = msec;
8401: }
8402: Sleep(tmp);
8403: msec -= tmp;
8404: }
1.1.1.35 root 8405:
8406: #ifdef USE_SERVICE_THREAD
8407: service_exit = true;
8408: #endif
8409: return(0);
8410: }
8411:
8412: inline void pcbios_int_15h_86h()
8413: {
8414: if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
8415: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 8416: if(!in_service && !in_service_29h) {
8417: start_service_loop(pcbios_int_15h_86h_thread);
8418: } else {
8419: #endif
8420: pcbios_int_15h_86h_thread(NULL);
8421: REQUEST_HARDWRE_UPDATE();
8422: #ifdef USE_SERVICE_THREAD
8423: }
1.1.1.35 root 8424: #endif
8425: }
1.1 root 8426: }
8427:
8428: inline void pcbios_int_15h_87h()
8429: {
8430: // copy extended memory (from DOSBox)
8431: int len = REG16(CX) * 2;
1.1.1.3 root 8432: int ofs = SREG_BASE(ES) + REG16(SI);
1.1 root 8433: int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
8434: int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
8435: memcpy(mem + dst, mem + src, len);
8436: REG16(AX) = 0x00;
8437: }
8438:
8439: inline void pcbios_int_15h_88h()
8440: {
1.1.1.17 root 8441: REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1 root 8442: }
8443:
8444: inline void pcbios_int_15h_89h()
8445: {
1.1.1.21 root 8446: #if defined(HAS_I286) || defined(HAS_I386)
1.1 root 8447: // switch to protected mode (from DOSBox)
8448: write_io_byte(0x20, 0x10);
8449: write_io_byte(0x21, REG8(BH));
8450: write_io_byte(0x21, 0x00);
8451: write_io_byte(0xa0, 0x10);
8452: write_io_byte(0xa1, REG8(BL));
8453: write_io_byte(0xa1, 0x00);
1.1.1.3 root 8454: i386_set_a20_line(1);
8455: int ofs = SREG_BASE(ES) + REG16(SI);
8456: m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
8457: m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
8458: m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
8459: m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
8460: #if defined(HAS_I386)
8461: m_cr[0] |= 1;
8462: #else
8463: m_msw |= 1;
8464: #endif
8465: SREG(DS) = 0x18;
8466: SREG(ES) = 0x20;
8467: SREG(SS) = 0x28;
8468: i386_load_segment_descriptor(DS);
8469: i386_load_segment_descriptor(ES);
8470: i386_load_segment_descriptor(SS);
1.1.1.21 root 8471: UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1 root 8472: REG16(SP) += 6;
1.1.1.3 root 8473: #if defined(HAS_I386)
1.1.1.21 root 8474: UINT32 flags = get_flags();
8475: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
8476: set_flags(flags);
1.1.1.3 root 8477: #else
1.1.1.21 root 8478: UINT32 flags = CompressFlags();
8479: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
8480: ExpandFlags(flags);
1.1.1.3 root 8481: #endif
1.1 root 8482: REG16(AX) = 0x00;
1.1.1.21 root 8483: i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1 root 8484: #else
1.1.1.21 root 8485: // i86/i186/v30: protected mode is not supported
1.1 root 8486: REG8(AH) = 0x86;
1.1.1.3 root 8487: m_CF = 1;
1.1 root 8488: #endif
8489: }
8490:
1.1.1.21 root 8491: inline void pcbios_int_15h_8ah()
8492: {
8493: UINT32 size = MAX_MEM - 0x100000;
8494: REG16(AX) = size & 0xffff;
8495: REG16(DX) = size >> 16;
8496: }
8497:
1.1.1.54 root 8498: #ifdef EXT_BIOS_TOP
8499: inline void pcbios_int_15h_c1h()
8500: {
8501: SREG(ES) = EXT_BIOS_TOP >> 4;
8502: i386_load_segment_descriptor(ES);
8503: }
8504: #endif
8505:
8506: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
8507: {
8508: // from DOSBox DoPS2Callback()
8509: UINT16 mdat = 0x08;
8510: INT16 xdiff = mouse.position.x - mouse.prev_position.x;
8511: INT16 ydiff = mouse.prev_position.y - mouse.position.y;
8512:
1.1.1.59 root 8513: #if 1
8514: if(xdiff > +16) xdiff = +16;
8515: if(xdiff < -16) xdiff = -16;
8516: if(ydiff > +16) ydiff = +16;
8517: if(ydiff < -16) ydiff = -16;
8518: #endif
8519:
1.1.1.54 root 8520: if(mouse.buttons[0].status) {
8521: mdat |= 0x01;
8522: }
8523: if(mouse.buttons[1].status) {
8524: mdat |= 0x02;
8525: }
8526: mouse.prev_position.x = mouse.position.x;
8527: mouse.prev_position.y = mouse.position.y;
1.1.1.59 root 8528:
1.1.1.54 root 8529: if((xdiff > 0xff) || (xdiff < -0xff)) {
8530: mdat |= 0x40; // x overflow
8531: }
8532: if((ydiff > 0xff) || (ydiff < -0xff)) {
8533: mdat |= 0x80; // y overflow
8534: }
8535: xdiff %= 256;
8536: ydiff %= 256;
8537: if(xdiff < 0) {
8538: xdiff = (0x100 + xdiff);
8539: mdat |= 0x10;
8540: }
8541: if(ydiff < 0) {
8542: ydiff = (0x100 + ydiff);
8543: mdat |= 0x20;
8544: }
8545: *data_1st = (UINT16)mdat;
8546: *data_2nd = (UINT16)(xdiff % 256);
8547: *data_3rd = (UINT16)(ydiff % 256);
8548: }
8549:
8550: inline void pcbios_int_15h_c2h()
8551: {
8552: static UINT8 sampling_rate = 5;
8553: static UINT8 resolution = 2;
8554: static UINT8 scaling = 1;
8555:
8556: switch(REG8(AL)) {
8557: case 0x00:
1.1.1.59 root 8558: if(REG8(BH) == 0x00) {
8559: // SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
8560: pic[1].imr |= 0x10; // disable irq12
8561: mouse.enabled_ps2 = false;
8562: REG8(AH) = 0x00; // successful
8563: } else if(REG8(BH) == 0x01) {
8564: if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
8565: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
8566: }
8567: pic[1].imr &= ~0x10; // enable irq12
8568: mouse.enabled_ps2 = true;
1.1.1.54 root 8569: REG8(AH) = 0x00; // successful
8570: } else {
8571: REG8(AH) = 0x01; // invalid function
8572: m_CF = 1;
8573: }
8574: break;
8575: case 0x01:
8576: REG8(BH) = 0x00; // device id
8577: REG8(BL) = 0xaa; // mouse
8578: case 0x05:
1.1.1.59 root 8579: // SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
8580: pic[1].imr |= 0x10; // disable irq12
8581: mouse.enabled_ps2 = false;
1.1.1.54 root 8582: sampling_rate = 5;
8583: resolution = 2;
8584: scaling = 1;
8585: REG8(AH) = 0x00; // successful
8586: break;
8587: case 0x02:
8588: sampling_rate = REG8(BH);
8589: REG8(AH) = 0x00; // successful
8590: break;
8591: case 0x03:
8592: resolution = REG8(BH);
8593: REG8(AH) = 0x00; // successful
8594: break;
8595: case 0x04:
8596: REG8(BH) = 0x00; // device id
8597: REG8(AH) = 0x00; // successful
8598: break;
8599: case 0x06:
8600: switch(REG8(BH)) {
8601: case 0x00:
8602: REG8(BL) = 0x00;
8603: if(mouse.buttons[1].status) {
8604: REG8(BL) |= 0x01;
8605: }
8606: if(mouse.buttons[0].status) {
8607: REG8(BL) |= 0x04;
8608: }
8609: if(scaling == 2) {
8610: REG8(BL) |= 0x10;
8611: }
8612: REG8(CL) = resolution;
8613: switch(sampling_rate) {
8614: case 0: REG8(DL) = 10; break;
8615: case 1: REG8(DL) = 20; break;
8616: case 2: REG8(DL) = 40; break;
8617: case 3: REG8(DL) = 60; break;
8618: case 4: REG8(DL) = 80; break;
8619: // case 5: REG8(DL) = 100; break;
8620: case 6: REG8(DL) = 200; break;
8621: default: REG8(DL) = 100; break;
8622: }
8623: REG8(AH) = 0x00; // successful
8624: break;
8625: case 0x01:
8626: case 0x02:
8627: scaling = REG8(BH);
8628: REG8(AH) = 0x00; // successful
8629: break;
8630: default:
8631: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8632: REG8(AH) = 0x01; // invalid function
8633: m_CF = 1;
8634: break;
8635: }
8636: break;
8637: case 0x07: // set device handler addr
8638: mouse.call_addr_ps2.w.l = REG16(BX);
8639: mouse.call_addr_ps2.w.h = SREG(ES);
8640: REG8(AH) = 0x00; // successful
8641: break;
8642: case 0x08:
8643: REG8(AH) = 0x00; // successful
8644: break;
8645: case 0x09:
8646: {
8647: UINT16 data_1st, data_2nd, data_3rd;
8648: pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
8649: REG8(BL) = (UINT8)(data_1st & 0xff);
8650: REG8(CL) = (UINT8)(data_2nd & 0xff);
8651: REG8(DL) = (UINT8)(data_3rd & 0xff);
8652: }
8653: REG8(AH) = 0x00; // successful
8654: break;
8655: default:
8656: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8657: // REG8(AH) = 0x86;
8658: REG8(AH) = 0x01; // invalid function
8659: m_CF = 1;
8660: break;
8661: }
8662: }
8663:
1.1.1.3 root 8664: #if defined(HAS_I386)
1.1 root 8665: inline void pcbios_int_15h_c9h()
8666: {
8667: REG8(AH) = 0x00;
8668: REG8(CH) = cpu_type;
8669: REG8(CL) = cpu_step;
8670: }
1.1.1.3 root 8671: #endif
1.1 root 8672:
8673: inline void pcbios_int_15h_cah()
8674: {
8675: switch(REG8(AL)) {
1.1.1.22 root 8676: case 0x00:
1.1 root 8677: if(REG8(BL) > 0x3f) {
8678: REG8(AH) = 0x03;
1.1.1.3 root 8679: m_CF = 1;
1.1 root 8680: } else if(REG8(BL) < 0x0e) {
8681: REG8(AH) = 0x04;
1.1.1.3 root 8682: m_CF = 1;
1.1 root 8683: } else {
1.1.1.8 root 8684: REG8(CL) = cmos_read(REG8(BL));
1.1 root 8685: }
8686: break;
1.1.1.22 root 8687: case 0x01:
1.1 root 8688: if(REG8(BL) > 0x3f) {
8689: REG8(AH) = 0x03;
1.1.1.3 root 8690: m_CF = 1;
1.1 root 8691: } else if(REG8(BL) < 0x0e) {
8692: REG8(AH) = 0x04;
1.1.1.3 root 8693: m_CF = 1;
1.1 root 8694: } else {
1.1.1.8 root 8695: cmos_write(REG8(BL), REG8(CL));
1.1 root 8696: }
8697: break;
8698: default:
1.1.1.22 root 8699: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 8700: REG8(AH) = 0x86;
1.1.1.3 root 8701: m_CF = 1;
1.1 root 8702: break;
8703: }
8704: }
8705:
1.1.1.22 root 8706: inline void pcbios_int_15h_e8h()
1.1.1.17 root 8707: {
1.1.1.22 root 8708: switch(REG8(AL)) {
8709: #if defined(HAS_I386)
8710: case 0x01:
8711: REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
8712: REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
8713: break;
1.1.1.17 root 8714: #endif
1.1.1.22 root 8715: default:
8716: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
8717: REG8(AH) = 0x86;
8718: m_CF = 1;
8719: break;
8720: }
8721: }
1.1.1.17 root 8722:
1.1.1.55 root 8723: bool pcbios_is_key_buffer_empty()
8724: {
8725: return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
8726: }
8727:
1.1.1.51 root 8728: void pcbios_clear_key_buffer()
8729: {
8730: key_buf_char->clear();
8731: key_buf_scan->clear();
8732:
8733: // update key buffer
8734: *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
8735: }
8736:
8737: void pcbios_set_key_buffer(int key_char, int key_scan)
8738: {
8739: // update key buffer
8740: UINT16 head = *(UINT16 *)(mem + 0x41a);
8741: UINT16 tail = *(UINT16 *)(mem + 0x41c);
8742: UINT16 next = tail + 2;
8743: if(next >= *(UINT16 *)(mem + 0x482)) {
8744: next = *(UINT16 *)(mem + 0x480);
8745: }
8746: if(next != head) {
8747: *(UINT16 *)(mem + 0x41c) = next;
8748: mem[0x400 + (tail++)] = key_char;
8749: mem[0x400 + (tail++)] = key_scan;
1.1.1.55 root 8750: } else {
8751: // store to extra key buffer
8752: if(key_buf_char != NULL && key_buf_scan != NULL) {
8753: key_buf_char->write(key_char);
8754: key_buf_scan->write(key_scan);
8755: }
1.1.1.51 root 8756: }
8757: }
8758:
8759: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
8760: {
8761: // update key buffer
8762: UINT16 head = *(UINT16 *)(mem + 0x41a);
8763: UINT16 tail = *(UINT16 *)(mem + 0x41c);
8764: UINT16 next = head + 2;
8765: if(next >= *(UINT16 *)(mem + 0x482)) {
8766: next = *(UINT16 *)(mem + 0x480);
8767: }
8768: if(head != tail) {
8769: *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55 root 8770: *key_char = mem[0x400 + (head++)];
8771: *key_scan = mem[0x400 + (head++)];
8772:
8773: // restore from extra key buffer
8774: if(key_buf_char != NULL && key_buf_scan != NULL) {
8775: if(!key_buf_char->empty()) {
8776: pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
8777: }
8778: }
8779: return(true);
8780: } else {
8781: *key_char = 0x00;
8782: *key_scan = 0x00;
8783: return(false);
1.1.1.51 root 8784: }
8785: }
8786:
1.1.1.60! root 8787: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
! 8788: {
! 8789: // do not remove from key buffer
! 8790: UINT16 head = *(UINT16 *)(mem + 0x41a);
! 8791: UINT16 tail = *(UINT16 *)(mem + 0x41c);
! 8792: if(head != tail) {
! 8793: *key_char = mem[0x400 + (head++)];
! 8794: *key_scan = mem[0x400 + (head++)];
! 8795: return(true);
! 8796: } else {
! 8797: *key_char = 0x00;
! 8798: *key_scan = 0x00;
! 8799: return(false);
! 8800: }
! 8801: }
! 8802:
1.1.1.33 root 8803: void pcbios_update_key_code(bool wait)
1.1 root 8804: {
1.1.1.32 root 8805: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8806: #ifdef USE_SERVICE_THREAD
8807: EnterCriticalSection(&key_buf_crit_sect);
8808: #endif
1.1.1.55 root 8809: bool empty = pcbios_is_key_buffer_empty();
1.1.1.35 root 8810: #ifdef USE_SERVICE_THREAD
8811: LeaveCriticalSection(&key_buf_crit_sect);
8812: #endif
8813: if(empty) {
1.1.1.32 root 8814: if(!update_key_buffer()) {
1.1.1.33 root 8815: if(wait) {
1.1.1.32 root 8816: Sleep(10);
8817: } else {
8818: maybe_idle();
8819: }
1.1.1.14 root 8820: }
8821: }
1.1.1.34 root 8822: }
8823: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8824: #ifdef USE_SERVICE_THREAD
8825: EnterCriticalSection(&key_buf_crit_sect);
8826: #endif
1.1.1.51 root 8827: int key_char, key_scan;
8828: if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41 root 8829: key_code = key_char << 0;
8830: key_code |= key_scan << 8;
1.1.1.35 root 8831: key_recv = 0x0000ffff;
1.1.1.51 root 8832: }
8833: if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41 root 8834: key_code |= key_char << 16;
8835: key_code |= key_scan << 24;
1.1.1.33 root 8836: key_recv |= 0xffff0000;
1.1.1.32 root 8837: }
1.1.1.35 root 8838: #ifdef USE_SERVICE_THREAD
8839: LeaveCriticalSection(&key_buf_crit_sect);
8840: #endif
1.1 root 8841: }
8842: }
8843:
1.1.1.35 root 8844: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1 root 8845: {
1.1.1.54 root 8846: while(key_recv == 0 && !m_exit) {
1.1.1.33 root 8847: pcbios_update_key_code(true);
1.1 root 8848: }
1.1.1.33 root 8849: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
8850: if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
8851: if(REG8(AH) == 0x10) {
8852: key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
8853: } else {
8854: key_code = ((key_code >> 16) & 0xff00);
8855: }
8856: key_recv >>= 16;
1.1 root 8857: }
8858: }
8859: REG16(AX) = key_code & 0xffff;
8860: key_code >>= 16;
1.1.1.33 root 8861: key_recv >>= 16;
1.1.1.35 root 8862:
8863: #ifdef USE_SERVICE_THREAD
8864: service_exit = true;
8865: #endif
8866: return(0);
8867: }
8868:
8869: inline void pcbios_int_16h_00h()
8870: {
8871: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 8872: if(!in_service && !in_service_29h) {
8873: start_service_loop(pcbios_int_16h_00h_thread);
8874: } else {
8875: #endif
8876: pcbios_int_16h_00h_thread(NULL);
8877: REQUEST_HARDWRE_UPDATE();
8878: #ifdef USE_SERVICE_THREAD
8879: }
1.1.1.35 root 8880: #endif
1.1 root 8881: }
8882:
8883: inline void pcbios_int_16h_01h()
8884: {
1.1.1.33 root 8885: if(key_recv == 0) {
8886: pcbios_update_key_code(false);
1.1.1.5 root 8887: }
1.1.1.33 root 8888: if(key_recv != 0) {
8889: UINT32 key_code_tmp = key_code;
8890: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
8891: if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
8892: if(REG8(AH) == 0x11) {
8893: key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
8894: } else {
8895: key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
8896: }
8897: }
1.1 root 8898: }
1.1.1.5 root 8899: REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3 root 8900: #if defined(HAS_I386)
1.1.1.33 root 8901: m_ZF = 0;
8902: #else
8903: m_ZeroVal = 1;
8904: #endif
8905: } else {
8906: #if defined(HAS_I386)
8907: m_ZF = 1;
1.1.1.3 root 8908: #else
1.1.1.33 root 8909: m_ZeroVal = 0;
1.1.1.3 root 8910: #endif
1.1.1.33 root 8911: }
1.1 root 8912: }
8913:
8914: inline void pcbios_int_16h_02h()
8915: {
8916: REG8(AL) = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
8917: REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
8918: REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
8919: REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
8920: REG8(AL) |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
8921: REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
8922: REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
8923: REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
8924: }
8925:
8926: inline void pcbios_int_16h_03h()
8927: {
8928: static UINT16 status = 0;
8929:
8930: switch(REG8(AL)) {
8931: case 0x05:
8932: status = REG16(BX);
8933: break;
8934: case 0x06:
8935: REG16(BX) = status;
8936: break;
8937: default:
1.1.1.3 root 8938: m_CF = 1;
1.1 root 8939: break;
8940: }
8941: }
8942:
8943: inline void pcbios_int_16h_05h()
8944: {
1.1.1.32 root 8945: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8946: #ifdef USE_SERVICE_THREAD
8947: EnterCriticalSection(&key_buf_crit_sect);
8948: #endif
1.1.1.51 root 8949: pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35 root 8950: #ifdef USE_SERVICE_THREAD
8951: LeaveCriticalSection(&key_buf_crit_sect);
8952: #endif
1.1.1.32 root 8953: }
1.1 root 8954: REG8(AL) = 0x00;
8955: }
8956:
1.1.1.60! root 8957: inline void pcbios_int_16h_09h()
! 8958: {
! 8959: REG8(AL) = 0x00;
! 8960: // REG8(AL) |= 0x01; // INT 16/AX=0300h supported (set default delay and rate (PCjr and some PS/2))
! 8961: // REG8(AL) |= 0x02; // INT 16/AX=0304h supported (turn off typematic repeat (PCjr and some PS/2))
! 8962: REG8(AL) |= 0x04; // INT 16/AX=0305h supported (set repeat rate and delay (AT,PS))
! 8963: REG8(AL) |= 0x08; // INT 16/AX=0306h supported (get current typematic rate and delay (newer PS/2s))
! 8964: REG8(AL) |= 0x10; // INT 16/AH=0Ah supported (get keyboard id)
! 8965: REG8(AL) |= 0x20; // INT 16/AH=10h-12h supported (enhanced keyboard support)
! 8966: // REG8(AL) |= 0x40; // INT 16/AH=20h-22h supported (122-key keyboard support)
! 8967: // REG8(AL) |= 0x80; // reserved
! 8968: }
! 8969:
! 8970: inline void pcbios_int_16h_0ah()
! 8971: {
! 8972: // REG16(BX) = 0x41ab; // MF2 Keyboard (usually in translate mode)
! 8973: REG16(BX) = 0x83ab; // MF2 Keyboard (pass-through mode)
! 8974: }
! 8975:
! 8976: inline void pcbios_int_16h_11h()
! 8977: {
! 8978: int key_char, key_scan;
! 8979:
! 8980: #ifdef USE_SERVICE_THREAD
! 8981: EnterCriticalSection(&key_buf_crit_sect);
! 8982: #endif
! 8983: if(pcbios_check_key_buffer(&key_char, &key_scan)) {
! 8984: REG8(AL) = key_char;
! 8985: REG8(AH) = key_scan;
! 8986: #if defined(HAS_I386)
! 8987: m_ZF = 0;
! 8988: #else
! 8989: m_ZeroVal = 1;
! 8990: #endif
! 8991: } else {
! 8992: #if defined(HAS_I386)
! 8993: m_ZF = 1;
! 8994: #else
! 8995: m_ZeroVal = 0;
! 8996: #endif
! 8997: }
! 8998: #ifdef USE_SERVICE_THREAD
! 8999: LeaveCriticalSection(&key_buf_crit_sect);
! 9000: #endif
! 9001: }
! 9002:
1.1 root 9003: inline void pcbios_int_16h_12h()
9004: {
9005: pcbios_int_16h_02h();
9006:
9007: REG8(AH) = 0;//(GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x80 : 0;
9008: REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
9009: REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
9010: REG8(AH) |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
9011: REG8(AH) |= (GetAsyncKeyState(VK_RMENU ) & 0x8000) ? 0x08 : 0;
9012: REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
9013: REG8(AH) |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
9014: REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
9015: }
9016:
9017: inline void pcbios_int_16h_13h()
9018: {
9019: static UINT16 status = 0;
9020:
9021: switch(REG8(AL)) {
9022: case 0x00:
9023: status = REG16(DX);
9024: break;
9025: case 0x01:
9026: REG16(DX) = status;
9027: break;
9028: default:
1.1.1.22 root 9029: unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3 root 9030: m_CF = 1;
1.1 root 9031: break;
9032: }
9033: }
9034:
9035: inline void pcbios_int_16h_14h()
9036: {
9037: static UINT8 status = 0;
9038:
9039: switch(REG8(AL)) {
9040: case 0x00:
9041: case 0x01:
9042: status = REG8(AL);
9043: break;
9044: case 0x02:
9045: REG8(AL) = status;
9046: break;
9047: default:
1.1.1.22 root 9048: unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3 root 9049: m_CF = 1;
1.1 root 9050: break;
9051: }
9052: }
9053:
1.1.1.24 root 9054: inline void pcbios_int_16h_55h()
9055: {
9056: switch(REG8(AL)) {
9057: case 0x00:
9058: // keyboard tsr is not present
9059: break;
9060: case 0xfe:
9061: // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
9062: break;
9063: case 0xff:
9064: break;
9065: default:
9066: unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
9067: m_CF = 1;
9068: break;
9069: }
9070: }
9071:
1.1.1.30 root 9072: inline void pcbios_int_16h_6fh()
9073: {
9074: switch(REG8(AL)) {
9075: case 0x00:
9076: // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
9077: break;
9078: default:
9079: unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
9080: m_CF = 1;
9081: break;
9082: }
9083: }
9084:
1.1.1.37 root 9085: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
9086: {
9087: UINT8 hi = jis >> 8;
9088: UINT8 lo = jis & 0xff;
9089:
9090: lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
9091: hi = (hi - 0x21) / 2 + 0x81;
9092: hi = (hi >= 0xa0) ? hi + 0x40 : hi;
9093: lo = (lo >= 0x7f) ? lo + 0x01 : lo;
9094:
9095: return((hi << 8) + lo);
9096: }
9097:
9098: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
9099: {
9100: UINT8 hi = sjis >> 8;
9101: UINT8 lo = sjis & 0xff;
9102:
9103: if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
9104: return(0x2121);
9105: }
9106: if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
9107: return(0x2121);
9108: }
9109: if(hi >= 0xf0 && hi <= 0xf3) {
9110: // gaiji
9111: if(lo >= 0x40 && lo <= 0x7e) {
9112: return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
9113: }
9114: if(lo >= 0x80 && lo <= 0x9e) {
9115: return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
9116: }
9117: if(lo >= 0x9f && lo <= 0xfc) {
9118: return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
9119: }
9120: }
9121: hi = (hi >= 0xe0) ? hi - 0x40 : hi;
9122: lo = (lo >= 0x80) ? lo - 0x01 : lo;
9123: hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
9124: lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
9125:
9126: return((hi << 8) + lo);
9127: }
9128:
1.1.1.38 root 9129: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
9130: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
9131: // 6. �R���g���[���R�[�h�̉��
1.1.1.37 root 9132:
9133: void pcbios_printer_out(int c, UINT8 data)
9134: {
9135: if(pio[c].conv_mode) {
9136: if(pio[c].sjis_hi != 0) {
9137: if(!pio[c].jis_mode) {
9138: printer_out(c, 0x1c);
9139: printer_out(c, 0x26);
9140: pio[c].jis_mode = true;
9141: }
9142: UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
9143: printer_out(c, jis >> 8);
9144: printer_out(c, jis & 0xff);
9145: pio[c].sjis_hi = 0;
9146: } else if(pio[c].esc_buf[0] == 0x1b) {
9147: printer_out(c, data);
9148: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
9149: pio[c].esc_buf[pio[c].esc_len] = data;
9150: }
9151: pio[c].esc_len++;
9152:
9153: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 9154: case 0x33: // 1Bh 33h XX
9155: case 0x4a: // 1Bh 4Ah XX
9156: case 0x4e: // 1Bh 4Eh XX
9157: case 0x51: // 1Bh 51h XX
9158: case 0x55: // 1Bh 55h XX
9159: case 0x6c: // 1Bh 6Ch XX
9160: case 0x71: // 1Bh 71h XX
9161: case 0x72: // 1Bh 72h XX
1.1.1.37 root 9162: if(pio[c].esc_len == 3) {
9163: pio[c].esc_buf[0] = 0x00;
9164: }
9165: break;
1.1.1.38 root 9166: case 0x24: // 1Bh 24h XX XX
9167: case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37 root 9168: if(pio[c].esc_len == 4) {
9169: pio[c].esc_buf[0] = 0x00;
9170: }
9171: break;
1.1.1.38 root 9172: case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37 root 9173: if(pio[c].esc_len >= 3) {
9174: switch(pio[c].esc_buf[2]) {
9175: case 0: case 1: case 2: case 3: case 4: case 6:
9176: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
9177: pio[c].esc_buf[0] = 0x00;
9178: }
9179: break;
9180: case 32: case 33: case 38: case 39: case 40:
9181: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
9182: pio[c].esc_buf[0] = 0x00;
9183: }
9184: break;
1.1.1.38 root 9185: case 71: case 72: case 73:
9186: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
9187: pio[c].esc_buf[0] = 0x00;
9188: }
9189: break;
1.1.1.37 root 9190: default:
9191: pio[c].esc_buf[0] = 0x00;
9192: break;
9193: }
9194: }
9195: break;
1.1.1.38 root 9196: case 0x40: // 1Bh 40h
1.1.1.37 root 9197: if(pio[c].jis_mode) {
9198: printer_out(c, 0x1c);
9199: printer_out(c, 0x2e);
9200: pio[c].jis_mode = false;
9201: }
9202: pio[c].esc_buf[0] = 0x00;
9203: break;
1.1.1.38 root 9204: case 0x42: // 1Bh 42h data 00h
9205: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 9206: if(pio[c].esc_len >= 3 && data == 0) {
9207: pio[c].esc_buf[0] = 0x00;
9208: }
9209: break;
1.1.1.38 root 9210: case 0x43: // 1Bh 43h (00h) XX
1.1.1.37 root 9211: if(pio[c].esc_len >= 3 && data != 0) {
9212: pio[c].esc_buf[0] = 0x00;
9213: }
9214: break;
1.1.1.38 root 9215: default: // 1Bh XX
1.1.1.37 root 9216: pio[c].esc_buf[0] = 0x00;
9217: break;
9218: }
9219: } else if(pio[c].esc_buf[0] == 0x1c) {
9220: printer_out(c, data);
9221: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
9222: pio[c].esc_buf[pio[c].esc_len] = data;
9223: }
9224: pio[c].esc_len++;
9225:
9226: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 9227: case 0x21: // 1Ch 21h XX
9228: case 0x2d: // 1Ch 2Dh XX
9229: case 0x57: // 1Ch 57h XX
9230: case 0x6b: // 1Ch 6Bh XX
9231: case 0x72: // 1Ch 72h XX
9232: case 0x78: // 1Ch 78h XX
1.1.1.37 root 9233: if(pio[c].esc_len == 3) {
9234: pio[c].esc_buf[0] = 0x00;
9235: }
9236: break;
1.1.1.38 root 9237: case 0x26: // 1Ch 26h
1.1.1.37 root 9238: pio[c].jis_mode = true;
9239: pio[c].esc_buf[0] = 0x00;
9240: break;
1.1.1.38 root 9241: case 0x2e: // 1Ch 2Eh
1.1.1.37 root 9242: pio[c].jis_mode = false;
9243: pio[c].esc_buf[0] = 0x00;
9244: break;
1.1.1.38 root 9245: case 0x32: // 1Ch 32h XX XX data
1.1.1.37 root 9246: if(pio[c].esc_len == 76) {
9247: pio[c].esc_buf[0] = 0x00;
9248: }
9249: break;
1.1.1.38 root 9250: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 9251: if(pio[c].esc_len == 6) {
9252: pio[c].esc_buf[0] = 0x00;
9253: }
9254: break;
1.1.1.38 root 9255: case 0x53: // 1Ch 53h XX XX
9256: case 0x54: // 1Ch 54h XX XX
1.1.1.37 root 9257: if(pio[c].esc_len == 4) {
9258: pio[c].esc_buf[0] = 0x00;
9259: }
9260: break;
1.1.1.38 root 9261: default: // 1Ch XX
1.1.1.37 root 9262: pio[c].esc_buf[0] = 0x00;
9263: break;
9264: }
9265: } else if(data == 0x1b || data == 0x1c) {
9266: printer_out(c, data);
9267: pio[c].esc_buf[0] = data;
9268: pio[c].esc_len = 1;
9269: } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
9270: pio[c].sjis_hi = data;
9271: } else {
9272: if(pio[c].jis_mode) {
9273: printer_out(c, 0x1c);
9274: printer_out(c, 0x2e);
9275: pio[c].jis_mode = false;
9276: }
9277: printer_out(c, data);
9278: }
9279: } else {
9280: if(pio[c].jis_mode) {
9281: printer_out(c, 0x1c);
9282: printer_out(c, 0x2e);
9283: pio[c].jis_mode = false;
9284: }
9285: printer_out(c, data);
9286: }
9287: }
9288:
9289: inline void pcbios_int_17h_00h()
9290: {
9291: if(REG16(DX) < 3) {
9292: pcbios_printer_out(REG16(DX), REG8(AL));
9293: REG8(AH) = 0xd0;
9294: }
9295: }
9296:
9297: inline void pcbios_int_17h_01h()
9298: {
9299: if(REG16(DX) < 3) {
9300: REG8(AH) = 0xd0;
9301: }
9302: }
9303:
9304: inline void pcbios_int_17h_02h()
9305: {
9306: if(REG16(DX) < 3) {
9307: REG8(AH) = 0xd0;
9308: }
9309: }
9310:
9311: inline void pcbios_int_17h_03h()
9312: {
9313: switch(REG8(AL)) {
9314: case 0x00:
9315: if(REG16(DX) < 3) {
9316: if(pio[REG16(DX)].jis_mode) {
9317: printer_out(REG16(DX), 0x1c);
9318: printer_out(REG16(DX), 0x2e);
9319: pio[REG16(DX)].jis_mode = false;
9320: }
9321: for(UINT16 i = 0; i < REG16(CX); i++) {
9322: printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
9323: }
9324: REG16(CX) = 0x0000;
9325: REG8(AH) = 0xd0;
9326: }
9327: break;
9328: default:
9329: unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
9330: break;
9331: }
9332: }
9333:
9334: inline void pcbios_int_17h_50h()
9335: {
9336: switch(REG8(AL)) {
9337: case 0x00:
9338: if(REG16(DX) < 3) {
9339: if(REG16(BX) = 0x0001) {
9340: pio[REG16(DX)].conv_mode = false;
9341: REG8(AL) = 0x00;
9342: } else if(REG16(BX) = 0x0051) {
9343: pio[REG16(DX)].conv_mode = true;
9344: REG8(AL) = 0x00;
9345: } else {
9346: REG8(AL) = 0x01;
9347: }
9348: } else {
9349: REG8(AL) = 0x02;
9350: }
9351: break;
9352: case 0x01:
9353: if(REG16(DX) < 3) {
9354: if(pio[REG16(DX)].conv_mode) {
9355: REG16(BX) = 0x0051;
9356: } else {
9357: REG16(BX) = 0x0001;
9358: }
9359: REG8(AL) = 0x00;
9360: } else {
9361: REG8(AL) = 0x02;
9362: }
9363: break;
9364: default:
9365: unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
9366: break;
9367: }
9368: }
9369:
9370: inline void pcbios_int_17h_51h()
9371: {
9372: if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
9373: REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
9374: } else {
9375: REG16(DX) = 0x0000;
9376: }
9377: }
9378:
9379: inline void pcbios_int_17h_52h()
9380: {
9381: if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
9382: REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
9383: } else {
9384: REG16(DX) = 0x0000;
9385: }
9386: }
9387:
9388: inline void pcbios_int_17h_84h()
9389: {
9390: if(REG16(DX) < 3) {
9391: if(pio[REG16(DX)].jis_mode) {
9392: printer_out(REG16(DX), 0x1c);
9393: printer_out(REG16(DX), 0x2e);
9394: pio[REG16(DX)].jis_mode = false;
9395: }
9396: printer_out(REG16(DX), REG8(AL));
9397: REG8(AH) = 0xd0;
9398: }
9399: }
9400:
9401: inline void pcbios_int_17h_85h()
9402: {
9403: pio[0].conv_mode = (REG8(AL) == 0x00);
9404: }
9405:
1.1 root 9406: inline void pcbios_int_1ah_00h()
9407: {
1.1.1.19 root 9408: pcbios_update_daily_timer_counter(timeGetTime());
9409: REG16(CX) = *(UINT16 *)(mem + 0x46e);
9410: REG16(DX) = *(UINT16 *)(mem + 0x46c);
9411: REG8(AL) = mem[0x470];
9412: mem[0x470] = 0;
1.1 root 9413: }
9414:
9415: inline int to_bcd(int t)
9416: {
9417: int u = (t % 100) / 10;
9418: return (u << 4) | (t % 10);
9419: }
9420:
9421: inline void pcbios_int_1ah_02h()
9422: {
9423: SYSTEMTIME time;
9424:
9425: GetLocalTime(&time);
9426: REG8(CH) = to_bcd(time.wHour);
9427: REG8(CL) = to_bcd(time.wMinute);
9428: REG8(DH) = to_bcd(time.wSecond);
9429: REG8(DL) = 0x00;
9430: }
9431:
9432: inline void pcbios_int_1ah_04h()
9433: {
9434: SYSTEMTIME time;
9435:
9436: GetLocalTime(&time);
9437: REG8(CH) = to_bcd(time.wYear / 100);
9438: REG8(CL) = to_bcd(time.wYear);
9439: REG8(DH) = to_bcd(time.wMonth);
9440: REG8(DL) = to_bcd(time.wDay);
9441: }
9442:
9443: inline void pcbios_int_1ah_0ah()
9444: {
9445: SYSTEMTIME time;
9446: FILETIME file_time;
9447: WORD dos_date, dos_time;
9448:
9449: GetLocalTime(&time);
9450: SystemTimeToFileTime(&time, &file_time);
9451: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
9452: REG16(CX) = dos_date;
9453: }
9454:
9455: // msdos system call
9456:
1.1.1.43 root 9457: inline void msdos_int_21h_56h(int lfn);
9458:
1.1 root 9459: inline void msdos_int_21h_00h()
9460: {
1.1.1.3 root 9461: msdos_process_terminate(SREG(CS), retval, 1);
1.1 root 9462: }
9463:
1.1.1.35 root 9464: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1 root 9465: {
9466: REG8(AL) = msdos_getche();
1.1.1.33 root 9467: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 9468:
1.1.1.35 root 9469: #ifdef USE_SERVICE_THREAD
9470: service_exit = true;
9471: #endif
9472: return(0);
9473: }
9474:
9475: inline void msdos_int_21h_01h()
9476: {
9477: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 9478: if(!in_service && !in_service_29h &&
1.1.1.58 root 9479: *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50 root 9480: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
9481: // msdos_putch() will be used in this service
9482: // if int 29h is hooked, run this service in main thread to call int 29h
9483: start_service_loop(msdos_int_21h_01h_thread);
9484: } else {
9485: #endif
9486: msdos_int_21h_01h_thread(NULL);
9487: REQUEST_HARDWRE_UPDATE();
9488: #ifdef USE_SERVICE_THREAD
9489: }
1.1.1.35 root 9490: #endif
1.1 root 9491: }
9492:
9493: inline void msdos_int_21h_02h()
9494: {
1.1.1.33 root 9495: UINT8 data = REG8(DL);
9496: msdos_putch(data);
9497: REG8(AL) = data;
9498: ctrl_break_detected = ctrl_break_pressed;
1.1 root 9499: }
9500:
9501: inline void msdos_int_21h_03h()
9502: {
9503: REG8(AL) = msdos_aux_in();
9504: }
9505:
9506: inline void msdos_int_21h_04h()
9507: {
9508: msdos_aux_out(REG8(DL));
9509: }
9510:
9511: inline void msdos_int_21h_05h()
9512: {
9513: msdos_prn_out(REG8(DL));
9514: }
9515:
9516: inline void msdos_int_21h_06h()
9517: {
9518: if(REG8(DL) == 0xff) {
9519: if(msdos_kbhit()) {
9520: REG8(AL) = msdos_getch();
1.1.1.3 root 9521: #if defined(HAS_I386)
9522: m_ZF = 0;
9523: #else
9524: m_ZeroVal = 1;
9525: #endif
1.1 root 9526: } else {
9527: REG8(AL) = 0;
1.1.1.3 root 9528: #if defined(HAS_I386)
9529: m_ZF = 1;
9530: #else
9531: m_ZeroVal = 0;
9532: #endif
1.1.1.14 root 9533: maybe_idle();
1.1 root 9534: }
9535: } else {
1.1.1.33 root 9536: UINT8 data = REG8(DL);
9537: msdos_putch(data);
9538: REG8(AL) = data;
1.1 root 9539: }
9540: }
9541:
1.1.1.35 root 9542: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1 root 9543: {
9544: REG8(AL) = msdos_getch();
1.1.1.26 root 9545:
1.1.1.35 root 9546: #ifdef USE_SERVICE_THREAD
9547: service_exit = true;
9548: #endif
9549: return(0);
1.1 root 9550: }
9551:
1.1.1.35 root 9552: inline void msdos_int_21h_07h()
9553: {
9554: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 9555: if(!in_service && !in_service_29h) {
9556: start_service_loop(msdos_int_21h_07h_thread);
9557: } else {
9558: #endif
9559: msdos_int_21h_07h_thread(NULL);
9560: REQUEST_HARDWRE_UPDATE();
9561: #ifdef USE_SERVICE_THREAD
9562: }
1.1.1.35 root 9563: #endif
9564: }
9565:
9566: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1 root 9567: {
9568: REG8(AL) = msdos_getch();
1.1.1.33 root 9569: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 9570:
1.1.1.35 root 9571: #ifdef USE_SERVICE_THREAD
9572: service_exit = true;
9573: #endif
9574: return(0);
9575: }
9576:
9577: inline void msdos_int_21h_08h()
9578: {
9579: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 9580: if(!in_service && !in_service_29h) {
9581: start_service_loop(msdos_int_21h_08h_thread);
9582: } else {
9583: #endif
9584: msdos_int_21h_08h_thread(NULL);
9585: REQUEST_HARDWRE_UPDATE();
9586: #ifdef USE_SERVICE_THREAD
9587: }
1.1.1.35 root 9588: #endif
1.1 root 9589: }
9590:
9591: inline void msdos_int_21h_09h()
9592: {
1.1.1.21 root 9593: msdos_stdio_reopen();
9594:
1.1.1.20 root 9595: process_t *process = msdos_process_info_get(current_psp);
9596: int fd = msdos_psp_get_file_table(1, current_psp);
9597:
1.1.1.14 root 9598: char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
9599: int len = 0;
1.1 root 9600:
1.1.1.14 root 9601: while(str[len] != '$' && len < 0x10000) {
9602: len++;
9603: }
1.1.1.20 root 9604: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 9605: // stdout is redirected to file
1.1.1.20 root 9606: msdos_write(fd, str, len);
1.1 root 9607: } else {
9608: for(int i = 0; i < len; i++) {
1.1.1.14 root 9609: msdos_putch(str[i]);
1.1 root 9610: }
9611: }
1.1.1.33 root 9612: REG8(AL) = '$';
9613: ctrl_break_detected = ctrl_break_pressed;
1.1 root 9614: }
9615:
1.1.1.35 root 9616: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1 root 9617: {
1.1.1.3 root 9618: int ofs = SREG_BASE(DS) + REG16(DX);
1.1 root 9619: int max = mem[ofs] - 1;
9620: UINT8 *buf = mem + ofs + 2;
9621: int chr, p = 0;
9622:
9623: while((chr = msdos_getch()) != 0x0d) {
1.1.1.33 root 9624: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26 root 9625: p = 0;
1.1.1.33 root 9626: msdos_putch(0x03);
9627: msdos_putch(0x0d);
9628: msdos_putch(0x0a);
1.1.1.26 root 9629: break;
1.1.1.33 root 9630: } else if(ctrl_break_pressed) {
9631: // skip this byte
1.1.1.26 root 9632: } else if(chr == 0x00) {
1.1 root 9633: // skip 2nd byte
9634: msdos_getch();
9635: } else if(chr == 0x08) {
9636: // back space
9637: if(p > 0) {
9638: p--;
1.1.1.20 root 9639: if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34 root 9640: msdos_putch(0x08);
9641: msdos_putch(0x08);
9642: msdos_putch(0x20);
9643: msdos_putch(0x20);
9644: msdos_putch(0x08);
9645: msdos_putch(0x08);
1.1.1.36 root 9646: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
9647: p--;
9648: msdos_putch(0x08);
9649: msdos_putch(0x08);
9650: msdos_putch(0x20);
9651: msdos_putch(0x20);
9652: msdos_putch(0x08);
9653: msdos_putch(0x08);
1.1.1.34 root 9654: } else {
9655: msdos_putch(0x08);
9656: msdos_putch(0x20);
9657: msdos_putch(0x08);
9658: }
9659: }
9660: } else if(chr == 0x1b) {
9661: // escape
9662: while(p > 0) {
9663: p--;
9664: if(msdos_ctrl_code_check(buf[p])) {
9665: msdos_putch(0x08);
9666: msdos_putch(0x08);
9667: msdos_putch(0x20);
9668: msdos_putch(0x20);
9669: msdos_putch(0x08);
9670: msdos_putch(0x08);
1.1.1.20 root 9671: } else {
1.1.1.34 root 9672: msdos_putch(0x08);
9673: msdos_putch(0x20);
9674: msdos_putch(0x08);
1.1.1.20 root 9675: }
1.1 root 9676: }
9677: } else if(p < max) {
9678: buf[p++] = chr;
9679: msdos_putch(chr);
9680: }
9681: }
9682: buf[p] = 0x0d;
9683: mem[ofs + 1] = p;
1.1.1.33 root 9684: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 9685:
1.1.1.35 root 9686: #ifdef USE_SERVICE_THREAD
9687: service_exit = true;
9688: #endif
9689: return(0);
9690: }
9691:
9692: inline void msdos_int_21h_0ah()
9693: {
9694: if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
9695: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 9696: if(!in_service && !in_service_29h &&
1.1.1.58 root 9697: *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50 root 9698: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
9699: // msdos_putch() will be used in this service
9700: // if int 29h is hooked, run this service in main thread to call int 29h
9701: start_service_loop(msdos_int_21h_0ah_thread);
9702: } else {
9703: #endif
9704: msdos_int_21h_0ah_thread(NULL);
9705: REQUEST_HARDWRE_UPDATE();
9706: #ifdef USE_SERVICE_THREAD
9707: }
1.1.1.35 root 9708: #endif
9709: }
1.1 root 9710: }
9711:
9712: inline void msdos_int_21h_0bh()
9713: {
9714: if(msdos_kbhit()) {
9715: REG8(AL) = 0xff;
9716: } else {
9717: REG8(AL) = 0x00;
1.1.1.14 root 9718: maybe_idle();
1.1 root 9719: }
1.1.1.33 root 9720: ctrl_break_detected = ctrl_break_pressed;
1.1 root 9721: }
9722:
9723: inline void msdos_int_21h_0ch()
9724: {
9725: // clear key buffer
1.1.1.21 root 9726: msdos_stdio_reopen();
9727:
1.1.1.20 root 9728: process_t *process = msdos_process_info_get(current_psp);
9729: int fd = msdos_psp_get_file_table(0, current_psp);
9730:
9731: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 9732: // stdin is redirected to file
9733: } else {
9734: while(msdos_kbhit()) {
9735: msdos_getch();
9736: }
9737: }
9738:
9739: switch(REG8(AL)) {
9740: case 0x01:
9741: msdos_int_21h_01h();
9742: break;
9743: case 0x06:
9744: msdos_int_21h_06h();
9745: break;
9746: case 0x07:
9747: msdos_int_21h_07h();
9748: break;
9749: case 0x08:
9750: msdos_int_21h_08h();
9751: break;
9752: case 0x0a:
9753: msdos_int_21h_0ah();
9754: break;
9755: default:
1.1.1.48 root 9756: // the buffer is flushed but no input is attempted
1.1 root 9757: break;
9758: }
9759: }
9760:
9761: inline void msdos_int_21h_0dh()
9762: {
9763: }
9764:
9765: inline void msdos_int_21h_0eh()
9766: {
9767: if(REG8(DL) < 26) {
9768: _chdrive(REG8(DL) + 1);
9769: msdos_cds_update(REG8(DL));
1.1.1.23 root 9770: msdos_sda_update(current_psp);
1.1 root 9771: }
9772: REG8(AL) = 26; // zdrive
9773: }
9774:
1.1.1.14 root 9775: inline void msdos_int_21h_0fh()
9776: {
9777: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9778: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 9779: const char *path = msdos_fcb_path(fcb);
1.1.1.60! root 9780: HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16 root 9781:
1.1.1.14 root 9782: if(hFile == INVALID_HANDLE_VALUE) {
9783: REG8(AL) = 0xff;
9784: } else {
9785: REG8(AL) = 0;
9786: fcb->current_block = 0;
9787: fcb->record_size = 128;
9788: fcb->file_size = GetFileSize(hFile, NULL);
9789: fcb->handle = hFile;
9790: fcb->cur_record = 0;
9791: }
9792: }
9793:
9794: inline void msdos_int_21h_10h()
9795: {
9796: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9797: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9798:
9799: REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
9800: }
9801:
1.1 root 9802: inline void msdos_int_21h_11h()
9803: {
1.1.1.3 root 9804: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9805: fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 9806:
9807: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 9808: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9809: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
9810: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45 root 9811: const char *path = msdos_fcb_path(fcb);
1.1.1.60! root 9812: WIN32_FIND_DATAA fd;
1.1 root 9813:
1.1.1.13 root 9814: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
9815: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
9816: FindClose(dtainfo->find_handle);
9817: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9818: }
9819: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 9820: dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
9821: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 9822:
1.1.1.14 root 9823: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
9824: dtainfo->allowable_mask &= ~8;
1.1 root 9825: }
1.1.1.60! root 9826: if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14 root 9827: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 9828: !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60! root 9829: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 9830: FindClose(dtainfo->find_handle);
9831: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9832: break;
9833: }
9834: }
9835: }
1.1.1.13 root 9836: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 9837: if(ext_fcb->flag == 0xff) {
9838: ext_find->flag = 0xff;
9839: memset(ext_find->reserved, 0, 5);
9840: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9841: }
9842: find->drive = _getdrive();
1.1.1.13 root 9843: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 9844: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9845: find->nt_res = 0;
9846: msdos_find_file_conv_local_time(&fd);
9847: find->create_time_ms = 0;
9848: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9849: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9850: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9851: find->cluster_hi = find->cluster_lo = 0;
9852: find->file_size = fd.nFileSizeLow;
9853: REG8(AL) = 0x00;
1.1.1.14 root 9854: } else if(dtainfo->allowable_mask & 8) {
1.1 root 9855: if(ext_fcb->flag == 0xff) {
9856: ext_find->flag = 0xff;
9857: memset(ext_find->reserved, 0, 5);
9858: ext_find->attribute = 8;
9859: }
9860: find->drive = _getdrive();
9861: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
9862: find->attribute = 8;
9863: find->nt_res = 0;
9864: msdos_find_file_conv_local_time(&fd);
9865: find->create_time_ms = 0;
9866: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9867: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9868: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9869: find->cluster_hi = find->cluster_lo = 0;
9870: find->file_size = 0;
1.1.1.14 root 9871: dtainfo->allowable_mask &= ~8;
1.1 root 9872: REG8(AL) = 0x00;
9873: } else {
9874: REG8(AL) = 0xff;
9875: }
9876: }
9877:
9878: inline void msdos_int_21h_12h()
9879: {
1.1.1.3 root 9880: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14 root 9881: // fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 9882:
9883: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 9884: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9885: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
9886: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60! root 9887: WIN32_FIND_DATAA fd;
1.1 root 9888:
1.1.1.13 root 9889: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
9890: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60! root 9891: if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14 root 9892: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 9893: !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60! root 9894: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 9895: FindClose(dtainfo->find_handle);
9896: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9897: break;
9898: }
9899: }
9900: } else {
1.1.1.13 root 9901: FindClose(dtainfo->find_handle);
9902: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9903: }
9904: }
1.1.1.13 root 9905: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 9906: if(ext_fcb->flag == 0xff) {
9907: ext_find->flag = 0xff;
9908: memset(ext_find->reserved, 0, 5);
9909: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9910: }
9911: find->drive = _getdrive();
1.1.1.13 root 9912: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 9913: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9914: find->nt_res = 0;
9915: msdos_find_file_conv_local_time(&fd);
9916: find->create_time_ms = 0;
9917: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9918: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9919: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9920: find->cluster_hi = find->cluster_lo = 0;
9921: find->file_size = fd.nFileSizeLow;
9922: REG8(AL) = 0x00;
1.1.1.14 root 9923: } else if(dtainfo->allowable_mask & 8) {
1.1 root 9924: if(ext_fcb->flag == 0xff) {
9925: ext_find->flag = 0xff;
9926: memset(ext_find->reserved, 0, 5);
9927: ext_find->attribute = 8;
9928: }
9929: find->drive = _getdrive();
9930: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
9931: find->attribute = 8;
9932: find->nt_res = 0;
9933: msdos_find_file_conv_local_time(&fd);
9934: find->create_time_ms = 0;
9935: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9936: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9937: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9938: find->cluster_hi = find->cluster_lo = 0;
9939: find->file_size = 0;
1.1.1.14 root 9940: dtainfo->allowable_mask &= ~8;
1.1 root 9941: REG8(AL) = 0x00;
9942: } else {
9943: REG8(AL) = 0xff;
9944: }
9945: }
9946:
9947: inline void msdos_int_21h_13h()
9948: {
1.1.1.3 root 9949: if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1 root 9950: REG8(AL) = 0xff;
9951: } else {
9952: REG8(AL) = 0x00;
9953: }
9954: }
9955:
1.1.1.16 root 9956: inline void msdos_int_21h_14h()
9957: {
9958: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9959: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9960: process_t *process = msdos_process_info_get(current_psp);
9961: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9962: DWORD num = 0;
9963:
9964: memset(mem + dta_laddr, 0, fcb->record_size);
9965: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9966: REG8(AL) = 1;
9967: } else {
9968: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9969: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9970: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9971: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
9972: }
9973: }
9974:
9975: inline void msdos_int_21h_15h()
1.1.1.14 root 9976: {
9977: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9978: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16 root 9979: process_t *process = msdos_process_info_get(current_psp);
9980: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9981: DWORD num = 0;
1.1.1.14 root 9982:
1.1.1.16 root 9983: if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9984: REG8(AL) = 1;
9985: } else {
9986: fcb->file_size = GetFileSize(fcb->handle, NULL);
9987: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9988: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9989: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9990: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
9991: }
9992: }
9993:
9994: inline void msdos_int_21h_16h()
9995: {
9996: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9997: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 9998: const char *path = msdos_fcb_path(fcb);
1.1.1.60! root 9999: HANDLE hFile = CreateFileA(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, ext_fcb->flag == 0xff ? ext_fcb->attribute : FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16 root 10000:
1.1.1.14 root 10001: if(hFile == INVALID_HANDLE_VALUE) {
10002: REG8(AL) = 0xff;
10003: } else {
10004: REG8(AL) = 0;
10005: fcb->current_block = 0;
10006: fcb->record_size = 128;
10007: fcb->file_size = 0;
10008: fcb->handle = hFile;
10009: fcb->cur_record = 0;
10010: }
10011: }
10012:
1.1.1.16 root 10013: inline void msdos_int_21h_17h()
10014: {
10015: ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10016: fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45 root 10017: // const char *path_src = msdos_fcb_path(fcb_src);
10018: char path_src[MAX_PATH];
10019: strcpy(path_src, msdos_fcb_path(fcb_src));
10020:
1.1.1.16 root 10021: ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
10022: fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45 root 10023: // const char *path_dst = msdos_fcb_path(fcb_dst);
10024: char path_dst[MAX_PATH];
10025: strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16 root 10026:
10027: if(rename(path_src, path_dst)) {
10028: REG8(AL) = 0xff;
10029: } else {
10030: REG8(AL) = 0;
10031: }
10032: }
10033:
1.1 root 10034: inline void msdos_int_21h_18h()
10035: {
10036: REG8(AL) = 0x00;
10037: }
10038:
10039: inline void msdos_int_21h_19h()
10040: {
10041: REG8(AL) = _getdrive() - 1;
10042: }
10043:
10044: inline void msdos_int_21h_1ah()
10045: {
10046: process_t *process = msdos_process_info_get(current_psp);
10047:
10048: process->dta.w.l = REG16(DX);
1.1.1.3 root 10049: process->dta.w.h = SREG(DS);
1.1.1.23 root 10050: msdos_sda_update(current_psp);
1.1 root 10051: }
10052:
10053: inline void msdos_int_21h_1bh()
10054: {
10055: int drive_num = _getdrive() - 1;
10056: UINT16 seg, ofs;
10057:
10058: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
10059: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
10060: REG8(AL) = dpb->highest_sector_num + 1;
10061: REG16(CX) = dpb->bytes_per_sector;
10062: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 10063: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 10064: } else {
10065: REG8(AL) = 0xff;
1.1.1.3 root 10066: m_CF = 1;
1.1 root 10067: }
10068:
10069: }
10070:
10071: inline void msdos_int_21h_1ch()
10072: {
1.1.1.41 root 10073: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1 root 10074: UINT16 seg, ofs;
10075:
10076: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
10077: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
10078: REG8(AL) = dpb->highest_sector_num + 1;
10079: REG16(CX) = dpb->bytes_per_sector;
10080: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 10081: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 10082: } else {
10083: REG8(AL) = 0xff;
1.1.1.3 root 10084: m_CF = 1;
1.1 root 10085: }
10086:
10087: }
10088:
10089: inline void msdos_int_21h_1dh()
10090: {
10091: REG8(AL) = 0;
10092: }
10093:
10094: inline void msdos_int_21h_1eh()
10095: {
10096: REG8(AL) = 0;
10097: }
10098:
10099: inline void msdos_int_21h_1fh()
10100: {
10101: int drive_num = _getdrive() - 1;
10102: UINT16 seg, ofs;
10103:
10104: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
10105: REG8(AL) = 0;
1.1.1.3 root 10106: SREG(DS) = seg;
10107: i386_load_segment_descriptor(DS);
1.1 root 10108: REG16(BX) = ofs;
10109: } else {
10110: REG8(AL) = 0xff;
1.1.1.3 root 10111: m_CF = 1;
1.1 root 10112: }
10113: }
10114:
10115: inline void msdos_int_21h_20h()
10116: {
10117: REG8(AL) = 0;
10118: }
10119:
1.1.1.14 root 10120: inline void msdos_int_21h_21h()
10121: {
10122: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10123: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
10124:
10125: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
10126: REG8(AL) = 1;
10127: } else {
10128: process_t *process = msdos_process_info_get(current_psp);
10129: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
10130: memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16 root 10131: DWORD num = 0;
1.1.1.14 root 10132: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
10133: REG8(AL) = 1;
10134: } else {
10135: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
10136: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 10137: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14 root 10138: }
10139: }
10140: }
10141:
10142: inline void msdos_int_21h_22h()
10143: {
10144: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10145: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
10146:
10147: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
10148: REG8(AL) = 0xff;
10149: } else {
10150: process_t *process = msdos_process_info_get(current_psp);
10151: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16 root 10152: DWORD num = 0;
1.1.1.14 root 10153: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
10154: fcb->file_size = GetFileSize(fcb->handle, NULL);
10155: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
10156: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 10157: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14 root 10158: }
10159: }
10160:
1.1.1.16 root 10161: inline void msdos_int_21h_23h()
10162: {
10163: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10164: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 10165: const char *path = msdos_fcb_path(fcb);
1.1.1.60! root 10166: HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16 root 10167:
10168: if(hFile == INVALID_HANDLE_VALUE) {
10169: REG8(AL) = 0xff;
10170: } else {
10171: UINT32 size = GetFileSize(hFile, NULL);
10172: fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
10173: REG8(AL) = 0;
10174: }
10175: }
10176:
10177: inline void msdos_int_21h_24h()
10178: {
10179: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10180: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
10181:
10182: fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
10183: }
10184:
1.1 root 10185: inline void msdos_int_21h_25h()
10186: {
10187: *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3 root 10188: *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1 root 10189: }
10190:
10191: inline void msdos_int_21h_26h()
10192: {
10193: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
10194:
10195: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48 root 10196: psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1 root 10197: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
10198: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
10199: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
10200: psp->parent_psp = 0;
10201: }
10202:
1.1.1.16 root 10203: inline void msdos_int_21h_27h()
10204: {
10205: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10206: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
10207:
10208: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
10209: REG8(AL) = 1;
10210: } else {
10211: process_t *process = msdos_process_info_get(current_psp);
10212: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
10213: memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
10214: DWORD num = 0;
10215: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
10216: REG8(AL) = 1;
10217: } else {
10218: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
10219: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
10220: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
10221: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
10222: }
10223: }
10224: }
10225:
10226: inline void msdos_int_21h_28h()
10227: {
10228: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
10229: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
10230:
10231: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
10232: REG8(AL) = 0xff;
10233: } else {
10234: process_t *process = msdos_process_info_get(current_psp);
10235: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
10236: DWORD num = 0;
10237: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
10238: fcb->file_size = GetFileSize(fcb->handle, NULL);
10239: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
10240: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
10241: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
10242: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
10243: }
10244: }
10245:
1.1 root 10246: inline void msdos_int_21h_29h()
10247: {
1.1.1.20 root 10248: int ofs = 0;//SREG_BASE(DS) + REG16(SI);
10249: char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1 root 10250: UINT8 drv = 0;
10251: char sep_chars[] = ":.;,=+";
10252: char end_chars[] = "\\<>|/\"[]";
10253: char spc_chars[] = " \t";
10254:
1.1.1.20 root 10255: memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
10256: buffer[1023] = 0;
10257: memset(name, 0x20, sizeof(name));
10258: memset(ext, 0x20, sizeof(ext));
10259:
1.1 root 10260: if(REG8(AL) & 1) {
1.1.1.20 root 10261: ofs += strspn((char *)(buffer + ofs), spc_chars);
10262: if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1 root 10263: ofs++;
10264: }
10265: }
1.1.1.20 root 10266: ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1 root 10267:
1.1.1.24 root 10268: if(buffer[ofs + 1] == ':') {
10269: if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
10270: drv = buffer[ofs] - 'a' + 1;
1.1.1.20 root 10271: ofs += 2;
1.1.1.24 root 10272: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
10273: ofs++;
10274: }
10275: } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
10276: drv = buffer[ofs] - 'A' + 1;
1.1 root 10277: ofs += 2;
1.1.1.24 root 10278: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
10279: ofs++;
10280: }
1.1 root 10281: }
10282: }
1.1.1.20 root 10283: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
10284: UINT8 c = buffer[ofs];
10285: if(is_kanji) {
10286: is_kanji = 0;
10287: } else if(msdos_lead_byte_check(c)) {
10288: is_kanji = 1;
10289: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 10290: break;
10291: } else if(c >= 'a' && c <= 'z') {
10292: c -= 0x20;
10293: }
10294: ofs++;
10295: name[i] = c;
10296: }
1.1.1.20 root 10297: if(buffer[ofs] == '.') {
1.1 root 10298: ofs++;
1.1.1.20 root 10299: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
10300: UINT8 c = buffer[ofs];
10301: if(is_kanji) {
10302: is_kanji = 0;
10303: } else if(msdos_lead_byte_check(c)) {
10304: is_kanji = 1;
10305: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 10306: break;
10307: } else if(c >= 'a' && c <= 'z') {
10308: c -= 0x20;
10309: }
10310: ofs++;
10311: ext[i] = c;
10312: }
10313: }
1.1.1.20 root 10314: int si = REG16(SI) + ofs;
1.1.1.3 root 10315: int ds = SREG(DS);
1.1 root 10316: while(si > 0xffff) {
10317: si -= 0x10;
10318: ds++;
10319: }
10320: REG16(SI) = si;
1.1.1.3 root 10321: SREG(DS) = ds;
10322: i386_load_segment_descriptor(DS);
1.1 root 10323:
1.1.1.3 root 10324: UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20 root 10325: if(!(REG8(AL) & 2) || drv != 0) {
10326: fcb[0] = drv;
10327: }
10328: if(!(REG8(AL) & 4) || name[0] != 0x20) {
10329: memcpy(fcb + 1, name, 8);
10330: }
10331: if(!(REG8(AL) & 8) || ext[0] != 0x20) {
10332: memcpy(fcb + 9, ext, 3);
10333: }
10334: for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1 root 10335: if(fcb[i] == '*') {
10336: found_star = 1;
10337: }
10338: if(found_star) {
10339: fcb[i] = '?';
10340: }
10341: }
1.1.1.20 root 10342: for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1 root 10343: if(fcb[i] == '*') {
10344: found_star = 1;
10345: }
10346: if(found_star) {
10347: fcb[i] = '?';
10348: }
10349: }
10350:
1.1.1.44 root 10351: if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1 root 10352: if(memchr(fcb + 1, '?', 8 + 3)) {
10353: REG8(AL) = 0x01;
1.1.1.20 root 10354: } else {
10355: REG8(AL) = 0x00;
1.1 root 10356: }
10357: } else {
10358: REG8(AL) = 0xff;
10359: }
10360: }
10361:
10362: inline void msdos_int_21h_2ah()
10363: {
10364: SYSTEMTIME sTime;
10365:
10366: GetLocalTime(&sTime);
10367: REG16(CX) = sTime.wYear;
10368: REG8(DH) = (UINT8)sTime.wMonth;
10369: REG8(DL) = (UINT8)sTime.wDay;
10370: REG8(AL) = (UINT8)sTime.wDayOfWeek;
10371: }
10372:
10373: inline void msdos_int_21h_2bh()
10374: {
1.1.1.14 root 10375: REG8(AL) = 0xff;
1.1 root 10376: }
10377:
10378: inline void msdos_int_21h_2ch()
10379: {
10380: SYSTEMTIME sTime;
10381:
10382: GetLocalTime(&sTime);
10383: REG8(CH) = (UINT8)sTime.wHour;
10384: REG8(CL) = (UINT8)sTime.wMinute;
10385: REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14 root 10386: REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1 root 10387: }
10388:
10389: inline void msdos_int_21h_2dh()
10390: {
10391: REG8(AL) = 0x00;
10392: }
10393:
10394: inline void msdos_int_21h_2eh()
10395: {
10396: process_t *process = msdos_process_info_get(current_psp);
10397:
10398: process->verify = REG8(AL);
10399: }
10400:
10401: inline void msdos_int_21h_2fh()
10402: {
10403: process_t *process = msdos_process_info_get(current_psp);
10404:
10405: REG16(BX) = process->dta.w.l;
1.1.1.3 root 10406: SREG(ES) = process->dta.w.h;
10407: i386_load_segment_descriptor(ES);
1.1 root 10408: }
10409:
10410: inline void msdos_int_21h_30h()
10411: {
10412: // Version Flag / OEM
1.1.1.27 root 10413: if(REG8(AL) == 0x01) {
1.1.1.29 root 10414: #ifdef SUPPORT_HMA
10415: REG16(BX) = 0x0000;
10416: #else
10417: REG16(BX) = 0x1000; // DOS is in HMA
10418: #endif
1.1 root 10419: } else {
1.1.1.27 root 10420: // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
10421: // but this is not correct on Windows 98 SE
10422: // REG16(BX) = 0xff01; // OEM = Microsoft, PC/AT
10423: REG16(BX) = 0xff00; // OEM = Microsoft
1.1 root 10424: }
1.1.1.27 root 10425: REG16(CX) = 0x0000;
1.1.1.30 root 10426: REG8(AL) = dos_major_version; // 7
10427: REG8(AH) = dos_minor_version; // 10
1.1 root 10428: }
10429:
10430: inline void msdos_int_21h_31h()
10431: {
1.1.1.29 root 10432: try {
10433: msdos_mem_realloc(current_psp, REG16(DX), NULL);
10434: } catch(...) {
10435: // recover the broken mcb
10436: int mcb_seg = current_psp - 1;
10437: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 10438:
1.1.1.29 root 10439: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 10440: mcb->mz = 'M';
10441: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
10442:
1.1.1.29 root 10443: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 10444: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 10445: } else {
1.1.1.39 root 10446: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 10447: }
10448: } else {
10449: mcb->mz = 'Z';
1.1.1.30 root 10450: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 10451: }
10452: msdos_mem_realloc(current_psp, REG16(DX), NULL);
10453: }
1.1 root 10454: msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
10455: }
10456:
10457: inline void msdos_int_21h_32h()
10458: {
10459: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
10460: UINT16 seg, ofs;
10461:
10462: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
10463: REG8(AL) = 0;
1.1.1.3 root 10464: SREG(DS) = seg;
10465: i386_load_segment_descriptor(DS);
1.1 root 10466: REG16(BX) = ofs;
10467: } else {
10468: REG8(AL) = 0xff;
1.1.1.3 root 10469: m_CF = 1;
1.1 root 10470: }
10471: }
10472:
10473: inline void msdos_int_21h_33h()
10474: {
10475: char path[MAX_PATH];
1.1.1.48 root 10476: char drive = 3; // C:
1.1 root 10477:
10478: switch(REG8(AL)) {
10479: case 0x00:
1.1.1.33 root 10480: REG8(DL) = ctrl_break_checking;
1.1 root 10481: break;
10482: case 0x01:
1.1.1.33 root 10483: ctrl_break_checking = REG8(DL);
10484: break;
10485: case 0x02:
10486: {
10487: UINT8 old = ctrl_break_checking;
10488: ctrl_break_checking = REG8(DL);
10489: REG8(DL) = old;
10490: }
10491: break;
10492: case 0x03:
10493: case 0x04:
10494: // DOS 4.0+ - Unused
1.1 root 10495: break;
10496: case 0x05:
1.1.1.60! root 10497: if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48 root 10498: if(path[0] >= 'a' && path[0] <= 'z') {
10499: drive = path[0] - 'a' + 1;
10500: } else if(path[0] >= 'A' && path[0] <= 'Z') {
10501: drive = path[0] - 'A' + 1;
10502: }
1.1 root 10503: }
1.1.1.48 root 10504: REG8(DL) = (UINT8)drive;
1.1 root 10505: break;
10506: case 0x06:
1.1.1.2 root 10507: // MS-DOS version (7.10)
1.1 root 10508: REG8(BL) = 7;
1.1.1.2 root 10509: REG8(BH) = 10;
1.1 root 10510: REG8(DL) = 0;
1.1.1.29 root 10511: #ifdef SUPPORT_HMA
10512: REG8(DH) = 0x00;
10513: #else
10514: REG8(DH) = 0x10; // DOS is in HMA
10515: #endif
1.1 root 10516: break;
1.1.1.6 root 10517: case 0x07:
10518: if(REG8(DL) == 0) {
10519: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
10520: } else if(REG8(DL) == 1) {
10521: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
10522: }
10523: break;
1.1 root 10524: default:
1.1.1.22 root 10525: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 10526: // REG16(AX) = 0x01;
10527: // m_CF = 1;
10528: REG8(AL) = 0xff;
1.1 root 10529: break;
10530: }
10531: }
10532:
1.1.1.23 root 10533: inline void msdos_int_21h_34h()
10534: {
10535: SREG(ES) = SDA_TOP >> 4;
10536: i386_load_segment_descriptor(ES);
10537: REG16(BX) = offsetof(sda_t, indos_flag);;
10538: }
10539:
1.1 root 10540: inline void msdos_int_21h_35h()
10541: {
10542: REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3 root 10543: SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
10544: i386_load_segment_descriptor(ES);
1.1 root 10545: }
10546:
10547: inline void msdos_int_21h_36h()
10548: {
10549: struct _diskfree_t df = {0};
10550:
10551: if(_getdiskfree(REG8(DL), &df) == 0) {
10552: REG16(AX) = (UINT16)df.sectors_per_cluster;
10553: REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13 root 10554: REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
10555: REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1 root 10556: } else {
10557: REG16(AX) = 0xffff;
10558: }
10559: }
10560:
10561: inline void msdos_int_21h_37h()
10562: {
1.1.1.22 root 10563: static UINT8 dev_flag = 0xff;
1.1 root 10564:
10565: switch(REG8(AL)) {
10566: case 0x00:
1.1.1.22 root 10567: {
10568: process_t *process = msdos_process_info_get(current_psp);
10569: REG8(AL) = 0x00;
10570: REG8(DL) = process->switchar;
10571: }
1.1 root 10572: break;
10573: case 0x01:
1.1.1.22 root 10574: {
10575: process_t *process = msdos_process_info_get(current_psp);
10576: REG8(AL) = 0x00;
10577: process->switchar = REG8(DL);
1.1.1.23 root 10578: msdos_sda_update(current_psp);
1.1.1.22 root 10579: }
10580: break;
10581: case 0x02:
10582: REG8(DL) = dev_flag;
10583: break;
10584: case 0x03:
10585: dev_flag = REG8(DL);
10586: break;
10587: case 0xd0:
10588: case 0xd1:
10589: case 0xd2:
10590: case 0xd3:
10591: case 0xd4:
10592: case 0xd5:
10593: case 0xd6:
10594: case 0xd7:
10595: case 0xdc:
10596: case 0xdd:
10597: case 0xde:
10598: case 0xdf:
1.1.1.48 root 10599: // DIET v1.43e
10600: // REG16(AX) = 1;
10601: REG8(AL) = 0xff;
1.1 root 10602: break;
10603: default:
1.1.1.22 root 10604: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 10605: // REG16(AX) = 1;
10606: REG8(AL) = 0xff;
1.1 root 10607: break;
10608: }
10609: }
10610:
1.1.1.52 root 10611: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17 root 10612: {
10613: char LCdata[80];
10614:
1.1.1.19 root 10615: ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60! root 10616: GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17 root 10617: ci->currency_dec_digits = atoi(LCdata);
1.1.1.60! root 10618: GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 10619: ci->currency_format = *LCdata - '0';
1.1.1.60! root 10620: GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 10621: ci->date_format = *LCdata - '0';
1.1.1.60! root 10622: GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 10623: memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60! root 10624: GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 10625: *ci->date_sep = *LCdata;
1.1.1.60! root 10626: GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17 root 10627: *ci->dec_sep = *LCdata;
1.1.1.60! root 10628: GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17 root 10629: *ci->list_sep = *LCdata;
1.1.1.60! root 10630: GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17 root 10631: *ci->thou_sep = *LCdata;
1.1.1.60! root 10632: GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17 root 10633: *ci->time_sep = *LCdata;
1.1.1.60! root 10634: GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17 root 10635: if(strchr(LCdata, 'H') != NULL) {
10636: ci->time_format = 1;
10637: }
1.1.1.49 root 10638: ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
10639: ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60! root 10640: GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17 root 10641: return atoi(LCdata);
10642: }
10643:
1.1.1.42 root 10644: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
10645: {
10646: return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
10647: }
10648:
1.1.1.43 root 10649: void set_country_info(country_info_t *ci, int size)
10650: {
10651: char LCdata[80];
10652:
10653: if(size >= 0x00 + 2) {
10654: memset(LCdata, 0, sizeof(LCdata));
10655: *LCdata = '0' + ci->date_format;
1.1.1.60! root 10656: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43 root 10657: }
10658: if(size >= 0x02 + 5) {
10659: memset(LCdata, 0, sizeof(LCdata));
10660: memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60! root 10661: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43 root 10662: }
10663: if(size >= 0x07 + 2) {
10664: memset(LCdata, 0, sizeof(LCdata));
10665: *LCdata = *ci->thou_sep;
1.1.1.60! root 10666: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43 root 10667: }
10668: if(size >= 0x09 + 2) {
10669: memset(LCdata, 0, sizeof(LCdata));
10670: *LCdata = *ci->dec_sep;
1.1.1.60! root 10671: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43 root 10672: }
10673: if(size >= 0x0b + 2) {
10674: memset(LCdata, 0, sizeof(LCdata));
10675: *LCdata = *ci->date_sep;
1.1.1.60! root 10676: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43 root 10677: }
10678: if(size >= 0x0d + 2) {
10679: memset(LCdata, 0, sizeof(LCdata));
10680: *LCdata = *ci->time_sep;
1.1.1.60! root 10681: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43 root 10682: }
10683: if(size >= 0x0f + 1) {
10684: memset(LCdata, 0, sizeof(LCdata));
10685: *LCdata = '0' + ci->currency_format;
1.1.1.60! root 10686: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43 root 10687: }
10688: if(size >= 0x10 + 1) {
10689: sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60! root 10690: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43 root 10691: }
10692: if(size >= 0x11 + 1) {
10693: // FIXME: is time format always H/h:mm:ss ???
10694: if(ci->time_format & 1) {
10695: sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
10696: } else {
10697: sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
10698: }
1.1.1.60! root 10699: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43 root 10700: }
10701: if(size >= 0x12 + 4) {
10702: // 12h DWORD address of case map routine
10703: // (FAR CALL, AL = character to map to upper case [>= 80h])
10704: }
10705: if(size >= 0x16 + 2) {
10706: memset(LCdata, 0, sizeof(LCdata));
10707: *LCdata = *ci->list_sep;
1.1.1.60! root 10708: SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43 root 10709: }
10710: }
10711:
1.1.1.42 root 10712: #ifndef SUBLANG_SWAHILI
10713: #define SUBLANG_SWAHILI 0x01
10714: #endif
10715: #ifndef SUBLANG_TSWANA_BOTSWANA
10716: #define SUBLANG_TSWANA_BOTSWANA 0x02
10717: #endif
10718: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
10719: #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
10720: #endif
10721: #ifndef LANG_BANGLA
10722: #define LANG_BANGLA 0x45
10723: #endif
10724: #ifndef SUBLANG_BANGLA_BANGLADESH
10725: #define SUBLANG_BANGLA_BANGLADESH 0x02
10726: #endif
10727:
10728: static const struct {
10729: int code;
10730: USHORT usPrimaryLanguage;
10731: USHORT usSubLanguage;
10732: } country_table[] = {
10733: {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US}, // United States
10734: {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN}, // Canadian-French
10735: {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN}, // Canada (English)
10736: {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA}, // Russia
10737: {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT}, // Egypt
10738: {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA}, // South Africa
10739: // {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA}, // South Africa
10740: // {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA}, // South Africa
10741: // {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA}, // South Africa
10742: // {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA}, // South Africa
10743: // {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA}, // South Africa
10744: {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE}, // Greece
10745: {0x01F, LANG_DUTCH, SUBLANG_DUTCH}, // Netherlands
10746: // {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS}, // Netherlands
10747: {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN}, // Belgium
10748: // {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN}, // Belgium
10749: {0x021, LANG_FRENCH, SUBLANG_FRENCH}, // France
10750: {0x022, LANG_SPANISH, SUBLANG_SPANISH}, // Spain
10751: {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria???
10752: {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY}, // Hungary (not supported by DR DOS 5.0)
10753: {0x027, LANG_ITALIAN, SUBLANG_ITALIAN}, // Italy / San Marino / Vatican City
10754: {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA}, // Romania
10755: {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS}, // Switzerland / Liechtenstein
10756: // {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS}, // Switzerland / Liechtenstein
10757: // {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS}, // Switzerland / Liechtenstein
10758: {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
10759: {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN}, // Austria (DR DOS 5.0)
10760: {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK}, // United Kingdom
10761: {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK}, // Denmark
10762: {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH}, // Sweden
10763: // {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN}, // Sweden
10764: // {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN}, // Sweden
10765: // {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN}, // Sweden
10766: {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL}, // Norway
10767: // {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK}, // Norway
10768: // {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY}, // Norway
10769: // {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY}, // Norway
10770: // {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY}, // Norway
10771: {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND}, // Poland (not supported by DR DOS 5.0)
10772: {0x031, LANG_GERMAN, SUBLANG_GERMAN}, // Germany
10773: {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU}, // Peru
10774: // {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU}, // Peru
10775: {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN}, // Mexico
10776: {0x035, LANG_SPANISH, SUBLANG_NEUTRAL}, // Cuba
10777: {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA}, // Argentina
10778: {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN}, // Brazil (not supported by DR DOS 5.0)
10779: {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE}, // Chile
10780: {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA}, // Columbia
10781: {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA}, // Venezuela
10782: {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA}, // Malaysia
10783: {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS}, // International English / Australia
10784: {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA}, // Indonesia / East Timor
10785: {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES}, // Philippines
10786: {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ}, // New Zealand
10787: {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE}, // Singapore
10788: // {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE}, // Singapore
10789: {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan???
10790: {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN}, // Japan (DR DOS 5.0, MS-DOS 5.0+)
10791: {0x052, LANG_KOREAN, SUBLANG_KOREAN}, // South Korea (DR DOS 5.0)
10792: {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM}, // Vietnam
10793: {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED}, // China (MS-DOS 5.0+)
10794: {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (MS-DOS 5.0+)
10795: {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY}, // Turkey (MS-DOS 5.0+)
10796: {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA}, // India
10797: {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN}, // Pakistan
10798: {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN}, // Afghanistan
10799: // {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN}, // Afghanistan
10800: {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA}, // Sri Lanka
10801: // {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA}, // Sri Lanka
10802: {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN}, // Iran
10803: {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS}, // Belarus
10804: {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND}, // Thailand
10805: {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO}, // Morocco
10806: {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA}, // Algeria
10807: {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA}, // Tunisia
10808: {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA}, // Libya
10809: {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL}, // Senegal
10810: // {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL}, // Senegal
10811: {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN}, // Nigeria
10812: // {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA}, // Nigeria
10813: // {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA}, // Nigeria
10814: {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA}, // Ethiopia
10815: // {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA}, // Ethiopia
10816: {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI}, // Kenya
10817: {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE}, // Zimbabwe
10818: {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA}, // Botswana
10819: {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS}, // Faroe Islands
10820: {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND}, // Greenland
10821: {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE}, // Portugal
10822: {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG}, // Luxembourg
10823: // {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG}, // Luxembourg
10824: // {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG}, // Luxembourg
10825: {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND}, // Ireland
10826: // {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND}, // Ireland
10827: {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND}, // Iceland
10828: {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA}, // Albania
10829: {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA}, // Malta
10830: {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND}, // Finland
10831: // {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND}, // Finland
10832: // {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND}, // Finland
10833: // {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND}, // Finland
10834: {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria
10835: {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA}, // Lithuania
10836: {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA}, // Latvia
10837: {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA}, // Estonia
10838: {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN}, // Serbia / Montenegro
10839: {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia???
10840: {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia
10841: {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA}, // Slovenia
10842: {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN}, // Bosnia-Herzegovina (Latin)
10843: {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC}, // Bosnia-Herzegovina (Cyrillic)
10844: {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA}, // FYR Macedonia
10845: {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC}, // Czech Republic
10846: {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Slovakia
10847: {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE}, // Belize
10848: {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA}, // Guatemala
10849: {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR}, // El Salvador
10850: {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS}, // Honduras
10851: {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA}, // Nicraragua
10852: {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA}, // Costa Rica
10853: {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA}, // Panama
10854: {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA}, // Bolivia
1.1.1.43 root 10855: // {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA}, // Bolivia
1.1.1.42 root 10856: {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR}, // Ecuador
10857: // {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR}, // Ecuador
10858: {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY}, // Paraguay
10859: {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY}, // Uruguay
10860: {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM}, // Brunei Darussalam
10861: {0x311, LANG_ARABIC, SUBLANG_NEUTRAL}, // Arabic (Middle East/Saudi Arabia/etc.)
10862: {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE}, // Ukraine
10863: {0x352, LANG_KOREAN, SUBLANG_KOREAN}, // North Korea
10864: {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG}, // Hong Kong
10865: {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU}, // Macao
10866: {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA}, // Cambodia
10867: {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH}, // Bangladesh
10868: {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (DOS 6.22+)
10869: {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES}, // Maldives
10870: {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON}, // Lebanon
10871: {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN}, // Jordan
10872: {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA}, // Syrian Arab Republic
10873: {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ}, // Ireq
10874: {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT}, // Kuwait
10875: {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA}, // Saudi Arabia
10876: {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN}, // Yemen
10877: {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN}, // Oman
10878: {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE}, // United Arab Emirates
10879: {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL}, // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
10880: {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN}, // Bahrain
10881: {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR}, // Qatar
10882: {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA}, // Mongolia
10883: // {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC}, // Mongolia
10884: {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL}, // Nepal
10885: {-1, 0, 0},
10886: };
10887:
1.1.1.14 root 10888: inline void msdos_int_21h_38h()
10889: {
10890: switch(REG8(AL)) {
10891: case 0x00:
1.1.1.19 root 10892: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14 root 10893: break;
10894: default:
1.1.1.42 root 10895: for(int i = 0;; i++) {
10896: if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
10897: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
10898: break;
10899: } else if(country_table[i].code == -1) {
10900: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
10901: // REG16(AX) = 2;
10902: // m_CF = 1;
1.1.1.48 root 10903: // get current coutry info
1.1.1.42 root 10904: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
10905: break;
10906: }
10907: }
1.1.1.14 root 10908: break;
10909: }
10910: }
10911:
1.1 root 10912: inline void msdos_int_21h_39h(int lfn)
10913: {
1.1.1.3 root 10914: if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10915: REG16(AX) = errno;
1.1.1.3 root 10916: m_CF = 1;
1.1 root 10917: }
10918: }
10919:
10920: inline void msdos_int_21h_3ah(int lfn)
10921: {
1.1.1.3 root 10922: if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10923: REG16(AX) = errno;
1.1.1.3 root 10924: m_CF = 1;
1.1 root 10925: }
10926: }
10927:
10928: inline void msdos_int_21h_3bh(int lfn)
10929: {
1.1.1.45 root 10930: const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44 root 10931:
10932: if(_chdir(path)) {
1.1.1.17 root 10933: REG16(AX) = 3; // must be 3 (path not found)
1.1.1.3 root 10934: m_CF = 1;
1.1.1.44 root 10935: } else {
10936: int drv = _getdrive() - 1;
10937: if(path[1] == ':') {
10938: if(path[0] >= 'A' && path[0] <= 'Z') {
10939: drv = path[0] - 'A';
10940: } else if(path[0] >= 'a' && path[0] <= 'z') {
10941: drv = path[0] - 'a';
10942: }
10943: }
10944: msdos_cds_update(drv, path);
1.1 root 10945: }
10946: }
10947:
10948: inline void msdos_int_21h_3ch()
10949: {
1.1.1.45 root 10950: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60! root 10951: int attr = GetFileAttributesA(path);
1.1.1.37 root 10952: int fd = -1;
10953: int sio_port = 0;
10954: int lpt_port = 0;
1.1 root 10955:
1.1.1.45 root 10956: if(msdos_is_device_path(path)) {
10957: fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1 root 10958: } else {
10959: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
10960: }
10961: if(fd != -1) {
10962: if(attr == -1) {
10963: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
10964: }
1.1.1.60! root 10965: SetFileAttributesA(path, attr);
1.1 root 10966: REG16(AX) = fd;
1.1.1.45 root 10967: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 10968: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 10969: } else {
10970: REG16(AX) = errno;
1.1.1.3 root 10971: m_CF = 1;
1.1 root 10972: }
10973: }
10974:
10975: inline void msdos_int_21h_3dh()
10976: {
1.1.1.45 root 10977: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 10978: int mode = REG8(AL) & 0x03;
1.1.1.37 root 10979: int fd = -1;
10980: int sio_port = 0;
10981: int lpt_port = 0;
1.1 root 10982:
10983: if(mode < 0x03) {
1.1.1.45 root 10984: if(msdos_is_device_path(path)) {
10985: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11 root 10986: } else {
1.1.1.13 root 10987: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 10988: }
1.1 root 10989: if(fd != -1) {
10990: REG16(AX) = fd;
1.1.1.45 root 10991: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 10992: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 10993: } else {
10994: REG16(AX) = errno;
1.1.1.3 root 10995: m_CF = 1;
1.1 root 10996: }
10997: } else {
10998: REG16(AX) = 0x0c;
1.1.1.3 root 10999: m_CF = 1;
1.1 root 11000: }
11001: }
11002:
11003: inline void msdos_int_21h_3eh()
11004: {
11005: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11006: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11007:
1.1.1.20 root 11008: if(fd < process->max_files && file_handler[fd].valid) {
11009: _close(fd);
11010: msdos_file_handler_close(fd);
11011: msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1 root 11012: } else {
11013: REG16(AX) = 0x06;
1.1.1.3 root 11014: m_CF = 1;
1.1 root 11015: }
11016: }
11017:
1.1.1.35 root 11018: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
11019: {
11020: UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
11021: int max = REG16(CX);
11022: int p = 0;
11023:
11024: while(max > p) {
11025: int chr = msdos_getch();
11026:
11027: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
11028: p = 0;
11029: buf[p++] = 0x0d;
11030: if(max > p) {
11031: buf[p++] = 0x0a;
11032: }
11033: msdos_putch(0x03);
11034: msdos_putch(0x0d);
11035: msdos_putch(0x0a);
11036: break;
11037: } else if(ctrl_break_pressed) {
11038: // skip this byte
11039: } else if(chr == 0x00) {
11040: // skip 2nd byte
11041: msdos_getch();
11042: } else if(chr == 0x0d) {
11043: // carriage return
11044: buf[p++] = 0x0d;
11045: if(max > p) {
11046: buf[p++] = 0x0a;
11047: }
11048: msdos_putch('\n');
11049: break;
11050: } else if(chr == 0x08) {
11051: // back space
11052: if(p > 0) {
11053: p--;
11054: if(msdos_ctrl_code_check(buf[p])) {
11055: msdos_putch(0x08);
11056: msdos_putch(0x08);
11057: msdos_putch(0x20);
11058: msdos_putch(0x20);
11059: msdos_putch(0x08);
11060: msdos_putch(0x08);
1.1.1.36 root 11061: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
11062: p--;
11063: msdos_putch(0x08);
11064: msdos_putch(0x08);
11065: msdos_putch(0x20);
11066: msdos_putch(0x20);
11067: msdos_putch(0x08);
11068: msdos_putch(0x08);
1.1.1.35 root 11069: } else {
11070: msdos_putch(0x08);
11071: msdos_putch(0x20);
11072: msdos_putch(0x08);
11073: }
11074: }
11075: } else if(chr == 0x1b) {
11076: // escape
11077: while(p > 0) {
11078: p--;
11079: if(msdos_ctrl_code_check(buf[p])) {
11080: msdos_putch(0x08);
11081: msdos_putch(0x08);
11082: msdos_putch(0x20);
11083: msdos_putch(0x20);
11084: msdos_putch(0x08);
11085: msdos_putch(0x08);
11086: } else {
11087: msdos_putch(0x08);
11088: msdos_putch(0x20);
11089: msdos_putch(0x08);
11090: }
11091: }
11092: } else {
11093: buf[p++] = chr;
11094: msdos_putch(chr);
11095: }
11096: }
11097: REG16(AX) = p;
11098:
11099: #ifdef USE_SERVICE_THREAD
11100: service_exit = true;
11101: #endif
11102: return(0);
11103: }
11104:
1.1 root 11105: inline void msdos_int_21h_3fh()
11106: {
11107: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11108: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11109:
1.1.1.20 root 11110: if(fd < process->max_files && file_handler[fd].valid) {
11111: if(file_mode[file_handler[fd].mode].in) {
11112: if(file_handler[fd].atty) {
1.1 root 11113: // BX is stdin or is redirected to stdin
1.1.1.35 root 11114: if(REG16(CX) != 0) {
11115: #ifdef USE_SERVICE_THREAD
1.1.1.51 root 11116: if(!in_service && !in_service_29h &&
1.1.1.58 root 11117: *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50 root 11118: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
11119: // msdos_putch() will be used in this service
11120: // if int 29h is hooked, run this service in main thread to call int 29h
11121: start_service_loop(msdos_int_21h_3fh_thread);
11122: } else {
11123: #endif
11124: msdos_int_21h_3fh_thread(NULL);
11125: REQUEST_HARDWRE_UPDATE();
11126: #ifdef USE_SERVICE_THREAD
11127: }
1.1.1.35 root 11128: #endif
11129: } else {
11130: REG16(AX) = 0;
1.1 root 11131: }
11132: } else {
1.1.1.37 root 11133: REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 11134: }
11135: } else {
11136: REG16(AX) = 0x05;
1.1.1.3 root 11137: m_CF = 1;
1.1 root 11138: }
11139: } else {
11140: REG16(AX) = 0x06;
1.1.1.3 root 11141: m_CF = 1;
1.1 root 11142: }
11143: }
11144:
11145: inline void msdos_int_21h_40h()
11146: {
11147: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11148: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11149:
1.1.1.20 root 11150: if(fd < process->max_files && file_handler[fd].valid) {
11151: if(file_mode[file_handler[fd].mode].out) {
1.1 root 11152: if(REG16(CX)) {
1.1.1.20 root 11153: if(file_handler[fd].atty) {
1.1 root 11154: // BX is stdout/stderr or is redirected to stdout
11155: for(int i = 0; i < REG16(CX); i++) {
1.1.1.3 root 11156: msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1 root 11157: }
11158: REG16(AX) = REG16(CX);
11159: } else {
1.1.1.20 root 11160: REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 11161: }
11162: } else {
1.1.1.20 root 11163: UINT32 pos = _tell(fd);
11164: _lseek(fd, 0, SEEK_END);
11165: UINT32 size = _tell(fd);
1.1.1.12 root 11166: if(pos < size) {
1.1.1.20 root 11167: _lseek(fd, pos, SEEK_SET);
11168: SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12 root 11169: } else {
11170: for(UINT32 i = size; i < pos; i++) {
11171: UINT8 tmp = 0;
1.1.1.23 root 11172: msdos_write(fd, &tmp, 1);
1.1.1.12 root 11173: }
1.1.1.20 root 11174: _lseek(fd, pos, SEEK_SET);
1.1 root 11175: }
1.1.1.23 root 11176: REG16(AX) = 0;
1.1 root 11177: }
11178: } else {
11179: REG16(AX) = 0x05;
1.1.1.3 root 11180: m_CF = 1;
1.1 root 11181: }
11182: } else {
11183: REG16(AX) = 0x06;
1.1.1.3 root 11184: m_CF = 1;
1.1 root 11185: }
11186: }
11187:
11188: inline void msdos_int_21h_41h(int lfn)
11189: {
1.1.1.3 root 11190: if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 11191: REG16(AX) = errno;
1.1.1.3 root 11192: m_CF = 1;
1.1 root 11193: }
11194: }
11195:
11196: inline void msdos_int_21h_42h()
11197: {
11198: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11199: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11200:
1.1.1.20 root 11201: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 11202: if(REG8(AL) < 0x03) {
1.1.1.35 root 11203: static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20 root 11204: _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
11205: UINT32 pos = _tell(fd);
1.1 root 11206: REG16(AX) = pos & 0xffff;
11207: REG16(DX) = (pos >> 16);
11208: } else {
11209: REG16(AX) = 0x01;
1.1.1.3 root 11210: m_CF = 1;
1.1 root 11211: }
11212: } else {
11213: REG16(AX) = 0x06;
1.1.1.3 root 11214: m_CF = 1;
1.1 root 11215: }
11216: }
11217:
11218: inline void msdos_int_21h_43h(int lfn)
11219: {
1.1.1.45 root 11220: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1 root 11221: int attr;
11222:
1.1.1.14 root 11223: if(!lfn && REG8(AL) > 2) {
11224: REG16(AX) = 0x01;
11225: m_CF = 1;
11226: return;
11227: }
11228: switch(REG8(lfn ? BL : AL)) {
1.1 root 11229: case 0x00:
1.1.1.60! root 11230: if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14 root 11231: REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
11232: } else {
11233: REG16(AX) = (UINT16)GetLastError();
11234: m_CF = 1;
11235: }
11236: break;
11237: case 0x01:
1.1.1.60! root 11238: if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14 root 11239: REG16(AX) = (UINT16)GetLastError();
11240: m_CF = 1;
11241: }
11242: break;
11243: case 0x02:
11244: {
1.1.1.60! root 11245: DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45 root 11246: if(compressed_size != INVALID_FILE_SIZE) {
11247: if(compressed_size != 0) {
1.1.1.60! root 11248: HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45 root 11249: if(hFile != INVALID_HANDLE_VALUE) {
11250: file_size = GetFileSize(hFile, NULL);
11251: CloseHandle(hFile);
11252: }
11253: if(compressed_size == file_size) {
11254: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
11255: // this isn't correct if the file is in the NTFS MFT
1.1.1.60! root 11256: if(GetDiskFreeSpaceA(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45 root 11257: compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
11258: }
1.1.1.14 root 11259: }
11260: }
1.1.1.45 root 11261: REG16(AX) = LOWORD(compressed_size);
11262: REG16(DX) = HIWORD(compressed_size);
1.1.1.14 root 11263: } else {
11264: REG16(AX) = (UINT16)GetLastError();
11265: m_CF = 1;
1.1 root 11266: }
1.1.1.14 root 11267: }
11268: break;
11269: case 0x03:
11270: case 0x05:
11271: case 0x07:
1.1.1.48 root 11272: if(lfn) {
1.1.1.60! root 11273: HANDLE hFile = CreateFileA(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 11274: if(hFile != INVALID_HANDLE_VALUE) {
11275: FILETIME local, time;
11276: DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
11277: if(REG8(BL) == 7) {
11278: ULARGE_INTEGER hund;
11279: hund.LowPart = local.dwLowDateTime;
11280: hund.HighPart = local.dwHighDateTime;
11281: hund.QuadPart += REG16(SI) * 100000;
11282: local.dwLowDateTime = hund.LowPart;
11283: local.dwHighDateTime = hund.HighPart;
11284: }
11285: LocalFileTimeToFileTime(&local, &time);
11286: if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
11287: REG8(BL) == 0x05 ? &time : NULL,
11288: REG8(BL) == 0x03 ? &time : NULL)) {
11289: REG16(AX) = (UINT16)GetLastError();
11290: m_CF = 1;
11291: }
11292: CloseHandle(hFile);
11293: } else {
11294: REG16(AX) = (UINT16)GetLastError();
11295: m_CF = 1;
1.1 root 11296: }
1.1.1.48 root 11297: } else {
11298: // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
11299: // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
11300: // 214307 DR DOS 6.0 - Set File Owner
11301: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
11302: REG16(AX) = 0x01;
11303: m_CF = 1;
1.1.1.14 root 11304: }
11305: break;
11306: case 0x04:
11307: case 0x06:
11308: case 0x08:
1.1.1.48 root 11309: if(lfn) {
1.1.1.14 root 11310: WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60! root 11311: if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14 root 11312: FILETIME *time, local;
11313: time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
11314: 0x06 ? &fad.ftLastAccessTime :
11315: &fad.ftCreationTime;
11316: FileTimeToLocalFileTime(time, &local);
11317: FileTimeToDosDateTime(&local, ®16(DI), ®16(CX));
11318: if(REG8(BL) == 0x08) {
11319: ULARGE_INTEGER hund;
11320: hund.LowPart = local.dwLowDateTime;
11321: hund.HighPart = local.dwHighDateTime;
11322: hund.QuadPart /= 100000;
11323: REG16(SI) = (UINT16)(hund.QuadPart % 200);
11324: }
11325: } else {
11326: REG16(AX) = (UINT16)GetLastError();
11327: m_CF = 1;
1.1 root 11328: }
1.1.1.48 root 11329: } else {
11330: // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
11331: // 214306 DR DOS 6.0 - Get File Owner
11332: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
11333: REG16(AX) = 0x01;
11334: m_CF = 1;
1.1.1.14 root 11335: }
11336: break;
1.1.1.43 root 11337: case 0xff:
1.1.1.48 root 11338: if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43 root 11339: if(REG8(CL) == 0x39) {
11340: msdos_int_21h_39h(1);
11341: break;
11342: } else if(REG8(CL) == 0x56) {
11343: msdos_int_21h_56h(1);
11344: break;
11345: }
11346: }
1.1.1.14 root 11347: default:
1.1.1.22 root 11348: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 11349: REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14 root 11350: m_CF = 1;
11351: break;
11352: }
11353: }
11354:
11355: inline void msdos_int_21h_44h()
11356: {
1.1.1.22 root 11357: static UINT16 iteration_count = 0;
11358:
1.1.1.44 root 11359: process_t *process;
11360: int fd, drv;
1.1.1.14 root 11361:
11362: switch(REG8(AL)) {
11363: case 0x00:
11364: case 0x01:
11365: case 0x02:
11366: case 0x03:
11367: case 0x04:
11368: case 0x05:
11369: case 0x06:
11370: case 0x07:
1.1.1.44 root 11371: process = msdos_process_info_get(current_psp);
11372: fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20 root 11373: if(fd >= process->max_files || !file_handler[fd].valid) {
11374: REG16(AX) = 0x06;
11375: m_CF = 1;
11376: return;
1.1.1.14 root 11377: }
11378: break;
11379: case 0x08:
11380: case 0x09:
1.1.1.44 root 11381: drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
11382: if(!msdos_is_valid_drive(drv)) {
11383: // invalid drive
1.1.1.14 root 11384: REG16(AX) = 0x0f;
11385: m_CF = 1;
11386: return;
1.1 root 11387: }
11388: break;
11389: }
11390: switch(REG8(AL)) {
1.1.1.48 root 11391: case 0x00: // Get Device Information
1.1.1.20 root 11392: REG16(DX) = file_handler[fd].info;
1.1 root 11393: break;
1.1.1.48 root 11394: case 0x01: // Set Device Information
1.1.1.45 root 11395: if(REG8(DH) != 0) {
11396: // REG16(AX) = 0x0d; // data invalid
11397: // m_CF = 1;
11398: file_handler[fd].info = REG16(DX);
11399: } else {
11400: file_handler[fd].info &= 0xff00;
11401: file_handler[fd].info |= REG8(DL);
11402: }
1.1 root 11403: break;
1.1.1.48 root 11404: case 0x02: // Read From Character Device Control Channel
1.1.1.45 root 11405: if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
11406: // from DOSBox
11407: switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
11408: case 0x00:
11409: if(REG16(CX) >= 6) {
11410: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
11411: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
11412: REG16(AX) = 6; // number of bytes actually read
11413: } else {
11414: REG16(AX) = 0x0d; // data invalid
11415: m_CF = 1;
11416: }
11417: break;
11418: case 0x01:
11419: if(REG16(CX) >= 6) {
11420: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004; // flags
11421: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d; // size of this structure
11422: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001; // version 1.0
11423: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
11424: for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
11425: if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
11426: int page = (addr - EMS_TOP) / 0x4000;
11427: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03; // frame type: EMS frame in 64k page
11428: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff; // owner: NONE
11429: *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff; // no logical page number
11430: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 4) = page; // physical EMS page number
11431: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00; // flags: EMS frame
11432: } else {
11433: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00; // frame type: NONE
11434: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff; // owner: NONE
11435: *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff; // non-EMS frame
11436: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff; // EMS page number (NONE)
11437: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa; // flags: direct mapping
11438: }
11439: }
11440: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74; // ??
11441: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00; // no UMB descriptors following
11442: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01; // 1 EMS handle info record
11443: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000; // system handle
11444: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
11445: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
11446: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001; // system handle
11447: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
11448:
11449: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
11450: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
11451: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
11452: REG16(AX) = 6; // number of bytes actually read
11453: } else {
11454: REG16(AX) = 0x0d; // data invalid
11455: m_CF = 1;
11456: }
11457: break;
11458: case 0x02:
11459: if(REG16(CX) >= 2) {
11460: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
11461: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
11462: REG16(AX) = 2; // number of bytes actually read
11463: } else {
11464: REG16(AX) = 0x0d; // data invalid
11465: m_CF = 1;
11466: }
11467: break;
11468: case 0x03:
11469: if(REG16(CX) >= 4) {
11470: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
11471: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
11472: REG16(AX) = 4; // number of bytes actually read
11473: } else {
11474: REG16(AX) = 0x0d; // data invalid
11475: m_CF = 1;
11476: }
11477: break;
11478: default:
11479: REG16(AX) = 0x01; // function number invalid
11480: m_CF = 1;
11481: }
11482: } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
11483: if(REG16(CX) >= 5) {
11484: memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
11485: REG16(AX) = 5; // number of bytes actually read
11486: } else {
11487: REG16(AX) = 0x0d; // data invalid
11488: m_CF = 1;
11489: }
11490: } else {
11491: // memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
11492: // REG16(AX) = REG16(CX);
11493: REG16(AX) = 0x05; // access denied
11494: m_CF = 1;
11495: }
11496: break;
1.1.1.48 root 11497: case 0x03: // Write To Character Device Control Channel
1.1.1.45 root 11498: // REG16(AX) = 0x05;
11499: // m_CF = 1;
11500: REG16(AX) = 0x00; // success
11501: break;
1.1.1.48 root 11502: case 0x04: // Read From Block Device Control Channel
11503: case 0x05: // Write To Block Device Control Channel
1.1 root 11504: REG16(AX) = 0x05;
1.1.1.3 root 11505: m_CF = 1;
1.1 root 11506: break;
1.1.1.48 root 11507: case 0x06: // Get Input Status
1.1.1.20 root 11508: if(file_mode[file_handler[fd].mode].in) {
11509: if(file_handler[fd].atty) {
1.1.1.14 root 11510: REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1 root 11511: } else {
1.1.1.20 root 11512: REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1 root 11513: }
1.1.1.14 root 11514: } else {
11515: REG8(AL) = 0x00;
1.1 root 11516: }
11517: break;
1.1.1.48 root 11518: case 0x07: // Get Output Status
1.1.1.20 root 11519: if(file_mode[file_handler[fd].mode].out) {
1.1.1.14 root 11520: REG8(AL) = 0xff;
11521: } else {
11522: REG8(AL) = 0x00;
1.1 root 11523: }
11524: break;
1.1.1.48 root 11525: case 0x08: // Check If Block Device Removable
1.1.1.44 root 11526: if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14 root 11527: // removable drive
11528: REG16(AX) = 0x00;
1.1 root 11529: } else {
1.1.1.14 root 11530: // fixed drive
11531: REG16(AX) = 0x01;
1.1 root 11532: }
11533: break;
1.1.1.48 root 11534: case 0x09: // Check If Block Device Remote
1.1.1.44 root 11535: if(msdos_is_remote_drive(drv)) {
1.1.1.14 root 11536: // remote drive
11537: REG16(DX) = 0x1000;
1.1.1.44 root 11538: } else if(msdos_is_subst_drive(drv)) {
11539: // subst drive
11540: REG16(DX) = 0x8000;
1.1 root 11541: } else {
1.1.1.14 root 11542: // local drive
1.1.1.44 root 11543: REG16(DX) = 0x0000;
1.1 root 11544: }
11545: break;
1.1.1.48 root 11546: case 0x0a: // Check If Handle Is Remote
1.1.1.45 root 11547: if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
11548: REG16(DX) = 0x8000;
11549: } else {
11550: REG16(DX) = 0x0000;
11551: }
1.1.1.21 root 11552: break;
1.1.1.48 root 11553: case 0x0b: // Set Sharing Retry Count
1.1 root 11554: break;
1.1.1.48 root 11555: case 0x0c: // Generic Character Device Request
1.1.1.22 root 11556: if(REG8(CL) == 0x45) {
11557: // set iteration (retry) count
11558: iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
11559: } else if(REG8(CL) == 0x4a) {
11560: // select code page
11561: active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
11562: msdos_nls_tables_update();
1.1.1.44 root 11563: } else if(REG8(CL) == 0x4c) {
11564: // start code-page preparation
11565: int ids[3] = {437, 0, 0}; // 437: US English
11566: int count = 1, offset = 0;
11567: if(active_code_page != 437) {
11568: ids[count++] = active_code_page;
11569: }
11570: if(system_code_page != 437 && system_code_page != active_code_page) {
11571: ids[count++] = system_code_page;
11572: }
11573: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
11574: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
11575: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11576: for(int i = 0; i < count; i++) {
11577: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11578: }
11579: } else if(REG8(CL) == 0x4d) {
11580: // end code-page preparation
11581: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
11582: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50 root 11583: } else if(REG8(CL) == 0x5f) {
11584: // set display information
11585: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
11586: int cur_width = *(UINT16 *)(mem + 0x44a) + 0;
11587: int cur_height = *(UINT8 *)(mem + 0x484) + 1;
11588: int new_width = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // character columns
11589: int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10); // character rows
11590:
11591: if(cur_width != new_width || cur_height != new_height) {
11592: pcbios_set_console_size(new_width, new_height, true);
11593: }
11594: }
1.1.1.22 root 11595: } else if(REG8(CL) == 0x65) {
11596: // get iteration (retry) count
11597: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
11598: } else if(REG8(CL) == 0x6a) {
11599: // query selected code page
11600: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
11601: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
11602:
11603: CPINFO info;
11604: GetCPInfo(active_code_page, &info);
11605:
11606: if(info.MaxCharSize != 1) {
11607: for(int i = 0;; i++) {
11608: UINT8 lo = info.LeadByte[2 * i + 0];
11609: UINT8 hi = info.LeadByte[2 * i + 1];
11610:
11611: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
11612: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
11613: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
11614:
11615: if(lo == 0 && hi == 0) {
11616: break;
11617: }
11618: }
11619: }
1.1.1.44 root 11620: } else if(REG8(CL) == 0x6b) {
11621: // query prepare list
11622: int ids[3] = {437, 0, 0}; // 437: US English
11623: int count = 1, offset = 0;
11624: if(active_code_page != 437) {
11625: ids[count++] = active_code_page;
11626: }
11627: if(system_code_page != 437 && system_code_page != active_code_page) {
11628: ids[count++] = system_code_page;
11629: }
11630: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
11631: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11632: for(int i = 0; i < count; i++) {
11633: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11634: }
11635: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11636: for(int i = 0; i < count; i++) {
11637: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11638: }
1.1.1.22 root 11639: } else if(REG8(CL) == 0x7f) {
1.1.1.44 root 11640: // get display information
1.1.1.50 root 11641: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0; // level (0 for DOS 4.x-6.0)
11642: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0; // reserved (0)
11643: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14; // length of following data (14)
11644: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1; // bit 0 set for blink, clear for intensity
11645: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1; // mode type (1=text, 2=graphics)
11646: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0; // reserved (0)
11647: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4; // 4 bits per pixel
11648: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) = 8 * (*(UINT16 *)(mem + 0x44a) + 0); // pixel columns
11649: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8 *)(mem + 0x484) + 1); // pixel rows
11650: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0; // character columns
11651: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8 *)(mem + 0x484) + 1; // character rows
1.1.1.22 root 11652: } else {
11653: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
11654: REG16(AX) = 0x01; // invalid function
11655: m_CF = 1;
11656: }
11657: break;
1.1.1.48 root 11658: case 0x0d: // Generic Block Device Request
1.1.1.22 root 11659: if(REG8(CL) == 0x40) {
11660: // set device parameters
1.1.1.48 root 11661: // } else if(REG8(CL) == 0x41) {
11662: // // write logical device track
11663: // } else if(REG8(CL) == 0x42) {
11664: // // format and verify logical device track
1.1.1.22 root 11665: } else if(REG8(CL) == 0x46) {
11666: // set volume serial number
1.1.1.48 root 11667: } else if(REG8(CL) == 0x47) {
11668: // set access flag
11669: // } else if(REG8(CL) == 0x48) {
11670: // // set media lock state
11671: // } else if(REG8(CL) == 0x49) {
11672: // // eject media in drive
1.1.1.22 root 11673: } else if(REG8(CL) == 0x4a) {
11674: // lock logical volume
11675: } else if(REG8(CL) == 0x4b) {
11676: // lock physical volume
11677: } else if(REG8(CL) == 0x60) {
11678: // get device parameters
1.1.1.42 root 11679: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 11680:
1.1.1.42 root 11681: if(pcbios_update_drive_param(drive_num, 1)) {
11682: drive_param_t *drive_param = &drive_params[drive_num];
11683: DISK_GEOMETRY *geo = &drive_param->geometry;
11684:
11685: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
11686: switch(geo->MediaType) {
11687: case F5_360_512:
11688: case F5_320_512:
11689: case F5_320_1024:
11690: case F5_180_512:
11691: case F5_160_512:
11692: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
11693: break;
11694: case F5_1Pt2_512:
11695: case F3_1Pt2_512:
11696: case F3_1Pt23_1024:
11697: case F5_1Pt23_1024:
11698: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
11699: break;
11700: case F3_720_512:
11701: case F3_640_512:
11702: case F5_640_512:
11703: case F5_720_512:
11704: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
11705: break;
11706: case F8_256_128:
11707: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
11708: break;
11709: case FixedMedia:
11710: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
11711: break;
11712: case F3_1Pt44_512:
11713: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
11714: break;
11715: case F3_2Pt88_512:
11716: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
11717: break;
11718: default:
11719: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
11720: // *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
11721: break;
1.1.1.22 root 11722: }
1.1.1.42 root 11723: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
11724: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
11725: switch(geo->MediaType) {
11726: case F5_360_512:
11727: case F5_320_512:
11728: case F5_320_1024:
11729: case F5_180_512:
11730: case F5_160_512:
11731: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
11732: break;
11733: default:
11734: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
11735: break;
11736: }
11737: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
11738: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
11739: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
11740: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
11741: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
11742: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
11743: switch(geo->MediaType) {
11744: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
11745: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
11746: break;
11747: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
11748: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
11749: break;
11750: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
11751: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
11752: break;
11753: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
11754: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
11755: break;
11756: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
11757: case F3_1Pt2_512:
11758: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
11759: case F5_720_512:
11760: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
11761: break;
11762: case FixedMedia: // hard disk
11763: case RemovableMedia:
11764: case Unknown:
11765: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
11766: break;
11767: default:
11768: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
11769: break;
11770: }
11771: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
11772: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
11773: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
11774: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
11775: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
11776: // 21h BYTE device type
11777: // 22h WORD device attributes (removable or not, etc)
1.1.1.22 root 11778: } else {
11779: REG16(AX) = 0x0f; // invalid drive
11780: m_CF = 1;
11781: }
1.1.1.48 root 11782: // } else if(REG8(CL) == 0x61) {
11783: // // read logical device track
11784: // } else if(REG8(CL) == 0x62) {
11785: // // verify logical device track
1.1.1.22 root 11786: } else if(REG8(CL) == 0x66) {
11787: // get volume serial number
11788: char path[] = "A:\\";
11789: char volume_label[MAX_PATH];
11790: DWORD serial_number = 0;
11791: char file_system[MAX_PATH];
11792:
11793: path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
11794:
1.1.1.60! root 11795: if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22 root 11796: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
11797: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
11798: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
11799: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
11800: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20, 8);
11801: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
11802: } else {
11803: REG16(AX) = 0x0f; // invalid drive
11804: m_CF = 1;
11805: }
11806: } else if(REG8(CL) == 0x67) {
11807: // get access flag
11808: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
11809: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
11810: } else if(REG8(CL) == 0x68) {
11811: // sense media type
1.1.1.42 root 11812: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 11813:
1.1.1.42 root 11814: if(pcbios_update_drive_param(drive_num, 1)) {
11815: drive_param_t *drive_param = &drive_params[drive_num];
11816: DISK_GEOMETRY *geo = &drive_param->geometry;
11817:
11818: switch(geo->MediaType) {
11819: case F3_720_512:
11820: case F5_720_512:
11821: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11822: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
11823: break;
11824: case F3_1Pt44_512:
11825: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11826: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
11827: break;
11828: case F3_2Pt88_512:
11829: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11830: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
11831: break;
11832: default:
11833: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
11834: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
11835: break;
1.1.1.22 root 11836: }
11837: } else {
11838: REG16(AX) = 0x0f; // invalid drive
11839: m_CF = 1;
11840: }
11841: } else if(REG8(CL) == 0x6a) {
11842: // unlock logical volume
11843: } else if(REG8(CL) == 0x6b) {
11844: // unlock physical volume
1.1.1.48 root 11845: // } else if(REG8(CL) == 0x6c) {
11846: // // get lock flag
11847: // } else if(REG8(CL) == 0x6d) {
11848: // // enumerate open files
11849: // } else if(REG8(CL) == 0x6e) {
11850: // // find swap file
11851: // } else if(REG8(CL) == 0x6f) {
11852: // // get drive map information
11853: // } else if(REG8(CL) == 0x70) {
11854: // // get current lock state
11855: // } else if(REG8(CL) == 0x71) {
11856: // // get first cluster
1.1.1.22 root 11857: } else {
11858: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
11859: REG16(AX) = 0x01; // invalid function
11860: m_CF = 1;
11861: }
11862: break;
1.1.1.48 root 11863: case 0x0e: // Get Lgical Drive Map
1.1.1.44 root 11864: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
11865: REG16(AX) = 0x0f; // invalid drive
11866: m_CF = 1;
11867: } else {
11868: REG8(AL) = 0;
1.1.1.22 root 11869: }
11870: break;
1.1.1.48 root 11871: case 0x0f: // Set Logical Drive Map
1.1.1.44 root 11872: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
11873: REG16(AX) = 0x0f; // invalid drive
11874: m_CF = 1;
1.1.1.22 root 11875: }
11876: break;
1.1.1.48 root 11877: case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22 root 11878: switch(REG8(CL)) {
11879: case 0x45:
11880: case 0x4a:
1.1.1.48 root 11881: case 0x4c:
11882: case 0x4d:
1.1.1.22 root 11883: case 0x65:
11884: case 0x6a:
1.1.1.48 root 11885: case 0x6b:
1.1.1.22 root 11886: case 0x7f:
11887: REG16(AX) = 0x0000; // supported
11888: break;
11889: default:
11890: REG8(AL) = 0x01; // ioctl capability not available
11891: m_CF = 1;
11892: break;
11893: }
11894: break;
1.1.1.48 root 11895: case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22 root 11896: switch(REG8(CL)) {
11897: case 0x40:
11898: case 0x46:
11899: case 0x4a:
11900: case 0x4b:
11901: case 0x60:
11902: case 0x66:
11903: case 0x67:
11904: case 0x68:
11905: case 0x6a:
11906: case 0x6b:
1.1.1.48 root 11907: if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
11908: // CH = 00h Unknown
11909: // CH = 01h COMn:
11910: // CH = 03h CON
11911: // CH = 05h LPTn:
11912: REG16(AX) = 0x0000; // supported
11913: break;
11914: }
1.1.1.22 root 11915: default:
11916: REG8(AL) = 0x01; // ioctl capability not available
11917: m_CF = 1;
11918: break;
11919: }
11920: break;
1.1.1.48 root 11921: case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
11922: case 0x14: // DR DOS 5.0-6.0 - Set Global Password
11923: case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
11924: case 0x51: // Concurrent DOS v3.2+ - Installation Check
11925: case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
11926: case 0x54: // DR DOS 3.41+ - Set Global Password
11927: case 0x56: // DR DOS 5.0+ - History Buffer Control
11928: case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
11929: case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
11930: case 0x59: // DR Multiuser DOS 5.0 - API
11931: REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22 root 11932: m_CF = 1;
11933: break;
1.1 root 11934: default:
1.1.1.22 root 11935: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 11936: REG16(AX) = 0x01;
1.1.1.3 root 11937: m_CF = 1;
1.1 root 11938: break;
11939: }
11940: }
11941:
11942: inline void msdos_int_21h_45h()
11943: {
11944: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11945: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11946:
1.1.1.20 root 11947: if(fd < process->max_files && file_handler[fd].valid) {
11948: int dup_fd = _dup(fd);
11949: if(dup_fd != -1) {
11950: REG16(AX) = dup_fd;
11951: msdos_file_handler_dup(dup_fd, fd, current_psp);
11952: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
11953: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 11954: } else {
11955: REG16(AX) = errno;
1.1.1.3 root 11956: m_CF = 1;
1.1 root 11957: }
11958: } else {
11959: REG16(AX) = 0x06;
1.1.1.3 root 11960: m_CF = 1;
1.1 root 11961: }
11962: }
11963:
11964: inline void msdos_int_21h_46h()
11965: {
11966: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11967: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
11968: int dup_fd = REG16(CX);
11969: int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1 root 11970:
1.1.1.20 root 11971: if(REG16(BX) == REG16(CX)) {
11972: REG16(AX) = 0x06;
11973: m_CF = 1;
11974: } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
11975: if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
11976: _close(tmp_fd);
11977: msdos_file_handler_close(tmp_fd);
11978: msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
11979: }
11980: if(_dup2(fd, dup_fd) != -1) {
11981: msdos_file_handler_dup(dup_fd, fd, current_psp);
11982: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
11983: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 11984: } else {
11985: REG16(AX) = errno;
1.1.1.3 root 11986: m_CF = 1;
1.1 root 11987: }
11988: } else {
11989: REG16(AX) = 0x06;
1.1.1.3 root 11990: m_CF = 1;
1.1 root 11991: }
11992: }
11993:
11994: inline void msdos_int_21h_47h(int lfn)
11995: {
11996: char path[MAX_PATH];
11997:
11998: if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45 root 11999: if(!lfn) {
12000: strcpy(path, msdos_short_path(path));
12001: }
1.1 root 12002: if(path[1] == ':') {
12003: // the returned path does not include a drive or the initial backslash
1.1.1.45 root 12004: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1 root 12005: } else {
1.1.1.45 root 12006: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1 root 12007: }
12008: } else {
12009: REG16(AX) = errno;
1.1.1.3 root 12010: m_CF = 1;
1.1 root 12011: }
12012: }
12013:
12014: inline void msdos_int_21h_48h()
12015: {
1.1.1.19 root 12016: int seg, umb_linked;
1.1 root 12017:
1.1.1.8 root 12018: if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19 root 12019: // unlink umb not to allocate memory in umb
12020: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
12021: msdos_mem_unlink_umb();
12022: }
1.1.1.8 root 12023: if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
12024: REG16(AX) = seg;
12025: } else {
12026: REG16(AX) = 0x08;
12027: REG16(BX) = msdos_mem_get_free(first_mcb, 0);
12028: m_CF = 1;
12029: }
1.1.1.19 root 12030: if(umb_linked != 0) {
12031: msdos_mem_link_umb();
12032: }
1.1.1.8 root 12033: } else if((malloc_strategy & 0xf0) == 0x40) {
12034: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
12035: REG16(AX) = seg;
12036: } else {
12037: REG16(AX) = 0x08;
12038: REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
12039: m_CF = 1;
12040: }
12041: } else if((malloc_strategy & 0xf0) == 0x80) {
12042: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
12043: REG16(AX) = seg;
12044: } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
12045: REG16(AX) = seg;
12046: } else {
12047: REG16(AX) = 0x08;
12048: REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
12049: m_CF = 1;
12050: }
1.1 root 12051: }
12052: }
12053:
12054: inline void msdos_int_21h_49h()
12055: {
1.1.1.14 root 12056: int mcb_seg = SREG(ES) - 1;
12057: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
12058:
12059: if(mcb->mz == 'M' || mcb->mz == 'Z') {
12060: msdos_mem_free(SREG(ES));
12061: } else {
1.1.1.33 root 12062: REG16(AX) = 0x09; // illegal memory block address
1.1.1.14 root 12063: m_CF = 1;
12064: }
1.1 root 12065: }
12066:
12067: inline void msdos_int_21h_4ah()
12068: {
1.1.1.14 root 12069: int mcb_seg = SREG(ES) - 1;
12070: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1 root 12071: int max_paragraphs;
12072:
1.1.1.14 root 12073: if(mcb->mz == 'M' || mcb->mz == 'Z') {
12074: if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
12075: REG16(AX) = 0x08;
12076: REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
12077: m_CF = 1;
12078: }
12079: } else {
1.1.1.33 root 12080: REG16(AX) = 0x09; // illegal memory block address
1.1.1.3 root 12081: m_CF = 1;
1.1 root 12082: }
12083: }
12084:
12085: inline void msdos_int_21h_4bh()
12086: {
1.1.1.3 root 12087: char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
12088: param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1 root 12089:
12090: switch(REG8(AL)) {
12091: case 0x00:
12092: case 0x01:
12093: if(msdos_process_exec(command, param, REG8(AL))) {
12094: REG16(AX) = 0x02;
1.1.1.3 root 12095: m_CF = 1;
1.1 root 12096: }
12097: break;
1.1.1.14 root 12098: case 0x03:
12099: {
12100: int fd;
12101: if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
12102: REG16(AX) = 0x02;
12103: m_CF = 1;
12104: break;
12105: }
12106: int size = _read(fd, file_buffer, sizeof(file_buffer));
12107: _close(fd);
12108:
12109: UINT16 *overlay = (UINT16 *)param;
12110:
12111: // check exe header
12112: exe_header_t *header = (exe_header_t *)file_buffer;
12113: int header_size = 0;
12114: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
12115: header_size = header->header_size * 16;
12116: // relocation
12117: int start_seg = overlay[1];
12118: for(int i = 0; i < header->relocations; i++) {
12119: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
12120: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
12121: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
12122: }
12123: }
12124: memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
12125: }
12126: break;
1.1.1.48 root 12127: case 0x04:
12128: // Load And Execute In Background (European MS-DOS 4.0 only)
12129: // case 0x05:
12130: // // DOS 5+ - Set Execution State
12131: case 0x80:
12132: // DR DOS v3.41 - Run Already-Loaded Kernel File
12133: case 0xf0:
12134: case 0xf1:
12135: // DIET v1.10+
1.1.1.43 root 12136: case 0xfd:
12137: case 0xfe:
12138: // unknown function called in FreeCOM
12139: REG16(AX) = 0x01;
12140: m_CF = 1;
12141: break;
1.1 root 12142: default:
1.1.1.22 root 12143: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 12144: REG16(AX) = 0x01;
1.1.1.3 root 12145: m_CF = 1;
1.1 root 12146: break;
12147: }
12148: }
12149:
12150: inline void msdos_int_21h_4ch()
12151: {
12152: msdos_process_terminate(current_psp, REG8(AL), 1);
12153: }
12154:
12155: inline void msdos_int_21h_4dh()
12156: {
12157: REG16(AX) = retval;
12158: }
12159:
12160: inline void msdos_int_21h_4eh()
12161: {
12162: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 12163: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
12164: find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45 root 12165: const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60! root 12166: WIN32_FIND_DATAA fd;
1.1 root 12167:
1.1.1.14 root 12168: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
12169: find->find_magic = FIND_MAGIC;
12170: find->dta_index = dtainfo - dtalist;
1.1 root 12171: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 12172: dtainfo->allowable_mask = REG8(CL);
1.1.1.58 root 12173: // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
12174: if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
12175: dtainfo->allowable_mask &= ~0x08;
12176: }
1.1.1.14 root 12177: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 12178:
1.1.1.14 root 12179: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
12180: dtainfo->allowable_mask &= ~8;
1.1 root 12181: }
1.1.1.60! root 12182: if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14 root 12183: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 12184: !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60! root 12185: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 12186: FindClose(dtainfo->find_handle);
12187: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12188: break;
12189: }
12190: }
12191: }
1.1.1.13 root 12192: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 12193: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
12194: msdos_find_file_conv_local_time(&fd);
12195: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
12196: find->size = fd.nFileSizeLow;
1.1.1.13 root 12197: strcpy(find->name, msdos_short_name(&fd));
1.1 root 12198: REG16(AX) = 0;
1.1.1.14 root 12199: } else if(dtainfo->allowable_mask & 8) {
1.1 root 12200: find->attrib = 8;
12201: find->size = 0;
12202: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 12203: dtainfo->allowable_mask &= ~8;
1.1 root 12204: REG16(AX) = 0;
12205: } else {
12206: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 12207: m_CF = 1;
1.1 root 12208: }
12209: }
12210:
12211: inline void msdos_int_21h_4fh()
12212: {
12213: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 12214: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
12215: find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60! root 12216: WIN32_FIND_DATAA fd;
1.1 root 12217:
1.1.1.14 root 12218: if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
12219: REG16(AX) = 0x12;
12220: m_CF = 1;
12221: return;
12222: }
12223: dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13 root 12224: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60! root 12225: if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14 root 12226: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 12227: !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60! root 12228: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 12229: FindClose(dtainfo->find_handle);
12230: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12231: break;
12232: }
12233: }
12234: } else {
1.1.1.13 root 12235: FindClose(dtainfo->find_handle);
12236: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12237: }
12238: }
1.1.1.13 root 12239: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 12240: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
12241: msdos_find_file_conv_local_time(&fd);
12242: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
12243: find->size = fd.nFileSizeLow;
1.1.1.13 root 12244: strcpy(find->name, msdos_short_name(&fd));
1.1 root 12245: REG16(AX) = 0;
1.1.1.14 root 12246: } else if(dtainfo->allowable_mask & 8) {
1.1 root 12247: find->attrib = 8;
12248: find->size = 0;
12249: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 12250: dtainfo->allowable_mask &= ~8;
1.1 root 12251: REG16(AX) = 0;
12252: } else {
12253: REG16(AX) = 0x12;
1.1.1.3 root 12254: m_CF = 1;
1.1 root 12255: }
12256: }
12257:
12258: inline void msdos_int_21h_50h()
12259: {
1.1.1.8 root 12260: if(current_psp != REG16(BX)) {
12261: process_t *process = msdos_process_info_get(current_psp);
12262: if(process != NULL) {
12263: process->psp = REG16(BX);
12264: }
12265: current_psp = REG16(BX);
1.1.1.23 root 12266: msdos_sda_update(current_psp);
1.1.1.8 root 12267: }
1.1 root 12268: }
12269:
12270: inline void msdos_int_21h_51h()
12271: {
12272: REG16(BX) = current_psp;
12273: }
12274:
12275: inline void msdos_int_21h_52h()
12276: {
1.1.1.25 root 12277: SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3 root 12278: i386_load_segment_descriptor(ES);
1.1.1.25 root 12279: REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1 root 12280: }
12281:
1.1.1.43 root 12282: inline void msdos_int_21h_53h()
12283: {
12284: dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
12285: bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
12286:
12287: dpb->bytes_per_sector = bpb->bytes_per_sector;
12288: dpb->highest_sector_num = bpb->sectors_per_track - 1;
12289: dpb->shift_count = 0;
12290: dpb->reserved_sectors = 0;
12291: dpb->fat_num = bpb->fat_num;
12292: dpb->root_entries = bpb->root_entries;
12293: dpb->first_data_sector = 0;
12294: if(bpb->sectors_per_cluster != 0) {
12295: dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
12296: } else {
12297: dpb->highest_cluster_num = 0;
12298: }
12299: dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
12300: dpb->first_dir_sector = 0;
12301: dpb->device_driver_header = 0;
12302: dpb->media_type = bpb->media_type;
12303: dpb->drive_accessed = 0;
12304: dpb->next_dpb_ofs = 0xffff;
12305: dpb->next_dpb_seg = 0xffff;
12306: dpb->first_free_cluster = 0;
12307: dpb->free_clusters = 0xffff;
12308: }
12309:
1.1 root 12310: inline void msdos_int_21h_54h()
12311: {
12312: process_t *process = msdos_process_info_get(current_psp);
12313:
12314: REG8(AL) = process->verify;
12315: }
12316:
12317: inline void msdos_int_21h_55h()
12318: {
12319: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
12320:
12321: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
12322: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
12323: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
12324: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
12325: psp->parent_psp = current_psp;
12326: }
12327:
12328: inline void msdos_int_21h_56h(int lfn)
12329: {
12330: char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3 root 12331: strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
12332: strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1 root 12333:
12334: if(rename(src, dst)) {
12335: REG16(AX) = errno;
1.1.1.3 root 12336: m_CF = 1;
1.1 root 12337: }
12338: }
12339:
12340: inline void msdos_int_21h_57h()
12341: {
12342: FILETIME time, local;
1.1.1.14 root 12343: FILETIME *ctime, *atime, *mtime;
1.1.1.21 root 12344: HANDLE hHandle;
1.1 root 12345:
1.1.1.21 root 12346: if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14 root 12347: REG16(AX) = (UINT16)GetLastError();
12348: m_CF = 1;
12349: return;
12350: }
12351: ctime = atime = mtime = NULL;
12352:
1.1 root 12353: switch(REG8(AL)) {
12354: case 0x00:
1.1.1.6 root 12355: case 0x01:
1.1.1.14 root 12356: mtime = &time;
1.1.1.6 root 12357: break;
12358: case 0x04:
12359: case 0x05:
1.1.1.14 root 12360: atime = &time;
1.1 root 12361: break;
1.1.1.6 root 12362: case 0x06:
12363: case 0x07:
1.1.1.14 root 12364: ctime = &time;
12365: break;
12366: default:
1.1.1.22 root 12367: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14 root 12368: REG16(AX) = 0x01;
12369: m_CF = 1;
12370: return;
12371: }
12372: if(REG8(AL) & 1) {
1.1 root 12373: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
12374: LocalFileTimeToFileTime(&local, &time);
1.1.1.21 root 12375: if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1 root 12376: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12377: m_CF = 1;
1.1 root 12378: }
1.1.1.14 root 12379: } else {
1.1.1.21 root 12380: if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14 root 12381: // assume a device and use the current time
12382: GetSystemTimeAsFileTime(&time);
12383: }
12384: FileTimeToLocalFileTime(&time, &local);
12385: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
1.1 root 12386: }
12387: }
12388:
12389: inline void msdos_int_21h_58h()
12390: {
12391: switch(REG8(AL)) {
12392: case 0x00:
1.1.1.7 root 12393: REG16(AX) = malloc_strategy;
12394: break;
12395: case 0x01:
1.1.1.24 root 12396: // switch(REG16(BX)) {
12397: switch(REG8(BL)) {
1.1.1.7 root 12398: case 0x0000:
12399: case 0x0001:
12400: case 0x0002:
12401: case 0x0040:
12402: case 0x0041:
12403: case 0x0042:
12404: case 0x0080:
12405: case 0x0081:
12406: case 0x0082:
12407: malloc_strategy = REG16(BX);
1.1.1.23 root 12408: msdos_sda_update(current_psp);
1.1.1.7 root 12409: break;
12410: default:
1.1.1.22 root 12411: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7 root 12412: REG16(AX) = 0x01;
12413: m_CF = 1;
12414: break;
12415: }
12416: break;
12417: case 0x02:
1.1.1.19 root 12418: REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7 root 12419: break;
12420: case 0x03:
1.1.1.24 root 12421: // switch(REG16(BX)) {
12422: switch(REG8(BL)) {
1.1.1.7 root 12423: case 0x0000:
1.1.1.19 root 12424: msdos_mem_unlink_umb();
12425: break;
1.1.1.7 root 12426: case 0x0001:
1.1.1.19 root 12427: msdos_mem_link_umb();
1.1.1.7 root 12428: break;
12429: default:
1.1.1.22 root 12430: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7 root 12431: REG16(AX) = 0x01;
12432: m_CF = 1;
12433: break;
12434: }
1.1 root 12435: break;
12436: default:
1.1.1.22 root 12437: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 12438: REG16(AX) = 0x01;
1.1.1.3 root 12439: m_CF = 1;
1.1 root 12440: break;
12441: }
12442: }
12443:
12444: inline void msdos_int_21h_59h()
12445: {
1.1.1.47 root 12446: if(REG16(BX) == 0x0000) {
12447: sda_t *sda = (sda_t *)(mem + SDA_TOP);
12448:
12449: REG16(AX) = sda->extended_error_code;
12450: REG8(BH) = sda->error_class;
12451: REG8(BL) = sda->suggested_action;
12452: REG8(CH) = sda->locus_of_last_error;
12453: // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
12454: if(sda->int21h_5d0ah_called != 0) {
12455: REG8(CL) = sda->int21h_5d0ah_cl;
12456: REG16(DX) = sda->int21h_5d0ah_dx;
12457: // REG16(SI) = sda->int21h_5d0ah_si;
12458: REG16(DI) = sda->last_error_pointer.w.l;
12459: // SREG(DS) = sda->int21h_5d0ah_ds;
12460: // i386_load_segment_descriptor(DS);
12461: SREG(ES) = sda->last_error_pointer.w.h;
12462: i386_load_segment_descriptor(ES);
12463: }
12464: sda->int21h_5d0ah_called = 0;
12465: // } else if(REG16(BX) == 0x0001) {
12466: // // European MS-DOS 4.0 - Get Hard Error Information
12467: } else {
12468: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
12469: REG16(AX) = 0x01;
12470: m_CF = 1;
12471: }
1.1 root 12472: }
12473:
12474: inline void msdos_int_21h_5ah()
12475: {
1.1.1.3 root 12476: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12477: int len = strlen(path);
12478: char tmp[MAX_PATH];
12479:
1.1.1.60! root 12480: if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1 root 12481: int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
12482:
1.1.1.60! root 12483: SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1 root 12484: REG16(AX) = fd;
12485: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 12486: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 12487:
12488: strcpy(path, tmp);
12489: int dx = REG16(DX) + len;
1.1.1.3 root 12490: int ds = SREG(DS);
1.1 root 12491: while(dx > 0xffff) {
12492: dx -= 0x10;
12493: ds++;
12494: }
12495: REG16(DX) = dx;
1.1.1.3 root 12496: SREG(DS) = ds;
12497: i386_load_segment_descriptor(DS);
1.1 root 12498: } else {
12499: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12500: m_CF = 1;
1.1 root 12501: }
12502: }
12503:
12504: inline void msdos_int_21h_5bh()
12505: {
1.1.1.45 root 12506: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 12507:
1.1.1.45 root 12508: // if(msdos_is_existing_file(path)) {
12509: if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1 root 12510: // already exists
12511: REG16(AX) = 0x50;
1.1.1.3 root 12512: m_CF = 1;
1.1 root 12513: } else {
12514: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
12515:
12516: if(fd != -1) {
1.1.1.60! root 12517: SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1 root 12518: REG16(AX) = fd;
12519: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 12520: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 12521: } else {
12522: REG16(AX) = errno;
1.1.1.3 root 12523: m_CF = 1;
1.1 root 12524: }
12525: }
12526: }
12527:
12528: inline void msdos_int_21h_5ch()
12529: {
12530: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 12531: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 12532:
1.1.1.20 root 12533: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 12534: if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35 root 12535: static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20 root 12536: UINT32 pos = _tell(fd);
12537: _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
12538: if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1 root 12539: REG16(AX) = errno;
1.1.1.3 root 12540: m_CF = 1;
1.1 root 12541: }
1.1.1.20 root 12542: _lseek(fd, pos, SEEK_SET);
1.1.1.26 root 12543:
1.1 root 12544: // some seconds may be passed in _locking()
1.1.1.35 root 12545: REQUEST_HARDWRE_UPDATE();
1.1 root 12546: } else {
12547: REG16(AX) = 0x01;
1.1.1.3 root 12548: m_CF = 1;
1.1 root 12549: }
12550: } else {
12551: REG16(AX) = 0x06;
1.1.1.3 root 12552: m_CF = 1;
1.1 root 12553: }
12554: }
12555:
1.1.1.22 root 12556: inline void msdos_int_21h_5dh()
12557: {
12558: switch(REG8(AL)) {
1.1.1.45 root 12559: case 0x00:
12560: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
12561: // current system
12562: static bool reenter = false;
12563: if(!reenter) {
12564: UINT32 offset = SREG_BASE(DS) + REG16(DX);
12565: REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
12566: REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
12567: REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
12568: REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
12569: REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
12570: REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
12571: SREG(DS) = *(UINT16 *)(mem + offset + 0x0c);
12572: SREG(ES) = *(UINT16 *)(mem + offset + 0x0e);
12573: i386_load_segment_descriptor(DS);
12574: i386_load_segment_descriptor(ES);
12575: reenter = true;
12576: try {
12577: msdos_syscall(0x21);
12578: } catch(...) {
12579: }
12580: reenter = false;
12581: }
12582: } else {
12583: REG16(AX) = 0x49; // network software not installed
12584: m_CF = 1;
12585: }
12586: break;
1.1.1.22 root 12587: case 0x06: // get address of dos swappable data area
1.1.1.23 root 12588: SREG(DS) = (SDA_TOP >> 4);
12589: i386_load_segment_descriptor(DS);
12590: REG16(SI) = offsetof(sda_t, crit_error_flag);
12591: REG16(CX) = 0x80;
12592: REG16(DX) = 0x1a;
12593: break;
1.1.1.45 root 12594: case 0x07: // get redirected printer mode
12595: case 0x08: // set redirected printer mode
12596: case 0x09: // flush redirected printer output
12597: REG16(AX) = 0x49; // network software not installed
12598: m_CF = 1;
12599: break;
1.1.1.43 root 12600: case 0x0a: // set extended error information
12601: {
12602: sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47 root 12603: sda->int21h_5d0ah_called = 1;
1.1.1.43 root 12604: sda->extended_error_code = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47 root 12605: // XXX: which one is correct ???
12606: #if 1
12607: // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43 root 12608: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
12609: sda->error_class = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47 root 12610: sda->int21h_5d0ah_cl = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43 root 12611: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47 root 12612: #else
12613: // PC DOS 7 Technical Update
12614: sda->error_class = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
12615: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
12616: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
12617: sda->int21h_5d0ah_cl = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
12618: #endif
12619: sda->int21h_5d0ah_dx = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
12620: // sda->int21h_5d0ah_si = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43 root 12621: sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47 root 12622: // sda->int21h_5d0ah_ds = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43 root 12623: sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
12624: }
12625: break;
1.1.1.23 root 12626: case 0x0b: // get dos swappable data areas
1.1.1.22 root 12627: REG16(AX) = 0x01;
12628: m_CF = 1;
12629: break;
12630: default:
12631: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
12632: REG16(AX) = 0x01;
12633: m_CF = 1;
12634: break;
12635: }
12636: }
12637:
1.1.1.42 root 12638: inline void msdos_int_21h_5eh()
12639: {
12640: switch(REG8(AL)) {
12641: case 0x00:
12642: {
12643: char name[256] = {0};
12644: DWORD dwSize = 256;
12645:
1.1.1.60! root 12646: if(GetComputerNameA(name, &dwSize)) {
1.1.1.42 root 12647: char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
12648: for(int i = 0; i < 15; i++) {
12649: dest[i] = (i < strlen(name)) ? name[i] : ' ';
12650: }
12651: dest[15] = '\0';
12652: REG8(CH) = 0x01; // nonzero valid
12653: REG8(CL) = 0x01; // NetBIOS number for machine name ???
12654: } else {
12655: REG16(AX) = 0x01;
12656: m_CF = 1;
12657: }
12658: }
12659: break;
12660: default:
1.1.1.45 root 12661: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
12662: // REG16(AX) = 0x01;
12663: REG16(AX) = 0x49; // network software not installed
1.1.1.42 root 12664: m_CF = 1;
12665: break;
12666: }
12667: }
12668:
1.1.1.30 root 12669: inline void msdos_int_21h_5fh()
12670: {
12671: switch(REG8(AL)) {
1.1.1.42 root 12672: case 0x05:
1.1.1.44 root 12673: REG16(BP) = 0;
12674: for(int i = 0; i < 26; i++) {
12675: if(msdos_is_remote_drive(i)) {
12676: REG16(BP)++;
1.1.1.42 root 12677: }
12678: }
1.1.1.30 root 12679: case 0x02:
1.1.1.44 root 12680: for(int i = 0, index = 0; i < 26; i++) {
12681: if(msdos_is_remote_drive(i)) {
12682: if(index == REG16(BX)) {
12683: char volume[] = "A:";
1.1.1.30 root 12684: volume[0] = 'A' + i;
1.1.1.44 root 12685: DWORD dwSize = 128;
12686: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60! root 12687: WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44 root 12688: REG8(BH) = 0x00; // valid
12689: REG8(BL) = 0x04; // disk drive
12690: REG16(CX) = 0x00; // LANtastic
12691: return;
1.1.1.30 root 12692: }
1.1.1.44 root 12693: index++;
1.1.1.30 root 12694: }
12695: }
12696: REG16(AX) = 0x12; // no more files
12697: m_CF = 1;
12698: break;
1.1.1.44 root 12699: case 0x07:
12700: if(msdos_is_valid_drive(REG8(DL))) {
12701: msdos_cds_update(REG8(DL));
12702: } else {
12703: REG16(AX) = 0x0f; // invalid drive
12704: m_CF = 1;
12705: }
12706: break;
12707: case 0x08:
12708: if(msdos_is_valid_drive(REG8(DL))) {
12709: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
12710: cds->drive_attrib = 0x0000;
12711: } else {
12712: REG16(AX) = 0x0f; // invalid drive
12713: m_CF = 1;
12714: }
12715: break;
1.1.1.30 root 12716: default:
1.1.1.45 root 12717: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
12718: // REG16(AX) = 0x01;
12719: REG16(AX) = 0x49; // network software not installed
1.1.1.30 root 12720: m_CF = 1;
12721: break;
12722: }
12723: }
12724:
1.1 root 12725: inline void msdos_int_21h_60h(int lfn)
12726: {
1.1.1.45 root 12727: char full[MAX_PATH];
12728: const char *path = NULL;
1.1.1.14 root 12729:
1.1 root 12730: if(lfn) {
1.1.1.14 root 12731: char *name;
12732: *full = '\0';
1.1.1.60! root 12733: GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14 root 12734: switch(REG8(CL)) {
12735: case 1:
1.1.1.60! root 12736: GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14 root 12737: my_strupr(full);
12738: break;
12739: case 2:
1.1.1.60! root 12740: GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14 root 12741: break;
12742: }
12743: path = full;
12744: } else {
12745: path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
12746: }
12747: if(*path != '\0') {
12748: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1 root 12749: } else {
1.1.1.14 root 12750: REG16(AX) = (UINT16)GetLastError();
12751: m_CF = 1;
1.1 root 12752: }
12753: }
12754:
12755: inline void msdos_int_21h_61h()
12756: {
12757: REG8(AL) = 0;
12758: }
12759:
12760: inline void msdos_int_21h_62h()
12761: {
12762: REG16(BX) = current_psp;
12763: }
12764:
12765: inline void msdos_int_21h_63h()
12766: {
12767: switch(REG8(AL)) {
12768: case 0x00:
1.1.1.3 root 12769: SREG(DS) = (DBCS_TABLE >> 4);
12770: i386_load_segment_descriptor(DS);
1.1 root 12771: REG16(SI) = (DBCS_TABLE & 0x0f);
12772: REG8(AL) = 0x00;
12773: break;
1.1.1.22 root 12774: case 0x01: // set korean input mode
12775: case 0x02: // get korean input mode
12776: REG8(AL) = 0xff; // not supported
12777: break;
1.1 root 12778: default:
1.1.1.22 root 12779: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 12780: REG16(AX) = 0x01;
1.1.1.3 root 12781: m_CF = 1;
1.1 root 12782: break;
12783: }
12784: }
12785:
1.1.1.25 root 12786: UINT16 get_extended_country_info(UINT8 func)
1.1 root 12787: {
1.1.1.25 root 12788: switch(func) {
1.1.1.17 root 12789: case 0x01:
12790: if(REG16(CX) >= 5) {
1.1.1.19 root 12791: UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17 root 12792: if(REG16(CX) > sizeof(data)) // cx = actual transfer size
12793: REG16(CX) = sizeof(data);
12794: ZeroMemory(data, sizeof(data));
12795: data[0] = 0x01;
12796: *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19 root 12797: *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17 root 12798: *(UINT16 *)(data + 5) = active_code_page;
12799: memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25 root 12800: // REG16(AX) = active_code_page;
1.1.1.17 root 12801: } else {
1.1.1.25 root 12802: return(0x08); // insufficient memory
1.1.1.17 root 12803: }
12804: break;
12805: case 0x02:
12806: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
12807: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
12808: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25 root 12809: // REG16(AX) = active_code_page;
1.1.1.17 root 12810: REG16(CX) = 0x05;
12811: break;
1.1.1.23 root 12812: case 0x03:
12813: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
12814: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
12815: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25 root 12816: // REG16(AX) = active_code_page;
1.1.1.23 root 12817: REG16(CX) = 0x05;
12818: break;
1.1.1.17 root 12819: case 0x04:
12820: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
12821: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
12822: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25 root 12823: // REG16(AX) = active_code_page;
1.1.1.17 root 12824: REG16(CX) = 0x05;
12825: break;
12826: case 0x05:
12827: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
12828: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
12829: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25 root 12830: // REG16(AX) = active_code_page;
1.1.1.17 root 12831: REG16(CX) = 0x05;
12832: break;
12833: case 0x06:
12834: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
12835: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
12836: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25 root 12837: // REG16(AX) = active_code_page;
1.1.1.17 root 12838: REG16(CX) = 0x05;
12839: break;
1.1 root 12840: case 0x07:
1.1.1.3 root 12841: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
12842: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
12843: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25 root 12844: // REG16(AX) = active_code_page;
1.1 root 12845: REG16(CX) = 0x05;
12846: break;
1.1.1.25 root 12847: default:
12848: return(0x01); // function number invalid
12849: }
12850: return(0x00);
12851: }
12852:
12853: inline void msdos_int_21h_65h()
12854: {
12855: char tmp[0x10000];
12856:
12857: switch(REG8(AL)) {
1.1.1.43 root 12858: case 0x00:
12859: if(REG16(CX) >= 7) {
12860: set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
12861: REG16(AX) = system_code_page;
12862: } else {
12863: REG16(AX) = 0x0c;
12864: m_CF = 1;
12865: }
12866: break;
1.1.1.25 root 12867: case 0x01:
12868: case 0x02:
12869: case 0x03:
12870: case 0x04:
12871: case 0x05:
12872: case 0x06:
12873: case 0x07:
12874: {
12875: UINT16 result = get_extended_country_info(REG8(AL));
12876: if(result) {
12877: REG16(AX) = result;
12878: m_CF = 1;
12879: } else {
12880: REG16(AX) = active_code_page; // FIXME: is this correct???
12881: }
12882: }
12883: break;
1.1 root 12884: case 0x20:
1.1.1.25 root 12885: case 0xa0:
1.1.1.19 root 12886: memset(tmp, 0, sizeof(tmp));
12887: tmp[0] = REG8(DL);
1.1 root 12888: my_strupr(tmp);
12889: REG8(DL) = tmp[0];
12890: break;
12891: case 0x21:
1.1.1.25 root 12892: case 0xa1:
1.1 root 12893: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 12894: memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 12895: my_strupr(tmp);
1.1.1.3 root 12896: memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1 root 12897: break;
12898: case 0x22:
1.1.1.25 root 12899: case 0xa2:
1.1.1.3 root 12900: my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1 root 12901: break;
1.1.1.25 root 12902: case 0x23:
12903: // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45 root 12904: if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25 root 12905: // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45 root 12906: REG16(AX) = 0x00;
12907: } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
12908: // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25 root 12909: REG16(AX) = 0x01;
12910: } else {
12911: REG16(AX) = 0x02;
12912: }
12913: break;
1.1 root 12914: default:
1.1.1.22 root 12915: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 12916: REG16(AX) = 0x01;
1.1.1.3 root 12917: m_CF = 1;
1.1 root 12918: break;
12919: }
12920: }
12921:
12922: inline void msdos_int_21h_66h()
12923: {
12924: switch(REG8(AL)) {
12925: case 0x01:
12926: REG16(BX) = active_code_page;
12927: REG16(DX) = system_code_page;
12928: break;
12929: case 0x02:
12930: if(active_code_page == REG16(BX)) {
12931: REG16(AX) = 0xeb41;
12932: } else if(_setmbcp(REG16(BX)) == 0) {
12933: active_code_page = REG16(BX);
1.1.1.17 root 12934: msdos_nls_tables_update();
1.1 root 12935: REG16(AX) = 0xeb41;
1.1.1.32 root 12936: SetConsoleCP(active_code_page);
12937: SetConsoleOutputCP(active_code_page);
1.1 root 12938: } else {
12939: REG16(AX) = 0x25;
1.1.1.3 root 12940: m_CF = 1;
1.1 root 12941: }
12942: break;
12943: default:
1.1.1.22 root 12944: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 12945: REG16(AX) = 0x01;
1.1.1.3 root 12946: m_CF = 1;
1.1 root 12947: break;
12948: }
12949: }
12950:
12951: inline void msdos_int_21h_67h()
12952: {
12953: process_t *process = msdos_process_info_get(current_psp);
12954:
12955: if(REG16(BX) <= MAX_FILES) {
12956: process->max_files = max(REG16(BX), 20);
12957: } else {
12958: REG16(AX) = 0x08;
1.1.1.3 root 12959: m_CF = 1;
1.1 root 12960: }
12961: }
12962:
12963: inline void msdos_int_21h_68h()
12964: {
12965: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 12966: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 12967:
1.1.1.20 root 12968: if(fd < process->max_files && file_handler[fd].valid) {
12969: // fflush(_fdopen(fd, ""));
1.1 root 12970: } else {
12971: REG16(AX) = 0x06;
1.1.1.3 root 12972: m_CF = 1;
1.1 root 12973: }
12974: }
12975:
12976: inline void msdos_int_21h_69h()
12977: {
1.1.1.3 root 12978: drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12979: char path[] = "A:\\";
12980: char volume_label[MAX_PATH];
12981: DWORD serial_number = 0;
12982: char file_system[MAX_PATH];
12983:
12984: if(REG8(BL) == 0) {
12985: path[0] = 'A' + _getdrive() - 1;
12986: } else {
12987: path[0] = 'A' + REG8(BL) - 1;
12988: }
12989:
12990: switch(REG8(AL)) {
12991: case 0x00:
1.1.1.60! root 12992: if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1 root 12993: info->info_level = 0;
12994: info->serial_number = serial_number;
12995: memset(info->volume_label, 0x20, 11);
12996: memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
12997: memset(info->file_system, 0x20, 8);
12998: memcpy(info->file_system, file_system, min(strlen(file_system), 8));
12999: } else {
13000: REG16(AX) = errno;
1.1.1.3 root 13001: m_CF = 1;
1.1 root 13002: }
13003: break;
13004: case 0x01:
13005: REG16(AX) = 0x03;
1.1.1.3 root 13006: m_CF = 1;
1.1.1.45 root 13007: break;
13008: default:
13009: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
13010: REG16(AX) = 0x01;
13011: m_CF = 1;
13012: break;
1.1 root 13013: }
13014: }
13015:
13016: inline void msdos_int_21h_6ah()
13017: {
13018: REG8(AH) = 0x68;
13019: msdos_int_21h_68h();
13020: }
13021:
13022: inline void msdos_int_21h_6bh()
13023: {
1.1.1.45 root 13024: REG8(AL) = 0x00;
1.1 root 13025: }
13026:
13027: inline void msdos_int_21h_6ch(int lfn)
13028: {
1.1.1.45 root 13029: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1 root 13030: int mode = REG8(BL) & 0x03;
13031:
13032: if(mode < 0x03) {
1.1.1.29 root 13033: if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1 root 13034: // file exists
13035: if(REG8(DL) & 1) {
1.1.1.37 root 13036: int fd = -1;
13037: int sio_port = 0;
13038: int lpt_port = 0;
1.1 root 13039:
1.1.1.45 root 13040: if(msdos_is_device_path(path)) {
13041: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11 root 13042: } else {
1.1.1.13 root 13043: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 13044: }
1.1 root 13045: if(fd != -1) {
13046: REG16(AX) = fd;
13047: REG16(CX) = 1;
1.1.1.45 root 13048: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 13049: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 13050: } else {
13051: REG16(AX) = errno;
1.1.1.3 root 13052: m_CF = 1;
1.1 root 13053: }
13054: } else if(REG8(DL) & 2) {
1.1.1.60! root 13055: int attr = GetFileAttributesA(path);
1.1.1.37 root 13056: int fd = -1;
13057: int sio_port = 0;
13058: int lpt_port = 0;
1.1 root 13059:
1.1.1.45 root 13060: if(msdos_is_device_path(path)) {
13061: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1 root 13062: } else {
13063: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
13064: }
13065: if(fd != -1) {
13066: if(attr == -1) {
13067: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
13068: }
1.1.1.60! root 13069: SetFileAttributesA(path, attr);
1.1 root 13070: REG16(AX) = fd;
13071: REG16(CX) = 3;
1.1.1.45 root 13072: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 13073: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 13074: } else {
13075: REG16(AX) = errno;
1.1.1.3 root 13076: m_CF = 1;
1.1 root 13077: }
13078: } else {
13079: REG16(AX) = 0x50;
1.1.1.3 root 13080: m_CF = 1;
1.1 root 13081: }
13082: } else {
13083: // file not exists
13084: if(REG8(DL) & 0x10) {
13085: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
13086:
13087: if(fd != -1) {
1.1.1.60! root 13088: SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1 root 13089: REG16(AX) = fd;
13090: REG16(CX) = 2;
13091: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 13092: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 13093: } else {
13094: REG16(AX) = errno;
1.1.1.3 root 13095: m_CF = 1;
1.1 root 13096: }
13097: } else {
13098: REG16(AX) = 0x02;
1.1.1.3 root 13099: m_CF = 1;
1.1 root 13100: }
13101: }
13102: } else {
13103: REG16(AX) = 0x0c;
1.1.1.3 root 13104: m_CF = 1;
1.1 root 13105: }
13106: }
13107:
1.1.1.43 root 13108: inline void msdos_int_21h_70h()
13109: {
13110: switch(REG8(AL)) {
1.1.1.48 root 13111: case 0x00: // get ??? info
13112: case 0x01: // set above info
13113: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
13114: REG16(AX) = 0x7000;
13115: m_CF = 1;
13116: break;
13117: case 0x02: // set general internationalization info
1.1.1.43 root 13118: if(REG16(CX) >= 7) {
13119: active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
13120: msdos_nls_tables_update();
13121: set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
13122: REG16(AX) = system_code_page;
13123: } else {
13124: REG16(AX) = 0x0c;
13125: m_CF = 1;
13126: }
13127: break;
13128: default:
13129: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 13130: REG16(AX) = 0x7000;
1.1.1.43 root 13131: m_CF = 1;
13132: break;
13133: }
13134: }
13135:
1.1 root 13136: inline void msdos_int_21h_710dh()
13137: {
13138: // reset drive
13139: }
13140:
1.1.1.48 root 13141: inline void msdos_int_21h_7141h()
1.1.1.17 root 13142: {
13143: if(REG16(SI) == 0) {
1.1.1.48 root 13144: msdos_int_21h_41h(1);
1.1.1.17 root 13145: return;
13146: }
13147: if(REG16(SI) != 1) {
13148: REG16(AX) = 5;
13149: m_CF = 1;
13150: }
13151: /* wild card and matching attributes... */
13152: char tmp[MAX_PATH * 2];
13153: // copy search pathname (and quick check overrun)
13154: ZeroMemory(tmp, sizeof(tmp));
13155: tmp[MAX_PATH - 1] = '\0';
13156: tmp[MAX_PATH] = 1;
13157: strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
13158:
13159: if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
13160: REG16(AX) = 1;
13161: m_CF = 1;
13162: return;
13163: }
13164: for(char *s = tmp; *s; ++s) {
13165: if(*s == '/') {
13166: *s = '\\';
13167: }
13168: }
1.1.1.60! root 13169: char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17 root 13170: if(tmp_name) {
13171: ++tmp_name;
13172: } else {
13173: tmp_name = strchr(tmp, ':');
13174: tmp_name = tmp_name ? tmp_name + 1 : tmp;
13175: }
13176:
13177: WIN32_FIND_DATAA fd;
13178: HANDLE fh = FindFirstFileA(tmp, &fd);
13179: if(fh == INVALID_HANDLE_VALUE) {
13180: REG16(AX) = 2;
13181: m_CF = 1;
13182: return;
13183: }
13184: do {
13185: if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
13186: strcpy(tmp_name, fd.cFileName);
1.1.1.48 root 13187: if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17 root 13188: REG16(AX) = 5;
13189: m_CF = 1;
13190: break;
13191: }
13192: }
13193: } while(FindNextFileA(fh, &fd));
13194: if(!m_CF) {
13195: if(GetLastError() != ERROR_NO_MORE_FILES) {
13196: m_CF = 1;
13197: REG16(AX) = 2;
13198: }
13199: }
13200: FindClose(fh);
13201: }
13202:
1.1 root 13203: inline void msdos_int_21h_714eh()
13204: {
13205: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 13206: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
13207: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60! root 13208: WIN32_FIND_DATAA fd;
1.1 root 13209:
1.1.1.13 root 13210: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
13211: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
13212: FindClose(dtainfo->find_handle);
13213: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 13214: }
13215: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 13216: dtainfo->allowable_mask = REG8(CL);
13217: dtainfo->required_mask = REG8(CH);
13218: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 13219:
1.1.1.14 root 13220: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
13221: dtainfo->allowable_mask &= ~8;
1.1 root 13222: }
1.1.1.60! root 13223: if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14 root 13224: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60! root 13225: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 13226: FindClose(dtainfo->find_handle);
13227: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 13228: break;
13229: }
13230: }
13231: }
1.1.1.13 root 13232: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 13233: find->attrib = fd.dwFileAttributes;
13234: msdos_find_file_conv_local_time(&fd);
13235: if(REG16(SI) == 0) {
13236: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
13237: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
13238: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
13239: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
13240: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
13241: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
13242: } else {
13243: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
13244: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
13245: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
13246: }
13247: find->size_hi = fd.nFileSizeHigh;
13248: find->size_lo = fd.nFileSizeLow;
13249: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 13250: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 13251: REG16(AX) = dtainfo - dtalist + 1;
13252: } else if(dtainfo->allowable_mask & 8) {
1.1 root 13253: // volume label
13254: find->attrib = 8;
13255: find->size_hi = find->size_lo = 0;
13256: strcpy(find->full_name, process->volume_label);
13257: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 13258: dtainfo->allowable_mask &= ~8;
13259: REG16(AX) = dtainfo - dtalist + 1;
1.1 root 13260: } else {
13261: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 13262: m_CF = 1;
1.1 root 13263: }
13264: }
13265:
13266: inline void msdos_int_21h_714fh()
13267: {
13268: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 13269: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60! root 13270: WIN32_FIND_DATAA fd;
1.1 root 13271:
1.1.1.14 root 13272: if(REG16(BX) - 1u >= MAX_DTAINFO) {
13273: REG16(AX) = 6;
1.1.1.13 root 13274: m_CF = 1;
13275: return;
13276: }
1.1.1.14 root 13277: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 13278: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60! root 13279: if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14 root 13280: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60! root 13281: if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13 root 13282: FindClose(dtainfo->find_handle);
13283: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 13284: break;
13285: }
13286: }
13287: } else {
1.1.1.13 root 13288: FindClose(dtainfo->find_handle);
13289: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 13290: }
13291: }
1.1.1.13 root 13292: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 13293: find->attrib = fd.dwFileAttributes;
13294: msdos_find_file_conv_local_time(&fd);
13295: if(REG16(SI) == 0) {
13296: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
13297: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
13298: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
13299: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
13300: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
13301: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
13302: } else {
13303: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
13304: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
13305: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
13306: }
13307: find->size_hi = fd.nFileSizeHigh;
13308: find->size_lo = fd.nFileSizeLow;
13309: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 13310: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 13311: } else if(dtainfo->allowable_mask & 8) {
1.1 root 13312: // volume label
13313: find->attrib = 8;
13314: find->size_hi = find->size_lo = 0;
13315: strcpy(find->full_name, process->volume_label);
13316: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 13317: dtainfo->allowable_mask &= ~8;
1.1 root 13318: } else {
13319: REG16(AX) = 0x12;
1.1.1.3 root 13320: m_CF = 1;
1.1 root 13321: }
13322: }
13323:
13324: inline void msdos_int_21h_71a0h()
13325: {
13326: DWORD max_component_len, file_sys_flag;
13327:
1.1.1.60! root 13328: if(GetVolumeInformationA((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, REG16(CX) == 0 ? NULL : (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
1.1.1.14 root 13329: REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
13330: REG16(BX) |= 0x4000; // supports LFN functions
1.1 root 13331: REG16(CX) = (UINT16)max_component_len; // 255
13332: REG16(DX) = (UINT16)max_component_len + 5; // 260
13333: } else {
13334: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 13335: m_CF = 1;
1.1 root 13336: }
13337: }
13338:
13339: inline void msdos_int_21h_71a1h()
13340: {
1.1.1.14 root 13341: if(REG16(BX) - 1u >= MAX_DTAINFO) {
13342: REG16(AX) = 6;
1.1.1.13 root 13343: m_CF = 1;
13344: return;
13345: }
1.1.1.14 root 13346: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 13347: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
13348: FindClose(dtainfo->find_handle);
13349: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 13350: }
13351: }
13352:
13353: inline void msdos_int_21h_71a6h()
13354: {
13355: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 13356: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
13357:
1.1.1.3 root 13358: UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 13359: struct _stat64 status;
13360: DWORD serial_number = 0;
13361:
1.1.1.20 root 13362: if(fd < process->max_files && file_handler[fd].valid) {
13363: if(_fstat64(fd, &status) == 0) {
13364: if(file_handler[fd].path[1] == ':') {
1.1 root 13365: // NOTE: we need to consider the network file path "\\host\share\"
13366: char volume[] = "A:\\";
1.1.1.20 root 13367: volume[0] = file_handler[fd].path[1];
1.1.1.60! root 13368: GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1 root 13369: }
1.1.1.60! root 13370: *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1 root 13371: *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
13372: *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
13373: *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
13374: *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
13375: *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
13376: *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
13377: *(UINT32 *)(buffer + 0x1c) = serial_number;
13378: *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
13379: *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
13380: *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14 root 13381: // this is dummy id and it will be changed when it is reopened...
1.1 root 13382: *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20 root 13383: *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1 root 13384: } else {
13385: REG16(AX) = errno;
1.1.1.3 root 13386: m_CF = 1;
1.1 root 13387: }
13388: } else {
13389: REG16(AX) = 0x06;
1.1.1.3 root 13390: m_CF = 1;
1.1 root 13391: }
13392: }
13393:
13394: inline void msdos_int_21h_71a7h()
13395: {
13396: switch(REG8(BL)) {
13397: case 0x00:
1.1.1.3 root 13398: if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), ®16(DX), ®16(CX))) {
1.1 root 13399: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 13400: m_CF = 1;
1.1 root 13401: }
13402: break;
13403: case 0x01:
13404: // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3 root 13405: if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1 root 13406: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 13407: m_CF = 1;
1.1 root 13408: }
13409: break;
13410: default:
1.1.1.22 root 13411: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 13412: REG16(AX) = 0x7100;
1.1.1.3 root 13413: m_CF = 1;
1.1 root 13414: break;
13415: }
13416: }
13417:
13418: inline void msdos_int_21h_71a8h()
13419: {
13420: if(REG8(DH) == 0) {
13421: char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3 root 13422: strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 13423: memset(fcb, 0x20, sizeof(fcb));
13424: int len = strlen(tmp);
1.1.1.21 root 13425: for(int i = 0, pos = 0; i < len; i++) {
1.1 root 13426: if(tmp[i] == '.') {
13427: pos = 8;
13428: } else {
13429: if(msdos_lead_byte_check(tmp[i])) {
13430: fcb[pos++] = tmp[i++];
13431: }
13432: fcb[pos++] = tmp[i];
13433: }
13434: }
1.1.1.3 root 13435: memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1 root 13436: } else {
1.1.1.3 root 13437: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 13438: }
13439: }
13440:
1.1.1.22 root 13441: inline void msdos_int_21h_71aah()
13442: {
13443: char drv[] = "A:", path[MAX_PATH];
13444: char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
13445:
13446: if(REG8(BL) == 0) {
13447: drv[0] = 'A' + _getdrive() - 1;
13448: } else {
13449: drv[0] = 'A' + REG8(BL) - 1;
13450: }
13451: switch(REG8(BH)) {
13452: case 0x00:
1.1.1.44 root 13453: if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
13454: REG16(AX) = 0x0f; // invalid drive
13455: m_CF = 1;
1.1.1.60! root 13456: } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44 root 13457: REG16(AX) = 0x03; // path not found
1.1.1.22 root 13458: m_CF = 1;
13459: }
13460: break;
13461: case 0x01:
1.1.1.44 root 13462: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
13463: REG16(AX) = 0x0f; // invalid drive
13464: m_CF = 1;
1.1.1.60! root 13465: } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22 root 13466: REG16(AX) = 0x0f; // invalid drive
13467: m_CF = 1;
13468: }
13469: break;
13470: case 0x02:
1.1.1.44 root 13471: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
13472: REG16(AX) = 0x0f; // invalid drive
13473: m_CF = 1;
1.1.1.60! root 13474: } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22 root 13475: REG16(AX) = 0x0f; // invalid drive
13476: m_CF = 1;
13477: } else if(strncmp(path, "\\??\\", 4) != 0) {
13478: REG16(AX) = 0x0f; // invalid drive
13479: m_CF = 1;
13480: } else {
13481: strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
13482: }
13483: break;
13484: default:
13485: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48 root 13486: REG16(AX) = 0x7100;
1.1.1.22 root 13487: m_CF = 1;
13488: break;
13489: }
13490: }
13491:
1.1.1.14 root 13492: inline void msdos_int_21h_7300h()
13493: {
1.1.1.44 root 13494: REG8(AL) = REG8(CL);
13495: REG8(AH) = 0;
1.1.1.14 root 13496: }
13497:
13498: inline void msdos_int_21h_7302h()
13499: {
13500: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
13501: UINT16 seg, ofs;
13502:
13503: if(REG16(CX) < 0x3f) {
13504: REG8(AL) = 0x18;
13505: m_CF = 1;
13506: } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
13507: REG8(AL) = 0xff;
13508: m_CF = 1;
13509: } else {
13510: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
13511: }
13512: }
13513:
1.1 root 13514: inline void msdos_int_21h_7303h()
13515: {
1.1.1.3 root 13516: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
13517: ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 13518: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
13519:
1.1.1.60! root 13520: if(GetDiskFreeSpaceA(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1 root 13521: info->size_of_structure = sizeof(ext_space_info_t);
13522: info->structure_version = 0;
13523: info->sectors_per_cluster = sectors_per_cluster;
13524: info->bytes_per_sector = bytes_per_sector;
13525: info->available_clusters_on_drive = free_clusters;
13526: info->total_clusters_on_drive = total_clusters;
13527: info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
13528: info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
13529: info->available_allocation_units = free_clusters; // ???
13530: info->total_allocation_units = total_clusters; // ???
13531: } else {
13532: REG16(AX) = errno;
1.1.1.3 root 13533: m_CF = 1;
1.1 root 13534: }
13535: }
13536:
1.1.1.30 root 13537: inline void msdos_int_21h_dbh()
13538: {
13539: // Novell NetWare - Workstation - Get Number of Local Drives
13540: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
13541: REG8(AL) = dos_info->last_drive;
13542: }
13543:
13544: inline void msdos_int_21h_dch()
13545: {
13546: // Novell NetWare - Connection Services - Get Connection Number
13547: REG8(AL) = 0x00;
13548: }
13549:
1.1.1.32 root 13550: inline void msdos_int_24h()
13551: {
13552: const char *message = NULL;
13553: int key = 0;
13554:
13555: for(int i = 0; i < array_length(critical_error_table); i++) {
13556: if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
13557: if(active_code_page == 932) {
13558: message = critical_error_table[i].message_japanese;
13559: }
13560: if(message == NULL) {
13561: message = critical_error_table[i].message_english;
13562: }
13563: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
13564: strcpy((char *)(mem + WORK_TOP + 1), message);
13565:
13566: SREG(ES) = WORK_TOP >> 4;
13567: i386_load_segment_descriptor(ES);
13568: REG16(DI) = 0x0000;
13569: break;
13570: }
13571: }
13572: fprintf(stderr, "\n%s", message);
13573: if(!(REG8(AH) & 0x80)) {
13574: if(REG8(AH) & 0x01) {
13575: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
13576: } else {
13577: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
13578: }
13579: }
13580: fprintf(stderr, "\n");
13581:
1.1.1.33 root 13582: {
1.1.1.32 root 13583: fprintf(stderr, "%s", (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33 root 13584: }
1.1.1.32 root 13585: if(REG8(AH) & 0x10) {
13586: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (R)" : "Retry");
13587: }
13588: if(REG8(AH) & 0x20) {
13589: fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
13590: }
13591: if(REG8(AH) & 0x08) {
13592: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
13593: }
13594: fprintf(stderr, "? ");
13595:
13596: while(1) {
13597: while(!_kbhit()) {
13598: Sleep(10);
13599: }
13600: key = _getch();
13601:
13602: if(key == 'I' || key == 'i') {
13603: if(REG8(AH) & 0x20) {
13604: REG8(AL) = 0;
13605: break;
13606: }
13607: } else if(key == 'R' || key == 'r') {
13608: if(REG8(AH) & 0x10) {
13609: REG8(AL) = 1;
13610: break;
13611: }
13612: } else if(key == 'A' || key == 'a') {
13613: REG8(AL) = 2;
13614: break;
13615: } else if(key == 'F' || key == 'f') {
13616: if(REG8(AH) & 0x08) {
13617: REG8(AL) = 3;
13618: break;
13619: }
13620: }
13621: }
13622: fprintf(stderr, "%c\n", key);
13623: }
13624:
1.1 root 13625: inline void msdos_int_25h()
13626: {
13627: UINT16 seg, ofs;
13628: DWORD dwSize;
13629:
1.1.1.3 root 13630: #if defined(HAS_I386)
13631: I386OP(pushf)();
13632: #else
13633: PREFIX86(_pushf());
13634: #endif
1.1 root 13635:
13636: if(!(REG8(AL) < 26)) {
13637: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 13638: m_CF = 1;
1.1 root 13639: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
13640: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13641: m_CF = 1;
1.1 root 13642: } else {
13643: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
13644: char dev[64];
13645: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
13646:
1.1.1.60! root 13647: HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1 root 13648: if(hFile == INVALID_HANDLE_VALUE) {
13649: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13650: m_CF = 1;
1.1 root 13651: } else {
1.1.1.19 root 13652: UINT32 top_sector = REG16(DX);
13653: UINT16 sector_num = REG16(CX);
13654: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
13655:
13656: if(sector_num == 0xffff) {
13657: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
13658: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
13659: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
13660: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
13661: buffer_addr = (seg << 4) + ofs;
13662: }
13663: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
13664: // REG8(AL) = 0x02; // drive not ready
13665: // m_CF = 1;
13666: // } else
13667: if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 13668: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 13669: m_CF = 1;
1.1.1.19 root 13670: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 13671: REG8(AL) = 0x0b; // read error
1.1.1.3 root 13672: m_CF = 1;
1.1 root 13673: }
13674: CloseHandle(hFile);
13675: }
13676: }
13677: }
13678:
13679: inline void msdos_int_26h()
13680: {
1.1.1.42 root 13681: // this operation may cause serious damage for drives, so support only floppy disk...
1.1 root 13682: UINT16 seg, ofs;
13683: DWORD dwSize;
13684:
1.1.1.3 root 13685: #if defined(HAS_I386)
13686: I386OP(pushf)();
13687: #else
13688: PREFIX86(_pushf());
13689: #endif
1.1 root 13690:
13691: if(!(REG8(AL) < 26)) {
13692: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 13693: m_CF = 1;
1.1 root 13694: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
13695: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13696: m_CF = 1;
1.1 root 13697: } else {
13698: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
13699: char dev[64];
13700: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
13701:
13702: if(dpb->media_type == 0xf8) {
13703: // this drive is not a floppy
1.1.1.6 root 13704: // if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
13705: // fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
13706: // }
1.1 root 13707: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13708: m_CF = 1;
1.1 root 13709: } else {
1.1.1.60! root 13710: HANDLE hFile = CreateFileA(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1 root 13711: if(hFile == INVALID_HANDLE_VALUE) {
13712: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13713: m_CF = 1;
1.1 root 13714: } else {
1.1.1.19 root 13715: UINT32 top_sector = REG16(DX);
13716: UINT16 sector_num = REG16(CX);
13717: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
13718:
13719: if(sector_num == 0xffff) {
13720: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
13721: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
13722: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
13723: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
13724: buffer_addr = (seg << 4) + ofs;
13725: }
1.1 root 13726: if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
13727: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13728: m_CF = 1;
1.1.1.19 root 13729: } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 13730: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 13731: m_CF = 1;
1.1.1.19 root 13732: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 13733: REG8(AL) = 0x0a; // write error
1.1.1.3 root 13734: m_CF = 1;
1.1 root 13735: }
13736: CloseHandle(hFile);
13737: }
13738: }
13739: }
13740: }
13741:
13742: inline void msdos_int_27h()
13743: {
1.1.1.29 root 13744: int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
13745: try {
13746: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
13747: } catch(...) {
13748: // recover the broken mcb
13749: int mcb_seg = SREG(CS) - 1;
13750: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 13751:
1.1.1.29 root 13752: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 13753: mcb->mz = 'M';
13754: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
13755:
1.1.1.29 root 13756: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 13757: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 13758: } else {
1.1.1.39 root 13759: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 13760: }
13761: } else {
13762: mcb->mz = 'Z';
1.1.1.30 root 13763: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 13764: }
13765: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
13766: }
1.1.1.3 root 13767: msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1 root 13768: }
13769:
13770: inline void msdos_int_29h()
13771: {
1.1.1.50 root 13772: msdos_putch_fast(REG8(AL));
1.1 root 13773: }
13774:
13775: inline void msdos_int_2eh()
13776: {
13777: char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
13778: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 13779: strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1 root 13780: char *token = my_strtok(tmp, " ");
13781: strcpy(command, token);
13782: strcpy(opt, token + strlen(token) + 1);
13783:
13784: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
13785: param->env_seg = 0;
13786: param->cmd_line.w.l = 44;
13787: param->cmd_line.w.h = (WORK_TOP >> 4);
13788: param->fcb1.w.l = 24;
13789: param->fcb1.w.h = (WORK_TOP >> 4);
13790: param->fcb2.w.l = 24;
13791: param->fcb2.w.h = (WORK_TOP >> 4);
13792:
13793: memset(mem + WORK_TOP + 24, 0x20, 20);
13794:
13795: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
13796: cmd_line->len = strlen(opt);
13797: strcpy(cmd_line->cmd, opt);
13798: cmd_line->cmd[cmd_line->len] = 0x0d;
13799:
1.1.1.28 root 13800: try {
13801: if(msdos_process_exec(command, param, 0)) {
13802: REG16(AX) = 0xffff; // error before processing command
13803: } else {
13804: // set flag to set retval to ax when the started process is terminated
13805: process_t *process = msdos_process_info_get(current_psp);
13806: process->called_by_int2eh = true;
13807: }
13808: } catch(...) {
13809: REG16(AX) = 0xffff; // error before processing command
13810: }
1.1 root 13811: }
13812:
1.1.1.29 root 13813: inline void msdos_int_2fh_05h()
13814: {
13815: switch(REG8(AL)) {
13816: case 0x00:
1.1.1.49 root 13817: // critical error handler is installed
1.1.1.32 root 13818: REG8(AL) = 0xff;
13819: break;
13820: case 0x01:
13821: case 0x02:
13822: for(int i = 0; i < array_length(standard_error_table); i++) {
13823: if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
13824: const char *message = NULL;
13825: if(active_code_page == 932) {
13826: message = standard_error_table[i].message_japanese;
13827: }
13828: if(message == NULL) {
13829: message = standard_error_table[i].message_english;
13830: }
13831: strcpy((char *)(mem + WORK_TOP), message);
13832:
13833: SREG(ES) = WORK_TOP >> 4;
13834: i386_load_segment_descriptor(ES);
13835: REG16(DI) = 0x0000;
13836: REG8(AL) = 0x01;
13837: break;
13838: }
13839: }
1.1.1.29 root 13840: break;
13841: default:
13842: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.49 root 13843: REG16(AX) = 0x01;
1.1.1.29 root 13844: m_CF = 1;
13845: }
13846: }
13847:
1.1.1.44 root 13848: inline void msdos_int_2fh_06h()
13849: {
13850: switch(REG8(AL)) {
13851: case 0x00:
13852: // ASSIGN is not installed
1.1.1.49 root 13853: // REG8(AL) = 0x00;
1.1.1.44 root 13854: break;
13855: case 0x01:
13856: // this call is available from within MIRROR.COM even if ASSIGN is not installed
13857: REG16(AX) = 0x01;
13858: m_CF = 1;
13859: break;
13860: default:
13861: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
13862: REG16(AX) = 0x01;
13863: m_CF = 1;
13864: break;
13865: }
13866: }
13867:
1.1.1.22 root 13868: inline void msdos_int_2fh_11h()
13869: {
13870: switch(REG8(AL)) {
13871: case 0x00:
1.1.1.29 root 13872: if(i386_read_stack() == 0xdada) {
1.1.1.53 root 13873: #ifdef SUPPORT_MSCDEX
13874: // MSCDEX is installed
13875: REG8(AL) = 0xff;
13876: i386_write_stack(0xadad);
13877: #else
1.1.1.29 root 13878: // MSCDEX is not installed
13879: // REG8(AL) = 0x00;
1.1.1.53 root 13880: #endif
1.1.1.29 root 13881: } else {
13882: // Network Redirector is not installed
13883: // REG8(AL) = 0x00;
13884: }
1.1.1.22 root 13885: break;
13886: default:
1.1.1.43 root 13887: // unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.29 root 13888: REG16(AX) = 0x49; // network software not installed
1.1.1.22 root 13889: m_CF = 1;
13890: break;
13891: }
13892: }
13893:
1.1.1.21 root 13894: inline void msdos_int_2fh_12h()
13895: {
13896: switch(REG8(AL)) {
1.1.1.22 root 13897: case 0x00:
1.1.1.29 root 13898: // DOS 3.0+ internal functions are installed
1.1.1.22 root 13899: REG8(AL) = 0xff;
13900: break;
1.1.1.29 root 13901: // case 0x01: // DOS 3.0+ internal - Close Current File
13902: case 0x02:
13903: {
13904: UINT16 stack = i386_read_stack();
13905: REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
13906: SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
13907: i386_load_segment_descriptor(ES);
13908: }
13909: break;
1.1.1.30 root 13910: case 0x03:
13911: SREG(DS) = (DEVICE_TOP >> 4);
13912: i386_load_segment_descriptor(DS);
13913: break;
1.1.1.29 root 13914: case 0x04:
13915: {
13916: UINT16 stack = i386_read_stack();
13917: REG8(AL) = (stack == '/') ? '\\' : stack;
13918: #if defined(HAS_I386)
13919: m_ZF = (REG8(AL) == '\\');
13920: #else
13921: m_ZeroVal = (REG8(AL) != '\\');
13922: #endif
13923: }
13924: break;
13925: case 0x05:
1.1.1.49 root 13926: {
13927: UINT16 c = i386_read_stack();
13928: if((c >> 0) & 0xff) {
13929: msdos_putch((c >> 0) & 0xff);
13930: }
13931: if((c >> 8) & 0xff) {
13932: msdos_putch((c >> 8) & 0xff);
13933: }
13934: }
1.1.1.29 root 13935: break;
1.1.1.49 root 13936: // case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29 root 13937: // case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
13938: // case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49 root 13939: // case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29 root 13940: // case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
13941: // case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
13942: // case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
13943: case 0x0d:
13944: {
13945: SYSTEMTIME time;
13946: FILETIME file_time;
13947: WORD dos_date, dos_time;
13948: GetLocalTime(&time);
13949: SystemTimeToFileTime(&time, &file_time);
13950: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
13951: REG16(AX) = dos_date;
13952: REG16(DX) = dos_time;
13953: }
13954: break;
13955: // case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
13956: // case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
13957: // case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
13958: case 0x11:
13959: {
13960: char path[MAX_PATH], *p;
13961: strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
13962: my_strupr(path);
13963: while((p = my_strchr(path, '/')) != NULL) {
13964: *p = '\\';
13965: }
13966: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
13967: }
13968: break;
13969: case 0x12:
13970: REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
13971: break;
13972: case 0x13:
13973: {
13974: char tmp[2] = {0};
13975: tmp[0] = i386_read_stack();
13976: my_strupr(tmp);
13977: REG8(AL) = tmp[0];
13978: }
13979: break;
13980: case 0x14:
13981: #if defined(HAS_I386)
13982: m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
13983: #else
13984: m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
13985: #endif
13986: m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
13987: break;
13988: // case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21 root 13989: case 0x16:
13990: if(REG16(BX) < 20) {
13991: SREG(ES) = SFT_TOP >> 4;
13992: i386_load_segment_descriptor(ES);
13993: REG16(DI) = 6 + 0x3b * REG16(BX);
13994:
13995: // update system file table
13996: UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
13997: if(file_handler[REG16(BX)].valid) {
13998: int count = 0;
13999: for(int i = 0; i < 20; i++) {
14000: if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
14001: count++;
14002: }
14003: }
14004: *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
14005: *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
14006: _lseek(REG16(BX), 0, SEEK_END);
14007: *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
14008: _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
14009: } else {
14010: memset(sft, 0, 0x3b);
14011: }
14012: } else {
14013: REG16(AX) = 0x06;
14014: m_CF = 1;
14015: }
14016: break;
1.1.1.49 root 14017: case 0x17:
14018: {
14019: UINT16 drive = i386_read_stack();
14020: if(msdos_is_valid_drive(drive)) {
14021: msdos_cds_update(drive);
14022: }
14023: REG16(SI) = 88 * drive;
14024: SREG(DS) = (CDS_TOP >> 4);
14025: i386_load_segment_descriptor(DS);
14026: }
14027: break;
1.1.1.29 root 14028: // case 0x18: // DOS 3.0+ internal - Get Caller's Registers
14029: // case 0x19: // DOS 3.0+ internal - Set Drive???
14030: case 0x1a:
14031: {
14032: char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
14033: if(path[1] == ':') {
14034: if(path[0] >= 'a' && path[0] <= 'z') {
14035: REG8(AL) = path[0] - 'a' + 1;
14036: } else if(path[0] >= 'A' && path[0] <= 'Z') {
14037: REG8(AL) = path[0] - 'A' + 1;
14038: } else {
14039: REG8(AL) = 0xff; // invalid
14040: }
14041: strcpy(full, path);
14042: strcpy(path, full + 2);
1.1.1.60! root 14043: } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29 root 14044: if(full[0] >= 'a' && full[0] <= 'z') {
14045: REG8(AL) = full[0] - 'a' + 1;
14046: } else if(full[0] >= 'A' && full[0] <= 'Z') {
14047: REG8(AL) = full[0] - 'A' + 1;
14048: } else {
14049: REG8(AL) = 0xff; // invalid
14050: }
14051: } else {
14052: REG8(AL) = 0x00; // default
14053: }
14054: }
14055: break;
14056: case 0x1b:
14057: {
14058: int year = REG16(CX) + 1980;
14059: REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
14060: }
14061: break;
14062: // case 0x1c: // DOS 3.0+ internal - Check Sum Memory
14063: // case 0x1d: // DOS 3.0+ internal - Sum Memory
14064: case 0x1e:
14065: {
14066: char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
14067: char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60! root 14068: if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29 root 14069: #if defined(HAS_I386)
14070: m_ZF = (strcmp(full_1st, full_2nd) == 0);
14071: #else
14072: m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
14073: #endif
14074: } else {
14075: #if defined(HAS_I386)
14076: m_ZF = (strcmp(path_1st, path_2nd) == 0);
14077: #else
14078: m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
14079: #endif
14080: }
14081: }
14082: break;
1.1.1.49 root 14083: case 0x1f:
14084: {
14085: UINT16 drive = i386_read_stack();
14086: if(msdos_is_valid_drive(drive)) {
14087: msdos_cds_update(drive);
14088: }
14089: REG16(SI) = 88 * drive;
14090: SREG(ES) = (CDS_TOP >> 4);
14091: i386_load_segment_descriptor(ES);
14092: }
14093: break;
1.1.1.21 root 14094: case 0x20:
14095: {
14096: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
14097:
14098: if(fd < 20) {
14099: SREG(ES) = current_psp;
14100: i386_load_segment_descriptor(ES);
14101: REG16(DI) = offsetof(psp_t, file_table) + fd;
14102: } else {
14103: REG16(AX) = 0x06;
14104: m_CF = 1;
14105: }
14106: }
14107: break;
1.1.1.29 root 14108: case 0x21:
14109: msdos_int_21h_60h(0);
14110: break;
1.1.1.49 root 14111: case 0x22:
14112: {
14113: sda_t *sda = (sda_t *)(mem + SDA_TOP);
14114: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
14115: sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
14116: }
14117: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
14118: sda->error_class = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
14119: }
14120: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
14121: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
14122: }
14123: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
14124: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
14125: }
14126: }
14127: break;
1.1.1.29 root 14128: // case 0x23: // DOS 3.0+ internal - Check If Character Device
14129: // case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
14130: case 0x25:
14131: REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
14132: break;
14133: case 0x26:
14134: REG8(AL) = REG8(CL);
14135: msdos_int_21h_3dh();
14136: break;
14137: case 0x27:
14138: msdos_int_21h_3eh();
14139: break;
14140: case 0x28:
14141: REG16(AX) = REG16(BP);
14142: msdos_int_21h_42h();
14143: break;
14144: case 0x29:
14145: msdos_int_21h_3fh();
14146: break;
14147: // case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
14148: case 0x2b:
14149: REG16(AX) = REG16(BP);
14150: msdos_int_21h_44h();
14151: break;
14152: case 0x2c:
14153: REG16(BX) = DEVICE_TOP >> 4;
14154: REG16(AX) = 22;
14155: break;
14156: case 0x2d:
14157: {
14158: sda_t *sda = (sda_t *)(mem + SDA_TOP);
14159: REG16(AX) = sda->extended_error_code;
14160: }
14161: break;
14162: case 0x2e:
14163: if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32 root 14164: SREG(ES) = 0x0001;
14165: i386_load_segment_descriptor(ES);
14166: REG16(DI) = 0x00;
14167: } else if(REG8(DL) == 0x08) {
1.1.1.49 root 14168: // dummy parameter error message read routine is at fffc:0010
14169: SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22 root 14170: i386_load_segment_descriptor(ES);
1.1.1.32 root 14171: REG16(DI) = 0x0010;
1.1.1.22 root 14172: }
14173: break;
1.1.1.29 root 14174: case 0x2f:
14175: if(REG16(DX) != 0) {
1.1.1.30 root 14176: dos_major_version = REG8(DL);
14177: dos_minor_version = REG8(DH);
1.1.1.29 root 14178: } else {
14179: REG8(DL) = 7;
14180: REG8(DH) = 10;
14181: }
14182: break;
14183: // case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
14184: // case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22 root 14185: default:
14186: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14187: REG16(AX) = 0x01;
14188: m_CF = 1;
14189: break;
14190: }
14191: }
14192:
1.1.1.30 root 14193: inline void msdos_int_2fh_13h()
14194: {
14195: static UINT16 prevDS = 0, prevDX = 0;
14196: static UINT16 prevES = 0, prevBX = 0;
14197: UINT16 tmp;
14198:
14199: tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
14200: i386_load_segment_descriptor(DS);
14201: tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
14202:
14203: tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
14204: i386_load_segment_descriptor(ES);
14205: tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
14206: }
14207:
1.1.1.22 root 14208: inline void msdos_int_2fh_14h()
14209: {
14210: switch(REG8(AL)) {
14211: case 0x00:
1.1.1.29 root 14212: // NLSFUNC.COM is installed
14213: REG8(AL) = 0xff;
1.1.1.25 root 14214: break;
14215: case 0x01:
14216: case 0x03:
14217: REG8(AL) = 0x00;
14218: active_code_page = REG16(BX);
14219: msdos_nls_tables_update();
14220: break;
14221: case 0x02:
14222: REG8(AL) = get_extended_country_info(REG16(BP));
14223: break;
14224: case 0x04:
1.1.1.42 root 14225: for(int i = 0;; i++) {
14226: if(country_table[i].code == REG16(DX)) {
14227: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
14228: break;
14229: } else if(country_table[i].code == -1) {
14230: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
14231: break;
14232: }
14233: }
1.1.1.25 root 14234: REG8(AL) = 0x00;
1.1.1.22 root 14235: break;
14236: default:
14237: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14238: REG16(AX) = 0x01;
14239: m_CF = 1;
14240: break;
14241: }
14242: }
14243:
14244: inline void msdos_int_2fh_15h()
14245: {
14246: switch(REG8(AL)) {
1.1.1.29 root 14247: case 0x00: // CD-ROM - Installation Check
14248: if(REG16(BX) == 0x0000) {
1.1.1.53 root 14249: #ifdef SUPPORT_MSCDEX
1.1.1.43 root 14250: // MSCDEX is installed
14251: REG16(BX) = 0;
14252: for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44 root 14253: if(msdos_is_cdrom_drive(i)) {
14254: if(REG16(BX) == 0) {
14255: REG16(CX) = i;
1.1.1.43 root 14256: }
1.1.1.44 root 14257: REG16(BX)++;
1.1.1.43 root 14258: }
14259: }
14260: #else
1.1.1.29 root 14261: // MSCDEX is not installed
14262: // REG8(AL) = 0x00;
1.1.1.43 root 14263: #endif
1.1.1.29 root 14264: } else {
14265: // GRAPHICS.COM is not installed
14266: // REG8(AL) = 0x00;
14267: }
1.1.1.22 root 14268: break;
1.1.1.43 root 14269: case 0x0b:
1.1.1.44 root 14270: // this call is available from within DOSSHELL even if MSCDEX is not installed
14271: REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
14272: REG16(BX) = 0xadad;
1.1.1.43 root 14273: break;
14274: case 0x0d:
1.1.1.44 root 14275: for(int i = 0, n = 0; i < 26; i++) {
14276: if(msdos_is_cdrom_drive(i)) {
14277: mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43 root 14278: }
14279: }
14280: break;
1.1.1.22 root 14281: case 0xff:
1.1.1.29 root 14282: if(REG16(BX) == 0x0000) {
14283: // CORELCDX is not installed
14284: } else {
14285: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14286: REG16(AX) = 0x01;
14287: m_CF = 1;
14288: }
1.1.1.22 root 14289: break;
1.1.1.21 root 14290: default:
1.1.1.22 root 14291: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.21 root 14292: REG16(AX) = 0x01;
14293: m_CF = 1;
14294: break;
14295: }
14296: }
14297:
1.1 root 14298: inline void msdos_int_2fh_16h()
14299: {
14300: switch(REG8(AL)) {
14301: case 0x00:
1.1.1.14 root 14302: if(no_windows) {
1.1.1.29 root 14303: // neither Windows 3.x enhanced mode nor Windows/386 2.x running
14304: // REG8(AL) = 0x00;
1.1.1.14 root 14305: } else {
1.1.1.30 root 14306: REG8(AL) = win_major_version;
14307: REG8(AH) = win_minor_version;
1.1 root 14308: }
14309: break;
1.1.1.43 root 14310: case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30 root 14311: // from DOSBox
14312: i386_set_a20_line(1);
14313: break;
1.1.1.49 root 14314: case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43 root 14315: case 0x08: // Windows Enhanced Mode Init Complete Broadcast
14316: case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
14317: break;
14318: case 0x07:
14319: // Virtual Device Call API
14320: break;
1.1.1.22 root 14321: case 0x0a:
14322: if(!no_windows) {
14323: REG16(AX) = 0x0000;
1.1.1.30 root 14324: REG8(BH) = win_major_version;
14325: REG8(BL) = win_minor_version;
1.1.1.49 root 14326: // REG16(CX) = 0x0002; // standard
1.1.1.22 root 14327: REG16(CX) = 0x0003; // enhanced
14328: }
14329: break;
1.1.1.30 root 14330: case 0x0b:
14331: // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22 root 14332: case 0x0e:
14333: case 0x0f:
1.1.1.30 root 14334: case 0x10:
1.1.1.22 root 14335: case 0x11:
14336: case 0x12:
14337: case 0x13:
14338: case 0x14:
1.1.1.30 root 14339: case 0x15:
1.1.1.43 root 14340: case 0x81:
14341: case 0x82:
1.1.1.44 root 14342: case 0x84:
1.1.1.49 root 14343: case 0x85:
1.1.1.33 root 14344: case 0x86:
1.1.1.22 root 14345: case 0x87:
1.1.1.30 root 14346: case 0x89:
1.1.1.33 root 14347: case 0x8a:
1.1.1.22 root 14348: // function not supported, do not clear AX
14349: break;
1.1.1.14 root 14350: case 0x80:
14351: Sleep(10);
1.1.1.35 root 14352: REQUEST_HARDWRE_UPDATE();
1.1.1.29 root 14353: REG8(AL) = 0x00;
1.1.1.14 root 14354: break;
1.1.1.33 root 14355: case 0x83:
14356: REG16(BX) = 0x01; // system vm id
14357: break;
1.1.1.22 root 14358: case 0x8e:
14359: REG16(AX) = 0x00; // failed
14360: break;
1.1.1.20 root 14361: case 0x8f:
14362: switch(REG8(DH)) {
14363: case 0x01:
1.1.1.49 root 14364: // REG16(AX) = 0x0000; // close command selected but not yet acknowledged
14365: // REG16(AX) = 0x0001; // close command issued and acknowledged
14366: REG16(AX) = 0x168f; // close command not selected -- application should continue
14367: break;
14368: default:
14369: REG16(AX) = 0x0000; // successful
1.1.1.20 root 14370: break;
14371: }
14372: break;
1.1 root 14373: default:
1.1.1.22 root 14374: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14375: REG16(AX) = 0x01;
14376: m_CF = 1;
14377: break;
14378: }
14379: }
14380:
14381: inline void msdos_int_2fh_19h()
14382: {
14383: switch(REG8(AL)) {
14384: case 0x00:
1.1.1.29 root 14385: // SHELLB.COM is not installed
14386: // REG8(AL) = 0x00;
1.1.1.22 root 14387: break;
14388: case 0x01:
14389: case 0x02:
14390: case 0x03:
14391: case 0x04:
14392: REG16(AX) = 0x01;
14393: m_CF = 1;
14394: break;
1.1.1.29 root 14395: case 0x80:
14396: // IBM ROM-DOS v4.0 is not installed
14397: // REG8(AL) = 0x00;
14398: break;
1.1.1.22 root 14399: default:
14400: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14401: REG16(AX) = 0x01;
1.1.1.3 root 14402: m_CF = 1;
1.1 root 14403: break;
14404: }
14405: }
14406:
14407: inline void msdos_int_2fh_1ah()
14408: {
14409: switch(REG8(AL)) {
14410: case 0x00:
1.1.1.29 root 14411: // ANSI.SYS is installed
1.1 root 14412: REG8(AL) = 0xff;
14413: break;
1.1.1.49 root 14414: case 0x01:
1.1.1.50 root 14415: if(REG8(CL) == 0x5f) {
14416: // set display information
14417: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
14418: int cur_width = *(UINT16 *)(mem + 0x44a) + 0;
14419: int cur_height = *(UINT8 *)(mem + 0x484) + 1;
14420: int new_width = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // character columns
14421: int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10); // character rows
14422:
14423: if(cur_width != new_width || cur_height != new_height) {
14424: pcbios_set_console_size(new_width, new_height, true);
14425: }
14426: }
14427: } else if(REG8(CL) == 0x7f) {
1.1.1.49 root 14428: // get display information
1.1.1.50 root 14429: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0; // level (0 for DOS 4.x-6.0)
14430: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0; // reserved (0)
14431: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14; // length of following data (14)
14432: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1; // bit 0 set for blink, clear for intensity
14433: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1; // mode type (1=text, 2=graphics)
14434: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0; // reserved (0)
14435: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4; // 4 bits per pixel
14436: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) = 8 * (*(UINT16 *)(mem + 0x44a) + 0); // pixel columns
14437: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8 *)(mem + 0x484) + 1); // pixel rows
14438: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0; // character columns
14439: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8 *)(mem + 0x484) + 1; // character rows
1.1.1.49 root 14440: } else {
14441: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14442: REG16(AX) = 0x01;
14443: m_CF = 1;
14444: }
14445: break;
1.1 root 14446: default:
1.1.1.22 root 14447: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14448: REG16(AX) = 0x01;
14449: m_CF = 1;
14450: break;
14451: }
14452: }
14453:
1.1.1.30 root 14454: inline void msdos_int_2fh_40h()
1.1.1.22 root 14455: {
14456: switch(REG8(AL)) {
14457: case 0x00:
1.1.1.30 root 14458: // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
14459: REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22 root 14460: break;
1.1.1.43 root 14461: case 0x10:
14462: // OS/2 v2.0+ - Installation Check
14463: REG16(AX) = 0x01;
14464: m_CF = 1;
14465: break;
1.1.1.22 root 14466: default:
14467: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14468: REG16(AX) = 0x01;
1.1.1.3 root 14469: m_CF = 1;
1.1 root 14470: break;
14471: }
14472: }
14473:
14474: inline void msdos_int_2fh_43h()
14475: {
14476: switch(REG8(AL)) {
14477: case 0x00:
1.1.1.29 root 14478: // XMS is installed ?
1.1.1.19 root 14479: #ifdef SUPPORT_XMS
14480: if(support_xms) {
14481: REG8(AL) = 0x80;
1.1.1.44 root 14482: }
14483: #endif
14484: break;
14485: case 0x08:
14486: #ifdef SUPPORT_XMS
14487: if(support_xms) {
14488: REG8(AL) = 0x43;
14489: REG8(BL) = 0x01; // IBM PC/AT
14490: REG8(BH) = 0x01; // Fast AT A20 switch time
14491: }
1.1.1.19 root 14492: #endif
14493: break;
14494: case 0x10:
14495: SREG(ES) = XMS_TOP >> 4;
14496: i386_load_segment_descriptor(ES);
1.1.1.26 root 14497: REG16(BX) = 0x15;
1.1 root 14498: break;
1.1.1.44 root 14499: case 0xe0:
14500: // DOS Protected Mode Services (DPMS) v1.0 is not installed
14501: if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
14502: break;
14503: }
1.1 root 14504: default:
1.1.1.22 root 14505: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14506: REG16(AX) = 0x01;
14507: m_CF = 1;
14508: break;
14509: }
14510: }
14511:
14512: inline void msdos_int_2fh_46h()
14513: {
14514: switch(REG8(AL)) {
14515: case 0x80:
1.1.1.29 root 14516: // Windows v3.0 is not installed
14517: // REG8(AL) = 0x00;
1.1.1.22 root 14518: break;
14519: default:
14520: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14521: REG16(AX) = 0x01;
14522: m_CF = 1;
14523: break;
14524: }
14525: }
14526:
14527: inline void msdos_int_2fh_48h()
14528: {
14529: switch(REG8(AL)) {
14530: case 0x00:
1.1.1.29 root 14531: // DOSKEY is not installed
14532: // REG8(AL) = 0x00;
1.1.1.22 root 14533: break;
14534: case 0x10:
14535: msdos_int_21h_0ah();
14536: REG16(AX) = 0x00;
14537: break;
14538: default:
14539: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14540: REG16(AX) = 0x01;
1.1.1.3 root 14541: m_CF = 1;
1.1 root 14542: break;
14543: }
14544: }
14545:
14546: inline void msdos_int_2fh_4ah()
14547: {
14548: switch(REG8(AL)) {
1.1.1.29 root 14549: #ifdef SUPPORT_HMA
14550: case 0x01: // DOS 5.0+ - Query Free HMA Space
14551: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
14552: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14553: // restore first free mcb in high memory area
14554: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14555: }
14556: int offset = 0xffff;
14557: if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
14558: REG16(DI) = offset + 0x10;
14559: } else {
14560: REG16(DI) = 0xffff;
14561: }
14562: } else {
14563: // HMA is already used
14564: REG16(BX) = 0;
14565: REG16(DI) = 0xffff;
14566: }
14567: SREG(ES) = 0xffff;
14568: i386_load_segment_descriptor(ES);
14569: break;
14570: case 0x02: // DOS 5.0+ - Allocate HMA Space
14571: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
14572: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14573: // restore first free mcb in high memory area
14574: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14575: }
14576: int size = REG16(BX), offset;
14577: if((size % 16) != 0) {
14578: size &= ~15;
14579: size += 16;
14580: }
14581: if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
14582: REG16(BX) = size;
14583: REG16(DI) = offset + 0x10;
14584: is_hma_used_by_int_2fh = true;
14585: } else {
14586: REG16(BX) = 0;
14587: REG16(DI) = 0xffff;
14588: }
14589: } else {
14590: // HMA is already used
14591: REG16(BX) = 0;
14592: REG16(DI) = 0xffff;
14593: }
14594: SREG(ES) = 0xffff;
14595: i386_load_segment_descriptor(ES);
14596: break;
14597: case 0x03: // Windows95 - (De)Allocate HMA Memory Block
14598: if(REG8(DL) == 0x00) {
14599: if(!is_hma_used_by_xms) {
14600: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14601: // restore first free mcb in high memory area
14602: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14603: is_hma_used_by_int_2fh = false;
14604: }
14605: int size = REG16(BX), offset;
14606: if((size % 16) != 0) {
14607: size &= ~15;
14608: size += 16;
14609: }
14610: if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
14611: // REG16(BX) = size;
14612: SREG(ES) = 0xffff;
14613: i386_load_segment_descriptor(ES);
14614: REG16(DI) = offset + 0x10;
14615: is_hma_used_by_int_2fh = true;
14616: } else {
14617: REG16(DI) = 0xffff;
14618: }
14619: } else {
14620: REG16(DI) = 0xffff;
14621: }
14622: } else if(REG8(DL) == 0x01) {
14623: if(!is_hma_used_by_xms) {
14624: int size = REG16(BX);
14625: if((size % 16) != 0) {
14626: size &= ~15;
14627: size += 16;
14628: }
14629: if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
14630: // memory block address is not changed
14631: } else {
14632: REG16(DI) = 0xffff;
14633: }
14634: } else {
14635: REG16(DI) = 0xffff;
14636: }
14637: } else if(REG8(DL) == 0x02) {
14638: if(!is_hma_used_by_xms) {
14639: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14640: // restore first free mcb in high memory area
14641: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14642: is_hma_used_by_int_2fh = false;
14643: } else {
14644: msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
14645: if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
14646: is_hma_used_by_int_2fh = false;
14647: }
14648: }
14649: }
14650: } else {
14651: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14652: REG16(AX) = 0x01;
14653: m_CF = 1;
14654: }
14655: break;
14656: case 0x04: // Windows95 - Get Start of HMA Memory Chain
14657: if(!is_hma_used_by_xms) {
14658: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14659: // restore first free mcb in high memory area
14660: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14661: is_hma_used_by_int_2fh = false;
14662: }
14663: REG16(AX) = 0x0000;
14664: SREG(ES) = 0xffff;
14665: i386_load_segment_descriptor(ES);
14666: REG16(DI) = 0x10;
14667: }
14668: break;
14669: #else
1.1 root 14670: case 0x01:
14671: case 0x02:
1.1.1.29 root 14672: // HMA is already used
1.1.1.27 root 14673: REG16(BX) = 0x0000;
1.1.1.3 root 14674: SREG(ES) = 0xffff;
14675: i386_load_segment_descriptor(ES);
1.1 root 14676: REG16(DI) = 0xffff;
14677: break;
1.1.1.19 root 14678: case 0x03:
14679: // unable to allocate
14680: REG16(DI) = 0xffff;
14681: break;
14682: case 0x04:
14683: // function not supported, do not clear AX
14684: break;
1.1.1.29 root 14685: #endif
14686: case 0x10:
1.1.1.42 root 14687: switch(REG16(BX)) {
14688: case 0x0000:
14689: case 0x0001:
14690: case 0x0002:
14691: case 0x0003:
14692: case 0x0004:
14693: case 0x0005:
14694: case 0x0006:
14695: case 0x0007:
14696: case 0x0008:
14697: case 0x000a:
14698: case 0x1234:
14699: // SMARTDRV v4.00+ is not installed
14700: break;
14701: default:
1.1.1.29 root 14702: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14703: REG16(AX) = 0x01;
14704: m_CF = 1;
1.1.1.42 root 14705: break;
1.1.1.29 root 14706: }
14707: break;
14708: case 0x11:
1.1.1.42 root 14709: switch(REG16(BX)) {
14710: case 0x0000:
14711: case 0x0001:
14712: case 0x0002:
14713: case 0x0003:
14714: case 0x0004:
14715: case 0x0005:
14716: case 0x0006:
14717: case 0x0007:
14718: case 0x0008:
14719: case 0x0009:
14720: case 0x000a:
14721: case 0x000b:
14722: case 0xfffe:
14723: case 0xffff:
1.1.1.29 root 14724: // DBLSPACE.BIN is not installed
1.1.1.42 root 14725: break;
14726: default:
14727: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14728: REG16(AX) = 0x01;
14729: m_CF = 1;
14730: break;
14731: }
14732: break;
14733: case 0x12:
14734: if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
14735: // Microsoft Realtime Compression Interface (MRCI) is not installed
14736: } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
14737: // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29 root 14738: } else {
14739: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14740: REG16(AX) = 0x01;
14741: m_CF = 1;
14742: }
1.1.1.22 root 14743: break;
1.1.1.42 root 14744: case 0x13:
14745: // DBLSPACE.BIN is not installed
14746: break;
1.1.1.22 root 14747: default:
14748: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14749: REG16(AX) = 0x01;
14750: m_CF = 1;
14751: break;
14752: }
14753: }
14754:
14755: inline void msdos_int_2fh_4bh()
14756: {
14757: switch(REG8(AL)) {
1.1.1.24 root 14758: case 0x01:
1.1.1.22 root 14759: case 0x02:
1.1.1.29 root 14760: // Task Switcher is not installed
1.1.1.24 root 14761: break;
14762: case 0x03:
14763: // this call is available from within DOSSHELL even if the task switcher is not installed
14764: REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22 root 14765: break;
1.1.1.30 root 14766: case 0x04:
14767: REG16(BX) = 0x0000; // free switcher id successfully
14768: break;
1.1.1.43 root 14769: case 0x05:
14770: REG16(BX) = 0x0000; // no instance data chain
14771: SREG(ES) = 0x0000;
14772: i386_load_segment_descriptor(ES);
14773: break;
1.1 root 14774: default:
1.1.1.22 root 14775: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14776: REG16(AX) = 0x01;
1.1.1.3 root 14777: m_CF = 1;
1.1 root 14778: break;
14779: }
14780: }
14781:
1.1.1.44 root 14782: inline void msdos_int_2fh_4dh()
14783: {
14784: switch(REG8(AL)) {
14785: case 0x00:
14786: // KKCFUNC is not installed ???
14787: break;
14788: default:
14789: // unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14790: REG16(AX) = 0x01; // invalid function
14791: m_CF = 1;
14792: break;
14793: }
14794: }
14795:
1.1 root 14796: inline void msdos_int_2fh_4fh()
14797: {
14798: switch(REG8(AL)) {
14799: case 0x00:
1.1.1.29 root 14800: // BILING is installed
1.1.1.27 root 14801: REG16(AX) = 0x0000;
14802: REG8(DL) = 0x01; // major version
14803: REG8(DH) = 0x00; // minor version
1.1 root 14804: break;
14805: case 0x01:
1.1.1.27 root 14806: REG16(AX) = 0x0000;
1.1 root 14807: REG16(BX) = active_code_page;
14808: break;
14809: default:
1.1.1.22 root 14810: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14811: REG16(AX) = 0x01;
14812: m_CF = 1;
14813: break;
14814: }
14815: }
14816:
14817: inline void msdos_int_2fh_55h()
14818: {
14819: switch(REG8(AL)) {
14820: case 0x00:
14821: case 0x01:
14822: // unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14823: break;
14824: default:
14825: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14826: REG16(AX) = 0x01;
1.1.1.3 root 14827: m_CF = 1;
1.1 root 14828: break;
14829: }
14830: }
14831:
1.1.1.44 root 14832: inline void msdos_int_2fh_56h()
14833: {
14834: switch(REG8(AL)) {
14835: case 0x00:
14836: // INTERLNK is not installed
14837: break;
14838: case 0x01:
14839: // this call is available from within SCANDISK even if INTERLNK is not installed
14840: // if(msdos_is_remote_drive(REG8(BH))) {
14841: // REG8(AL) = 0x00;
14842: // }
14843: break;
14844: default:
14845: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14846: REG16(AX) = 0x01;
14847: m_CF = 1;
14848: break;
14849: }
14850: }
14851:
1.1.1.24 root 14852: inline void msdos_int_2fh_adh()
14853: {
14854: switch(REG8(AL)) {
14855: case 0x00:
1.1.1.29 root 14856: // DISPLAY.SYS is installed
1.1.1.24 root 14857: REG8(AL) = 0xff;
14858: REG16(BX) = 0x100; // ???
14859: break;
14860: case 0x01:
14861: active_code_page = REG16(BX);
14862: msdos_nls_tables_update();
14863: REG16(AX) = 0x01;
14864: break;
14865: case 0x02:
14866: REG16(BX) = active_code_page;
14867: break;
14868: case 0x03:
14869: // FIXME
14870: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
14871: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
14872: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
14873: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
14874: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
14875: break;
14876: case 0x80:
1.1.1.49 root 14877: // KEYB.COM is not installed
14878: break;
1.1.1.24 root 14879: default:
14880: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14881: REG16(AX) = 0x01;
14882: m_CF = 1;
14883: break;
14884: }
14885: }
14886:
1.1 root 14887: inline void msdos_int_2fh_aeh()
14888: {
14889: switch(REG8(AL)) {
14890: case 0x00:
1.1.1.28 root 14891: // FIXME: we need to check the given command line
14892: REG8(AL) = 0x00; // the command should be executed as usual
14893: // REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1 root 14894: break;
14895: case 0x01:
14896: {
14897: char command[MAX_PATH];
14898: memset(command, 0, sizeof(command));
1.1.1.3 root 14899: memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1 root 14900:
14901: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
14902: param->env_seg = 0;
14903: param->cmd_line.w.l = 44;
14904: param->cmd_line.w.h = (WORK_TOP >> 4);
14905: param->fcb1.w.l = 24;
14906: param->fcb1.w.h = (WORK_TOP >> 4);
14907: param->fcb2.w.l = 24;
14908: param->fcb2.w.h = (WORK_TOP >> 4);
14909:
14910: memset(mem + WORK_TOP + 24, 0x20, 20);
14911:
14912: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3 root 14913: cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
14914: memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1 root 14915: cmd_line->cmd[cmd_line->len] = 0x0d;
14916:
1.1.1.28 root 14917: try {
14918: msdos_process_exec(command, param, 0);
14919: } catch(...) {
14920: fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1 root 14921: }
14922: }
14923: break;
14924: default:
1.1.1.22 root 14925: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 14926: REG16(AX) = 0x01;
1.1.1.3 root 14927: m_CF = 1;
1.1 root 14928: break;
14929: }
14930: }
14931:
1.1.1.34 root 14932: inline void msdos_int_2fh_b7h()
14933: {
14934: switch(REG8(AL)) {
14935: case 0x00:
14936: // APPEND is not installed
14937: // REG8(AL) = 0x00;
14938: break;
1.1.1.44 root 14939: case 0x06:
14940: REG16(BX) = 0x0000;
14941: break;
1.1.1.34 root 14942: case 0x07:
1.1.1.43 root 14943: case 0x11:
1.1.1.34 root 14944: // COMMAND.COM calls this service without checking APPEND is installed
14945: break;
14946: default:
14947: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
14948: REG16(AX) = 0x01;
14949: m_CF = 1;
14950: break;
14951: }
14952: }
14953:
1.1.1.24 root 14954: inline void msdos_int_33h_0000h()
14955: {
14956: REG16(AX) = 0xffff; // hardware/driver installed
14957: REG16(BX) = MAX_MOUSE_BUTTONS;
14958: }
14959:
14960: inline void msdos_int_33h_0001h()
14961: {
1.1.1.34 root 14962: if(mouse.hidden > 0) {
14963: mouse.hidden--;
14964: }
14965: if(mouse.hidden == 0) {
1.1.1.24 root 14966: if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
14967: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
14968: }
1.1.1.59 root 14969: pic[1].imr &= ~0x10; // enable irq12
1.1.1.24 root 14970: }
14971: }
14972:
14973: inline void msdos_int_33h_0002h()
14974: {
1.1.1.34 root 14975: mouse.hidden++;
14976: // SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
1.1.1.59 root 14977: pic[1].imr |= 0x10; // disable irq12
1.1.1.24 root 14978: }
14979:
14980: inline void msdos_int_33h_0003h()
14981: {
1.1.1.34 root 14982: // if(mouse.hidden > 0) {
14983: update_console_input();
14984: // }
1.1.1.24 root 14985: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 14986: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
14987: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
14988: }
14989:
14990: inline void msdos_int_33h_0004h()
14991: {
14992: mouse.position.x = REG16(CX);
14993: mouse.position.x = REG16(DX);
1.1.1.24 root 14994: }
14995:
14996: inline void msdos_int_33h_0005h()
14997: {
1.1.1.34 root 14998: // if(mouse.hidden > 0) {
14999: update_console_input();
15000: // }
1.1.1.24 root 15001: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
15002: int idx = REG16(BX);
1.1.1.34 root 15003: REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
15004: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
15005: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.buttons[idx].pressed_position.y));
1.1.1.24 root 15006: mouse.buttons[idx].pressed_times = 0;
15007: } else {
15008: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
15009: }
15010: REG16(AX) = mouse.get_buttons();
15011: }
15012:
15013: inline void msdos_int_33h_0006h()
15014: {
1.1.1.34 root 15015: // if(mouse.hidden > 0) {
15016: update_console_input();
15017: // }
1.1.1.24 root 15018: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
15019: int idx = REG16(BX);
1.1.1.34 root 15020: REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
15021: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
15022: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.buttons[idx].released_position.y));
1.1.1.24 root 15023: mouse.buttons[idx].released_times = 0;
15024: } else {
15025: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
15026: }
15027: REG16(AX) = mouse.get_buttons();
15028: }
15029:
15030: inline void msdos_int_33h_0007h()
15031: {
15032: mouse.min_position.x = min(REG16(CX), REG16(DX));
15033: mouse.max_position.x = max(REG16(CX), REG16(DX));
15034: }
15035:
15036: inline void msdos_int_33h_0008h()
15037: {
15038: mouse.min_position.y = min(REG16(CX), REG16(DX));
15039: mouse.max_position.y = max(REG16(CX), REG16(DX));
15040: }
15041:
15042: inline void msdos_int_33h_0009h()
15043: {
15044: mouse.hot_spot[0] = REG16(BX);
15045: mouse.hot_spot[1] = REG16(CX);
15046: }
15047:
1.1.1.49 root 15048: inline void msdos_int_33h_000ah()
15049: {
15050: mouse.screen_mask = REG16(CX);
15051: mouse.cursor_mask = REG16(DX);
15052: }
15053:
1.1.1.24 root 15054: inline void msdos_int_33h_000bh()
15055: {
1.1.1.34 root 15056: // if(mouse.hidden > 0) {
15057: update_console_input();
15058: // }
1.1.1.24 root 15059: int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
15060: int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
15061: mouse.prev_position.x = mouse.position.x;
15062: mouse.prev_position.y = mouse.position.y;
15063: REG16(CX) = dx;
15064: REG16(DX) = dy;
15065: }
15066:
15067: inline void msdos_int_33h_000ch()
15068: {
15069: mouse.call_mask = REG16(CX);
15070: mouse.call_addr.w.l = REG16(DX);
15071: mouse.call_addr.w.h = SREG(ES);
15072: }
15073:
15074: inline void msdos_int_33h_000fh()
15075: {
15076: mouse.mickey.x = REG16(CX);
15077: mouse.mickey.y = REG16(DX);
15078: }
15079:
15080: inline void msdos_int_33h_0011h()
15081: {
15082: REG16(AX) = 0xffff;
15083: REG16(BX) = MAX_MOUSE_BUTTONS;
15084: }
15085:
15086: inline void msdos_int_33h_0014h()
15087: {
15088: UINT16 old_mask = mouse.call_mask;
15089: UINT16 old_ofs = mouse.call_addr.w.l;
15090: UINT16 old_seg = mouse.call_addr.w.h;
15091:
15092: mouse.call_mask = REG16(CX);
15093: mouse.call_addr.w.l = REG16(DX);
15094: mouse.call_addr.w.h = SREG(ES);
15095:
15096: REG16(CX) = old_mask;
15097: REG16(DX) = old_ofs;
15098: SREG(ES) = old_seg;
15099: i386_load_segment_descriptor(ES);
15100: }
15101:
15102: inline void msdos_int_33h_0015h()
15103: {
15104: REG16(BX) = sizeof(mouse);
15105: }
15106:
15107: inline void msdos_int_33h_0016h()
15108: {
15109: memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
15110: }
15111:
15112: inline void msdos_int_33h_0017h()
15113: {
15114: memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
15115: }
15116:
1.1.1.43 root 15117: inline void msdos_int_33h_0018h()
15118: {
15119: for(int i = 0; i < 8; i++) {
15120: if(REG16(CX) & (1 << i)) {
15121: if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
15122: // event handler already exists
15123: REG16(AX) = 0xffff;
15124: break;
15125: }
15126: mouse.call_addr_alt[i].w.l = REG16(DX);
15127: mouse.call_addr_alt[i].w.h = SREG(ES);
15128: }
15129: }
15130: }
15131:
15132: inline void msdos_int_33h_0019h()
15133: {
15134: UINT16 call_mask = REG16(CX);
15135:
15136: REG16(CX) = 0;
15137:
15138: for(int i = 0; i < 8; i++) {
15139: if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
15140: for(int j = 0; j < 8; j++) {
15141: if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
15142: REG16(CX) |= (1 << j);
15143: }
15144: }
15145: REG16(DX) = mouse.call_addr_alt[i].w.l;
15146: REG16(BX) = mouse.call_addr_alt[i].w.h;
15147: break;
15148: }
15149: }
15150: }
15151:
1.1.1.24 root 15152: inline void msdos_int_33h_001ah()
15153: {
15154: mouse.sensitivity[0] = REG16(BX);
15155: mouse.sensitivity[1] = REG16(CX);
15156: mouse.sensitivity[2] = REG16(DX);
15157: }
15158:
15159: inline void msdos_int_33h_001bh()
15160: {
15161: REG16(BX) = mouse.sensitivity[0];
15162: REG16(CX) = mouse.sensitivity[1];
15163: REG16(DX) = mouse.sensitivity[2];
15164: }
15165:
15166: inline void msdos_int_33h_001dh()
15167: {
15168: mouse.display_page = REG16(BX);
15169: }
15170:
15171: inline void msdos_int_33h_001eh()
15172: {
15173: REG16(BX) = mouse.display_page;
15174: }
15175:
1.1.1.34 root 15176: inline void msdos_int_33h_001fh()
15177: {
15178: // from DOSBox
15179: REG16(BX) = 0x0000;
15180: SREG(ES) = 0x0000;
15181: i386_load_segment_descriptor(ES);
15182: mouse.enabled = false;
15183: mouse.old_hidden = mouse.hidden;
15184: mouse.hidden = 1;
15185: }
15186:
15187: inline void msdos_int_33h_0020h()
15188: {
15189: // from DOSBox
15190: mouse.enabled = true;
15191: mouse.hidden = mouse.old_hidden;
15192: }
15193:
1.1.1.24 root 15194: inline void msdos_int_33h_0021h()
15195: {
15196: REG16(AX) = 0xffff;
15197: REG16(BX) = MAX_MOUSE_BUTTONS;
15198: }
15199:
15200: inline void msdos_int_33h_0022h()
15201: {
15202: mouse.language = REG16(BX);
15203: }
15204:
15205: inline void msdos_int_33h_0023h()
15206: {
15207: REG16(BX) = mouse.language;
15208: }
15209:
15210: inline void msdos_int_33h_0024h()
15211: {
15212: REG16(BX) = 0x0805; // V8.05
15213: REG16(CX) = 0x0400; // PS/2
15214: }
15215:
1.1.1.49 root 15216: inline void msdos_int_33h_0025h()
15217: {
15218: REG16(AX) = 0x8000; // driver (not TSR), software text cursor
15219: }
15220:
1.1.1.24 root 15221: inline void msdos_int_33h_0026h()
15222: {
15223: REG16(BX) = 0x0000;
15224: REG16(CX) = mouse.max_position.x;
15225: REG16(DX) = mouse.max_position.y;
15226: }
15227:
1.1.1.49 root 15228: inline void msdos_int_33h_0027h()
15229: {
15230: // if(mouse.hidden > 0) {
15231: update_console_input();
15232: // }
15233: int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
15234: int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
15235: mouse.prev_position.x = mouse.position.x;
15236: mouse.prev_position.y = mouse.position.y;
15237: REG16(AX) = mouse.screen_mask;
15238: REG16(BX) = mouse.cursor_mask;
15239: REG16(CX) = dx;
15240: REG16(DX) = dy;
15241: }
15242:
15243: inline void msdos_int_33h_0028h()
15244: {
15245: if(REG16(CX) != 0) {
15246: UINT8 tmp = REG8(AL);
15247: REG8(AL) = REG8(CL);
15248: pcbios_int_10h_00h();
15249: REG8(AL) = tmp;
15250: }
15251: REG8(CL) = 0x00; // successful
15252: }
15253:
15254: inline void msdos_int_33h_0029h()
15255: {
15256: switch(REG16(CX)) {
15257: case 0x0000:
15258: REG16(CX) = 0x0003;
15259: sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
15260: break;
15261: case 0x0003:
15262: REG16(CX) = 0x0070;
15263: sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
15264: break;
15265: case 0x0070:
15266: REG16(CX) = 0x0071;
15267: sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
15268: break;
15269: case 0x0071:
15270: REG16(CX) = 0x0073;
15271: sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
15272: break;
15273: default:
15274: REG16(CX) = 0x0000;
15275: break;
15276: }
15277: if(REG16(CX) != 0) {
15278: SREG(DS) = (WORK_TOP >> 4);
15279: } else {
15280: SREG(DS) = 0x0000;
15281: }
15282: i386_load_segment_descriptor(DS);
15283: REG16(DX) = 0x0000;
15284: }
15285:
1.1.1.24 root 15286: inline void msdos_int_33h_002ah()
15287: {
1.1.1.34 root 15288: REG16(AX) = -mouse.hidden;
1.1.1.24 root 15289: REG16(BX) = mouse.hot_spot[0];
15290: REG16(CX) = mouse.hot_spot[1];
15291: REG16(DX) = 4; // PS/2
15292: }
15293:
15294: inline void msdos_int_33h_0031h()
15295: {
15296: REG16(AX) = mouse.min_position.x;
15297: REG16(BX) = mouse.min_position.y;
15298: REG16(CX) = mouse.max_position.x;
15299: REG16(DX) = mouse.max_position.y;
15300: }
15301:
15302: inline void msdos_int_33h_0032h()
15303: {
15304: REG16(AX) = 0;
1.1.1.49 root 15305: REG16(AX) |= 0x8000; // 0025h
1.1.1.24 root 15306: REG16(AX) |= 0x4000; // 0026h
1.1.1.49 root 15307: REG16(AX) |= 0x2000; // 0027h
1.1.1.24 root 15308: // REG16(AX) |= 0x1000; // 0028h
15309: // REG16(AX) |= 0x0800; // 0029h
15310: REG16(AX) |= 0x0400; // 002ah
15311: // REG16(AX) |= 0x0200; // 002bh
15312: // REG16(AX) |= 0x0100; // 002ch
15313: // REG16(AX) |= 0x0080; // 002dh
15314: // REG16(AX) |= 0x0040; // 002eh
15315: REG16(AX) |= 0x0020; // 002fh
15316: // REG16(AX) |= 0x0010; // 0030h
15317: REG16(AX) |= 0x0008; // 0031h
15318: REG16(AX) |= 0x0004; // 0032h
15319: // REG16(AX) |= 0x0002; // 0033h
15320: // REG16(AX) |= 0x0001; // 0034h
15321: }
15322:
1.1.1.49 root 15323: inline void msdos_int_33h_004dh()
15324: {
15325: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
15326: }
15327:
15328: inline void msdos_int_33h_006dh()
15329: {
15330: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
15331: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
15332: }
15333:
1.1.1.19 root 15334: inline void msdos_int_67h_40h()
15335: {
15336: if(!support_ems) {
15337: REG8(AH) = 0x84;
15338: } else {
15339: REG8(AH) = 0x00;
15340: }
15341: }
15342:
15343: inline void msdos_int_67h_41h()
15344: {
15345: if(!support_ems) {
15346: REG8(AH) = 0x84;
15347: } else {
15348: REG8(AH) = 0x00;
15349: REG16(BX) = EMS_TOP >> 4;
15350: }
15351: }
15352:
15353: inline void msdos_int_67h_42h()
15354: {
15355: if(!support_ems) {
15356: REG8(AH) = 0x84;
15357: } else {
15358: REG8(AH) = 0x00;
15359: REG16(BX) = free_ems_pages;
15360: REG16(DX) = MAX_EMS_PAGES;
15361: }
15362: }
15363:
15364: inline void msdos_int_67h_43h()
15365: {
15366: if(!support_ems) {
15367: REG8(AH) = 0x84;
15368: } else if(REG16(BX) > MAX_EMS_PAGES) {
15369: REG8(AH) = 0x87;
15370: } else if(REG16(BX) > free_ems_pages) {
15371: REG8(AH) = 0x88;
15372: } else if(REG16(BX) == 0) {
15373: REG8(AH) = 0x89;
15374: } else {
1.1.1.31 root 15375: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15376: if(!ems_handles[i].allocated) {
15377: ems_allocate_pages(i, REG16(BX));
15378: REG8(AH) = 0x00;
15379: REG16(DX) = i;
15380: return;
15381: }
15382: }
15383: REG8(AH) = 0x85;
15384: }
15385: }
15386:
15387: inline void msdos_int_67h_44h()
15388: {
15389: if(!support_ems) {
15390: REG8(AH) = 0x84;
1.1.1.31 root 15391: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15392: REG8(AH) = 0x83;
15393: } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
15394: REG8(AH) = 0x8a;
15395: // } else if(!(REG8(AL) < 4)) {
15396: // REG8(AH) = 0x8b;
15397: } else if(REG16(BX) == 0xffff) {
15398: ems_unmap_page(REG8(AL) & 3);
15399: REG8(AH) = 0x00;
15400: } else {
15401: ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
15402: REG8(AH) = 0x00;
15403: }
15404: }
15405:
15406: inline void msdos_int_67h_45h()
15407: {
15408: if(!support_ems) {
15409: REG8(AH) = 0x84;
1.1.1.31 root 15410: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15411: REG8(AH) = 0x83;
15412: } else {
15413: ems_release_pages(REG16(DX));
15414: REG8(AH) = 0x00;
15415: }
15416: }
15417:
15418: inline void msdos_int_67h_46h()
15419: {
15420: if(!support_ems) {
15421: REG8(AH) = 0x84;
15422: } else {
1.1.1.29 root 15423: // REG16(AX) = 0x0032; // EMS 3.2
15424: REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19 root 15425: }
15426: }
15427:
15428: inline void msdos_int_67h_47h()
15429: {
15430: // NOTE: the map data should be stored in the specified ems page, not process data
15431: process_t *process = msdos_process_info_get(current_psp);
15432:
15433: if(!support_ems) {
15434: REG8(AH) = 0x84;
1.1.1.31 root 15435: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15436: // REG8(AH) = 0x83;
15437: } else if(process->ems_pages_stored) {
15438: REG8(AH) = 0x8d;
15439: } else {
15440: for(int i = 0; i < 4; i++) {
15441: process->ems_pages[i].handle = ems_pages[i].handle;
15442: process->ems_pages[i].page = ems_pages[i].page;
15443: process->ems_pages[i].mapped = ems_pages[i].mapped;
15444: }
15445: process->ems_pages_stored = true;
15446: REG8(AH) = 0x00;
15447: }
15448: }
15449:
15450: inline void msdos_int_67h_48h()
15451: {
15452: // NOTE: the map data should be restored from the specified ems page, not process data
15453: process_t *process = msdos_process_info_get(current_psp);
15454:
15455: if(!support_ems) {
15456: REG8(AH) = 0x84;
1.1.1.31 root 15457: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15458: // REG8(AH) = 0x83;
15459: } else if(!process->ems_pages_stored) {
15460: REG8(AH) = 0x8e;
15461: } else {
15462: for(int i = 0; i < 4; i++) {
15463: if(process->ems_pages[i].mapped) {
15464: ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
15465: } else {
15466: ems_unmap_page(i);
15467: }
15468: }
15469: process->ems_pages_stored = false;
15470: REG8(AH) = 0x00;
15471: }
15472: }
15473:
15474: inline void msdos_int_67h_4bh()
15475: {
15476: if(!support_ems) {
15477: REG8(AH) = 0x84;
15478: } else {
15479: REG8(AH) = 0x00;
15480: REG16(BX) = 0;
1.1.1.31 root 15481: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15482: if(ems_handles[i].allocated) {
15483: REG16(BX)++;
15484: }
15485: }
15486: }
15487: }
15488:
15489: inline void msdos_int_67h_4ch()
15490: {
15491: if(!support_ems) {
15492: REG8(AH) = 0x84;
1.1.1.31 root 15493: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15494: REG8(AH) = 0x83;
15495: } else {
15496: REG8(AH) = 0x00;
15497: REG16(BX) = ems_handles[REG16(DX)].pages;
15498: }
15499: }
15500:
15501: inline void msdos_int_67h_4dh()
15502: {
15503: if(!support_ems) {
15504: REG8(AH) = 0x84;
15505: } else {
15506: REG8(AH) = 0x00;
15507: REG16(BX) = 0;
1.1.1.31 root 15508: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15509: if(ems_handles[i].allocated) {
15510: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
15511: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
15512: REG16(BX)++;
15513: }
15514: }
15515: }
15516: }
15517:
1.1.1.20 root 15518: inline void msdos_int_67h_4eh()
15519: {
15520: if(!support_ems) {
15521: REG8(AH) = 0x84;
15522: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
15523: if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
15524: // save page map
15525: for(int i = 0; i < 4; i++) {
15526: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
15527: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
15528: }
15529: }
15530: if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
15531: // restore page map
15532: for(int i = 0; i < 4; i++) {
15533: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
15534: UINT16 page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
15535:
1.1.1.31 root 15536: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20 root 15537: ems_map_page(i, handle, page);
15538: } else {
15539: ems_unmap_page(i);
15540: }
15541: }
15542: }
15543: REG8(AH) = 0x00;
15544: } else if(REG8(AL) == 0x03) {
15545: REG8(AH) = 0x00;
1.1.1.21 root 15546: REG8(AL) = 4 * 4;
15547: } else {
1.1.1.22 root 15548: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.21 root 15549: REG8(AH) = 0x8f;
15550: }
15551: }
15552:
15553: inline void msdos_int_67h_4fh()
15554: {
15555: if(!support_ems) {
15556: REG8(AH) = 0x84;
15557: } else if(REG8(AL) == 0x00) {
15558: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
15559:
15560: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
15561: for(int i = 0; i < count; i++) {
15562: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
15563: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
15564:
15565: // if(!(physical < 4)) {
15566: // REG8(AH) = 0x8b;
15567: // return;
15568: // }
15569: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41 root 15570: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
15571: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
1.1.1.21 root 15572: }
15573: REG8(AH) = 0x00;
15574: } else if(REG8(AL) == 0x01) {
15575: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
15576:
15577: for(int i = 0; i < count; i++) {
15578: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
15579: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
15580: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
15581: UINT16 logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
15582:
15583: // if(!(physical < 4)) {
15584: // REG8(AH) = 0x8b;
15585: // return;
15586: // } else
1.1.1.41 root 15587: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21 root 15588: ems_map_page(physical & 3, handle, logical);
15589: } else {
1.1.1.41 root 15590: ems_unmap_page(physical & 3);
1.1.1.21 root 15591: }
15592: }
15593: REG8(AH) = 0x00;
15594: } else if(REG8(AL) == 0x02) {
15595: REG8(AH) = 0x00;
15596: REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20 root 15597: } else {
1.1.1.22 root 15598: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15599: REG8(AH) = 0x8f;
15600: }
15601: }
15602:
15603: inline void msdos_int_67h_50h()
15604: {
15605: if(!support_ems) {
15606: REG8(AH) = 0x84;
1.1.1.31 root 15607: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20 root 15608: REG8(AH) = 0x83;
15609: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15610: for(int i = 0; i < REG16(CX); i++) {
15611: int logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
15612: int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
15613:
15614: if(REG8(AL) == 0x01) {
15615: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15616: }
15617: // if(!(physical < 4)) {
15618: // REG8(AH) = 0x8b;
15619: // return;
15620: // } else
15621: if(logical == 0xffff) {
15622: ems_unmap_page(physical & 3);
15623: } else if(logical < ems_handles[REG16(DX)].pages) {
15624: ems_map_page(physical & 3, REG16(DX), logical);
15625: } else {
15626: REG8(AH) = 0x8a;
15627: return;
15628: }
15629: }
15630: REG8(AH) = 0x00;
15631: } else {
1.1.1.22 root 15632: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15633: REG8(AH) = 0x8f;
15634: }
15635: }
15636:
1.1.1.19 root 15637: inline void msdos_int_67h_51h()
15638: {
15639: if(!support_ems) {
15640: REG8(AH) = 0x84;
1.1.1.31 root 15641: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15642: REG8(AH) = 0x83;
15643: } else if(REG16(BX) > MAX_EMS_PAGES) {
15644: REG8(AH) = 0x87;
15645: } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
15646: REG8(AH) = 0x88;
15647: } else {
15648: ems_reallocate_pages(REG16(DX), REG16(BX));
15649: REG8(AH) = 0x00;
15650: }
15651: }
15652:
1.1.1.20 root 15653: inline void msdos_int_67h_52h()
15654: {
15655: if(!support_ems) {
15656: REG8(AH) = 0x84;
1.1.1.31 root 15657: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15658: // REG8(AH) = 0x83;
1.1.1.20 root 15659: } else if(REG8(AL) == 0x00) {
15660: REG8(AL) = 0x00; // handle is volatile
15661: REG8(AH) = 0x00;
15662: } else if(REG8(AL) == 0x01) {
15663: if(REG8(BL) == 0x00) {
15664: REG8(AH) = 0x00;
15665: } else {
15666: REG8(AH) = 0x90; // undefined attribute type
15667: }
15668: } else if(REG8(AL) == 0x02) {
15669: REG8(AL) = 0x00; // only volatile handles supported
15670: REG8(AH) = 0x00;
15671: } else {
1.1.1.22 root 15672: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15673: REG8(AH) = 0x8f;
15674: }
15675: }
15676:
1.1.1.19 root 15677: inline void msdos_int_67h_53h()
15678: {
15679: if(!support_ems) {
15680: REG8(AH) = 0x84;
1.1.1.31 root 15681: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15682: REG8(AH) = 0x83;
15683: } else if(REG8(AL) == 0x00) {
15684: memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
15685: REG8(AH) = 0x00;
15686: } else if(REG8(AL) == 0x01) {
1.1.1.31 root 15687: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15688: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
15689: REG8(AH) = 0xa1;
15690: return;
15691: }
15692: }
15693: REG8(AH) = 0x00;
15694: memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
15695: } else {
1.1.1.22 root 15696: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15697: REG8(AH) = 0x8f;
1.1.1.19 root 15698: }
15699: }
15700:
15701: inline void msdos_int_67h_54h()
15702: {
15703: if(!support_ems) {
15704: REG8(AH) = 0x84;
15705: } else if(REG8(AL) == 0x00) {
1.1.1.31 root 15706: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15707: if(ems_handles[i].allocated) {
15708: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
15709: } else {
15710: memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
15711: }
15712: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
15713: }
15714: REG8(AH) = 0x00;
15715: REG8(AL) = MAX_EMS_HANDLES;
15716: } else if(REG8(AL) == 0x01) {
15717: REG8(AH) = 0xa0; // not found
1.1.1.31 root 15718: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15719: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
15720: REG8(AH) = 0x00;
15721: REG16(DX) = i;
15722: break;
15723: }
15724: }
15725: } else if(REG8(AL) == 0x02) {
15726: REG8(AH) = 0x00;
15727: REG16(BX) = MAX_EMS_HANDLES;
15728: } else {
1.1.1.22 root 15729: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15730: REG8(AH) = 0x8f;
15731: }
15732: }
15733:
1.1.1.49 root 15734: inline void msdos_int_67h_55h()
15735: {
15736: if(!support_ems) {
15737: REG8(AH) = 0x84;
15738: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15739: REG8(AH) = 0x83;
15740: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15741: UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
15742: UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
15743: UINT8 entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
15744: UINT16 map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
15745: UINT16 map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
15746:
15747: for(int i = 0; i < (int)entries; i++) {
15748: int logical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
15749: int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
15750:
15751: if(REG8(AL) == 0x01) {
15752: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15753: }
15754: // if(!(physical < 4)) {
15755: // REG8(AH) = 0x8b;
15756: // return;
15757: // } else
15758: if(logical == 0xffff) {
15759: ems_unmap_page(physical & 3);
15760: } else if(logical < ems_handles[REG16(DX)].pages) {
15761: ems_map_page(physical & 3, REG16(DX), logical);
15762: } else {
15763: REG8(AH) = 0x8a;
15764: return;
15765: }
15766: }
15767: i386_jmp_far(jump_seg, jump_ofs);
15768: REG8(AH) = 0x00;
15769: } else {
15770: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
15771: REG8(AH) = 0x8f;
15772: }
15773: }
15774:
15775: inline void msdos_int_67h_56h()
15776: {
15777: if(!support_ems) {
15778: REG8(AH) = 0x84;
15779: } else if(REG8(AL) == 0x02) {
15780: REG16(BX) = (2 + 2) * 4;
15781: REG8(AH) = 0x00;
15782: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15783: REG8(AH) = 0x83;
15784: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15785: UINT16 call_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
15786: UINT16 call_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
15787: UINT8 new_entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
15788: UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
15789: UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
15790: #if 0
15791: UINT8 old_entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 9);
15792: UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
15793: UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
15794: #endif
15795: UINT16 handles[4], pages[4];
15796:
15797: // alter page map and call routine is at fffc:001f
15798: if(!(call_seg == 0 && call_ofs == 0)) {
15799: mem[DUMMY_TOP + 0x1f] = 0x9a; // call far
15800: mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
15801: mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
15802: mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
15803: mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
15804: } else {
15805: // invalid call addr :-(
15806: mem[DUMMY_TOP + 0x1f] = 0x90; // nop
15807: mem[DUMMY_TOP + 0x20] = 0x90; // nop
15808: mem[DUMMY_TOP + 0x21] = 0x90; // nop
15809: mem[DUMMY_TOP + 0x22] = 0x90; // nop
15810: mem[DUMMY_TOP + 0x23] = 0x90; // nop
15811: }
15812: // do call far (push cs/ip) in old mapping
15813: i386_call_far(DUMMY_TOP >> 4, 0x001f);
15814:
15815: // get old mapping data
15816: #if 0
15817: for(int i = 0; i < (int)old_entries; i++) {
15818: int logical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
15819: int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
15820:
15821: if(REG8(AL) == 0x01) {
15822: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15823: }
15824: // if(!(physical < 4)) {
15825: // REG8(AH) = 0x8b;
15826: // return;
15827: // } else
15828: if(logical == 0xffff) {
15829: ems_unmap_page(physical & 3);
15830: } else if(logical < ems_handles[REG16(DX)].pages) {
15831: ems_map_page(physical & 3, REG16(DX), logical);
15832: } else {
15833: REG8(AH) = 0x8a;
15834: return;
15835: }
15836: }
15837: #endif
15838: for(int i = 0; i < 4; i++) {
15839: handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
15840: pages [i] = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
15841: }
15842:
15843: // set new mapping
15844: for(int i = 0; i < (int)new_entries; i++) {
15845: int logical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
15846: int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
15847:
15848: if(REG8(AL) == 0x01) {
15849: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15850: }
15851: // if(!(physical < 4)) {
15852: // REG8(AH) = 0x8b;
15853: // return;
15854: // } else
15855: if(logical == 0xffff) {
15856: ems_unmap_page(physical & 3);
15857: } else if(logical < ems_handles[REG16(DX)].pages) {
15858: ems_map_page(physical & 3, REG16(DX), logical);
15859: } else {
15860: REG8(AH) = 0x8a;
15861: return;
15862: }
15863: }
15864:
15865: // push old mapping data in new mapping
15866: for(int i = 0; i < 4; i++) {
15867: i386_push16(handles[i]);
15868: i386_push16(pages [i]);
15869: }
15870: REG8(AH) = 0x00;
15871: } else {
15872: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
15873: REG8(AH) = 0x8f;
15874: }
15875: }
15876:
1.1.1.20 root 15877: inline void msdos_int_67h_57h_tmp()
15878: {
15879: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
15880: UINT8 src_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
15881: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
15882: UINT16 src_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
15883: UINT16 src_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
15884: UINT8 dest_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
15885: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
15886: UINT16 dest_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
15887: UINT16 dest_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
15888:
1.1.1.32 root 15889: UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20 root 15890: UINT32 src_addr, dest_addr;
15891: UINT32 src_addr_max, dest_addr_max;
15892:
15893: if(src_type == 0) {
15894: src_buffer = mem;
15895: src_addr = (src_seg << 4) + src_ofs;
15896: src_addr_max = MAX_MEM;
15897: } else {
1.1.1.31 root 15898: if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20 root 15899: REG8(AH) = 0x83;
15900: return;
15901: } else if(!(src_seg < ems_handles[src_handle].pages)) {
15902: REG8(AH) = 0x8a;
15903: return;
15904: }
1.1.1.32 root 15905: if(ems_handles[src_handle].buffer != NULL) {
15906: src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
15907: }
1.1.1.20 root 15908: src_addr = src_ofs;
1.1.1.32 root 15909: src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20 root 15910: }
15911: if(dest_type == 0) {
15912: dest_buffer = mem;
15913: dest_addr = (dest_seg << 4) + dest_ofs;
15914: dest_addr_max = MAX_MEM;
15915: } else {
1.1.1.31 root 15916: if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20 root 15917: REG8(AH) = 0x83;
15918: return;
15919: } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
15920: REG8(AH) = 0x8a;
15921: return;
15922: }
1.1.1.32 root 15923: if(ems_handles[dest_handle].buffer != NULL) {
15924: dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
15925: }
1.1.1.20 root 15926: dest_addr = dest_ofs;
1.1.1.32 root 15927: dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20 root 15928: }
1.1.1.32 root 15929: if(src_buffer != NULL && dest_buffer != NULL) {
15930: for(int i = 0; i < copy_length; i++) {
15931: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
15932: if(REG8(AL) == 0x00) {
15933: dest_buffer[dest_addr++] = src_buffer[src_addr++];
15934: } else if(REG8(AL) == 0x01) {
15935: UINT8 tmp = dest_buffer[dest_addr];
15936: dest_buffer[dest_addr++] = src_buffer[src_addr];
15937: src_buffer[src_addr++] = tmp;
15938: }
15939: } else {
15940: REG8(AH) = 0x93;
15941: return;
1.1.1.20 root 15942: }
15943: }
1.1.1.32 root 15944: REG8(AH) = 0x00;
15945: } else {
15946: REG8(AH) = 0x80;
1.1.1.20 root 15947: }
15948: }
15949:
15950: inline void msdos_int_67h_57h()
15951: {
15952: if(!support_ems) {
15953: REG8(AH) = 0x84;
15954: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15955: struct {
15956: UINT16 handle;
15957: UINT16 page;
15958: bool mapped;
15959: } tmp_pages[4];
15960:
15961: // unmap pages to copy memory data to ems buffer
15962: for(int i = 0; i < 4; i++) {
15963: tmp_pages[i].handle = ems_pages[i].handle;
15964: tmp_pages[i].page = ems_pages[i].page;
15965: tmp_pages[i].mapped = ems_pages[i].mapped;
15966: ems_unmap_page(i);
15967: }
15968:
15969: // run move/exchange operation
15970: msdos_int_67h_57h_tmp();
15971:
15972: // restore unmapped pages
15973: for(int i = 0; i < 4; i++) {
15974: if(tmp_pages[i].mapped) {
15975: ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
15976: }
15977: }
15978: } else {
1.1.1.22 root 15979: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 15980: REG8(AH) = 0x8f;
15981: }
15982: }
15983:
15984: inline void msdos_int_67h_58h()
15985: {
15986: if(!support_ems) {
15987: REG8(AH) = 0x84;
15988: } else if(REG8(AL) == 0x00) {
15989: for(int i = 0; i < 4; i++) {
1.1.1.30 root 15990: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
15991: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20 root 15992: }
15993: REG8(AH) = 0x00;
15994: REG16(CX) = 4;
15995: } else if(REG8(AL) == 0x01) {
15996: REG8(AH) = 0x00;
15997: REG16(CX) = 4;
15998: } else {
1.1.1.22 root 15999: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 16000: REG8(AH) = 0x8f;
16001: }
16002: }
16003:
1.1.1.42 root 16004: inline void msdos_int_67h_59h()
16005: {
16006: if(!support_ems) {
16007: REG8(AH) = 0x84;
16008: } else if(REG8(AL) == 0x00) {
1.1.1.49 root 16009: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
16010: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
16011: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
16012: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
16013: REG8(AH) = 0x00;
16014: // REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42 root 16015: } else if(REG8(AL) == 0x01) {
16016: REG8(AH) = 0x00;
16017: REG16(BX) = free_ems_pages;
16018: REG16(DX) = MAX_EMS_PAGES;
16019: } else {
16020: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16021: REG8(AH) = 0x8f;
16022: }
16023: }
16024:
1.1.1.20 root 16025: inline void msdos_int_67h_5ah()
16026: {
16027: if(!support_ems) {
1.1.1.19 root 16028: REG8(AH) = 0x84;
1.1.1.20 root 16029: } else if(REG16(BX) > MAX_EMS_PAGES) {
16030: REG8(AH) = 0x87;
16031: } else if(REG16(BX) > free_ems_pages) {
16032: REG8(AH) = 0x88;
16033: // } else if(REG16(BX) == 0) {
16034: // REG8(AH) = 0x89;
16035: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31 root 16036: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20 root 16037: if(!ems_handles[i].allocated) {
16038: ems_allocate_pages(i, REG16(BX));
16039: REG8(AH) = 0x00;
16040: REG16(DX) = i;
16041: return;
16042: }
16043: }
16044: REG8(AH) = 0x85;
16045: } else {
1.1.1.22 root 16046: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20 root 16047: REG8(AH) = 0x8f;
1.1.1.19 root 16048: }
16049: }
16050:
1.1.1.49 root 16051: inline void msdos_int_67h_5bh()
16052: {
16053: static UINT8 stored_bl = 0x00;
16054: static UINT16 stored_es = 0x0000;
16055: static UINT16 stored_di = 0x0000;
16056:
16057: if(!support_ems) {
16058: REG8(AH) = 0x84;
16059: } else if(REG8(AL) == 0x00) {
16060: if(stored_bl == 0x00) {
16061: if(!(stored_es == 0 && stored_di == 0)) {
16062: for(int i = 0; i < 4; i++) {
16063: *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
16064: *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
16065: }
16066: }
16067: SREG(ES) = stored_es;
16068: i386_load_segment_descriptor(ES);
16069: REG16(DI) = stored_di;
16070: } else {
16071: REG8(BL) = stored_bl;
16072: }
16073: REG8(AH) = 0x00;
16074: } else if(REG8(AL) == 0x01) {
16075: if(REG8(BL) == 0x00) {
16076: if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
16077: for(int i = 0; i < 4; i++) {
16078: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
16079: UINT16 page = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
16080:
16081: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
16082: ems_map_page(i, handle, page);
16083: } else {
16084: ems_unmap_page(i);
16085: }
16086: }
16087: }
16088: }
16089: stored_bl = REG8(BL);
16090: stored_es = SREG(ES);
16091: stored_di = REG16(DI);
16092: REG8(AH) = 0x00;
16093: } else if(REG8(AL) == 0x02) {
16094: REG16(DX) = 4 * 4;
16095: REG8(AH) = 0x00;
16096: } else if(REG8(AL) == 0x03) {
16097: REG8(BL) = 0x00; // not supported
16098: REG8(AH) = 0x00;
16099: } else if(REG8(AL) == 0x04) {
16100: REG8(AH) = 0x00;
16101: } else if(REG8(AL) == 0x05) {
16102: REG8(BL) = 0x00; // not supported
16103: REG8(AH) = 0x00;
16104: } else {
16105: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16106: REG8(AH) = 0x8f;
16107: }
16108: }
16109:
1.1.1.43 root 16110: inline void msdos_int_67h_5dh()
16111: {
16112: if(!support_ems) {
16113: REG8(AH) = 0x84;
16114: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
16115: REG8(AH) = 0xa4; // operating system denied access
16116: } else {
16117: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16118: REG8(AH) = 0x8f;
16119: }
16120: }
16121:
1.1.1.49 root 16122: inline void msdos_int_67h_70h()
16123: {
16124: if(!support_ems) {
16125: REG8(AH) = 0x84;
16126: } else if(REG8(AL) == 0x00) {
16127: REG8(AL) = 0x00;
16128: REG8(AH) = 0x00;
16129: } else if(REG8(AL) == 0x01) {
16130: REG8(AL) = 0x00;
16131: // REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
16132: REG8(AH) = 0x00;
16133: } else {
16134: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16135: REG8(AH) = 0x8f;
16136: }
16137: }
16138:
1.1.1.30 root 16139: inline void msdos_int_67h_deh()
16140: {
16141: REG8(AH) = 0x84;
16142: }
16143:
1.1.1.19 root 16144: #ifdef SUPPORT_XMS
16145:
1.1.1.32 root 16146: void msdos_xms_init()
1.1.1.26 root 16147: {
1.1.1.30 root 16148: emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
16149: emb_handle_top->address = EMB_TOP;
16150: emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26 root 16151: xms_a20_local_enb_count = 0;
16152: }
16153:
1.1.1.32 root 16154: void msdos_xms_finish()
16155: {
16156: msdos_xms_release();
16157: }
16158:
16159: void msdos_xms_release()
1.1.1.30 root 16160: {
16161: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
16162: emb_handle_t *next_handle = emb_handle->next;
16163: free(emb_handle);
16164: emb_handle = next_handle;
16165: }
16166: }
16167:
16168: emb_handle_t *msdos_xms_get_emb_handle(int handle)
16169: {
16170: if(handle != 0) {
16171: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16172: if(emb_handle->handle == handle) {
16173: return(emb_handle);
16174: }
16175: }
16176: }
16177: return(NULL);
16178: }
16179:
16180: int msdos_xms_get_unused_emb_handle_id()
16181: {
16182: for(int handle = 1;; handle++) {
16183: if(msdos_xms_get_emb_handle(handle) == NULL) {
16184: return(handle);
16185: }
16186: }
16187: return(0);
16188: }
16189:
16190: int msdos_xms_get_unused_emb_handle_count()
16191: {
16192: int count = 64; //255;
16193:
16194: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16195: if(emb_handle->handle != 0) {
16196: if(--count == 1) {
16197: break;
16198: }
16199: }
16200: }
16201: return(count);
16202: }
16203:
16204: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
16205: {
16206: if(emb_handle->size_kb > size_kb) {
16207: emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
16208:
16209: new_handle->address = emb_handle->address + size_kb * 1024;
16210: new_handle->size_kb = emb_handle->size_kb - size_kb;
16211: emb_handle->size_kb = size_kb;
16212:
16213: new_handle->prev = emb_handle;
16214: new_handle->next = emb_handle->next;
16215: if(emb_handle->next != NULL) {
16216: emb_handle->next->prev = new_handle;
16217: }
16218: emb_handle->next = new_handle;
16219: }
16220: }
16221:
16222: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
16223: {
16224: emb_handle_t *next_handle = emb_handle->next;
16225:
16226: if(next_handle != NULL) {
16227: emb_handle->size_kb += next_handle->size_kb;
16228:
16229: if(next_handle->next != NULL) {
16230: next_handle->next->prev = emb_handle;
16231: }
16232: emb_handle->next = next_handle->next;
16233: free(next_handle);
16234: }
16235: }
16236:
16237: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
16238: {
16239: emb_handle_t *target_handle = NULL;
16240:
16241: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16242: if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
16243: if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
16244: target_handle = emb_handle;
16245: }
16246: }
16247: }
16248: if(target_handle != NULL) {
16249: if(target_handle->size_kb > size_kb) {
16250: msdos_xms_split_emb_handle(target_handle, size_kb);
16251: }
16252: // target_handle->handle = msdos_xms_get_unused_emb_handle_id();
16253: return(target_handle);
16254: }
16255: return(NULL);
16256: }
16257:
16258: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
16259: {
16260: emb_handle_t *prev_handle = emb_handle->prev;
16261: emb_handle_t *next_handle = emb_handle->next;
16262:
16263: if(prev_handle != NULL && prev_handle->handle == 0) {
16264: msdos_xms_combine_emb_handles(prev_handle);
16265: emb_handle = prev_handle;
16266: }
16267: if(next_handle != NULL && next_handle->handle == 0) {
16268: msdos_xms_combine_emb_handles(emb_handle);
16269: }
16270: emb_handle->handle = 0;
16271: }
16272:
1.1.1.19 root 16273: inline void msdos_call_xms_00h()
16274: {
1.1.1.29 root 16275: #if defined(HAS_I386)
16276: REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45 root 16277: // REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29 root 16278: REG16(BX) = 0x035f; // V3.95 (Driver Revision)
16279: #else
16280: REG16(AX) = 0x0200; // V2.00 (XMS Version)
16281: REG16(BX) = 0x0270; // V2.70 (Driver Revision)
16282: #endif
16283: // REG16(DX) = 0x0000; // HMA does not exist
16284: REG16(DX) = 0x0001; // HMA does exist
1.1.1.19 root 16285: }
16286:
16287: inline void msdos_call_xms_01h()
16288: {
1.1.1.29 root 16289: if(REG8(AL) == 0x40) {
16290: // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
16291: // DX=KB free extended memory returned by last call of function 08h
16292: REG16(AX) = 0x0000;
16293: REG8(BL) = 0x91;
16294: REG16(DX) = xms_dx_after_call_08h;
16295: } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
16296: REG16(AX) = 0x0000;
16297: REG8(BL) = 0x81; // Vdisk was detected
16298: #ifdef SUPPORT_HMA
16299: } else if(is_hma_used_by_int_2fh) {
16300: REG16(AX) = 0x0000;
16301: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
16302: } else if(is_hma_used_by_xms) {
16303: REG16(AX) = 0x0000;
16304: REG8(BL) = 0x91; // HMA is already in use
16305: } else {
16306: REG16(AX) = 0x0001;
16307: is_hma_used_by_xms = true;
16308: #else
16309: } else {
16310: REG16(AX) = 0x0000;
16311: REG8(BL) = 0x91; // HMA is already in use
16312: #endif
16313: }
1.1.1.19 root 16314: }
16315:
16316: inline void msdos_call_xms_02h()
16317: {
1.1.1.29 root 16318: if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
16319: REG16(AX) = 0x0000;
16320: REG8(BL) = 0x81; // Vdisk was detected
16321: #ifdef SUPPORT_HMA
16322: } else if(is_hma_used_by_int_2fh) {
16323: REG16(AX) = 0x0000;
16324: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
16325: } else if(!is_hma_used_by_xms) {
16326: REG16(AX) = 0x0000;
16327: REG8(BL) = 0x93; // HMA is not allocated
16328: } else {
16329: REG16(AX) = 0x0001;
16330: is_hma_used_by_xms = false;
16331: // restore first free mcb in high memory area
16332: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
16333: #else
16334: } else {
16335: REG16(AX) = 0x0000;
16336: REG8(BL) = 0x91; // HMA is already in use
16337: #endif
16338: }
1.1.1.19 root 16339: }
16340:
16341: inline void msdos_call_xms_03h()
16342: {
16343: i386_set_a20_line(1);
16344: REG16(AX) = 0x0001;
16345: REG8(BL) = 0x00;
16346: }
16347:
16348: inline void msdos_call_xms_04h()
16349: {
1.1.1.21 root 16350: i386_set_a20_line(0);
16351: REG16(AX) = 0x0001;
16352: REG8(BL) = 0x00;
1.1.1.19 root 16353: }
16354:
16355: inline void msdos_call_xms_05h()
16356: {
16357: i386_set_a20_line(1);
16358: REG16(AX) = 0x0001;
16359: REG8(BL) = 0x00;
1.1.1.21 root 16360: xms_a20_local_enb_count++;
1.1.1.19 root 16361: }
16362:
16363: void msdos_call_xms_06h()
16364: {
1.1.1.21 root 16365: if(xms_a20_local_enb_count > 0) {
1.1.1.45 root 16366: if(--xms_a20_local_enb_count == 0) {
16367: i386_set_a20_line(0);
16368: REG16(AX) = 0x0001;
16369: REG8(BL) = 0x00;
16370: } else {
16371: REG16(AX) = 0x0000;
16372: REG8(BL) = 0x94;
16373: }
1.1.1.21 root 16374: } else {
1.1.1.45 root 16375: i386_set_a20_line(0);
1.1.1.21 root 16376: REG16(AX) = 0x0001;
16377: REG8(BL) = 0x00;
1.1.1.19 root 16378: }
16379: }
16380:
16381: inline void msdos_call_xms_07h()
16382: {
16383: REG16(AX) = (m_a20_mask >> 20) & 1;
16384: REG8(BL) = 0x00;
16385: }
16386:
16387: inline void msdos_call_xms_08h()
16388: {
1.1.1.45 root 16389: UINT32 eax = 0, edx = 0;
1.1.1.19 root 16390:
1.1.1.30 root 16391: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16392: if(emb_handle->handle == 0) {
1.1.1.45 root 16393: if(eax < emb_handle->size_kb) {
16394: eax = emb_handle->size_kb;
1.1.1.19 root 16395: }
1.1.1.45 root 16396: edx += emb_handle->size_kb;
1.1.1.19 root 16397: }
16398: }
1.1.1.45 root 16399: if(eax > 65535) {
16400: eax = 65535;
16401: }
16402: if(edx > 65535) {
16403: edx = 65535;
16404: }
16405: if(eax == 0 && edx == 0) {
1.1.1.19 root 16406: REG8(BL) = 0xa0;
16407: } else {
16408: REG8(BL) = 0x00;
16409: }
1.1.1.45 root 16410: #if defined(HAS_I386)
16411: REG32(EAX) = eax;
16412: REG32(EDX) = edx;
16413: #else
16414: REG16(AX) = (UINT16)eax;
16415: REG16(DX) = (UINT16)edx;
16416: #endif
1.1.1.29 root 16417: xms_dx_after_call_08h = REG16(DX);
1.1.1.19 root 16418: }
16419:
1.1.1.30 root 16420: void msdos_call_xms_09h(int size_kb)
1.1.1.19 root 16421: {
1.1.1.30 root 16422: emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
16423:
16424: if(emb_handle != NULL) {
16425: emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
16426:
16427: REG16(AX) = 0x0001;
16428: REG16(DX) = emb_handle->handle;
16429: REG8(BL) = 0x00;
16430: } else {
16431: REG16(AX) = REG16(DX) = 0x0000;
16432: REG8(BL) = 0xa0;
1.1.1.19 root 16433: }
1.1.1.30 root 16434: }
16435:
16436: inline void msdos_call_xms_09h()
16437: {
16438: msdos_call_xms_09h(REG16(DX));
1.1.1.19 root 16439: }
16440:
16441: inline void msdos_call_xms_0ah()
16442: {
1.1.1.30 root 16443: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16444:
16445: if(emb_handle == NULL) {
1.1.1.19 root 16446: REG16(AX) = 0x0000;
16447: REG8(BL) = 0xa2;
1.1.1.45 root 16448: // } else if(emb_handle->lock > 0) {
16449: // REG16(AX) = 0x0000;
16450: // REG8(BL) = 0xab;
1.1.1.19 root 16451: } else {
1.1.1.30 root 16452: msdos_xms_free_emb_handle(emb_handle);
1.1.1.19 root 16453:
16454: REG16(AX) = 0x0001;
16455: REG8(BL) = 0x00;
16456: }
16457: }
16458:
16459: inline void msdos_call_xms_0bh()
16460: {
16461: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
16462: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
16463: UINT32 src_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
16464: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
16465: UINT32 dest_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
16466:
16467: UINT8 *src_buffer, *dest_buffer;
16468: UINT32 src_addr_max, dest_addr_max;
1.1.1.30 root 16469: emb_handle_t *emb_handle;
1.1.1.19 root 16470:
16471: if(src_handle == 0) {
16472: src_buffer = mem;
16473: src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
16474: src_addr_max = MAX_MEM;
16475: } else {
1.1.1.30 root 16476: if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19 root 16477: REG16(AX) = 0x0000;
16478: REG8(BL) = 0xa3;
16479: return;
16480: }
1.1.1.30 root 16481: src_buffer = mem + emb_handle->address;
16482: src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 16483: }
16484: if(dest_handle == 0) {
16485: dest_buffer = mem;
16486: dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
16487: dest_addr_max = MAX_MEM;
16488: } else {
1.1.1.30 root 16489: if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19 root 16490: REG16(AX) = 0x0000;
16491: REG8(BL) = 0xa5;
16492: return;
16493: }
1.1.1.30 root 16494: dest_buffer = mem + emb_handle->address;
16495: dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 16496: }
16497: for(int i = 0; i < copy_length; i++) {
16498: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
16499: dest_buffer[dest_addr++] = src_buffer[src_addr++];
16500: } else {
16501: break;
16502: }
16503: }
16504: REG16(AX) = 0x0001;
16505: REG8(BL) = 0x00;
16506: }
16507:
16508: inline void msdos_call_xms_0ch()
16509: {
1.1.1.30 root 16510: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16511:
16512: if(emb_handle == NULL) {
1.1.1.19 root 16513: REG16(AX) = 0x0000;
16514: REG8(BL) = 0xa2;
16515: } else {
1.1.1.45 root 16516: if(emb_handle->lock < 255) {
16517: emb_handle->lock++;
16518: }
1.1.1.19 root 16519: REG16(AX) = 0x0001;
1.1.1.30 root 16520: REG16(DX) = (emb_handle->address >> 16) & 0xffff;
16521: REG16(BX) = (emb_handle->address ) & 0xffff;
1.1.1.19 root 16522: }
16523: }
16524:
16525: inline void msdos_call_xms_0dh()
16526: {
1.1.1.30 root 16527: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16528:
16529: if(emb_handle == NULL) {
1.1.1.19 root 16530: REG16(AX) = 0x0000;
16531: REG8(BL) = 0xa2;
1.1.1.30 root 16532: } else if(!(emb_handle->lock > 0)) {
1.1.1.19 root 16533: REG16(AX) = 0x0000;
16534: REG8(BL) = 0xaa;
16535: } else {
1.1.1.30 root 16536: emb_handle->lock--;
1.1.1.19 root 16537: REG16(AX) = 0x0001;
16538: REG8(BL) = 0x00;
16539: }
16540: }
16541:
16542: inline void msdos_call_xms_0eh()
16543: {
1.1.1.30 root 16544: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16545:
16546: if(emb_handle == NULL) {
1.1.1.19 root 16547: REG16(AX) = 0x0000;
16548: REG8(BL) = 0xa2;
16549: } else {
16550: REG16(AX) = 0x0001;
1.1.1.30 root 16551: REG8(BH) = emb_handle->lock;
16552: REG8(BL) = msdos_xms_get_unused_emb_handle_count();
16553: REG16(DX) = emb_handle->size_kb;
1.1.1.19 root 16554: }
16555: }
16556:
1.1.1.30 root 16557: void msdos_call_xms_0fh(int size_kb)
1.1.1.19 root 16558: {
1.1.1.30 root 16559: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16560:
16561: if(emb_handle == NULL) {
1.1.1.19 root 16562: REG16(AX) = 0x0000;
16563: REG8(BL) = 0xa2;
1.1.1.30 root 16564: } else if(emb_handle->lock > 0) {
1.1.1.19 root 16565: REG16(AX) = 0x0000;
16566: REG8(BL) = 0xab;
16567: } else {
1.1.1.30 root 16568: if(emb_handle->size_kb < size_kb) {
16569: if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
16570: msdos_xms_combine_emb_handles(emb_handle);
16571: if(emb_handle->size_kb > size_kb) {
16572: msdos_xms_split_emb_handle(emb_handle, size_kb);
16573: }
16574: } else {
16575: int old_handle = emb_handle->handle;
16576: int old_size_kb = emb_handle->size_kb;
16577: UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
16578:
16579: memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
16580: msdos_xms_free_emb_handle(emb_handle);
16581:
16582: if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
16583: emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
16584: }
16585: emb_handle->handle = old_handle;
16586: memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
16587: free(buffer);
16588: }
16589: } else if(emb_handle->size_kb > size_kb) {
16590: msdos_xms_split_emb_handle(emb_handle, size_kb);
16591: }
16592: if(emb_handle->size_kb != size_kb) {
16593: REG16(AX) = 0x0000;
16594: REG8(BL) = 0xa0;
16595: } else {
16596: REG16(AX) = 0x0001;
16597: REG8(BL) = 0x00;
16598: }
1.1.1.19 root 16599: }
16600: }
16601:
1.1.1.30 root 16602: inline void msdos_call_xms_0fh()
16603: {
16604: msdos_call_xms_0fh(REG16(BX));
16605: }
16606:
1.1.1.19 root 16607: inline void msdos_call_xms_10h()
16608: {
16609: int seg;
16610:
16611: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
16612: REG16(AX) = 0x0001;
16613: REG16(BX) = seg;
16614: } else {
16615: REG16(AX) = 0x0000;
16616: REG8(BL) = 0xb0;
16617: REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
16618: }
16619: }
16620:
16621: inline void msdos_call_xms_11h()
16622: {
16623: int mcb_seg = REG16(DX) - 1;
16624: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
16625:
16626: if(mcb->mz == 'M' || mcb->mz == 'Z') {
16627: msdos_mem_free(REG16(DX));
16628: REG16(AX) = 0x0001;
16629: REG8(BL) = 0x00;
16630: } else {
16631: REG16(AX) = 0x0000;
16632: REG8(BL) = 0xb2;
16633: }
16634: }
16635:
16636: inline void msdos_call_xms_12h()
16637: {
16638: int mcb_seg = REG16(DX) - 1;
16639: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
16640: int max_paragraphs;
16641:
16642: if(mcb->mz == 'M' || mcb->mz == 'Z') {
16643: if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
16644: REG16(AX) = 0x0001;
16645: REG8(BL) = 0x00;
16646: } else {
16647: REG16(AX) = 0x0000;
16648: REG8(BL) = 0xb0;
16649: REG16(DX) = max_paragraphs;
16650: }
16651: } else {
16652: REG16(AX) = 0x0000;
16653: REG8(BL) = 0xb2;
16654: }
16655: }
16656:
1.1.1.29 root 16657: #if defined(HAS_I386)
16658:
16659: inline void msdos_call_xms_88h()
16660: {
16661: REG32(EAX) = REG32(EDX) = 0x0000;
16662:
1.1.1.30 root 16663: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16664: if(emb_handle->handle == 0) {
16665: if(REG32(EAX) < emb_handle->size_kb) {
16666: REG32(EAX) = emb_handle->size_kb;
1.1.1.29 root 16667: }
1.1.1.30 root 16668: REG32(EDX) += emb_handle->size_kb;
1.1.1.29 root 16669: }
16670: }
16671: if(REG32(EAX) == 0 && REG32(EDX) == 0) {
16672: REG8(BL) = 0xa0;
16673: } else {
16674: REG8(BL) = 0x00;
16675: }
16676: REG32(ECX) = EMB_END - 1;
16677: }
16678:
16679: inline void msdos_call_xms_89h()
16680: {
1.1.1.30 root 16681: msdos_call_xms_09h(REG32(EDX));
1.1.1.29 root 16682: }
16683:
16684: inline void msdos_call_xms_8eh()
16685: {
1.1.1.30 root 16686: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16687:
16688: if(emb_handle == NULL) {
1.1.1.29 root 16689: REG16(AX) = 0x0000;
16690: REG8(BL) = 0xa2;
16691: } else {
16692: REG16(AX) = 0x0001;
1.1.1.30 root 16693: REG8(BH) = emb_handle->lock;
16694: REG16(CX) = msdos_xms_get_unused_emb_handle_count();
16695: REG32(EDX) = emb_handle->size_kb;
1.1.1.29 root 16696: }
16697: }
16698:
16699: inline void msdos_call_xms_8fh()
16700: {
1.1.1.30 root 16701: msdos_call_xms_0fh(REG32(EBX));
1.1.1.29 root 16702: }
16703:
16704: #endif
1.1.1.19 root 16705: #endif
16706:
1.1.1.26 root 16707: UINT16 msdos_get_equipment()
16708: {
16709: static UINT16 equip = 0;
16710:
16711: if(equip == 0) {
16712: #ifdef SUPPORT_FPU
16713: equip |= (1 << 1); // 80x87 coprocessor installed
16714: #endif
16715: equip |= (1 << 2); // pointing device installed (PS/2)
16716: equip |= (2 << 4); // initial video mode (80x25 color)
16717: // equip |= (1 << 8); // 0 if DMA installed
16718: equip |= (2 << 9); // number of serial ports
16719: equip |= (3 << 14); // number of printer ports (NOTE: this number is 3 on Windows 98 SE though only LPT1 exists)
1.1.1.28 root 16720:
16721: // check only A: and B: if it is floppy drive
16722: int n = 0;
16723: for(int i = 0; i < 2; i++) {
1.1.1.44 root 16724: if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
16725: n++;
1.1.1.28 root 16726: }
16727: }
16728: if(n != 0) {
16729: equip |= (1 << 0); // floppy disk(s) installed
16730: n--;
16731: equip |= (n << 6); // number of floppies installed less 1
16732: }
16733: // if(joyGetNumDevs() != 0) {
16734: // equip |= (1 << 12); // game port installed
16735: // }
1.1.1.26 root 16736: }
16737: return(equip);
16738: }
16739:
1.1 root 16740: void msdos_syscall(unsigned num)
16741: {
1.1.1.22 root 16742: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43 root 16743: if(num == 0x08 || num == 0x1c) {
16744: // don't log the timer interrupts
1.1.1.45 root 16745: // fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.50 root 16746: } else if(num == 0x30) {
16747: // dummy interrupt for call 0005h (call near)
16748: fprintf(fp_debug_log, "call 0005h (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59 root 16749: } else if(num == 0x65) {
1.1.1.22 root 16750: // dummy interrupt for EMS (int 67h)
1.1.1.33 root 16751: fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59 root 16752: } else if(num == 0x66) {
1.1.1.22 root 16753: // dummy interrupt for XMS (call far)
1.1.1.33 root 16754: fprintf(fp_debug_log, "call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59 root 16755: } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45 root 16756: // dummy interrupt
1.1.1.22 root 16757: } else {
1.1.1.33 root 16758: fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.22 root 16759: }
16760: #endif
1.1.1.36 root 16761: // update cursor position
16762: if(cursor_moved) {
16763: pcbios_update_cursor_position();
16764: cursor_moved = false;
16765: }
1.1.1.50 root 16766: #ifdef USE_SERVICE_THREAD
16767: // this is called from dummy loop to wait until a serive that waits input is done
16768: if(!in_service)
16769: #endif
1.1.1.33 root 16770: ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22 root 16771:
1.1 root 16772: switch(num) {
16773: case 0x00:
1.1.1.28 root 16774: try {
16775: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16776: error("division by zero\n");
16777: } catch(...) {
16778: fatalerror("division by zero detected, and failed to terminate current process\n");
16779: }
1.1 root 16780: break;
16781: case 0x04:
1.1.1.28 root 16782: try {
16783: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16784: error("overflow\n");
16785: } catch(...) {
16786: fatalerror("overflow detected, and failed to terminate current process\n");
16787: }
1.1 root 16788: break;
16789: case 0x06:
16790: // NOTE: ish.com has illegal instruction...
1.1.1.14 root 16791: if(!ignore_illegal_insn) {
1.1.1.28 root 16792: try {
16793: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16794: error("illegal instruction\n");
16795: } catch(...) {
16796: fatalerror("illegal instruction detected, and failed to terminate current process\n");
16797: }
1.1.1.14 root 16798: } else {
16799: #if defined(HAS_I386)
1.1.1.39 root 16800: m_eip = m_int6h_skip_eip;
16801: #elif defined(HAS_I286)
16802: m_pc = m_int6h_skip_pc;
1.1.1.14 root 16803: #else
1.1.1.39 root 16804: // 8086/80186 ignore an invalid opcode
1.1.1.14 root 16805: #endif
16806: }
1.1 root 16807: break;
1.1.1.33 root 16808: case 0x09:
16809: // ctrl-break is pressed
16810: if(raise_int_1bh) {
16811: #if defined(HAS_I386)
16812: m_ext = 0; // not an external interrupt
16813: i386_trap(0x1b, 1, 0);
16814: m_ext = 1;
16815: #else
16816: PREFIX86(_interrupt)(0x1b);
16817: #endif
16818: raise_int_1bh = false;
16819: }
1.1.1.8 root 16820: case 0x08:
1.1.1.14 root 16821: // pcbios_irq0(); // this causes too slow emulation...
1.1.1.8 root 16822: case 0x0b:
16823: case 0x0c:
16824: case 0x0d:
16825: case 0x0e:
16826: case 0x0f:
16827: // EOI
16828: pic[0].isr &= ~(1 << (num - 0x08));
16829: pic_update();
16830: break;
1.1 root 16831: case 0x10:
16832: // PC BIOS - Video
1.1.1.14 root 16833: if(!restore_console_on_exit) {
1.1.1.15 root 16834: change_console_size(scr_width, scr_height);
1.1 root 16835: }
1.1.1.3 root 16836: m_CF = 0;
1.1 root 16837: switch(REG8(AH)) {
1.1.1.16 root 16838: case 0x00: pcbios_int_10h_00h(); break;
1.1 root 16839: case 0x01: pcbios_int_10h_01h(); break;
16840: case 0x02: pcbios_int_10h_02h(); break;
16841: case 0x03: pcbios_int_10h_03h(); break;
16842: case 0x05: pcbios_int_10h_05h(); break;
16843: case 0x06: pcbios_int_10h_06h(); break;
16844: case 0x07: pcbios_int_10h_07h(); break;
16845: case 0x08: pcbios_int_10h_08h(); break;
16846: case 0x09: pcbios_int_10h_09h(); break;
16847: case 0x0a: pcbios_int_10h_0ah(); break;
16848: case 0x0b: break;
1.1.1.40 root 16849: case 0x0c: pcbios_int_10h_0ch(); break;
16850: case 0x0d: pcbios_int_10h_0dh(); break;
1.1 root 16851: case 0x0e: pcbios_int_10h_0eh(); break;
16852: case 0x0f: pcbios_int_10h_0fh(); break;
16853: case 0x10: break;
1.1.1.14 root 16854: case 0x11: pcbios_int_10h_11h(); break;
16855: case 0x12: pcbios_int_10h_12h(); break;
1.1 root 16856: case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30 root 16857: case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14 root 16858: case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24 root 16859: case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
16860: case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1 root 16861: case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24 root 16862: case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
16863: case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22 root 16864: case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30 root 16865: case 0x6f: break;
1.1.1.22 root 16866: case 0x80: m_CF = 1; break; // unknown
16867: case 0x81: m_CF = 1; break; // unknown
1.1 root 16868: case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22 root 16869: case 0x83: pcbios_int_10h_83h(); break;
16870: case 0x8b: break;
16871: case 0x8c: m_CF = 1; break; // unknown
16872: case 0x8d: m_CF = 1; break; // unknown
16873: case 0x8e: m_CF = 1; break; // unknown
16874: case 0x90: pcbios_int_10h_90h(); break;
16875: case 0x91: pcbios_int_10h_91h(); break;
16876: case 0x92: break;
16877: case 0x93: break;
16878: case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24 root 16879: case 0xfa: break; // ega register interface library is not installed
1.1 root 16880: case 0xfe: pcbios_int_10h_feh(); break;
16881: case 0xff: pcbios_int_10h_ffh(); break;
16882: default:
1.1.1.22 root 16883: unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16884: m_CF = 1;
1.1 root 16885: break;
16886: }
16887: break;
16888: case 0x11:
16889: // PC BIOS - Get Equipment List
1.1.1.26 root 16890: REG16(AX) = msdos_get_equipment();
1.1 root 16891: break;
16892: case 0x12:
16893: // PC BIOS - Get Memory Size
1.1.1.33 root 16894: REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1 root 16895: break;
16896: case 0x13:
1.1.1.42 root 16897: // PC BIOS - Disk I/O
16898: {
16899: static UINT8 last = 0x00;
16900: switch(REG8(AH)) {
16901: case 0x00: pcbios_int_13h_00h(); break;
16902: case 0x01: // get last status
16903: REG8(AH) = last;
16904: break;
16905: case 0x02: pcbios_int_13h_02h(); break;
16906: case 0x03: pcbios_int_13h_03h(); break;
16907: case 0x04: pcbios_int_13h_04h(); break;
16908: case 0x08: pcbios_int_13h_08h(); break;
16909: case 0x0a: pcbios_int_13h_02h(); break;
16910: case 0x0b: pcbios_int_13h_03h(); break;
16911: case 0x0d: pcbios_int_13h_00h(); break;
16912: case 0x10: pcbios_int_13h_10h(); break;
16913: case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43 root 16914: case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42 root 16915: case 0x05: // format
16916: case 0x06:
16917: case 0x07:
16918: REG8(AH) = 0x0c; // unsupported track or invalid media
16919: m_CF = 1;
16920: break;
16921: case 0x09:
16922: case 0x0c: // seek
16923: case 0x11: // recalib
16924: case 0x14:
16925: case 0x17:
16926: REG8(AH) = 0x00; // successful completion
16927: break;
1.1.1.43 root 16928: case 0x21: // QUICKCACHE II v4.20 - Flush Cache
16929: case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
16930: REG8(AH) = 0x01; // invalid function
16931: m_CF = 1;
16932: break;
1.1.1.42 root 16933: default:
16934: unimplemented_13h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16935: REG8(AH) = 0x01; // invalid function
16936: m_CF = 1;
16937: break;
16938: }
16939: last = REG8(AH);
16940: }
1.1 root 16941: break;
16942: case 0x14:
16943: // PC BIOS - Serial I/O
1.1.1.25 root 16944: switch(REG8(AH)) {
16945: case 0x00: pcbios_int_14h_00h(); break;
16946: case 0x01: pcbios_int_14h_01h(); break;
16947: case 0x02: pcbios_int_14h_02h(); break;
16948: case 0x03: pcbios_int_14h_03h(); break;
16949: case 0x04: pcbios_int_14h_04h(); break;
16950: case 0x05: pcbios_int_14h_05h(); break;
16951: default:
16952: unimplemented_14h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16953: break;
16954: }
1.1 root 16955: break;
16956: case 0x15:
16957: // PC BIOS
1.1.1.3 root 16958: m_CF = 0;
1.1 root 16959: switch(REG8(AH)) {
1.1.1.14 root 16960: case 0x10: pcbios_int_15h_10h(); break;
1.1 root 16961: case 0x23: pcbios_int_15h_23h(); break;
16962: case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24 root 16963: case 0x41: break;
1.1 root 16964: case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22 root 16965: case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30 root 16966: case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43 root 16967: case 0x84: pcbios_int_15h_84h(); break;
1.1 root 16968: case 0x86: pcbios_int_15h_86h(); break;
16969: case 0x87: pcbios_int_15h_87h(); break;
16970: case 0x88: pcbios_int_15h_88h(); break;
16971: case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21 root 16972: case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22 root 16973: case 0xc0: // PS/2 ???
1.1.1.54 root 16974: #ifndef EXT_BIOS_TOP
1.1.1.22 root 16975: case 0xc1:
1.1.1.54 root 16976: #endif
1.1.1.30 root 16977: case 0xc3: // PS50+ ???
16978: case 0xc4:
1.1.1.22 root 16979: REG8(AH) = 0x86;
16980: m_CF = 1;
16981: break;
1.1.1.54 root 16982: #ifdef EXT_BIOS_TOP
16983: case 0xc1: pcbios_int_15h_c1h(); break;
16984: #endif
16985: case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3 root 16986: #if defined(HAS_I386)
1.1 root 16987: case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3 root 16988: #endif
1.1 root 16989: case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22 root 16990: case 0xe8: pcbios_int_15h_e8h(); break;
1.1 root 16991: default:
1.1.1.22 root 16992: unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
16993: REG8(AH) = 0x86;
1.1.1.3 root 16994: m_CF = 1;
1.1 root 16995: break;
16996: }
16997: break;
16998: case 0x16:
16999: // PC BIOS - Keyboard
1.1.1.3 root 17000: m_CF = 0;
1.1 root 17001: switch(REG8(AH)) {
17002: case 0x00: pcbios_int_16h_00h(); break;
17003: case 0x01: pcbios_int_16h_01h(); break;
17004: case 0x02: pcbios_int_16h_02h(); break;
17005: case 0x03: pcbios_int_16h_03h(); break;
17006: case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60! root 17007: case 0x09: pcbios_int_16h_09h(); break;
! 17008: case 0x0a: pcbios_int_16h_0ah(); break;
1.1 root 17009: case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60! root 17010: case 0x11: pcbios_int_16h_11h(); break;
1.1 root 17011: case 0x12: pcbios_int_16h_12h(); break;
17012: case 0x13: pcbios_int_16h_13h(); break;
17013: case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24 root 17014: case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30 root 17015: case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22 root 17016: case 0xda: break; // unknown
1.1.1.43 root 17017: case 0xdb: break; // unknown
1.1.1.22 root 17018: case 0xff: break; // unknown
1.1 root 17019: default:
1.1.1.22 root 17020: unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 17021: break;
17022: }
17023: break;
17024: case 0x17:
17025: // PC BIOS - Printer
1.1.1.37 root 17026: m_CF = 0;
17027: switch(REG8(AH)) {
17028: case 0x00: pcbios_int_17h_00h(); break;
17029: case 0x01: pcbios_int_17h_01h(); break;
17030: case 0x02: pcbios_int_17h_02h(); break;
17031: case 0x03: pcbios_int_17h_03h(); break;
17032: case 0x50: pcbios_int_17h_50h(); break;
17033: case 0x51: pcbios_int_17h_51h(); break;
17034: case 0x52: pcbios_int_17h_52h(); break;
17035: case 0x84: pcbios_int_17h_84h(); break;
17036: case 0x85: pcbios_int_17h_85h(); break;
17037: default:
17038: unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17039: break;
17040: }
1.1 root 17041: break;
17042: case 0x1a:
17043: // PC BIOS - Timer
1.1.1.3 root 17044: m_CF = 0;
1.1 root 17045: switch(REG8(AH)) {
17046: case 0x00: pcbios_int_1ah_00h(); break;
17047: case 0x01: break;
17048: case 0x02: pcbios_int_1ah_02h(); break;
17049: case 0x03: break;
17050: case 0x04: pcbios_int_1ah_04h(); break;
17051: case 0x05: break;
17052: case 0x0a: pcbios_int_1ah_0ah(); break;
17053: case 0x0b: break;
1.1.1.14 root 17054: case 0x35: break; // Word Perfect Third Party Interface?
17055: case 0x36: break; // Word Perfect Third Party Interface
17056: case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44 root 17057: case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43 root 17058: case 0xb1: break; // PCI BIOS v2.0c+
17059: case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1 root 17060: default:
1.1.1.22 root 17061: unimplemented_1ah("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 17062: break;
17063: }
17064: break;
1.1.1.33 root 17065: case 0x1b:
17066: mem[0x471] = 0x00;
17067: break;
1.1 root 17068: case 0x20:
1.1.1.28 root 17069: try {
17070: msdos_process_terminate(SREG(CS), retval, 1);
17071: } catch(...) {
17072: fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
17073: }
1.1 root 17074: break;
1.1.1.49 root 17075: case 0x30:
1.1.1.46 root 17076: // dummy interrupt for case map routine pointed in the country info
17077: // if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
17078: // REG8(AL) = 0x00;
17079: // break;
17080: // }
1.1 root 17081: case 0x21:
17082: // MS-DOS System Call
1.1.1.3 root 17083: m_CF = 0;
1.1.1.28 root 17084: try {
1.1.1.46 root 17085: switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28 root 17086: case 0x00: msdos_int_21h_00h(); break;
17087: case 0x01: msdos_int_21h_01h(); break;
17088: case 0x02: msdos_int_21h_02h(); break;
17089: case 0x03: msdos_int_21h_03h(); break;
17090: case 0x04: msdos_int_21h_04h(); break;
17091: case 0x05: msdos_int_21h_05h(); break;
17092: case 0x06: msdos_int_21h_06h(); break;
17093: case 0x07: msdos_int_21h_07h(); break;
17094: case 0x08: msdos_int_21h_08h(); break;
17095: case 0x09: msdos_int_21h_09h(); break;
17096: case 0x0a: msdos_int_21h_0ah(); break;
17097: case 0x0b: msdos_int_21h_0bh(); break;
17098: case 0x0c: msdos_int_21h_0ch(); break;
17099: case 0x0d: msdos_int_21h_0dh(); break;
17100: case 0x0e: msdos_int_21h_0eh(); break;
17101: case 0x0f: msdos_int_21h_0fh(); break;
17102: case 0x10: msdos_int_21h_10h(); break;
17103: case 0x11: msdos_int_21h_11h(); break;
17104: case 0x12: msdos_int_21h_12h(); break;
17105: case 0x13: msdos_int_21h_13h(); break;
17106: case 0x14: msdos_int_21h_14h(); break;
17107: case 0x15: msdos_int_21h_15h(); break;
17108: case 0x16: msdos_int_21h_16h(); break;
17109: case 0x17: msdos_int_21h_17h(); break;
17110: case 0x18: msdos_int_21h_18h(); break;
17111: case 0x19: msdos_int_21h_19h(); break;
17112: case 0x1a: msdos_int_21h_1ah(); break;
17113: case 0x1b: msdos_int_21h_1bh(); break;
17114: case 0x1c: msdos_int_21h_1ch(); break;
17115: case 0x1d: msdos_int_21h_1dh(); break;
17116: case 0x1e: msdos_int_21h_1eh(); break;
17117: case 0x1f: msdos_int_21h_1fh(); break;
17118: case 0x20: msdos_int_21h_20h(); break;
17119: case 0x21: msdos_int_21h_21h(); break;
17120: case 0x22: msdos_int_21h_22h(); break;
17121: case 0x23: msdos_int_21h_23h(); break;
17122: case 0x24: msdos_int_21h_24h(); break;
17123: case 0x25: msdos_int_21h_25h(); break;
17124: case 0x26: msdos_int_21h_26h(); break;
17125: case 0x27: msdos_int_21h_27h(); break;
17126: case 0x28: msdos_int_21h_28h(); break;
17127: case 0x29: msdos_int_21h_29h(); break;
17128: case 0x2a: msdos_int_21h_2ah(); break;
17129: case 0x2b: msdos_int_21h_2bh(); break;
17130: case 0x2c: msdos_int_21h_2ch(); break;
17131: case 0x2d: msdos_int_21h_2dh(); break;
17132: case 0x2e: msdos_int_21h_2eh(); break;
17133: case 0x2f: msdos_int_21h_2fh(); break;
17134: case 0x30: msdos_int_21h_30h(); break;
17135: case 0x31: msdos_int_21h_31h(); break;
17136: case 0x32: msdos_int_21h_32h(); break;
17137: case 0x33: msdos_int_21h_33h(); break;
17138: case 0x34: msdos_int_21h_34h(); break;
17139: case 0x35: msdos_int_21h_35h(); break;
17140: case 0x36: msdos_int_21h_36h(); break;
17141: case 0x37: msdos_int_21h_37h(); break;
17142: case 0x38: msdos_int_21h_38h(); break;
17143: case 0x39: msdos_int_21h_39h(0); break;
17144: case 0x3a: msdos_int_21h_3ah(0); break;
17145: case 0x3b: msdos_int_21h_3bh(0); break;
17146: case 0x3c: msdos_int_21h_3ch(); break;
17147: case 0x3d: msdos_int_21h_3dh(); break;
17148: case 0x3e: msdos_int_21h_3eh(); break;
17149: case 0x3f: msdos_int_21h_3fh(); break;
17150: case 0x40: msdos_int_21h_40h(); break;
17151: case 0x41: msdos_int_21h_41h(0); break;
17152: case 0x42: msdos_int_21h_42h(); break;
17153: case 0x43: msdos_int_21h_43h(0); break;
17154: case 0x44: msdos_int_21h_44h(); break;
17155: case 0x45: msdos_int_21h_45h(); break;
17156: case 0x46: msdos_int_21h_46h(); break;
17157: case 0x47: msdos_int_21h_47h(0); break;
17158: case 0x48: msdos_int_21h_48h(); break;
17159: case 0x49: msdos_int_21h_49h(); break;
17160: case 0x4a: msdos_int_21h_4ah(); break;
17161: case 0x4b: msdos_int_21h_4bh(); break;
17162: case 0x4c: msdos_int_21h_4ch(); break;
17163: case 0x4d: msdos_int_21h_4dh(); break;
17164: case 0x4e: msdos_int_21h_4eh(); break;
17165: case 0x4f: msdos_int_21h_4fh(); break;
17166: case 0x50: msdos_int_21h_50h(); break;
17167: case 0x51: msdos_int_21h_51h(); break;
17168: case 0x52: msdos_int_21h_52h(); break;
1.1.1.43 root 17169: case 0x53: msdos_int_21h_53h(); break;
1.1.1.28 root 17170: case 0x54: msdos_int_21h_54h(); break;
17171: case 0x55: msdos_int_21h_55h(); break;
17172: case 0x56: msdos_int_21h_56h(0); break;
17173: case 0x57: msdos_int_21h_57h(); break;
17174: case 0x58: msdos_int_21h_58h(); break;
17175: case 0x59: msdos_int_21h_59h(); break;
17176: case 0x5a: msdos_int_21h_5ah(); break;
17177: case 0x5b: msdos_int_21h_5bh(); break;
17178: case 0x5c: msdos_int_21h_5ch(); break;
17179: case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42 root 17180: case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30 root 17181: case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28 root 17182: case 0x60: msdos_int_21h_60h(0); break;
17183: case 0x61: msdos_int_21h_61h(); break;
17184: case 0x62: msdos_int_21h_62h(); break;
17185: case 0x63: msdos_int_21h_63h(); break;
1.1.1.33 root 17186: // 0x64: Set Device Driver Lockahead Flag
1.1.1.28 root 17187: case 0x65: msdos_int_21h_65h(); break;
17188: case 0x66: msdos_int_21h_66h(); break;
17189: case 0x67: msdos_int_21h_67h(); break;
17190: case 0x68: msdos_int_21h_68h(); break;
17191: case 0x69: msdos_int_21h_69h(); break;
17192: case 0x6a: msdos_int_21h_6ah(); break;
17193: case 0x6b: msdos_int_21h_6bh(); break;
17194: case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48 root 17195: case 0x6d: // Find First ROM Program
17196: case 0x6e: // Find Next ROM Program
17197: case 0x6f: // Get/Set ROM Scan Start Address
17198: REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
17199: break;
1.1.1.43 root 17200: case 0x70: msdos_int_21h_70h(); break;
1.1.1.48 root 17201: case 0x71: // Windows95 - Long Filename Functions
1.1.1.28 root 17202: switch(REG8(AL)) {
17203: case 0x0d: msdos_int_21h_710dh(); break;
17204: case 0x39: msdos_int_21h_39h(1); break;
17205: case 0x3a: msdos_int_21h_3ah(1); break;
17206: case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48 root 17207: case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28 root 17208: case 0x43: msdos_int_21h_43h(1); break;
17209: case 0x47: msdos_int_21h_47h(1); break;
17210: case 0x4e: msdos_int_21h_714eh(); break;
17211: case 0x4f: msdos_int_21h_714fh(); break;
17212: case 0x56: msdos_int_21h_56h(1); break;
17213: case 0x60: msdos_int_21h_60h(1); break;
17214: case 0x6c: msdos_int_21h_6ch(1); break;
17215: case 0xa0: msdos_int_21h_71a0h(); break;
17216: case 0xa1: msdos_int_21h_71a1h(); break;
17217: case 0xa6: msdos_int_21h_71a6h(); break;
17218: case 0xa7: msdos_int_21h_71a7h(); break;
17219: case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45 root 17220: case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28 root 17221: case 0xaa: msdos_int_21h_71aah(); break;
17222: default:
17223: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17224: REG16(AX) = 0x7100;
17225: m_CF = 1;
17226: break;
17227: }
17228: break;
1.1.1.48 root 17229: case 0x72: // Windows95 beta - LFN FindClose
17230: // unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17231: REG16(AX) = 0x7200;
17232: m_CF = 1;
17233: break;
17234: case 0x73: // Windows95 - FAT32 Functions
1.1.1.28 root 17235: switch(REG8(AL)) {
17236: case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33 root 17237: // 0x01: Set Drive Locking ???
1.1.1.28 root 17238: case 0x02: msdos_int_21h_7302h(); break;
17239: case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33 root 17240: // 0x04: Set DPB to Use for Formatting
17241: // 0x05: Extended Absolute Disk Read/Write
1.1.1.28 root 17242: default:
17243: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17244: REG16(AX) = 0x7300;
17245: m_CF = 1;
17246: break;
17247: }
1.1 root 17248: break;
1.1.1.30 root 17249: case 0xdb: msdos_int_21h_dbh(); break;
17250: case 0xdc: msdos_int_21h_dch(); break;
1.1 root 17251: default:
1.1.1.22 root 17252: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.28 root 17253: REG16(AX) = 0x01;
1.1.1.3 root 17254: m_CF = 1;
1.1 root 17255: break;
17256: }
1.1.1.28 root 17257: } catch(int error) {
17258: REG16(AX) = error;
17259: m_CF = 1;
17260: } catch(...) {
17261: REG16(AX) = 0x1f; // general failure
1.1.1.3 root 17262: m_CF = 1;
1.1 root 17263: }
1.1.1.3 root 17264: if(m_CF) {
1.1.1.23 root 17265: sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47 root 17266: sda->int21h_5d0ah_called = 0;
1.1.1.23 root 17267: sda->extended_error_code = REG16(AX);
17268: switch(sda->extended_error_code) {
17269: case 4: // Too many open files
17270: case 8: // Insufficient memory
17271: sda->error_class = 1; // Out of resource
17272: break;
17273: case 5: // Access denied
17274: sda->error_class = 3; // Authorization
17275: break;
17276: case 7: // Memory control block destroyed
17277: sda->error_class = 4; // Internal
17278: break;
17279: case 2: // File not found
17280: case 3: // Path not found
17281: case 15: // Invaid drive specified
17282: case 18: // No more files
17283: sda->error_class = 8; // Not found
17284: break;
17285: case 32: // Sharing violation
17286: case 33: // Lock violation
17287: sda->error_class = 10; // Locked
17288: break;
17289: // case 16: // Removal of current directory attempted
17290: case 19: // Attempted write on protected disk
17291: case 21: // Drive not ready
17292: // case 29: // Write failure
17293: // case 30: // Read failure
17294: // case 82: // Cannot create subdirectory
17295: sda->error_class = 11; // Media
17296: break;
17297: case 80: // File already exists
17298: sda->error_class = 12; // Already exist
17299: break;
17300: default:
17301: sda->error_class = 13; // Unknown
17302: break;
17303: }
17304: sda->suggested_action = 1; // Retry
17305: sda->locus_of_last_error = 1; // Unknown
1.1 root 17306: }
1.1.1.33 root 17307: if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26 root 17308: // raise int 23h
17309: #if defined(HAS_I386)
17310: m_ext = 0; // not an external interrupt
17311: i386_trap(0x23, 1, 0);
17312: m_ext = 1;
17313: #else
17314: PREFIX86(_interrupt)(0x23);
17315: #endif
17316: }
1.1 root 17317: break;
17318: case 0x22:
17319: fatalerror("int 22h (terminate address)\n");
17320: case 0x23:
1.1.1.28 root 17321: try {
17322: msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
17323: } catch(...) {
17324: fatalerror("failed to terminate the current process by int 23h\n");
17325: }
1.1 root 17326: break;
17327: case 0x24:
1.1.1.32 root 17328: /*
1.1.1.28 root 17329: try {
17330: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
17331: } catch(...) {
17332: fatalerror("failed to terminate the current process by int 24h\n");
17333: }
1.1.1.32 root 17334: */
17335: msdos_int_24h();
1.1 root 17336: break;
17337: case 0x25:
17338: msdos_int_25h();
17339: break;
17340: case 0x26:
17341: msdos_int_26h();
17342: break;
17343: case 0x27:
1.1.1.28 root 17344: try {
17345: msdos_int_27h();
17346: } catch(...) {
17347: fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
17348: }
1.1 root 17349: break;
17350: case 0x28:
17351: Sleep(10);
1.1.1.35 root 17352: REQUEST_HARDWRE_UPDATE();
1.1 root 17353: break;
17354: case 0x29:
17355: msdos_int_29h();
17356: break;
17357: case 0x2e:
17358: msdos_int_2eh();
17359: break;
17360: case 0x2f:
17361: // multiplex interrupt
17362: switch(REG8(AH)) {
1.1.1.22 root 17363: case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44 root 17364: case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22 root 17365: case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21 root 17366: case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30 root 17367: case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22 root 17368: case 0x14: msdos_int_2fh_14h(); break;
17369: case 0x15: msdos_int_2fh_15h(); break;
1.1 root 17370: case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22 root 17371: case 0x19: msdos_int_2fh_19h(); break;
1.1 root 17372: case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30 root 17373: case 0x40: msdos_int_2fh_40h(); break;
1.1 root 17374: case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22 root 17375: case 0x46: msdos_int_2fh_46h(); break;
17376: case 0x48: msdos_int_2fh_48h(); break;
1.1 root 17377: case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22 root 17378: case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44 root 17379: case 0x4d: msdos_int_2fh_4dh(); break;
1.1 root 17380: case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22 root 17381: case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44 root 17382: case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24 root 17383: case 0xad: msdos_int_2fh_adh(); break;
1.1 root 17384: case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34 root 17385: case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43 root 17386: default:
1.1.1.30 root 17387: switch(REG8(AL)) {
17388: case 0x00:
17389: // This is not installed
17390: // REG8(AL) = 0x00;
17391: break;
1.1.1.33 root 17392: case 0x01:
1.1.1.42 root 17393: // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
17394: if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
17395: break;
17396: }
1.1.1.33 root 17397: // Banyan VINES v4+ is not installed
17398: if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
17399: break;
17400: }
1.1.1.42 root 17401: // Quarterdeck QDPMI.SYS v1.0 is not installed
17402: if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
17403: break;
17404: }
1.1.1.30 root 17405: default:
1.1.1.42 root 17406: // NORTON UTILITIES 5.0+
17407: if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
17408: break;
17409: }
1.1.1.30 root 17410: unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.43 root 17411: REG16(AX) = 0x01; // invalid function
1.1.1.30 root 17412: m_CF = 1;
17413: break;
17414: }
17415: break;
1.1 root 17416: }
17417: break;
1.1.1.24 root 17418: case 0x33:
17419: switch(REG8(AH)) {
17420: case 0x00:
17421: // Mouse
1.1.1.49 root 17422: switch(REG16(AX)) {
17423: case 0x0000: msdos_int_33h_0000h(); break;
17424: case 0x0001: msdos_int_33h_0001h(); break;
17425: case 0x0002: msdos_int_33h_0002h(); break;
17426: case 0x0003: msdos_int_33h_0003h(); break;
17427: case 0x0004: msdos_int_33h_0004h(); break;
17428: case 0x0005: msdos_int_33h_0005h(); break;
17429: case 0x0006: msdos_int_33h_0006h(); break;
17430: case 0x0007: msdos_int_33h_0007h(); break;
17431: case 0x0008: msdos_int_33h_0008h(); break;
17432: case 0x0009: msdos_int_33h_0009h(); break;
17433: case 0x000a: msdos_int_33h_000ah(); break;
17434: case 0x000b: msdos_int_33h_000bh(); break;
17435: case 0x000c: msdos_int_33h_000ch(); break;
17436: case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
17437: case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
17438: case 0x000f: msdos_int_33h_000fh(); break;
17439: case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
17440: case 0x0011: msdos_int_33h_0011h(); break;
17441: case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
17442: case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
17443: case 0x0014: msdos_int_33h_0014h(); break;
17444: case 0x0015: msdos_int_33h_0015h(); break;
17445: case 0x0016: msdos_int_33h_0016h(); break;
17446: case 0x0017: msdos_int_33h_0017h(); break;
17447: case 0x0018: msdos_int_33h_0018h(); break;
17448: case 0x0019: msdos_int_33h_0019h(); break;
17449: case 0x001a: msdos_int_33h_001ah(); break;
17450: case 0x001b: msdos_int_33h_001bh(); break;
17451: case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
17452: case 0x001d: msdos_int_33h_001dh(); break;
17453: case 0x001e: msdos_int_33h_001eh(); break;
17454: case 0x001f: msdos_int_33h_001fh(); break;
17455: case 0x0020: msdos_int_33h_0020h(); break;
17456: case 0x0021: msdos_int_33h_0021h(); break;
17457: case 0x0022: msdos_int_33h_0022h(); break;
17458: case 0x0023: msdos_int_33h_0023h(); break;
17459: case 0x0024: msdos_int_33h_0024h(); break;
17460: case 0x0025: msdos_int_33h_0025h(); break;
17461: case 0x0026: msdos_int_33h_0026h(); break;
17462: case 0x0027: msdos_int_33h_0027h(); break;
17463: case 0x0028: msdos_int_33h_0028h(); break;
17464: case 0x0029: msdos_int_33h_0029h(); break;
17465: case 0x002a: msdos_int_33h_002ah(); break;
17466: // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
17467: // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
17468: // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
17469: // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
17470: case 0x002f: break; // Mouse Hardware Reset
17471: // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
17472: case 0x0031: msdos_int_33h_0031h(); break;
17473: case 0x0032: msdos_int_33h_0032h(); break;
17474: // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
17475: // 0x0034: MS MOUSE v8.0+ - Get Initialization File
17476: // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
17477: case 0x004d: msdos_int_33h_004dh(); break;
17478: case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24 root 17479: default:
17480: unimplemented_33h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17481: break;
17482: }
17483: break;
17484: default:
17485: unimplemented_33h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17486: break;
17487: }
17488: break;
1.1.1.59 root 17489: case 0x65:
1.1.1.19 root 17490: // dummy interrupt for EMS (int 67h)
17491: switch(REG8(AH)) {
17492: case 0x40: msdos_int_67h_40h(); break;
17493: case 0x41: msdos_int_67h_41h(); break;
17494: case 0x42: msdos_int_67h_42h(); break;
17495: case 0x43: msdos_int_67h_43h(); break;
17496: case 0x44: msdos_int_67h_44h(); break;
17497: case 0x45: msdos_int_67h_45h(); break;
17498: case 0x46: msdos_int_67h_46h(); break;
17499: case 0x47: msdos_int_67h_47h(); break;
17500: case 0x48: msdos_int_67h_48h(); break;
1.1.1.20 root 17501: // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
17502: // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19 root 17503: case 0x4b: msdos_int_67h_4bh(); break;
17504: case 0x4c: msdos_int_67h_4ch(); break;
17505: case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20 root 17506: case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21 root 17507: case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20 root 17508: case 0x50: msdos_int_67h_50h(); break;
1.1.1.19 root 17509: case 0x51: msdos_int_67h_51h(); break;
1.1.1.20 root 17510: case 0x52: msdos_int_67h_52h(); break;
1.1.1.19 root 17511: case 0x53: msdos_int_67h_53h(); break;
17512: case 0x54: msdos_int_67h_54h(); break;
1.1.1.49 root 17513: case 0x55: msdos_int_67h_55h(); break;
17514: case 0x56: msdos_int_67h_56h(); break;
1.1.1.20 root 17515: case 0x57: msdos_int_67h_57h(); break;
17516: case 0x58: msdos_int_67h_58h(); break;
1.1.1.42 root 17517: case 0x59: msdos_int_67h_59h(); break;
1.1.1.20 root 17518: case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49 root 17519: case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43 root 17520: // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
17521: case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49 root 17522: case 0x70: msdos_int_67h_70h(); break;
1.1.1.31 root 17523: // 0xde: VCPI
1.1.1.30 root 17524: case 0xde: msdos_int_67h_deh(); break;
1.1.1.19 root 17525: default:
1.1.1.22 root 17526: unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.19 root 17527: REG8(AH) = 0x84;
17528: break;
17529: }
17530: break;
17531: #ifdef SUPPORT_XMS
1.1.1.59 root 17532: case 0x66:
1.1.1.19 root 17533: // dummy interrupt for XMS (call far)
1.1.1.28 root 17534: try {
17535: switch(REG8(AH)) {
17536: case 0x00: msdos_call_xms_00h(); break;
17537: case 0x01: msdos_call_xms_01h(); break;
17538: case 0x02: msdos_call_xms_02h(); break;
17539: case 0x03: msdos_call_xms_03h(); break;
17540: case 0x04: msdos_call_xms_04h(); break;
17541: case 0x05: msdos_call_xms_05h(); break;
17542: case 0x06: msdos_call_xms_06h(); break;
17543: case 0x07: msdos_call_xms_07h(); break;
17544: case 0x08: msdos_call_xms_08h(); break;
17545: case 0x09: msdos_call_xms_09h(); break;
17546: case 0x0a: msdos_call_xms_0ah(); break;
17547: case 0x0b: msdos_call_xms_0bh(); break;
17548: case 0x0c: msdos_call_xms_0ch(); break;
17549: case 0x0d: msdos_call_xms_0dh(); break;
17550: case 0x0e: msdos_call_xms_0eh(); break;
17551: case 0x0f: msdos_call_xms_0fh(); break;
17552: case 0x10: msdos_call_xms_10h(); break;
17553: case 0x11: msdos_call_xms_11h(); break;
17554: case 0x12: msdos_call_xms_12h(); break;
1.1.1.29 root 17555: #if defined(HAS_I386)
17556: case 0x88: msdos_call_xms_88h(); break;
17557: case 0x89: msdos_call_xms_89h(); break;
17558: case 0x8e: msdos_call_xms_8eh(); break;
17559: case 0x8f: msdos_call_xms_8fh(); break;
17560: #endif
1.1.1.28 root 17561: default:
17562: unimplemented_xms("call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
17563: REG16(AX) = 0x0000;
17564: REG8(BL) = 0x80; // function not implemented
17565: break;
17566: }
17567: } catch(...) {
1.1.1.19 root 17568: REG16(AX) = 0x0000;
1.1.1.28 root 17569: REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19 root 17570: }
17571: break;
17572: #endif
1.1.1.59 root 17573: /*
17574: case 0x67:
17575: // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
17576: // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
17577: break;
17578: */
17579: case 0x69:
1.1.1.24 root 17580: // irq12 (mouse)
17581: mouse_push_ax = REG16(AX);
17582: mouse_push_bx = REG16(BX);
17583: mouse_push_cx = REG16(CX);
17584: mouse_push_dx = REG16(DX);
17585: mouse_push_si = REG16(SI);
17586: mouse_push_di = REG16(DI);
17587:
1.1.1.43 root 17588: if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24 root 17589: REG16(AX) = mouse.status_irq;
17590: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 17591: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
17592: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24 root 17593: REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
17594: REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
17595:
1.1.1.49 root 17596: mem[DUMMY_TOP + 0x02] = 0x9a; // call far
17597: mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
17598: mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
17599: mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
17600: mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59 root 17601: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6bh (dummy)
17602: mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43 root 17603: break;
1.1.1.24 root 17604: }
1.1.1.43 root 17605: for(int i = 0; i < 8; i++) {
17606: if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
17607: REG16(AX) = mouse.status_irq_alt;
17608: REG16(BX) = mouse.get_buttons();
17609: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
17610: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
17611: REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
17612: REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
17613:
1.1.1.49 root 17614: mem[DUMMY_TOP + 0x02] = 0x9a; // call far
17615: mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
17616: mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
17617: mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
17618: mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59 root 17619: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6bh (dummy)
17620: mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43 root 17621: break;
17622: }
17623: }
1.1.1.59 root 17624: if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54 root 17625: UINT16 data_1st, data_2nd, data_3rd;
17626: pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
17627: i386_push16(data_1st);
17628: i386_push16(data_2nd);
17629: i386_push16(data_3rd);
17630: i386_push16(0x0000);
17631:
17632: mem[DUMMY_TOP + 0x02] = 0x9a; // call far
17633: mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
17634: mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
17635: mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
17636: mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59 root 17637: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6ah (dummy)
17638: mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54 root 17639: break;
17640: }
1.1.1.43 root 17641: // invalid call addr :-(
1.1.1.49 root 17642: mem[DUMMY_TOP + 0x02] = 0x90; // nop
17643: mem[DUMMY_TOP + 0x03] = 0x90; // nop
17644: mem[DUMMY_TOP + 0x04] = 0x90; // nop
17645: mem[DUMMY_TOP + 0x05] = 0x90; // nop
17646: mem[DUMMY_TOP + 0x06] = 0x90; // nop
1.1.1.59 root 17647: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6bh (dummy)
17648: mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24 root 17649: break;
1.1.1.59 root 17650: case 0x6a:
17651: // end of ps/2 mouse bios
17652: i386_pop16();
17653: i386_pop16();
17654: i386_pop16();
17655: i386_pop16();
1.1.1.24 root 17656: case 0x6b:
17657: // end of irq12 (mouse)
17658: REG16(AX) = mouse_push_ax;
17659: REG16(BX) = mouse_push_bx;
17660: REG16(CX) = mouse_push_cx;
17661: REG16(DX) = mouse_push_dx;
17662: REG16(SI) = mouse_push_si;
17663: REG16(DI) = mouse_push_di;
17664:
17665: // EOI
17666: if((pic[1].isr &= ~(1 << 4)) == 0) {
17667: pic[0].isr &= ~(1 << 2); // master
17668: }
17669: pic_update();
17670: break;
17671: case 0x6c:
1.1.1.19 root 17672: // dummy interrupt for case map routine pointed in the country info
17673: if(REG8(AL) >= 0x80) {
17674: char tmp[2] = {0};
17675: tmp[0] = REG8(AL);
17676: my_strupr(tmp);
17677: REG8(AL) = tmp[0];
17678: }
17679: break;
1.1.1.27 root 17680: case 0x6d:
17681: // dummy interrupt for font read routine pointed by int 15h, ax=5000h
17682: REG8(AL) = 0x86; // not supported
17683: m_CF = 1;
17684: break;
1.1.1.32 root 17685: case 0x6e:
17686: // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
17687: {
17688: UINT16 code = REG16(AX);
17689: if(code & 0xf0) {
17690: code = (code & 7) | ((code & 0x10) >> 1);
17691: }
17692: for(int i = 0; i < array_length(param_error_table); i++) {
17693: if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
17694: const char *message = NULL;
17695: if(active_code_page == 932) {
17696: message = param_error_table[i].message_japanese;
17697: }
17698: if(message == NULL) {
17699: message = param_error_table[i].message_english;
17700: }
17701: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
17702: strcpy((char *)(mem + WORK_TOP + 1), message);
17703:
17704: SREG(ES) = WORK_TOP >> 4;
17705: i386_load_segment_descriptor(ES);
17706: REG16(DI) = 0x0000;
17707: break;
17708: }
17709: }
17710: }
17711: break;
1.1.1.49 root 17712: case 0x6f:
17713: // dummy interrupt for end of alter page map and call
17714: {
17715: UINT16 handles[4], pages[4];
17716:
17717: // pop old mapping data in new mapping
17718: for(int i = 0; i < 4; i++) {
17719: pages [3 - i] = i386_pop16();
17720: handles[3 - i] = i386_pop16();
17721: }
17722:
17723: // restore old mapping
17724: for(int i = 0; i < 4; i++) {
17725: UINT16 handle = handles[i];
17726: UINT16 page = pages [i];
17727:
17728: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
17729: ems_map_page(i, handle, page);
17730: } else {
17731: ems_unmap_page(i);
17732: }
17733: }
17734: // do ret_far (pop cs/ip) in old mapping
17735: }
17736: break;
1.1.1.8 root 17737: case 0x70:
17738: case 0x71:
17739: case 0x72:
17740: case 0x73:
17741: case 0x74:
17742: case 0x75:
17743: case 0x76:
17744: case 0x77:
17745: // EOI
17746: if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
17747: pic[0].isr &= ~(1 << 2); // master
17748: }
17749: pic_update();
17750: break;
1.1 root 17751: default:
1.1.1.22 root 17752: // fatalerror("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1 root 17753: break;
17754: }
17755:
17756: // update cursor position
17757: if(cursor_moved) {
1.1.1.36 root 17758: pcbios_update_cursor_position();
1.1 root 17759: cursor_moved = false;
17760: }
17761: }
17762:
17763: // init
17764:
17765: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
17766: {
17767: // init file handler
17768: memset(file_handler, 0, sizeof(file_handler));
17769: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
17770: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
17771: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21 root 17772: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 17773: if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21 root 17774: #else
17775: if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
17776: #endif
17777: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1 root 17778: }
1.1.1.21 root 17779: if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45 root 17780: // msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
17781: msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21 root 17782: }
1.1 root 17783: _dup2(0, DUP_STDIN);
17784: _dup2(1, DUP_STDOUT);
17785: _dup2(2, DUP_STDERR);
1.1.1.21 root 17786: _dup2(3, DUP_STDAUX);
17787: _dup2(4, DUP_STDPRN);
1.1 root 17788:
1.1.1.24 root 17789: // init mouse
17790: memset(&mouse, 0, sizeof(mouse));
1.1.1.34 root 17791: mouse.enabled = true; // from DOSBox
17792: mouse.hidden = 1; // hidden in default ???
17793: mouse.old_hidden = 1; // from DOSBox
17794: mouse.max_position.x = 8 * (scr_width - 1);
17795: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 17796: mouse.mickey.x = 8;
17797: mouse.mickey.y = 16;
17798:
1.1.1.26 root 17799: #ifdef SUPPORT_XMS
17800: // init xms
17801: msdos_xms_init();
17802: #endif
17803:
1.1 root 17804: // init process
17805: memset(process, 0, sizeof(process));
17806:
1.1.1.13 root 17807: // init dtainfo
17808: msdos_dta_info_init();
17809:
1.1 root 17810: // init memory
17811: memset(mem, 0, sizeof(mem));
17812:
17813: // bios data area
1.1.1.23 root 17814: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 17815: CONSOLE_SCREEN_BUFFER_INFO csbi;
17816: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58 root 17817: // CONSOLE_FONT_INFO cfi;
1.1.1.57 root 17818: // GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.58 root 17819: int font_width = 10, font_height = 18; // default in english mode
17820: get_console_font_size(hStdout, &font_width, &font_height);
1.1.1.14 root 17821:
17822: int regen = min(scr_width * scr_height * 2, 0x8000);
17823: text_vram_top_address = TEXT_VRAM_TOP;
17824: text_vram_end_address = text_vram_top_address + regen;
17825: shadow_buffer_top_address = SHADOW_BUF_TOP;
17826: shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51 root 17827: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14 root 17828:
17829: if(regen > 0x4000) {
17830: regen = 0x8000;
17831: vram_pages = 1;
17832: } else if(regen > 0x2000) {
17833: regen = 0x4000;
17834: vram_pages = 2;
17835: } else if(regen > 0x1000) {
17836: regen = 0x2000;
17837: vram_pages = 4;
17838: } else {
17839: regen = 0x1000;
17840: vram_pages = 8;
17841: }
1.1 root 17842:
1.1.1.25 root 17843: *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
17844: *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29 root 17845: *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
17846: *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25 root 17847: *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37 root 17848: *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
17849: *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32 root 17850: #ifdef EXT_BIOS_TOP
1.1.1.25 root 17851: *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32 root 17852: #endif
1.1.1.26 root 17853: *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33 root 17854: *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41 root 17855: *(UINT16 *)(mem + 0x41a) = 0x1e;
17856: *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1 root 17857: *(UINT8 *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14 root 17858: *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
17859: *(UINT16 *)(mem + 0x44c) = regen;
1.1 root 17860: *(UINT16 *)(mem + 0x44e) = 0;
17861: *(UINT8 *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14 root 17862: *(UINT8 *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1 root 17863: *(UINT8 *)(mem + 0x460) = 7;
17864: *(UINT8 *)(mem + 0x461) = 7;
17865: *(UINT8 *)(mem + 0x462) = 0;
17866: *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19 root 17867: *(UINT8 *)(mem + 0x465) = 0x09;
17868: *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40 root 17869: *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41 root 17870: *(UINT16 *)(mem + 0x480) = 0x1e;
17871: *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14 root 17872: *(UINT8 *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58 root 17873: *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14 root 17874: *(UINT8 *)(mem + 0x487) = 0x60;
17875: *(UINT8 *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32 root 17876: #ifdef EXT_BIOS_TOP
1.1.1.25 root 17877: *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32 root 17878: #endif
1.1.1.14 root 17879:
17880: // initial screen
17881: SMALL_RECT rect;
17882: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60! root 17883: ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14 root 17884: for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
17885: for(int x = 0; x < scr_width; x++) {
17886: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
17887: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
17888: }
17889: }
1.1 root 17890:
1.1.1.19 root 17891: // init mcb
1.1 root 17892: int seg = MEMORY_TOP >> 4;
1.1.1.19 root 17893:
17894: // iret table
17895: // note: int 2eh vector should address the routine in command.com,
17896: // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
17897: // so move iret table into allocated memory block
17898: // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58 root 17899: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19 root 17900: IRET_TOP = seg << 4;
1.1.1.58 root 17901: seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25 root 17902: memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19 root 17903:
1.1.1.58 root 17904: // note: SO1 checks int 21h vector and if it aims iret (cfh)
17905: // it is recognized SO1 is not running on MS-DOS environment
17906: for(int i = 0; i < 128; i++) {
17907: // jmp far (IRET_TOP >> 4):(interrupt number)
17908: *(UINT8 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
17909: *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
17910: *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
17911: }
17912:
1.1.1.19 root 17913: // dummy xms/ems device
1.1.1.33 root 17914: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19 root 17915: XMS_TOP = seg << 4;
17916: seg += XMS_SIZE >> 4;
17917:
17918: // environment
1.1.1.33 root 17919: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1 root 17920: int env_seg = seg;
17921: int ofs = 0;
1.1.1.32 root 17922: char env_append[ENV_SIZE] = {0}, append_added = 0;
17923: char comspec_added = 0;
1.1.1.33 root 17924: char lastdrive_added = 0;
1.1.1.32 root 17925: char env_msdos_path[ENV_SIZE] = {0};
17926: char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33 root 17927: char prompt_added = 0;
1.1.1.32 root 17928: char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33 root 17929: char tz_added = 0;
1.1.1.45 root 17930: const char *path, *short_path;
1.1.1.32 root 17931:
17932: if((path = getenv("MSDOS_APPEND")) != NULL) {
17933: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17934: strcpy(env_append, short_path);
17935: }
17936: }
17937: if((path = getenv("APPEND")) != NULL) {
17938: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17939: if(env_append[0] != '\0') {
17940: strcat(env_append, ";");
17941: }
17942: strcat(env_append, short_path);
17943: }
17944: }
17945:
17946: if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
17947: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17948: strcpy(comspec_path, short_path);
17949: }
17950: }
17951: if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
17952: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17953: strcpy(comspec_path, short_path);
17954: }
17955: }
1.1 root 17956:
1.1.1.28 root 17957: if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32 root 17958: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17959: strcpy(env_msdos_path, short_path);
17960: strcpy(env_path, short_path);
1.1.1.14 root 17961: }
17962: }
1.1.1.28 root 17963: if((path = getenv("PATH")) != NULL) {
1.1.1.32 root 17964: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17965: if(env_path[0] != '\0') {
17966: strcat(env_path, ";");
17967: }
17968: strcat(env_path, short_path);
1.1.1.9 root 17969: }
17970: }
1.1.1.32 root 17971:
1.1.1.60! root 17972: if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32 root 17973: strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15 root 17974: }
1.1.1.32 root 17975: for(int i = 0; i < 4; i++) {
17976: static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
17977: if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
17978: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17979: strcpy(env_temp, short_path);
17980: break;
17981: }
17982: }
1.1.1.24 root 17983: }
1.1.1.32 root 17984:
1.1.1.9 root 17985: for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1 root 17986: // lower to upper
1.1.1.28 root 17987: char tmp[ENV_SIZE], name[ENV_SIZE];
1.1 root 17988: strcpy(tmp, *p);
17989: for(int i = 0;; i++) {
17990: if(tmp[i] == '=') {
17991: tmp[i] = '\0';
17992: sprintf(name, ";%s;", tmp);
1.1.1.25 root 17993: my_strupr(name);
1.1 root 17994: tmp[i] = '=';
17995: break;
17996: } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28 root 17997: tmp[i] = (tmp[i] - 'a') + 'A';
1.1 root 17998: }
17999: }
1.1.1.33 root 18000: if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
18001: // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
18002: } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18 root 18003: // ignore non standard environments
18004: } else {
1.1.1.33 root 18005: if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32 root 18006: if(env_append[0] != '\0') {
18007: sprintf(tmp, "APPEND=%s", env_append);
18008: } else {
18009: sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
18010: }
18011: append_added = 1;
18012: } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14 root 18013: strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32 root 18014: comspec_added = 1;
1.1.1.33 root 18015: } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
18016: char *env = getenv("MSDOS_LASTDRIVE");
18017: if(env != NULL) {
18018: sprintf(tmp, "LASTDRIVE=%s", env);
18019: }
18020: lastdrive_added = 1;
18021: } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28 root 18022: if(env_msdos_path[0] != '\0') {
18023: sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
18024: } else {
18025: sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
18026: }
1.1.1.33 root 18027: } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28 root 18028: if(env_path[0] != '\0') {
18029: sprintf(tmp, "PATH=%s", env_path);
18030: } else {
18031: sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
18032: }
1.1.1.32 root 18033: path_added = 1;
1.1.1.33 root 18034: } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
18035: prompt_added = 1;
1.1.1.28 root 18036: } else if(strncmp(tmp, "TEMP=", 5) == 0) {
18037: if(env_temp[0] != '\0') {
18038: sprintf(tmp, "TEMP=%s", env_temp);
18039: } else {
18040: sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
18041: }
1.1.1.32 root 18042: temp_added = 1;
1.1.1.33 root 18043: } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28 root 18044: if(env_temp[0] != '\0') {
18045: sprintf(tmp, "TMP=%s", env_temp);
18046: } else {
18047: sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1 root 18048: }
1.1.1.32 root 18049: tmp_added = 1;
1.1.1.33 root 18050: } else if(strncmp(tmp, "TZ=", 3) == 0) {
18051: char *env = getenv("MSDOS_TZ");
18052: if(env != NULL) {
18053: sprintf(tmp, "TZ=%s", env);
18054: }
18055: tz_added = 1;
1.1 root 18056: }
18057: int len = strlen(tmp);
1.1.1.14 root 18058: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1 root 18059: fatalerror("too many environments\n");
18060: }
18061: memcpy(mem + (seg << 4) + ofs, tmp, len);
18062: ofs += len + 1;
18063: }
18064: }
1.1.1.32 root 18065: if(!append_added && env_append[0] != '\0') {
18066: #define SET_ENV(name, value) { \
18067: char tmp[ENV_SIZE]; \
18068: sprintf(tmp, "%s=%s", name, value); \
18069: int len = strlen(tmp); \
18070: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
18071: fatalerror("too many environments\n"); \
18072: } \
18073: memcpy(mem + (seg << 4) + ofs, tmp, len); \
18074: ofs += len + 1; \
18075: }
18076: SET_ENV("APPEND", env_append);
18077: }
18078: if(!comspec_added) {
18079: SET_ENV("COMSPEC", "C:\\COMMAND.COM");
18080: }
1.1.1.33 root 18081: if(!lastdrive_added) {
18082: SET_ENV("LASTDRIVE", "Z");
18083: }
1.1.1.32 root 18084: if(!path_added) {
18085: SET_ENV("PATH", env_path);
18086: }
1.1.1.33 root 18087: if(!prompt_added) {
18088: SET_ENV("PROMPT", "$P$G");
18089: }
1.1.1.32 root 18090: if(!temp_added) {
18091: SET_ENV("TEMP", env_temp);
18092: }
18093: if(!tmp_added) {
18094: SET_ENV("TMP", env_temp);
18095: }
1.1.1.33 root 18096: if(!tz_added) {
18097: TIME_ZONE_INFORMATION tzi;
18098: HKEY hKey, hSubKey;
18099: char tzi_std_name[64];
18100: char tz_std[8] = "GMT";
18101: char tz_dlt[8] = "GST";
18102: char tz_value[32];
18103:
18104: // timezone name from GetTimeZoneInformation may not be english
18105: bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
18106: setlocale(LC_CTYPE, "");
18107: wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
18108:
18109: // get english timezone name from registry
1.1.1.60! root 18110: if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33 root 18111: for(DWORD i = 0; !tz_added; i++) {
18112: char reg_name[256], sub_key[1024], std_name[256];
18113: DWORD size;
18114: FILETIME ftTime;
1.1.1.60! root 18115: LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33 root 18116:
18117: if(result == ERROR_SUCCESS) {
18118: sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60! root 18119: if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
! 18120: if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33 root 18121: // search english timezone name from table
1.1.1.37 root 18122: if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33 root 18123: for(int j = 0; j < array_length(tz_table); j++) {
18124: if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
18125: if(tz_table[j].std != NULL) {
18126: strcpy(tz_std, tz_table[j].std);
18127: }
18128: if(tz_table[j].dlt != NULL) {
18129: strcpy(tz_dlt, tz_table[j].dlt);
18130: }
18131: tz_added = 1;
18132: break;
18133: }
18134: }
18135: }
18136: }
18137: RegCloseKey(hSubKey);
18138: }
18139: } else if(result == ERROR_NO_MORE_ITEMS) {
18140: break;
18141: }
18142: }
18143: RegCloseKey(hKey);
18144: }
18145: if((tzi.Bias % 60) != 0) {
18146: sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
18147: } else {
18148: sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
18149: }
18150: if(daylight) {
18151: strcat(tz_value, tz_dlt);
18152: }
18153: SET_ENV("TZ", tz_value);
18154: }
1.1 root 18155: seg += (ENV_SIZE >> 4);
18156:
18157: // psp
1.1.1.33 root 18158: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1 root 18159: current_psp = seg;
1.1.1.35 root 18160: psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
18161: psp->parent_psp = current_psp;
1.1 root 18162: seg += (PSP_SIZE >> 4);
18163:
1.1.1.19 root 18164: // first free mcb in conventional memory
1.1.1.33 root 18165: msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8 root 18166: first_mcb = seg;
18167:
1.1.1.19 root 18168: // dummy mcb to link to umb
1.1.1.33 root 18169: #if 0
1.1.1.39 root 18170: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33 root 18171: #else
1.1.1.39 root 18172: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33 root 18173: #endif
1.1.1.19 root 18174:
18175: // first free mcb in upper memory block
1.1.1.8 root 18176: msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1 root 18177:
1.1.1.29 root 18178: #ifdef SUPPORT_HMA
18179: // first free mcb in high memory area
18180: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
18181: #endif
18182:
1.1.1.26 root 18183: // interrupt vector
1.1.1.58 root 18184: for(int i = 0; i < 256; i++) {
18185: // 00-07: CPU exception handler
18186: // 08-0F: IRQ 0-7
18187: // 10-1F: PC BIOS
18188: // 20-3F: MS-DOS system call
18189: // 70-77: IRQ 8-15
18190: *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26 root 18191: *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
18192: }
1.1.1.49 root 18193: *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018; // fffc:0018 irq0 (system timer)
18194: *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26 root 18195: *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000; // ffff:0000 boot
18196: *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
18197: *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012; // xxxx:0012 ems
18198: *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49 root 18199: *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000; // fffc:0000 irq12 (mouse)
18200: *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26 root 18201:
1.1.1.29 root 18202: // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26 root 18203: static const struct {
18204: UINT16 attributes;
18205: char *dev_name;
18206: } dummy_devices[] = {
18207: {0x8013, "CON "},
18208: {0x8000, "AUX "},
18209: {0xa0c0, "PRN "},
18210: {0x8008, "CLOCK$ "},
18211: {0x8000, "COM1 "},
18212: {0xa0c0, "LPT1 "},
18213: {0xa0c0, "LPT2 "},
18214: {0xa0c0, "LPT3 "},
18215: {0x8000, "COM2 "},
18216: {0x8000, "COM3 "},
18217: {0x8000, "COM4 "},
1.1.1.30 root 18218: // {0xc000, "CONFIG$ "},
18219: {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26 root 18220: };
18221: static const UINT8 dummy_device_routine[] = {
18222: // from NUL device of Windows 98 SE
18223: // or word ptr ES:[BX+03],0100
18224: 0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
18225: // retf
18226: 0xcb,
18227: };
1.1.1.29 root 18228: device_t *last = NULL;
1.1.1.32 root 18229: for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26 root 18230: device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29 root 18231: device->next_driver.w.l = 22 + 18 * (i + 1);
18232: device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 18233: device->attributes = dummy_devices[i].attributes;
1.1.1.29 root 18234: device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
18235: device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26 root 18236: memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29 root 18237: last = device;
18238: }
18239: if(last != NULL) {
18240: last->next_driver.w.l = 0;
18241: last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26 root 18242: }
1.1.1.29 root 18243: memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26 root 18244:
1.1.1.25 root 18245: // dos info
18246: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
18247: dos_info->magic_word = 1;
18248: dos_info->first_mcb = MEMORY_TOP >> 4;
18249: dos_info->first_dpb.w.l = 0;
18250: dos_info->first_dpb.w.h = DPB_TOP >> 4;
18251: dos_info->first_sft.w.l = 0;
18252: dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41 root 18253: dos_info->clock_device.w.l = 22 + 18 * 3; // CLOCK$ is the 4th device in IO.SYS
1.1.1.25 root 18254: dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 18255: dos_info->con_device.w.l = 22 + 18 * 0; // CON is the 1st device in IO.SYS
1.1.1.25 root 18256: dos_info->con_device.w.h = DEVICE_TOP >> 4;
18257: dos_info->max_sector_len = 512;
18258: dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
18259: dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
18260: dos_info->cds.w.l = 0;
18261: dos_info->cds.w.h = CDS_TOP >> 4;
18262: dos_info->fcb_table.w.l = 0;
18263: dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
18264: dos_info->last_drive = 'Z' - 'A' + 1;
18265: dos_info->buffers_x = 20;
18266: dos_info->buffers_y = 0;
18267: dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29 root 18268: dos_info->nul_device.next_driver.w.l = 22;
18269: dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25 root 18270: dos_info->nul_device.attributes = 0x8004;
1.1.1.29 root 18271: dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
18272: dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 18273: memcpy(dos_info->nul_device.dev_name, "NUL ", 8);
18274: dos_info->disk_buf_heads.w.l = 0;
18275: dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39 root 18276: dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25 root 18277: dos_info->first_umb_fcb = UMB_TOP >> 4;
18278: dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29 root 18279: memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25 root 18280:
18281: char *env;
18282: if((env = getenv("LASTDRIVE")) != NULL) {
18283: if(env[0] >= 'A' && env[0] <= 'Z') {
18284: dos_info->last_drive = env[0] - 'A' + 1;
18285: } else if(env[0] >= 'a' && env[0] <= 'z') {
18286: dos_info->last_drive = env[0] - 'a' + 1;
18287: }
18288: }
18289: if((env = getenv("windir")) != NULL) {
18290: if(env[0] >= 'A' && env[0] <= 'Z') {
18291: dos_info->boot_drive = env[0] - 'A' + 1;
18292: } else if(env[0] >= 'a' && env[0] <= 'z') {
18293: dos_info->boot_drive = env[0] - 'a' + 1;
18294: }
18295: }
18296: #if defined(HAS_I386)
18297: dos_info->i386_or_later = 1;
18298: #else
18299: dos_info->i386_or_later = 0;
18300: #endif
18301: dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
18302:
1.1.1.27 root 18303: // ems (int 67h) and xms
1.1.1.25 root 18304: device_t *xms_device = (device_t *)(mem + XMS_TOP);
18305: xms_device->next_driver.w.l = 0xffff;
18306: xms_device->next_driver.w.h = 0xffff;
18307: xms_device->attributes = 0xc000;
1.1.1.29 root 18308: xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
18309: xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 18310: memcpy(xms_device->dev_name, "EMMXXXX0", 8);
18311:
1.1.1.59 root 18312: mem[XMS_TOP + 0x12] = 0xcd; // int 65h (dummy)
18313: mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26 root 18314: mem[XMS_TOP + 0x14] = 0xcf; // iret
1.1.1.19 root 18315: #ifdef SUPPORT_XMS
18316: if(support_xms) {
1.1.1.59 root 18317: mem[XMS_TOP + 0x15] = 0xcd; // int 66h (dummy)
18318: mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26 root 18319: mem[XMS_TOP + 0x17] = 0xcb; // retf
1.1.1.19 root 18320: } else
18321: #endif
1.1.1.26 root 18322: mem[XMS_TOP + 0x15] = 0xcb; // retf
1.1.1.29 root 18323: memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19 root 18324:
1.1.1.26 root 18325: // irq12 routine (mouse)
1.1.1.59 root 18326: mem[DUMMY_TOP + 0x00] = 0xcd; // int 69h (dummy)
18327: mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49 root 18328: mem[DUMMY_TOP + 0x02] = 0x9a; // call far mouse
18329: mem[DUMMY_TOP + 0x03] = 0xff;
18330: mem[DUMMY_TOP + 0x04] = 0xff;
18331: mem[DUMMY_TOP + 0x05] = 0xff;
18332: mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59 root 18333: // mem[DUMMY_TOP + 0x07] = 0xcd; // int 6ah (dummy)
18334: // mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49 root 18335: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6bh (dummy)
18336: mem[DUMMY_TOP + 0x08] = 0x6b;
18337: mem[DUMMY_TOP + 0x09] = 0xcf; // iret
1.1.1.24 root 18338:
1.1.1.27 root 18339: // case map routine
1.1.1.49 root 18340: mem[DUMMY_TOP + 0x0a] = 0xcd; // int 6ch (dummy)
18341: mem[DUMMY_TOP + 0x0b] = 0x6c;
18342: mem[DUMMY_TOP + 0x0c] = 0xcb; // retf
1.1.1.27 root 18343:
18344: // font read routine
1.1.1.49 root 18345: mem[DUMMY_TOP + 0x0d] = 0xcd; // int 6dh (dummy)
18346: mem[DUMMY_TOP + 0x0e] = 0x6d;
18347: mem[DUMMY_TOP + 0x0f] = 0xcb; // retf
1.1.1.19 root 18348:
1.1.1.32 root 18349: // error message read routine
1.1.1.49 root 18350: mem[DUMMY_TOP + 0x10] = 0xcd; // int 6eh (dummy)
18351: mem[DUMMY_TOP + 0x11] = 0x6e;
18352: mem[DUMMY_TOP + 0x12] = 0xcb; // retf
1.1.1.32 root 18353:
1.1.1.35 root 18354: // dummy loop to wait BIOS/DOS service is done
1.1.1.49 root 18355: mem[DUMMY_TOP + 0x13] = 0xe6; // out f7h, al
18356: mem[DUMMY_TOP + 0x14] = 0xf7;
18357: mem[DUMMY_TOP + 0x15] = 0x78; // js/jns -4
18358: mem[DUMMY_TOP + 0x16] = 0xfc;
18359: mem[DUMMY_TOP + 0x17] = 0xcb; // retf
1.1.1.35 root 18360:
1.1.1.50 root 18361: // irq0 routine (system timer)
1.1.1.49 root 18362: mem[DUMMY_TOP + 0x18] = 0xcd; // int 1ch
18363: mem[DUMMY_TOP + 0x19] = 0x1c;
18364: mem[DUMMY_TOP + 0x1a] = 0xea; // jmp far (IRET_TOP >> 4):0008
18365: mem[DUMMY_TOP + 0x1b] = 0x08;
18366: mem[DUMMY_TOP + 0x1c] = 0x00;
18367: mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4) ) & 0xff;
18368: mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
18369:
18370: // alter page map and call routine
18371: mem[DUMMY_TOP + 0x1f] = 0x9a; // call far
18372: mem[DUMMY_TOP + 0x20] = 0xff;
18373: mem[DUMMY_TOP + 0x21] = 0xff;
18374: mem[DUMMY_TOP + 0x22] = 0xff;
18375: mem[DUMMY_TOP + 0x23] = 0xff;
18376: mem[DUMMY_TOP + 0x24] = 0xcd; // int 6fh (dummy)
18377: mem[DUMMY_TOP + 0x25] = 0x6f;
18378: mem[DUMMY_TOP + 0x26] = 0xcb; // retf
1.1.1.14 root 18379:
1.1.1.50 root 18380: // call int 29h routine
18381: mem[DUMMY_TOP + 0x27] = 0xcd; // int 29h
18382: mem[DUMMY_TOP + 0x28] = 0x29;
18383: mem[DUMMY_TOP + 0x29] = 0xcb; // retf
18384:
1.1.1.26 root 18385: // boot routine
1.1.1.59 root 18386: mem[0xffff0 + 0x00] = 0xf4; // halt to exit MS-DOS Player
18387: #if 1
18388: mem[0xffff0 + 0x05] = '0'; // rom date (same as DOSBox)
18389: mem[0xffff0 + 0x06] = '1';
18390: mem[0xffff0 + 0x07] = '/';
18391: mem[0xffff0 + 0x08] = '0';
18392: mem[0xffff0 + 0x09] = '1';
18393: mem[0xffff0 + 0x0a] = '/';
18394: mem[0xffff0 + 0x0b] = '9';
18395: mem[0xffff0 + 0x0c] = '2';
18396: mem[0xffff0 + 0x0e] = 0xfc; // machine id (pc/at)
18397: mem[0xffff0 + 0x0f] = 0x55; // signature
18398: #else
18399: mem[0xffff0 + 0x05] = '0'; // rom date (same as Windows 98 SE)
1.1.1.49 root 18400: mem[0xffff0 + 0x06] = '2';
18401: mem[0xffff0 + 0x07] = '/';
18402: mem[0xffff0 + 0x08] = '2';
18403: mem[0xffff0 + 0x09] = '2';
18404: mem[0xffff0 + 0x0a] = '/';
18405: mem[0xffff0 + 0x0b] = '0';
18406: mem[0xffff0 + 0x0c] = '6';
1.1.1.59 root 18407: mem[0xffff0 + 0x0e] = 0xfc; // machine id (pc/at)
1.1.1.49 root 18408: mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59 root 18409: #endif
1.1.1.24 root 18410:
1.1 root 18411: // param block
18412: // + 0: param block (22bytes)
18413: // +24: fcb1/2 (20bytes)
18414: // +44: command tail (128bytes)
18415: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
18416: param->env_seg = 0;
18417: param->cmd_line.w.l = 44;
18418: param->cmd_line.w.h = (WORK_TOP >> 4);
18419: param->fcb1.w.l = 24;
18420: param->fcb1.w.h = (WORK_TOP >> 4);
18421: param->fcb2.w.l = 24;
18422: param->fcb2.w.h = (WORK_TOP >> 4);
18423:
18424: memset(mem + WORK_TOP + 24, 0x20, 20);
18425:
18426: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
18427: if(argc > 1) {
18428: sprintf(cmd_line->cmd, " %s", argv[1]);
18429: for(int i = 2; i < argc; i++) {
18430: char tmp[128];
18431: sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
18432: strcpy(cmd_line->cmd, tmp);
18433: }
18434: cmd_line->len = (UINT8)strlen(cmd_line->cmd);
18435: } else {
18436: cmd_line->len = 0;
18437: }
18438: cmd_line->cmd[cmd_line->len] = 0x0d;
18439:
18440: // system file table
1.1.1.21 root 18441: *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
18442: *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1 root 18443:
1.1.1.19 root 18444: // disk buffer header (from DOSBox)
18445: *(UINT16 *)(mem + DISK_BUF_TOP + 0) = 0xffff; // forward ptr
18446: *(UINT16 *)(mem + DISK_BUF_TOP + 2) = 0xffff; // backward ptr
18447: *(UINT8 *)(mem + DISK_BUF_TOP + 4) = 0xff; // not in use
18448: *(UINT8 *)(mem + DISK_BUF_TOP + 10) = 0x01; // number of FATs
18449: *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff; // pointer to DPB
18450:
1.1 root 18451: // fcb table
18452: *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14 root 18453: *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1 root 18454:
1.1.1.41 root 18455: // drive parameter block
1.1.1.42 root 18456: for(int i = 0; i < 2; i++) {
1.1.1.43 root 18457: // may be a floppy drive
1.1.1.44 root 18458: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
18459: sprintf(cds->path_name, "%c:\\", 'A' + i);
18460: cds->drive_attrib = 0x4000; // physical drive
18461: cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
18462: cds->dpb_ptr.w.h = DPB_TOP >> 4;
18463: cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
18464: cds->bs_offset = 2;
18465:
1.1.1.41 root 18466: dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
18467: dpb->drive_num = i;
18468: dpb->unit_num = i;
1.1.1.43 root 18469: dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
18470: dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42 root 18471: }
18472: for(int i = 2; i < 26; i++) {
1.1.1.44 root 18473: msdos_cds_update(i);
1.1.1.42 root 18474: UINT16 seg, ofs;
18475: msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41 root 18476: }
18477:
1.1.1.17 root 18478: // nls stuff
18479: msdos_nls_tables_init();
1.1 root 18480:
18481: // execute command
1.1.1.28 root 18482: try {
1.1.1.52 root 18483: if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28 root 18484: fatalerror("'%s' not found\n", argv[0]);
18485: }
18486: } catch(...) {
18487: // we should not reach here :-(
18488: fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1 root 18489: }
18490: retval = 0;
18491: return(0);
18492: }
18493:
18494: #define remove_std_file(path) { \
18495: int fd = _open(path, _O_RDONLY | _O_BINARY); \
18496: if(fd != -1) { \
18497: _lseek(fd, 0, SEEK_END); \
18498: int size = _tell(fd); \
18499: _close(fd); \
18500: if(size == 0) { \
18501: remove(path); \
18502: } \
18503: } \
18504: }
18505:
18506: void msdos_finish()
18507: {
18508: for(int i = 0; i < MAX_FILES; i++) {
18509: if(file_handler[i].valid) {
18510: _close(i);
18511: }
18512: }
1.1.1.21 root 18513: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 18514: remove_std_file("stdaux.txt");
1.1.1.21 root 18515: #endif
1.1.1.30 root 18516: #ifdef SUPPORT_XMS
18517: msdos_xms_finish();
18518: #endif
1.1 root 18519: msdos_dbcs_table_finish();
18520: }
18521:
18522: /* ----------------------------------------------------------------------------
18523: PC/AT hardware emulation
18524: ---------------------------------------------------------------------------- */
18525:
18526: void hardware_init()
18527: {
1.1.1.3 root 18528: CPU_INIT_CALL(CPU_MODEL);
1.1 root 18529: CPU_RESET_CALL(CPU_MODEL);
1.1.1.14 root 18530: m_IF = 1;
1.1.1.3 root 18531: #if defined(HAS_I386)
1.1 root 18532: cpu_type = (REG32(EDX) >> 8) & 0x0f;
18533: cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3 root 18534: #endif
18535: i386_set_a20_line(0);
1.1.1.14 root 18536:
1.1.1.19 root 18537: ems_init();
1.1.1.25 root 18538: dma_init();
1.1 root 18539: pic_init();
1.1.1.25 root 18540: pio_init();
1.1.1.8 root 18541: #ifdef PIT_ALWAYS_RUNNING
18542: pit_init();
18543: #else
1.1 root 18544: pit_active = 0;
18545: #endif
1.1.1.25 root 18546: sio_init();
1.1.1.8 root 18547: cmos_init();
18548: kbd_init();
1.1 root 18549: }
18550:
1.1.1.10 root 18551: void hardware_finish()
18552: {
18553: #if defined(HAS_I386)
18554: vtlb_free(m_vtlb);
18555: #endif
1.1.1.19 root 18556: ems_finish();
1.1.1.37 root 18557: pio_finish();
1.1.1.25 root 18558: sio_finish();
1.1.1.10 root 18559: }
18560:
1.1.1.28 root 18561: void hardware_release()
18562: {
18563: // release hardware resources when this program will be terminated abnormally
18564: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 18565: if(fp_debug_log != NULL) {
18566: fclose(fp_debug_log);
18567: fp_debug_log = NULL;
1.1.1.28 root 18568: }
18569: #endif
18570: #if defined(HAS_I386)
18571: vtlb_free(m_vtlb);
18572: #endif
18573: ems_release();
1.1.1.37 root 18574: pio_release();
1.1.1.28 root 18575: sio_release();
18576: }
18577:
1.1 root 18578: void hardware_run()
18579: {
1.1.1.22 root 18580: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28 root 18581: // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33 root 18582: fp_debug_log = fopen("debug.log", "w");
1.1.1.22 root 18583: #endif
1.1.1.51 root 18584: #ifdef USE_DEBUGGER
18585: m_int_num = -1;
18586: #endif
1.1.1.54 root 18587: while(!m_exit) {
1.1.1.50 root 18588: hardware_run_cpu();
1.1 root 18589: }
1.1.1.22 root 18590: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 18591: if(fp_debug_log != NULL) {
18592: fclose(fp_debug_log);
18593: fp_debug_log = NULL;
1.1.1.28 root 18594: }
1.1.1.22 root 18595: #endif
1.1 root 18596: }
18597:
1.1.1.50 root 18598: inline void hardware_run_cpu()
18599: {
18600: #if defined(HAS_I386)
18601: CPU_EXECUTE_CALL(i386);
18602: if(m_eip != m_prev_eip) {
18603: idle_ops++;
18604: }
18605: #else
18606: CPU_EXECUTE_CALL(CPU_MODEL);
18607: if(m_pc != m_prevpc) {
18608: idle_ops++;
18609: }
18610: #endif
18611: #ifdef USE_DEBUGGER
18612: // Disallow reentering CPU_EXECUTE() in msdos_syscall()
18613: if(m_int_num >= 0) {
18614: unsigned num = (unsigned)m_int_num;
18615: m_int_num = -1;
18616: msdos_syscall(num);
18617: }
18618: #endif
18619: if(++update_ops == UPDATE_OPS) {
18620: update_ops = 0;
18621: hardware_update();
18622: }
18623: }
18624:
1.1 root 18625: void hardware_update()
18626: {
1.1.1.8 root 18627: static UINT32 prev_time = 0;
18628: UINT32 cur_time = timeGetTime();
18629:
18630: if(prev_time != cur_time) {
18631: // update pit and raise irq0
18632: #ifndef PIT_ALWAYS_RUNNING
18633: if(pit_active)
18634: #endif
18635: {
18636: if(pit_run(0, cur_time)) {
18637: pic_req(0, 0, 1);
18638: }
18639: pit_run(1, cur_time);
18640: pit_run(2, cur_time);
18641: }
1.1.1.24 root 18642:
1.1.1.25 root 18643: // update sio and raise irq4/3
1.1.1.29 root 18644: for(int c = 0; c < 4; c++) {
1.1.1.25 root 18645: sio_update(c);
18646: }
18647:
1.1.1.24 root 18648: // update keyboard and mouse
1.1.1.14 root 18649: static UINT32 prev_tick = 0;
18650: UINT32 cur_tick = cur_time / 32;
1.1.1.25 root 18651:
1.1.1.14 root 18652: if(prev_tick != cur_tick) {
18653: // update keyboard flags
18654: UINT8 state;
1.1.1.24 root 18655: state = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
18656: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
18657: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
18658: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
18659: state |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
18660: state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
18661: state |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
18662: state |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
1.1.1.14 root 18663: mem[0x417] = state;
18664: state = (GetAsyncKeyState(VK_INSERT ) & 0x8000) ? 0x80 : 0;
18665: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
18666: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
18667: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
1.1.1.24 root 18668: // state |= (GetAsyncKeyState(VK_PAUSE ) & 0x0001) ? 0x08 : 0;
18669: // state |= (GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x04 : 0;
1.1.1.14 root 18670: state |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
18671: state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
18672: mem[0x418] = state;
18673:
1.1.1.24 root 18674: // update console input if needed
1.1.1.34 root 18675: if(!key_changed || mouse.hidden == 0) {
1.1.1.24 root 18676: update_console_input();
18677: }
1.1.1.57 root 18678: if(!(kbd_status & 1)) {
18679: if(key_buf_data != NULL) {
18680: #ifdef USE_SERVICE_THREAD
18681: EnterCriticalSection(&key_buf_crit_sect);
18682: #endif
18683: if(!key_buf_data->empty()) {
18684: kbd_data = key_buf_data->read();
18685: kbd_status |= 1;
18686: key_changed = true;
18687: }
18688: #ifdef USE_SERVICE_THREAD
18689: LeaveCriticalSection(&key_buf_crit_sect);
18690: #endif
18691: }
18692: }
1.1.1.24 root 18693:
1.1.1.57 root 18694: // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56 root 18695: if(!key_changed) {
1.1.1.55 root 18696: #ifdef USE_SERVICE_THREAD
1.1.1.56 root 18697: EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55 root 18698: #endif
1.1.1.57 root 18699: if(!pcbios_is_key_buffer_empty()) {
18700: /*
18701: if(!(kbd_status & 1)) {
18702: UINT16 head = *(UINT16 *)(mem + 0x41a);
18703: UINT16 tail = *(UINT16 *)(mem + 0x41c);
18704: if(head != tail) {
18705: int key_char = mem[0x400 + (head++)];
18706: int key_scan = mem[0x400 + (head++)];
18707: kbd_data = key_char ? key_char : key_scan;
18708: kbd_status |= 1;
18709: }
18710: }
18711: */
18712: key_changed = true;
18713: }
1.1.1.55 root 18714: #ifdef USE_SERVICE_THREAD
1.1.1.56 root 18715: LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55 root 18716: #endif
1.1.1.56 root 18717: }
18718: if(key_changed) {
1.1.1.8 root 18719: pic_req(0, 1, 1);
1.1.1.56 root 18720: key_changed = false;
1.1.1.24 root 18721: }
18722:
18723: // raise irq12 if mouse status is changed
1.1.1.59 root 18724: if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54 root 18725: mouse.status_irq = 0; // ???
18726: mouse.status_irq_alt = 0; // ???
18727: mouse.status_irq_ps2 = mouse.status & 0x1f;
18728: mouse.status = 0;
18729: pic_req(1, 4, 1);
1.1.1.59 root 18730: } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43 root 18731: mouse.status_irq = mouse.status & mouse.call_mask;
18732: mouse.status_irq_alt = 0; // ???
1.1.1.54 root 18733: mouse.status_irq_ps2 = 0; // ???
1.1.1.24 root 18734: mouse.status &= ~mouse.call_mask;
1.1.1.43 root 18735: pic_req(1, 4, 1);
18736: } else {
18737: for(int i = 0; i < 8; i++) {
18738: if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
18739: mouse.status_irq = 0; // ???
18740: mouse.status_irq_alt = 0;
1.1.1.54 root 18741: mouse.status_irq_ps2 = 0; // ???
1.1.1.43 root 18742: for(int j = 0; j < 8; j++) {
18743: if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
18744: mouse.status_irq_alt |= (1 << j);
18745: mouse.status_alt &= ~(1 << j);
18746: }
18747: }
18748: pic_req(1, 4, 1);
18749: break;
18750: }
18751: }
1.1.1.8 root 18752: }
1.1.1.24 root 18753:
1.1.1.60! root 18754: prev_tick = cur_tick;
! 18755: }
! 18756:
! 18757: // update cursor size/position by crtc
! 18758: if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
! 18759: int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
! 18760: if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
! 18761: ci_new.bVisible = TRUE;
! 18762: ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59 root 18763: } else {
1.1.1.60! root 18764: ci_new.bVisible = FALSE;
! 18765: }
! 18766: crtc_changed[10] = crtc_changed[11] = 0;
! 18767: }
! 18768: if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
! 18769: if(cursor_moved) {
! 18770: pcbios_update_cursor_position();
! 18771: cursor_moved = false;
1.1.1.59 root 18772: }
1.1.1.60! root 18773: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
! 18774: int position = crtc_regs[14] * 256 + crtc_regs[15];
! 18775: int width = *(UINT16 *)(mem + 0x44a);
! 18776: COORD co;
! 18777: co.X = position % width;
! 18778: co.Y = position / width + scr_top;
! 18779: SetConsoleCursorPosition(hStdout, co);
1.1.1.59 root 18780:
1.1.1.60! root 18781: crtc_changed[14] = crtc_changed[15] = 0;
! 18782: cursor_moved_by_crtc = true;
! 18783: }
! 18784:
! 18785: // update cursor info
! 18786: if(!is_cursor_blink_off()) {
! 18787: ci_new.bVisible = TRUE;
! 18788: }
! 18789: if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
! 18790: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
! 18791: SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8 root 18792: }
1.1.1.60! root 18793: ci_old = ci_new;
1.1.1.24 root 18794:
1.1.1.19 root 18795: // update daily timer counter
18796: pcbios_update_daily_timer_counter(cur_time);
1.1.1.25 root 18797:
1.1.1.8 root 18798: prev_time = cur_time;
1.1 root 18799: }
18800: }
18801:
1.1.1.19 root 18802: // ems
18803:
18804: void ems_init()
18805: {
18806: memset(ems_handles, 0, sizeof(ems_handles));
18807: memset(ems_pages, 0, sizeof(ems_pages));
18808: free_ems_pages = MAX_EMS_PAGES;
18809: }
18810:
18811: void ems_finish()
18812: {
1.1.1.28 root 18813: ems_release();
18814: }
18815:
18816: void ems_release()
18817: {
1.1.1.31 root 18818: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28 root 18819: if(ems_handles[i].buffer != NULL) {
1.1.1.19 root 18820: free(ems_handles[i].buffer);
18821: ems_handles[i].buffer = NULL;
18822: }
18823: }
18824: }
18825:
18826: void ems_allocate_pages(int handle, int pages)
18827: {
18828: if(pages > 0) {
18829: ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
18830: } else {
18831: ems_handles[handle].buffer = NULL;
18832: }
18833: ems_handles[handle].pages = pages;
18834: ems_handles[handle].allocated = true;
18835: free_ems_pages -= pages;
18836: }
18837:
18838: void ems_reallocate_pages(int handle, int pages)
18839: {
18840: if(ems_handles[handle].allocated) {
18841: if(ems_handles[handle].pages != pages) {
18842: UINT8 *new_buffer = NULL;
18843:
18844: if(pages > 0) {
18845: new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
18846: }
1.1.1.32 root 18847: if(ems_handles[handle].buffer != NULL) {
18848: if(new_buffer != NULL) {
1.1.1.19 root 18849: if(pages > ems_handles[handle].pages) {
18850: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
18851: } else {
18852: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
18853: }
18854: }
18855: free(ems_handles[handle].buffer);
18856: ems_handles[handle].buffer = NULL;
18857: }
18858: free_ems_pages += ems_handles[handle].pages;
18859:
18860: ems_handles[handle].buffer = new_buffer;
18861: ems_handles[handle].pages = pages;
18862: free_ems_pages -= pages;
18863: }
18864: } else {
18865: ems_allocate_pages(handle, pages);
18866: }
18867: }
18868:
18869: void ems_release_pages(int handle)
18870: {
18871: if(ems_handles[handle].allocated) {
1.1.1.32 root 18872: if(ems_handles[handle].buffer != NULL) {
1.1.1.19 root 18873: free(ems_handles[handle].buffer);
18874: ems_handles[handle].buffer = NULL;
18875: }
18876: free_ems_pages += ems_handles[handle].pages;
18877: ems_handles[handle].allocated = false;
18878: }
18879: }
18880:
18881: void ems_map_page(int physical, int handle, int logical)
18882: {
18883: if(ems_pages[physical].mapped) {
18884: if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
18885: return;
18886: }
18887: ems_unmap_page(physical);
18888: }
1.1.1.32 root 18889: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 18890: memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
18891: }
18892: ems_pages[physical].handle = handle;
18893: ems_pages[physical].page = logical;
18894: ems_pages[physical].mapped = true;
18895: }
18896:
18897: void ems_unmap_page(int physical)
18898: {
18899: if(ems_pages[physical].mapped) {
18900: int handle = ems_pages[physical].handle;
18901: int logical = ems_pages[physical].page;
18902:
1.1.1.32 root 18903: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 18904: memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
18905: }
18906: ems_pages[physical].mapped = false;
18907: }
18908: }
18909:
1.1.1.25 root 18910: // dma
1.1 root 18911:
1.1.1.25 root 18912: void dma_init()
1.1 root 18913: {
1.1.1.26 root 18914: memset(dma, 0, sizeof(dma));
1.1.1.25 root 18915: for(int c = 0; c < 2; c++) {
1.1.1.26 root 18916: // for(int ch = 0; ch < 4; ch++) {
18917: // dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
18918: // }
1.1.1.25 root 18919: dma_reset(c);
18920: }
1.1 root 18921: }
18922:
1.1.1.25 root 18923: void dma_reset(int c)
1.1 root 18924: {
1.1.1.25 root 18925: dma[c].low_high = false;
18926: dma[c].cmd = dma[c].req = dma[c].tc = 0;
18927: dma[c].mask = 0xff;
18928: }
18929:
18930: void dma_write(int c, UINT32 addr, UINT8 data)
18931: {
18932: int ch = (addr >> 1) & 3;
18933: UINT8 bit = 1 << (data & 3);
18934:
18935: switch(addr & 0x0f) {
18936: case 0x00: case 0x02: case 0x04: case 0x06:
18937: if(dma[c].low_high) {
18938: dma[c].ch[ch].bareg.b.h = data;
1.1 root 18939: } else {
1.1.1.25 root 18940: dma[c].ch[ch].bareg.b.l = data;
18941: }
18942: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
18943: dma[c].low_high = !dma[c].low_high;
18944: break;
18945: case 0x01: case 0x03: case 0x05: case 0x07:
18946: if(dma[c].low_high) {
18947: dma[c].ch[ch].bcreg.b.h = data;
18948: } else {
18949: dma[c].ch[ch].bcreg.b.l = data;
18950: }
18951: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
18952: dma[c].low_high = !dma[c].low_high;
18953: break;
18954: case 0x08:
18955: // command register
18956: dma[c].cmd = data;
18957: break;
18958: case 0x09:
18959: // dma[c].request register
18960: if(data & 4) {
18961: if(!(dma[c].req & bit)) {
18962: dma[c].req |= bit;
18963: // dma_run(c, ch);
18964: }
18965: } else {
18966: dma[c].req &= ~bit;
18967: }
18968: break;
18969: case 0x0a:
18970: // single mask register
18971: if(data & 4) {
18972: dma[c].mask |= bit;
18973: } else {
18974: dma[c].mask &= ~bit;
18975: }
18976: break;
18977: case 0x0b:
18978: // mode register
18979: dma[c].ch[data & 3].mode = data;
18980: break;
18981: case 0x0c:
18982: dma[c].low_high = false;
18983: break;
18984: case 0x0d:
18985: // clear master
18986: dma_reset(c);
18987: break;
18988: case 0x0e:
18989: // clear mask register
18990: dma[c].mask = 0;
18991: break;
18992: case 0x0f:
18993: // all mask register
18994: dma[c].mask = data & 0x0f;
18995: break;
18996: }
18997: }
18998:
18999: UINT8 dma_read(int c, UINT32 addr)
19000: {
19001: int ch = (addr >> 1) & 3;
19002: UINT8 val = 0xff;
19003:
19004: switch(addr & 0x0f) {
19005: case 0x00: case 0x02: case 0x04: case 0x06:
19006: if(dma[c].low_high) {
19007: val = dma[c].ch[ch].areg.b.h;
19008: } else {
19009: val = dma[c].ch[ch].areg.b.l;
19010: }
19011: dma[c].low_high = !dma[c].low_high;
19012: return(val);
19013: case 0x01: case 0x03: case 0x05: case 0x07:
19014: if(dma[c].low_high) {
19015: val = dma[c].ch[ch].creg.b.h;
19016: } else {
19017: val = dma[c].ch[ch].creg.b.l;
19018: }
19019: dma[c].low_high = !dma[c].low_high;
19020: return(val);
19021: case 0x08:
19022: // status register
19023: val = (dma[c].req << 4) | dma[c].tc;
19024: dma[c].tc = 0;
19025: return(val);
19026: case 0x0d:
1.1.1.26 root 19027: // temporary register (intel 82374 does not support)
1.1.1.25 root 19028: return(dma[c].tmp & 0xff);
1.1.1.26 root 19029: case 0x0f:
19030: // mask register (intel 82374 does support)
19031: return(dma[c].mask);
1.1.1.25 root 19032: }
19033: return(0xff);
19034: }
19035:
19036: void dma_page_write(int c, int ch, UINT8 data)
19037: {
19038: dma[c].ch[ch].pagereg = data;
19039: }
19040:
19041: UINT8 dma_page_read(int c, int ch)
19042: {
19043: return(dma[c].ch[ch].pagereg);
19044: }
19045:
19046: void dma_run(int c, int ch)
19047: {
19048: UINT8 bit = 1 << ch;
19049:
19050: if((dma[c].req & bit) && !(dma[c].mask & bit)) {
19051: // execute dma
19052: while(dma[c].req & bit) {
19053: if(ch == 0 && (dma[c].cmd & 0x01)) {
19054: // memory -> memory
19055: UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
19056: UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
19057:
19058: if(c == 0) {
19059: dma[c].tmp = read_byte(saddr);
19060: write_byte(daddr, dma[c].tmp);
19061: } else {
19062: dma[c].tmp = read_word(saddr << 1);
19063: write_word(daddr << 1, dma[c].tmp);
19064: }
19065: if(!(dma[c].cmd & 0x02)) {
19066: if(dma[c].ch[0].mode & 0x20) {
19067: dma[c].ch[0].areg.w--;
19068: if(dma[c].ch[0].areg.w == 0xffff) {
19069: dma[c].ch[0].pagereg--;
19070: }
19071: } else {
19072: dma[c].ch[0].areg.w++;
19073: if(dma[c].ch[0].areg.w == 0) {
19074: dma[c].ch[0].pagereg++;
19075: }
19076: }
19077: }
19078: if(dma[c].ch[1].mode & 0x20) {
19079: dma[c].ch[1].areg.w--;
19080: if(dma[c].ch[1].areg.w == 0xffff) {
19081: dma[c].ch[1].pagereg--;
19082: }
19083: } else {
19084: dma[c].ch[1].areg.w++;
19085: if(dma[c].ch[1].areg.w == 0) {
19086: dma[c].ch[1].pagereg++;
19087: }
19088: }
19089:
19090: // check dma condition
19091: if(dma[c].ch[0].creg.w-- == 0) {
19092: if(dma[c].ch[0].mode & 0x10) {
19093: // self initialize
19094: dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
19095: dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
19096: } else {
19097: // dma[c].mask |= bit;
19098: }
19099: }
19100: if(dma[c].ch[1].creg.w-- == 0) {
19101: // terminal count
19102: if(dma[c].ch[1].mode & 0x10) {
19103: // self initialize
19104: dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
19105: dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
19106: } else {
19107: dma[c].mask |= bit;
19108: }
19109: dma[c].req &= ~bit;
19110: dma[c].tc |= bit;
19111: }
19112: } else {
19113: UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
19114:
19115: if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
19116: // verify
19117: } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
19118: // io -> memory
19119: if(c == 0) {
19120: dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
19121: write_byte(addr, dma[c].tmp);
19122: } else {
19123: dma[c].tmp = read_io_word(dma[c].ch[ch].port);
19124: write_word(addr << 1, dma[c].tmp);
19125: }
19126: } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
19127: // memory -> io
19128: if(c == 0) {
19129: dma[c].tmp = read_byte(addr);
19130: write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
19131: } else {
19132: dma[c].tmp = read_word(addr << 1);
19133: write_io_word(dma[c].ch[ch].port, dma[c].tmp);
19134: }
19135: }
19136: if(dma[c].ch[ch].mode & 0x20) {
19137: dma[c].ch[ch].areg.w--;
19138: if(dma[c].ch[ch].areg.w == 0xffff) {
19139: dma[c].ch[ch].pagereg--;
19140: }
19141: } else {
19142: dma[c].ch[ch].areg.w++;
19143: if(dma[c].ch[ch].areg.w == 0) {
19144: dma[c].ch[ch].pagereg++;
19145: }
19146: }
19147:
19148: // check dma condition
19149: if(dma[c].ch[ch].creg.w-- == 0) {
19150: // terminal count
19151: if(dma[c].ch[ch].mode & 0x10) {
19152: // self initialize
19153: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
19154: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
19155: } else {
19156: dma[c].mask |= bit;
19157: }
19158: dma[c].req &= ~bit;
19159: dma[c].tc |= bit;
19160: } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
19161: // single mode
19162: break;
19163: }
19164: }
19165: }
19166: }
19167: }
19168:
19169: // pic
19170:
19171: void pic_init()
19172: {
19173: memset(pic, 0, sizeof(pic));
19174: pic[0].imr = pic[1].imr = 0xff;
19175:
19176: // from bochs bios
19177: pic_write(0, 0, 0x11); // icw1 = 11h
19178: pic_write(0, 1, 0x08); // icw2 = 08h
19179: pic_write(0, 1, 0x04); // icw3 = 04h
19180: pic_write(0, 1, 0x01); // icw4 = 01h
19181: pic_write(0, 1, 0xb8); // ocw1 = b8h
19182: pic_write(1, 0, 0x11); // icw1 = 11h
19183: pic_write(1, 1, 0x70); // icw2 = 70h
19184: pic_write(1, 1, 0x02); // icw3 = 02h
19185: pic_write(1, 1, 0x01); // icw4 = 01h
19186: }
19187:
19188: void pic_write(int c, UINT32 addr, UINT8 data)
19189: {
19190: if(addr & 1) {
19191: if(pic[c].icw2_r) {
19192: // icw2
19193: pic[c].icw2 = data;
19194: pic[c].icw2_r = 0;
19195: } else if(pic[c].icw3_r) {
19196: // icw3
19197: pic[c].icw3 = data;
19198: pic[c].icw3_r = 0;
19199: } else if(pic[c].icw4_r) {
19200: // icw4
19201: pic[c].icw4 = data;
19202: pic[c].icw4_r = 0;
19203: } else {
19204: // ocw1
1.1 root 19205: pic[c].imr = data;
19206: }
19207: } else {
19208: if(data & 0x10) {
19209: // icw1
19210: pic[c].icw1 = data;
19211: pic[c].icw2_r = 1;
19212: pic[c].icw3_r = (data & 2) ? 0 : 1;
19213: pic[c].icw4_r = data & 1;
19214: pic[c].irr = 0;
19215: pic[c].isr = 0;
19216: pic[c].imr = 0;
19217: pic[c].prio = 0;
19218: if(!(pic[c].icw1 & 1)) {
19219: pic[c].icw4 = 0;
19220: }
19221: pic[c].ocw3 = 0;
19222: } else if(data & 8) {
19223: // ocw3
19224: if(!(data & 2)) {
19225: data = (data & ~1) | (pic[c].ocw3 & 1);
19226: }
19227: if(!(data & 0x40)) {
19228: data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
19229: }
19230: pic[c].ocw3 = data;
19231: } else {
19232: // ocw2
19233: int level = 0;
19234: if(data & 0x40) {
19235: level = data & 7;
19236: } else {
19237: if(!pic[c].isr) {
19238: return;
19239: }
19240: level = pic[c].prio;
19241: while(!(pic[c].isr & (1 << level))) {
19242: level = (level + 1) & 7;
19243: }
19244: }
19245: if(data & 0x80) {
19246: pic[c].prio = (level + 1) & 7;
19247: }
19248: if(data & 0x20) {
19249: pic[c].isr &= ~(1 << level);
19250: }
19251: }
19252: }
19253: pic_update();
19254: }
19255:
19256: UINT8 pic_read(int c, UINT32 addr)
19257: {
19258: if(addr & 1) {
19259: return(pic[c].imr);
19260: } else {
19261: // polling mode is not supported...
19262: //if(pic[c].ocw3 & 4) {
19263: // return ???;
19264: //}
19265: if(pic[c].ocw3 & 1) {
19266: return(pic[c].isr);
19267: } else {
19268: return(pic[c].irr);
19269: }
19270: }
19271: }
19272:
19273: void pic_req(int c, int level, int signal)
19274: {
19275: if(signal) {
19276: pic[c].irr |= (1 << level);
19277: } else {
19278: pic[c].irr &= ~(1 << level);
19279: }
19280: pic_update();
19281: }
19282:
19283: int pic_ack()
19284: {
19285: // ack (INTA=L)
19286: pic[pic_req_chip].isr |= pic_req_bit;
19287: pic[pic_req_chip].irr &= ~pic_req_bit;
19288: if(pic_req_chip > 0) {
19289: // update isr and irr of master
19290: UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
19291: pic[pic_req_chip - 1].isr |= slave;
19292: pic[pic_req_chip - 1].irr &= ~slave;
19293: }
19294: //if(pic[pic_req_chip].icw4 & 1) {
19295: // 8086 mode
19296: int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
19297: //} else {
19298: // // 8080 mode
19299: // UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
19300: // if(pic[pic_req_chip].icw1 & 4) {
19301: // addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
19302: // } else {
19303: // addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
19304: // }
19305: // vector = 0xcd | (addr << 8);
19306: //}
19307: if(pic[pic_req_chip].icw4 & 2) {
19308: // auto eoi
19309: pic[pic_req_chip].isr &= ~pic_req_bit;
19310: }
19311: return(vector);
19312: }
19313:
19314: void pic_update()
19315: {
19316: for(int c = 0; c < 2; c++) {
19317: UINT8 irr = pic[c].irr;
19318: if(c + 1 < 2) {
19319: // this is master
19320: if(pic[c + 1].irr & (~pic[c + 1].imr)) {
19321: // request from slave
19322: irr |= 1 << (pic[c + 1].icw3 & 7);
19323: }
19324: }
19325: irr &= (~pic[c].imr);
19326: if(!irr) {
19327: break;
19328: }
19329: if(!(pic[c].ocw3 & 0x20)) {
19330: irr |= pic[c].isr;
19331: }
19332: int level = pic[c].prio;
19333: UINT8 bit = 1 << level;
19334: while(!(irr & bit)) {
19335: level = (level + 1) & 7;
19336: bit = 1 << level;
19337: }
19338: if((c + 1 < 2) && (pic[c].icw3 & bit)) {
19339: // check slave
19340: continue;
19341: }
19342: if(pic[c].isr & bit) {
19343: break;
19344: }
19345: // interrupt request
19346: pic_req_chip = c;
19347: pic_req_level = level;
19348: pic_req_bit = bit;
1.1.1.3 root 19349: i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1 root 19350: return;
19351: }
1.1.1.3 root 19352: i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2 root 19353: }
1.1 root 19354:
1.1.1.25 root 19355: // pio
19356:
19357: void pio_init()
19358: {
1.1.1.38 root 19359: // bool conv_mode = (GetConsoleCP() == 932);
19360:
1.1.1.26 root 19361: memset(pio, 0, sizeof(pio));
1.1.1.37 root 19362:
1.1.1.25 root 19363: for(int c = 0; c < 2; c++) {
1.1.1.37 root 19364: pio[c].stat = 0xdf;
1.1.1.25 root 19365: pio[c].ctrl = 0x0c;
1.1.1.38 root 19366: // pio[c].conv_mode = conv_mode;
1.1.1.25 root 19367: }
19368: }
19369:
1.1.1.37 root 19370: void pio_finish()
19371: {
19372: pio_release();
19373: }
19374:
19375: void pio_release()
19376: {
19377: for(int c = 0; c < 2; c++) {
19378: if(pio[c].fp != NULL) {
1.1.1.38 root 19379: if(pio[c].jis_mode) {
19380: fputc(0x1c, pio[c].fp);
19381: fputc(0x2e, pio[c].fp);
19382: }
1.1.1.37 root 19383: fclose(pio[c].fp);
19384: pio[c].fp = NULL;
19385: }
19386: }
19387: }
19388:
1.1.1.25 root 19389: void pio_write(int c, UINT32 addr, UINT8 data)
19390: {
19391: switch(addr & 3) {
19392: case 0:
19393: pio[c].data = data;
19394: break;
19395: case 2:
1.1.1.37 root 19396: if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
19397: // strobe H -> L
19398: if(pio[c].data == 0x0d && (data & 0x02)) {
19399: // auto feed
19400: printer_out(c, 0x0d);
19401: printer_out(c, 0x0a);
19402: } else {
19403: printer_out(c, pio[c].data);
19404: }
19405: pio[c].stat &= ~0x40; // set ack
19406: }
1.1.1.25 root 19407: pio[c].ctrl = data;
19408: break;
19409: }
19410: }
19411:
19412: UINT8 pio_read(int c, UINT32 addr)
19413: {
19414: switch(addr & 3) {
19415: case 0:
1.1.1.37 root 19416: if(pio[c].ctrl & 0x20) {
19417: // input mode
19418: return(0xff);
19419: }
1.1.1.25 root 19420: return(pio[c].data);
19421: case 1:
1.1.1.37 root 19422: {
19423: UINT8 stat = pio[c].stat;
19424: pio[c].stat |= 0x40; // clear ack
19425: return(stat);
19426: }
1.1.1.25 root 19427: case 2:
19428: return(pio[c].ctrl);
19429: }
19430: return(0xff);
19431: }
19432:
1.1.1.37 root 19433: void printer_out(int c, UINT8 data)
19434: {
19435: SYSTEMTIME time;
1.1.1.38 root 19436: bool jis_mode = false;
1.1.1.37 root 19437:
19438: GetLocalTime(&time);
19439:
19440: if(pio[c].fp != NULL) {
19441: // if at least 1000ms passed from last written, close the current file
19442: FILETIME ftime1;
19443: FILETIME ftime2;
19444: SystemTimeToFileTime(&pio[c].time, &ftime1);
19445: SystemTimeToFileTime(&time, &ftime2);
19446: INT64 *time1 = (INT64 *)&ftime1;
19447: INT64 *time2 = (INT64 *)&ftime2;
19448: INT64 msec = (*time2 - *time1) / 10000;
19449:
19450: if(msec >= 1000) {
1.1.1.38 root 19451: if(pio[c].jis_mode) {
19452: fputc(0x1c, pio[c].fp);
19453: fputc(0x2e, pio[c].fp);
19454: jis_mode = true;
19455: }
1.1.1.37 root 19456: fclose(pio[c].fp);
19457: pio[c].fp = NULL;
19458: }
19459: }
19460: if(pio[c].fp == NULL) {
19461: // create a new file in the temp folder
19462: char file_name[MAX_PATH];
19463:
19464: sprintf(file_name, "%d-%0.2d-%0.2d_%0.2d-%0.2d-%0.2d.PRN", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
1.1.1.60! root 19465: if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37 root 19466: strcat(pio[c].path, file_name);
19467: } else {
19468: strcpy(pio[c].path, file_name);
19469: }
1.1.1.38 root 19470: pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37 root 19471: }
19472: if(pio[c].fp != NULL) {
1.1.1.38 root 19473: if(jis_mode) {
19474: fputc(0x1c, pio[c].fp);
19475: fputc(0x26, pio[c].fp);
19476: }
1.1.1.37 root 19477: fputc(data, pio[c].fp);
1.1.1.38 root 19478:
19479: // reopen file if 1ch 26h 1ch 2eh (kanji-on kanji-off) are written at the top
19480: if(data == 0x2e && ftell(pio[c].fp) == 4) {
19481: UINT8 buffer[4];
19482: fseek(pio[c].fp, 0, SEEK_SET);
19483: fread(buffer, 4, 1, pio[c].fp);
19484: if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
19485: fclose(pio[c].fp);
19486: pio[c].fp = fopen(pio[c].path, "w+b");
19487: }
19488: }
1.1.1.37 root 19489: pio[c].time = time;
19490: }
19491: }
19492:
1.1 root 19493: // pit
19494:
1.1.1.22 root 19495: #define PIT_FREQ 1193182ULL
1.1 root 19496: #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)
19497:
19498: void pit_init()
19499: {
1.1.1.8 root 19500: memset(pit, 0, sizeof(pit));
1.1 root 19501: for(int ch = 0; ch < 3; ch++) {
19502: pit[ch].count = 0x10000;
19503: pit[ch].ctrl_reg = 0x34;
19504: pit[ch].mode = 3;
19505: }
19506:
19507: // from bochs bios
19508: pit_write(3, 0x34);
19509: pit_write(0, 0x00);
19510: pit_write(0, 0x00);
19511: }
19512:
19513: void pit_write(int ch, UINT8 val)
19514: {
1.1.1.8 root 19515: #ifndef PIT_ALWAYS_RUNNING
1.1 root 19516: if(!pit_active) {
19517: pit_active = 1;
19518: pit_init();
19519: }
1.1.1.8 root 19520: #endif
1.1 root 19521: switch(ch) {
19522: case 0:
19523: case 1:
19524: case 2:
19525: // write count register
19526: if(!pit[ch].low_write && !pit[ch].high_write) {
19527: if(pit[ch].ctrl_reg & 0x10) {
19528: pit[ch].low_write = 1;
19529: }
19530: if(pit[ch].ctrl_reg & 0x20) {
19531: pit[ch].high_write = 1;
19532: }
19533: }
19534: if(pit[ch].low_write) {
19535: pit[ch].count_reg = val;
19536: pit[ch].low_write = 0;
19537: } else if(pit[ch].high_write) {
19538: if((pit[ch].ctrl_reg & 0x30) == 0x20) {
19539: pit[ch].count_reg = val << 8;
19540: } else {
19541: pit[ch].count_reg |= val << 8;
19542: }
19543: pit[ch].high_write = 0;
19544: }
19545: // start count
1.1.1.8 root 19546: if(!pit[ch].low_write && !pit[ch].high_write) {
19547: if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
19548: pit[ch].count = PIT_COUNT_VALUE(ch);
19549: pit[ch].prev_time = timeGetTime();
19550: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 19551: }
19552: }
19553: break;
19554: case 3: // ctrl reg
19555: if((val & 0xc0) == 0xc0) {
19556: // i8254 read-back command
19557: for(ch = 0; ch < 3; ch++) {
19558: if(!(val & 0x10) && !pit[ch].status_latched) {
19559: pit[ch].status = pit[ch].ctrl_reg & 0x3f;
19560: pit[ch].status_latched = 1;
19561: }
19562: if(!(val & 0x20) && !pit[ch].count_latched) {
19563: pit_latch_count(ch);
19564: }
19565: }
19566: break;
19567: }
19568: ch = (val >> 6) & 3;
19569: if(val & 0x30) {
1.1.1.35 root 19570: static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1 root 19571: pit[ch].mode = modes[(val >> 1) & 7];
19572: pit[ch].count_latched = 0;
19573: pit[ch].low_read = pit[ch].high_read = 0;
19574: pit[ch].low_write = pit[ch].high_write = 0;
19575: pit[ch].ctrl_reg = val;
19576: // stop count
1.1.1.8 root 19577: pit[ch].prev_time = pit[ch].expired_time = 0;
1.1 root 19578: pit[ch].count_reg = 0;
19579: } else if(!pit[ch].count_latched) {
19580: pit_latch_count(ch);
19581: }
19582: break;
19583: }
19584: }
19585:
19586: UINT8 pit_read(int ch)
19587: {
1.1.1.8 root 19588: #ifndef PIT_ALWAYS_RUNNING
1.1 root 19589: if(!pit_active) {
19590: pit_active = 1;
19591: pit_init();
19592: }
1.1.1.8 root 19593: #endif
1.1 root 19594: switch(ch) {
19595: case 0:
19596: case 1:
19597: case 2:
19598: if(pit[ch].status_latched) {
19599: pit[ch].status_latched = 0;
19600: return(pit[ch].status);
19601: }
19602: // if not latched, through current count
19603: if(!pit[ch].count_latched) {
19604: if(!pit[ch].low_read && !pit[ch].high_read) {
19605: pit_latch_count(ch);
19606: }
19607: }
19608: // return latched count
19609: if(pit[ch].low_read) {
19610: pit[ch].low_read = 0;
19611: if(!pit[ch].high_read) {
19612: pit[ch].count_latched = 0;
19613: }
19614: return(pit[ch].latch & 0xff);
19615: } else if(pit[ch].high_read) {
19616: pit[ch].high_read = 0;
19617: pit[ch].count_latched = 0;
19618: return((pit[ch].latch >> 8) & 0xff);
19619: }
19620: }
19621: return(0xff);
19622: }
19623:
1.1.1.8 root 19624: int pit_run(int ch, UINT32 cur_time)
1.1 root 19625: {
1.1.1.8 root 19626: if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1 root 19627: pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8 root 19628: pit[ch].prev_time = pit[ch].expired_time;
19629: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
19630: if(cur_time >= pit[ch].expired_time) {
19631: pit[ch].prev_time = cur_time;
19632: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 19633: }
1.1.1.8 root 19634: return(1);
1.1 root 19635: }
1.1.1.8 root 19636: return(0);
1.1 root 19637: }
19638:
19639: void pit_latch_count(int ch)
19640: {
1.1.1.8 root 19641: if(pit[ch].expired_time != 0) {
1.1.1.26 root 19642: UINT32 cur_time = timeGetTime();
1.1.1.8 root 19643: pit_run(ch, cur_time);
19644: UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26 root 19645: UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
19646:
19647: if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
19648: // decrement counter in 1msec period
19649: if(pit[ch].next_latch == 0) {
19650: tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
19651: pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
19652: }
19653: if(pit[ch].latch > pit[ch].next_latch) {
19654: pit[ch].latch--;
19655: }
19656: } else {
19657: pit[ch].prev_latch = pit[ch].latch = latch;
19658: pit[ch].next_latch = 0;
19659: }
1.1.1.8 root 19660: } else {
19661: pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26 root 19662: pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1 root 19663: }
19664: pit[ch].count_latched = 1;
19665: if((pit[ch].ctrl_reg & 0x30) == 0x10) {
19666: // lower byte
19667: pit[ch].low_read = 1;
19668: pit[ch].high_read = 0;
19669: } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
19670: // upper byte
19671: pit[ch].low_read = 0;
19672: pit[ch].high_read = 1;
19673: } else {
19674: // lower -> upper
1.1.1.14 root 19675: pit[ch].low_read = pit[ch].high_read = 1;
1.1 root 19676: }
19677: }
19678:
1.1.1.8 root 19679: int pit_get_expired_time(int ch)
1.1 root 19680: {
1.1.1.22 root 19681: pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
19682: UINT64 val = pit[ch].accum >> 10;
19683: pit[ch].accum -= val << 10;
19684: return((val != 0) ? val : 1);
1.1.1.8 root 19685: }
19686:
1.1.1.25 root 19687: // sio
19688:
19689: void sio_init()
19690: {
1.1.1.26 root 19691: memset(sio, 0, sizeof(sio));
19692: memset(sio_mt, 0, sizeof(sio_mt));
19693:
1.1.1.29 root 19694: for(int c = 0; c < 4; c++) {
1.1.1.25 root 19695: sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
19696: sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
19697:
19698: sio[c].divisor.w = 12; // 115200Hz / 9600Baud
19699: sio[c].line_ctrl = 0x03; // 8bit, stop 1bit, non parity
1.1.1.26 root 19700: sio[c].modem_ctrl = 0x03; // rts=on, dtr=on
19701: sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25 root 19702: sio[c].line_stat_buf = 0x60; // send/recv buffers are empty
19703: sio[c].irq_identify = 0x01; // no pending irq
19704:
19705: InitializeCriticalSection(&sio_mt[c].csSendData);
19706: InitializeCriticalSection(&sio_mt[c].csRecvData);
19707: InitializeCriticalSection(&sio_mt[c].csLineCtrl);
19708: InitializeCriticalSection(&sio_mt[c].csLineStat);
19709: InitializeCriticalSection(&sio_mt[c].csModemCtrl);
19710: InitializeCriticalSection(&sio_mt[c].csModemStat);
19711:
1.1.1.26 root 19712: if(sio_port_number[c] != 0) {
1.1.1.25 root 19713: sio[c].channel = c;
19714: sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
19715: }
19716: }
19717: }
19718:
19719: void sio_finish()
19720: {
1.1.1.29 root 19721: for(int c = 0; c < 4; c++) {
1.1.1.25 root 19722: if(sio_mt[c].hThread != NULL) {
19723: WaitForSingleObject(sio_mt[c].hThread, INFINITE);
19724: CloseHandle(sio_mt[c].hThread);
1.1.1.28 root 19725: sio_mt[c].hThread = NULL;
1.1.1.25 root 19726: }
19727: DeleteCriticalSection(&sio_mt[c].csSendData);
19728: DeleteCriticalSection(&sio_mt[c].csRecvData);
19729: DeleteCriticalSection(&sio_mt[c].csLineCtrl);
19730: DeleteCriticalSection(&sio_mt[c].csLineStat);
19731: DeleteCriticalSection(&sio_mt[c].csModemCtrl);
19732: DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28 root 19733: }
19734: sio_release();
19735: }
19736:
19737: void sio_release()
19738: {
1.1.1.29 root 19739: for(int c = 0; c < 4; c++) {
1.1.1.28 root 19740: // sio_thread() may access the resources :-(
1.1.1.32 root 19741: bool running = (sio_mt[c].hThread != NULL);
19742:
19743: if(running) {
19744: EnterCriticalSection(&sio_mt[c].csSendData);
19745: }
19746: if(sio[c].send_buffer != NULL) {
19747: sio[c].send_buffer->release();
19748: delete sio[c].send_buffer;
19749: sio[c].send_buffer = NULL;
19750: }
19751: if(running) {
19752: LeaveCriticalSection(&sio_mt[c].csSendData);
19753: EnterCriticalSection(&sio_mt[c].csRecvData);
19754: }
19755: if(sio[c].recv_buffer != NULL) {
19756: sio[c].recv_buffer->release();
19757: delete sio[c].recv_buffer;
19758: sio[c].recv_buffer = NULL;
19759: }
19760: if(running) {
19761: LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28 root 19762: }
1.1.1.25 root 19763: }
19764: }
19765:
19766: void sio_write(int c, UINT32 addr, UINT8 data)
19767: {
19768: switch(addr & 7) {
19769: case 0:
19770: if(sio[c].selector & 0x80) {
19771: if(sio[c].divisor.b.l != data) {
19772: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19773: sio[c].divisor.b.l = data;
19774: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19775: }
19776: } else {
19777: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19778: if(sio[c].send_buffer != NULL) {
19779: sio[c].send_buffer->write(data);
19780: }
1.1.1.25 root 19781: // transmitter holding/shift registers are not empty
19782: sio[c].line_stat_buf &= ~0x60;
19783: LeaveCriticalSection(&sio_mt[c].csSendData);
19784:
19785: if(sio[c].irq_enable & 0x02) {
19786: sio_update_irq(c);
19787: }
19788: }
19789: break;
19790: case 1:
19791: if(sio[c].selector & 0x80) {
19792: if(sio[c].divisor.b.h != data) {
19793: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19794: sio[c].divisor.b.h = data;
19795: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19796: }
19797: } else {
19798: if(sio[c].irq_enable != data) {
19799: sio[c].irq_enable = data;
19800: sio_update_irq(c);
19801: }
19802: }
19803: break;
19804: case 3:
19805: {
19806: UINT8 line_ctrl = data & 0x3f;
19807: bool set_brk = ((data & 0x40) != 0);
19808:
19809: if(sio[c].line_ctrl != line_ctrl) {
19810: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19811: sio[c].line_ctrl = line_ctrl;
19812: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19813: }
19814: if(sio[c].set_brk != set_brk) {
19815: EnterCriticalSection(&sio_mt[c].csModemCtrl);
19816: sio[c].set_brk = set_brk;
19817: LeaveCriticalSection(&sio_mt[c].csModemCtrl);
19818: }
19819: }
19820: sio[c].selector = data;
19821: break;
19822: case 4:
19823: {
19824: bool set_dtr = ((data & 0x01) != 0);
19825: bool set_rts = ((data & 0x02) != 0);
19826:
19827: if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26 root 19828: // EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25 root 19829: sio[c].set_dtr = set_dtr;
19830: sio[c].set_rts = set_rts;
1.1.1.26 root 19831: // LeaveCriticalSection(&sio_mt[c].csModemCtrl);
19832:
19833: bool state_changed = false;
19834:
19835: EnterCriticalSection(&sio_mt[c].csModemStat);
19836: if(set_dtr) {
19837: sio[c].modem_stat |= 0x20; // dsr on
19838: } else {
19839: sio[c].modem_stat &= ~0x20; // dsr off
19840: }
19841: if(set_rts) {
19842: sio[c].modem_stat |= 0x10; // cts on
19843: } else {
19844: sio[c].modem_stat &= ~0x10; // cts off
19845: }
19846: if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
19847: if(!(sio[c].modem_stat & 0x02)) {
19848: if(sio[c].irq_enable & 0x08) {
19849: state_changed = true;
19850: }
19851: sio[c].modem_stat |= 0x02;
19852: }
19853: }
19854: if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
19855: if(!(sio[c].modem_stat & 0x01)) {
19856: if(sio[c].irq_enable & 0x08) {
19857: state_changed = true;
19858: }
19859: sio[c].modem_stat |= 0x01;
19860: }
19861: }
19862: LeaveCriticalSection(&sio_mt[c].csModemStat);
19863:
19864: if(state_changed) {
19865: sio_update_irq(c);
19866: }
1.1.1.25 root 19867: }
19868: }
19869: sio[c].modem_ctrl = data;
19870: break;
19871: case 7:
19872: sio[c].scratch = data;
19873: break;
19874: }
19875: }
19876:
19877: UINT8 sio_read(int c, UINT32 addr)
19878: {
19879: switch(addr & 7) {
19880: case 0:
19881: if(sio[c].selector & 0x80) {
19882: return(sio[c].divisor.b.l);
19883: } else {
19884: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19885: UINT8 data = 0;
19886: if(sio[c].recv_buffer != NULL) {
19887: data = sio[c].recv_buffer->read();
19888: }
1.1.1.25 root 19889: // data is not ready
19890: sio[c].line_stat_buf &= ~0x01;
19891: LeaveCriticalSection(&sio_mt[c].csRecvData);
19892:
19893: if(sio[c].irq_enable & 0x01) {
19894: sio_update_irq(c);
19895: }
19896: return(data);
19897: }
19898: case 1:
19899: if(sio[c].selector & 0x80) {
19900: return(sio[c].divisor.b.h);
19901: } else {
19902: return(sio[c].irq_enable);
19903: }
19904: case 2:
19905: return(sio[c].irq_identify);
19906: case 3:
19907: return(sio[c].selector);
19908: case 4:
19909: return(sio[c].modem_ctrl);
19910: case 5:
19911: {
19912: EnterCriticalSection(&sio_mt[c].csLineStat);
19913: UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
19914: sio[c].line_stat_err = 0x00;
19915: LeaveCriticalSection(&sio_mt[c].csLineStat);
19916:
19917: bool state_changed = false;
19918:
19919: if((sio[c].line_stat_buf & 0x60) == 0x00) {
19920: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19921: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 19922: // transmitter holding register will be empty first
19923: if(sio[c].irq_enable & 0x02) {
19924: state_changed = true;
19925: }
19926: sio[c].line_stat_buf |= 0x20;
19927: }
19928: LeaveCriticalSection(&sio_mt[c].csSendData);
19929: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
19930: // transmitter shift register will be empty later
19931: sio[c].line_stat_buf |= 0x40;
19932: }
19933: if(!(sio[c].line_stat_buf & 0x01)) {
19934: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19935: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 19936: // data is ready
19937: if(sio[c].irq_enable & 0x01) {
19938: state_changed = true;
19939: }
19940: sio[c].line_stat_buf |= 0x01;
19941: }
19942: LeaveCriticalSection(&sio_mt[c].csRecvData);
19943: }
19944: if(state_changed) {
19945: sio_update_irq(c);
19946: }
19947: return(val);
19948: }
19949: case 6:
19950: {
19951: EnterCriticalSection(&sio_mt[c].csModemStat);
19952: UINT8 val = sio[c].modem_stat;
19953: sio[c].modem_stat &= 0xf0;
19954: sio[c].prev_modem_stat = sio[c].modem_stat;
19955: LeaveCriticalSection(&sio_mt[c].csModemStat);
19956:
19957: if(sio[c].modem_ctrl & 0x10) {
19958: // loop-back
19959: val &= 0x0f;
19960: val |= (sio[c].modem_ctrl & 0x0c) << 4;
19961: val |= (sio[c].modem_ctrl & 0x01) << 5;
19962: val |= (sio[c].modem_ctrl & 0x02) << 3;
19963: }
19964: return(val);
19965: }
19966: case 7:
19967: return(sio[c].scratch);
19968: }
19969: return(0xff);
19970: }
19971:
19972: void sio_update(int c)
19973: {
19974: if((sio[c].line_stat_buf & 0x60) == 0x00) {
19975: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19976: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 19977: // transmitter holding/shift registers will be empty
19978: sio[c].line_stat_buf |= 0x60;
19979: }
19980: LeaveCriticalSection(&sio_mt[c].csSendData);
19981: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
19982: // transmitter shift register will be empty
19983: sio[c].line_stat_buf |= 0x40;
19984: }
19985: if(!(sio[c].line_stat_buf & 0x01)) {
19986: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19987: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 19988: // data is ready
19989: sio[c].line_stat_buf |= 0x01;
19990: }
19991: LeaveCriticalSection(&sio_mt[c].csRecvData);
19992: }
19993: sio_update_irq(c);
19994: }
19995:
19996: void sio_update_irq(int c)
19997: {
19998: int level = -1;
19999:
20000: if(sio[c].irq_enable & 0x08) {
20001: EnterCriticalSection(&sio_mt[c].csModemStat);
20002: if((sio[c].modem_stat & 0x0f) != 0) {
20003: level = 0;
20004: }
20005: EnterCriticalSection(&sio_mt[c].csModemStat);
20006: }
20007: if(sio[c].irq_enable & 0x02) {
20008: if(sio[c].line_stat_buf & 0x20) {
20009: level = 1;
20010: }
20011: }
20012: if(sio[c].irq_enable & 0x01) {
20013: if(sio[c].line_stat_buf & 0x01) {
20014: level = 2;
20015: }
20016: }
20017: if(sio[c].irq_enable & 0x04) {
20018: EnterCriticalSection(&sio_mt[c].csLineStat);
20019: if(sio[c].line_stat_err != 0) {
20020: level = 3;
20021: }
20022: LeaveCriticalSection(&sio_mt[c].csLineStat);
20023: }
1.1.1.29 root 20024:
20025: // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25 root 20026: if(level != -1) {
20027: sio[c].irq_identify = level << 1;
1.1.1.29 root 20028: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25 root 20029: } else {
20030: sio[c].irq_identify = 1;
1.1.1.29 root 20031: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25 root 20032: }
20033: }
20034:
20035: DWORD WINAPI sio_thread(void *lpx)
20036: {
20037: volatile sio_t *p = (sio_t *)lpx;
20038: sio_mt_t *q = &sio_mt[p->channel];
20039:
20040: char name[] = "COM1";
1.1.1.26 root 20041: name[3] = '0' + sio_port_number[p->channel];
20042: HANDLE hComm = NULL;
20043: COMMPROP commProp;
20044: DCB dcb;
20045: DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
20046: BYTE bytBuffer[SIO_BUFFER_SIZE];
20047:
1.1.1.60! root 20048: if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26 root 20049: if(GetCommProperties(hComm, &commProp)) {
20050: dwSettableBaud = commProp.dwSettableBaud;
20051: }
1.1.1.25 root 20052: EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26 root 20053: // EscapeCommFunction(hComm, SETRTS);
20054: // EscapeCommFunction(hComm, SETDTR);
1.1.1.25 root 20055:
1.1.1.54 root 20056: while(!m_exit) {
1.1.1.25 root 20057: // setup comm port
20058: bool comm_state_changed = false;
20059:
20060: EnterCriticalSection(&q->csLineCtrl);
20061: if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
20062: p->prev_divisor = p->divisor.w;
20063: p->prev_line_ctrl = p->line_ctrl;
20064: comm_state_changed = true;
20065: }
20066: LeaveCriticalSection(&q->csLineCtrl);
20067:
20068: if(comm_state_changed) {
1.1.1.26 root 20069: if(GetCommState(hComm, &dcb)) {
20070: // dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
20071: DWORD baud = 115200 / p->prev_divisor;
20072: dcb.BaudRate = 9600; // default
20073:
20074: if((dwSettableBaud & BAUD_075 ) && baud >= 75 ) dcb.BaudRate = 75;
20075: if((dwSettableBaud & BAUD_110 ) && baud >= 110 ) dcb.BaudRate = 110;
20076: // 134.5bps is not supported ???
20077: // if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
20078: if((dwSettableBaud & BAUD_150 ) && baud >= 150 ) dcb.BaudRate = 150;
20079: if((dwSettableBaud & BAUD_300 ) && baud >= 300 ) dcb.BaudRate = 300;
20080: if((dwSettableBaud & BAUD_600 ) && baud >= 600 ) dcb.BaudRate = 600;
20081: if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
20082: if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
20083: if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
20084: if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
20085: if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
20086: if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
20087: // if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
20088: // if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
20089: // if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
20090:
20091: switch(p->prev_line_ctrl & 0x03) {
20092: case 0x00: dcb.ByteSize = 5; break;
20093: case 0x01: dcb.ByteSize = 6; break;
20094: case 0x02: dcb.ByteSize = 7; break;
20095: case 0x03: dcb.ByteSize = 8; break;
20096: }
20097: switch(p->prev_line_ctrl & 0x04) {
20098: case 0x00: dcb.StopBits = ONESTOPBIT; break;
20099: case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
20100: }
20101: switch(p->prev_line_ctrl & 0x38) {
20102: case 0x08: dcb.Parity = ODDPARITY; break;
20103: case 0x18: dcb.Parity = EVENPARITY; break;
20104: case 0x28: dcb.Parity = MARKPARITY; break;
20105: case 0x38: dcb.Parity = SPACEPARITY; break;
20106: default: dcb.Parity = NOPARITY; break;
20107: }
20108: dcb.fBinary = TRUE;
20109: dcb.fParity = (dcb.Parity != NOPARITY);
20110: dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
20111: dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
20112: dcb.fDsrSensitivity = FALSE;//TRUE;
20113: dcb.fTXContinueOnXoff = TRUE;
20114: dcb.fOutX = dcb.fInX = FALSE;
20115: dcb.fErrorChar = FALSE;
20116: dcb.fNull = FALSE;
20117: dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
20118: dcb.fAbortOnError = FALSE;
20119:
20120: SetCommState(hComm, &dcb);
1.1.1.25 root 20121: }
20122:
20123: // check again to apply all comm state changes
20124: Sleep(10);
20125: continue;
20126: }
20127:
20128: // set comm pins
20129: bool change_brk = false;
1.1.1.26 root 20130: // bool change_rts = false;
20131: // bool change_dtr = false;
1.1.1.25 root 20132:
20133: EnterCriticalSection(&q->csModemCtrl);
20134: if(p->prev_set_brk != p->set_brk) {
20135: p->prev_set_brk = p->set_brk;
20136: change_brk = true;
20137: }
1.1.1.26 root 20138: // if(p->prev_set_rts != p->set_rts) {
20139: // p->prev_set_rts = p->set_rts;
20140: // change_rts = true;
20141: // }
20142: // if(p->prev_set_dtr != p->set_dtr) {
20143: // p->prev_set_dtr = p->set_dtr;
20144: // change_dtr = true;
20145: // }
1.1.1.25 root 20146: LeaveCriticalSection(&q->csModemCtrl);
20147:
20148: if(change_brk) {
1.1.1.26 root 20149: static UINT32 clear_time = 0;
20150: if(p->prev_set_brk) {
20151: EscapeCommFunction(hComm, SETBREAK);
20152: clear_time = timeGetTime() + 200;
20153: } else {
20154: // keep break for at least 200msec
20155: UINT32 cur_time = timeGetTime();
20156: if(clear_time > cur_time) {
20157: Sleep(clear_time - cur_time);
20158: }
20159: EscapeCommFunction(hComm, CLRBREAK);
20160: }
1.1.1.25 root 20161: }
1.1.1.26 root 20162: // if(change_rts) {
20163: // if(p->prev_set_rts) {
20164: // EscapeCommFunction(hComm, SETRTS);
20165: // } else {
20166: // EscapeCommFunction(hComm, CLRRTS);
20167: // }
20168: // }
20169: // if(change_dtr) {
20170: // if(p->prev_set_dtr) {
20171: // EscapeCommFunction(hComm, SETDTR);
20172: // } else {
20173: // EscapeCommFunction(hComm, CLRDTR);
20174: // }
20175: // }
1.1.1.25 root 20176:
20177: // get comm pins
20178: DWORD dwModemStat = 0;
20179:
20180: if(GetCommModemStatus(hComm, &dwModemStat)) {
20181: EnterCriticalSection(&q->csModemStat);
20182: if(dwModemStat & MS_RLSD_ON) {
20183: p->modem_stat |= 0x80;
20184: } else {
20185: p->modem_stat &= ~0x80;
20186: }
20187: if(dwModemStat & MS_RING_ON) {
20188: p->modem_stat |= 0x40;
20189: } else {
20190: p->modem_stat &= ~0x40;
20191: }
1.1.1.26 root 20192: // if(dwModemStat & MS_DSR_ON) {
20193: // p->modem_stat |= 0x20;
20194: // } else {
20195: // p->modem_stat &= ~0x20;
20196: // }
20197: // if(dwModemStat & MS_CTS_ON) {
20198: // p->modem_stat |= 0x10;
20199: // } else {
20200: // p->modem_stat &= ~0x10;
20201: // }
1.1.1.25 root 20202: if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
20203: p->modem_stat |= 0x08;
20204: }
20205: if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
20206: p->modem_stat |= 0x04;
20207: }
1.1.1.26 root 20208: // if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
20209: // p->modem_stat |= 0x02;
20210: // }
20211: // if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
20212: // p->modem_stat |= 0x01;
20213: // }
1.1.1.25 root 20214: LeaveCriticalSection(&q->csModemStat);
20215: }
20216:
20217: // send data
20218: DWORD dwSend = 0;
20219:
20220: EnterCriticalSection(&q->csSendData);
1.1.1.32 root 20221: while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25 root 20222: bytBuffer[dwSend++] = p->send_buffer->read();
20223: }
20224: LeaveCriticalSection(&q->csSendData);
20225:
20226: if(dwSend != 0) {
20227: DWORD dwWritten = 0;
20228: WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
20229: }
20230:
20231: // get line status and recv data
20232: DWORD dwLineStat = 0;
20233: COMSTAT comStat;
20234:
20235: if(ClearCommError(hComm, &dwLineStat, &comStat)) {
20236: EnterCriticalSection(&q->csLineStat);
20237: if(dwLineStat & CE_BREAK) {
20238: p->line_stat_err |= 0x10;
20239: }
20240: if(dwLineStat & CE_FRAME) {
20241: p->line_stat_err |= 0x08;
20242: }
20243: if(dwLineStat & CE_RXPARITY) {
20244: p->line_stat_err |= 0x04;
20245: }
20246: if(dwLineStat & CE_OVERRUN) {
20247: p->line_stat_err |= 0x02;
20248: }
20249: LeaveCriticalSection(&q->csLineStat);
20250:
20251: if(comStat.cbInQue != 0) {
20252: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 20253: DWORD dwRecv = 0;
20254: if(p->recv_buffer != NULL) {
20255: dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
20256: }
1.1.1.25 root 20257: LeaveCriticalSection(&q->csRecvData);
20258:
20259: if(dwRecv != 0) {
20260: DWORD dwRead = 0;
20261: if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
20262: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 20263: if(p->recv_buffer != NULL) {
20264: for(int i = 0; i < dwRead; i++) {
20265: p->recv_buffer->write(bytBuffer[i]);
20266: }
1.1.1.25 root 20267: }
20268: LeaveCriticalSection(&q->csRecvData);
20269: }
20270: }
20271: }
20272: }
20273: Sleep(10);
20274: }
20275: CloseHandle(hComm);
20276: }
20277: return 0;
20278: }
20279:
1.1.1.8 root 20280: // cmos
20281:
20282: void cmos_init()
20283: {
20284: memset(cmos, 0, sizeof(cmos));
20285: cmos_addr = 0;
1.1 root 20286:
1.1.1.8 root 20287: // from DOSBox
20288: cmos_write(0x0a, 0x26);
20289: cmos_write(0x0b, 0x02);
20290: cmos_write(0x0d, 0x80);
1.1 root 20291: }
20292:
1.1.1.8 root 20293: void cmos_write(int addr, UINT8 val)
1.1 root 20294: {
1.1.1.8 root 20295: cmos[addr & 0x7f] = val;
20296: }
20297:
20298: #define CMOS_GET_TIME() { \
20299: UINT32 cur_sec = timeGetTime() / 1000 ; \
20300: if(prev_sec != cur_sec) { \
20301: GetLocalTime(&time); \
20302: prev_sec = cur_sec; \
20303: } \
1.1 root 20304: }
1.1.1.8 root 20305: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1 root 20306:
1.1.1.8 root 20307: UINT8 cmos_read(int addr)
1.1 root 20308: {
1.1.1.8 root 20309: static SYSTEMTIME time;
20310: static UINT32 prev_sec = 0;
1.1 root 20311:
1.1.1.8 root 20312: switch(addr & 0x7f) {
20313: case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
20314: case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
20315: case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
20316: case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
20317: case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
20318: case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
20319: case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
20320: // case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0)); // 2msec
20321: case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0)); // precision of timeGetTime() may not be 1msec
20322: case 0x15: return((MEMORY_END >> 10) & 0xff);
20323: case 0x16: return((MEMORY_END >> 18) & 0xff);
20324: case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
20325: case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
20326: case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
20327: case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
20328: case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1 root 20329: }
1.1.1.8 root 20330: return(cmos[addr & 0x7f]);
1.1 root 20331: }
20332:
1.1.1.7 root 20333: // kbd (a20)
20334:
20335: void kbd_init()
20336: {
1.1.1.8 root 20337: kbd_data = kbd_command = 0;
1.1.1.7 root 20338: kbd_status = 0x18;
20339: }
20340:
20341: UINT8 kbd_read_data()
20342: {
1.1.1.57 root 20343: UINT8 data = kbd_data;
20344: kbd_data = 0;
1.1.1.8 root 20345: kbd_status &= ~1;
1.1.1.57 root 20346: return(data);
1.1.1.7 root 20347: }
20348:
20349: void kbd_write_data(UINT8 val)
20350: {
20351: switch(kbd_command) {
20352: case 0xd1:
20353: i386_set_a20_line((val >> 1) & 1);
20354: break;
20355: }
20356: kbd_command = 0;
1.1.1.8 root 20357: kbd_status &= ~8;
1.1.1.7 root 20358: }
20359:
20360: UINT8 kbd_read_status()
20361: {
20362: return(kbd_status);
20363: }
20364:
20365: void kbd_write_command(UINT8 val)
20366: {
20367: switch(val) {
20368: case 0xd0:
20369: kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8 root 20370: kbd_status |= 1;
1.1.1.7 root 20371: break;
20372: case 0xdd:
20373: i386_set_a20_line(0);
20374: break;
20375: case 0xdf:
20376: i386_set_a20_line(1);
20377: break;
1.1.1.26 root 20378: case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
20379: case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7 root 20380: if(!(val & 1)) {
1.1.1.8 root 20381: if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7 root 20382: // reset pic
20383: pic_init();
20384: pic[0].irr = pic[1].irr = 0x00;
20385: pic[0].imr = pic[1].imr = 0xff;
20386: }
20387: CPU_RESET_CALL(CPU_MODEL);
1.1.1.40 root 20388: UINT16 address = *(UINT16 *)(mem + 0x467);
20389: UINT16 selector = *(UINT16 *)(mem + 0x469);
20390: i386_jmp_far(selector, address);
1.1.1.7 root 20391: }
20392: i386_set_a20_line((val >> 1) & 1);
20393: break;
20394: }
20395: kbd_command = val;
1.1.1.8 root 20396: kbd_status |= 8;
1.1.1.7 root 20397: }
20398:
1.1.1.9 root 20399: // vga
20400:
20401: UINT8 vga_read_status()
20402: {
20403: // 60hz
20404: static const int period[3] = {16, 17, 17};
20405: static int index = 0;
20406: UINT32 time = timeGetTime() % period[index];
20407:
20408: index = (index + 1) % 3;
1.1.1.14 root 20409: return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9 root 20410: }
20411:
1.1 root 20412: // i/o bus
20413:
1.1.1.29 root 20414: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
20415: //#define SW1US_PATCH
20416:
1.1.1.25 root 20417: UINT8 read_io_byte(offs_t addr)
1.1.1.33 root 20418: #ifdef USE_DEBUGGER
1.1.1.25 root 20419: {
1.1.1.33 root 20420: if(now_debugging) {
20421: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
20422: if(in_break_point.table[i].status == 1) {
20423: if(addr == in_break_point.table[i].addr) {
20424: in_break_point.hit = i + 1;
20425: now_suspended = true;
20426: break;
20427: }
20428: }
20429: }
1.1.1.25 root 20430: }
1.1.1.33 root 20431: return(debugger_read_io_byte(addr));
1.1.1.25 root 20432: }
1.1.1.33 root 20433: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25 root 20434: #endif
1.1 root 20435: {
1.1.1.33 root 20436: UINT8 val = 0xff;
20437:
1.1 root 20438: switch(addr) {
1.1.1.29 root 20439: #ifdef SW1US_PATCH
20440: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
20441: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33 root 20442: val = sio_read(0, addr - 1);
20443: break;
1.1.1.29 root 20444: #else
1.1.1.25 root 20445: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
20446: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33 root 20447: val = dma_read(0, addr);
20448: break;
1.1.1.29 root 20449: #endif
1.1.1.25 root 20450: case 0x20: case 0x21:
1.1.1.33 root 20451: val = pic_read(0, addr);
20452: break;
1.1.1.25 root 20453: case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33 root 20454: val = pit_read(addr & 0x03);
20455: break;
1.1.1.7 root 20456: case 0x60:
1.1.1.33 root 20457: val = kbd_read_data();
20458: break;
1.1.1.9 root 20459: case 0x61:
1.1.1.33 root 20460: val = system_port;
20461: break;
1.1.1.7 root 20462: case 0x64:
1.1.1.33 root 20463: val = kbd_read_status();
20464: break;
1.1 root 20465: case 0x71:
1.1.1.33 root 20466: val = cmos_read(cmos_addr);
20467: break;
1.1.1.25 root 20468: case 0x81:
1.1.1.33 root 20469: val = dma_page_read(0, 2);
20470: break;
1.1.1.25 root 20471: case 0x82:
1.1.1.33 root 20472: val = dma_page_read(0, 3);
20473: break;
1.1.1.25 root 20474: case 0x83:
1.1.1.33 root 20475: val = dma_page_read(0, 1);
20476: break;
1.1.1.25 root 20477: case 0x87:
1.1.1.33 root 20478: val = dma_page_read(0, 0);
20479: break;
1.1.1.25 root 20480: case 0x89:
1.1.1.33 root 20481: val = dma_page_read(1, 2);
20482: break;
1.1.1.25 root 20483: case 0x8a:
1.1.1.33 root 20484: val = dma_page_read(1, 3);
20485: break;
1.1.1.25 root 20486: case 0x8b:
1.1.1.33 root 20487: val = dma_page_read(1, 1);
20488: break;
1.1.1.25 root 20489: case 0x8f:
1.1.1.33 root 20490: val = dma_page_read(1, 0);
20491: break;
1.1 root 20492: case 0x92:
1.1.1.33 root 20493: val = (m_a20_mask >> 19) & 2;
20494: break;
1.1.1.25 root 20495: case 0xa0: case 0xa1:
1.1.1.33 root 20496: val = pic_read(1, addr);
20497: break;
1.1.1.25 root 20498: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
20499: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33 root 20500: val = dma_read(1, (addr - 0xc0) >> 1);
20501: break;
1.1.1.37 root 20502: case 0x278: case 0x279: case 0x27a:
20503: val = pio_read(1, addr);
20504: break;
1.1.1.29 root 20505: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33 root 20506: val = sio_read(3, addr);
20507: break;
1.1.1.25 root 20508: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33 root 20509: val = sio_read(1, addr);
20510: break;
1.1.1.25 root 20511: case 0x378: case 0x379: case 0x37a:
1.1.1.33 root 20512: val = pio_read(0, addr);
20513: break;
1.1.1.25 root 20514: case 0x3ba: case 0x3da:
1.1.1.33 root 20515: val = vga_read_status();
20516: break;
1.1.1.37 root 20517: case 0x3bc: case 0x3bd: case 0x3be:
20518: val = pio_read(2, addr);
20519: break;
1.1.1.58 root 20520: case 0x3d5:
20521: if(crtc_addr < 16) {
20522: val = crtc_regs[crtc_addr];
20523: }
20524: break;
1.1.1.29 root 20525: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33 root 20526: val = sio_read(2, addr);
20527: break;
1.1.1.25 root 20528: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33 root 20529: val = sio_read(0, addr);
20530: break;
1.1 root 20531: default:
1.1.1.33 root 20532: // fatalerror("unknown inb %4x\n", addr);
1.1 root 20533: break;
20534: }
1.1.1.33 root 20535: #ifdef ENABLE_DEBUG_IOPORT
20536: if(fp_debug_log != NULL) {
20537: fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
20538: }
20539: #endif
20540: return(val);
1.1 root 20541: }
20542:
20543: UINT16 read_io_word(offs_t addr)
20544: {
20545: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
20546: }
20547:
1.1.1.33 root 20548: #ifdef USE_DEBUGGER
20549: UINT16 debugger_read_io_word(offs_t addr)
20550: {
20551: return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
20552: }
20553: #endif
20554:
1.1 root 20555: UINT32 read_io_dword(offs_t addr)
20556: {
20557: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
20558: }
20559:
1.1.1.33 root 20560: #ifdef USE_DEBUGGER
20561: UINT32 debugger_read_io_dword(offs_t addr)
20562: {
20563: return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8) | (debugger_read_io_byte(addr + 2) << 16) | (debugger_read_io_byte(addr + 3) << 24));
20564: }
20565: #endif
20566:
1.1 root 20567: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33 root 20568: #ifdef USE_DEBUGGER
20569: {
20570: if(now_debugging) {
20571: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
20572: if(out_break_point.table[i].status == 1) {
20573: if(addr == out_break_point.table[i].addr) {
20574: out_break_point.hit = i + 1;
20575: now_suspended = true;
20576: break;
20577: }
20578: }
20579: }
20580: }
20581: debugger_write_io_byte(addr, val);
20582: }
20583: void debugger_write_io_byte(offs_t addr, UINT8 val)
20584: #endif
1.1 root 20585: {
1.1.1.25 root 20586: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33 root 20587: if(fp_debug_log != NULL) {
1.1.1.43 root 20588: #ifdef USE_SERVICE_THREAD
20589: if(addr != 0xf7)
20590: #endif
1.1.1.33 root 20591: fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25 root 20592: }
20593: #endif
1.1 root 20594: switch(addr) {
1.1.1.29 root 20595: #ifdef SW1US_PATCH
20596: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
20597: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
20598: sio_write(0, addr - 1, val);
20599: break;
20600: #else
1.1.1.25 root 20601: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
20602: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
20603: dma_write(0, addr, val);
20604: break;
1.1.1.29 root 20605: #endif
1.1.1.25 root 20606: case 0x20: case 0x21:
1.1 root 20607: pic_write(0, addr, val);
20608: break;
1.1.1.25 root 20609: case 0x40: case 0x41: case 0x42: case 0x43:
1.1 root 20610: pit_write(addr & 0x03, val);
20611: break;
1.1.1.7 root 20612: case 0x60:
20613: kbd_write_data(val);
20614: break;
1.1.1.9 root 20615: case 0x61:
20616: if((system_port & 3) != 3 && (val & 3) == 3) {
20617: // beep on
20618: // MessageBeep(-1);
20619: } else if((system_port & 3) == 3 && (val & 3) != 3) {
20620: // beep off
20621: }
20622: system_port = val;
20623: break;
1.1 root 20624: case 0x64:
1.1.1.7 root 20625: kbd_write_command(val);
1.1 root 20626: break;
20627: case 0x70:
20628: cmos_addr = val;
20629: break;
20630: case 0x71:
1.1.1.8 root 20631: cmos_write(cmos_addr, val);
1.1 root 20632: break;
1.1.1.25 root 20633: case 0x81:
20634: dma_page_write(0, 2, val);
20635: case 0x82:
20636: dma_page_write(0, 3, val);
20637: case 0x83:
20638: dma_page_write(0, 1, val);
20639: case 0x87:
20640: dma_page_write(0, 0, val);
20641: case 0x89:
20642: dma_page_write(1, 2, val);
20643: case 0x8a:
20644: dma_page_write(1, 3, val);
20645: case 0x8b:
20646: dma_page_write(1, 1, val);
20647: case 0x8f:
20648: dma_page_write(1, 0, val);
1.1 root 20649: case 0x92:
1.1.1.7 root 20650: i386_set_a20_line((val >> 1) & 1);
1.1 root 20651: break;
1.1.1.25 root 20652: case 0xa0: case 0xa1:
1.1 root 20653: pic_write(1, addr, val);
20654: break;
1.1.1.25 root 20655: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
20656: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26 root 20657: dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25 root 20658: break;
1.1.1.35 root 20659: #ifdef USE_SERVICE_THREAD
20660: case 0xf7:
20661: // dummy i/o for BIOS/DOS service
1.1.1.36 root 20662: if(in_service && cursor_moved) {
20663: // update cursor position before service is done
20664: pcbios_update_cursor_position();
20665: cursor_moved = false;
20666: }
1.1.1.35 root 20667: finish_service_loop();
20668: break;
20669: #endif
1.1.1.37 root 20670: case 0x278: case 0x279: case 0x27a:
20671: pio_write(1, addr, val);
20672: break;
1.1.1.29 root 20673: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
20674: sio_write(3, addr, val);
20675: break;
1.1.1.25 root 20676: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
20677: sio_write(1, addr, val);
20678: break;
20679: case 0x378: case 0x379: case 0x37a:
20680: pio_write(0, addr, val);
20681: break;
1.1.1.37 root 20682: case 0x3bc: case 0x3bd: case 0x3be:
20683: pio_write(2, addr, val);
20684: break;
1.1.1.58 root 20685: case 0x3d4:
20686: crtc_addr = val;
20687: break;
20688: case 0x3d5:
20689: if(crtc_addr < 16) {
20690: if(crtc_regs[crtc_addr] != val) {
20691: crtc_regs[crtc_addr] = val;
20692: crtc_changed[crtc_addr] = 1;
20693: }
20694: }
20695: break;
1.1.1.29 root 20696: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
20697: sio_write(2, addr, val);
20698: break;
1.1.1.25 root 20699: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
20700: sio_write(0, addr, val);
20701: break;
1.1 root 20702: default:
1.1.1.33 root 20703: // fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1 root 20704: break;
20705: }
20706: }
20707:
20708: void write_io_word(offs_t addr, UINT16 val)
20709: {
20710: write_io_byte(addr + 0, (val >> 0) & 0xff);
20711: write_io_byte(addr + 1, (val >> 8) & 0xff);
20712: }
20713:
1.1.1.33 root 20714: #ifdef USE_DEBUGGER
20715: void debugger_write_io_word(offs_t addr, UINT16 val)
20716: {
20717: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
20718: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
20719: }
20720: #endif
20721:
1.1 root 20722: void write_io_dword(offs_t addr, UINT32 val)
20723: {
20724: write_io_byte(addr + 0, (val >> 0) & 0xff);
20725: write_io_byte(addr + 1, (val >> 8) & 0xff);
20726: write_io_byte(addr + 2, (val >> 16) & 0xff);
20727: write_io_byte(addr + 3, (val >> 24) & 0xff);
20728: }
1.1.1.33 root 20729:
20730: #ifdef USE_DEBUGGER
20731: void debugger_write_io_dword(offs_t addr, UINT32 val)
20732: {
20733: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
20734: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
20735: debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
20736: debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
20737: }
20738: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.