|
|
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.32 root 84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22 root 85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1 root 88:
1.1.1.12 root 89: #if defined(__MINGW32__)
90: extern "C" int _CRT_glob = 0;
91: #endif
92:
93: /*
94: kludge for "more-standardized" C++
95: */
96: #if !defined(_MSC_VER)
97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
99: #define min(a,b) kludge_min(a,b)
100: #define max(a,b) kludge_max(a,b)
1.1.1.14 root 101: #elif _MSC_VER >= 1400
102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
103: {
104: }
105: #endif
106:
1.1.1.35 root 107: #define USE_VRAM_THREAD
1.1.1.14 root 108:
1.1.1.35 root 109: #ifdef USE_VRAM_THREAD
1.1.1.14 root 110: static CRITICAL_SECTION vram_crit_sect;
111: #else
112: #define vram_flush()
1.1.1.12 root 113: #endif
114:
1.1.1.14 root 115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
117:
118: void change_console_size(int width, int height);
119: void clear_scr_buffer(WORD attr);
120:
121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
123: static COORD vram_coord_char, vram_coord_attr;
124:
1.1.1.28 root 125: char temp_file_path[MAX_PATH];
126: bool temp_file_created = false;
127:
1.1.1.14 root 128: bool ignore_illegal_insn = false;
129: bool limit_max_memory = false;
130: bool no_windows = false;
131: bool stay_busy = false;
1.1.1.19 root 132: bool support_ems = false;
133: #ifdef SUPPORT_XMS
134: bool support_xms = false;
135: #endif
1.1.1.29 root 136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14 root 137:
138: BOOL is_vista_or_later;
139:
1.1.1.35 root 140: #define UPDATE_OPS 16384
141: #define REQUEST_HARDWRE_UPDATE() { \
142: update_ops = UPDATE_OPS - 1; \
143: }
144: UINT32 update_ops = 0;
145: UINT32 idle_ops = 0;
146:
1.1.1.14 root 147: inline void maybe_idle()
148: {
149: // if it appears to be in a tight loop, assume waiting for input
150: // allow for one updated video character, for a spinning cursor
1.1.1.35 root 151: if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.14 root 152: Sleep(10);
1.1.1.35 root 153: REQUEST_HARDWRE_UPDATE();
1.1.1.14 root 154: }
1.1.1.35 root 155: idle_ops = 0;
1.1.1.14 root 156: }
1.1.1.12 root 157:
1.1 root 158: /* ----------------------------------------------------------------------------
1.1.1.3 root 159: MAME i86/i386
1.1 root 160: ---------------------------------------------------------------------------- */
161:
1.1.1.10 root 162: #ifndef __BIG_ENDIAN__
1.1 root 163: #define LSB_FIRST
1.1.1.10 root 164: #endif
1.1 root 165:
166: #ifndef INLINE
167: #define INLINE inline
168: #endif
169: #define U64(v) UINT64(v)
170:
171: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
172: #define logerror(...)
173: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
174: #define popmessage(...)
175:
176: /*****************************************************************************/
1.1.1.10 root 177: /* src/emu/devcpu.h */
178:
179: // CPU interface functions
180: #define CPU_INIT_NAME(name) cpu_init_##name
181: #define CPU_INIT(name) void CPU_INIT_NAME(name)()
182: #define CPU_INIT_CALL(name) CPU_INIT_NAME(name)()
183:
184: #define CPU_RESET_NAME(name) cpu_reset_##name
185: #define CPU_RESET(name) void CPU_RESET_NAME(name)()
186: #define CPU_RESET_CALL(name) CPU_RESET_NAME(name)()
187:
188: #define CPU_EXECUTE_NAME(name) cpu_execute_##name
189: #define CPU_EXECUTE(name) void CPU_EXECUTE_NAME(name)()
190: #define CPU_EXECUTE_CALL(name) CPU_EXECUTE_NAME(name)()
191:
192: #define CPU_TRANSLATE_NAME(name) cpu_translate_##name
193: #define CPU_TRANSLATE(name) int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
194: #define CPU_TRANSLATE_CALL(name) CPU_TRANSLATE_NAME(name)(space, intention, address)
195:
196: #define CPU_DISASSEMBLE_NAME(name) cpu_disassemble_##name
197: #define CPU_DISASSEMBLE(name) int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
198: #define CPU_DISASSEMBLE_CALL(name) CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
199:
1.1.1.14 root 200: #define CPU_MODEL_STR(name) #name
201: #define CPU_MODEL_NAME(name) CPU_MODEL_STR(name)
202:
1.1.1.10 root 203: /*****************************************************************************/
204: /* src/emu/didisasm.h */
205:
206: // Disassembler constants
207: const UINT32 DASMFLAG_SUPPORTED = 0x80000000; // are disassembly flags supported?
208: const UINT32 DASMFLAG_STEP_OUT = 0x40000000; // this instruction should be the end of a step out sequence
209: const UINT32 DASMFLAG_STEP_OVER = 0x20000000; // this instruction should be stepped over by setting a breakpoint afterwards
210: const UINT32 DASMFLAG_OVERINSTMASK = 0x18000000; // number of extra instructions to skip when stepping over
211: const UINT32 DASMFLAG_OVERINSTSHIFT = 27; // bits to shift after masking to get the value
212: const UINT32 DASMFLAG_LENGTHMASK = 0x0000ffff; // the low 16-bits contain the actual length
213:
214: /*****************************************************************************/
1.1 root 215: /* src/emu/diexec.h */
216:
217: // I/O line states
218: enum line_state
219: {
220: CLEAR_LINE = 0, // clear (a fired or held) line
221: ASSERT_LINE, // assert an interrupt immediately
222: HOLD_LINE, // hold interrupt line until acknowledged
223: PULSE_LINE // pulse interrupt line instantaneously (only for NMI, RESET)
224: };
225:
226: // I/O line definitions
227: enum
228: {
229: INPUT_LINE_IRQ = 0,
230: INPUT_LINE_NMI
231: };
232:
233: /*****************************************************************************/
1.1.1.10 root 234: /* src/emu/dimemory.h */
1.1 root 235:
1.1.1.10 root 236: // Translation intentions
237: const int TRANSLATE_TYPE_MASK = 0x03; // read write or fetch
238: const int TRANSLATE_USER_MASK = 0x04; // user mode or fully privileged
239: const int TRANSLATE_DEBUG_MASK = 0x08; // debug mode (no side effects)
240:
241: const int TRANSLATE_READ = 0; // translate for read
242: const int TRANSLATE_WRITE = 1; // translate for write
243: const int TRANSLATE_FETCH = 2; // translate for instruction fetch
244: const int TRANSLATE_READ_USER = (TRANSLATE_READ | TRANSLATE_USER_MASK);
245: const int TRANSLATE_WRITE_USER = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
246: const int TRANSLATE_FETCH_USER = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
247: const int TRANSLATE_READ_DEBUG = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
248: const int TRANSLATE_WRITE_DEBUG = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
249: const int TRANSLATE_FETCH_DEBUG = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1 root 250:
1.1.1.10 root 251: /*****************************************************************************/
252: /* src/emu/emucore.h */
1.1 root 253:
1.1.1.10 root 254: // constants for expression endianness
255: enum endianness_t
256: {
257: ENDIANNESS_LITTLE,
258: ENDIANNESS_BIG
259: };
1.1 root 260:
1.1.1.10 root 261: // declare native endianness to be one or the other
262: #ifdef LSB_FIRST
263: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
264: #else
265: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
266: #endif
267:
268: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
269: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
270:
271: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
272: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
273:
274: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
275: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval) (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1 root 276:
277: /*****************************************************************************/
278: /* src/emu/memory.h */
279:
1.1.1.10 root 280: // address spaces
281: enum address_spacenum
282: {
283: AS_0, // first address space
284: AS_1, // second address space
285: AS_2, // third address space
286: AS_3, // fourth address space
287: ADDRESS_SPACES, // maximum number of address spaces
288:
289: // alternate address space names for common use
290: AS_PROGRAM = AS_0, // program address space
291: AS_DATA = AS_1, // data address space
292: AS_IO = AS_2 // I/O address space
293: };
294:
1.1 root 295: // offsets and addresses are 32-bit (for now...)
1.1.1.30 root 296: //typedef UINT32 offs_t;
1.1 root 297:
298: // read accessors
299: UINT8 read_byte(offs_t byteaddress)
1.1.1.33 root 300: #ifdef USE_DEBUGGER
301: {
302: if(now_debugging) {
303: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
304: if(rd_break_point.table[i].status == 1) {
305: if(byteaddress == rd_break_point.table[i].addr) {
306: rd_break_point.hit = i + 1;
307: now_suspended = true;
308: break;
309: }
310: }
311: }
312: }
313: return(debugger_read_byte(byteaddress));
314: }
315: UINT8 debugger_read_byte(offs_t byteaddress)
316: #endif
1.1 root 317: {
1.1.1.4 root 318: #if defined(HAS_I386)
1.1 root 319: if(byteaddress < MAX_MEM) {
320: return mem[byteaddress];
1.1.1.3 root 321: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
322: // return read_byte(byteaddress & 0xfffff);
1.1 root 323: }
324: return 0;
1.1.1.4 root 325: #else
326: return mem[byteaddress];
327: #endif
1.1 root 328: }
329:
330: UINT16 read_word(offs_t byteaddress)
1.1.1.33 root 331: #ifdef USE_DEBUGGER
332: {
333: if(now_debugging) {
334: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
335: if(rd_break_point.table[i].status == 1) {
336: if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
337: rd_break_point.hit = i + 1;
338: now_suspended = true;
339: break;
340: }
341: }
342: }
343: }
344: return(debugger_read_word(byteaddress));
345: }
346: UINT16 debugger_read_word(offs_t byteaddress)
347: #endif
1.1 root 348: {
1.1.1.14 root 349: if(byteaddress == 0x41c) {
350: // pointer to first free slot in keyboard buffer
1.1.1.35 root 351: if(key_buf_char != NULL && key_buf_scan != NULL) {
352: #ifdef USE_SERVICE_THREAD
353: EnterCriticalSection(&key_buf_crit_sect);
354: #endif
1.1.1.51! root 355: bool empty = key_buf_char->empty();
1.1.1.35 root 356: #ifdef USE_SERVICE_THREAD
357: LeaveCriticalSection(&key_buf_crit_sect);
358: #endif
1.1.1.51! root 359: if(empty) maybe_idle();
1.1.1.14 root 360: }
361: }
1.1.1.4 root 362: #if defined(HAS_I386)
1.1 root 363: if(byteaddress < MAX_MEM - 1) {
364: return *(UINT16 *)(mem + byteaddress);
1.1.1.3 root 365: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
366: // return read_word(byteaddress & 0xfffff);
1.1 root 367: }
368: return 0;
1.1.1.4 root 369: #else
370: return *(UINT16 *)(mem + byteaddress);
371: #endif
1.1 root 372: }
373:
374: UINT32 read_dword(offs_t byteaddress)
1.1.1.33 root 375: #ifdef USE_DEBUGGER
376: {
377: if(now_debugging) {
378: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
379: if(rd_break_point.table[i].status == 1) {
380: if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
381: rd_break_point.hit = i + 1;
382: now_suspended = true;
383: break;
384: }
385: }
386: }
387: }
388: return(debugger_read_dword(byteaddress));
389: }
390: UINT32 debugger_read_dword(offs_t byteaddress)
391: #endif
1.1 root 392: {
1.1.1.4 root 393: #if defined(HAS_I386)
1.1 root 394: if(byteaddress < MAX_MEM - 3) {
395: return *(UINT32 *)(mem + byteaddress);
1.1.1.3 root 396: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
397: // return read_dword(byteaddress & 0xfffff);
1.1 root 398: }
399: return 0;
1.1.1.4 root 400: #else
401: return *(UINT32 *)(mem + byteaddress);
402: #endif
1.1 root 403: }
404:
405: // write accessors
1.1.1.35 root 406: #ifdef USE_VRAM_THREAD
1.1.1.14 root 407: void vram_flush_char()
408: {
409: if(vram_length_char != 0) {
410: DWORD num;
1.1.1.23 root 411: WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14 root 412: vram_length_char = vram_last_length_char = 0;
413: }
414: }
415:
416: void vram_flush_attr()
417: {
418: if(vram_length_attr != 0) {
419: DWORD num;
1.1.1.23 root 420: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14 root 421: vram_length_attr = vram_last_length_attr = 0;
422: }
423: }
424:
425: void vram_flush()
426: {
427: if(vram_length_char != 0 || vram_length_attr != 0) {
428: EnterCriticalSection(&vram_crit_sect);
429: vram_flush_char();
430: vram_flush_attr();
431: LeaveCriticalSection(&vram_crit_sect);
432: }
433: }
434: #endif
435:
436: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8 root 437: {
1.1.1.35 root 438: #ifdef USE_VRAM_THREAD
1.1.1.14 root 439: static offs_t first_offset_char, last_offset_char;
440:
441: if(vram_length_char != 0) {
442: if(offset <= last_offset_char && offset >= first_offset_char) {
443: scr_char[(offset - first_offset_char) >> 1] = data;
444: return;
445: }
446: if(offset != last_offset_char + 2) {
447: vram_flush_char();
448: }
449: }
450: if(vram_length_char == 0) {
451: first_offset_char = offset;
452: vram_coord_char.X = (offset >> 1) % scr_width;
453: vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
454: }
455: scr_char[vram_length_char++] = data;
456: last_offset_char = offset;
457: #else
1.1.1.8 root 458: COORD co;
459: DWORD num;
460:
1.1.1.14 root 461: co.X = (offset >> 1) % scr_width;
462: co.Y = (offset >> 1) / scr_width;
463: scr_char[0] = data;
1.1.1.23 root 464: WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14 root 465: #endif
466: }
467:
468: void write_text_vram_attr(offs_t offset, UINT8 data)
469: {
1.1.1.35 root 470: #ifdef USE_VRAM_THREAD
1.1.1.14 root 471: static offs_t first_offset_attr, last_offset_attr;
472:
473: if(vram_length_attr != 0) {
474: if(offset <= last_offset_attr && offset >= first_offset_attr) {
475: scr_attr[(offset - first_offset_attr) >> 1] = data;
476: return;
477: }
478: if(offset != last_offset_attr + 2) {
479: vram_flush_attr();
480: }
481: }
482: if(vram_length_attr == 0) {
483: first_offset_attr = offset;
484: vram_coord_attr.X = (offset >> 1) % scr_width;
485: vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
486: }
487: scr_attr[vram_length_attr++] = data;
488: last_offset_attr = offset;
489: #else
490: COORD co;
491: DWORD num;
1.1.1.8 root 492:
1.1.1.14 root 493: co.X = (offset >> 1) % scr_width;
494: co.Y = (offset >> 1) / scr_width;
495: scr_attr[0] = data;
1.1.1.23 root 496: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14 root 497: #endif
498: }
499:
500: void write_text_vram_byte(offs_t offset, UINT8 data)
501: {
1.1.1.35 root 502: #ifdef USE_VRAM_THREAD
1.1.1.14 root 503: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 504: #endif
1.1.1.8 root 505: if(offset & 1) {
1.1.1.14 root 506: write_text_vram_attr(offset, data);
1.1.1.8 root 507: } else {
1.1.1.14 root 508: write_text_vram_char(offset, data);
1.1.1.8 root 509: }
1.1.1.35 root 510: #ifdef USE_VRAM_THREAD
1.1.1.14 root 511: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 512: #endif
1.1.1.8 root 513: }
514:
515: void write_text_vram_word(offs_t offset, UINT16 data)
516: {
1.1.1.35 root 517: #ifdef USE_VRAM_THREAD
1.1.1.14 root 518: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 519: #endif
1.1.1.8 root 520: if(offset & 1) {
1.1.1.14 root 521: write_text_vram_attr(offset , (data ) & 0xff);
522: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 523: } else {
1.1.1.14 root 524: write_text_vram_char(offset , (data ) & 0xff);
525: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 526: }
1.1.1.35 root 527: #ifdef USE_VRAM_THREAD
1.1.1.14 root 528: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 529: #endif
1.1.1.8 root 530: }
531:
532: void write_text_vram_dword(offs_t offset, UINT32 data)
533: {
1.1.1.35 root 534: #ifdef USE_VRAM_THREAD
1.1.1.14 root 535: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 536: #endif
1.1.1.8 root 537: if(offset & 1) {
1.1.1.14 root 538: write_text_vram_attr(offset , (data ) & 0xff);
539: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
540: write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
541: write_text_vram_char(offset + 3, (data >> 24) & 0xff);
542: } else {
543: write_text_vram_char(offset , (data ) & 0xff);
544: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
545: write_text_vram_char(offset + 2, (data >> 16) & 0xff);
546: write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8 root 547: }
1.1.1.35 root 548: #ifdef USE_VRAM_THREAD
1.1.1.14 root 549: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 550: #endif
1.1.1.8 root 551: }
552:
1.1 root 553: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33 root 554: #ifdef USE_DEBUGGER
555: {
556: if(now_debugging) {
557: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
558: if(wr_break_point.table[i].status == 1) {
559: if(byteaddress == wr_break_point.table[i].addr) {
560: wr_break_point.hit = i + 1;
561: now_suspended = true;
562: break;
563: }
564: }
565: }
566: }
567: debugger_write_byte(byteaddress, data);
568: }
569: void debugger_write_byte(offs_t byteaddress, UINT8 data)
570: #endif
1.1 root 571: {
1.1.1.8 root 572: if(byteaddress < MEMORY_END) {
1.1.1.3 root 573: mem[byteaddress] = data;
1.1.1.8 root 574: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 575: if(!restore_console_on_exit) {
576: change_console_size(scr_width, scr_height);
1.1.1.12 root 577: }
1.1.1.8 root 578: write_text_vram_byte(byteaddress - text_vram_top_address, data);
579: mem[byteaddress] = data;
580: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
581: if(int_10h_feh_called && !int_10h_ffh_called) {
582: write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1 root 583: }
584: mem[byteaddress] = data;
1.1.1.4 root 585: #if defined(HAS_I386)
1.1.1.3 root 586: } else if(byteaddress < MAX_MEM) {
1.1.1.4 root 587: #else
588: } else {
589: #endif
1.1.1.3 root 590: mem[byteaddress] = data;
1.1 root 591: }
592: }
593:
594: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33 root 595: #ifdef USE_DEBUGGER
596: {
597: if(now_debugging) {
598: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
599: if(wr_break_point.table[i].status == 1) {
600: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
601: wr_break_point.hit = i + 1;
602: now_suspended = true;
603: break;
604: }
605: }
606: }
607: }
608: debugger_write_word(byteaddress, data);
609: }
610: void debugger_write_word(offs_t byteaddress, UINT16 data)
611: #endif
1.1 root 612: {
1.1.1.8 root 613: if(byteaddress < MEMORY_END) {
1.1.1.51! root 614: if(byteaddress == cursor_position_address) {
! 615: if(*(UINT16 *)(mem + byteaddress) != data) {
! 616: COORD co;
! 617: co.X = data & 0xff;
! 618: co.Y = (data >> 8) + scr_top;
! 619: SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
! 620: }
1.1.1.14 root 621: }
1.1.1.3 root 622: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8 root 623: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 624: if(!restore_console_on_exit) {
625: change_console_size(scr_width, scr_height);
1.1.1.12 root 626: }
1.1.1.8 root 627: write_text_vram_word(byteaddress - text_vram_top_address, data);
628: *(UINT16 *)(mem + byteaddress) = data;
629: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
630: if(int_10h_feh_called && !int_10h_ffh_called) {
631: write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1 root 632: }
633: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4 root 634: #if defined(HAS_I386)
1.1.1.3 root 635: } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4 root 636: #else
637: } else {
638: #endif
1.1.1.3 root 639: *(UINT16 *)(mem + byteaddress) = data;
1.1 root 640: }
641: }
642:
643: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33 root 644: #ifdef USE_DEBUGGER
645: {
646: if(now_debugging) {
647: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
648: if(wr_break_point.table[i].status == 1) {
649: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
650: wr_break_point.hit = i + 1;
651: now_suspended = true;
652: break;
653: }
654: }
655: }
656: }
657: debugger_write_dword(byteaddress, data);
658: }
659: void debugger_write_dword(offs_t byteaddress, UINT32 data)
660: #endif
1.1 root 661: {
1.1.1.8 root 662: if(byteaddress < MEMORY_END) {
1.1.1.3 root 663: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8 root 664: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 665: if(!restore_console_on_exit) {
666: change_console_size(scr_width, scr_height);
1.1.1.12 root 667: }
1.1.1.8 root 668: write_text_vram_dword(byteaddress - text_vram_top_address, data);
669: *(UINT32 *)(mem + byteaddress) = data;
670: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
671: if(int_10h_feh_called && !int_10h_ffh_called) {
672: write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1 root 673: }
674: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4 root 675: #if defined(HAS_I386)
1.1.1.3 root 676: } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4 root 677: #else
678: } else {
679: #endif
1.1.1.3 root 680: *(UINT32 *)(mem + byteaddress) = data;
1.1 root 681: }
682: }
683:
684: #define read_decrypted_byte read_byte
685: #define read_decrypted_word read_word
686: #define read_decrypted_dword read_dword
687:
1.1.1.3 root 688: #define read_raw_byte read_byte
689: #define write_raw_byte write_byte
690:
691: #define read_word_unaligned read_word
692: #define write_word_unaligned write_word
693:
694: #define read_io_word_unaligned read_io_word
695: #define write_io_word_unaligned write_io_word
696:
1.1 root 697: UINT8 read_io_byte(offs_t byteaddress);
698: UINT16 read_io_word(offs_t byteaddress);
699: UINT32 read_io_dword(offs_t byteaddress);
700:
701: void write_io_byte(offs_t byteaddress, UINT8 data);
702: void write_io_word(offs_t byteaddress, UINT16 data);
703: void write_io_dword(offs_t byteaddress, UINT32 data);
704:
705: /*****************************************************************************/
706: /* src/osd/osdcomm.h */
707:
708: /* Highly useful macro for compile-time knowledge of an array size */
709: #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
710:
1.1.1.3 root 711: #if defined(HAS_I386)
1.1.1.10 root 712: static CPU_TRANSLATE(i386);
713: #include "mame/lib/softfloat/softfloat.c"
714: #include "mame/emu/cpu/i386/i386.c"
1.1.1.12 root 715: #include "mame/emu/cpu/vtlb.c"
1.1.1.3 root 716: #elif defined(HAS_I286)
1.1.1.10 root 717: #include "mame/emu/cpu/i86/i286.c"
1.1.1.3 root 718: #else
1.1.1.10 root 719: #include "mame/emu/cpu/i86/i86.c"
1.1.1.3 root 720: #endif
1.1.1.33 root 721: #ifdef USE_DEBUGGER
1.1.1.10 root 722: #include "mame/emu/cpu/i386/i386dasm.c"
1.1 root 723: #endif
724:
1.1.1.3 root 725: #if defined(HAS_I386)
726: #define SREG(x) m_sreg[x].selector
727: #define SREG_BASE(x) m_sreg[x].base
728: int cpu_type, cpu_step;
729: #else
730: #define REG8(x) m_regs.b[x]
731: #define REG16(x) m_regs.w[x]
732: #define SREG(x) m_sregs[x]
733: #define SREG_BASE(x) m_base[x]
1.1.1.33 root 734: #define m_eip (m_pc - m_base[CS])
1.1.1.3 root 735: #define m_CF m_CarryVal
736: #define m_a20_mask AMASK
737: #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
738: #if defined(HAS_I286)
739: #define i386_set_a20_line(x) i80286_set_a20_line(x)
740: #else
741: #define i386_set_a20_line(x)
742: #endif
743: #define i386_set_irq_line(x, y) set_irq_line(x, y)
744: #endif
1.1 root 745:
746: void i386_jmp_far(UINT16 selector, UINT32 address)
747: {
1.1.1.3 root 748: #if defined(HAS_I386)
1.1 root 749: if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3 root 750: i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1 root 751: } else {
1.1.1.3 root 752: SREG(CS) = selector;
753: m_performed_intersegment_jump = 1;
754: i386_load_segment_descriptor(CS);
755: m_eip = address;
756: CHANGE_PC(m_eip);
1.1 root 757: }
1.1.1.3 root 758: #elif defined(HAS_I286)
759: i80286_code_descriptor(selector, address, 1);
760: #else
761: SREG(CS) = selector;
762: i386_load_segment_descriptor(CS);
763: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
764: #endif
1.1 root 765: }
766:
1.1.1.24 root 767: void i386_call_far(UINT16 selector, UINT32 address)
768: {
769: #if defined(HAS_I386)
770: if(PROTECTED_MODE && !V8086_MODE) {
771: i386_protected_mode_call(selector, address, 1, m_operand_size);
772: } else {
773: PUSH16(SREG(CS));
774: PUSH16(m_eip);
775: SREG(CS) = selector;
776: m_performed_intersegment_jump = 1;
777: i386_load_segment_descriptor(CS);
778: m_eip = address;
779: CHANGE_PC(m_eip);
780: }
781: #else
782: UINT16 ip = m_pc - SREG_BASE(CS);
783: UINT16 cs = SREG(CS);
784: #if defined(HAS_I286)
785: i80286_code_descriptor(selector, address, 2);
786: #else
787: SREG(CS) = selector;
788: i386_load_segment_descriptor(CS);
789: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
790: #endif
791: PUSH(cs);
792: PUSH(ip);
793: CHANGE_PC(m_pc);
794: #endif
795: }
1.1.1.49 root 796:
797: void i386_push16(UINT16 value)
798: {
799: #if defined(HAS_I386)
800: PUSH16(value);
801: #else
802: PUSH(value);
1.1.1.35 root 803: #endif
1.1.1.49 root 804: }
805:
806: UINT16 i386_pop16()
807: {
808: #if defined(HAS_I386)
809: return POP16();
810: #else
811: UINT16 value;
812: POP(value);
813: return value;
814: #endif
815: }
1.1.1.24 root 816:
1.1.1.29 root 817: UINT16 i386_read_stack()
818: {
819: #if defined(HAS_I386)
820: UINT32 ea, new_esp;
821: if( STACK_32BIT ) {
822: new_esp = REG32(ESP) + 2;
823: ea = i386_translate(SS, new_esp - 2, 0);
824: } else {
825: new_esp = REG16(SP) + 2;
826: ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
827: }
828: return READ16(ea);
829: #else
830: UINT16 sp = m_regs.w[SP] + 2;
831: return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
832: #endif
833: }
834:
1.1 root 835: /* ----------------------------------------------------------------------------
1.1.1.33 root 836: debugger
837: ---------------------------------------------------------------------------- */
838:
839: #ifdef USE_DEBUGGER
840: #define TELNET_BLUE 0x0004 // text color contains blue.
841: #define TELNET_GREEN 0x0002 // text color contains green.
842: #define TELNET_RED 0x0001 // text color contains red.
843: #define TELNET_INTENSITY 0x0008 // text color is intensified.
844:
845: int svr_socket = 0;
846: int cli_socket = 0;
847:
848: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
849:
850: void debugger_init()
851: {
852: now_debugging = false;
853: now_going = false;
854: now_suspended = false;
855: force_suspend = false;
856:
857: memset(&break_point, 0, sizeof(break_point_t));
858: memset(&rd_break_point, 0, sizeof(break_point_t));
859: memset(&wr_break_point, 0, sizeof(break_point_t));
860: memset(&in_break_point, 0, sizeof(break_point_t));
861: memset(&out_break_point, 0, sizeof(break_point_t));
862: memset(&int_break_point, 0, sizeof(int_break_point_t));
863: }
864:
1.1.1.45 root 865: void telnet_send(const char *string)
1.1.1.33 root 866: {
867: char buffer[8192], *ptr;
868: strcpy(buffer, string);
869: while((ptr = strstr(buffer, "\n")) != NULL) {
870: char tmp[8192];
871: *ptr = '\0';
872: sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
873: strcpy(buffer, tmp);
874: }
875:
876: int len = strlen(buffer), res;
877: ptr = buffer;
878: while(len > 0) {
879: if((res = send(cli_socket, ptr, len, 0)) > 0) {
880: len -= res;
881: ptr += res;
882: }
883: }
884: }
885:
886: void telnet_command(const char *format, ...)
887: {
888: char buffer[1024];
889: va_list ap;
890: va_start(ap, format);
891: vsprintf(buffer, format, ap);
892: va_end(ap);
893:
894: telnet_send(buffer);
895: }
896:
897: void telnet_printf(const char *format, ...)
898: {
899: char buffer[1024];
900: va_list ap;
901: va_start(ap, format);
902: vsprintf(buffer, format, ap);
903: va_end(ap);
904:
905: if(fp_debugger != NULL) {
906: fprintf(fp_debugger, "%s", buffer);
907: }
908: telnet_send(buffer);
909: }
910:
911: bool telnet_gets(char *str, int n)
912: {
913: char buffer[1024];
914: int ptr = 0;
915:
916: telnet_command("\033[12l"); // local echo on
917: telnet_command("\033[2l"); // key unlock
918:
919: while(!m_halted) {
920: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
921:
922: if(len > 0 && buffer[0] != 0xff) {
923: for(int i = 0; i < len; i++) {
924: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
925: str[ptr] = 0;
926: telnet_command("\033[2h"); // key lock
927: telnet_command("\033[12h"); // local echo off
928: return(!m_halted);
929: } else if(buffer[i] == 0x08) {
930: if(ptr > 0) {
931: telnet_command("\033[0K"); // erase from cursor position
932: ptr--;
933: } else {
934: telnet_command("\033[1C"); // move cursor forward
935: }
936: } else if(ptr < n - 1) {
1.1.1.37 root 937: if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33 root 938: str[ptr++] = buffer[i];
939: }
940: } else {
941: telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
942: }
943: }
944: } else if(len == -1) {
945: if(WSAGetLastError() != WSAEWOULDBLOCK) {
946: return(false);
947: }
948: } else if(len == 0) {
949: return(false);
950: }
951: Sleep(10);
952: }
953: return(!m_halted);
954: }
955:
956: bool telnet_kbhit()
957: {
958: char buffer[1024];
959:
960: if(!m_halted) {
961: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
962:
963: if(len > 0) {
964: for(int i = 0; i < len; i++) {
965: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
966: return(true);
967: }
968: }
969: } else if(len == 0) {
970: return(true); // disconnected
971: }
972: }
973: return(false);
974: }
975:
976: bool telnet_disconnected()
977: {
978: char buffer[1024];
979: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
980:
981: if(len == 0) {
982: return(true);
983: } else if(len == -1) {
984: if(WSAGetLastError() != WSAEWOULDBLOCK) {
985: return(true);
986: }
987: }
988: return(false);
989: }
990:
991: void telnet_set_color(int color)
992: {
993: telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
994: }
995:
996: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
997: {
998: // UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
999: UINT8 ops[16];
1000: for(int i = 0; i < 16; i++) {
1001: ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
1002: }
1003: UINT8 *oprom = ops;
1004:
1005: #if defined(HAS_I386)
1006: if(m_operand_size) {
1007: return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
1008: } else
1009: #endif
1010: return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
1011: }
1012:
1013: void debugger_regs_info(char *buffer)
1014: {
1015: #if defined(HAS_I386)
1016: UINT32 flags = get_flags();
1017: #else
1018: UINT32 flags = CompressFlags();
1019: #endif
1020: #if defined(HAS_I386)
1021: if(m_operand_size) {
1022: 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",
1023: 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),
1024: PROTECTED_MODE ? "PE" : "--",
1025: (flags & 0x40000) ? 'A' : '-',
1026: (flags & 0x20000) ? 'V' : '-',
1027: (flags & 0x10000) ? 'R' : '-',
1028: (flags & 0x04000) ? 'N' : '-',
1029: (flags & 0x02000) ? '1' : '0',
1030: (flags & 0x01000) ? '1' : '0',
1031: (flags & 0x00800) ? 'O' : '-',
1032: (flags & 0x00400) ? 'D' : '-',
1033: (flags & 0x00200) ? 'I' : '-',
1034: (flags & 0x00100) ? 'T' : '-',
1035: (flags & 0x00080) ? 'S' : '-',
1036: (flags & 0x00040) ? 'Z' : '-',
1037: (flags & 0x00010) ? 'A' : '-',
1038: (flags & 0x00004) ? 'P' : '-',
1039: (flags & 0x00001) ? 'C' : '-');
1040: } else {
1041: #endif
1042: 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",
1043: 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),
1044: #if defined(HAS_I386)
1045: PROTECTED_MODE ? "PE" : "--",
1046: #else
1047: "--",
1048: #endif
1049: (flags & 0x40000) ? 'A' : '-',
1050: (flags & 0x20000) ? 'V' : '-',
1051: (flags & 0x10000) ? 'R' : '-',
1052: (flags & 0x04000) ? 'N' : '-',
1053: (flags & 0x02000) ? '1' : '0',
1054: (flags & 0x01000) ? '1' : '0',
1055: (flags & 0x00800) ? 'O' : '-',
1056: (flags & 0x00400) ? 'D' : '-',
1057: (flags & 0x00200) ? 'I' : '-',
1058: (flags & 0x00100) ? 'T' : '-',
1059: (flags & 0x00080) ? 'S' : '-',
1060: (flags & 0x00040) ? 'Z' : '-',
1061: (flags & 0x00010) ? 'A' : '-',
1062: (flags & 0x00004) ? 'P' : '-',
1063: (flags & 0x00001) ? 'C' : '-');
1064: #if defined(HAS_I386)
1065: }
1066: #endif
1067: }
1068:
1069: void debugger_process_info(char *buffer)
1070: {
1071: UINT16 psp_seg = current_psp;
1072: process_t *process;
1073: bool check[0x10000] = {0};
1074:
1075: buffer[0] = '\0';
1076:
1077: while(!check[psp_seg] && (process = msdos_process_info_get(psp_seg, false)) != NULL) {
1078: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
1079: char *file = process->module_path, *s;
1080: char tmp[8192];
1081:
1082: while((s = strstr(file, "\\")) != NULL) {
1083: file = s + 1;
1084: }
1085: 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));
1086: strcat(tmp, buffer);
1087: strcpy(buffer, tmp);
1088:
1089: check[psp_seg] = true;
1090: psp_seg = psp->parent_psp;
1091: }
1092: }
1093:
1094: UINT32 debugger_get_val(const char *str)
1095: {
1096: char tmp[1024];
1097:
1098: if(str == NULL || strlen(str) == 0) {
1099: return(0);
1100: }
1101: strcpy(tmp, str);
1102:
1103: if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
1104: // ank
1105: return(tmp[1] & 0xff);
1106: } else if(tmp[0] == '%') {
1107: // decimal
1108: return(strtoul(tmp + 1, NULL, 10));
1109: }
1110: return(strtoul(tmp, NULL, 16));
1111: }
1112:
1113: UINT32 debugger_get_seg(const char *str, UINT32 val)
1114: {
1115: char tmp[1024], *s;
1116:
1117: if(str == NULL || strlen(str) == 0) {
1118: return(val);
1119: }
1120: strcpy(tmp, str);
1121:
1122: if((s = strstr(tmp, ":")) != NULL) {
1123: // 0000:0000
1124: *s = '\0';
1125: return(debugger_get_val(tmp));
1126: }
1127: return(val);
1128: }
1129:
1130: UINT32 debugger_get_ofs(const char *str)
1131: {
1132: char tmp[1024], *s;
1133:
1134: if(str == NULL || strlen(str) == 0) {
1135: return(0);
1136: }
1137: strcpy(tmp, str);
1138:
1139: if((s = strstr(tmp, ":")) != NULL) {
1140: // 0000:0000
1141: return(debugger_get_val(s + 1));
1142: }
1143: return(debugger_get_val(tmp));
1144: }
1145:
1146: void debugger_main()
1147: {
1148: telnet_command("\033[20h"); // cr-lf
1149:
1150: force_suspend = true;
1151: now_going = false;
1152: now_debugging = true;
1153: Sleep(100);
1154:
1155: if(!m_halted && !now_suspended) {
1156: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1157: telnet_printf("waiting until cpu is suspended...\n");
1158: }
1159: while(!m_halted && !now_suspended) {
1160: if(telnet_disconnected()) {
1161: break;
1162: }
1163: Sleep(10);
1164: }
1165:
1166: char buffer[8192];
1167:
1168: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1169: debugger_process_info(buffer);
1170: telnet_printf("%s", buffer);
1171: debugger_regs_info(buffer);
1172: telnet_printf("%s", buffer);
1173: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1174: telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
1175: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1176: debugger_dasm(buffer, SREG(CS), m_eip);
1177: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1178: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1179:
1180: #define MAX_COMMAND_LEN 64
1181:
1182: char command[MAX_COMMAND_LEN + 1];
1183: char prev_command[MAX_COMMAND_LEN + 1] = {0};
1184:
1185: UINT32 data_seg = SREG(DS);
1186: UINT32 data_ofs = 0;
1187: UINT32 dasm_seg = SREG(CS);
1188: UINT32 dasm_ofs = m_eip;
1189:
1190: while(!m_halted) {
1191: telnet_printf("- ");
1192: command[0] = '\0';
1193:
1194: if(fi_debugger != NULL) {
1195: while(command[0] == '\0') {
1196: if(fgets(command, sizeof(command), fi_debugger) == NULL) {
1197: break;
1198: }
1199: while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
1200: command[strlen(command) - 1] = '\0';
1201: }
1202: }
1203: if(command[0] != '\0') {
1204: telnet_command("%s\n", command);
1205: }
1206: }
1207: if(command[0] == '\0') {
1208: if(!telnet_gets(command, sizeof(command))) {
1209: break;
1210: }
1211: }
1212: if(command[0] == '\0') {
1213: strcpy(command, prev_command);
1214: } else {
1215: strcpy(prev_command, command);
1216: }
1217: if(fp_debugger != NULL) {
1218: fprintf(fp_debugger, "%s\n", command);
1219: }
1220:
1221: if(!m_halted && command[0] != 0) {
1222: char *params[32], *token = NULL;
1223: int num = 0;
1224:
1225: if((token = strtok(command, " ")) != NULL) {
1226: params[num++] = token;
1227: while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
1228: params[num++] = token;
1229: }
1230: }
1231: if(stricmp(params[0], "D") == 0) {
1232: if(num <= 3) {
1233: if(num >= 2) {
1234: data_seg = debugger_get_seg(params[1], data_seg);
1235: data_ofs = debugger_get_ofs(params[1]);
1236: }
1237: UINT32 end_seg = data_seg;
1238: UINT32 end_ofs = data_ofs + 8 * 16 - 1;
1239: if(num == 3) {
1240: end_seg = debugger_get_seg(params[2], data_seg);
1241: end_ofs = debugger_get_ofs(params[2]);
1242: }
1243: UINT64 start_addr = (data_seg << 4) + data_ofs;
1244: UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37 root 1245: // bool is_sjis = false;
1.1.1.33 root 1246:
1247: for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
1248: if((addr & 0x0f) == 0) {
1249: if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
1250: data_seg += 0x1000;
1251: data_ofs -= 0x10000;
1252: }
1253: telnet_printf("%06X:%04X ", data_seg, data_ofs);
1254: memset(buffer, 0, sizeof(buffer));
1255: }
1256: if(addr < start_addr || addr > end_addr) {
1257: telnet_printf(" ");
1258: buffer[addr & 0x0f] = ' ';
1259: } else {
1260: UINT8 data = debugger_read_byte(addr & ADDR_MASK);
1261: telnet_printf(" %02X", data);
1.1.1.37 root 1262: // if(is_sjis) {
1.1.1.33 root 1263: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1264: // is_sjis = false;
1.1.1.33 root 1265: // } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
1266: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1267: // is_sjis = true;
1.1.1.33 root 1268: // } else
1269: if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
1270: buffer[addr & 0x0f] = data;
1271: } else {
1272: buffer[addr & 0x0f] = '.';
1273: }
1274: }
1275: if((addr & 0x0f) == 0x0f) {
1276: telnet_printf(" %s\n", buffer);
1277: }
1278: }
1279: if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
1280: data_seg += 0x1000;
1281: data_ofs -= 0x10000;
1282: }
1283: prev_command[1] = '\0'; // remove parameters to dump continuously
1284: } else {
1285: telnet_printf("invalid parameter number\n");
1286: }
1287: } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
1288: if(num >= 3) {
1289: UINT32 seg = debugger_get_seg(params[1], data_seg);
1290: UINT32 ofs = debugger_get_ofs(params[1]);
1291: for(int i = 2, j = 0; i < num; i++, j++) {
1292: debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
1293: }
1294: } else {
1295: telnet_printf("invalid parameter number\n");
1296: }
1297: } else if(stricmp(params[0], "EW") == 0) {
1298: if(num >= 3) {
1299: UINT32 seg = debugger_get_seg(params[1], data_seg);
1300: UINT32 ofs = debugger_get_ofs(params[1]);
1301: for(int i = 2, j = 0; i < num; i++, j += 2) {
1302: debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
1303: }
1304: } else {
1305: telnet_printf("invalid parameter number\n");
1306: }
1307: } else if(stricmp(params[0], "ED") == 0) {
1308: if(num >= 3) {
1309: UINT32 seg = debugger_get_seg(params[1], data_seg);
1310: UINT32 ofs = debugger_get_ofs(params[1]);
1311: for(int i = 2, j = 0; i < num; i++, j += 4) {
1312: debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
1313: }
1314: } else {
1315: telnet_printf("invalid parameter number\n");
1316: }
1317: } else if(stricmp(params[0], "EA") == 0) {
1318: if(num >= 3) {
1319: UINT32 seg = debugger_get_seg(params[1], data_seg);
1320: UINT32 ofs = debugger_get_ofs(params[1]);
1321: strcpy(buffer, prev_command);
1322: if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
1323: int len = strlen(token);
1324: for(int i = 0; i < len; i++) {
1325: debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
1326: }
1327: } else {
1328: telnet_printf("invalid parameter\n");
1329: }
1330: } else {
1331: telnet_printf("invalid parameter number\n");
1332: }
1333: } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
1334: if(num == 2) {
1335: telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
1336: } else {
1337: telnet_printf("invalid parameter number\n");
1338: }
1339: } else if(stricmp(params[0], "IW") == 0) {
1340: if(num == 2) {
1341: telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
1342: } else {
1343: telnet_printf("invalid parameter number\n");
1344: }
1345: } else if(stricmp(params[0], "ID") == 0) {
1346: if(num == 2) {
1347: telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
1348: } else {
1349: telnet_printf("invalid parameter number\n");
1350: }
1351: } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
1352: if(num == 3) {
1353: debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
1354: } else {
1355: telnet_printf("invalid parameter number\n");
1356: }
1357: } else if(stricmp(params[0], "OW") == 0) {
1358: if(num == 3) {
1359: debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
1360: } else {
1361: telnet_printf("invalid parameter number\n");
1362: }
1363: } else if(stricmp(params[0], "OD") == 0) {
1364: if(num == 3) {
1365: debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
1366: } else {
1367: telnet_printf("invalid parameter number\n");
1368: }
1369: } else if(stricmp(params[0], "R") == 0) {
1370: if(num == 1) {
1371: debugger_regs_info(buffer);
1372: telnet_printf("%s", buffer);
1373: } else if(num == 3) {
1374: #if defined(HAS_I386)
1375: if(stricmp(params[1], "EAX") == 0) {
1376: REG32(EAX) = debugger_get_val(params[2]);
1377: } else if(stricmp(params[1], "EBX") == 0) {
1378: REG32(EBX) = debugger_get_val(params[2]);
1379: } else if(stricmp(params[1], "ECX") == 0) {
1380: REG32(ECX) = debugger_get_val(params[2]);
1381: } else if(stricmp(params[1], "EDX") == 0) {
1382: REG32(EDX) = debugger_get_val(params[2]);
1383: } else if(stricmp(params[1], "ESP") == 0) {
1384: REG32(ESP) = debugger_get_val(params[2]);
1385: } else if(stricmp(params[1], "EBP") == 0) {
1386: REG32(EBP) = debugger_get_val(params[2]);
1387: } else if(stricmp(params[1], "ESI") == 0) {
1388: REG32(ESI) = debugger_get_val(params[2]);
1389: } else if(stricmp(params[1], "EDI") == 0) {
1390: REG32(EDI) = debugger_get_val(params[2]);
1391: } else
1392: #endif
1393: if(stricmp(params[1], "AX") == 0) {
1394: REG16(AX) = debugger_get_val(params[2]);
1395: } else if(stricmp(params[1], "BX") == 0) {
1396: REG16(BX) = debugger_get_val(params[2]);
1397: } else if(stricmp(params[1], "CX") == 0) {
1398: REG16(CX) = debugger_get_val(params[2]);
1399: } else if(stricmp(params[1], "DX") == 0) {
1400: REG16(DX) = debugger_get_val(params[2]);
1401: } else if(stricmp(params[1], "SP") == 0) {
1402: REG16(SP) = debugger_get_val(params[2]);
1403: } else if(stricmp(params[1], "BP") == 0) {
1404: REG16(BP) = debugger_get_val(params[2]);
1405: } else if(stricmp(params[1], "SI") == 0) {
1406: REG16(SI) = debugger_get_val(params[2]);
1407: } else if(stricmp(params[1], "DI") == 0) {
1408: REG16(DI) = debugger_get_val(params[2]);
1409: } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
1410: #if defined(HAS_I386)
1411: if(m_operand_size) {
1412: m_eip = debugger_get_val(params[2]);
1413: } else {
1414: m_eip = debugger_get_val(params[2]) & 0xffff;
1415: }
1416: CHANGE_PC(m_eip);
1417: #else
1418: m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
1419: CHANGE_PC(m_pc);
1420: #endif
1421: } else if(stricmp(params[1], "AL") == 0) {
1422: REG8(AL) = debugger_get_val(params[2]);
1423: } else if(stricmp(params[1], "AH") == 0) {
1424: REG8(AH) = debugger_get_val(params[2]);
1425: } else if(stricmp(params[1], "BL") == 0) {
1426: REG8(BL) = debugger_get_val(params[2]);
1427: } else if(stricmp(params[1], "BH") == 0) {
1428: REG8(BH) = debugger_get_val(params[2]);
1429: } else if(stricmp(params[1], "CL") == 0) {
1430: REG8(CL) = debugger_get_val(params[2]);
1431: } else if(stricmp(params[1], "CH") == 0) {
1432: REG8(CH) = debugger_get_val(params[2]);
1433: } else if(stricmp(params[1], "DL") == 0) {
1434: REG8(DL) = debugger_get_val(params[2]);
1435: } else if(stricmp(params[1], "DH") == 0) {
1436: REG8(DH) = debugger_get_val(params[2]);
1437: } else {
1438: telnet_printf("unknown register %s\n", params[1]);
1439: }
1440: } else {
1441: telnet_printf("invalid parameter number\n");
1442: }
1443: } else if(_tcsicmp(params[0], "S") == 0) {
1444: if(num >= 4) {
1445: UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
1446: UINT32 cur_ofs = debugger_get_ofs(params[1]);
1447: UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
1448: UINT32 end_ofs = debugger_get_ofs(params[2]);
1449: UINT8 list[32];
1450:
1451: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1452: list[j] = debugger_get_val(params[i]);
1453: }
1454: while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
1455: bool found = true;
1456: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1457: if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
1458: found = false;
1459: break;
1460: }
1461: }
1462: if(found) {
1463: telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
1464: }
1465: if((cur_ofs += 1) > 0xffff) {
1466: cur_seg += 0x1000;
1467: cur_ofs -= 0x10000;
1468: }
1469: }
1470: } else {
1471: telnet_printf("invalid parameter number\n");
1472: }
1473: } else if(stricmp(params[0], "U") == 0) {
1474: if(num <= 3) {
1475: if(num >= 2) {
1476: dasm_seg = debugger_get_seg(params[1], dasm_seg);
1477: dasm_ofs = debugger_get_ofs(params[1]);
1478: }
1479: if(num == 3) {
1480: UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
1481: UINT32 end_ofs = debugger_get_ofs(params[2]);
1482:
1483: while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
1484: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1485: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1486: for(int i = 0; i < len; i++) {
1487: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1488: }
1489: for(int i = len; i < 8; i++) {
1490: telnet_printf(" ");
1491: }
1492: telnet_printf(" %s\n", buffer);
1493: if((dasm_ofs += len) > 0xffff) {
1494: dasm_seg += 0x1000;
1495: dasm_ofs -= 0x10000;
1496: }
1497: }
1498: } else {
1499: for(int i = 0; i < 16; i++) {
1500: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1501: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1502: for(int i = 0; i < len; i++) {
1503: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1504: }
1505: for(int i = len; i < 8; i++) {
1506: telnet_printf(" ");
1507: }
1508: telnet_printf(" %s\n", buffer);
1509: if((dasm_ofs += len) > 0xffff) {
1510: dasm_seg += 0x1000;
1511: dasm_ofs -= 0x10000;
1512: }
1513: }
1514: }
1515: prev_command[1] = '\0'; // remove parameters to disassemble continuously
1516: } else {
1517: telnet_printf("invalid parameter number\n");
1518: }
1519: } else if(stricmp(params[0], "H") == 0) {
1520: if(num == 3) {
1521: UINT32 l = debugger_get_val(params[1]);
1522: UINT32 r = debugger_get_val(params[2]);
1523: telnet_printf("%08X %08X\n", l + r, l - r);
1524: } else {
1525: telnet_printf("invalid parameter number\n");
1526: }
1527: } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
1528: break_point_t *break_point_ptr;
1529: #define GET_BREAK_POINT_PTR() { \
1530: if(params[0][0] == 'R') { \
1531: break_point_ptr = &rd_break_point; \
1532: } else if(params[0][0] == 'W') { \
1533: break_point_ptr = &wr_break_point; \
1534: } else if(params[0][0] == 'I') { \
1535: break_point_ptr = &in_break_point; \
1536: } else if(params[0][0] == 'O') { \
1537: break_point_ptr = &out_break_point; \
1538: } else { \
1539: break_point_ptr = &break_point; \
1540: } \
1541: }
1542: GET_BREAK_POINT_PTR();
1543: if(num == 2) {
1544: UINT32 seg = debugger_get_seg(params[1], SREG(CS));
1545: UINT32 ofs = debugger_get_ofs(params[1]);
1546: bool found = false;
1547: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1548: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
1549: break_point_ptr->table[i].addr = (seg << 4) + ofs;
1550: break_point_ptr->table[i].seg = seg;
1551: break_point_ptr->table[i].ofs = ofs;
1552: break_point_ptr->table[i].status = 1;
1553: found = true;
1554: }
1555: }
1556: if(!found) {
1557: telnet_printf("too many break points\n");
1558: }
1559: } else {
1560: telnet_printf("invalid parameter number\n");
1561: }
1562: } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
1563: break_point_t *break_point_ptr;
1564: GET_BREAK_POINT_PTR();
1565: if(num == 2) {
1566: UINT32 addr = debugger_get_val(params[1]);
1567: bool found = false;
1568: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1569: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
1570: break_point_ptr->table[i].addr = addr;
1571: break_point_ptr->table[i].status = 1;
1572: found = true;
1573: }
1574: }
1575: if(!found) {
1576: telnet_printf("too many break points\n");
1577: }
1578: } else {
1579: telnet_printf("invalid parameter number\n");
1580: }
1581: } 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) {
1582: break_point_t *break_point_ptr;
1583: GET_BREAK_POINT_PTR();
1584: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1585: memset(break_point_ptr, 0, sizeof(break_point_t));
1586: } else if(num >= 2) {
1587: for(int i = 1; i < num; i++) {
1588: int index = debugger_get_val(params[i]);
1589: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1590: telnet_printf("invalid index %x\n", index);
1591: } else {
1592: break_point_ptr->table[index - 1].addr = 0;
1593: break_point_ptr->table[index - 1].seg = 0;
1594: break_point_ptr->table[index - 1].ofs = 0;
1595: break_point_ptr->table[index - 1].status = 0;
1596: }
1597: }
1598: } else {
1599: telnet_printf("invalid parameter number\n");
1600: }
1601: } 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 ||
1602: 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) {
1603: break_point_t *break_point_ptr;
1604: GET_BREAK_POINT_PTR();
1605: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1606: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1607: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1608: if(break_point_ptr->table[i].status != 0) {
1609: break_point_ptr->table[i].status = enabled ? 1 : -1;
1610: }
1611: }
1612: } else if(num >= 2) {
1613: for(int i = 1; i < num; i++) {
1614: int index = debugger_get_val(params[i]);
1615: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1616: telnet_printf("invalid index %x\n", index);
1617: } else if(break_point_ptr->table[index - 1].status == 0) {
1618: telnet_printf("break point %x is null\n", index);
1619: } else {
1620: break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
1621: }
1622: }
1623: } else {
1624: telnet_printf("invalid parameter number\n");
1625: }
1626: } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
1627: break_point_t *break_point_ptr;
1628: GET_BREAK_POINT_PTR();
1629: if(num == 1) {
1630: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1631: if(break_point_ptr->table[i].status) {
1632: 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);
1633: }
1634: }
1635: } else {
1636: telnet_printf("invalid parameter number\n");
1637: }
1638: } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
1639: break_point_t *break_point_ptr;
1640: GET_BREAK_POINT_PTR();
1641: if(num == 1) {
1642: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1643: if(break_point_ptr->table[i].status) {
1644: telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
1645: }
1646: }
1647: } else {
1648: telnet_printf("invalid parameter number\n");
1649: }
1650: } else if(stricmp(params[0], "INTBP") == 0) {
1651: if(num >= 2 && num <= 4) {
1652: int int_num = debugger_get_val(params[1]);
1653: UINT8 ah = 0, ah_registered = 0;
1654: UINT8 al = 0, al_registered = 0;
1655: if(num >= 3) {
1656: ah = debugger_get_val(params[2]);
1657: ah_registered = 1;
1658: }
1659: if(num == 4) {
1660: al = debugger_get_val(params[3]);
1661: al_registered = 1;
1662: }
1663: bool found = false;
1664: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1665: if(int_break_point.table[i].status == 0 || (
1666: int_break_point.table[i].int_num == int_num &&
1667: int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
1668: int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
1669: int_break_point.table[i].int_num = int_num;
1670: int_break_point.table[i].ah = ah;
1671: int_break_point.table[i].ah_registered = ah_registered;
1672: int_break_point.table[i].al = al;
1673: int_break_point.table[i].al_registered = al_registered;
1674: int_break_point.table[i].status = 1;
1675: found = true;
1676: }
1677: }
1678: if(!found) {
1679: telnet_printf("too many break points\n");
1680: }
1681: } else {
1682: telnet_printf("invalid parameter number\n");
1683: }
1684: } else if(stricmp(params[0], "INTBC") == 0) {
1685: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1686: memset(&int_break_point, 0, sizeof(int_break_point_t));
1687: } else if(num >= 2) {
1688: for(int i = 1; i < num; i++) {
1689: int index = debugger_get_val(params[i]);
1690: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1691: telnet_printf("invalid index %x\n", index);
1692: } else {
1693: int_break_point.table[index - 1].int_num = 0;
1694: int_break_point.table[index - 1].ah = 0;
1695: int_break_point.table[index - 1].ah_registered = 0;
1696: int_break_point.table[index - 1].al = 0;
1697: int_break_point.table[index - 1].al_registered = 0;
1698: int_break_point.table[index - 1].status = 0;
1699: }
1700: }
1701: } else {
1702: telnet_printf("invalid parameter number\n");
1703: }
1704: } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
1705: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1706: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1707: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1708: if(int_break_point.table[i].status != 0) {
1709: int_break_point.table[i].status = enabled ? 1 : -1;
1710: }
1711: }
1712: } else if(num >= 2) {
1713: for(int i = 1; i < num; i++) {
1714: int index = debugger_get_val(params[i]);
1715: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1716: telnet_printf("invalid index %x\n", index);
1717: } else if(int_break_point.table[index - 1].status == 0) {
1718: telnet_printf("break point %x is null\n", index);
1719: } else {
1720: int_break_point.table[index - 1].status = enabled ? 1 : -1;
1721: }
1722: }
1723: } else {
1724: telnet_printf("invalid parameter number\n");
1725: }
1726: } else if(stricmp(params[0], "INTBL") == 0) {
1727: if(num == 1) {
1728: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1729: if(int_break_point.table[i].status) {
1730: telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
1731: if(int_break_point.table[i].ah_registered) {
1732: telnet_printf(" %02X", int_break_point.table[i].ah);
1733: }
1734: if(int_break_point.table[i].al_registered) {
1735: telnet_printf(" %02X", int_break_point.table[i].al);
1736: }
1737: telnet_printf("\n");
1738: }
1739: }
1740: } else {
1741: telnet_printf("invalid parameter number\n");
1742: }
1743: } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
1744: if(num == 1 || num == 2) {
1745: break_point_t break_point_stored;
1746: bool break_points_stored = false;
1747:
1748: if(stricmp(params[0], "P") == 0) {
1749: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1750: memset(&break_point, 0, sizeof(break_point_t));
1751: break_points_stored = true;
1752:
1753: break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
1754: break_point.table[0].status = 1;
1755: } else if(num >= 2) {
1756: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1757: memset(&break_point, 0, sizeof(break_point_t));
1758: break_points_stored = true;
1759:
1760: UINT32 seg = debugger_get_seg(params[1], SREG(CS));
1761: UINT32 ofs = debugger_get_ofs(params[1]);
1762: break_point.table[0].addr = (seg << 4) + ofs;
1763: break_point.table[0].seg = seg;
1764: break_point.table[0].ofs = ofs;
1765: break_point.table[0].status = 1;
1766: }
1767: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1768: now_going = true;
1769: now_suspended = false;
1770:
1771: telnet_command("\033[2l"); // key unlock
1772: while(!m_halted && !now_suspended) {
1773: if(telnet_kbhit()) {
1774: break;
1775: }
1776: Sleep(10);
1777: }
1778: now_going = false;
1779: telnet_command("\033[2h"); // key lock
1780:
1781: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1782: Sleep(100);
1783: if(!m_halted && !now_suspended) {
1784: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1785: telnet_printf("waiting until cpu is suspended...\n");
1786: }
1787: }
1788: while(!m_halted && !now_suspended) {
1789: if(telnet_disconnected()) {
1790: break;
1791: }
1792: Sleep(10);
1793: }
1794: dasm_seg = SREG(CS);
1795: dasm_ofs = m_eip;
1796:
1797: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1798: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1799: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1800:
1801: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1802: debugger_regs_info(buffer);
1803: telnet_printf("%s", buffer);
1804:
1805: if(break_point.hit) {
1806: if(stricmp(params[0], "G") == 0 && num == 1) {
1807: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1808: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1809: }
1810: } else if(rd_break_point.hit) {
1811: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1812: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1813: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1814: m_prev_cs, m_prev_eip);
1815: } else if(wr_break_point.hit) {
1816: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1817: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1818: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1819: m_prev_cs, m_prev_eip);
1820: } else if(in_break_point.hit) {
1821: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1822: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
1823: in_break_point.table[in_break_point.hit - 1].addr,
1824: m_prev_cs, m_prev_eip);
1825: } else if(out_break_point.hit) {
1826: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1827: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
1828: out_break_point.table[out_break_point.hit - 1].addr,
1829: m_prev_cs, m_prev_eip);
1830: } else if(int_break_point.hit) {
1831: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1832: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
1833: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
1834: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
1835: }
1836: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
1837: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
1838: }
1839: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
1840: } else {
1841: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1842: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
1843: }
1844: if(break_points_stored) {
1845: memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
1846: }
1847: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1848: debugger_dasm(buffer, SREG(CS), m_eip);
1849: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1850: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1851: } else {
1852: telnet_printf("invalid parameter number\n");
1853: }
1854: } else if(stricmp(params[0], "T") == 0) {
1855: if(num == 1 || num == 2) {
1856: int steps = 1;
1857: if(num >= 2) {
1858: steps = debugger_get_val(params[1]);
1859: }
1860:
1861: telnet_command("\033[2l"); // key unlock
1862: while(steps-- > 0) {
1863: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1864: now_going = false;
1865: now_suspended = false;
1866:
1867: while(!m_halted && !now_suspended) {
1868: if(telnet_disconnected()) {
1869: break;
1870: }
1871: Sleep(10);
1872: }
1873: dasm_seg = SREG(CS);
1874: dasm_ofs = m_eip;
1875:
1876: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1877: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1878: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1879:
1880: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1881: debugger_regs_info(buffer);
1882: telnet_printf("%s", buffer);
1883:
1884: 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()) {
1885: break;
1886: }
1887: }
1888: telnet_command("\033[2h"); // key lock
1889:
1890: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1891: Sleep(100);
1892: if(!m_halted && !now_suspended) {
1893: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1894: telnet_printf("waiting until cpu is suspended...\n");
1895: }
1896: }
1897: while(!m_halted && !now_suspended) {
1898: if(telnet_disconnected()) {
1899: break;
1900: }
1901: Sleep(10);
1902: }
1903: if(break_point.hit) {
1904: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1905: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1906: } else if(rd_break_point.hit) {
1907: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1908: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1909: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1910: m_prev_cs, m_prev_eip);
1911: } else if(wr_break_point.hit) {
1912: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1913: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1914: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1915: m_prev_cs, m_prev_eip);
1916: } else if(in_break_point.hit) {
1917: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1918: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
1919: in_break_point.table[in_break_point.hit - 1].addr,
1920: m_prev_cs, m_prev_eip);
1921: } else if(out_break_point.hit) {
1922: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1923: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
1924: out_break_point.table[out_break_point.hit - 1].addr,
1925: m_prev_cs, m_prev_eip);
1926: } else if(int_break_point.hit) {
1927: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1928: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
1929: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
1930: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
1931: }
1932: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
1933: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
1934: }
1935: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
1936: } else if(steps > 0) {
1937: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1938: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
1939: }
1940: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1941: debugger_dasm(buffer, SREG(CS), m_eip);
1942: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1943: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1944: } else {
1945: telnet_printf("invalid parameter number\n");
1946: }
1947: } else if(stricmp(params[0], "Q") == 0) {
1948: break;
1949: } else if(stricmp(params[0], "X") == 0) {
1950: debugger_process_info(buffer);
1951: telnet_printf("%s", buffer);
1952: } else if(stricmp(params[0], ">") == 0) {
1953: if(num == 2) {
1954: if(fp_debugger != NULL) {
1955: fclose(fp_debugger);
1956: fp_debugger = NULL;
1957: }
1958: fp_debugger = fopen(params[1], "w");
1959: } else {
1960: telnet_printf("invalid parameter number\n");
1961: }
1962: } else if(stricmp(params[0], "<") == 0) {
1963: if(num == 2) {
1964: if(fi_debugger != NULL) {
1965: fclose(fi_debugger);
1966: fi_debugger = NULL;
1967: }
1968: fi_debugger = fopen(params[1], "r");
1969: } else {
1970: telnet_printf("invalid parameter number\n");
1971: }
1972: } else if(stricmp(params[0], "?") == 0) {
1973: telnet_printf("D [<start> [<end>]] - dump memory\n");
1974: telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
1975: telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
1976: telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
1977: telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
1978:
1979: telnet_printf("R - show registers\n");
1980: telnet_printf("R <reg> <value> - edit register\n");
1981: telnet_printf("S <start> <end> <list> - search\n");
1982: telnet_printf("U [<start> [<end>]] - unassemble\n");
1983:
1984: telnet_printf("H <value> <value> - hexadd\n");
1985:
1986: telnet_printf("BP <address> - set breakpoint\n");
1987: telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
1988: telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
1989: telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
1990: telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
1991: telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
1992:
1993: telnet_printf("G - go (press enter key to break)\n");
1994: telnet_printf("G <address> - go and break at address\n");
1995: telnet_printf("P - trace one opcode (step over)\n");
1996: telnet_printf("T [<count>] - trace (step in)\n");
1997: telnet_printf("Q - quit\n");
1998: telnet_printf("X - show dos process info\n");
1999:
2000: telnet_printf("> <filename> - output logfile\n");
2001: telnet_printf("< <filename> - input commands from file\n");
2002:
2003: telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
2004: telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
2005: } else {
2006: telnet_printf("unknown command %s\n", params[0]);
2007: }
2008: }
2009: }
2010: if(fp_debugger != NULL) {
2011: fclose(fp_debugger);
2012: fp_debugger = NULL;
2013: }
2014: if(fi_debugger != NULL) {
2015: fclose(fi_debugger);
2016: fi_debugger = NULL;
2017: }
2018: now_debugging = now_going = now_suspended = force_suspend = false;
2019: closesocket(cli_socket);
2020: }
2021:
2022: const char *debugger_get_ttermpro_path()
2023: {
2024: static char path[MAX_PATH] = {0};
2025:
2026: if(getenv("ProgramFiles")) {
2027: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
2028: }
2029: return(path);
2030: }
2031:
2032: const char *debugger_get_ttermpro_x86_path()
2033: {
2034: static char path[MAX_PATH] = {0};
2035:
2036: if(getenv("ProgramFiles(x86)")) {
2037: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
2038: }
2039: return(path);
2040: }
2041:
2042: const char *debugger_get_putty_path()
2043: {
2044: static char path[MAX_PATH] = {0};
2045:
2046: if(getenv("ProgramFiles")) {
2047: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
2048: }
2049: return(path);
2050: }
2051:
2052: const char *debugger_get_putty_x86_path()
2053: {
2054: static char path[MAX_PATH] = {0};
2055:
2056: if(getenv("ProgramFiles(x86)")) {
2057: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
2058: }
2059: return(path);
2060: }
2061:
2062: const char *debugger_get_telnet_path()
2063: {
2064: // NOTE: When you run 32bit version of msdos.exe on Windows x64,
2065: // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
2066: // But 32bit version of telnet.exe will not be installed in SysWOW64
2067: // and 64bit version of telnet.exe will be installed in System32.
2068: static char path[MAX_PATH] = {0};
2069:
2070: if(getenv("windir") != NULL) {
2071: sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
2072: }
2073: return(path);
2074: }
2075:
2076: DWORD WINAPI debugger_thread(LPVOID)
2077: {
2078: WSADATA was_data;
2079: struct sockaddr_in svr_addr;
2080: struct sockaddr_in cli_addr;
2081: int cli_addr_len = sizeof(cli_addr);
2082: int port = 23;
2083: int bind_stat = SOCKET_ERROR;
2084: struct timeval timeout;
2085:
2086: WSAStartup(MAKEWORD(2,0), &was_data);
2087:
2088: if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
2089: memset(&svr_addr, 0, sizeof(svr_addr));
2090: svr_addr.sin_family = AF_INET;
2091: svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
2092:
2093: while(!m_halted && port < 10000) {
2094: svr_addr.sin_port = htons(port);
2095: if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
2096: break;
2097: } else {
2098: port = (port == 23) ? 9000 : (port + 1);
2099: }
2100: }
2101: if(bind_stat == 0) {
2102: timeout.tv_sec = 1;
2103: timeout.tv_usec = 0;
1.1.1.45 root 2104: setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33 root 2105:
2106: listen(svr_socket, 1);
2107:
2108: char command[MAX_PATH] = {0};
2109: STARTUPINFO si;
2110: PROCESS_INFORMATION pi;
2111:
2112: if(_access(debugger_get_ttermpro_path(), 0) == 0) {
2113: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
2114: } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
2115: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
2116: } else if(_access(debugger_get_putty_path(), 0) == 0) {
2117: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
2118: } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
2119: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
2120: } else if(_access(debugger_get_telnet_path(), 0) == 0) {
2121: sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
2122: }
2123: if(command[0] != '\0') {
2124: memset(&si, 0, sizeof(STARTUPINFO));
2125: memset(&pi, 0, sizeof(PROCESS_INFORMATION));
2126: CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
2127: }
2128:
2129: while(!m_halted) {
2130: if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
2131: u_long val = 1;
2132: ioctlsocket(cli_socket, FIONBIO, &val);
2133: debugger_main();
2134: }
2135: }
2136: }
2137: }
2138: WSACleanup();
2139: return(0);
2140: }
2141: #endif
2142:
2143: /* ----------------------------------------------------------------------------
1.1 root 2144: main
2145: ---------------------------------------------------------------------------- */
2146:
1.1.1.28 root 2147: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
2148: {
2149: if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33 root 2150: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 2151: #ifdef USE_SERVICE_THREAD
2152: EnterCriticalSection(&key_buf_crit_sect);
2153: #endif
1.1.1.51! root 2154: pcbios_clear_key_buffer();
1.1.1.35 root 2155: #ifdef USE_SERVICE_THREAD
2156: LeaveCriticalSection(&key_buf_crit_sect);
2157: #endif
1.1.1.33 root 2158: }
2159: // key_code = key_recv = 0;
1.1.1.28 root 2160: return TRUE;
2161: } else if(dwCtrlType == CTRL_C_EVENT) {
2162: return TRUE;
2163: } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
2164: // this program will be terminated abnormally, do minimum end process
2165: exit_handler();
2166: exit(1);
2167: }
2168: return FALSE;
2169: }
2170:
2171: void exit_handler()
2172: {
2173: if(temp_file_created) {
2174: DeleteFile(temp_file_path);
2175: temp_file_created = false;
2176: }
2177: if(key_buf_char != NULL) {
2178: key_buf_char->release();
2179: delete key_buf_char;
2180: key_buf_char = NULL;
2181: }
2182: if(key_buf_scan != NULL) {
2183: key_buf_scan->release();
2184: delete key_buf_scan;
2185: key_buf_scan = NULL;
2186: }
1.1.1.32 root 2187: #ifdef SUPPORT_XMS
2188: msdos_xms_release();
2189: #endif
1.1.1.28 root 2190: hardware_release();
2191: }
2192:
1.1.1.35 root 2193: #ifdef USE_VRAM_THREAD
1.1.1.28 root 2194: DWORD WINAPI vram_thread(LPVOID)
2195: {
2196: while(!m_halted) {
2197: EnterCriticalSection(&vram_crit_sect);
2198: if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
2199: vram_flush_char();
2200: }
2201: if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
2202: vram_flush_attr();
2203: }
2204: vram_last_length_char = vram_length_char;
2205: vram_last_length_attr = vram_length_attr;
2206: LeaveCriticalSection(&vram_crit_sect);
2207: // this is about half the maximum keyboard repeat rate - any
2208: // lower tends to be jerky, any higher misses updates
2209: Sleep(15);
2210: }
2211: return 0;
2212: }
2213: #endif
2214:
1.1.1.45 root 2215: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28 root 2216: {
2217: UINT8 header[0x400];
2218:
2219: long position = ftell(fp);
2220: fseek(fp, 0, SEEK_SET);
2221: fread(header, sizeof(header), 1, fp);
2222: fseek(fp, position, SEEK_SET);
2223:
2224: try {
2225: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2226: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2227: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2228: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2229: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2230: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2231:
2232: for(int i = 0; i < coffHeader->NumberOfSections; i++) {
2233: _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
2234: if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
2235: return(sectionHeader->PointerToRawData);
2236: }
2237: }
2238: } catch(...) {
2239: }
2240: return(0);
2241: }
2242:
1.1.1.10 root 2243: bool is_started_from_command_prompt()
2244: {
1.1.1.18 root 2245: bool ret = false;
2246:
2247: HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
2248: if(hLibrary) {
2249: typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
2250: GetConsoleProcessListFunction lpfnGetConsoleProcessList;
2251: lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
2252: if(lpfnGetConsoleProcessList) {
2253: DWORD pl;
2254: ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
2255: FreeLibrary(hLibrary);
2256: return(ret);
2257: }
2258: FreeLibrary(hLibrary);
2259: }
2260:
2261: HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2262: if(hSnapshot != INVALID_HANDLE_VALUE) {
2263: DWORD dwParentProcessID = 0;
2264: PROCESSENTRY32 pe32;
2265: pe32.dwSize = sizeof(PROCESSENTRY32);
2266: if(Process32First(hSnapshot, &pe32)) {
2267: do {
2268: if(pe32.th32ProcessID == GetCurrentProcessId()) {
2269: dwParentProcessID = pe32.th32ParentProcessID;
2270: break;
2271: }
2272: } while(Process32Next(hSnapshot, &pe32));
2273: }
2274: CloseHandle(hSnapshot);
2275: if(dwParentProcessID != 0) {
2276: HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
2277: if(hProcess != NULL) {
2278: HMODULE hMod;
2279: DWORD cbNeeded;
2280: if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
2281: char module_name[MAX_PATH];
2282: if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
2283: ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
2284: }
2285: }
2286: CloseHandle(hProcess);
2287: }
2288: }
2289: }
2290: return(ret);
1.1.1.14 root 2291: }
2292:
2293: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
2294: {
1.1.1.24 root 2295: // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14 root 2296: OSVERSIONINFOEX osvi;
2297: DWORDLONG dwlConditionMask = 0;
2298: int op = VER_GREATER_EQUAL;
2299:
2300: // Initialize the OSVERSIONINFOEX structure.
2301: ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
2302: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
2303: osvi.dwMajorVersion = dwMajorVersion;
2304: osvi.dwMinorVersion = dwMinorVersion;
2305: osvi.wServicePackMajor = wServicePackMajor;
2306: osvi.wServicePackMinor = wServicePackMinor;
2307:
2308: // Initialize the condition mask.
2309: VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
2310: VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
2311: VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
2312: VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
2313:
2314: // Perform the test.
2315: return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
2316: }
2317:
1.1.1.27 root 2318: void get_sio_port_numbers()
2319: {
2320: SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
2321: HDEVINFO hDevInfo = 0;
2322: HKEY hKey = 0;
2323: if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
2324: for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
2325: if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
2326: char chData[256];
2327: DWORD dwType = 0;
2328: DWORD dwSize = sizeof(chData);
2329: int port_number = 0;
2330:
2331: if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
2332: if(_strnicmp(chData, "COM", 3) == 0) {
2333: port_number = atoi(chData + 3);
2334: }
2335: }
2336: RegCloseKey(hKey);
2337:
1.1.1.29 root 2338: 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 2339: continue;
2340: }
2341: if(sio_port_number[0] == 0) {
2342: sio_port_number[0] = port_number;
2343: } else if(sio_port_number[1] == 0) {
2344: sio_port_number[1] = port_number;
1.1.1.29 root 2345: } else if(sio_port_number[2] == 0) {
2346: sio_port_number[2] = port_number;
2347: } else if(sio_port_number[3] == 0) {
2348: sio_port_number[3] = port_number;
1.1.1.27 root 2349: }
1.1.1.29 root 2350: 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 2351: break;
2352: }
2353: }
2354: }
2355: }
2356: }
2357:
1.1.1.28 root 2358: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
2359:
1.1 root 2360: int main(int argc, char *argv[], char *envp[])
2361: {
1.1.1.9 root 2362: int arg_offset = 0;
2363: int standard_env = 0;
1.1.1.14 root 2364: int buf_width = 0, buf_height = 0;
1.1.1.28 root 2365: bool get_console_info_success = false;
2366: bool screen_size_changed = false;
2367:
2368: _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
2369: GetModuleFileName(NULL, path, MAX_PATH);
2370: GetFullPathName(path, MAX_PATH, full, &name);
1.1 root 2371:
1.1.1.27 root 2372: char dummy_argv_0[] = "msdos.exe";
2373: char dummy_argv_1[MAX_PATH];
2374: char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
2375: char new_exec_file[MAX_PATH];
2376: bool convert_cmd_file = false;
1.1.1.28 root 2377: unsigned int code_page = 0;
1.1.1.27 root 2378:
2379: if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28 root 2380: // check if command file is embedded to this execution file
2381: // if this execution file name is msdos.exe, don't check
1.1.1.27 root 2382: FILE* fp = fopen(full, "rb");
1.1.1.28 root 2383: long offset = get_section_in_exec_file(fp, ".msdos");
2384: if(offset != 0) {
1.1.1.30 root 2385: UINT8 buffer[16];
1.1.1.28 root 2386: fseek(fp, offset, SEEK_SET);
2387: fread(buffer, sizeof(buffer), 1, fp);
2388:
2389: // restore flags
2390: stay_busy = ((buffer[0] & 0x01) != 0);
2391: no_windows = ((buffer[0] & 0x02) != 0);
2392: standard_env = ((buffer[0] & 0x04) != 0);
2393: ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
2394: limit_max_memory = ((buffer[0] & 0x10) != 0);
2395: if((buffer[0] & 0x20) != 0) {
2396: get_sio_port_numbers();
2397: }
2398: if((buffer[0] & 0x40) != 0) {
2399: UMB_TOP = EMS_TOP + EMS_SIZE;
2400: support_ems = true;
1.1.1.30 root 2401: }
1.1.1.27 root 2402: #ifdef SUPPORT_XMS
1.1.1.30 root 2403: if((buffer[0] & 0x80) != 0) {
1.1.1.28 root 2404: support_xms = true;
2405: }
1.1.1.30 root 2406: #endif
1.1.1.28 root 2407: if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
2408: buf_width = buffer[1] | (buffer[2] << 8);
2409: buf_height = buffer[3] | (buffer[4] << 8);
2410: }
2411: if(buffer[5] != 0) {
1.1.1.30 root 2412: dos_major_version = buffer[5];
2413: dos_minor_version = buffer[6];
2414: }
2415: if(buffer[7] != 0) {
2416: win_major_version = buffer[7];
2417: win_minor_version = buffer[8];
1.1.1.28 root 2418: }
1.1.1.30 root 2419: if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28 root 2420: SetConsoleCP(code_page);
2421: SetConsoleOutputCP(code_page);
2422: }
1.1.1.30 root 2423: int name_len = buffer[11];
2424: int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28 root 2425:
2426: // restore command file name
2427: memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
2428: fread(dummy_argv_1, name_len, 1, fp);
2429:
2430: if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
2431: // if original command file exists, create a temporary file name
2432: if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
2433: // create a temporary command file in the current director
2434: DeleteFile(dummy_argv_1);
1.1.1.27 root 2435: } else {
1.1.1.28 root 2436: // create a temporary command file in the temporary folder
2437: GetTempPath(MAX_PATH, path);
2438: if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
2439: DeleteFile(dummy_argv_1);
2440: } else {
2441: sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
2442: }
1.1.1.27 root 2443: }
1.1.1.28 root 2444: // check the command file type
2445: fread(buffer, 2, 1, fp);
2446: fseek(fp, -2, SEEK_CUR);
2447: if(memcmp(buffer, "MZ", 2) != 0) {
2448: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
2449: } else {
2450: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27 root 2451: }
2452: }
1.1.1.28 root 2453:
2454: // restore command file
2455: FILE* fo = fopen(dummy_argv_1, "wb");
2456: for(int i = 0; i < file_len; i++) {
2457: fputc(fgetc(fp), fo);
2458: }
2459: fclose(fo);
2460:
2461: GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
2462: temp_file_created = true;
2463: SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
2464:
2465: // adjust argc/argv
2466: for(int i = 1; i < argc && (i + 1) < 256; i++) {
2467: dummy_argv[i + 1] = argv[i];
2468: }
2469: argc++;
2470: argv = dummy_argv;
1.1.1.27 root 2471: }
2472: fclose(fp);
2473: }
1.1.1.9 root 2474: for(int i = 1; i < argc; i++) {
1.1.1.25 root 2475: if(_strnicmp(argv[i], "-b", 2) == 0) {
2476: stay_busy = true;
2477: arg_offset++;
1.1.1.27 root 2478: } else if(_strnicmp(argv[i], "-c", 2) == 0) {
2479: if(argv[i][2] != '\0') {
2480: strcpy(new_exec_file, &argv[i][2]);
2481: } else {
2482: strcpy(new_exec_file, "new_exec_file.exe");
2483: }
2484: convert_cmd_file = true;
2485: arg_offset++;
1.1.1.28 root 2486: } else if(_strnicmp(argv[i], "-p", 2) == 0) {
2487: if(IS_NUMERIC(argv[i][2])) {
2488: code_page = atoi(&argv[i][2]);
2489: } else {
2490: code_page = GetConsoleCP();
2491: }
2492: arg_offset++;
1.1.1.25 root 2493: } else if(_strnicmp(argv[i], "-d", 2) == 0) {
2494: no_windows = true;
2495: arg_offset++;
2496: } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9 root 2497: standard_env = 1;
2498: arg_offset++;
1.1.1.14 root 2499: } else if(_strnicmp(argv[i], "-i", 2) == 0) {
2500: ignore_illegal_insn = true;
2501: arg_offset++;
2502: } else if(_strnicmp(argv[i], "-m", 2) == 0) {
2503: limit_max_memory = true;
2504: arg_offset++;
2505: } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51! root 2506: int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
! 2507: if(result == 1) {
! 2508: buf_width = 0;
! 2509: } else if(result != 2) {
1.1.1.17 root 2510: buf_width = buf_height = 0;
2511: }
2512: if(buf_width <= 0 || buf_width > 0x7fff) {
2513: buf_width = 80;
2514: }
2515: if(buf_height <= 0 || buf_height > 0x7fff) {
2516: buf_height = 25;
2517: }
1.1.1.14 root 2518: arg_offset++;
1.1.1.25 root 2519: } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28 root 2520: if(IS_NUMERIC(argv[i][2])) {
1.1.1.29 root 2521: char *p0 = &argv[i][2], *p1, *p2, *p3;
2522: if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
2523: sio_port_number[1] = atoi(p1 + 1);
2524: if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
2525: sio_port_number[2] = atoi(p2 + 1);
2526: if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
2527: sio_port_number[3] = atoi(p3 + 1);
2528: }
2529: }
1.1.1.25 root 2530: }
1.1.1.29 root 2531: sio_port_number[0] = atoi(p0);
1.1.1.25 root 2532: }
1.1.1.29 root 2533: 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 2534: get_sio_port_numbers();
1.1.1.25 root 2535: }
2536: arg_offset++;
1.1.1.9 root 2537: } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17 root 2538: 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 2539: dos_major_version = argv[i][2] - '0';
2540: dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
2541: }
2542: arg_offset++;
2543: } else if(_strnicmp(argv[i], "-w", 2) == 0) {
2544: 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]))) {
2545: win_major_version = argv[i][2] - '0';
2546: win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9 root 2547: }
2548: arg_offset++;
1.1.1.25 root 2549: } else if(_strnicmp(argv[i], "-x", 2) == 0) {
2550: UMB_TOP = EMS_TOP + EMS_SIZE;
2551: support_ems = true;
2552: #ifdef SUPPORT_XMS
2553: support_xms = true;
2554: #endif
2555: arg_offset++;
1.1.1.9 root 2556: } else {
2557: break;
2558: }
2559: }
2560: if(argc < 2 + arg_offset) {
1.1 root 2561: #ifdef _WIN64
1.1.1.14 root 2562: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1 root 2563: #else
1.1.1.14 root 2564: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1 root 2565: #endif
1.1.1.25 root 2566: fprintf(stderr,
1.1.1.28 root 2567: "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30 root 2568: " [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25 root 2569: "\n"
2570: "\t-b\tstay busy during keyboard polling\n"
1.1.1.28 root 2571: #ifdef _WIN64
1.1.1.27 root 2572: "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28 root 2573: #else
1.1.1.27 root 2574: "\t-c\tconvert command file to 32bit execution file\n"
2575: #endif
1.1.1.28 root 2576: "\t-p\trecord current code page when convert command file\n"
1.1.1.25 root 2577: "\t-d\tpretend running under straight DOS, not Windows\n"
2578: "\t-e\tuse a reduced environment block\n"
2579: "\t-i\tignore invalid instructions\n"
2580: "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
2581: "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
2582: "\t-s\tenable serial I/O and set host's COM port numbers\n"
2583: "\t-v\tset the DOS version\n"
1.1.1.30 root 2584: "\t-w\tset the Windows version\n"
1.1.1.19 root 2585: #ifdef SUPPORT_XMS
1.1.1.28 root 2586: "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19 root 2587: #else
1.1.1.28 root 2588: "\t-x\tenable LIM EMS\n"
1.1.1.19 root 2589: #endif
2590: );
1.1.1.10 root 2591:
2592: if(!is_started_from_command_prompt()) {
2593: fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
2594: while(!_kbhit()) {
2595: Sleep(10);
2596: }
2597: }
1.1.1.20 root 2598: #ifdef _DEBUG
2599: _CrtDumpMemoryLeaks();
2600: #endif
1.1 root 2601: return(EXIT_FAILURE);
2602: }
1.1.1.27 root 2603: if(convert_cmd_file) {
2604: retval = EXIT_FAILURE;
1.1.1.28 root 2605: if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27 root 2606: FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28 root 2607: int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27 root 2608:
1.1.1.28 root 2609: if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
2610: fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
2611: } else if((fp = fopen(full, "rb")) == NULL) {
2612: fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27 root 2613: } else {
1.1.1.28 root 2614: long offset = get_section_in_exec_file(fp, ".msdos");
2615: if(offset != 0) {
2616: UINT8 buffer[14];
2617: fseek(fp, offset, SEEK_SET);
2618: fread(buffer, sizeof(buffer), 1, fp);
2619: memset(path, 0, sizeof(path));
2620: fread(path, buffer[9], 1, fp);
2621: fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
2622: } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
2623: fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
2624: } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
2625: fprintf(stderr, "Can't open '%s'\n", new_exec_file);
2626: } else {
2627: // read pe header of msdos.exe
2628: UINT8 header[0x400];
2629: fseek(fp, 0, SEEK_SET);
2630: fread(header, sizeof(header), 1, fp);
2631:
2632: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2633: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2634: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2635: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2636: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2637: _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
2638: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2639:
2640: _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
2641: DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
2642: DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
2643: DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
2644: if(dwExtraLastSectionBytes != 0) {
2645: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
2646: dwLastSectionSize += dwRemain;
2647: }
2648: DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
2649:
2650: // store msdos.exe
2651: fseek(fp, 0, SEEK_SET);
2652: for(int i = 0; i < dwEndOfFile; i++) {
2653: if((data = fgetc(fp)) != EOF) {
2654: fputc(data, fo);
2655: } else {
2656: // we should not reach here :-(
2657: fputc(0, fo);
2658: }
2659: }
2660:
2661: // store options
2662: UINT8 flags = 0;
2663: if(stay_busy) {
2664: flags |= 0x01;
2665: }
2666: if(no_windows) {
2667: flags |= 0x02;
2668: }
2669: if(standard_env) {
2670: flags |= 0x04;
2671: }
2672: if(ignore_illegal_insn) {
2673: flags |= 0x08;
2674: }
2675: if(limit_max_memory) {
2676: flags |= 0x10;
2677: }
1.1.1.29 root 2678: 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 2679: flags |= 0x20;
2680: }
2681: if(support_ems) {
2682: flags |= 0x40;
2683: }
1.1.1.30 root 2684: #ifdef SUPPORT_XMS
2685: if(support_xms) {
2686: flags |= 0x80;
2687: }
2688: #endif
1.1.1.28 root 2689:
2690: fputc(flags, fo);
2691: fputc((buf_width >> 0) & 0xff, fo);
2692: fputc((buf_width >> 8) & 0xff, fo);
2693: fputc((buf_height >> 0) & 0xff, fo);
2694: fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30 root 2695: fputc(dos_major_version, fo);
2696: fputc(dos_minor_version, fo);
2697: fputc(win_major_version, fo);
2698: fputc(win_minor_version, fo);
1.1.1.28 root 2699: fputc((code_page >> 0) & 0xff, fo);
2700: fputc((code_page >> 8) & 0xff, fo);
2701:
2702: // store command file info
2703: GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
2704: int name_len = strlen(name);
2705: fseek(fs, 0, SEEK_END);
2706: long file_size = ftell(fs);
2707:
2708: fputc(name_len, fo);
2709: fputc((file_size >> 0) & 0xff, fo);
2710: fputc((file_size >> 8) & 0xff, fo);
2711: fputc((file_size >> 16) & 0xff, fo);
2712: fputc((file_size >> 24) & 0xff, fo);
2713: fwrite(name, name_len, 1, fo);
2714:
2715: // store command file
2716: fseek(fs, 0, SEEK_SET);
2717: for(int i = 0; i < file_size; i++) {
2718: if((data = fgetc(fs)) != EOF) {
2719: fputc(data, fo);
2720: } else {
2721: // we should not reach here :-(
2722: fputc(0, fo);
2723: }
2724: }
2725:
2726: // store padding data and update pe header
1.1.1.29 root 2727: _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
2728: coffHeader->NumberOfSections++;
2729: memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
2730: memcpy(newSectionHeader->Name, ".msdos", 6);
2731: newSectionHeader->VirtualAddress = dwVirtualAddress;
2732: newSectionHeader->PointerToRawData = dwEndOfFile;
2733: newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28 root 2734: newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
2735: DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
2736: if(dwExtraRawBytes != 0) {
1.1.1.29 root 2737: static const char padding[] = "PADDINGXXPADDING";
1.1.1.28 root 2738: DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
2739: for(int i = 0; i < dwRemain; i++) {
1.1.1.29 root 2740: if(i < 2) {
2741: fputc(padding[i & 15], fo);
2742: } else {
2743: fputc(padding[(i - 2) & 15], fo);
2744: }
1.1.1.28 root 2745: }
2746: newSectionHeader->SizeOfRawData += dwRemain;
2747: }
2748: newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
2749:
2750: DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
2751: DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
2752: if(dwExtraNewSectionBytes != 0) {
2753: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
2754: dwNewSectionSize += dwRemain;
2755: }
2756: optionalHeader->SizeOfImage += dwNewSectionSize;
2757:
2758: fseek(fo, 0, SEEK_SET);
2759: fwrite(header, sizeof(header), 1, fo);
2760:
2761: fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
2762: retval = EXIT_SUCCESS;
1.1.1.27 root 2763: }
2764: }
2765: if(fp != NULL) {
2766: fclose(fp);
2767: }
2768: if(fs != NULL) {
2769: fclose(fs);
2770: }
2771: if(fo != NULL) {
2772: fclose(fo);
2773: }
2774: }
2775: #ifdef _DEBUG
2776: _CrtDumpMemoryLeaks();
2777: #endif
2778: return(retval);
2779: }
1.1 root 2780:
1.1.1.14 root 2781: is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
2782:
1.1.1.23 root 2783: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 2784: CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14 root 2785: CONSOLE_CURSOR_INFO ci;
1.1.1.23 root 2786:
1.1.1.28 root 2787: get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14 root 2788: GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24 root 2789: GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1 root 2790:
1.1.1.14 root 2791: for(int y = 0; y < SCR_BUF_WIDTH; y++) {
2792: for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
2793: SCR_BUF(y,x).Char.AsciiChar = ' ';
2794: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 2795: }
2796: }
1.1.1.28 root 2797: if(get_console_info_success) {
1.1.1.12 root 2798: scr_width = csbi.dwSize.X;
1.1.1.14 root 2799: scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
2800:
1.1.1.28 root 2801: // v-text shadow buffer size must be lesser than 0x7fd0
2802: if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14 root 2803: (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
2804: scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
2805: scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28 root 2806: if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14 root 2807: scr_width = 80;
2808: scr_height = 25;
2809: }
1.1.1.28 root 2810: screen_size_changed = true;
1.1.1.14 root 2811: }
1.1.1.12 root 2812: } else {
2813: // for a proof (not a console)
2814: scr_width = 80;
2815: scr_height = 25;
2816: }
1.1.1.14 root 2817: scr_buf_size.X = scr_width;
2818: scr_buf_size.Y = scr_height;
2819: scr_buf_pos.X = scr_buf_pos.Y = 0;
2820: scr_top = csbi.srWindow.Top;
1.1 root 2821: cursor_moved = false;
2822:
1.1.1.35 root 2823: #ifdef USE_SERVICE_THREAD
2824: InitializeCriticalSection(&input_crit_sect);
2825: InitializeCriticalSection(&key_buf_crit_sect);
2826: InitializeCriticalSection(&putch_crit_sect);
1.1.1.50 root 2827: main_thread_id = GetCurrentThreadId();
1.1.1.35 root 2828: #endif
1.1.1.50 root 2829:
1.1.1.25 root 2830: key_buf_char = new FIFO(256);
2831: key_buf_scan = new FIFO(256);
1.1 root 2832:
2833: hardware_init();
2834:
1.1.1.33 root 2835: #ifdef USE_DEBUGGER
2836: debugger_init();
2837: #endif
2838:
1.1.1.9 root 2839: if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1 root 2840: retval = EXIT_FAILURE;
2841: } else {
1.1.1.27 root 2842: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14 root 2843: _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
2844: #endif
2845: SetConsoleCtrlHandler(ctrl_handler, TRUE);
2846:
1.1.1.28 root 2847: if(screen_size_changed) {
1.1.1.24 root 2848: change_console_size(scr_width, scr_height);
2849: }
1.1.1.8 root 2850: TIMECAPS caps;
2851: timeGetDevCaps(&caps, sizeof(TIMECAPS));
2852: timeBeginPeriod(caps.wPeriodMin);
1.1.1.35 root 2853: #ifdef USE_VRAM_THREAD
1.1.1.14 root 2854: InitializeCriticalSection(&vram_crit_sect);
2855: CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
2856: #endif
1.1.1.33 root 2857: #ifdef USE_DEBUGGER
2858: CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
2859: // wait until telnet client starts and connects to me
2860: if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
2861: _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
2862: _access(debugger_get_putty_path(), 0) == 0 ||
2863: _access(debugger_get_putty_x86_path(), 0) == 0 ||
2864: _access(debugger_get_telnet_path(), 0) == 0) {
2865: for(int i = 0; i < 100 && cli_socket == 0; i++) {
2866: Sleep(100);
2867: }
2868: }
2869: #endif
1.1 root 2870: hardware_run();
1.1.1.35 root 2871: #ifdef USE_VRAM_THREAD
1.1.1.14 root 2872: vram_flush();
2873: DeleteCriticalSection(&vram_crit_sect);
2874: #endif
1.1.1.24 root 2875: timeEndPeriod(caps.wPeriodMin);
1.1.1.14 root 2876:
1.1.1.24 root 2877: // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28 root 2878: if(get_console_info_success) {
1.1.1.23 root 2879: hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 2880: if(restore_console_on_exit) {
1.1.1.14 root 2881: // window can't be bigger than buffer,
2882: // buffer can't be smaller than window,
2883: // so make a tiny window,
2884: // set the required buffer,
2885: // then set the required window
2886: SMALL_RECT rect;
2887: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
2888: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12 root 2889: SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14 root 2890: SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12 root 2891: SetConsoleWindowInfo(hStdout, TRUE, &rect);
2892: }
1.1.1.14 root 2893: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
2894: SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12 root 2895: }
1.1.1.24 root 2896: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
2897:
1.1 root 2898: msdos_finish();
1.1.1.14 root 2899:
2900: SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1 root 2901: }
1.1.1.35 root 2902: if(temp_file_created) {
2903: DeleteFile(temp_file_path);
2904: temp_file_created = false;
2905: }
1.1.1.10 root 2906: hardware_finish();
2907:
1.1.1.28 root 2908: if(key_buf_char != NULL) {
2909: key_buf_char->release();
2910: delete key_buf_char;
2911: key_buf_char = NULL;
2912: }
2913: if(key_buf_scan != NULL) {
2914: key_buf_scan->release();
2915: delete key_buf_scan;
2916: key_buf_scan = NULL;
2917: }
1.1.1.35 root 2918: #ifdef USE_SERVICE_THREAD
2919: DeleteCriticalSection(&input_crit_sect);
2920: DeleteCriticalSection(&key_buf_crit_sect);
2921: DeleteCriticalSection(&putch_crit_sect);
2922: #endif
1.1.1.20 root 2923: #ifdef _DEBUG
2924: _CrtDumpMemoryLeaks();
2925: #endif
1.1 root 2926: return(retval);
2927: }
2928:
1.1.1.20 root 2929: /* ----------------------------------------------------------------------------
2930: console
2931: ---------------------------------------------------------------------------- */
2932:
1.1.1.14 root 2933: void change_console_size(int width, int height)
1.1.1.12 root 2934: {
1.1.1.23 root 2935: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 2936: CONSOLE_SCREEN_BUFFER_INFO csbi;
2937: SMALL_RECT rect;
2938: COORD co;
2939:
2940: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14 root 2941: if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
2942: if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
2943: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
2944: SET_RECT(rect, 0, 0, width - 1, height - 1);
2945: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
2946: } else if(csbi.dwCursorPosition.Y > height - 1) {
2947: SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
2948: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
2949: SET_RECT(rect, 0, 0, width - 1, height - 1);
2950: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12 root 2951: }
2952: }
1.1.1.14 root 2953: if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12 root 2954: co.X = csbi.dwCursorPosition.X;
1.1.1.14 root 2955: co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12 root 2956: SetConsoleCursorPosition(hStdout, co);
2957: cursor_moved = true;
2958: }
1.1.1.14 root 2959:
2960: // window can't be bigger than buffer,
2961: // buffer can't be smaller than window,
2962: // so make a tiny window,
2963: // set the required buffer,
2964: // then set the required window
2965: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12 root 2966: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14 root 2967: co.X = width;
2968: co.Y = height;
1.1.1.12 root 2969: SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14 root 2970: SET_RECT(rect, 0, 0, width - 1, height - 1);
2971: SetConsoleWindowInfo(hStdout, TRUE, &rect);
2972:
2973: scr_width = scr_buf_size.X = width;
2974: scr_height = scr_buf_size.Y = height;
2975: scr_top = 0;
2976:
2977: clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
2978:
2979: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15 root 2980: text_vram_end_address = text_vram_top_address + regen;
2981: shadow_buffer_end_address = shadow_buffer_top_address + regen;
2982:
1.1.1.14 root 2983: if(regen > 0x4000) {
2984: regen = 0x8000;
2985: vram_pages = 1;
2986: } else if(regen > 0x2000) {
2987: regen = 0x4000;
2988: vram_pages = 2;
2989: } else if(regen > 0x1000) {
2990: regen = 0x2000;
2991: vram_pages = 4;
2992: } else {
2993: regen = 0x1000;
2994: vram_pages = 8;
2995: }
1.1.1.15 root 2996: *(UINT16 *)(mem + 0x44a) = scr_width;
2997: *(UINT16 *)(mem + 0x44c) = regen;
2998: *(UINT8 *)(mem + 0x484) = scr_height - 1;
2999:
1.1.1.24 root 3000: mouse.min_position.x = 0;
3001: mouse.min_position.y = 0;
1.1.1.34 root 3002: mouse.max_position.x = 8 * (scr_width - 1);
3003: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 3004:
1.1.1.15 root 3005: restore_console_on_exit = true;
1.1.1.14 root 3006: }
3007:
3008: void clear_scr_buffer(WORD attr)
3009: {
3010: for(int y = 0; y < scr_height; y++) {
3011: for(int x = 0; x < scr_width; x++) {
3012: SCR_BUF(y,x).Char.AsciiChar = ' ';
3013: SCR_BUF(y,x).Attributes = attr;
3014: }
3015: }
1.1.1.12 root 3016: }
3017:
1.1.1.24 root 3018: bool update_console_input()
1.1 root 3019: {
1.1.1.35 root 3020: #ifdef USE_SERVICE_THREAD
3021: EnterCriticalSection(&input_crit_sect);
3022: #endif
1.1.1.23 root 3023: HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8 root 3024: DWORD dwNumberOfEvents = 0;
1.1 root 3025: DWORD dwRead;
3026: INPUT_RECORD ir[16];
1.1.1.24 root 3027: CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
3028: bool result = false;
1.1 root 3029:
1.1.1.8 root 3030: if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
3031: if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
3032: for(int i = 0; i < dwRead; i++) {
1.1.1.24 root 3033: if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34 root 3034: if(mouse.hidden == 0) {
3035: // NOTE: if restore_console_on_exit, console is not scrolled
3036: if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
3037: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
3038: }
3039: // FIXME: character size is always 8x8 ???
3040: int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
3041: int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
3042:
3043: if(mouse.position.x != x || mouse.position.y != y) {
3044: mouse.position.x = x;
3045: mouse.position.y = y;
3046: mouse.status |= 1;
1.1.1.43 root 3047: mouse.status_alt |= 1;
1.1.1.34 root 3048: }
3049: }
3050: if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
3051: for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
3052: static const DWORD bits[] = {
3053: FROM_LEFT_1ST_BUTTON_PRESSED, // left
3054: RIGHTMOST_BUTTON_PRESSED, // right
3055: FROM_LEFT_2ND_BUTTON_PRESSED, // middle
3056: };
3057: bool prev_status = mouse.buttons[i].status;
3058: mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
3059:
3060: if(!prev_status && mouse.buttons[i].status) {
3061: mouse.buttons[i].pressed_times++;
3062: mouse.buttons[i].pressed_position.x = mouse.position.x;
3063: mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43 root 3064: if(i < 2) {
3065: mouse.status_alt |= 2 << (i * 2);
3066: }
1.1.1.34 root 3067: mouse.status |= 2 << (i * 2);
3068: } else if(prev_status && !mouse.buttons[i].status) {
3069: mouse.buttons[i].released_times++;
3070: mouse.buttons[i].released_position.x = mouse.position.x;
3071: mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43 root 3072: if(i < 2) {
3073: mouse.status_alt |= 4 << (i * 2);
3074: }
1.1.1.34 root 3075: mouse.status |= 4 << (i * 2);
1.1.1.14 root 3076: }
3077: }
3078: }
1.1.1.24 root 3079: } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33 root 3080: // update keyboard flags in bios data area
1.1.1.35 root 3081: if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
3082: mem[0x417] |= 0x40;
1.1.1.33 root 3083: } else {
1.1.1.35 root 3084: mem[0x417] &= ~0x40;
1.1.1.33 root 3085: }
1.1.1.35 root 3086: if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
3087: mem[0x417] |= 0x20;
1.1.1.33 root 3088: } else {
1.1.1.35 root 3089: mem[0x417] &= ~0x20;
3090: }
3091: if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
3092: mem[0x417] |= 0x10;
3093: } else {
3094: mem[0x417] &= ~0x10;
1.1.1.33 root 3095: }
3096: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43 root 3097: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3098: mouse.status_alt |= 0x80;
3099: }
1.1.1.33 root 3100: mem[0x417] |= 0x08;
3101: } else {
3102: mem[0x417] &= ~0x08;
3103: }
1.1.1.35 root 3104: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43 root 3105: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3106: mouse.status_alt |= 0x40;
3107: }
1.1.1.35 root 3108: mem[0x417] |= 0x04;
1.1.1.33 root 3109: } else {
1.1.1.35 root 3110: mem[0x417] &= ~0x04;
1.1.1.33 root 3111: }
3112: if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43 root 3113: if(mouse.buttons[0].status || mouse.buttons[1].status) {
3114: mouse.status_alt |= 0x20;
3115: }
1.1.1.33 root 3116: if(!(mem[0x417] & 0x03)) {
3117: mem[0x417] |= 0x02; // left shift
3118: }
3119: } else {
3120: mem[0x417] &= ~0x03;
3121: }
1.1.1.35 root 3122: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
3123: mem[0x418] |= 0x02;
3124: } else {
3125: mem[0x418] &= ~0x02;
3126: }
3127: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
3128: mem[0x418] |= 0x01;
3129: } else {
3130: mem[0x418] &= ~0x01;
3131: }
1.1.1.33 root 3132:
1.1.1.28 root 3133: // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24 root 3134: kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33 root 3135: kbd_status |= 1;
3136:
3137: // update dos key buffer
3138: UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
3139: UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51! root 3140: UINT8 scn_old = scn;
1.1.1.33 root 3141:
3142: if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28 root 3143: // make
1.1.1.24 root 3144: kbd_data &= 0x7f;
3145:
1.1.1.33 root 3146: if(chr == 0x00) {
1.1.1.24 root 3147: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3148: if(scn >= 0x3b && scn <= 0x44) {
3149: scn += 0x68 - 0x3b; // F1 to F10
3150: } else if(scn == 0x57 || scn == 0x58) {
3151: scn += 0x8b - 0x57; // F11 & F12
3152: } else if(scn >= 0x47 && scn <= 0x53) {
3153: scn += 0x97 - 0x47; // edit/arrow clusters
3154: } else if(scn == 0x35) {
3155: scn = 0xa4; // keypad /
3156: }
3157: } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
3158: if(scn == 0x07) {
3159: chr = 0x1e; // Ctrl+^
3160: } else if(scn == 0x0c) {
3161: chr = 0x1f; // Ctrl+_
3162: } else if(scn >= 0x35 && scn <= 0x58) {
3163: static const UINT8 ctrl_map[] = {
3164: 0x95, // keypad /
3165: 0,
3166: 0x96, // keypad *
3167: 0, 0, 0,
3168: 0x5e, // F1
3169: 0x5f, // F2
3170: 0x60, // F3
3171: 0x61, // F4
3172: 0x62, // F5
3173: 0x63, // F6
3174: 0x64, // F7
3175: 0x65, // F8
3176: 0x66, // F9
3177: 0x67, // F10
3178: 0,
3179: 0,
3180: 0x77, // Home
3181: 0x8d, // Up
3182: 0x84, // PgUp
3183: 0x8e, // keypad -
3184: 0x73, // Left
3185: 0x8f, // keypad center
3186: 0x74, // Right
3187: 0x90, // keyapd +
3188: 0x75, // End
3189: 0x91, // Down
3190: 0x76, // PgDn
3191: 0x92, // Insert
3192: 0x93, // Delete
3193: 0, 0, 0,
3194: 0x89, // F11
3195: 0x8a, // F12
3196: };
3197: scn = ctrl_map[scn - 0x35];
3198: }
3199: } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
3200: if(scn >= 0x3b && scn <= 0x44) {
3201: scn += 0x54 - 0x3b; // F1 to F10
3202: } else if(scn == 0x57 || scn == 0x58) {
3203: scn += 0x87 - 0x57; // F11 & F12
3204: }
3205: } else if(scn == 0x57 || scn == 0x58) {
3206: scn += 0x85 - 0x57;
3207: }
3208: // ignore shift, ctrl, alt, win and menu keys
1.1.1.51! root 3209: if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32 root 3210: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3211: #ifdef USE_SERVICE_THREAD
3212: EnterCriticalSection(&key_buf_crit_sect);
3213: #endif
1.1.1.32 root 3214: if(chr == 0) {
1.1.1.51! root 3215: pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32 root 3216: }
1.1.1.51! root 3217: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3218: #ifdef USE_SERVICE_THREAD
3219: LeaveCriticalSection(&key_buf_crit_sect);
3220: #endif
1.1.1.24 root 3221: }
3222: }
3223: } else {
3224: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3225: chr = 0;
3226: if(scn >= 0x02 && scn <= 0x0e) {
3227: scn += 0x78 - 0x02; // 1 to 0 - =
3228: }
3229: }
1.1.1.32 root 3230: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3231: #ifdef USE_SERVICE_THREAD
3232: EnterCriticalSection(&key_buf_crit_sect);
3233: #endif
1.1.1.51! root 3234: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3235: #ifdef USE_SERVICE_THREAD
3236: LeaveCriticalSection(&key_buf_crit_sect);
3237: #endif
1.1.1.32 root 3238: }
1.1.1.24 root 3239: }
1.1.1.33 root 3240: } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
3241: // ctrl-break, ctrl-c
3242: if(scn == 0x46) {
3243: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3244: #ifdef USE_SERVICE_THREAD
3245: EnterCriticalSection(&key_buf_crit_sect);
3246: #endif
1.1.1.51! root 3247: pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35 root 3248: #ifdef USE_SERVICE_THREAD
3249: LeaveCriticalSection(&key_buf_crit_sect);
3250: #endif
1.1.1.33 root 3251: }
3252: ctrl_break_pressed = true;
3253: mem[0x471] = 0x80;
3254: raise_int_1bh = true;
3255: } else {
3256: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3257: #ifdef USE_SERVICE_THREAD
3258: EnterCriticalSection(&key_buf_crit_sect);
3259: #endif
1.1.1.51! root 3260: pcbios_set_key_buffer(chr, scn);
1.1.1.35 root 3261: #ifdef USE_SERVICE_THREAD
3262: LeaveCriticalSection(&key_buf_crit_sect);
3263: #endif
1.1.1.33 root 3264: }
3265: ctrl_c_pressed = (scn == 0x2e);
3266: }
3267: } else {
3268: // break
3269: kbd_data |= 0x80;
1.1 root 3270: }
1.1.1.24 root 3271: result = key_changed = true;
1.1.1.36 root 3272: // IME may be on and it may causes screen scroll up and cursor position change
3273: cursor_moved = true;
1.1 root 3274: }
3275: }
3276: }
3277: }
1.1.1.35 root 3278: #ifdef USE_SERVICE_THREAD
3279: LeaveCriticalSection(&input_crit_sect);
3280: #endif
1.1.1.24 root 3281: return(result);
1.1.1.8 root 3282: }
3283:
1.1.1.14 root 3284: bool update_key_buffer()
1.1.1.8 root 3285: {
1.1.1.35 root 3286: if(update_console_input()) {
3287: return(true);
3288: }
3289: if(key_buf_char != NULL && key_buf_scan != NULL) {
3290: #ifdef USE_SERVICE_THREAD
3291: EnterCriticalSection(&key_buf_crit_sect);
3292: #endif
3293: bool empty = key_buf_char->empty();
3294: #ifdef USE_SERVICE_THREAD
3295: LeaveCriticalSection(&key_buf_crit_sect);
3296: #endif
3297: if(!empty) return(true);
3298: }
3299: return(false);
1.1.1.8 root 3300: }
3301:
1.1.1.20 root 3302: /* ----------------------------------------------------------------------------
3303: MS-DOS virtual machine
3304: ---------------------------------------------------------------------------- */
3305:
1.1.1.32 root 3306: static const struct {
1.1.1.33 root 3307: char *name;
3308: DWORD lcid;
3309: char *std;
3310: char *dlt;
3311: } tz_table[] = {
3312: // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
3313: // 0 GMT Greenwich Mean Time GMT0
3314: {"GMT Standard Time", 0x0809, "GMT", "BST"}, // (UTC+00:00) GB London (en-gb)
3315: {"GMT Standard Time", 0x1809, "GMT", "IST"}, // (UTC+00:00) IE Dublin (en-ie)
3316: {"GMT Standard Time", 0x0000, "WET", "WES"}, // (UTC+00:00) PT Lisbon
3317: {"Greenwich Standard Time", 0x0000, "GMT", "GST"}, // (UTC+00:00) IS Reykjavik
3318: // 2 FST FDT Fernando De Noronha Std FST2FDT
3319: {"Mid-Atlantic Standard Time", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3320: {"UTC-02", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3321: // 3 BST Brazil Standard Time BST3
3322: {"Bahia Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Bahia
3323: {"SA Eastern Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Fortaleza
3324: {"Tocantins Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Palmas
3325: // 3 EST EDT Eastern Standard (Brazil) EST3EDT
3326: {"E. South America Standard Time", 0x0000, "EST", "EDT"}, // (UTC-03:00) BR Sao Paulo
3327: // 3 GST Greenland Standard Time GST3
3328: {"Greenland Standard Time", 0x0000, "GST", "GDT"}, // (UTC-03:00) GL Godthab
3329: // 3:30 NST NDT Newfoundland Standard Time NST3:30NDT
3330: {"Newfoundland Standard Time", 0x0000, "NST", "NDT"}, // (UTC-03:30) CA St.Johns
3331: // 4 AST ADT Atlantic Standard Time AST4ADT
3332: {"Atlantic Standard Time", 0x0000, "AST", "ADT"}, // (UTC-04:00) CA Halifax
3333: // 4 WST WDT Western Standard (Brazil) WST4WDT
3334: {"Central Brazilian Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Cuiaba
3335: {"SA Western Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Manaus
3336: // 5 EST EDT Eastern Standard Time EST5EDT
3337: {"Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US New York
3338: {"Eastern Standard Time (Mexico)", 0x0000, "EST", "EDT"}, // (UTC-05:00) MX Cancun
3339: {"US Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US Indianapolis
3340: // 5 CST CDT Chile Standard Time CST5CDT
3341: {"Pacific SA Standard Time", 0x0000, "CST", "CDT"}, // (UTC-04:00) CL Santiago
3342: // 5 AST ADT Acre Standard Time AST5ADT
3343: {"SA Pacific Standard Time", 0x0000, "AST", "ADT"}, // (UTC-05:00) BR Rio Branco
3344: // 5 CST CDT Cuba Standard Time CST5CDT
3345: {"Cuba Standard Time", 0x0000, "CST", "CDT"}, // (UTC-05:00) CU Havana
3346: // 6 CST CDT Central Standard Time CST6CDT
3347: {"Canada Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) CA Regina
3348: {"Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) US Chicago
3349: {"Central Standard Time (Mexico)", 0x0000, "CST", "CDT"}, // (UTC-06:00) MX Mexico City
3350: // 6 EST EDT Easter Island Standard EST6EDT
3351: {"Easter Island Standard Time", 0x0000, "EST", "EDT"}, // (UTC-06:00) CL Easter
3352: // 7 MST MDT Mountain Standard Time MST7MDT
3353: {"Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Denver
3354: {"Mountain Standard Time (Mexico)", 0x0000, "MST", "MDT"}, // (UTC-07:00) MX Chihuahua
3355: {"US Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Phoenix
3356: // 8 PST PDT Pacific Standard Time PST8PDT
3357: {"Pacific Standard Time", 0x0000, "PST", "PDT"}, // (UTC-08:00) US Los Angeles
3358: {"Pacific Standard Time (Mexico)", 0x0000, "PST", "PDT"}, // (UTC-08:00) MX Tijuana
3359: // 9 AKS AKD Alaska Standard Time AKS9AKD
3360: // 9 YST YDT Yukon Standard Time YST9YST
3361: {"Alaskan Standard Time", 0x0000, "AKS", "AKD"}, // (UTC-09:00) US Anchorage
3362: // 10 HST HDT Hawaii Standard Time HST10HDT
3363: {"Aleutian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Aleutian
3364: {"Hawaiian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Honolulu
3365: // 11 SST Samoa Standard Time SST11
3366: {"Samoa Standard Time", 0x0000, "SST", "SDT"}, // (UTC-11:00) US Samoa
3367: // -12 NZS NZD New Zealand Standard Time NZS-12NZD
3368: {"New Zealand Standard Time", 0x0000, "NZS", "NZD"}, // (UTC+12:00) NZ Auckland
3369: // -10 GST Guam Standard Time GST-10
3370: {"West Pacific Standard Time", 0x0000, "GST", "GDT"}, // (UTC+10:00) GU Guam
3371: // -10 EAS EAD Eastern Australian Standard EAS-10EAD
3372: {"AUS Eastern Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Sydney
3373: {"E. Australia Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Brisbane
3374: {"Tasmania Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Hobart
3375: // -9:30 CAS CAD Central Australian Standard CAS-9:30CAD
3376: {"AUS Central Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Darwin
3377: {"Cen. Australia Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Adelaide
3378: // -9 JST Japan Standard Time JST-9
3379: {"Tokyo Standard Time", 0x0000, "JST", "JDT"}, // (UTC+09:00) JP Tokyo
3380: // -9 KST KDT Korean Standard Time KST-9KDT
3381: {"Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+09:00) KR Seoul
3382: {"North Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+08:30) KP Pyongyang
3383: // -8 HKT Hong Kong Time HKT-8
3384: {"China Standard Time", 0x0C04, "HKT", "HKS"}, // (UTC+08:00) HK Hong Kong (zh-hk)
3385: // -8 CCT China Coast Time CCT-8
3386: {"China Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) CN Shanghai
3387: {"Taipei Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) TW Taipei
3388: // -8 SST Singapore Standard Time SST-8
3389: {"Singapore Standard Time", 0x0000, "SST", "SDT"}, // (UTC+08:00) SG Singapore
3390: // -8 WAS WAD Western Australian Standard WAS-8WAD
3391: {"Aus Central W. Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:45) AU Eucla
3392: {"W. Australia Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:00) AU Perth
3393: // -7:30 JT Java Standard Time JST-7:30
3394: // -7 NST North Sumatra Time NST-7
3395: {"SE Asia Standard Time", 0x0000, "NST", "NDT"}, // (UTC+07:00) ID Jakarta
3396: // -5:30 IST Indian Standard Time IST-5:30
3397: {"India Standard Time", 0x0000, "IST", "IDT"}, // (UTC+05:30) IN Calcutta
3398: // -3:30 IST IDT Iran Standard Time IST-3:30IDT
3399: {"Iran Standard Time", 0x0000, "IST", "IDT"}, // (UTC+03:30) IR Tehran
3400: // -3 MSK MSD Moscow Winter Time MSK-3MSD
3401: {"Belarus Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) BY Minsk
3402: {"Russian Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) RU Moscow
3403: // -2 EET Eastern Europe Time EET-2
3404: {"E. Europe Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) MD Chisinau
3405: {"FLE Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) UA Kiev
3406: {"GTB Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RO Bucharest
3407: {"Kaliningrad Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RU Kaliningrad
3408: // -2 IST IDT Israel Standard Time IST-2IDT
3409: {"Israel Standard Time", 0x0000, "IST", "IDT"}, // (UTC+02:00) IL Jerusalem
3410: // -1 MEZ MES Middle European Time MEZ-1MES
3411: // -1 SWT SST Swedish Winter Time SWT-1SST
3412: // -1 FWT FST French Winter Time FWT-1FST
3413: // -1 CET CES Central European Time CET-1CES
3414: {"Central Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) HU Budapest
3415: {"Central European Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) PL Warsaw
3416: {"Romance Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) FR Paris
3417: {"W. Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) DE Berlin
3418: // -1 WAT West African Time WAT-1
3419: {"Namibia Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NA Windhoek
3420: {"W. Central Africa Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NG Lagos
3421: // 0 UTC Universal Coordinated Time UTC0
3422: {"UTC", 0x0000, "UTC", "" }, // (UTC+00:00) GMT+0
3423: {"UTC-02", 0x0000, "UTC", "" }, // (UTC-02:00) GMT+2
3424: {"UTC-08", 0x0000, "UTC", "" }, // (UTC-08:00) GMT+8
3425: {"UTC-09", 0x0000, "UTC", "" }, // (UTC-09:00) GMT+9
3426: {"UTC-11", 0x0000, "UTC", "" }, // (UTC-11:00) GMT+11
3427: {"UTC+12", 0x0000, "UTC", "" }, // (UTC+12:00) GMT-12
3428: };
3429:
3430: static const struct {
1.1.1.32 root 3431: UINT16 code;
3432: char *message_english;
3433: char *message_japanese;
3434: } standard_error_table[] = {
3435: {0x01, "Invalid function", "�����ȃt�@���N�V�����ł�."},
3436: {0x02, "File not found", "�t�@�C����������܂���."},
3437: {0x03, "Path not found", "�p�X��������܂���."},
3438: {0x04, "Too many open files", "�J����Ă���t�@�C�����������܂�."},
3439: {0x05, "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
3440: {0x06, "Invalid handle", "�����ȃn���h���ł�."},
3441: {0x07, "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
3442: {0x08, "Insufficient memory", "������������܂���."},
3443: {0x09, "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
3444: {0x0A, "Invalid Environment", "�����Ȋ��ł�."},
3445: {0x0B, "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
3446: {0x0C, "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
3447: {0x0D, "Invalid data", "�����ȃf�[�^�ł�."},
3448: {0x0F, "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
3449: {0x10, "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
3450: {0x11, "Not same device", "�����f�o�C�X�ł͂���܂���."},
3451: {0x12, "No more files", "�t�@�C���͂���ȏ゠��܂���."},
3452: {0x13, "Write protect error", "�������ݕی�G���[�ł�."},
3453: {0x14, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3454: {0x15, "Not ready", "�������ł��Ă��܂���."},
3455: {0x16, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3456: {0x17, "Data error", "�f�[�^�G���[�ł�."},
3457: {0x18, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3458: {0x19, "Seek error", "�V�[�N�G���[�ł�."},
3459: {0x1A, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3460: {0x1B, "Sector not found", "�Z�N�^��������܂���."},
3461: {0x1C, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3462: {0x1D, "Write fault error", "�������݃G���[�ł�."},
3463: {0x1E, "Read fault error", "�ǂݎ��G���[�ł�."},
3464: {0x1F, "General failure", "�G���[�ł�."},
3465: {0x20, "Sharing violation", "���L�ᔽ�ł�."},
3466: {0x21, "Lock violation", "���b�N�ᔽ�ł�."},
3467: {0x22, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3468: {0x23, "FCB unavailable", "FCB �͎g���܂���."},
3469: {0x24, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3470: {0x25, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3471: {0x26, "Out of input", "���͂��I���܂���."},
3472: {0x27, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3473: /*
3474: {0x32, "Network request not supported", NULL},
3475: {0x33, "Remote computer not listening", NULL},
3476: {0x34, "Duplicate name on network", NULL},
3477: {0x35, "Network name not found", NULL},
3478: {0x36, "Network busy", NULL},
3479: {0x37, "Network device no longer exists", NULL},
3480: {0x38, "Network BIOS command limit exceeded", NULL},
3481: {0x39, "Network adapter hardware error", NULL},
3482: {0x3A, "Incorrect response from network", NULL},
3483: {0x3B, "Unexpected network error", NULL},
3484: {0x3C, "Incompatible remote adapter", NULL},
3485: {0x3D, "Print queue full", NULL},
3486: {0x3E, "Queue not full", NULL},
3487: {0x3F, "Not enough space to print file", NULL},
3488: {0x40, "Network name was deleted", NULL},
3489: {0x41, "Network: Access denied", NULL},
3490: {0x42, "Network device type incorrect", NULL},
3491: {0x43, "Network name not found", NULL},
3492: {0x44, "Network name limit exceeded", NULL},
3493: {0x45, "Network BIOS session limit exceeded", NULL},
3494: {0x46, "Temporarily paused", NULL},
3495: {0x47, "Network request not accepted", NULL},
3496: {0x48, "Network print/disk redirection paused", NULL},
3497: {0x49, "Network software not installed", NULL},
3498: {0x4A, "Unexpected adapter close", NULL},
3499: */
3500: {0x50, "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33 root 3501: {0x52, "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32 root 3502: {0x53, "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
3503: {0x54, "Too many redirections", "���_�C���N�g���������܂�."},
3504: {0x55, "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
3505: {0x56, "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
3506: {0x57, "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
3507: {0x58, "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
3508: {0x59, "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
3509: {0x5A, "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
3510: /*
3511: {0x64, "Unknown error", "�s���ȃG���[�ł�."},
3512: {0x65, "Not ready", "�������ł��Ă��܂���."},
3513: {0x66, "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
3514: {0x67, "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
3515: {0x68, "Door open", "���o�[���܂��Ă��܂���."
3516: */
3517: {0xB0, "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
3518: {0xB1, "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
3519: {0xB2, "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
3520: {0xB4, "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
3521: {0xB5, "A valid eject request failed", "���o���Ɏ��s���܂���."},
3522: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3523: };
3524:
3525: static const struct {
3526: UINT16 code;
3527: char *message_english;
3528: char *message_japanese;
3529: } param_error_table[] = {
3530: {0x01, "Too many parameters", "�p�����[�^���������܂�."},
3531: {0x02, "Required parameter missing", "�p�����[�^������܂���."},
3532: {0x03, "Invalid switch", "�����ȃX�C�b�`�ł�."},
3533: {0x04, "Invalid keyword", "�����ȃL�[���[�h�ł�."},
3534: {0x06, "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂��Ă��܂�."},
3535: {0x07, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3536: {0x08, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3537: {0x09, "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
3538: {0x0A, "Invalid parameter", "�����ȃp�����[�^�ł�."},
3539: {0x0B, "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
3540: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3541: };
3542:
3543: static const struct {
3544: UINT16 code;
3545: char *message_english;
3546: char *message_japanese;
3547: } critical_error_table[] = {
3548: {0x00, "Write protect error", "�������ݕی�G���[�ł�."},
3549: {0x01, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3550: {0x02, "Not ready", "�������ł��Ă��܂���."},
3551: {0x03, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3552: {0x04, "Data error", "�f�[�^�G���[�ł�."},
3553: {0x05, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3554: {0x06, "Seek error", "�V�[�N�G���[�ł�."},
3555: {0x07, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3556: {0x08, "Sector not found", "�Z�N�^��������܂���."},
3557: {0x09, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3558: {0x0A, "Write fault error", "�������݃G���[�ł�."},
3559: {0x0B, "Read fault error", "�ǂݎ��G���[�ł�."},
3560: {0x0C, "General failure", "�G���[�ł�."},
3561: {0x0D, "Sharing violation", "���L�ᔽ�ł�."},
3562: {0x0E, "Lock violation", "���b�N�ᔽ�ł�."},
3563: {0x0F, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3564: {0x10, "FCB unavailable", "FCB �͎g���܂���."},
3565: {0x11, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3566: {0x12, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3567: {0x13, "Out of input", "���͂��I���܂���."},
3568: {0x14, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3569: {-1 , "Critical error", "�v���I�ȃG���[�ł�."},
3570: };
3571:
1.1.1.20 root 3572: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
3573: int msdos_psp_get_file_table(int fd, int psp_seg);
3574: void msdos_putch(UINT8 data);
1.1.1.50 root 3575: void msdos_putch_fast(UINT8 data);
1.1.1.35 root 3576: #ifdef USE_SERVICE_THREAD
3577: void msdos_putch_tmp(UINT8 data);
3578: #endif
1.1.1.45 root 3579: const char *msdos_short_path(const char *path);
1.1.1.44 root 3580: bool msdos_is_valid_drive(int drv);
3581: bool msdos_is_removable_drive(int drv);
3582: bool msdos_is_cdrom_drive(int drv);
3583: bool msdos_is_remote_drive(int drv);
3584: bool msdos_is_subst_drive(int drv);
1.1.1.20 root 3585:
1.1 root 3586: // process info
3587:
3588: process_t *msdos_process_info_create(UINT16 psp_seg)
3589: {
3590: for(int i = 0; i < MAX_PROCESS; i++) {
3591: if(process[i].psp == 0 || process[i].psp == psp_seg) {
3592: memset(&process[i], 0, sizeof(process_t));
3593: process[i].psp = psp_seg;
3594: return(&process[i]);
3595: }
3596: }
3597: fatalerror("too many processes\n");
3598: return(NULL);
3599: }
3600:
1.1.1.33 root 3601: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1 root 3602: {
3603: for(int i = 0; i < MAX_PROCESS; i++) {
3604: if(process[i].psp == psp_seg) {
3605: return(&process[i]);
3606: }
3607: }
1.1.1.33 root 3608: if(show_error) {
3609: fatalerror("invalid psp address\n");
3610: }
1.1 root 3611: return(NULL);
3612: }
3613:
1.1.1.33 root 3614: process_t *msdos_process_info_get(UINT16 psp_seg)
3615: {
3616: return(msdos_process_info_get(psp_seg, true));
3617: }
3618:
1.1.1.23 root 3619: void msdos_sda_update(int psp_seg)
3620: {
3621: sda_t *sda = (sda_t *)(mem + SDA_TOP);
3622:
3623: for(int i = 0; i < MAX_PROCESS; i++) {
3624: if(process[i].psp == psp_seg) {
3625: sda->switchar = process[i].switchar;
3626: sda->current_dta.w.l = process[i].dta.w.l;
3627: sda->current_dta.w.h = process[i].dta.w.h;
3628: sda->current_psp = process[i].psp;
3629: break;
3630: }
3631: }
3632: sda->malloc_strategy = malloc_strategy;
3633: sda->return_code = retval;
3634: sda->current_drive = _getdrive();
3635: }
3636:
1.1.1.13 root 3637: // dta info
3638:
3639: void msdos_dta_info_init()
3640: {
1.1.1.14 root 3641: for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13 root 3642: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3643: }
3644: }
3645:
3646: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
3647: {
3648: dtainfo_t *free_dta = NULL;
1.1.1.14 root 3649: for(int i = 0; i < MAX_DTAINFO; i++) {
3650: if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
3651: if(free_dta == NULL) {
1.1.1.13 root 3652: free_dta = &dtalist[i];
3653: }
1.1.1.14 root 3654: } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13 root 3655: return(&dtalist[i]);
3656: }
3657: }
1.1.1.14 root 3658: if(free_dta) {
1.1.1.13 root 3659: free_dta->psp = psp_seg;
3660: free_dta->dta = dta_laddr;
3661: return(free_dta);
3662: }
3663: fatalerror("too many dta\n");
3664: return(NULL);
3665: }
3666:
3667: void msdos_dta_info_free(UINT16 psp_seg)
3668: {
1.1.1.14 root 3669: for(int i = 0; i < MAX_DTAINFO; i++) {
3670: if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13 root 3671: FindClose(dtalist[i].find_handle);
3672: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3673: }
3674: }
3675: }
3676:
1.1 root 3677: void msdos_cds_update(int drv)
3678: {
1.1.1.44 root 3679: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1 root 3680:
1.1.1.44 root 3681: memset(cds, 0, 88);
3682:
3683: if(msdos_is_valid_drive(drv)) {
3684: char path[MAX_PATH];
3685: if(msdos_is_remote_drive(drv)) {
3686: cds->drive_attrib = 0xc000; // network drive
3687: } else if(msdos_is_subst_drive(drv)) {
3688: cds->drive_attrib = 0x5000; // subst drive
3689: } else {
3690: cds->drive_attrib = 0x4000; // physical drive
3691: }
3692: if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
3693: strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
3694: }
3695: }
3696: if(cds->path_name[0] == '\0') {
3697: sprintf(cds->path_name, "%c:\\", 'A' + drv);
3698: }
3699: cds->dpb_ptr.w.h = DPB_TOP >> 4;
3700: cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
3701: cds->word_1 = cds->word_2 = 0xffff;
3702: cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
3703: cds->bs_offset = 2;
3704: }
3705:
1.1.1.45 root 3706: void msdos_cds_update(int drv, const char *path)
1.1.1.44 root 3707: {
3708: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
3709:
3710: strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1 root 3711: }
3712:
1.1.1.17 root 3713: // nls information tables
3714:
3715: // uppercase table (func 6502h)
3716: void msdos_upper_table_update()
3717: {
3718: *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 3719: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 3720: UINT8 c[4];
1.1.1.33 root 3721: *(UINT32 *)c = 0; // reset internal conversion state
3722: CharUpperBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.17 root 3723: c[0] = 0x80 + i;
3724: DWORD rc = CharUpperBuffA((LPSTR)c, 1);
3725: mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
3726: }
3727: }
3728:
1.1.1.23 root 3729: // lowercase table (func 6503h)
3730: void msdos_lower_table_update()
3731: {
3732: *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
3733: for(unsigned i = 0; i < 0x80; ++i) {
3734: UINT8 c[4];
1.1.1.33 root 3735: *(UINT32 *)c = 0; // reset internal conversion state
3736: CharLowerBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.23 root 3737: c[0] = 0x80 + i;
3738: DWORD rc = CharLowerBuffA((LPSTR)c, 1);
3739: mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
3740: }
3741: }
3742:
1.1.1.17 root 3743: // filename uppercase table (func 6504h)
3744: void msdos_filename_upper_table_init()
3745: {
3746: // depended on (file)system, not on active codepage
3747: // temporary solution: just filling data
3748: *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 3749: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 3750: mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
3751: }
3752: }
3753:
3754: // filaname terminator table (func 6505h)
3755: void msdos_filename_terminator_table_init()
3756: {
3757: const char illegal_chars[] = ".\"/\\[]:|<>+=;,"; // for standard MS-DOS fs.
3758: UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
3759:
3760: data[2] = 1; // marker? (permissible character value)
3761: data[3] = 0x00; // 00h...FFh
3762: data[4] = 0xff;
3763: data[5] = 0; // marker? (excluded character)
3764: data[6] = 0x00; // 00h...20h
3765: data[7] = 0x20;
3766: data[8] = 2; // marker? (illegal characters for filename)
3767: data[9] = (UINT8)strlen(illegal_chars);
3768: memcpy(data + 10, illegal_chars, data[9]);
3769:
3770: // total length
3771: *(UINT16 *)data = (10 - 2) + data[9];
3772: }
3773:
3774: // collating table (func 6506h)
3775: void msdos_collating_table_update()
3776: {
3777: // temporary solution: just filling data
3778: *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22 root 3779: for(unsigned i = 0; i < 256; ++i) {
1.1.1.17 root 3780: mem[COLLATING_TABLE_TOP + 2 + i] = i;
3781: }
3782: }
3783:
1.1 root 3784: // dbcs
3785:
3786: void msdos_dbcs_table_update()
3787: {
3788: UINT8 dbcs_data[DBCS_SIZE];
3789: memset(dbcs_data, 0, sizeof(dbcs_data));
3790:
3791: CPINFO info;
3792: GetCPInfo(active_code_page, &info);
3793:
3794: if(info.MaxCharSize != 1) {
3795: for(int i = 0;; i += 2) {
3796: UINT8 lo = info.LeadByte[i + 0];
3797: UINT8 hi = info.LeadByte[i + 1];
3798: dbcs_data[2 + i + 0] = lo;
3799: dbcs_data[2 + i + 1] = hi;
3800: if(lo == 0 && hi == 0) {
3801: dbcs_data[0] = i + 2;
3802: break;
3803: }
3804: }
3805: } else {
3806: dbcs_data[0] = 2; // ???
3807: }
3808: memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
3809: }
3810:
1.1.1.17 root 3811: void msdos_dbcs_table_finish()
3812: {
1.1.1.32 root 3813: if(system_code_page != _getmbcp()) {
1.1.1.17 root 3814: _setmbcp(system_code_page);
3815: }
1.1.1.32 root 3816: if(console_code_page != GetConsoleCP()) {
3817: SetConsoleCP(console_code_page);
3818: SetConsoleOutputCP(console_code_page);
3819: }
1.1.1.17 root 3820: }
3821:
3822: void msdos_nls_tables_init()
1.1 root 3823: {
1.1.1.32 root 3824: active_code_page = console_code_page = GetConsoleCP();
3825: system_code_page = _getmbcp();
3826:
3827: if(active_code_page != system_code_page) {
3828: if(_setmbcp(active_code_page) != 0) {
3829: active_code_page = system_code_page;
3830: }
3831: }
3832:
1.1.1.17 root 3833: msdos_upper_table_update();
1.1.1.23 root 3834: msdos_lower_table_update();
1.1.1.17 root 3835: msdos_filename_terminator_table_init();
3836: msdos_filename_upper_table_init();
3837: msdos_collating_table_update();
1.1 root 3838: msdos_dbcs_table_update();
3839: }
3840:
1.1.1.17 root 3841: void msdos_nls_tables_update()
1.1 root 3842: {
1.1.1.17 root 3843: msdos_dbcs_table_update();
3844: msdos_upper_table_update();
1.1.1.23 root 3845: msdos_lower_table_update();
3846: // msdos_collating_table_update();
1.1 root 3847: }
3848:
3849: int msdos_lead_byte_check(UINT8 code)
3850: {
3851: UINT8 *dbcs_table = mem + DBCS_TABLE;
3852:
3853: for(int i = 0;; i += 2) {
3854: UINT8 lo = dbcs_table[i + 0];
3855: UINT8 hi = dbcs_table[i + 1];
3856: if(lo == 0 && hi == 0) {
3857: break;
3858: }
3859: if(lo <= code && code <= hi) {
3860: return(1);
3861: }
3862: }
3863: return(0);
3864: }
3865:
1.1.1.20 root 3866: int msdos_ctrl_code_check(UINT8 code)
3867: {
1.1.1.22 root 3868: return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20 root 3869: }
3870:
1.1.1.36 root 3871: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
3872: {
3873: int is_kanji_1st = 0;
3874: int is_kanji_2nd = 0;
3875:
3876: for(int p = 0;; p++) {
3877: if(is_kanji_1st) {
3878: is_kanji_1st = 0;
3879: is_kanji_2nd = 1;
3880: } else if(msdos_lead_byte_check(buf[p])) {
3881: is_kanji_1st = 1;
3882: }
3883: if(p == n) {
3884: return(is_kanji_2nd);
3885: }
3886: is_kanji_2nd = 0;
3887: }
3888: }
3889:
1.1 root 3890: // file control
3891:
1.1.1.45 root 3892: const char *msdos_remove_double_quote(const char *path)
1.1.1.14 root 3893: {
3894: static char tmp[MAX_PATH];
3895:
3896: if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45 root 3897: memset(tmp, 0, sizeof(tmp));
1.1.1.14 root 3898: memcpy(tmp, path + 1, strlen(path) - 2);
3899: } else {
3900: strcpy(tmp, path);
3901: }
3902: return(tmp);
3903: }
3904:
1.1.1.45 root 3905: const char *msdos_remove_end_separator(const char *path)
1.1.1.32 root 3906: {
3907: static char tmp[MAX_PATH];
3908:
3909: strcpy(tmp, path);
1.1.1.45 root 3910:
3911: // for example "C:\" case, the end separator should not be removed
3912: if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
3913: tmp[strlen(tmp) - 1] = '\0';
1.1.1.32 root 3914: }
3915: return(tmp);
3916: }
3917:
1.1.1.45 root 3918: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14 root 3919: {
3920: static char tmp[MAX_PATH];
1.1.1.45 root 3921: const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14 root 3922:
3923: if(strlen(tmp_dir) == 0) {
3924: strcpy(tmp, file);
3925: } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
3926: sprintf(tmp, "%s%s", tmp_dir, file);
3927: } else {
3928: sprintf(tmp, "%s\\%s", tmp_dir, file);
3929: }
3930: return(tmp);
3931: }
3932:
1.1.1.45 root 3933: const char *msdos_trimmed_path(const char *path, int lfn)
1.1 root 3934: {
3935: static char tmp[MAX_PATH];
3936:
3937: if(lfn) {
3938: strcpy(tmp, path);
3939: } else {
3940: // remove space in the path
1.1.1.45 root 3941: const char *src = path;
3942: char *dst = tmp;
1.1 root 3943:
3944: while(*src != '\0') {
3945: if(msdos_lead_byte_check(*src)) {
3946: *dst++ = *src++;
3947: *dst++ = *src++;
3948: } else if(*src != ' ') {
3949: *dst++ = *src++;
3950: } else {
3951: src++; // skip space
3952: }
3953: }
3954: *dst = '\0';
3955: }
1.1.1.14 root 3956: if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
3957: // redirect C:\COMMAND.COM to comspec_path
3958: strcpy(tmp, comspec_path);
3959: } else if(is_vista_or_later && _access(tmp, 0) != 0) {
3960: // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
3961: static int root_drive_protected = -1;
3962: char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
3963: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
3964:
3965: if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
3966: name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
3967: strcpy(name, name_temp);
3968: name_temp[0] = '\0';
3969:
3970: if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
3971: (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
3972: if(root_drive_protected == -1) {
3973: FILE *fp = NULL;
3974:
3975: sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
3976: root_drive_protected = 1;
3977: try {
3978: if((fp = fopen(temp, "w")) != NULL) {
3979: if(fprintf(fp, "TEST") == 4) {
3980: root_drive_protected = 0;
3981: }
3982: }
3983: } catch(...) {
3984: }
3985: if(fp != NULL) {
3986: fclose(fp);
3987: }
3988: if(_access(temp, 0) == 0) {
3989: remove(temp);
3990: }
3991: }
3992: if(root_drive_protected == 1) {
3993: if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
3994: GetEnvironmentVariable("TMP", temp, MAX_PATH) != 0) {
3995: strcpy(tmp, msdos_combine_path(temp, name));
3996: }
3997: }
3998: }
3999: }
4000: }
1.1 root 4001: return(tmp);
4002: }
4003:
1.1.1.45 root 4004: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28 root 4005: {
1.1.1.32 root 4006: // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28 root 4007: static char env_path[ENV_SIZE];
4008: char tmp[ENV_SIZE], *token;
4009:
4010: memset(env_path, 0, sizeof(env_path));
4011: strcpy(tmp, src);
4012: token = my_strtok(tmp, ";");
4013:
4014: while(token != NULL) {
4015: if(token[0] != '\0') {
1.1.1.45 root 4016: const char *path = msdos_remove_double_quote(token);
4017: char short_path[MAX_PATH];
1.1.1.32 root 4018: if(path != NULL && strlen(path) != 0) {
4019: if(env_path[0] != '\0') {
4020: strcat(env_path, ";");
4021: }
1.1.1.28 root 4022: if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32 root 4023: strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28 root 4024: } else {
4025: my_strupr(short_path);
1.1.1.32 root 4026: strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28 root 4027: }
4028: }
4029: }
4030: token = my_strtok(NULL, ";");
4031: }
4032: return(env_path);
4033: }
4034:
1.1.1.45 root 4035: bool match(const char *text, const char *pattern)
1.1 root 4036: {
1.1.1.24 root 4037: // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14 root 4038: switch(*pattern) {
1.1 root 4039: case '\0':
4040: return !*text;
4041: case '*':
1.1.1.14 root 4042: return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1 root 4043: case '?':
4044: return *text && match(text + 1, pattern + 1);
4045: default:
4046: return (*text == *pattern) && match(text + 1, pattern + 1);
4047: }
4048: }
4049:
1.1.1.45 root 4050: bool msdos_match_volume_label(const char *path, const char *volume)
1.1 root 4051: {
1.1.1.45 root 4052: const char *p = NULL;
1.1 root 4053:
1.1.1.14 root 4054: if(!*volume) {
4055: return false;
4056: } else if((p = my_strchr(path, ':')) != NULL) {
1.1 root 4057: return msdos_match_volume_label(p + 1, volume);
4058: } else if((p = my_strchr(path, '\\')) != NULL) {
4059: return msdos_match_volume_label(p + 1, volume);
4060: } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14 root 4061: char tmp[MAX_PATH];
4062: sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
4063: return match(volume, tmp);
1.1 root 4064: } else {
4065: return match(volume, path);
4066: }
4067: }
4068:
1.1.1.45 root 4069: const char *msdos_fcb_path(fcb_t *fcb)
1.1 root 4070: {
4071: static char tmp[MAX_PATH];
4072: char name[9], ext[4];
4073:
4074: memset(name, 0, sizeof(name));
4075: memcpy(name, fcb->file_name, 8);
4076: strcpy(name, msdos_trimmed_path(name, 0));
4077:
4078: memset(ext, 0, sizeof(ext));
4079: memcpy(ext, fcb->file_name + 8, 3);
4080: strcpy(ext, msdos_trimmed_path(ext, 0));
4081:
4082: if(name[0] == '\0' || strcmp(name, "????????") == 0) {
4083: strcpy(name, "*");
4084: }
4085: if(ext[0] == '\0') {
4086: strcpy(tmp, name);
4087: } else {
4088: if(strcmp(ext, "???") == 0) {
4089: strcpy(ext, "*");
4090: }
4091: sprintf(tmp, "%s.%s", name, ext);
4092: }
4093: return(tmp);
4094: }
4095:
1.1.1.45 root 4096: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1 root 4097: {
4098: char *ext = my_strchr(path, '.');
4099:
4100: memset(fcb->file_name, 0x20, 8 + 3);
4101: if(ext != NULL && path[0] != '.') {
4102: *ext = '\0';
4103: memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
4104: }
4105: memcpy(fcb->file_name, path, strlen(path));
4106: }
4107:
1.1.1.45 root 4108: const char *msdos_short_path(const char *path)
1.1 root 4109: {
4110: static char tmp[MAX_PATH];
4111:
1.1.1.24 root 4112: if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
4113: strcpy(tmp, path);
4114: }
1.1 root 4115: my_strupr(tmp);
4116: return(tmp);
4117: }
4118:
1.1.1.45 root 4119: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13 root 4120: {
4121: static char tmp[MAX_PATH];
1.1.1.45 root 4122:
1.1.1.14 root 4123: if(fd->cAlternateFileName[0]) {
1.1.1.13 root 4124: strcpy(tmp, fd->cAlternateFileName);
4125: } else {
4126: strcpy(tmp, fd->cFileName);
4127: }
4128: my_strupr(tmp);
4129: return(tmp);
4130: }
4131:
1.1.1.45 root 4132: const char *msdos_short_full_path(const char *path)
1.1 root 4133: {
4134: static char tmp[MAX_PATH];
4135: char full[MAX_PATH], *name;
4136:
1.1.1.14 root 4137: // Full works with non-existent files, but Short does not
1.1 root 4138: GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14 root 4139: *tmp = '\0';
4140: if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
4141: name[-1] = '\0';
4142: DWORD len = GetShortPathName(full, tmp, MAX_PATH);
4143: if(len == 0) {
4144: strcpy(tmp, full);
4145: } else {
4146: tmp[len++] = '\\';
4147: strcpy(tmp + len, name);
4148: }
4149: }
1.1 root 4150: my_strupr(tmp);
4151: return(tmp);
4152: }
4153:
1.1.1.45 root 4154: const char *msdos_short_full_dir(const char *path)
1.1 root 4155: {
4156: static char tmp[MAX_PATH];
4157: char full[MAX_PATH], *name;
4158:
4159: GetFullPathName(path, MAX_PATH, full, &name);
4160: name[-1] = '\0';
1.1.1.24 root 4161: if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
4162: strcpy(tmp, full);
4163: }
1.1 root 4164: my_strupr(tmp);
4165: return(tmp);
4166: }
4167:
1.1.1.45 root 4168: const char *msdos_local_file_path(const char *path, int lfn)
1.1 root 4169: {
1.1.1.45 root 4170: static char trimmed[MAX_PATH];
4171:
4172: strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14 root 4173: #if 0
4174: // I have forgotten the reason of this routine... :-(
1.1 root 4175: if(_access(trimmed, 0) != 0) {
4176: process_t *process = msdos_process_info_get(current_psp);
4177: static char tmp[MAX_PATH];
4178:
4179: sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
4180: if(_access(tmp, 0) == 0) {
4181: return(tmp);
4182: }
4183: }
1.1.1.14 root 4184: #endif
1.1 root 4185: return(trimmed);
4186: }
4187:
1.1.1.45 root 4188: bool msdos_is_device_path(const char *path)
1.1.1.11 root 4189: {
4190: char full[MAX_PATH], *name;
4191:
1.1.1.24 root 4192: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4193: if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
4194: _stricmp(full, "\\\\.\\CON" ) == 0 ||
4195: _stricmp(full, "\\\\.\\NUL" ) == 0 ||
4196: _stricmp(full, "\\\\.\\PRN" ) == 0 ||
4197: _stricmp(full, "\\\\.\\COM1") == 0 ||
4198: _stricmp(full, "\\\\.\\COM2") == 0 ||
4199: _stricmp(full, "\\\\.\\COM3") == 0 ||
4200: _stricmp(full, "\\\\.\\COM4") == 0 ||
4201: _stricmp(full, "\\\\.\\COM5") == 0 ||
4202: _stricmp(full, "\\\\.\\COM6") == 0 ||
4203: _stricmp(full, "\\\\.\\COM7") == 0 ||
4204: _stricmp(full, "\\\\.\\COM8") == 0 ||
4205: _stricmp(full, "\\\\.\\COM9") == 0 ||
4206: _stricmp(full, "\\\\.\\LPT1") == 0 ||
4207: _stricmp(full, "\\\\.\\LPT2") == 0 ||
4208: _stricmp(full, "\\\\.\\LPT3") == 0 ||
4209: _stricmp(full, "\\\\.\\LPT4") == 0 ||
4210: _stricmp(full, "\\\\.\\LPT5") == 0 ||
4211: _stricmp(full, "\\\\.\\LPT6") == 0 ||
4212: _stricmp(full, "\\\\.\\LPT7") == 0 ||
4213: _stricmp(full, "\\\\.\\LPT8") == 0 ||
4214: _stricmp(full, "\\\\.\\LPT9") == 0) {
4215: return(true);
4216: } else if(name != NULL) {
4217: if(_stricmp(name, "CLOCK$" ) == 0 ||
4218: _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30 root 4219: _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43 root 4220: // _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30 root 4221: _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29 root 4222: return(true);
4223: }
4224: }
1.1.1.24 root 4225: }
4226: return(false);
1.1.1.11 root 4227: }
4228:
1.1.1.45 root 4229: bool msdos_is_con_path(const char *path)
1.1.1.8 root 4230: {
1.1.1.14 root 4231: char full[MAX_PATH], *name;
1.1.1.8 root 4232:
1.1.1.24 root 4233: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4234: return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24 root 4235: }
4236: return(false);
4237: }
4238:
1.1.1.45 root 4239: int msdos_is_comm_path(const char *path)
1.1.1.24 root 4240: {
4241: char full[MAX_PATH], *name;
4242:
4243: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4244: if(_stricmp(full, "\\\\.\\COM1") == 0) {
4245: return(1);
4246: } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
4247: return(2);
4248: } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
4249: return(3);
4250: } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
4251: return(4);
1.1.1.24 root 4252: }
4253: }
1.1.1.29 root 4254: return(0);
4255: }
4256:
1.1.1.45 root 4257: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37 root 4258: {
4259: // 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 4260: const char *p = NULL;
1.1.1.37 root 4261:
4262: if((p = strstr(path, ":")) != NULL) {
4263: UINT8 selector = sio_read(sio_port - 1, 3);
4264:
4265: // baud rate
4266: int baud = max(110, min(9600, atoi(p + 1)));
4267: UINT16 divisor = 115200 / baud;
4268:
4269: if((p = strstr(p + 1, ",")) != NULL) {
4270: // parity
4271: if(p[1] == 'N' || p[1] == 'n') {
4272: selector = (selector & ~0x38) | 0x00;
4273: } else if(p[1] == 'O' || p[1] == 'o') {
4274: selector = (selector & ~0x38) | 0x08;
4275: } else if(p[1] == 'E' || p[1] == 'e') {
4276: selector = (selector & ~0x38) | 0x18;
4277: } else if(p[1] == 'M' || p[1] == 'm') {
4278: selector = (selector & ~0x38) | 0x28;
4279: } else if(p[1] == 'S' || p[1] == 's') {
4280: selector = (selector & ~0x38) | 0x38;
4281: }
4282: if((p = strstr(p + 1, ",")) != NULL) {
4283: // word length
4284: if(p[1] == '8') {
4285: selector = (selector & ~0x03) | 0x03;
4286: } else if(p[1] == '7') {
4287: selector = (selector & ~0x03) | 0x02;
4288: } else if(p[1] == '6') {
4289: selector = (selector & ~0x03) | 0x01;
4290: } else if(p[1] == '5') {
4291: selector = (selector & ~0x03) | 0x00;
4292: }
4293: if((p = strstr(p + 1, ",")) != NULL) {
4294: // stop bits
4295: float bits = atof(p + 1);
4296: if(bits > 1.0F) {
4297: selector |= 0x04;
4298: } else {
4299: selector &= ~0x04;
4300: }
4301: }
4302: }
4303: }
4304: sio_write(sio_port - 1, 3, selector | 0x80);
4305: sio_write(sio_port - 1, 0, divisor & 0xff);
4306: sio_write(sio_port - 1, 1, divisor >> 8);
4307: sio_write(sio_port - 1, 3, selector);
4308: }
4309: }
4310:
1.1.1.45 root 4311: int msdos_is_prn_path(const char *path)
1.1.1.30 root 4312: {
4313: char full[MAX_PATH], *name;
4314:
4315: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
4316: if(_stricmp(full, "\\\\.\\PRN") == 0) {
4317: return(1);
4318: } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
4319: return(1);
4320: } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
4321: return(2);
4322: } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
4323: return(3);
4324: }
4325: }
4326: return(0);
4327: }
4328:
1.1.1.44 root 4329: bool msdos_is_valid_drive(int drv)
4330: {
4331: return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
4332: }
4333:
4334: bool msdos_is_removable_drive(int drv)
4335: {
4336: char volume[] = "A:\\";
4337:
4338: volume[0] = 'A' + drv;
4339:
4340: return(GetDriveType(volume) == DRIVE_REMOVABLE);
4341: }
4342:
4343: bool msdos_is_cdrom_drive(int drv)
4344: {
4345: char volume[] = "A:\\";
4346:
4347: volume[0] = 'A' + drv;
4348:
4349: return(GetDriveType(volume) == DRIVE_CDROM);
4350: }
4351:
4352: bool msdos_is_remote_drive(int drv)
4353: {
4354: char volume[] = "A:\\";
4355:
4356: volume[0] = 'A' + drv;
4357:
4358: return(GetDriveType(volume) == DRIVE_REMOTE);
4359: }
4360:
4361: bool msdos_is_subst_drive(int drv)
4362: {
4363: char device[] = "A:", path[MAX_PATH];
4364:
4365: device[0] = 'A' + drv;
4366:
4367: if(QueryDosDevice(device, path, MAX_PATH)) {
4368: if(strncmp(path, "\\??\\", 4) == 0) {
4369: return(true);
4370: }
4371: }
4372: return(false);
4373: }
4374:
1.1.1.45 root 4375: bool msdos_is_existing_file(const char *path)
1.1.1.24 root 4376: {
4377: // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
4378: WIN32_FIND_DATA FindData;
4379: HANDLE hFind;
4380:
4381: if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
4382: FindClose(hFind);
4383: return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
4384: }
4385: return(false);
1.1.1.8 root 4386: }
4387:
1.1.1.45 root 4388: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9 root 4389: {
4390: static char tmp[MAX_PATH];
1.1.1.28 root 4391: char path[ENV_SIZE], *file_name;
1.1.1.9 root 4392:
1.1.1.28 root 4393: // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9 root 4394: if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
4395: sprintf(file_name, "COMMAND.COM");
4396: if(_access(tmp, 0) == 0) {
4397: return(tmp);
4398: }
4399: }
1.1.1.28 root 4400:
4401: // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9 root 4402: if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
4403: sprintf(file_name, "COMMAND.COM");
4404: if(_access(tmp, 0) == 0) {
4405: return(tmp);
4406: }
4407: }
1.1.1.28 root 4408:
4409: // check if COMMAND.COM is in the current directory
1.1.1.9 root 4410: if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
4411: if(_access(tmp, 0) == 0) {
4412: return(tmp);
4413: }
4414: }
1.1.1.28 root 4415:
4416: // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
4417: strcpy(path, env_path);
4418: char *token = my_strtok(path, ";");
1.1.1.9 root 4419: while(token != NULL) {
1.1.1.14 root 4420: if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9 root 4421: strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
4422: if(_access(tmp, 0) == 0) {
4423: return(tmp);
4424: }
4425: }
4426: token = my_strtok(NULL, ";");
4427: }
4428: return(NULL);
4429: }
4430:
1.1.1.14 root 4431: int msdos_drive_number(const char *path)
1.1 root 4432: {
4433: char tmp[MAX_PATH], *name;
4434:
1.1.1.45 root 4435: if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
4436: if(tmp[0] >= 'a' && tmp[0] <= 'z') {
4437: return(tmp[0] - 'a');
4438: } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
4439: return(tmp[0] - 'A');
4440: }
1.1 root 4441: }
1.1.1.45 root 4442: // return(msdos_drive_number("."));
4443: return(_getdrive() - 1);
1.1 root 4444: }
4445:
1.1.1.45 root 4446: const char *msdos_volume_label(const char *path)
1.1 root 4447: {
4448: static char tmp[MAX_PATH];
4449: char volume[] = "A:\\";
4450:
4451: if(path[1] == ':') {
4452: volume[0] = path[0];
4453: } else {
4454: volume[0] = 'A' + _getdrive() - 1;
4455: }
4456: if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
4457: memset(tmp, 0, sizeof(tmp));
4458: }
4459: return(tmp);
4460: }
4461:
1.1.1.45 root 4462: const char *msdos_short_volume_label(const char *label)
1.1 root 4463: {
4464: static char tmp[(8 + 1 + 3) + 1];
1.1.1.45 root 4465: const char *src = label;
1.1 root 4466: int remain = strlen(label);
4467: char *dst_n = tmp;
4468: char *dst_e = tmp + 9;
4469:
4470: strcpy(tmp, " . ");
4471: for(int i = 0; i < 8 && remain > 0; i++) {
4472: if(msdos_lead_byte_check(*src)) {
4473: if(++i == 8) {
4474: break;
4475: }
4476: *dst_n++ = *src++;
4477: remain--;
4478: }
4479: *dst_n++ = *src++;
4480: remain--;
4481: }
4482: if(remain > 0) {
4483: for(int i = 0; i < 3 && remain > 0; i++) {
4484: if(msdos_lead_byte_check(*src)) {
4485: if(++i == 3) {
4486: break;
4487: }
4488: *dst_e++ = *src++;
4489: remain--;
4490: }
4491: *dst_e++ = *src++;
4492: remain--;
4493: }
4494: *dst_e = '\0';
4495: } else {
4496: *dst_n = '\0';
4497: }
4498: my_strupr(tmp);
4499: return(tmp);
4500: }
4501:
1.1.1.13 root 4502: errno_t msdos_maperr(unsigned long oserrno)
4503: {
4504: _doserrno = oserrno;
1.1.1.14 root 4505: switch(oserrno) {
1.1.1.13 root 4506: case ERROR_FILE_NOT_FOUND: // 2
4507: case ERROR_PATH_NOT_FOUND: // 3
4508: case ERROR_INVALID_DRIVE: // 15
4509: case ERROR_NO_MORE_FILES: // 18
4510: case ERROR_BAD_NETPATH: // 53
4511: case ERROR_BAD_NET_NAME: // 67
4512: case ERROR_BAD_PATHNAME: // 161
4513: case ERROR_FILENAME_EXCED_RANGE: // 206
4514: return ENOENT;
4515: case ERROR_TOO_MANY_OPEN_FILES: // 4
4516: return EMFILE;
4517: case ERROR_ACCESS_DENIED: // 5
4518: case ERROR_CURRENT_DIRECTORY: // 16
4519: case ERROR_NETWORK_ACCESS_DENIED: // 65
4520: case ERROR_CANNOT_MAKE: // 82
4521: case ERROR_FAIL_I24: // 83
4522: case ERROR_DRIVE_LOCKED: // 108
4523: case ERROR_SEEK_ON_DEVICE: // 132
4524: case ERROR_NOT_LOCKED: // 158
4525: case ERROR_LOCK_FAILED: // 167
4526: return EACCES;
4527: case ERROR_INVALID_HANDLE: // 6
4528: case ERROR_INVALID_TARGET_HANDLE: // 114
4529: case ERROR_DIRECT_ACCESS_HANDLE: // 130
4530: return EBADF;
4531: case ERROR_ARENA_TRASHED: // 7
4532: case ERROR_NOT_ENOUGH_MEMORY: // 8
4533: case ERROR_INVALID_BLOCK: // 9
4534: case ERROR_NOT_ENOUGH_QUOTA: // 1816
4535: return ENOMEM;
4536: case ERROR_BAD_ENVIRONMENT: // 10
4537: return E2BIG;
4538: case ERROR_BAD_FORMAT: // 11
4539: return ENOEXEC;
4540: case ERROR_NOT_SAME_DEVICE: // 17
4541: return EXDEV;
4542: case ERROR_FILE_EXISTS: // 80
4543: case ERROR_ALREADY_EXISTS: // 183
4544: return EEXIST;
4545: case ERROR_NO_PROC_SLOTS: // 89
4546: case ERROR_MAX_THRDS_REACHED: // 164
4547: case ERROR_NESTING_NOT_ALLOWED: // 215
4548: return EAGAIN;
4549: case ERROR_BROKEN_PIPE: // 109
4550: return EPIPE;
4551: case ERROR_DISK_FULL: // 112
4552: return ENOSPC;
4553: case ERROR_WAIT_NO_CHILDREN: // 128
4554: case ERROR_CHILD_NOT_COMPLETE: // 129
4555: return ECHILD;
4556: case ERROR_DIR_NOT_EMPTY: // 145
4557: return ENOTEMPTY;
4558: }
1.1.1.14 root 4559: if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13 root 4560: oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
4561: return EACCES;
4562: }
1.1.1.14 root 4563: if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13 root 4564: oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
4565: return ENOEXEC;
4566: }
4567: return EINVAL;
4568: }
4569:
1.1.1.45 root 4570: int msdos_open(const char *path, int oflag)
1.1.1.13 root 4571: {
1.1.1.14 root 4572: if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45 root 4573: return(_open(path, oflag));
1.1.1.13 root 4574: }
1.1.1.14 root 4575:
4576: SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13 root 4577: DWORD disposition;
1.1.1.14 root 4578: switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
4579: default:
1.1.1.13 root 4580: case _O_EXCL:
4581: disposition = OPEN_EXISTING;
4582: break;
4583: case _O_CREAT:
4584: disposition = OPEN_ALWAYS;
4585: break;
4586: case _O_CREAT | _O_EXCL:
4587: case _O_CREAT | _O_TRUNC | _O_EXCL:
4588: disposition = CREATE_NEW;
4589: break;
4590: case _O_TRUNC:
4591: case _O_TRUNC | _O_EXCL:
4592: disposition = TRUNCATE_EXISTING;
4593: break;
4594: case _O_CREAT | _O_TRUNC:
4595: disposition = CREATE_ALWAYS;
4596: break;
4597: }
1.1.1.14 root 4598:
1.1.1.45 root 4599: HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13 root 4600: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4601: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4602: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4603: // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
4604: // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45 root 4605: h = CreateFile(path, GENERIC_READ,
1.1.1.13 root 4606: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4607: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4608: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4609: errno = msdos_maperr(GetLastError());
1.1.1.45 root 4610: return(-1);
1.1.1.13 root 4611: }
4612: }
1.1.1.14 root 4613:
1.1.1.13 root 4614: int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14 root 4615: if(fd == -1) {
1.1.1.13 root 4616: CloseHandle(h);
4617: }
1.1.1.45 root 4618: return(fd);
4619: }
4620:
4621: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
4622: {
4623: int fd = -1;
4624:
4625: *sio_port = *lpt_port = 0;
4626:
4627: if(msdos_is_con_path(path)) {
4628: // MODE.COM opens CON device with read/write mode :-(
4629: if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
4630: oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
4631: oflag |= _O_RDONLY;
4632: }
4633: if((fd = msdos_open("CON", oflag)) == -1) {
4634: // fd = msdos_open("NUL", oflag);
4635: }
4636: } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
4637: fd = msdos_open("NUL", oflag);
4638: msdos_set_comm_params(*sio_port, path);
4639: } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
4640: fd = msdos_open("NUL", oflag);
4641: } else if(msdos_is_device_path(path)) {
4642: fd = msdos_open("NUL", oflag);
4643: // } else if(oflag & _O_CREAT) {
4644: // fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
4645: // } else {
4646: // fd = _open(path, oflag);
4647: }
4648: return(fd);
4649: }
4650:
4651: UINT16 msdos_device_info(const char *path)
4652: {
4653: if(msdos_is_con_path(path)) {
4654: return(0x80d3);
4655: } else if(msdos_is_comm_path(path)) {
4656: return(0x80a0);
4657: } else if(msdos_is_prn_path(path)) {
4658: // return(0xa8c0);
4659: return(0x80a0);
4660: } else if(msdos_is_device_path(path)) {
4661: if(strstr(path, "EMMXXXX0") != NULL) {
4662: return(0xc0c0);
4663: } else if(strstr(path, "MSCD001") != NULL) {
4664: return(0xc880);
4665: } else {
4666: return(0x8084);
4667: }
4668: } else {
4669: return(msdos_drive_number(path));
4670: }
1.1.1.13 root 4671: }
4672:
1.1.1.37 root 4673: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port, int lpt_port)
1.1 root 4674: {
4675: static int id = 0;
4676: char full[MAX_PATH], *name;
4677:
4678: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
4679: strcpy(file_handler[fd].path, full);
4680: } else {
4681: strcpy(file_handler[fd].path, path);
4682: }
1.1.1.14 root 4683: // isatty makes no distinction between CON & NUL
4684: // GetFileSize fails on CON, succeeds on NUL
4685: if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45 root 4686: if(info == 0x80d3) {
4687: info = 0x8084;
4688: }
1.1.1.14 root 4689: atty = 0;
4690: } else if(!atty && info == 0x80d3) {
1.1.1.45 root 4691: // info = msdos_drive_number(".");
4692: info = msdos_drive_number(path);
1.1.1.14 root 4693: }
1.1 root 4694: file_handler[fd].valid = 1;
4695: file_handler[fd].id = id++; // dummy id for int 21h ax=71a6h
1.1.1.37 root 4696: file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1 root 4697: file_handler[fd].mode = mode;
4698: file_handler[fd].info = info;
4699: file_handler[fd].psp = psp_seg;
1.1.1.37 root 4700: file_handler[fd].sio_port = sio_port;
4701: file_handler[fd].lpt_port = lpt_port;
1.1.1.21 root 4702:
4703: // init system file table
4704: if(fd < 20) {
4705: UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
4706:
4707: memset(sft, 0, 0x3b);
4708:
4709: *(UINT16 *)(sft + 0x00) = 1;
4710: *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
4711: *(UINT8 *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
4712: *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
4713:
4714: if(!(file_handler[fd].info & 0x80)) {
4715: *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
4716: *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
4717:
4718: FILETIME time, local;
4719: HANDLE hHandle;
4720: WORD dos_date = 0, dos_time = 0;
4721: DWORD file_size = 0;
4722: if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
4723: if(GetFileTime(hHandle, NULL, NULL, &time)) {
4724: FileTimeToLocalFileTime(&time, &local);
4725: FileTimeToDosDateTime(&local, &dos_date, &dos_time);
4726: }
4727: file_size = GetFileSize(hHandle, NULL);
4728: }
4729: *(UINT16 *)(sft + 0x0d) = dos_time;
4730: *(UINT16 *)(sft + 0x0f) = dos_date;
4731: *(UINT32 *)(sft + 0x11) = file_size;
4732: }
4733:
4734: char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
4735: _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
4736: my_strupr(fname);
4737: my_strupr(ext);
4738: memset(sft + 0x20, 0x20, 11);
4739: memcpy(sft + 0x20, fname, min(strlen(fname), 8));
4740: memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
4741:
4742: *(UINT16 *)(sft + 0x31) = psp_seg;
4743: }
1.1 root 4744: }
4745:
1.1.1.37 root 4746: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
4747: {
4748: msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
4749: }
4750:
1.1 root 4751: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
4752: {
4753: strcpy(file_handler[dst].path, file_handler[src].path);
4754: file_handler[dst].valid = 1;
4755: file_handler[dst].id = file_handler[src].id;
4756: file_handler[dst].atty = file_handler[src].atty;
4757: file_handler[dst].mode = file_handler[src].mode;
4758: file_handler[dst].info = file_handler[src].info;
4759: file_handler[dst].psp = psp_seg;
1.1.1.37 root 4760: file_handler[dst].sio_port = file_handler[src].sio_port;
4761: file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1 root 4762: }
4763:
1.1.1.20 root 4764: void msdos_file_handler_close(int fd)
1.1 root 4765: {
4766: file_handler[fd].valid = 0;
1.1.1.21 root 4767:
4768: if(fd < 20) {
4769: memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
4770: }
1.1 root 4771: }
4772:
1.1.1.14 root 4773: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1 root 4774: {
1.1.1.14 root 4775: return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
4776: FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
4777: FILE_ATTRIBUTE_DIRECTORY));
1.1 root 4778: }
4779:
4780: // find file
4781:
4782: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
4783: {
4784: if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
4785: return(0); // search directory only !!!
4786: } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
4787: return(0);
4788: } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
4789: return(0);
4790: } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
4791: return(0);
4792: } else if((attribute & required_mask) != required_mask) {
4793: return(0);
4794: } else {
4795: return(1);
4796: }
4797: }
4798:
1.1.1.13 root 4799: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
4800: {
1.1.1.14 root 4801: if(fd->cAlternateFileName[0]) {
1.1.1.42 root 4802: return(1);
1.1.1.13 root 4803: }
4804: size_t len = strlen(fd->cFileName);
1.1.1.14 root 4805: if(len > 12) {
1.1.1.42 root 4806: return(0);
1.1.1.13 root 4807: }
4808: const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14 root 4809: if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42 root 4810: return(0);
1.1.1.13 root 4811: }
1.1.1.42 root 4812: return(1);
1.1.1.13 root 4813: }
4814:
1.1 root 4815: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
4816: {
4817: FILETIME local;
4818:
4819: FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
4820: fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
4821: fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
4822:
4823: FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
4824: fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
4825: fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
4826:
4827: FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
4828: fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
4829: fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
4830: }
4831:
4832: // i/o
4833:
4834: void msdos_stdio_reopen()
4835: {
4836: if(!file_handler[0].valid) {
4837: _dup2(DUP_STDIN, 0);
4838: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
4839: }
4840: if(!file_handler[1].valid) {
4841: _dup2(DUP_STDOUT, 1);
4842: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
4843: }
4844: if(!file_handler[2].valid) {
4845: _dup2(DUP_STDERR, 2);
4846: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
4847: }
1.1.1.21 root 4848: if(!file_handler[3].valid) {
4849: _dup2(DUP_STDAUX, 3);
4850: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
4851: }
4852: if(!file_handler[4].valid) {
4853: _dup2(DUP_STDPRN, 4);
1.1.1.45 root 4854: // msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
4855: msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21 root 4856: }
4857: for(int i = 0; i < 5; i++) {
4858: if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
4859: msdos_psp_set_file_table(i, i, current_psp);
4860: }
4861: }
1.1 root 4862: }
4863:
1.1.1.37 root 4864: int msdos_read(int fd, void *buffer, unsigned int count)
4865: {
4866: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
4867: // read from serial port
4868: int read = 0;
4869: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
4870: UINT8 *buf = (UINT8 *)buffer;
4871: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
4872: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 4873: DWORD timeout = timeGetTime() + 1000;
4874: while(read < count) {
4875: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
4876: buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
4877: timeout = timeGetTime() + 1000;
4878: } else {
4879: if(timeGetTime() > timeout) {
4880: break;
4881: }
4882: Sleep(10);
1.1.1.37 root 4883: }
4884: }
4885: sio_write(file_handler[fd].sio_port - 1, 3, selector);
4886: }
4887: return(read);
4888: }
4889: return(_read(fd, buffer, count));
4890: }
4891:
1.1 root 4892: int msdos_kbhit()
4893: {
4894: msdos_stdio_reopen();
4895:
1.1.1.20 root 4896: process_t *process = msdos_process_info_get(current_psp);
4897: int fd = msdos_psp_get_file_table(0, current_psp);
4898:
4899: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 4900: // stdin is redirected to file
1.1.1.20 root 4901: return(eof(fd) == 0);
1.1 root 4902: }
4903:
4904: // check keyboard status
1.1.1.35 root 4905: if(key_recv != 0) {
1.1 root 4906: return(1);
4907: }
1.1.1.35 root 4908: if(key_buf_char != NULL && key_buf_scan != NULL) {
4909: #ifdef USE_SERVICE_THREAD
4910: EnterCriticalSection(&key_buf_crit_sect);
4911: #endif
4912: bool empty = key_buf_char->empty();
4913: #ifdef USE_SERVICE_THREAD
4914: LeaveCriticalSection(&key_buf_crit_sect);
4915: #endif
4916: if(!empty) return(1);
4917: }
4918: return(_kbhit());
1.1 root 4919: }
4920:
4921: int msdos_getch_ex(int echo)
4922: {
4923: static char prev = 0;
4924:
4925: msdos_stdio_reopen();
4926:
1.1.1.20 root 4927: process_t *process = msdos_process_info_get(current_psp);
4928: int fd = msdos_psp_get_file_table(0, current_psp);
4929:
4930: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 4931: // stdin is redirected to file
4932: retry:
4933: char data;
1.1.1.37 root 4934: if(msdos_read(fd, &data, 1) == 1) {
1.1 root 4935: char tmp = data;
4936: if(data == 0x0a) {
4937: if(prev == 0x0d) {
4938: goto retry; // CRLF -> skip LF
4939: } else {
4940: data = 0x0d; // LF only -> CR
4941: }
4942: }
4943: prev = tmp;
4944: return(data);
4945: }
4946: return(EOF);
4947: }
4948:
4949: // input from console
1.1.1.5 root 4950: int key_char, key_scan;
1.1.1.33 root 4951: if(key_recv != 0) {
1.1.1.5 root 4952: key_char = (key_code >> 0) & 0xff;
4953: key_scan = (key_code >> 8) & 0xff;
4954: key_code >>= 16;
1.1.1.33 root 4955: key_recv >>= 16;
1.1.1.5 root 4956: } else {
1.1.1.35 root 4957: while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
4958: if(key_buf_char != NULL && key_buf_scan != NULL) {
4959: #ifdef USE_SERVICE_THREAD
4960: EnterCriticalSection(&key_buf_crit_sect);
4961: #endif
4962: bool empty = key_buf_char->empty();
4963: #ifdef USE_SERVICE_THREAD
4964: LeaveCriticalSection(&key_buf_crit_sect);
4965: #endif
4966: if(!empty) break;
4967: }
1.1.1.23 root 4968: if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
4969: // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
4970: if(_kbhit()) {
1.1.1.32 root 4971: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 4972: #ifdef USE_SERVICE_THREAD
4973: EnterCriticalSection(&key_buf_crit_sect);
4974: #endif
1.1.1.51! root 4975: pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35 root 4976: #ifdef USE_SERVICE_THREAD
4977: LeaveCriticalSection(&key_buf_crit_sect);
4978: #endif
1.1.1.32 root 4979: }
1.1.1.23 root 4980: } else {
4981: Sleep(10);
4982: }
4983: } else {
4984: if(!update_key_buffer()) {
4985: Sleep(10);
4986: }
1.1.1.14 root 4987: }
4988: }
4989: if(m_halted) {
1.1.1.33 root 4990: // insert CR to terminate input loops
1.1.1.14 root 4991: key_char = 0x0d;
4992: key_scan = 0;
1.1.1.32 root 4993: } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 4994: #ifdef USE_SERVICE_THREAD
4995: EnterCriticalSection(&key_buf_crit_sect);
4996: #endif
1.1.1.51! root 4997: pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35 root 4998: #ifdef USE_SERVICE_THREAD
4999: LeaveCriticalSection(&key_buf_crit_sect);
5000: #endif
1.1.1.5 root 5001: }
1.1 root 5002: }
5003: if(echo && key_char) {
5004: msdos_putch(key_char);
5005: }
5006: return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
5007: }
5008:
5009: inline int msdos_getch()
5010: {
5011: return(msdos_getch_ex(0));
5012: }
5013:
5014: inline int msdos_getche()
5015: {
5016: return(msdos_getch_ex(1));
5017: }
5018:
5019: int msdos_write(int fd, const void *buffer, unsigned int count)
5020: {
1.1.1.37 root 5021: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
5022: // write to serial port
1.1.1.38 root 5023: int written = 0;
1.1.1.37 root 5024: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
5025: UINT8 *buf = (UINT8 *)buffer;
5026: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
5027: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 5028: DWORD timeout = timeGetTime() + 1000;
5029: while(written < count) {
5030: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
5031: sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
5032: timeout = timeGetTime() + 1000;
5033: } else {
5034: if(timeGetTime() > timeout) {
5035: break;
5036: }
5037: Sleep(10);
5038: }
1.1.1.37 root 5039: }
5040: sio_write(file_handler[fd].sio_port - 1, 3, selector);
5041: }
1.1.1.38 root 5042: return(written);
1.1.1.37 root 5043: } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
5044: // write to printer port
5045: UINT8 *buf = (UINT8 *)buffer;
5046: for(unsigned int i = 0; i < count; i++) {
5047: // printer_out(file_handler[fd].lpt_port - 1, buf[i]);
5048: pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
5049: }
5050: return(count);
5051: } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1 root 5052: // CR+LF -> LF
1.1.1.37 root 5053: static int is_cr = 0;
1.1 root 5054: UINT8 *buf = (UINT8 *)buffer;
5055: for(unsigned int i = 0; i < count; i++) {
5056: UINT8 data = buf[i];
5057: if(is_cr) {
5058: if(data != 0x0a) {
5059: UINT8 tmp = 0x0d;
5060: _write(1, &tmp, 1);
5061: }
5062: _write(1, &data, 1);
5063: is_cr = 0;
5064: } else if(data == 0x0d) {
5065: is_cr = 1;
5066: } else {
5067: _write(1, &data, 1);
5068: }
5069: }
5070: return(count);
5071: }
1.1.1.14 root 5072: vram_flush();
1.1 root 5073: return(_write(fd, buffer, count));
5074: }
5075:
5076: void msdos_putch(UINT8 data)
1.1.1.50 root 5077: {
5078: msdos_stdio_reopen();
5079:
5080: process_t *process = msdos_process_info_get(current_psp);
5081: int fd = msdos_psp_get_file_table(1, current_psp);
5082:
5083: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
5084: // stdout is redirected to file
5085: msdos_write(fd, &data, 1);
5086: return;
5087: }
5088:
5089: // call int 29h ?
5090: if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
5091: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
5092: // int 29h is not hooked, no need to call int 29h
5093: msdos_putch_fast(data);
5094: #ifdef USE_SERVICE_THREAD
5095: } else if(in_service && main_thread_id != GetCurrentThreadId()) {
5096: // XXX: in usually we should not reach here
5097: // this is called from service thread to echo the input
5098: // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
5099: msdos_putch_fast(data);
5100: #endif
1.1.1.51! root 5101: } else if(in_service_29h) {
1.1.1.50 root 5102: // disallow reentering call int 29h routine to prevent an infinite loop :-(
5103: msdos_putch_fast(data);
5104: } else {
5105: // this is called from main thread, so we can call int 29h :-)
1.1.1.51! root 5106: in_service_29h = true;
1.1.1.50 root 5107: try {
5108: UINT32 tmp_pc = m_pc;
5109: UINT16 tmp_ax = REG16(AX);
5110: UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
5111:
5112: // call int 29h routine is at fffc:0027
5113: i386_call_far(DUMMY_TOP >> 4, 0x0027);
5114: REG8(AL) = data;
5115:
5116: // run cpu until call int 29h routine is done
5117: while(!m_halted && tmp_pc != m_pc) {
5118: try {
5119: hardware_run_cpu();
5120: } catch(...) {
5121: }
5122: }
5123: REG16(AX) = tmp_ax;
5124: REG16(BX) = tmp_bx;
5125: } catch(...) {
5126: }
1.1.1.51! root 5127: in_service_29h = false;
1.1.1.50 root 5128: }
5129: }
5130:
5131: void msdos_putch_fast(UINT8 data)
1.1.1.35 root 5132: #ifdef USE_SERVICE_THREAD
5133: {
5134: EnterCriticalSection(&putch_crit_sect);
5135: msdos_putch_tmp(data);
5136: LeaveCriticalSection(&putch_crit_sect);
5137: }
5138: void msdos_putch_tmp(UINT8 data)
5139: #endif
1.1 root 5140: {
1.1.1.34 root 5141: CONSOLE_SCREEN_BUFFER_INFO csbi;
5142: SMALL_RECT rect;
5143: COORD co;
1.1 root 5144: static int p = 0;
5145: static int is_kanji = 0;
5146: static int is_esc = 0;
5147: static int stored_x;
5148: static int stored_y;
5149: static WORD stored_a;
1.1.1.20 root 5150: static char tmp[64], out[64];
1.1 root 5151:
1.1.1.23 root 5152: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 5153:
5154: // output to console
5155: tmp[p++] = data;
5156:
1.1.1.14 root 5157: vram_flush();
5158:
1.1 root 5159: if(is_kanji) {
5160: // kanji character
5161: is_kanji = 0;
5162: } else if(is_esc) {
5163: // escape sequense
5164: if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
5165: p = is_esc = 0;
5166: } else if(tmp[1] == '=' && p == 4) {
5167: co.X = tmp[3] - 0x20;
1.1.1.14 root 5168: co.Y = tmp[2] - 0x20 + scr_top;
1.1 root 5169: SetConsoleCursorPosition(hStdout, co);
5170: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 5171: mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1 root 5172: cursor_moved = false;
5173: p = is_esc = 0;
5174: } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
5175: GetConsoleScreenBufferInfo(hStdout, &csbi);
5176: co.X = csbi.dwCursorPosition.X;
5177: co.Y = csbi.dwCursorPosition.Y;
5178: WORD wAttributes = csbi.wAttributes;
5179:
5180: if(tmp[1] == 'D') {
5181: co.Y++;
5182: } else if(tmp[1] == 'E') {
5183: co.X = 0;
5184: co.Y++;
5185: } else if(tmp[1] == 'M') {
5186: co.Y--;
5187: } else if(tmp[1] == '*') {
1.1.1.14 root 5188: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5189: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5190: co.X = 0;
5191: co.Y = csbi.srWindow.Top;
1.1 root 5192: } else if(tmp[1] == '[') {
5193: int param[256], params = 0;
5194: memset(param, 0, sizeof(param));
5195: for(int i = 2; i < p; i++) {
5196: if(tmp[i] >= '0' && tmp[i] <= '9') {
5197: param[params] *= 10;
5198: param[params] += tmp[i] - '0';
5199: } else {
5200: params++;
5201: }
5202: }
5203: if(data == 'A') {
1.1.1.14 root 5204: co.Y -= (params == 0) ? 1 : param[0];
1.1 root 5205: } else if(data == 'B') {
1.1.1.14 root 5206: co.Y += (params == 0) ? 1 : param[0];
1.1 root 5207: } else if(data == 'C') {
1.1.1.14 root 5208: co.X += (params == 0) ? 1 : param[0];
1.1 root 5209: } else if(data == 'D') {
1.1.1.14 root 5210: co.X -= (params == 0) ? 1 : param[0];
1.1 root 5211: } else if(data == 'H' || data == 'f') {
1.1.1.14 root 5212: co.X = (param[1] == 0 ? 1 : param[1]) - 1;
5213: co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1 root 5214: } else if(data == 'J') {
1.1.1.14 root 5215: clear_scr_buffer(csbi.wAttributes);
1.1 root 5216: if(param[0] == 0) {
5217: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5218: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5219: if(co.Y < csbi.srWindow.Bottom) {
5220: SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5221: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5222: }
5223: } else if(param[0] == 1) {
1.1.1.14 root 5224: if(co.Y > csbi.srWindow.Top) {
5225: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
5226: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5227: }
5228: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14 root 5229: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5230: } else if(param[0] == 2) {
1.1.1.14 root 5231: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5232: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5233: co.X = co.Y = 0;
5234: }
5235: } else if(data == 'K') {
1.1.1.14 root 5236: clear_scr_buffer(csbi.wAttributes);
1.1 root 5237: if(param[0] == 0) {
5238: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5239: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5240: } else if(param[0] == 1) {
5241: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14 root 5242: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5243: } else if(param[0] == 2) {
5244: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5245: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5246: }
5247: } else if(data == 'L') {
1.1.1.14 root 5248: if(params == 0) {
5249: param[0] = 1;
1.1 root 5250: }
1.1.1.14 root 5251: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5252: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5253: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5254: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5255: clear_scr_buffer(csbi.wAttributes);
1.1 root 5256: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14 root 5257: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5258: co.X = 0;
5259: } else if(data == 'M') {
1.1.1.14 root 5260: if(params == 0) {
5261: param[0] = 1;
5262: }
5263: if(co.Y + param[0] > csbi.srWindow.Bottom) {
5264: clear_scr_buffer(csbi.wAttributes);
5265: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5266: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5267: } else {
1.1.1.14 root 5268: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5269: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5270: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5271: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5272: clear_scr_buffer(csbi.wAttributes);
1.1 root 5273: }
5274: co.X = 0;
5275: } else if(data == 'h') {
5276: if(tmp[2] == '>' && tmp[3] == '5') {
5277: CONSOLE_CURSOR_INFO cur;
5278: GetConsoleCursorInfo(hStdout, &cur);
5279: if(cur.bVisible) {
5280: cur.bVisible = FALSE;
1.1.1.14 root 5281: // SetConsoleCursorInfo(hStdout, &cur);
1.1 root 5282: }
5283: }
5284: } else if(data == 'l') {
5285: if(tmp[2] == '>' && tmp[3] == '5') {
5286: CONSOLE_CURSOR_INFO cur;
5287: GetConsoleCursorInfo(hStdout, &cur);
5288: if(!cur.bVisible) {
5289: cur.bVisible = TRUE;
1.1.1.14 root 5290: // SetConsoleCursorInfo(hStdout, &cur);
1.1 root 5291: }
5292: }
5293: } else if(data == 'm') {
5294: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
5295: int reverse = 0, hidden = 0;
5296: for(int i = 0; i < params; i++) {
5297: if(param[i] == 1) {
5298: wAttributes |= FOREGROUND_INTENSITY;
5299: } else if(param[i] == 4) {
5300: wAttributes |= COMMON_LVB_UNDERSCORE;
5301: } else if(param[i] == 7) {
5302: reverse = 1;
5303: } else if(param[i] == 8 || param[i] == 16) {
5304: hidden = 1;
5305: } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
5306: wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
5307: if(param[i] >= 17 && param[i] <= 23) {
5308: param[i] -= 16;
5309: } else {
5310: param[i] -= 30;
5311: }
5312: if(param[i] & 1) {
5313: wAttributes |= FOREGROUND_RED;
5314: }
5315: if(param[i] & 2) {
5316: wAttributes |= FOREGROUND_GREEN;
5317: }
5318: if(param[i] & 4) {
5319: wAttributes |= FOREGROUND_BLUE;
5320: }
5321: } else if(param[i] >= 40 && param[i] <= 47) {
5322: wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
5323: if((param[i] - 40) & 1) {
5324: wAttributes |= BACKGROUND_RED;
5325: }
5326: if((param[i] - 40) & 2) {
5327: wAttributes |= BACKGROUND_GREEN;
5328: }
5329: if((param[i] - 40) & 4) {
5330: wAttributes |= BACKGROUND_BLUE;
5331: }
5332: }
5333: }
5334: if(reverse) {
5335: wAttributes &= ~0xff;
5336: wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
5337: }
5338: if(hidden) {
5339: wAttributes &= ~0x0f;
5340: wAttributes |= (wAttributes >> 4) & 0x0f;
5341: }
5342: } else if(data == 'n') {
5343: if(param[0] == 6) {
5344: char tmp[16];
5345: sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
5346: int len = strlen(tmp);
1.1.1.32 root 5347: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 5348: #ifdef USE_SERVICE_THREAD
5349: EnterCriticalSection(&key_buf_crit_sect);
5350: #endif
1.1.1.32 root 5351: for(int i = 0; i < len; i++) {
1.1.1.51! root 5352: pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32 root 5353: }
1.1.1.35 root 5354: #ifdef USE_SERVICE_THREAD
5355: LeaveCriticalSection(&key_buf_crit_sect);
5356: #endif
1.1 root 5357: }
5358: }
5359: } else if(data == 's') {
5360: stored_x = co.X;
5361: stored_y = co.Y;
5362: stored_a = wAttributes;
5363: } else if(data == 'u') {
5364: co.X = stored_x;
5365: co.Y = stored_y;
5366: wAttributes = stored_a;
5367: }
5368: }
5369: if(co.X < 0) {
5370: co.X = 0;
5371: } else if(co.X >= csbi.dwSize.X) {
5372: co.X = csbi.dwSize.X - 1;
5373: }
1.1.1.14 root 5374: if(co.Y < csbi.srWindow.Top) {
5375: co.Y = csbi.srWindow.Top;
5376: } else if(co.Y > csbi.srWindow.Bottom) {
5377: co.Y = csbi.srWindow.Bottom;
1.1 root 5378: }
5379: if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
5380: SetConsoleCursorPosition(hStdout, co);
5381: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 5382: mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1 root 5383: cursor_moved = false;
5384: }
5385: if(wAttributes != csbi.wAttributes) {
5386: SetConsoleTextAttribute(hStdout, wAttributes);
5387: }
5388: p = is_esc = 0;
5389: }
5390: return;
5391: } else {
5392: if(msdos_lead_byte_check(data)) {
5393: is_kanji = 1;
5394: return;
5395: } else if(data == 0x1b) {
5396: is_esc = 1;
5397: return;
5398: }
5399: }
1.1.1.20 root 5400:
5401: DWORD q = 0, num;
5402: is_kanji = 0;
5403: for(int i = 0; i < p; i++) {
5404: UINT8 c = tmp[i];
5405: if(is_kanji) {
5406: is_kanji = 0;
5407: } else if(msdos_lead_byte_check(data)) {
5408: is_kanji = 1;
5409: } else if(msdos_ctrl_code_check(data)) {
5410: out[q++] = '^';
5411: c += 'A' - 1;
5412: }
5413: out[q++] = c;
5414: }
1.1.1.34 root 5415: if(q == 1 && out[0] == 0x08) {
5416: // back space
5417: GetConsoleScreenBufferInfo(hStdout, &csbi);
5418: if(csbi.dwCursorPosition.X > 0) {
5419: co.X = csbi.dwCursorPosition.X - 1;
5420: co.Y = csbi.dwCursorPosition.Y;
5421: SetConsoleCursorPosition(hStdout, co);
5422: } else if(csbi.dwCursorPosition.Y > 0) {
5423: co.X = csbi.dwSize.X - 1;
5424: co.Y = csbi.dwCursorPosition.Y - 1;
5425: SetConsoleCursorPosition(hStdout, co);
5426: } else {
5427: WriteConsole(hStdout, out, q, &num, NULL); // to make sure
5428: }
5429: } else {
5430: WriteConsole(hStdout, out, q, &num, NULL);
5431: }
1.1 root 5432: p = 0;
1.1.1.14 root 5433:
1.1.1.15 root 5434: if(!restore_console_on_exit) {
5435: GetConsoleScreenBufferInfo(hStdout, &csbi);
5436: scr_top = csbi.srWindow.Top;
5437: }
1.1 root 5438: cursor_moved = true;
5439: }
5440:
5441: int msdos_aux_in()
5442: {
1.1.1.21 root 5443: msdos_stdio_reopen();
5444:
1.1.1.20 root 5445: process_t *process = msdos_process_info_get(current_psp);
5446: int fd = msdos_psp_get_file_table(3, current_psp);
5447:
5448: if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1 root 5449: char data = 0;
1.1.1.37 root 5450: msdos_read(fd, &data, 1);
1.1 root 5451: return(data);
5452: } else {
5453: return(EOF);
5454: }
5455: }
5456:
5457: void msdos_aux_out(char data)
5458: {
1.1.1.21 root 5459: msdos_stdio_reopen();
5460:
1.1.1.20 root 5461: process_t *process = msdos_process_info_get(current_psp);
5462: int fd = msdos_psp_get_file_table(3, current_psp);
5463:
5464: if(fd < process->max_files && file_handler[fd].valid) {
5465: msdos_write(fd, &data, 1);
1.1 root 5466: }
5467: }
5468:
5469: void msdos_prn_out(char data)
5470: {
1.1.1.21 root 5471: msdos_stdio_reopen();
5472:
1.1.1.20 root 5473: process_t *process = msdos_process_info_get(current_psp);
5474: int fd = msdos_psp_get_file_table(4, current_psp);
5475:
5476: if(fd < process->max_files && file_handler[fd].valid) {
5477: msdos_write(fd, &data, 1);
1.1 root 5478: }
5479: }
5480:
5481: // memory control
5482:
1.1.1.45 root 5483: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name)
1.1 root 5484: {
5485: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5486:
5487: mcb->mz = mz;
5488: mcb->psp = psp;
1.1.1.30 root 5489: mcb->paragraphs = paragraphs;
1.1.1.39 root 5490:
5491: if(prog_name != NULL) {
5492: memset(mcb->prog_name, 0, 8);
5493: memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
5494: }
1.1 root 5495: return(mcb);
5496: }
5497:
1.1.1.39 root 5498: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
5499: {
5500: return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
5501: }
5502:
1.1 root 5503: void msdos_mcb_check(mcb_t *mcb)
5504: {
5505: if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28 root 5506: #if 0
5507: // shutdown now !!!
5508: fatalerror("broken memory control block\n");
5509: #else
5510: // return error code and continue
5511: throw(0x07); // broken memory control block
5512: #endif
1.1 root 5513: }
5514: }
5515:
1.1.1.39 root 5516: void msdos_mem_split(int seg, int paragraphs)
1.1 root 5517: {
5518: int mcb_seg = seg - 1;
5519: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5520: msdos_mcb_check(mcb);
5521:
1.1.1.30 root 5522: if(mcb->paragraphs > paragraphs) {
1.1 root 5523: int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30 root 5524: int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1 root 5525:
5526: msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
5527: mcb->mz = 'M';
1.1.1.30 root 5528: mcb->paragraphs = paragraphs;
1.1 root 5529: }
5530: }
5531:
5532: void msdos_mem_merge(int seg)
5533: {
5534: int mcb_seg = seg - 1;
5535: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5536: msdos_mcb_check(mcb);
5537:
5538: while(1) {
5539: if(mcb->mz == 'Z') {
5540: break;
5541: }
1.1.1.30 root 5542: int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1 root 5543: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5544: msdos_mcb_check(next_mcb);
5545:
5546: if(next_mcb->psp != 0) {
5547: break;
5548: }
5549: mcb->mz = next_mcb->mz;
1.1.1.30 root 5550: mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1 root 5551: }
5552: }
5553:
1.1.1.8 root 5554: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1 root 5555: {
5556: while(1) {
5557: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5558: bool last_block;
1.1 root 5559:
1.1.1.14 root 5560: if(mcb->psp == 0) {
5561: msdos_mem_merge(mcb_seg + 1);
5562: } else {
5563: msdos_mcb_check(mcb);
5564: }
1.1.1.33 root 5565: if(!(last_block = (mcb->mz == 'Z'))) {
5566: // check if the next is dummy mcb to link to umb
5567: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5568: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5569: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5570: }
5571: if(!(new_process && !last_block)) {
1.1.1.30 root 5572: if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1 root 5573: msdos_mem_split(mcb_seg + 1, paragraphs);
5574: mcb->psp = current_psp;
5575: return(mcb_seg + 1);
5576: }
5577: }
5578: if(mcb->mz == 'Z') {
5579: break;
5580: }
1.1.1.30 root 5581: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5582: }
5583: return(-1);
5584: }
5585:
5586: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
5587: {
5588: int mcb_seg = seg - 1;
5589: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5590: msdos_mcb_check(mcb);
1.1.1.30 root 5591: int current_paragraphs = mcb->paragraphs;
1.1 root 5592:
5593: msdos_mem_merge(seg);
1.1.1.30 root 5594: if(paragraphs > mcb->paragraphs) {
1.1.1.14 root 5595: if(max_paragraphs) {
1.1.1.30 root 5596: *max_paragraphs = mcb->paragraphs;
1.1.1.14 root 5597: }
1.1 root 5598: msdos_mem_split(seg, current_paragraphs);
5599: return(-1);
5600: }
5601: msdos_mem_split(seg, paragraphs);
5602: return(0);
5603: }
5604:
5605: void msdos_mem_free(int seg)
5606: {
5607: int mcb_seg = seg - 1;
5608: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5609: msdos_mcb_check(mcb);
5610:
5611: mcb->psp = 0;
5612: msdos_mem_merge(seg);
5613: }
5614:
1.1.1.8 root 5615: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1 root 5616: {
5617: int max_paragraphs = 0;
5618:
5619: while(1) {
5620: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5621: bool last_block;
5622:
1.1 root 5623: msdos_mcb_check(mcb);
5624:
1.1.1.33 root 5625: if(!(last_block = (mcb->mz == 'Z'))) {
5626: // check if the next is dummy mcb to link to umb
5627: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5628: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5629: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5630: }
5631: if(!(new_process && !last_block)) {
1.1.1.30 root 5632: if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
5633: max_paragraphs = mcb->paragraphs;
1.1 root 5634: }
5635: }
5636: if(mcb->mz == 'Z') {
5637: break;
5638: }
1.1.1.30 root 5639: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5640: }
1.1.1.14 root 5641: return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1 root 5642: }
5643:
1.1.1.8 root 5644: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
5645: {
5646: int last_seg = -1;
5647:
5648: while(1) {
5649: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5650: msdos_mcb_check(mcb);
5651:
1.1.1.14 root 5652: if(mcb->psp == psp) {
1.1.1.8 root 5653: last_seg = mcb_seg;
5654: }
1.1.1.14 root 5655: if(mcb->mz == 'Z') {
5656: break;
5657: }
1.1.1.30 root 5658: mcb_seg += 1 + mcb->paragraphs;
1.1.1.8 root 5659: }
5660: return(last_seg);
5661: }
5662:
1.1.1.19 root 5663: int msdos_mem_get_umb_linked()
5664: {
1.1.1.33 root 5665: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5666: msdos_mcb_check(mcb);
1.1.1.19 root 5667:
1.1.1.33 root 5668: if(mcb->mz == 'M') {
5669: return(-1);
1.1.1.19 root 5670: }
5671: return(0);
5672: }
5673:
1.1.1.33 root 5674: void msdos_mem_link_umb()
1.1.1.19 root 5675: {
1.1.1.33 root 5676: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5677: msdos_mcb_check(mcb);
1.1.1.19 root 5678:
1.1.1.33 root 5679: mcb->mz = 'M';
5680: mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39 root 5681:
5682: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19 root 5683: }
5684:
1.1.1.33 root 5685: void msdos_mem_unlink_umb()
1.1.1.19 root 5686: {
1.1.1.33 root 5687: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5688: msdos_mcb_check(mcb);
1.1.1.19 root 5689:
1.1.1.33 root 5690: mcb->mz = 'Z';
5691: mcb->paragraphs = 0;
1.1.1.39 root 5692:
5693: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19 root 5694: }
5695:
1.1.1.29 root 5696: #ifdef SUPPORT_HMA
5697:
5698: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
5699: {
5700: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5701:
5702: mcb->ms[0] = 'M';
5703: mcb->ms[1] = 'S';
5704: mcb->owner = owner;
5705: mcb->size = size;
5706: mcb->next = next;
5707: return(mcb);
5708: }
5709:
5710: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
5711: {
5712: return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
5713: }
5714:
5715: int msdos_hma_mem_split(int offset, int size)
5716: {
5717: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5718:
5719: if(!msdos_is_hma_mcb_valid(mcb)) {
5720: return(-1);
5721: }
5722: if(mcb->size >= size + 0x10) {
5723: int new_offset = offset + 0x10 + size;
5724: int new_size = mcb->size - 0x10 - size;
5725:
5726: msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
5727: mcb->size = size;
5728: mcb->next = new_offset;
5729: return(0);
5730: }
5731: return(-1);
5732: }
5733:
5734: void msdos_hma_mem_merge(int offset)
5735: {
5736: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5737:
5738: if(!msdos_is_hma_mcb_valid(mcb)) {
5739: return;
5740: }
5741: while(1) {
5742: if(mcb->next == 0) {
5743: break;
5744: }
5745: hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
5746:
5747: if(!msdos_is_hma_mcb_valid(next_mcb)) {
5748: return;
5749: }
5750: if(next_mcb->owner != 0) {
5751: break;
5752: }
5753: mcb->size += 0x10 + next_mcb->size;
5754: mcb->next = next_mcb->next;
5755: }
5756: }
5757:
5758: int msdos_hma_mem_alloc(int size, UINT16 owner)
5759: {
5760: int offset = 0x10; // first mcb in HMA
5761:
5762: while(1) {
5763: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5764:
5765: if(!msdos_is_hma_mcb_valid(mcb)) {
5766: return(-1);
5767: }
5768: if(mcb->owner == 0) {
5769: msdos_hma_mem_merge(offset);
5770: }
5771: if(mcb->owner == 0 && mcb->size >= size) {
5772: msdos_hma_mem_split(offset, size);
5773: mcb->owner = owner;
5774: return(offset);
5775: }
5776: if(mcb->next == 0) {
5777: break;
5778: }
5779: offset = mcb->next;
5780: }
5781: return(-1);
5782: }
5783:
5784: int msdos_hma_mem_realloc(int offset, int size)
5785: {
5786: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5787:
5788: if(!msdos_is_hma_mcb_valid(mcb)) {
5789: return(-1);
5790: }
5791: if(mcb->size < size) {
5792: return(-1);
5793: }
5794: msdos_hma_mem_split(offset, size);
5795: return(0);
5796: }
5797:
5798: void msdos_hma_mem_free(int offset)
5799: {
5800: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5801:
5802: if(!msdos_is_hma_mcb_valid(mcb)) {
5803: return;
5804: }
5805: mcb->owner = 0;
5806: msdos_hma_mem_merge(offset);
5807: }
5808:
5809: int msdos_hma_mem_get_free(int *available_offset)
5810: {
5811: int offset = 0x10; // first mcb in HMA
5812: int size = 0;
5813:
5814: while(1) {
5815: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5816:
5817: if(!msdos_is_hma_mcb_valid(mcb)) {
5818: return(0);
5819: }
5820: if(mcb->owner == 0 && size < mcb->size) {
5821: if(available_offset != NULL) {
5822: *available_offset = offset;
5823: }
5824: size = mcb->size;
5825: }
5826: if(mcb->next == 0) {
5827: break;
5828: }
5829: offset = mcb->next;
5830: }
5831: return(size);
5832: }
5833:
5834: #endif
5835:
1.1 root 5836: // environment
5837:
1.1.1.45 root 5838: void msdos_env_set_argv(int env_seg, const char *argv)
1.1 root 5839: {
5840: char *dst = (char *)(mem + (env_seg << 4));
5841:
5842: while(1) {
5843: if(dst[0] == 0) {
5844: break;
5845: }
5846: dst += strlen(dst) + 1;
5847: }
5848: *dst++ = 0; // end of environment
5849: *dst++ = 1; // top of argv[0]
5850: *dst++ = 0;
5851: memcpy(dst, argv, strlen(argv));
5852: dst += strlen(argv);
5853: *dst++ = 0;
5854: *dst++ = 0;
5855: }
5856:
1.1.1.45 root 5857: const char *msdos_env_get_argv(int env_seg)
1.1 root 5858: {
5859: static char env[ENV_SIZE];
5860: char *src = env;
5861:
5862: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
5863: while(1) {
5864: if(src[0] == 0) {
5865: if(src[1] == 1) {
5866: return(src + 3);
5867: }
5868: break;
5869: }
5870: src += strlen(src) + 1;
5871: }
5872: return(NULL);
5873: }
5874:
1.1.1.45 root 5875: const char *msdos_env_get(int env_seg, const char *name)
1.1 root 5876: {
5877: static char env[ENV_SIZE];
5878: char *src = env;
5879:
5880: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
5881: while(1) {
5882: if(src[0] == 0) {
5883: break;
5884: }
5885: int len = strlen(src);
5886: char *n = my_strtok(src, "=");
5887: char *v = src + strlen(n) + 1;
5888:
5889: if(_stricmp(name, n) == 0) {
5890: return(v);
5891: }
5892: src += len + 1;
5893: }
5894: return(NULL);
5895: }
5896:
1.1.1.45 root 5897: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1 root 5898: {
5899: char env[ENV_SIZE];
5900: char *src = env;
5901: char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45 root 5902: const char *argv = msdos_env_get_argv(env_seg);
1.1 root 5903: int done = 0;
5904:
5905: memcpy(src, dst, ENV_SIZE);
5906: memset(dst, 0, ENV_SIZE);
5907: while(1) {
5908: if(src[0] == 0) {
5909: break;
5910: }
5911: int len = strlen(src);
5912: char *n = my_strtok(src, "=");
5913: char *v = src + strlen(n) + 1;
5914: char tmp[1024];
5915:
5916: if(_stricmp(name, n) == 0) {
5917: sprintf(tmp, "%s=%s", n, value);
5918: done = 1;
5919: } else {
5920: sprintf(tmp, "%s=%s", n, v);
5921: }
5922: memcpy(dst, tmp, strlen(tmp));
5923: dst += strlen(tmp) + 1;
5924: src += len + 1;
5925: }
5926: if(!done) {
5927: char tmp[1024];
5928:
5929: sprintf(tmp, "%s=%s", name, value);
5930: memcpy(dst, tmp, strlen(tmp));
5931: dst += strlen(tmp) + 1;
5932: }
5933: if(argv) {
5934: *dst++ = 0; // end of environment
5935: *dst++ = 1; // top of argv[0]
5936: *dst++ = 0;
5937: memcpy(dst, argv, strlen(argv));
5938: dst += strlen(argv);
5939: *dst++ = 0;
5940: *dst++ = 0;
5941: }
5942: }
5943:
5944: // process
5945:
1.1.1.8 root 5946: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1 root 5947: {
5948: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
5949:
5950: memset(psp, 0, PSP_SIZE);
5951: psp->exit[0] = 0xcd;
5952: psp->exit[1] = 0x20;
1.1.1.8 root 5953: psp->first_mcb = mcb_seg;
1.1.1.46 root 5954: #if 1
1.1.1.49 root 5955: psp->call5[0] = 0xcd; // int 30h
5956: psp->call5[1] = 0x30;
1.1.1.46 root 5957: psp->call5[2] = 0xc3; // ret
5958: #else
5959: psp->call5[0] = 0x8a; // mov ah, cl
5960: psp->call5[1] = 0xe1;
5961: psp->call5[2] = 0xcd; // int 21h
5962: psp->call5[3] = 0x21;
5963: psp->call5[4] = 0xc3; // ret
5964: #endif
1.1 root 5965: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
5966: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
5967: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
5968: psp->parent_psp = parent_psp;
1.1.1.20 root 5969: if(parent_psp == (UINT16)-1) {
5970: for(int i = 0; i < 20; i++) {
5971: if(file_handler[i].valid) {
5972: psp->file_table[i] = i;
5973: } else {
5974: psp->file_table[i] = 0xff;
5975: }
1.1 root 5976: }
1.1.1.20 root 5977: } else {
5978: memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1 root 5979: }
5980: psp->env_seg = env_seg;
5981: psp->stack.w.l = REG16(SP);
1.1.1.3 root 5982: psp->stack.w.h = SREG(SS);
1.1.1.14 root 5983: psp->file_table_size = 20;
5984: psp->file_table_ptr.w.l = 0x18;
5985: psp->file_table_ptr.w.h = psp_seg;
1.1 root 5986: psp->service[0] = 0xcd;
5987: psp->service[1] = 0x21;
5988: psp->service[2] = 0xcb;
5989: return(psp);
5990: }
5991:
1.1.1.20 root 5992: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
5993: {
5994: if(psp_seg && fd < 20) {
5995: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
5996: psp->file_table[fd] = value;
5997: }
5998: }
5999:
6000: int msdos_psp_get_file_table(int fd, int psp_seg)
6001: {
6002: if(psp_seg && fd < 20) {
6003: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6004: fd = psp->file_table[fd];
6005: }
6006: return fd;
6007: }
6008:
1.1.1.45 root 6009: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al)
1.1 root 6010: {
6011: // load command file
6012: int fd = -1;
1.1.1.45 root 6013: int sio_port = 0;
6014: int lpt_port = 0;
1.1 root 6015: int dos_command = 0;
1.1.1.24 root 6016: char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38 root 6017: char pipe_stdin_path[MAX_PATH] = {0};
6018: char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39 root 6019: char pipe_stderr_path[MAX_PATH] = {0};
1.1 root 6020:
6021: int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
6022: int opt_len = mem[opt_ofs];
6023: memset(opt, 0, sizeof(opt));
6024: memcpy(opt, mem + opt_ofs + 1, opt_len);
6025:
1.1.1.14 root 6026: if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
6027: // this is a batch file, run command.com
6028: char tmp[MAX_PATH];
6029: if(opt_len != 0) {
6030: sprintf(tmp, "/C %s %s", cmd, opt);
6031: } else {
6032: sprintf(tmp, "/C %s", cmd);
6033: }
6034: strcpy(opt, tmp);
6035: opt_len = strlen(opt);
6036: mem[opt_ofs] = opt_len;
6037: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6038: strcpy(command, comspec_path);
6039: strcpy(name_tmp, "COMMAND.COM");
6040: } else {
6041: if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
6042: // redirect C:\COMMAND.COM to comspec_path
6043: strcpy(command, comspec_path);
6044: } else {
6045: strcpy(command, cmd);
6046: }
1.1.1.24 root 6047: if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
6048: return(-1);
6049: }
1.1.1.14 root 6050: memset(name_tmp, 0, sizeof(name_tmp));
6051: strcpy(name_tmp, name);
6052:
6053: // check command.com
1.1.1.38 root 6054: if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
6055: // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14 root 6056: if(opt_len == 0) {
6057: // process_t *current_process = msdos_process_info_get(current_psp);
6058: process_t *current_process = NULL;
6059: for(int i = 0; i < MAX_PROCESS; i++) {
6060: if(process[i].psp == current_psp) {
6061: current_process = &process[i];
6062: break;
6063: }
6064: }
6065: if(current_process != NULL) {
6066: param->cmd_line.dw = current_process->dta.dw;
6067: opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
6068: opt_len = mem[opt_ofs];
6069: memset(opt, 0, sizeof(opt));
6070: memcpy(opt, mem + opt_ofs + 1, opt_len);
6071: }
6072: }
6073: for(int i = 0; i < opt_len; i++) {
6074: if(opt[i] == ' ') {
6075: continue;
6076: }
6077: if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
6078: for(int j = i + 3; j < opt_len; j++) {
6079: if(opt[j] == ' ') {
6080: continue;
6081: }
6082: char *token = my_strtok(opt + j, " ");
6083:
1.1.1.38 root 6084: strcpy(command, token);
6085: char tmp[MAX_PATH];
6086: strcpy(tmp, token + strlen(token) + 1);
1.1.1.39 root 6087: strcpy(opt, "");
6088: for(int i = 0; i < strlen(tmp); i++) {
6089: if(tmp[i] != ' ') {
6090: strcpy(opt, tmp + i);
6091: break;
6092: }
6093: }
6094: strcpy(tmp, opt);
1.1.1.38 root 6095:
6096: if(al == 0x00) {
1.1.1.39 root 6097: #define GET_FILE_PATH() { \
6098: if(token[0] != '>' && token[0] != '<') { \
6099: token++; \
6100: } \
6101: token++; \
6102: while(*token == ' ') { \
6103: token++; \
6104: } \
6105: char *ptr = token; \
6106: while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
6107: ptr++; \
6108: } \
6109: *ptr = '\0'; \
6110: }
6111: if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
6112: GET_FILE_PATH();
1.1.1.38 root 6113: strcpy(pipe_stdin_path, token);
6114: strcpy(opt, tmp);
6115: }
1.1.1.39 root 6116: if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
6117: GET_FILE_PATH();
1.1.1.38 root 6118: strcpy(pipe_stdout_path, token);
6119: strcpy(opt, tmp);
6120: }
1.1.1.39 root 6121: if((token = strstr(opt, "2>")) != NULL) {
6122: GET_FILE_PATH();
6123: strcpy(pipe_stderr_path, token);
6124: strcpy(opt, tmp);
6125: }
6126: #undef GET_FILE_PATH
6127:
6128: if((token = strstr(opt, "0<")) != NULL) {
6129: *token = '\0';
6130: }
6131: if((token = strstr(opt, "1>")) != NULL) {
6132: *token = '\0';
6133: }
6134: if((token = strstr(opt, "2>")) != NULL) {
6135: *token = '\0';
6136: }
1.1.1.38 root 6137: if((token = strstr(opt, "<")) != NULL) {
6138: *token = '\0';
6139: }
6140: if((token = strstr(opt, ">")) != NULL) {
6141: *token = '\0';
6142: }
1.1.1.14 root 6143: }
1.1.1.39 root 6144: for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
6145: opt[i] = '\0';
6146: }
1.1.1.38 root 6147: opt_len = strlen(opt);
6148: mem[opt_ofs] = opt_len;
6149: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6150: dos_command = 1;
1.1.1.14 root 6151: break;
1.1 root 6152: }
6153: }
1.1.1.14 root 6154: break;
1.1 root 6155: }
6156: }
6157: }
6158:
6159: // load command file
6160: strcpy(path, command);
6161: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
6162: sprintf(path, "%s.COM", command);
6163: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
6164: sprintf(path, "%s.EXE", command);
6165: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14 root 6166: sprintf(path, "%s.BAT", command);
6167: if(_access(path, 0) == 0) {
6168: // this is a batch file, run command.com
6169: char tmp[MAX_PATH];
6170: if(opt_len != 0) {
6171: sprintf(tmp, "/C %s %s", path, opt);
6172: } else {
6173: sprintf(tmp, "/C %s", path);
6174: }
6175: strcpy(opt, tmp);
6176: opt_len = strlen(opt);
6177: mem[opt_ofs] = opt_len;
6178: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6179: strcpy(path, comspec_path);
6180: strcpy(name_tmp, "COMMAND.COM");
6181: fd = _open(path, _O_RDONLY | _O_BINARY);
6182: } else {
6183: // search path in parent environments
6184: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45 root 6185: const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14 root 6186: if(env != NULL) {
6187: char env_path[4096];
6188: strcpy(env_path, env);
6189: char *token = my_strtok(env_path, ";");
6190:
6191: while(token != NULL) {
6192: if(strlen(token) != 0) {
6193: sprintf(path, "%s", msdos_combine_path(token, command));
6194: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6195: break;
6196: }
6197: sprintf(path, "%s.COM", msdos_combine_path(token, command));
6198: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6199: break;
6200: }
6201: sprintf(path, "%s.EXE", msdos_combine_path(token, command));
6202: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
6203: break;
6204: }
6205: sprintf(path, "%s.BAT", msdos_combine_path(token, command));
6206: if(_access(path, 0) == 0) {
6207: // this is a batch file, run command.com
6208: char tmp[MAX_PATH];
6209: if(opt_len != 0) {
6210: sprintf(tmp, "/C %s %s", path, opt);
6211: } else {
6212: sprintf(tmp, "/C %s", path);
6213: }
6214: strcpy(opt, tmp);
6215: opt_len = strlen(opt);
6216: mem[opt_ofs] = opt_len;
6217: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
6218: strcpy(path, comspec_path);
6219: strcpy(name_tmp, "COMMAND.COM");
6220: fd = _open(path, _O_RDONLY | _O_BINARY);
6221: break;
6222: }
1.1.1.8 root 6223: }
1.1.1.14 root 6224: token = my_strtok(NULL, ";");
1.1 root 6225: }
6226: }
6227: }
6228: }
6229: }
6230: }
6231: if(fd == -1) {
1.1.1.38 root 6232: // we can not find command.com in the path, so open comspec_path
6233: if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
6234: strcpy(command, comspec_path);
6235: strcpy(path, command);
6236: fd = _open(path, _O_RDONLY | _O_BINARY);
6237: }
6238: }
6239: if(fd == -1) {
1.1 root 6240: if(dos_command) {
6241: // may be dos command
6242: char tmp[MAX_PATH];
6243: sprintf(tmp, "%s %s", command, opt);
6244: system(tmp);
6245: return(0);
6246: } else {
6247: return(-1);
6248: }
6249: }
6250: _read(fd, file_buffer, sizeof(file_buffer));
6251: _close(fd);
6252:
6253: // copy environment
1.1.1.29 root 6254: int umb_linked, env_seg, psp_seg;
1.1 root 6255:
1.1.1.29 root 6256: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
6257: msdos_mem_unlink_umb();
6258: }
1.1.1.8 root 6259: if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29 root 6260: if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
6261: if(umb_linked != 0) {
6262: msdos_mem_link_umb();
6263: }
6264: return(-1);
6265: }
1.1 root 6266: }
6267: if(param->env_seg == 0) {
6268: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
6269: memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
6270: } else {
6271: memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
6272: }
6273: msdos_env_set_argv(env_seg, msdos_short_full_path(path));
6274:
6275: // check exe header
6276: exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8 root 6277: int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1 root 6278: UINT16 cs, ss, ip, sp;
6279:
6280: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
6281: // memory allocation
6282: int header_size = header->header_size * 16;
6283: int load_size = header->pages * 512 - header_size;
6284: if(header_size + load_size < 512) {
6285: load_size = 512 - header_size;
6286: }
6287: paragraphs = (PSP_SIZE + load_size) >> 4;
6288: if(paragraphs + header->min_alloc > free_paragraphs) {
6289: msdos_mem_free(env_seg);
6290: return(-1);
6291: }
6292: paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
6293: if(paragraphs > free_paragraphs) {
6294: paragraphs = free_paragraphs;
6295: }
1.1.1.8 root 6296: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6297: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6298: if(umb_linked != 0) {
6299: msdos_mem_link_umb();
6300: }
6301: msdos_mem_free(env_seg);
6302: return(-1);
6303: }
1.1 root 6304: }
6305: // relocation
6306: int start_seg = psp_seg + (PSP_SIZE >> 4);
6307: for(int i = 0; i < header->relocations; i++) {
6308: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
6309: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
6310: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
6311: }
6312: memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
6313: // segments
6314: cs = header->init_cs + start_seg;
6315: ss = header->init_ss + start_seg;
6316: ip = header->init_ip;
6317: sp = header->init_sp - 2; // for symdeb
6318: } else {
6319: // memory allocation
6320: paragraphs = free_paragraphs;
1.1.1.8 root 6321: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6322: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6323: if(umb_linked != 0) {
6324: msdos_mem_link_umb();
6325: }
6326: msdos_mem_free(env_seg);
6327: return(-1);
6328: }
1.1 root 6329: }
6330: int start_seg = psp_seg + (PSP_SIZE >> 4);
6331: memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
6332: // segments
6333: cs = ss = psp_seg;
6334: ip = 0x100;
6335: sp = 0xfffe;
6336: }
1.1.1.29 root 6337: if(umb_linked != 0) {
6338: msdos_mem_link_umb();
6339: }
1.1 root 6340:
6341: // create psp
1.1.1.3 root 6342: *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
6343: *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1 root 6344: psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
6345: memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
6346: memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
6347: memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
6348:
6349: mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
6350: mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
6351: mcb_psp->psp = mcb_env->psp = psp_seg;
6352:
1.1.1.4 root 6353: for(int i = 0; i < 8; i++) {
6354: if(name_tmp[i] == '.') {
6355: mcb_psp->prog_name[i] = '\0';
6356: break;
6357: } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
6358: mcb_psp->prog_name[i] = name_tmp[i];
6359: i++;
6360: mcb_psp->prog_name[i] = name_tmp[i];
6361: } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
6362: mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
6363: } else {
6364: mcb_psp->prog_name[i] = name_tmp[i];
6365: }
6366: }
6367:
1.1 root 6368: // process info
6369: process_t *process = msdos_process_info_create(psp_seg);
6370: strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33 root 6371: #ifdef USE_DEBUGGER
6372: strcpy(process->module_path, path);
6373: #endif
1.1 root 6374: process->dta.w.l = 0x80;
6375: process->dta.w.h = psp_seg;
6376: process->switchar = '/';
6377: process->max_files = 20;
6378: process->parent_int_10h_feh_called = int_10h_feh_called;
6379: process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14 root 6380: process->parent_ds = SREG(DS);
1.1.1.31 root 6381: process->parent_es = SREG(ES);
1.1 root 6382:
6383: current_psp = psp_seg;
1.1.1.23 root 6384: msdos_sda_update(current_psp);
1.1 root 6385:
6386: if(al == 0x00) {
6387: int_10h_feh_called = int_10h_ffh_called = false;
6388:
1.1.1.38 root 6389: // pipe
6390: if(pipe_stdin_path[0] != '\0') {
1.1.1.45 root 6391: // if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
6392: if(msdos_is_device_path(pipe_stdin_path)) {
6393: fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
6394: } else {
6395: fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
6396: }
6397: if(fd != -1) {
6398: 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 6399: psp->file_table[0] = fd;
6400: msdos_psp_set_file_table(fd, fd, current_psp);
6401: }
6402: }
6403: if(pipe_stdout_path[0] != '\0') {
6404: if(_access(pipe_stdout_path, 0) == 0) {
6405: SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
6406: DeleteFile(pipe_stdout_path);
6407: }
1.1.1.45 root 6408: // if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
6409: if(msdos_is_device_path(pipe_stdout_path)) {
6410: fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
6411: } else {
6412: fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
6413: }
6414: if(fd != -1) {
6415: 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 6416: psp->file_table[1] = fd;
6417: msdos_psp_set_file_table(fd, fd, current_psp);
6418: }
6419: }
1.1.1.39 root 6420: if(pipe_stderr_path[0] != '\0') {
6421: if(_access(pipe_stderr_path, 0) == 0) {
6422: SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
6423: DeleteFile(pipe_stderr_path);
6424: }
1.1.1.45 root 6425: // if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
6426: if(msdos_is_device_path(pipe_stderr_path)) {
6427: fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
6428: } else {
6429: fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
6430: }
6431: if(fd != -1) {
6432: 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 6433: psp->file_table[2] = fd;
6434: msdos_psp_set_file_table(fd, fd, current_psp);
6435: }
6436: }
1.1.1.38 root 6437:
1.1 root 6438: // registers and segments
6439: REG16(AX) = REG16(BX) = 0x00;
6440: REG16(CX) = 0xff;
6441: REG16(DX) = psp_seg;
6442: REG16(SI) = ip;
6443: REG16(DI) = sp;
6444: REG16(SP) = sp;
1.1.1.3 root 6445: SREG(DS) = SREG(ES) = psp_seg;
6446: SREG(SS) = ss;
6447: i386_load_segment_descriptor(DS);
6448: i386_load_segment_descriptor(ES);
6449: i386_load_segment_descriptor(SS);
1.1 root 6450:
6451: *(UINT16 *)(mem + (ss << 4) + sp) = 0;
6452: i386_jmp_far(cs, ip);
6453: } else if(al == 0x01) {
6454: // copy ss:sp and cs:ip to param block
6455: param->sp = sp;
6456: param->ss = ss;
6457: param->ip = ip;
6458: param->cs = cs;
1.1.1.31 root 6459:
6460: // the AX value to be passed to the child program is put on top of the child's stack
6461: *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1 root 6462: }
6463: return(0);
6464: }
6465:
6466: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
6467: {
6468: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6469:
6470: *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
6471: *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
6472: *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
6473:
1.1.1.3 root 6474: SREG(SS) = psp->stack.w.h;
6475: i386_load_segment_descriptor(SS);
1.1 root 6476: REG16(SP) = psp->stack.w.l;
6477: i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
6478:
1.1.1.28 root 6479: // process_t *current_process = msdos_process_info_get(psp_seg);
6480: process_t *current_process = NULL;
6481: for(int i = 0; i < MAX_PROCESS; i++) {
6482: if(process[i].psp == psp_seg) {
6483: current_process = &process[i];
6484: break;
6485: }
6486: }
6487: if(current_process == NULL) {
6488: throw(0x1f); // general failure
6489: }
6490: int_10h_feh_called = current_process->parent_int_10h_feh_called;
6491: int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
6492: if(current_process->called_by_int2eh) {
6493: REG16(AX) = ret;
6494: }
6495: SREG(DS) = current_process->parent_ds;
1.1.1.31 root 6496: SREG(ES) = current_process->parent_es;
1.1.1.14 root 6497: i386_load_segment_descriptor(DS);
1.1.1.31 root 6498: i386_load_segment_descriptor(ES);
1.1 root 6499:
6500: if(mem_free) {
1.1.1.8 root 6501: int mcb_seg;
6502: while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
6503: msdos_mem_free(mcb_seg + 1);
6504: }
6505: while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
6506: msdos_mem_free(mcb_seg + 1);
6507: }
1.1 root 6508:
6509: for(int i = 0; i < MAX_FILES; i++) {
6510: if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
6511: _close(i);
1.1.1.20 root 6512: msdos_file_handler_close(i);
6513: msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1 root 6514: }
6515: }
1.1.1.13 root 6516: msdos_dta_info_free(psp_seg);
1.1 root 6517: }
1.1.1.14 root 6518: msdos_stdio_reopen();
1.1 root 6519:
1.1.1.28 root 6520: memset(current_process, 0, sizeof(process_t));
1.1 root 6521:
6522: current_psp = psp->parent_psp;
6523: retval = ret;
1.1.1.23 root 6524: msdos_sda_update(current_psp);
1.1 root 6525: }
6526:
6527: // drive
6528:
1.1.1.42 root 6529: int pcbios_update_drive_param(int drive_num, int force_update);
6530:
1.1 root 6531: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
6532: {
1.1.1.41 root 6533: if(!(drive_num >= 0 && drive_num < 26)) {
6534: return(0);
6535: }
1.1.1.42 root 6536: pcbios_update_drive_param(drive_num, force_update);
6537:
6538: drive_param_t *drive_param = &drive_params[drive_num];
1.1 root 6539: *seg = DPB_TOP >> 4;
6540: *ofs = sizeof(dpb_t) * drive_num;
6541: dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
6542:
6543: memset(dpb, 0, sizeof(dpb_t));
6544:
1.1.1.41 root 6545: dpb->drive_num = drive_num;
6546: dpb->unit_num = drive_num;
1.1.1.42 root 6547:
6548: if(drive_param->valid) {
6549: DISK_GEOMETRY *geo = &drive_param->geometry;
6550:
6551: dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
6552: dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
6553: dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
6554: dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
6555: switch(geo->MediaType) {
6556: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
6557: dpb->media_type = 0xff;
6558: break;
6559: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
6560: dpb->media_type = 0xfe;
6561: break;
6562: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
6563: dpb->media_type = 0xfd;
6564: break;
6565: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
6566: dpb->media_type = 0xfc;
6567: break;
6568: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
6569: case F3_1Pt2_512:
6570: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
6571: case F5_720_512:
6572: dpb->media_type = 0xf9;
6573: break;
6574: case FixedMedia: // hard disk
6575: case RemovableMedia:
6576: case Unknown:
6577: dpb->media_type = 0xf8;
6578: break;
6579: default:
6580: dpb->media_type = 0xf0;
6581: break;
6582: }
6583: }
1.1.1.41 root 6584: dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
6585: dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42 root 6586: dpb->info_sector = 0xffff;
6587: dpb->backup_boot_sector = 0xffff;
6588: dpb->free_clusters = 0xffff;
6589: dpb->free_search_cluster = 0xffffffff;
6590:
6591: return(drive_param->valid);
1.1 root 6592: }
6593:
6594: // pc bios
6595:
1.1.1.35 root 6596: #ifdef USE_SERVICE_THREAD
6597: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
6598: {
6599: #if defined(HAS_I386)
6600: if(m_SF != 0) {
6601: m_SF = 0;
1.1.1.49 root 6602: mem[DUMMY_TOP + 0x15] = 0x79; // jns -4
1.1.1.35 root 6603: } else {
6604: m_SF = 1;
1.1.1.49 root 6605: mem[DUMMY_TOP + 0x15] = 0x78; // js -4
1.1.1.35 root 6606: }
6607: #else
6608: if(m_SignVal < 0) {
6609: m_SignVal = 0;
1.1.1.49 root 6610: mem[DUMMY_TOP + 0x15] = 0x79; // jns -4
1.1.1.35 root 6611: } else {
6612: m_SignVal = -1;
1.1.1.49 root 6613: mem[DUMMY_TOP + 0x15] = 0x78; // js -4
1.1.1.35 root 6614: }
6615: #endif
1.1.1.49 root 6616: i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35 root 6617: in_service = true;
6618: service_exit = false;
6619: CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
6620: }
6621:
6622: void finish_service_loop()
6623: {
6624: if(in_service && service_exit) {
6625: #if defined(HAS_I386)
6626: if(m_SF != 0) {
6627: m_SF = 0;
6628: } else {
6629: m_SF = 1;
6630: }
6631: #else
6632: if(m_SignVal < 0) {
6633: m_SignVal = 0;
6634: } else {
6635: m_SignVal = -1;
6636: }
6637: #endif
6638: in_service = false;
6639: }
6640: }
6641: #endif
6642:
1.1.1.19 root 6643: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
6644: {
6645: static unsigned __int64 start_msec_since_midnight = 0;
6646: static unsigned __int64 start_msec_since_hostboot = 0;
6647:
6648: if(start_msec_since_midnight == 0) {
6649: SYSTEMTIME time;
6650: GetLocalTime(&time);
6651: start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
6652: start_msec_since_hostboot = cur_msec;
6653: }
6654: unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
6655: unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
6656: return (UINT32)tick;
6657: }
6658:
6659: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
6660: {
6661: UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
6662: UINT32 next_tick = get_ticks_since_midnight(cur_msec);
6663:
6664: if(prev_tick > next_tick) {
6665: mem[0x470] = 1;
6666: }
6667: *(UINT32 *)(mem + 0x46c) = next_tick;
6668: }
6669:
1.1.1.14 root 6670: inline void pcbios_irq0()
6671: {
6672: //++*(UINT32 *)(mem + 0x46c);
1.1.1.19 root 6673: pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14 root 6674: }
6675:
1.1.1.16 root 6676: int pcbios_get_text_vram_address(int page)
1.1 root 6677: {
6678: if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6679: return TEXT_VRAM_TOP;
1.1 root 6680: } else {
1.1.1.14 root 6681: return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1 root 6682: }
6683: }
6684:
1.1.1.16 root 6685: int pcbios_get_shadow_buffer_address(int page)
1.1 root 6686: {
1.1.1.14 root 6687: if(!int_10h_feh_called) {
1.1.1.16 root 6688: return pcbios_get_text_vram_address(page);
1.1.1.14 root 6689: } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6690: return SHADOW_BUF_TOP;
6691: } else {
1.1.1.14 root 6692: return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8 root 6693: }
6694: }
6695:
1.1.1.16 root 6696: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8 root 6697: {
1.1.1.16 root 6698: return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1 root 6699: }
6700:
1.1.1.16 root 6701: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1 root 6702: {
1.1.1.14 root 6703: // clear the existing screen, not just the new one
6704: int clr_height = max(height, scr_height);
6705:
1.1.1.16 root 6706: if(scr_width != width || scr_height != height) {
6707: change_console_size(width, height);
1.1.1.14 root 6708: }
6709: mem[0x462] = 0;
6710: *(UINT16 *)(mem + 0x44e) = 0;
6711:
1.1.1.16 root 6712: text_vram_top_address = pcbios_get_text_vram_address(0);
6713: text_vram_end_address = text_vram_top_address + width * height * 2;
6714: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
6715: shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51! root 6716: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1 root 6717:
1.1.1.23 root 6718: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16 root 6719: if(clr_screen) {
1.1.1.14 root 6720: for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
6721: mem[ofs++] = 0x20;
6722: mem[ofs++] = 0x07;
6723: }
6724:
1.1.1.35 root 6725: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6726: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6727: #endif
1.1.1.14 root 6728: for(int y = 0; y < clr_height; y++) {
6729: for(int x = 0; x < scr_width; x++) {
6730: SCR_BUF(y,x).Char.AsciiChar = ' ';
6731: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 6732: }
6733: }
6734: SMALL_RECT rect;
1.1.1.14 root 6735: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
6736: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6737: vram_length_char = vram_last_length_char = 0;
6738: vram_length_attr = vram_last_length_attr = 0;
1.1.1.35 root 6739: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6740: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 6741: #endif
1.1 root 6742: }
1.1.1.14 root 6743: COORD co;
6744: co.X = 0;
6745: co.Y = scr_top;
6746: SetConsoleCursorPosition(hStdout, co);
6747: cursor_moved = true;
6748: SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1 root 6749: }
6750:
1.1.1.36 root 6751: void pcbios_update_cursor_position()
6752: {
6753: CONSOLE_SCREEN_BUFFER_INFO csbi;
6754: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
6755: if(!restore_console_on_exit) {
6756: scr_top = csbi.srWindow.Top;
6757: }
6758: mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
6759: mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
6760: }
6761:
1.1.1.16 root 6762: inline void pcbios_int_10h_00h()
6763: {
6764: switch(REG8(AL) & 0x7f) {
6765: case 0x70: // v-text mode
6766: case 0x71: // extended cga v-text mode
6767: pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
6768: break;
6769: default:
6770: pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
6771: break;
6772: }
6773: if(REG8(AL) & 0x80) {
6774: mem[0x487] |= 0x80;
6775: } else {
6776: mem[0x487] &= ~0x80;
6777: }
6778: mem[0x449] = REG8(AL) & 0x7f;
6779: }
6780:
1.1 root 6781: inline void pcbios_int_10h_01h()
6782: {
1.1.1.13 root 6783: mem[0x460] = REG8(CL);
6784: mem[0x461] = REG8(CH);
1.1.1.14 root 6785:
1.1.1.23 root 6786: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6787: CONSOLE_CURSOR_INFO ci;
6788: GetConsoleCursorInfo(hStdout, &ci);
6789: // ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
6790: // if(ci.bVisible) {
6791: int lines = max(8, REG8(CL) + 1);
6792: ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
6793: // }
6794: SetConsoleCursorInfo(hStdout, &ci);
1.1 root 6795: }
6796:
6797: inline void pcbios_int_10h_02h()
6798: {
1.1.1.14 root 6799: // continuously setting the cursor effectively stops it blinking
6800: if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1 root 6801: COORD co;
6802: co.X = REG8(DL);
1.1.1.14 root 6803: co.Y = REG8(DH) + scr_top;
6804:
6805: // some programs hide the cursor by moving it off screen
6806: static bool hidden = false;
1.1.1.23 root 6807: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6808: CONSOLE_CURSOR_INFO ci;
6809: GetConsoleCursorInfo(hStdout, &ci);
6810:
6811: if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
6812: if(ci.bVisible) {
6813: ci.bVisible = FALSE;
6814: // SetConsoleCursorInfo(hStdout, &ci);
6815: hidden = true;
6816: }
6817: } else if(hidden) {
6818: if(!ci.bVisible) {
6819: ci.bVisible = TRUE;
6820: // SetConsoleCursorInfo(hStdout, &ci);
6821: }
6822: hidden = false;
6823: }
1.1 root 6824: }
1.1.1.14 root 6825: mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
6826: mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1 root 6827: }
6828:
6829: inline void pcbios_int_10h_03h()
6830: {
1.1.1.14 root 6831: REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6832: REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 6833: REG8(CL) = mem[0x460];
6834: REG8(CH) = mem[0x461];
6835: }
6836:
6837: inline void pcbios_int_10h_05h()
6838: {
1.1.1.14 root 6839: if(REG8(AL) >= vram_pages) {
6840: return;
6841: }
6842: if(mem[0x462] != REG8(AL)) {
6843: vram_flush();
6844:
1.1.1.23 root 6845: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6846: SMALL_RECT rect;
1.1.1.14 root 6847: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6848: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6849:
1.1.1.16 root 6850: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14 root 6851: for(int x = 0; x < scr_width; x++) {
6852: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6853: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6854: }
6855: }
1.1.1.16 root 6856: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14 root 6857: for(int x = 0; x < scr_width; x++) {
6858: SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
6859: SCR_BUF(y,x).Attributes = mem[ofs++];
1.1 root 6860: }
6861: }
1.1.1.14 root 6862: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6863:
6864: COORD co;
1.1.1.14 root 6865: co.X = mem[0x450 + REG8(AL) * 2];
6866: co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
6867: if(co.Y < scr_top + scr_height) {
6868: SetConsoleCursorPosition(hStdout, co);
6869: }
1.1 root 6870: }
1.1.1.14 root 6871: mem[0x462] = REG8(AL);
6872: *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
6873: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16 root 6874: text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14 root 6875: text_vram_end_address = text_vram_top_address + regen;
1.1.1.16 root 6876: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14 root 6877: shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51! root 6878: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1 root 6879: }
6880:
6881: inline void pcbios_int_10h_06h()
6882: {
1.1.1.14 root 6883: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
6884: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
6885: return;
6886: }
6887: vram_flush();
6888:
1.1.1.23 root 6889: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6890: SMALL_RECT rect;
1.1.1.14 root 6891: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6892: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6893:
6894: int right = min(REG8(DL), scr_width - 1);
6895: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 6896:
6897: if(REG8(AL) == 0) {
1.1.1.14 root 6898: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 6899: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6900: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
6901: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6902: }
6903: }
6904: } else {
1.1.1.14 root 6905: for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16 root 6906: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6907: if(y2 <= bottom) {
6908: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 6909: } else {
1.1.1.14 root 6910: SCR_BUF(y,x).Char.AsciiChar = ' ';
6911: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6912: }
1.1.1.14 root 6913: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6914: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6915: }
6916: }
6917: }
1.1.1.14 root 6918: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6919: }
6920:
6921: inline void pcbios_int_10h_07h()
6922: {
1.1.1.14 root 6923: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
6924: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
6925: return;
6926: }
6927: vram_flush();
6928:
1.1.1.23 root 6929: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6930: SMALL_RECT rect;
1.1.1.14 root 6931: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6932: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6933:
6934: int right = min(REG8(DL), scr_width - 1);
6935: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 6936:
6937: if(REG8(AL) == 0) {
1.1.1.14 root 6938: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 6939: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6940: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
6941: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6942: }
6943: }
6944: } else {
1.1.1.14 root 6945: for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16 root 6946: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6947: if(y2 >= REG8(CH)) {
6948: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 6949: } else {
1.1.1.14 root 6950: SCR_BUF(y,x).Char.AsciiChar = ' ';
6951: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6952: }
1.1.1.14 root 6953: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6954: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6955: }
6956: }
6957: }
1.1.1.14 root 6958: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6959: }
6960:
6961: inline void pcbios_int_10h_08h()
6962: {
6963: COORD co;
6964: DWORD num;
6965:
1.1.1.14 root 6966: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6967: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 6968:
6969: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 6970: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6971: co.Y += scr_top;
6972: vram_flush();
1.1 root 6973: ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
6974: ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
6975: REG8(AL) = scr_char[0];
6976: REG8(AH) = scr_attr[0];
6977: } else {
1.1.1.16 root 6978: REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1 root 6979: }
6980: }
6981:
6982: inline void pcbios_int_10h_09h()
6983: {
6984: COORD co;
6985:
1.1.1.14 root 6986: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6987: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
6988:
1.1.1.16 root 6989: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
6990: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 6991:
6992: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 6993: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6994: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6995: #endif
1.1.1.16 root 6996: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 6997: while(dest < end) {
6998: write_text_vram_char(dest - vram, REG8(AL));
6999: mem[dest++] = REG8(AL);
7000: write_text_vram_attr(dest - vram, REG8(BL));
7001: mem[dest++] = REG8(BL);
1.1 root 7002: }
1.1.1.35 root 7003: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7004: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7005: #endif
1.1 root 7006: } else {
1.1.1.14 root 7007: while(dest < end) {
1.1 root 7008: mem[dest++] = REG8(AL);
7009: mem[dest++] = REG8(BL);
7010: }
7011: }
7012: }
7013:
7014: inline void pcbios_int_10h_0ah()
7015: {
7016: COORD co;
7017:
1.1.1.14 root 7018: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7019: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
7020:
1.1.1.16 root 7021: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
7022: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 7023:
7024: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 7025: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7026: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7027: #endif
1.1.1.16 root 7028: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 7029: while(dest < end) {
7030: write_text_vram_char(dest - vram, REG8(AL));
7031: mem[dest++] = REG8(AL);
7032: dest++;
1.1 root 7033: }
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: } else {
1.1.1.14 root 7038: while(dest < end) {
1.1 root 7039: mem[dest++] = REG8(AL);
7040: dest++;
7041: }
7042: }
7043: }
7044:
1.1.1.40 root 7045: HDC get_console_window_device_context()
7046: {
7047: static HWND hwndFound = 0;
7048:
7049: if(hwndFound == 0) {
7050: // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
7051: char pszNewWindowTitle[1024];
7052: char pszOldWindowTitle[1024];
7053:
7054: GetConsoleTitle(pszOldWindowTitle, 1024);
7055: wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
7056: SetConsoleTitle(pszNewWindowTitle);
7057: Sleep(100);
7058: hwndFound = FindWindow(NULL, pszNewWindowTitle);
7059: SetConsoleTitle(pszOldWindowTitle);
7060: }
7061: return GetDC(hwndFound);
7062: }
7063:
7064: inline void pcbios_int_10h_0ch()
7065: {
7066: HDC hdc = get_console_window_device_context();
7067:
7068: if(hdc != NULL) {
7069: BYTE r = (REG8(AL) & 2) ? 255 : 0;
7070: BYTE g = (REG8(AL) & 4) ? 255 : 0;
7071: BYTE b = (REG8(AL) & 1) ? 255 : 0;
7072:
7073: if(REG8(AL) & 0x80) {
7074: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
7075: if(color != CLR_INVALID) {
7076: r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
7077: g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
7078: b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
7079: }
7080: }
7081: SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
7082: }
7083: }
7084:
7085: inline void pcbios_int_10h_0dh()
7086: {
7087: HDC hdc = get_console_window_device_context();
7088: BYTE r = 0;
7089: BYTE g = 0;
7090: BYTE b = 0;
7091:
7092: if(hdc != NULL) {
7093: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
7094: if(color != CLR_INVALID) {
7095: r = ((DWORD)color & 0x0000ff) ? 255 : 0;
7096: g = ((DWORD)color & 0x00ff00) ? 255 : 0;
7097: b = ((DWORD)color & 0xff0000) ? 255 : 0;
7098: }
7099: }
7100: REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
7101: }
7102:
1.1 root 7103: inline void pcbios_int_10h_0eh()
7104: {
1.1.1.14 root 7105: DWORD num;
7106: COORD co;
7107:
7108: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
7109: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
7110:
7111: if(REG8(AL) == 7) {
7112: //MessageBeep(-1);
7113: } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
7114: if(REG8(AL) == 10) {
7115: vram_flush();
7116: }
1.1.1.23 root 7117: WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), ®8(AL), 1, &num, NULL);
1.1.1.14 root 7118: cursor_moved = true;
7119: } else {
1.1.1.16 root 7120: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14 root 7121: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 7122: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7123: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 7124: #endif
1.1.1.16 root 7125: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 7126: write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35 root 7127: #ifdef USE_VRAM_THREAD
1.1.1.14 root 7128: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 7129: #endif
1.1.1.14 root 7130:
1.1.1.23 root 7131: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7132: if(++co.X == scr_width) {
7133: co.X = 0;
7134: if(++co.Y == scr_height) {
7135: vram_flush();
7136: WriteConsole(hStdout, "\n", 1, &num, NULL);
7137: cursor_moved = true;
7138: }
7139: }
7140: if(!cursor_moved) {
7141: co.Y += scr_top;
7142: SetConsoleCursorPosition(hStdout, co);
7143: cursor_moved = true;
7144: }
7145: }
7146: mem[dest] = REG8(AL);
7147: }
1.1 root 7148: }
7149:
7150: inline void pcbios_int_10h_0fh()
7151: {
7152: REG8(AL) = mem[0x449];
7153: REG8(AH) = mem[0x44a];
7154: REG8(BH) = mem[0x462];
7155: }
7156:
1.1.1.14 root 7157: inline void pcbios_int_10h_11h()
7158: {
7159: switch(REG8(AL)) {
1.1.1.16 root 7160: case 0x01:
1.1.1.14 root 7161: case 0x11:
1.1.1.16 root 7162: pcbios_set_console_size(80, 28, true);
1.1.1.14 root 7163: break;
1.1.1.16 root 7164: case 0x02:
1.1.1.14 root 7165: case 0x12:
1.1.1.16 root 7166: pcbios_set_console_size(80, 50, true);
1.1.1.14 root 7167: break;
1.1.1.16 root 7168: case 0x04:
1.1.1.14 root 7169: case 0x14:
1.1.1.16 root 7170: pcbios_set_console_size(80, 25, true);
7171: break;
7172: case 0x18:
7173: pcbios_set_console_size(80, 50, true);
1.1.1.14 root 7174: break;
7175: case 0x30:
7176: SREG(ES) = 0;
7177: i386_load_segment_descriptor(ES);
7178: REG16(BP) = 0;
7179: REG16(CX) = mem[0x485];
7180: REG8(DL) = mem[0x484];
7181: break;
7182: }
7183: }
7184:
7185: inline void pcbios_int_10h_12h()
7186: {
1.1.1.16 root 7187: switch(REG8(BL)) {
7188: case 0x10:
1.1.1.14 root 7189: REG16(BX) = 0x0003;
7190: REG16(CX) = 0x0009;
1.1.1.16 root 7191: break;
1.1.1.14 root 7192: }
7193: }
7194:
1.1 root 7195: inline void pcbios_int_10h_13h()
7196: {
1.1.1.3 root 7197: int ofs = SREG_BASE(ES) + REG16(BP);
1.1 root 7198: COORD co;
7199: DWORD num;
7200:
7201: co.X = REG8(DL);
1.1.1.14 root 7202: co.Y = REG8(DH) + scr_top;
7203:
7204: vram_flush();
1.1 root 7205:
7206: switch(REG8(AL)) {
7207: case 0x00:
7208: case 0x01:
7209: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7210: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7211: CONSOLE_SCREEN_BUFFER_INFO csbi;
7212: GetConsoleScreenBufferInfo(hStdout, &csbi);
7213: SetConsoleCursorPosition(hStdout, co);
7214:
7215: if(csbi.wAttributes != REG8(BL)) {
7216: SetConsoleTextAttribute(hStdout, REG8(BL));
7217: }
1.1.1.14 root 7218: WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
7219:
1.1 root 7220: if(csbi.wAttributes != REG8(BL)) {
7221: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
7222: }
7223: if(REG8(AL) == 0x00) {
1.1.1.15 root 7224: if(!restore_console_on_exit) {
7225: GetConsoleScreenBufferInfo(hStdout, &csbi);
7226: scr_top = csbi.srWindow.Top;
7227: }
1.1.1.14 root 7228: co.X = mem[0x450 + REG8(BH) * 2];
7229: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 7230: SetConsoleCursorPosition(hStdout, co);
7231: } else {
7232: cursor_moved = true;
7233: }
7234: } else {
1.1.1.3 root 7235: m_CF = 1;
1.1 root 7236: }
7237: break;
7238: case 0x02:
7239: case 0x03:
7240: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7241: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7242: CONSOLE_SCREEN_BUFFER_INFO csbi;
7243: GetConsoleScreenBufferInfo(hStdout, &csbi);
7244: SetConsoleCursorPosition(hStdout, co);
7245:
7246: WORD wAttributes = csbi.wAttributes;
7247: for(int i = 0; i < REG16(CX); i++, ofs += 2) {
7248: if(wAttributes != mem[ofs + 1]) {
7249: SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
7250: wAttributes = mem[ofs + 1];
7251: }
1.1.1.14 root 7252: WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1 root 7253: }
7254: if(csbi.wAttributes != wAttributes) {
7255: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
7256: }
7257: if(REG8(AL) == 0x02) {
1.1.1.14 root 7258: co.X = mem[0x450 + REG8(BH) * 2];
7259: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 7260: SetConsoleCursorPosition(hStdout, co);
7261: } else {
7262: cursor_moved = true;
7263: }
7264: } else {
1.1.1.3 root 7265: m_CF = 1;
1.1 root 7266: }
7267: break;
7268: case 0x10:
7269: case 0x11:
7270: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7271: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7272: ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
7273: ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
7274: for(int i = 0; i < num; i++) {
7275: mem[ofs++] = scr_char[i];
7276: mem[ofs++] = scr_attr[i];
1.1.1.45 root 7277: if(REG8(AL) & 0x01) {
1.1 root 7278: mem[ofs++] = 0;
7279: mem[ofs++] = 0;
7280: }
7281: }
7282: } else {
1.1.1.16 root 7283: 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 7284: mem[ofs++] = mem[src++];
7285: mem[ofs++] = mem[src++];
1.1.1.45 root 7286: if(REG8(AL) & 0x01) {
1.1 root 7287: mem[ofs++] = 0;
7288: mem[ofs++] = 0;
7289: }
1.1.1.14 root 7290: if(++co.X == scr_width) {
7291: if(++co.Y == scr_height) {
1.1 root 7292: break;
7293: }
7294: co.X = 0;
7295: }
7296: }
7297: }
7298: break;
1.1.1.45 root 7299: case 0x12: // ???
7300: case 0x13: // ???
1.1 root 7301: case 0x20:
7302: case 0x21:
7303: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7304: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7305: int len = min(REG16(CX), scr_width * scr_height);
7306: for(int i = 0; i < len; i++) {
1.1 root 7307: scr_char[i] = mem[ofs++];
7308: scr_attr[i] = mem[ofs++];
1.1.1.45 root 7309: if(REG8(AL) & 0x01) {
1.1 root 7310: ofs += 2;
7311: }
7312: }
1.1.1.14 root 7313: WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
7314: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7315: } else {
1.1.1.16 root 7316: 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 7317: mem[dest++] = mem[ofs++];
7318: mem[dest++] = mem[ofs++];
1.1.1.45 root 7319: if(REG8(AL) & 0x01) {
1.1 root 7320: ofs += 2;
7321: }
1.1.1.14 root 7322: if(++co.X == scr_width) {
7323: if(++co.Y == scr_height) {
1.1 root 7324: break;
7325: }
7326: co.X = 0;
7327: }
7328: }
7329: }
7330: break;
7331: default:
1.1.1.22 root 7332: 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 7333: m_CF = 1;
1.1 root 7334: break;
7335: }
7336: }
7337:
1.1.1.30 root 7338: inline void pcbios_int_10h_18h()
7339: {
7340: switch(REG8(AL)) {
7341: case 0x00:
7342: case 0x01:
7343: // REG8(AL) = 0x86;
7344: REG8(AL) = 0x00;
7345: break;
7346: default:
7347: 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));
7348: m_CF = 1;
7349: break;
7350: }
7351: }
7352:
1.1.1.14 root 7353: inline void pcbios_int_10h_1ah()
7354: {
7355: switch(REG8(AL)) {
7356: case 0x00:
7357: REG8(AL) = 0x1a;
7358: REG8(BL) = 0x08;
7359: REG8(BH) = 0x00;
7360: break;
7361: default:
1.1.1.22 root 7362: 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 7363: m_CF = 1;
7364: break;
7365: }
7366: }
7367:
1.1 root 7368: inline void pcbios_int_10h_1dh()
7369: {
7370: switch(REG8(AL)) {
1.1.1.43 root 7371: case 0x00:
7372: // DOS/V Shift Status Line Control is not supported
7373: m_CF = 1;
7374: break;
1.1 root 7375: case 0x01:
7376: break;
7377: case 0x02:
7378: REG16(BX) = 0;
7379: break;
7380: default:
1.1.1.22 root 7381: 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));
7382: m_CF = 1;
7383: break;
7384: }
7385: }
7386:
7387: inline void pcbios_int_10h_4fh()
7388: {
7389: switch(REG8(AL)) {
7390: case 0x00:
7391: REG8(AH) = 0x02; // not supported
7392: break;
7393: case 0x01:
7394: case 0x02:
7395: case 0x03:
7396: case 0x04:
7397: case 0x05:
7398: case 0x06:
7399: case 0x07:
7400: case 0x08:
7401: case 0x09:
7402: case 0x0a:
7403: case 0x0b:
7404: case 0x0c:
7405: REG8(AH) = 0x01; // failed
7406: break;
7407: default:
7408: 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 7409: m_CF = 1;
1.1 root 7410: break;
7411: }
7412: }
7413:
7414: inline void pcbios_int_10h_82h()
7415: {
7416: static UINT8 mode = 0;
7417:
7418: switch(REG8(AL)) {
1.1.1.22 root 7419: case 0x00:
1.1 root 7420: if(REG8(BL) != 0xff) {
7421: mode = REG8(BL);
7422: }
7423: REG8(AL) = mode;
7424: break;
7425: default:
1.1.1.22 root 7426: 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 7427: m_CF = 1;
1.1 root 7428: break;
7429: }
7430: }
7431:
1.1.1.22 root 7432: inline void pcbios_int_10h_83h()
7433: {
7434: static UINT8 mode = 0;
7435:
7436: switch(REG8(AL)) {
7437: case 0x00:
7438: REG16(AX) = 0; // offset???
7439: SREG(ES) = (SHADOW_BUF_TOP >> 4);
7440: i386_load_segment_descriptor(ES);
7441: REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
7442: break;
7443: default:
7444: 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));
7445: m_CF = 1;
7446: break;
7447: }
7448: }
7449:
7450: inline void pcbios_int_10h_90h()
7451: {
7452: REG8(AL) = mem[0x449];
7453: }
7454:
7455: inline void pcbios_int_10h_91h()
7456: {
7457: REG8(AL) = 0x04; // VGA
7458: }
7459:
7460: inline void pcbios_int_10h_efh()
7461: {
7462: REG16(DX) = 0xffff;
7463: }
7464:
1.1 root 7465: inline void pcbios_int_10h_feh()
7466: {
7467: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 7468: SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3 root 7469: i386_load_segment_descriptor(ES);
1.1.1.8 root 7470: REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1 root 7471: }
7472: int_10h_feh_called = true;
7473: }
7474:
7475: inline void pcbios_int_10h_ffh()
7476: {
7477: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23 root 7478: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7479: COORD co;
7480: DWORD num;
7481:
1.1.1.14 root 7482: vram_flush();
7483:
7484: co.X = (REG16(DI) >> 1) % scr_width;
7485: co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16 root 7486: int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
7487: int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14 root 7488: int len;
7489: for(len = 0; ofs < end; len++) {
7490: scr_char[len] = mem[ofs++];
7491: scr_attr[len] = mem[ofs++];
7492: }
7493: co.Y += scr_top;
7494: WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
7495: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7496: }
7497: int_10h_ffh_called = true;
7498: }
7499:
1.1.1.42 root 7500: int pcbios_update_drive_param(int drive_num, int force_update)
7501: {
7502: if(drive_num >= 0 && drive_num < 26) {
7503: drive_param_t *drive_param = &drive_params[drive_num];
7504:
7505: if(force_update || !drive_param->initialized) {
7506: drive_param->valid = 0;
7507: char dev[64];
7508: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7509:
7510: HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
7511: if(hFile != INVALID_HANDLE_VALUE) {
7512: DWORD dwSize;
7513: if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
7514: drive_param->valid = 1;
7515: }
7516: CloseHandle(hFile);
7517: }
7518: drive_param->initialized = 1;
7519: }
7520: return(drive_param->valid);
7521: }
7522: return(0);
7523: }
7524:
7525: inline void pcbios_int_13h_00h()
7526: {
7527: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7528:
7529: if(pcbios_update_drive_param(drive_num, 1)) {
7530: REG8(AH) = 0x00; // successful completion
7531: } else {
7532: if(REG8(DL) & 0x80) {
7533: REG8(AH) = 0x05; // reset failed (hard disk)
7534: } else {
7535: REG8(AH) = 0x80; // timeout (not ready)
7536: }
7537: m_CF = 1;
7538: }
7539: }
7540:
7541: inline void pcbios_int_13h_02h()
7542: {
7543: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7544:
7545: if(REG8(AL) == 0) {
7546: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7547: m_CF = 1;
7548: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7549: REG8(AH) = 0xff; // sense operation failed (hard disk)
7550: m_CF = 1;
7551: } else {
7552: drive_param_t *drive_param = &drive_params[drive_num];
7553: DISK_GEOMETRY *geo = &drive_param->geometry;
7554: char dev[64];
7555: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7556:
7557: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
7558: if(hFile == INVALID_HANDLE_VALUE) {
7559: REG8(AH) = 0xff; // sense operation failed (hard disk)
7560: m_CF = 1;
7561: } else {
7562: UINT32 sector_num = REG8(AL);
7563: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7564: UINT32 head = REG8(DH);
7565: UINT32 sector = REG8(CL) & 0x3f;
7566: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
7567: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
7568: DWORD dwSize;
7569:
7570: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
7571: // REG8(AH) = 0xff; // sense operation failed (hard disk)
7572: // m_CF = 1;
7573: // } else
7574: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
7575: REG8(AH) = 0x04; // sector not found/read error
7576: m_CF = 1;
7577: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
7578: REG8(AH) = 0x04; // sector not found/read error
7579: m_CF = 1;
7580: } else {
7581: REG8(AH) = 0x00; // successful completion
7582: }
7583: CloseHandle(hFile);
7584: }
7585: }
7586: }
7587:
7588: inline void pcbios_int_13h_03h()
7589: {
7590: // this operation may cause serious damage for drives, so support only floppy disk...
7591: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7592:
7593: if(REG8(AL) == 0) {
7594: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7595: m_CF = 1;
7596: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7597: REG8(AH) = 0xff; // sense operation failed (hard disk)
7598: m_CF = 1;
7599: } else if(!drive_params[drive_num].is_fdd()) {
7600: REG8(AH) = 0xff; // sense operation failed (hard disk)
7601: m_CF = 1;
7602: } else {
7603: drive_param_t *drive_param = &drive_params[drive_num];
7604: DISK_GEOMETRY *geo = &drive_param->geometry;
7605: char dev[64];
7606: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7607:
7608: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
7609: if(hFile == INVALID_HANDLE_VALUE) {
7610: REG8(AH) = 0xff; // sense operation failed (hard disk)
7611: m_CF = 1;
7612: } else {
7613: UINT32 sector_num = REG8(AL);
7614: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7615: UINT32 head = REG8(DH);
7616: UINT32 sector = REG8(CL) & 0x3f;
7617: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
7618: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
7619: DWORD dwSize;
7620:
7621: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
7622: // REG8(AH) = 0xff; // sense operation failed (hard disk)
7623: // m_CF = 1;
7624: // } else
7625: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
7626: REG8(AH) = 0x04; // sector not found/read error
7627: m_CF = 1;
7628: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
7629: REG8(AH) = 0x04; // sector not found/read error
7630: m_CF = 1;
7631: } else {
7632: REG8(AH) = 0x00; // successful completion
7633: }
7634: CloseHandle(hFile);
7635: }
7636: }
7637: }
7638:
7639: inline void pcbios_int_13h_04h()
7640: {
7641: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7642:
7643: if(REG8(AL) == 0) {
7644: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
7645: m_CF = 1;
7646: } else if(!pcbios_update_drive_param(drive_num, 0)) {
7647: REG8(AH) = 0xff; // sense operation failed (hard disk)
7648: m_CF = 1;
7649: } else {
7650: drive_param_t *drive_param = &drive_params[drive_num];
7651: DISK_GEOMETRY *geo = &drive_param->geometry;
7652: char dev[64];
7653: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
7654:
7655: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
7656: if(hFile == INVALID_HANDLE_VALUE) {
7657: REG8(AH) = 0xff; // sense operation failed (hard disk)
7658: m_CF = 1;
7659: } else {
7660: UINT32 sector_num = REG8(AL);
7661: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
7662: UINT32 head = REG8(DH);
7663: UINT32 sector = REG8(CL) & 0x3f;
7664: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
7665: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
7666: DWORD dwSize;
7667: UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
7668:
7669: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
7670: // REG8(AH) = 0xff; // sense operation failed (hard disk)
7671: // m_CF = 1;
7672: // } else
7673: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
7674: REG8(AH) = 0x04; // sector not found/read error
7675: m_CF = 1;
7676: } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
7677: REG8(AH) = 0x04; // sector not found/read error
7678: m_CF = 1;
7679: } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
7680: REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
7681: m_CF = 1;
7682: } else {
7683: REG8(AH) = 0x00; // successful completion
7684: }
7685: free(tmp_buffer);
7686: CloseHandle(hFile);
7687: }
7688: }
7689: }
7690:
7691: inline void pcbios_int_13h_08h()
7692: {
7693: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7694:
7695: if(pcbios_update_drive_param(drive_num, 1)) {
7696: drive_param_t *drive_param = &drive_params[drive_num];
7697: DISK_GEOMETRY *geo = &drive_param->geometry;
7698:
7699: REG16(AX) = 0x0000;
7700: switch(geo->MediaType) {
7701: case F5_360_512:
7702: case F5_320_512:
7703: case F5_320_1024:
7704: case F5_180_512:
7705: case F5_160_512:
7706: REG8(BL) = 0x01; // 320K/360K disk
7707: break;
7708: case F5_1Pt2_512:
7709: case F3_1Pt2_512:
7710: case F3_1Pt23_1024:
7711: case F5_1Pt23_1024:
7712: REG8(BL) = 0x02; // 1.2M disk
7713: break;
7714: case F3_720_512:
7715: case F3_640_512:
7716: case F5_640_512:
7717: case F5_720_512:
7718: REG8(BL) = 0x03; // 720K disk
7719: break;
7720: case F3_1Pt44_512:
7721: REG8(BL) = 0x04; // 1.44M disk
7722: break;
7723: case F3_2Pt88_512:
7724: REG8(BL) = 0x06; // 2.88M disk
7725: break;
7726: case RemovableMedia:
7727: REG8(BL) = 0x10; // ATAPI Removable Media Device
7728: break;
7729: default:
7730: REG8(BL) = 0x00; // unknown
7731: break;
7732: }
7733: if(REG8(DL) & 0x80) {
7734: switch(GetLogicalDrives() & 0x0c) {
7735: case 0x00: REG8(DL) = 0x00; break;
7736: case 0x04:
7737: case 0x08: REG8(DL) = 0x01; break;
7738: case 0x0c: REG8(DL) = 0x02; break;
7739: }
7740: } else {
7741: switch(GetLogicalDrives() & 0x03) {
7742: case 0x00: REG8(DL) = 0x00; break;
7743: case 0x01:
7744: case 0x02: REG8(DL) = 0x01; break;
7745: case 0x03: REG8(DL) = 0x02; break;
7746: }
7747: }
7748: REG8(DH) = drive_param->head_num();
7749: int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
7750: int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
7751: REG8(CH) = cyl & 0xff;
7752: REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
7753: } else {
7754: REG8(AH) = 0x07;
7755: m_CF = 1;
7756: }
7757: }
7758:
7759: inline void pcbios_int_13h_10h()
7760: {
7761: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7762:
7763: if(pcbios_update_drive_param(drive_num, 1)) {
7764: REG8(AH) = 0x00; // successful completion
7765: } else {
7766: if(REG8(DL) & 0x80) {
7767: REG8(AH) = 0xaa; // drive not ready (hard disk)
7768: } else {
7769: REG8(AH) = 0x80; // timeout (not ready)
7770: }
7771: m_CF = 1;
7772: }
7773: }
7774:
7775: inline void pcbios_int_13h_15h()
7776: {
7777: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
7778:
7779: if(pcbios_update_drive_param(drive_num, 1)) {
7780: if(REG8(DL) & 0x80) {
7781: REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
7782: } else {
7783: REG8(AH) = 0x03; // hard disk
7784: }
7785: } else {
7786: REG8(AH) = 0x00; // no such drive
7787: }
7788: }
7789:
1.1.1.43 root 7790: inline void pcbios_int_13h_41h()
7791: {
7792: if(REG16(BX) == 0x55aa) {
7793: // IBM/MS INT 13 Extensions is not installed
7794: REG8(AH) = 0x01;
7795: m_CF = 1;
7796: } else {
7797: 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));
7798: REG8(AH) = 0x01;
7799: m_CF = 1;
7800: }
7801: }
7802:
1.1.1.25 root 7803: inline void pcbios_int_14h_00h()
7804: {
1.1.1.29 root 7805: if(REG16(DX) < 4) {
1.1.1.25 root 7806: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
7807: UINT8 selector = sio_read(REG16(DX), 3);
7808: selector &= ~0x3f;
7809: selector |= REG8(AL) & 0x1f;
7810: UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
7811: sio_write(REG16(DX), 3, selector | 0x80);
7812: sio_write(REG16(DX), 0, divisor & 0xff);
7813: sio_write(REG16(DX), 1, divisor >> 8);
7814: sio_write(REG16(DX), 3, selector);
7815: REG8(AH) = sio_read(REG16(DX), 5);
7816: REG8(AL) = sio_read(REG16(DX), 6);
7817: } else {
7818: REG8(AH) = 0x80;
7819: }
7820: }
7821:
7822: inline void pcbios_int_14h_01h()
7823: {
1.1.1.29 root 7824: if(REG16(DX) < 4) {
1.1.1.25 root 7825: UINT8 selector = sio_read(REG16(DX), 3);
7826: sio_write(REG16(DX), 3, selector & ~0x80);
7827: sio_write(REG16(DX), 0, REG8(AL));
7828: sio_write(REG16(DX), 3, selector);
7829: REG8(AH) = sio_read(REG16(DX), 5);
7830: } else {
7831: REG8(AH) = 0x80;
7832: }
7833: }
7834:
7835: inline void pcbios_int_14h_02h()
7836: {
1.1.1.29 root 7837: if(REG16(DX) < 4) {
1.1.1.25 root 7838: UINT8 selector = sio_read(REG16(DX), 3);
7839: sio_write(REG16(DX), 3, selector & ~0x80);
7840: REG8(AL) = sio_read(REG16(DX), 0);
7841: sio_write(REG16(DX), 3, selector);
7842: REG8(AH) = sio_read(REG16(DX), 5);
7843: } else {
7844: REG8(AH) = 0x80;
7845: }
7846: }
7847:
7848: inline void pcbios_int_14h_03h()
7849: {
1.1.1.29 root 7850: if(REG16(DX) < 4) {
1.1.1.25 root 7851: REG8(AH) = sio_read(REG16(DX), 5);
7852: REG8(AL) = sio_read(REG16(DX), 6);
7853: } else {
7854: REG8(AH) = 0x80;
7855: }
7856: }
7857:
7858: inline void pcbios_int_14h_04h()
7859: {
1.1.1.29 root 7860: if(REG16(DX) < 4) {
1.1.1.25 root 7861: UINT8 selector = sio_read(REG16(DX), 3);
7862: if(REG8(CH) <= 0x03) {
7863: selector = (selector & ~0x03) | REG8(CH);
7864: }
7865: if(REG8(BL) == 0x00) {
7866: selector &= ~0x04;
7867: } else if(REG8(BL) == 0x01) {
7868: selector |= 0x04;
7869: }
7870: if(REG8(BH) == 0x00) {
7871: selector = (selector & ~0x38) | 0x00;
7872: } else if(REG8(BH) == 0x01) {
7873: selector = (selector & ~0x38) | 0x08;
7874: } else if(REG8(BH) == 0x02) {
7875: selector = (selector & ~0x38) | 0x18;
7876: } else if(REG8(BH) == 0x03) {
7877: selector = (selector & ~0x38) | 0x28;
7878: } else if(REG8(BH) == 0x04) {
7879: selector = (selector & ~0x38) | 0x38;
7880: }
7881: if(REG8(AL) == 0x00) {
7882: selector |= 0x40;
7883: } else if(REG8(AL) == 0x01) {
7884: selector &= ~0x40;
7885: }
7886: if(REG8(CL) <= 0x0b) {
7887: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
7888: UINT16 divisor = 115200 / rate[REG8(CL)];
7889: sio_write(REG16(DX), 3, selector | 0x80);
7890: sio_write(REG16(DX), 0, divisor & 0xff);
7891: sio_write(REG16(DX), 1, divisor >> 8);
7892: }
7893: sio_write(REG16(DX), 3, selector);
7894: REG8(AH) = sio_read(REG16(DX), 5);
7895: REG8(AL) = sio_read(REG16(DX), 6);
7896: } else {
7897: REG8(AH) = 0x80;
7898: }
7899: }
7900:
7901: inline void pcbios_int_14h_05h()
7902: {
1.1.1.29 root 7903: if(REG16(DX) < 4) {
1.1.1.25 root 7904: if(REG8(AL) == 0x00) {
7905: REG8(BL) = sio_read(REG16(DX), 4);
7906: REG8(AH) = sio_read(REG16(DX), 5);
7907: REG8(AL) = sio_read(REG16(DX), 6);
7908: } else if(REG8(AL) == 0x01) {
7909: sio_write(REG16(DX), 4, REG8(BL));
7910: REG8(AH) = sio_read(REG16(DX), 5);
7911: REG8(AL) = sio_read(REG16(DX), 6);
7912: } else {
7913: 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));
7914: }
7915: } else {
7916: REG8(AH) = 0x80;
7917: }
7918: }
7919:
1.1.1.14 root 7920: inline void pcbios_int_15h_10h()
7921: {
1.1.1.22 root 7922: switch(REG8(AL)) {
7923: case 0x00:
1.1.1.14 root 7924: Sleep(10);
1.1.1.35 root 7925: REQUEST_HARDWRE_UPDATE();
1.1.1.22 root 7926: break;
7927: default:
7928: 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 7929: REG8(AH) = 0x86;
7930: m_CF = 1;
7931: }
7932: }
7933:
1.1 root 7934: inline void pcbios_int_15h_23h()
7935: {
7936: switch(REG8(AL)) {
1.1.1.22 root 7937: case 0x00:
1.1.1.8 root 7938: REG8(CL) = cmos_read(0x2d);
7939: REG8(CH) = cmos_read(0x2e);
1.1 root 7940: break;
1.1.1.22 root 7941: case 0x01:
1.1.1.8 root 7942: cmos_write(0x2d, REG8(CL));
7943: cmos_write(0x2e, REG8(CH));
1.1 root 7944: break;
7945: default:
1.1.1.22 root 7946: 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 7947: REG8(AH) = 0x86;
1.1.1.3 root 7948: m_CF = 1;
1.1 root 7949: break;
7950: }
7951: }
7952:
7953: inline void pcbios_int_15h_24h()
7954: {
7955: switch(REG8(AL)) {
1.1.1.22 root 7956: case 0x00:
1.1.1.3 root 7957: i386_set_a20_line(0);
1.1 root 7958: REG8(AH) = 0;
7959: break;
1.1.1.22 root 7960: case 0x01:
1.1.1.3 root 7961: i386_set_a20_line(1);
1.1 root 7962: REG8(AH) = 0;
7963: break;
1.1.1.22 root 7964: case 0x02:
1.1 root 7965: REG8(AH) = 0;
1.1.1.3 root 7966: REG8(AL) = (m_a20_mask >> 20) & 1;
1.1 root 7967: REG16(CX) = 0;
7968: break;
1.1.1.22 root 7969: case 0x03:
1.1 root 7970: REG16(AX) = 0;
7971: REG16(BX) = 0;
7972: break;
1.1.1.22 root 7973: default:
7974: 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));
7975: REG8(AH) = 0x86;
7976: m_CF = 1;
7977: break;
1.1 root 7978: }
7979: }
7980:
7981: inline void pcbios_int_15h_49h()
7982: {
1.1.1.27 root 7983: REG8(AH) = 0x00;
7984: REG8(BL) = 0x00; // DOS/V
1.1 root 7985: }
7986:
1.1.1.22 root 7987: inline void pcbios_int_15h_50h()
7988: {
7989: switch(REG8(AL)) {
7990: case 0x00:
7991: case 0x01:
7992: if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
7993: REG8(AH) = 0x01; // invalid font type in bh
7994: m_CF = 1;
1.1.1.27 root 7995: } else if(REG8(BL) != 0x00) {
1.1.1.22 root 7996: REG8(AH) = 0x02; // bl not zero
7997: m_CF = 1;
7998: } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
7999: REG8(AH) = 0x04; // invalid code page
8000: m_CF = 1;
1.1.1.27 root 8001: } else if(REG8(AL) == 0x01) {
8002: REG8(AH) = 0x06; // font is read only
1.1.1.22 root 8003: m_CF = 1;
1.1.1.27 root 8004: } else {
1.1.1.49 root 8005: // dummy font read routine is at fffc:000d
8006: SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27 root 8007: i386_load_segment_descriptor(ES);
1.1.1.32 root 8008: REG16(BX) = 0x000d;
1.1.1.27 root 8009: REG8(AH) = 0x00; // success
1.1.1.22 root 8010: }
8011: break;
8012: default:
8013: 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));
8014: REG8(AH) = 0x86;
8015: m_CF = 1;
8016: break;
8017: }
8018: }
8019:
1.1.1.30 root 8020: inline void pcbios_int_15h_53h()
8021: {
8022: switch(REG8(AL)) {
8023: case 0x00:
8024: // APM is not installed
8025: REG8(AH) = 0x86;
8026: m_CF = 1;
8027: break;
8028: default:
8029: 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));
8030: REG8(AH) = 0x86;
8031: m_CF = 1;
8032: break;
8033: }
8034: }
8035:
1.1.1.43 root 8036: inline void pcbios_int_15h_84h()
8037: {
8038: // joystick support (from DOSBox)
8039: switch(REG16(DX)) {
8040: case 0x00:
8041: REG16(AX) = 0x00f0;
8042: REG16(DX) = 0x0201;
8043: m_CF = 1;
8044: break;
8045: case 0x01:
8046: REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
8047: m_CF = 1;
8048: break;
8049: default:
8050: 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));
8051: REG8(AH) = 0x86;
8052: m_CF = 1;
8053: break;
8054: }
8055: }
1.1.1.35 root 8056:
8057: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1 root 8058: {
8059: UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14 root 8060: UINT32 msec = usec / 1000;
8061:
1.1.1.35 root 8062: while(msec && !m_halted) {
1.1.1.14 root 8063: UINT32 tmp = min(msec, 100);
8064: if(msec - tmp < 10) {
8065: tmp = msec;
8066: }
8067: Sleep(tmp);
8068: msec -= tmp;
8069: }
1.1.1.35 root 8070:
8071: #ifdef USE_SERVICE_THREAD
8072: service_exit = true;
8073: #endif
8074: return(0);
8075: }
8076:
8077: inline void pcbios_int_15h_86h()
8078: {
8079: if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
8080: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 8081: if(!in_service && !in_service_29h) {
! 8082: start_service_loop(pcbios_int_15h_86h_thread);
! 8083: } else {
! 8084: #endif
! 8085: pcbios_int_15h_86h_thread(NULL);
! 8086: REQUEST_HARDWRE_UPDATE();
! 8087: #ifdef USE_SERVICE_THREAD
! 8088: }
1.1.1.35 root 8089: #endif
8090: }
1.1 root 8091: }
8092:
8093: inline void pcbios_int_15h_87h()
8094: {
8095: // copy extended memory (from DOSBox)
8096: int len = REG16(CX) * 2;
1.1.1.3 root 8097: int ofs = SREG_BASE(ES) + REG16(SI);
1.1 root 8098: int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
8099: int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
8100: memcpy(mem + dst, mem + src, len);
8101: REG16(AX) = 0x00;
8102: }
8103:
8104: inline void pcbios_int_15h_88h()
8105: {
1.1.1.17 root 8106: REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1 root 8107: }
8108:
8109: inline void pcbios_int_15h_89h()
8110: {
1.1.1.21 root 8111: #if defined(HAS_I286) || defined(HAS_I386)
1.1 root 8112: // switch to protected mode (from DOSBox)
8113: write_io_byte(0x20, 0x10);
8114: write_io_byte(0x21, REG8(BH));
8115: write_io_byte(0x21, 0x00);
8116: write_io_byte(0xa0, 0x10);
8117: write_io_byte(0xa1, REG8(BL));
8118: write_io_byte(0xa1, 0x00);
1.1.1.3 root 8119: i386_set_a20_line(1);
8120: int ofs = SREG_BASE(ES) + REG16(SI);
8121: m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
8122: m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
8123: m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
8124: m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
8125: #if defined(HAS_I386)
8126: m_cr[0] |= 1;
8127: #else
8128: m_msw |= 1;
8129: #endif
8130: SREG(DS) = 0x18;
8131: SREG(ES) = 0x20;
8132: SREG(SS) = 0x28;
8133: i386_load_segment_descriptor(DS);
8134: i386_load_segment_descriptor(ES);
8135: i386_load_segment_descriptor(SS);
1.1.1.21 root 8136: UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1 root 8137: REG16(SP) += 6;
1.1.1.3 root 8138: #if defined(HAS_I386)
1.1.1.21 root 8139: UINT32 flags = get_flags();
8140: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
8141: set_flags(flags);
1.1.1.3 root 8142: #else
1.1.1.21 root 8143: UINT32 flags = CompressFlags();
8144: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
8145: ExpandFlags(flags);
1.1.1.3 root 8146: #endif
1.1 root 8147: REG16(AX) = 0x00;
1.1.1.21 root 8148: i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1 root 8149: #else
1.1.1.21 root 8150: // i86/i186/v30: protected mode is not supported
1.1 root 8151: REG8(AH) = 0x86;
1.1.1.3 root 8152: m_CF = 1;
1.1 root 8153: #endif
8154: }
8155:
1.1.1.21 root 8156: inline void pcbios_int_15h_8ah()
8157: {
8158: UINT32 size = MAX_MEM - 0x100000;
8159: REG16(AX) = size & 0xffff;
8160: REG16(DX) = size >> 16;
8161: }
8162:
1.1.1.3 root 8163: #if defined(HAS_I386)
1.1 root 8164: inline void pcbios_int_15h_c9h()
8165: {
8166: REG8(AH) = 0x00;
8167: REG8(CH) = cpu_type;
8168: REG8(CL) = cpu_step;
8169: }
1.1.1.3 root 8170: #endif
1.1 root 8171:
8172: inline void pcbios_int_15h_cah()
8173: {
8174: switch(REG8(AL)) {
1.1.1.22 root 8175: case 0x00:
1.1 root 8176: if(REG8(BL) > 0x3f) {
8177: REG8(AH) = 0x03;
1.1.1.3 root 8178: m_CF = 1;
1.1 root 8179: } else if(REG8(BL) < 0x0e) {
8180: REG8(AH) = 0x04;
1.1.1.3 root 8181: m_CF = 1;
1.1 root 8182: } else {
1.1.1.8 root 8183: REG8(CL) = cmos_read(REG8(BL));
1.1 root 8184: }
8185: break;
1.1.1.22 root 8186: case 0x01:
1.1 root 8187: if(REG8(BL) > 0x3f) {
8188: REG8(AH) = 0x03;
1.1.1.3 root 8189: m_CF = 1;
1.1 root 8190: } else if(REG8(BL) < 0x0e) {
8191: REG8(AH) = 0x04;
1.1.1.3 root 8192: m_CF = 1;
1.1 root 8193: } else {
1.1.1.8 root 8194: cmos_write(REG8(BL), REG8(CL));
1.1 root 8195: }
8196: break;
8197: default:
1.1.1.22 root 8198: 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 8199: REG8(AH) = 0x86;
1.1.1.3 root 8200: m_CF = 1;
1.1 root 8201: break;
8202: }
8203: }
8204:
1.1.1.22 root 8205: inline void pcbios_int_15h_e8h()
1.1.1.17 root 8206: {
1.1.1.22 root 8207: switch(REG8(AL)) {
8208: #if defined(HAS_I386)
8209: case 0x01:
8210: REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
8211: REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
8212: break;
1.1.1.17 root 8213: #endif
1.1.1.22 root 8214: default:
8215: 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));
8216: REG8(AH) = 0x86;
8217: m_CF = 1;
8218: break;
8219: }
8220: }
1.1.1.17 root 8221:
1.1.1.51! root 8222: void pcbios_clear_key_buffer()
! 8223: {
! 8224: key_buf_char->clear();
! 8225: key_buf_scan->clear();
! 8226:
! 8227: // update key buffer
! 8228: *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
! 8229: }
! 8230:
! 8231: void pcbios_set_key_buffer(int key_char, int key_scan)
! 8232: {
! 8233: key_buf_char->write(key_char);
! 8234: key_buf_scan->write(key_scan);
! 8235:
! 8236: // update key buffer
! 8237: UINT16 head = *(UINT16 *)(mem + 0x41a);
! 8238: UINT16 tail = *(UINT16 *)(mem + 0x41c);
! 8239: UINT16 next = tail + 2;
! 8240: if(next >= *(UINT16 *)(mem + 0x482)) {
! 8241: next = *(UINT16 *)(mem + 0x480);
! 8242: }
! 8243: if(next != head) {
! 8244: *(UINT16 *)(mem + 0x41c) = next;
! 8245: mem[0x400 + (tail++)] = key_char;
! 8246: mem[0x400 + (tail++)] = key_scan;
! 8247: }
! 8248: }
! 8249:
! 8250: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
! 8251: {
! 8252: if(key_buf_char->empty()) {
! 8253: *key_char = 0x00;
! 8254: *key_scan = 0x00;
! 8255: return(false);
! 8256: }
! 8257: *key_char = key_buf_char->read();
! 8258: *key_scan = key_buf_scan->read();
! 8259:
! 8260: // update key buffer
! 8261: UINT16 head = *(UINT16 *)(mem + 0x41a);
! 8262: UINT16 tail = *(UINT16 *)(mem + 0x41c);
! 8263: UINT16 next = head + 2;
! 8264: if(next >= *(UINT16 *)(mem + 0x482)) {
! 8265: next = *(UINT16 *)(mem + 0x480);
! 8266: }
! 8267: if(head != tail) {
! 8268: *(UINT16 *)(mem + 0x41a) = next;
! 8269: // *key_char = mem[0x400 + (head++)];
! 8270: // *key_scan = mem[0x400 + (head++)];
! 8271: }
! 8272: return(true);
! 8273: }
! 8274:
1.1.1.33 root 8275: void pcbios_update_key_code(bool wait)
1.1 root 8276: {
1.1.1.32 root 8277: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8278: #ifdef USE_SERVICE_THREAD
8279: EnterCriticalSection(&key_buf_crit_sect);
8280: #endif
8281: bool empty = key_buf_char->empty();
8282: #ifdef USE_SERVICE_THREAD
8283: LeaveCriticalSection(&key_buf_crit_sect);
8284: #endif
8285: if(empty) {
1.1.1.32 root 8286: if(!update_key_buffer()) {
1.1.1.33 root 8287: if(wait) {
1.1.1.32 root 8288: Sleep(10);
8289: } else {
8290: maybe_idle();
8291: }
1.1.1.14 root 8292: }
8293: }
1.1.1.34 root 8294: }
8295: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8296: #ifdef USE_SERVICE_THREAD
8297: EnterCriticalSection(&key_buf_crit_sect);
8298: #endif
1.1.1.51! root 8299: int key_char, key_scan;
! 8300: if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41 root 8301: key_code = key_char << 0;
8302: key_code |= key_scan << 8;
1.1.1.35 root 8303: key_recv = 0x0000ffff;
1.1.1.51! root 8304: }
! 8305: if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41 root 8306: key_code |= key_char << 16;
8307: key_code |= key_scan << 24;
1.1.1.33 root 8308: key_recv |= 0xffff0000;
1.1.1.32 root 8309: }
1.1.1.35 root 8310: #ifdef USE_SERVICE_THREAD
8311: LeaveCriticalSection(&key_buf_crit_sect);
8312: #endif
1.1 root 8313: }
8314: }
8315:
1.1.1.35 root 8316: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1 root 8317: {
1.1.1.33 root 8318: while(key_recv == 0 && !m_halted) {
8319: pcbios_update_key_code(true);
1.1 root 8320: }
1.1.1.33 root 8321: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
8322: if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
8323: if(REG8(AH) == 0x10) {
8324: key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
8325: } else {
8326: key_code = ((key_code >> 16) & 0xff00);
8327: }
8328: key_recv >>= 16;
1.1 root 8329: }
8330: }
8331: REG16(AX) = key_code & 0xffff;
8332: key_code >>= 16;
1.1.1.33 root 8333: key_recv >>= 16;
1.1.1.35 root 8334:
8335: #ifdef USE_SERVICE_THREAD
8336: service_exit = true;
8337: #endif
8338: return(0);
8339: }
8340:
8341: inline void pcbios_int_16h_00h()
8342: {
8343: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 8344: if(!in_service && !in_service_29h) {
! 8345: start_service_loop(pcbios_int_16h_00h_thread);
! 8346: } else {
! 8347: #endif
! 8348: pcbios_int_16h_00h_thread(NULL);
! 8349: REQUEST_HARDWRE_UPDATE();
! 8350: #ifdef USE_SERVICE_THREAD
! 8351: }
1.1.1.35 root 8352: #endif
1.1 root 8353: }
8354:
8355: inline void pcbios_int_16h_01h()
8356: {
1.1.1.33 root 8357: if(key_recv == 0) {
8358: pcbios_update_key_code(false);
1.1.1.5 root 8359: }
1.1.1.33 root 8360: if(key_recv != 0) {
8361: UINT32 key_code_tmp = key_code;
8362: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
8363: if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
8364: if(REG8(AH) == 0x11) {
8365: key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
8366: } else {
8367: key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
8368: }
8369: }
1.1 root 8370: }
1.1.1.5 root 8371: REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3 root 8372: #if defined(HAS_I386)
1.1.1.33 root 8373: m_ZF = 0;
8374: #else
8375: m_ZeroVal = 1;
8376: #endif
8377: } else {
8378: #if defined(HAS_I386)
8379: m_ZF = 1;
1.1.1.3 root 8380: #else
1.1.1.33 root 8381: m_ZeroVal = 0;
1.1.1.3 root 8382: #endif
1.1.1.33 root 8383: }
1.1 root 8384: }
8385:
8386: inline void pcbios_int_16h_02h()
8387: {
8388: REG8(AL) = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
8389: REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
8390: REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
8391: REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
8392: REG8(AL) |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
8393: REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
8394: REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
8395: REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
8396: }
8397:
8398: inline void pcbios_int_16h_03h()
8399: {
8400: static UINT16 status = 0;
8401:
8402: switch(REG8(AL)) {
8403: case 0x05:
8404: status = REG16(BX);
8405: break;
8406: case 0x06:
8407: REG16(BX) = status;
8408: break;
8409: default:
1.1.1.3 root 8410: m_CF = 1;
1.1 root 8411: break;
8412: }
8413: }
8414:
8415: inline void pcbios_int_16h_05h()
8416: {
1.1.1.32 root 8417: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8418: #ifdef USE_SERVICE_THREAD
8419: EnterCriticalSection(&key_buf_crit_sect);
8420: #endif
1.1.1.51! root 8421: pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35 root 8422: #ifdef USE_SERVICE_THREAD
8423: LeaveCriticalSection(&key_buf_crit_sect);
8424: #endif
1.1.1.32 root 8425: }
1.1 root 8426: REG8(AL) = 0x00;
8427: }
8428:
8429: inline void pcbios_int_16h_12h()
8430: {
8431: pcbios_int_16h_02h();
8432:
8433: REG8(AH) = 0;//(GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x80 : 0;
8434: REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
8435: REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
8436: REG8(AH) |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
8437: REG8(AH) |= (GetAsyncKeyState(VK_RMENU ) & 0x8000) ? 0x08 : 0;
8438: REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
8439: REG8(AH) |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
8440: REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
8441: }
8442:
8443: inline void pcbios_int_16h_13h()
8444: {
8445: static UINT16 status = 0;
8446:
8447: switch(REG8(AL)) {
8448: case 0x00:
8449: status = REG16(DX);
8450: break;
8451: case 0x01:
8452: REG16(DX) = status;
8453: break;
8454: default:
1.1.1.22 root 8455: 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 8456: m_CF = 1;
1.1 root 8457: break;
8458: }
8459: }
8460:
8461: inline void pcbios_int_16h_14h()
8462: {
8463: static UINT8 status = 0;
8464:
8465: switch(REG8(AL)) {
8466: case 0x00:
8467: case 0x01:
8468: status = REG8(AL);
8469: break;
8470: case 0x02:
8471: REG8(AL) = status;
8472: break;
8473: default:
1.1.1.22 root 8474: 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 8475: m_CF = 1;
1.1 root 8476: break;
8477: }
8478: }
8479:
1.1.1.24 root 8480: inline void pcbios_int_16h_55h()
8481: {
8482: switch(REG8(AL)) {
8483: case 0x00:
8484: // keyboard tsr is not present
8485: break;
8486: case 0xfe:
8487: // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
8488: break;
8489: case 0xff:
8490: break;
8491: default:
8492: 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));
8493: m_CF = 1;
8494: break;
8495: }
8496: }
8497:
1.1.1.30 root 8498: inline void pcbios_int_16h_6fh()
8499: {
8500: switch(REG8(AL)) {
8501: case 0x00:
8502: // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
8503: break;
8504: default:
8505: 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));
8506: m_CF = 1;
8507: break;
8508: }
8509: }
8510:
1.1.1.37 root 8511: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
8512: {
8513: UINT8 hi = jis >> 8;
8514: UINT8 lo = jis & 0xff;
8515:
8516: lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
8517: hi = (hi - 0x21) / 2 + 0x81;
8518: hi = (hi >= 0xa0) ? hi + 0x40 : hi;
8519: lo = (lo >= 0x7f) ? lo + 0x01 : lo;
8520:
8521: return((hi << 8) + lo);
8522: }
8523:
8524: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
8525: {
8526: UINT8 hi = sjis >> 8;
8527: UINT8 lo = sjis & 0xff;
8528:
8529: if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
8530: return(0x2121);
8531: }
8532: if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
8533: return(0x2121);
8534: }
8535: if(hi >= 0xf0 && hi <= 0xf3) {
8536: // gaiji
8537: if(lo >= 0x40 && lo <= 0x7e) {
8538: return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
8539: }
8540: if(lo >= 0x80 && lo <= 0x9e) {
8541: return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
8542: }
8543: if(lo >= 0x9f && lo <= 0xfc) {
8544: return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
8545: }
8546: }
8547: hi = (hi >= 0xe0) ? hi - 0x40 : hi;
8548: lo = (lo >= 0x80) ? lo - 0x01 : lo;
8549: hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
8550: lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
8551:
8552: return((hi << 8) + lo);
8553: }
8554:
1.1.1.38 root 8555: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
8556: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
8557: // 6. �R���g���[���R�[�h�̉��
1.1.1.37 root 8558:
8559: void pcbios_printer_out(int c, UINT8 data)
8560: {
8561: if(pio[c].conv_mode) {
8562: if(pio[c].sjis_hi != 0) {
8563: if(!pio[c].jis_mode) {
8564: printer_out(c, 0x1c);
8565: printer_out(c, 0x26);
8566: pio[c].jis_mode = true;
8567: }
8568: UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
8569: printer_out(c, jis >> 8);
8570: printer_out(c, jis & 0xff);
8571: pio[c].sjis_hi = 0;
8572: } else if(pio[c].esc_buf[0] == 0x1b) {
8573: printer_out(c, data);
8574: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
8575: pio[c].esc_buf[pio[c].esc_len] = data;
8576: }
8577: pio[c].esc_len++;
8578:
8579: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 8580: case 0x33: // 1Bh 33h XX
8581: case 0x4a: // 1Bh 4Ah XX
8582: case 0x4e: // 1Bh 4Eh XX
8583: case 0x51: // 1Bh 51h XX
8584: case 0x55: // 1Bh 55h XX
8585: case 0x6c: // 1Bh 6Ch XX
8586: case 0x71: // 1Bh 71h XX
8587: case 0x72: // 1Bh 72h XX
1.1.1.37 root 8588: if(pio[c].esc_len == 3) {
8589: pio[c].esc_buf[0] = 0x00;
8590: }
8591: break;
1.1.1.38 root 8592: case 0x24: // 1Bh 24h XX XX
8593: case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37 root 8594: if(pio[c].esc_len == 4) {
8595: pio[c].esc_buf[0] = 0x00;
8596: }
8597: break;
1.1.1.38 root 8598: case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37 root 8599: if(pio[c].esc_len >= 3) {
8600: switch(pio[c].esc_buf[2]) {
8601: case 0: case 1: case 2: case 3: case 4: case 6:
8602: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
8603: pio[c].esc_buf[0] = 0x00;
8604: }
8605: break;
8606: case 32: case 33: case 38: case 39: case 40:
8607: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
8608: pio[c].esc_buf[0] = 0x00;
8609: }
8610: break;
1.1.1.38 root 8611: case 71: case 72: case 73:
8612: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
8613: pio[c].esc_buf[0] = 0x00;
8614: }
8615: break;
1.1.1.37 root 8616: default:
8617: pio[c].esc_buf[0] = 0x00;
8618: break;
8619: }
8620: }
8621: break;
1.1.1.38 root 8622: case 0x40: // 1Bh 40h
1.1.1.37 root 8623: if(pio[c].jis_mode) {
8624: printer_out(c, 0x1c);
8625: printer_out(c, 0x2e);
8626: pio[c].jis_mode = false;
8627: }
8628: pio[c].esc_buf[0] = 0x00;
8629: break;
1.1.1.38 root 8630: case 0x42: // 1Bh 42h data 00h
8631: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 8632: if(pio[c].esc_len >= 3 && data == 0) {
8633: pio[c].esc_buf[0] = 0x00;
8634: }
8635: break;
1.1.1.38 root 8636: case 0x43: // 1Bh 43h (00h) XX
1.1.1.37 root 8637: if(pio[c].esc_len >= 3 && data != 0) {
8638: pio[c].esc_buf[0] = 0x00;
8639: }
8640: break;
1.1.1.38 root 8641: default: // 1Bh XX
1.1.1.37 root 8642: pio[c].esc_buf[0] = 0x00;
8643: break;
8644: }
8645: } else if(pio[c].esc_buf[0] == 0x1c) {
8646: printer_out(c, data);
8647: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
8648: pio[c].esc_buf[pio[c].esc_len] = data;
8649: }
8650: pio[c].esc_len++;
8651:
8652: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 8653: case 0x21: // 1Ch 21h XX
8654: case 0x2d: // 1Ch 2Dh XX
8655: case 0x57: // 1Ch 57h XX
8656: case 0x6b: // 1Ch 6Bh XX
8657: case 0x72: // 1Ch 72h XX
8658: case 0x78: // 1Ch 78h XX
1.1.1.37 root 8659: if(pio[c].esc_len == 3) {
8660: pio[c].esc_buf[0] = 0x00;
8661: }
8662: break;
1.1.1.38 root 8663: case 0x26: // 1Ch 26h
1.1.1.37 root 8664: pio[c].jis_mode = true;
8665: pio[c].esc_buf[0] = 0x00;
8666: break;
1.1.1.38 root 8667: case 0x2e: // 1Ch 2Eh
1.1.1.37 root 8668: pio[c].jis_mode = false;
8669: pio[c].esc_buf[0] = 0x00;
8670: break;
1.1.1.38 root 8671: case 0x32: // 1Ch 32h XX XX data
1.1.1.37 root 8672: if(pio[c].esc_len == 76) {
8673: pio[c].esc_buf[0] = 0x00;
8674: }
8675: break;
1.1.1.38 root 8676: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 8677: if(pio[c].esc_len == 6) {
8678: pio[c].esc_buf[0] = 0x00;
8679: }
8680: break;
1.1.1.38 root 8681: case 0x53: // 1Ch 53h XX XX
8682: case 0x54: // 1Ch 54h XX XX
1.1.1.37 root 8683: if(pio[c].esc_len == 4) {
8684: pio[c].esc_buf[0] = 0x00;
8685: }
8686: break;
1.1.1.38 root 8687: default: // 1Ch XX
1.1.1.37 root 8688: pio[c].esc_buf[0] = 0x00;
8689: break;
8690: }
8691: } else if(data == 0x1b || data == 0x1c) {
8692: printer_out(c, data);
8693: pio[c].esc_buf[0] = data;
8694: pio[c].esc_len = 1;
8695: } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
8696: pio[c].sjis_hi = data;
8697: } else {
8698: if(pio[c].jis_mode) {
8699: printer_out(c, 0x1c);
8700: printer_out(c, 0x2e);
8701: pio[c].jis_mode = false;
8702: }
8703: printer_out(c, data);
8704: }
8705: } else {
8706: if(pio[c].jis_mode) {
8707: printer_out(c, 0x1c);
8708: printer_out(c, 0x2e);
8709: pio[c].jis_mode = false;
8710: }
8711: printer_out(c, data);
8712: }
8713: }
8714:
8715: inline void pcbios_int_17h_00h()
8716: {
8717: if(REG16(DX) < 3) {
8718: pcbios_printer_out(REG16(DX), REG8(AL));
8719: REG8(AH) = 0xd0;
8720: }
8721: }
8722:
8723: inline void pcbios_int_17h_01h()
8724: {
8725: if(REG16(DX) < 3) {
8726: REG8(AH) = 0xd0;
8727: }
8728: }
8729:
8730: inline void pcbios_int_17h_02h()
8731: {
8732: if(REG16(DX) < 3) {
8733: REG8(AH) = 0xd0;
8734: }
8735: }
8736:
8737: inline void pcbios_int_17h_03h()
8738: {
8739: switch(REG8(AL)) {
8740: case 0x00:
8741: if(REG16(DX) < 3) {
8742: if(pio[REG16(DX)].jis_mode) {
8743: printer_out(REG16(DX), 0x1c);
8744: printer_out(REG16(DX), 0x2e);
8745: pio[REG16(DX)].jis_mode = false;
8746: }
8747: for(UINT16 i = 0; i < REG16(CX); i++) {
8748: printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
8749: }
8750: REG16(CX) = 0x0000;
8751: REG8(AH) = 0xd0;
8752: }
8753: break;
8754: default:
8755: 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));
8756: break;
8757: }
8758: }
8759:
8760: inline void pcbios_int_17h_50h()
8761: {
8762: switch(REG8(AL)) {
8763: case 0x00:
8764: if(REG16(DX) < 3) {
8765: if(REG16(BX) = 0x0001) {
8766: pio[REG16(DX)].conv_mode = false;
8767: REG8(AL) = 0x00;
8768: } else if(REG16(BX) = 0x0051) {
8769: pio[REG16(DX)].conv_mode = true;
8770: REG8(AL) = 0x00;
8771: } else {
8772: REG8(AL) = 0x01;
8773: }
8774: } else {
8775: REG8(AL) = 0x02;
8776: }
8777: break;
8778: case 0x01:
8779: if(REG16(DX) < 3) {
8780: if(pio[REG16(DX)].conv_mode) {
8781: REG16(BX) = 0x0051;
8782: } else {
8783: REG16(BX) = 0x0001;
8784: }
8785: REG8(AL) = 0x00;
8786: } else {
8787: REG8(AL) = 0x02;
8788: }
8789: break;
8790: default:
8791: 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));
8792: break;
8793: }
8794: }
8795:
8796: inline void pcbios_int_17h_51h()
8797: {
8798: if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
8799: REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
8800: } else {
8801: REG16(DX) = 0x0000;
8802: }
8803: }
8804:
8805: inline void pcbios_int_17h_52h()
8806: {
8807: if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
8808: REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
8809: } else {
8810: REG16(DX) = 0x0000;
8811: }
8812: }
8813:
8814: inline void pcbios_int_17h_84h()
8815: {
8816: if(REG16(DX) < 3) {
8817: if(pio[REG16(DX)].jis_mode) {
8818: printer_out(REG16(DX), 0x1c);
8819: printer_out(REG16(DX), 0x2e);
8820: pio[REG16(DX)].jis_mode = false;
8821: }
8822: printer_out(REG16(DX), REG8(AL));
8823: REG8(AH) = 0xd0;
8824: }
8825: }
8826:
8827: inline void pcbios_int_17h_85h()
8828: {
8829: pio[0].conv_mode = (REG8(AL) == 0x00);
8830: }
8831:
1.1 root 8832: inline void pcbios_int_1ah_00h()
8833: {
1.1.1.19 root 8834: pcbios_update_daily_timer_counter(timeGetTime());
8835: REG16(CX) = *(UINT16 *)(mem + 0x46e);
8836: REG16(DX) = *(UINT16 *)(mem + 0x46c);
8837: REG8(AL) = mem[0x470];
8838: mem[0x470] = 0;
1.1 root 8839: }
8840:
8841: inline int to_bcd(int t)
8842: {
8843: int u = (t % 100) / 10;
8844: return (u << 4) | (t % 10);
8845: }
8846:
8847: inline void pcbios_int_1ah_02h()
8848: {
8849: SYSTEMTIME time;
8850:
8851: GetLocalTime(&time);
8852: REG8(CH) = to_bcd(time.wHour);
8853: REG8(CL) = to_bcd(time.wMinute);
8854: REG8(DH) = to_bcd(time.wSecond);
8855: REG8(DL) = 0x00;
8856: }
8857:
8858: inline void pcbios_int_1ah_04h()
8859: {
8860: SYSTEMTIME time;
8861:
8862: GetLocalTime(&time);
8863: REG8(CH) = to_bcd(time.wYear / 100);
8864: REG8(CL) = to_bcd(time.wYear);
8865: REG8(DH) = to_bcd(time.wMonth);
8866: REG8(DL) = to_bcd(time.wDay);
8867: }
8868:
8869: inline void pcbios_int_1ah_0ah()
8870: {
8871: SYSTEMTIME time;
8872: FILETIME file_time;
8873: WORD dos_date, dos_time;
8874:
8875: GetLocalTime(&time);
8876: SystemTimeToFileTime(&time, &file_time);
8877: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
8878: REG16(CX) = dos_date;
8879: }
8880:
8881: // msdos system call
8882:
1.1.1.43 root 8883: inline void msdos_int_21h_56h(int lfn);
8884:
1.1 root 8885: inline void msdos_int_21h_00h()
8886: {
1.1.1.3 root 8887: msdos_process_terminate(SREG(CS), retval, 1);
1.1 root 8888: }
8889:
1.1.1.35 root 8890: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1 root 8891: {
8892: REG8(AL) = msdos_getche();
1.1.1.33 root 8893: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 8894:
1.1.1.35 root 8895: #ifdef USE_SERVICE_THREAD
8896: service_exit = true;
8897: #endif
8898: return(0);
8899: }
8900:
8901: inline void msdos_int_21h_01h()
8902: {
8903: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 8904: if(!in_service && !in_service_29h &&
! 8905: *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50 root 8906: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
8907: // msdos_putch() will be used in this service
8908: // if int 29h is hooked, run this service in main thread to call int 29h
8909: start_service_loop(msdos_int_21h_01h_thread);
8910: } else {
8911: #endif
8912: msdos_int_21h_01h_thread(NULL);
8913: REQUEST_HARDWRE_UPDATE();
8914: #ifdef USE_SERVICE_THREAD
8915: }
1.1.1.35 root 8916: #endif
1.1 root 8917: }
8918:
8919: inline void msdos_int_21h_02h()
8920: {
1.1.1.33 root 8921: UINT8 data = REG8(DL);
8922: msdos_putch(data);
8923: REG8(AL) = data;
8924: ctrl_break_detected = ctrl_break_pressed;
1.1 root 8925: }
8926:
8927: inline void msdos_int_21h_03h()
8928: {
8929: REG8(AL) = msdos_aux_in();
8930: }
8931:
8932: inline void msdos_int_21h_04h()
8933: {
8934: msdos_aux_out(REG8(DL));
8935: }
8936:
8937: inline void msdos_int_21h_05h()
8938: {
8939: msdos_prn_out(REG8(DL));
8940: }
8941:
8942: inline void msdos_int_21h_06h()
8943: {
8944: if(REG8(DL) == 0xff) {
8945: if(msdos_kbhit()) {
8946: REG8(AL) = msdos_getch();
1.1.1.3 root 8947: #if defined(HAS_I386)
8948: m_ZF = 0;
8949: #else
8950: m_ZeroVal = 1;
8951: #endif
1.1 root 8952: } else {
8953: REG8(AL) = 0;
1.1.1.3 root 8954: #if defined(HAS_I386)
8955: m_ZF = 1;
8956: #else
8957: m_ZeroVal = 0;
8958: #endif
1.1.1.14 root 8959: maybe_idle();
1.1 root 8960: }
8961: } else {
1.1.1.33 root 8962: UINT8 data = REG8(DL);
8963: msdos_putch(data);
8964: REG8(AL) = data;
1.1 root 8965: }
8966: }
8967:
1.1.1.35 root 8968: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1 root 8969: {
8970: REG8(AL) = msdos_getch();
1.1.1.26 root 8971:
1.1.1.35 root 8972: #ifdef USE_SERVICE_THREAD
8973: service_exit = true;
8974: #endif
8975: return(0);
1.1 root 8976: }
8977:
1.1.1.35 root 8978: inline void msdos_int_21h_07h()
8979: {
8980: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 8981: if(!in_service && !in_service_29h) {
! 8982: start_service_loop(msdos_int_21h_07h_thread);
! 8983: } else {
! 8984: #endif
! 8985: msdos_int_21h_07h_thread(NULL);
! 8986: REQUEST_HARDWRE_UPDATE();
! 8987: #ifdef USE_SERVICE_THREAD
! 8988: }
1.1.1.35 root 8989: #endif
8990: }
8991:
8992: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1 root 8993: {
8994: REG8(AL) = msdos_getch();
1.1.1.33 root 8995: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 8996:
1.1.1.35 root 8997: #ifdef USE_SERVICE_THREAD
8998: service_exit = true;
8999: #endif
9000: return(0);
9001: }
9002:
9003: inline void msdos_int_21h_08h()
9004: {
9005: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 9006: if(!in_service && !in_service_29h) {
! 9007: start_service_loop(msdos_int_21h_08h_thread);
! 9008: } else {
! 9009: #endif
! 9010: msdos_int_21h_08h_thread(NULL);
! 9011: REQUEST_HARDWRE_UPDATE();
! 9012: #ifdef USE_SERVICE_THREAD
! 9013: }
1.1.1.35 root 9014: #endif
1.1 root 9015: }
9016:
9017: inline void msdos_int_21h_09h()
9018: {
1.1.1.21 root 9019: msdos_stdio_reopen();
9020:
1.1.1.20 root 9021: process_t *process = msdos_process_info_get(current_psp);
9022: int fd = msdos_psp_get_file_table(1, current_psp);
9023:
1.1.1.14 root 9024: char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
9025: int len = 0;
1.1 root 9026:
1.1.1.14 root 9027: while(str[len] != '$' && len < 0x10000) {
9028: len++;
9029: }
1.1.1.20 root 9030: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 9031: // stdout is redirected to file
1.1.1.20 root 9032: msdos_write(fd, str, len);
1.1 root 9033: } else {
9034: for(int i = 0; i < len; i++) {
1.1.1.14 root 9035: msdos_putch(str[i]);
1.1 root 9036: }
9037: }
1.1.1.33 root 9038: REG8(AL) = '$';
9039: ctrl_break_detected = ctrl_break_pressed;
1.1 root 9040: }
9041:
1.1.1.35 root 9042: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1 root 9043: {
1.1.1.3 root 9044: int ofs = SREG_BASE(DS) + REG16(DX);
1.1 root 9045: int max = mem[ofs] - 1;
9046: UINT8 *buf = mem + ofs + 2;
9047: int chr, p = 0;
9048:
9049: while((chr = msdos_getch()) != 0x0d) {
1.1.1.33 root 9050: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26 root 9051: p = 0;
1.1.1.33 root 9052: msdos_putch(0x03);
9053: msdos_putch(0x0d);
9054: msdos_putch(0x0a);
1.1.1.26 root 9055: break;
1.1.1.33 root 9056: } else if(ctrl_break_pressed) {
9057: // skip this byte
1.1.1.26 root 9058: } else if(chr == 0x00) {
1.1 root 9059: // skip 2nd byte
9060: msdos_getch();
9061: } else if(chr == 0x08) {
9062: // back space
9063: if(p > 0) {
9064: p--;
1.1.1.20 root 9065: if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34 root 9066: msdos_putch(0x08);
9067: msdos_putch(0x08);
9068: msdos_putch(0x20);
9069: msdos_putch(0x20);
9070: msdos_putch(0x08);
9071: msdos_putch(0x08);
1.1.1.36 root 9072: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
9073: p--;
9074: msdos_putch(0x08);
9075: msdos_putch(0x08);
9076: msdos_putch(0x20);
9077: msdos_putch(0x20);
9078: msdos_putch(0x08);
9079: msdos_putch(0x08);
1.1.1.34 root 9080: } else {
9081: msdos_putch(0x08);
9082: msdos_putch(0x20);
9083: msdos_putch(0x08);
9084: }
9085: }
9086: } else if(chr == 0x1b) {
9087: // escape
9088: while(p > 0) {
9089: p--;
9090: if(msdos_ctrl_code_check(buf[p])) {
9091: msdos_putch(0x08);
9092: msdos_putch(0x08);
9093: msdos_putch(0x20);
9094: msdos_putch(0x20);
9095: msdos_putch(0x08);
9096: msdos_putch(0x08);
1.1.1.20 root 9097: } else {
1.1.1.34 root 9098: msdos_putch(0x08);
9099: msdos_putch(0x20);
9100: msdos_putch(0x08);
1.1.1.20 root 9101: }
1.1 root 9102: }
9103: } else if(p < max) {
9104: buf[p++] = chr;
9105: msdos_putch(chr);
9106: }
9107: }
9108: buf[p] = 0x0d;
9109: mem[ofs + 1] = p;
1.1.1.33 root 9110: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 9111:
1.1.1.35 root 9112: #ifdef USE_SERVICE_THREAD
9113: service_exit = true;
9114: #endif
9115: return(0);
9116: }
9117:
9118: inline void msdos_int_21h_0ah()
9119: {
9120: if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
9121: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 9122: if(!in_service && !in_service_29h &&
! 9123: *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50 root 9124: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
9125: // msdos_putch() will be used in this service
9126: // if int 29h is hooked, run this service in main thread to call int 29h
9127: start_service_loop(msdos_int_21h_0ah_thread);
9128: } else {
9129: #endif
9130: msdos_int_21h_0ah_thread(NULL);
9131: REQUEST_HARDWRE_UPDATE();
9132: #ifdef USE_SERVICE_THREAD
9133: }
1.1.1.35 root 9134: #endif
9135: }
1.1 root 9136: }
9137:
9138: inline void msdos_int_21h_0bh()
9139: {
9140: if(msdos_kbhit()) {
9141: REG8(AL) = 0xff;
9142: } else {
9143: REG8(AL) = 0x00;
1.1.1.14 root 9144: maybe_idle();
1.1 root 9145: }
1.1.1.33 root 9146: ctrl_break_detected = ctrl_break_pressed;
1.1 root 9147: }
9148:
9149: inline void msdos_int_21h_0ch()
9150: {
9151: // clear key buffer
1.1.1.21 root 9152: msdos_stdio_reopen();
9153:
1.1.1.20 root 9154: process_t *process = msdos_process_info_get(current_psp);
9155: int fd = msdos_psp_get_file_table(0, current_psp);
9156:
9157: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 9158: // stdin is redirected to file
9159: } else {
9160: while(msdos_kbhit()) {
9161: msdos_getch();
9162: }
9163: }
9164:
9165: switch(REG8(AL)) {
9166: case 0x01:
9167: msdos_int_21h_01h();
9168: break;
9169: case 0x06:
9170: msdos_int_21h_06h();
9171: break;
9172: case 0x07:
9173: msdos_int_21h_07h();
9174: break;
9175: case 0x08:
9176: msdos_int_21h_08h();
9177: break;
9178: case 0x0a:
9179: msdos_int_21h_0ah();
9180: break;
9181: default:
1.1.1.48 root 9182: // the buffer is flushed but no input is attempted
1.1 root 9183: break;
9184: }
9185: }
9186:
9187: inline void msdos_int_21h_0dh()
9188: {
9189: }
9190:
9191: inline void msdos_int_21h_0eh()
9192: {
9193: if(REG8(DL) < 26) {
9194: _chdrive(REG8(DL) + 1);
9195: msdos_cds_update(REG8(DL));
1.1.1.23 root 9196: msdos_sda_update(current_psp);
1.1 root 9197: }
9198: REG8(AL) = 26; // zdrive
9199: }
9200:
1.1.1.14 root 9201: inline void msdos_int_21h_0fh()
9202: {
9203: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9204: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 9205: const char *path = msdos_fcb_path(fcb);
1.1.1.14 root 9206: HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16 root 9207:
1.1.1.14 root 9208: if(hFile == INVALID_HANDLE_VALUE) {
9209: REG8(AL) = 0xff;
9210: } else {
9211: REG8(AL) = 0;
9212: fcb->current_block = 0;
9213: fcb->record_size = 128;
9214: fcb->file_size = GetFileSize(hFile, NULL);
9215: fcb->handle = hFile;
9216: fcb->cur_record = 0;
9217: }
9218: }
9219:
9220: inline void msdos_int_21h_10h()
9221: {
9222: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9223: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9224:
9225: REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
9226: }
9227:
1.1 root 9228: inline void msdos_int_21h_11h()
9229: {
1.1.1.3 root 9230: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9231: fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 9232:
9233: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 9234: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9235: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
9236: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45 root 9237: const char *path = msdos_fcb_path(fcb);
1.1 root 9238: WIN32_FIND_DATA fd;
9239:
1.1.1.13 root 9240: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
9241: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
9242: FindClose(dtainfo->find_handle);
9243: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9244: }
9245: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 9246: dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
9247: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 9248:
1.1.1.14 root 9249: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
9250: dtainfo->allowable_mask &= ~8;
1.1 root 9251: }
1.1.1.14 root 9252: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
9253: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 9254: !msdos_find_file_has_8dot3name(&fd)) {
9255: if(!FindNextFile(dtainfo->find_handle, &fd)) {
9256: FindClose(dtainfo->find_handle);
9257: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9258: break;
9259: }
9260: }
9261: }
1.1.1.13 root 9262: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 9263: if(ext_fcb->flag == 0xff) {
9264: ext_find->flag = 0xff;
9265: memset(ext_find->reserved, 0, 5);
9266: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9267: }
9268: find->drive = _getdrive();
1.1.1.13 root 9269: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 9270: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9271: find->nt_res = 0;
9272: msdos_find_file_conv_local_time(&fd);
9273: find->create_time_ms = 0;
9274: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9275: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9276: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9277: find->cluster_hi = find->cluster_lo = 0;
9278: find->file_size = fd.nFileSizeLow;
9279: REG8(AL) = 0x00;
1.1.1.14 root 9280: } else if(dtainfo->allowable_mask & 8) {
1.1 root 9281: if(ext_fcb->flag == 0xff) {
9282: ext_find->flag = 0xff;
9283: memset(ext_find->reserved, 0, 5);
9284: ext_find->attribute = 8;
9285: }
9286: find->drive = _getdrive();
9287: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
9288: find->attribute = 8;
9289: find->nt_res = 0;
9290: msdos_find_file_conv_local_time(&fd);
9291: find->create_time_ms = 0;
9292: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9293: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9294: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9295: find->cluster_hi = find->cluster_lo = 0;
9296: find->file_size = 0;
1.1.1.14 root 9297: dtainfo->allowable_mask &= ~8;
1.1 root 9298: REG8(AL) = 0x00;
9299: } else {
9300: REG8(AL) = 0xff;
9301: }
9302: }
9303:
9304: inline void msdos_int_21h_12h()
9305: {
1.1.1.3 root 9306: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14 root 9307: // fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 9308:
9309: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 9310: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9311: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
9312: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 9313: WIN32_FIND_DATA fd;
9314:
1.1.1.13 root 9315: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
9316: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
9317: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 9318: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 9319: !msdos_find_file_has_8dot3name(&fd)) {
9320: if(!FindNextFile(dtainfo->find_handle, &fd)) {
9321: FindClose(dtainfo->find_handle);
9322: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9323: break;
9324: }
9325: }
9326: } else {
1.1.1.13 root 9327: FindClose(dtainfo->find_handle);
9328: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 9329: }
9330: }
1.1.1.13 root 9331: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 9332: if(ext_fcb->flag == 0xff) {
9333: ext_find->flag = 0xff;
9334: memset(ext_find->reserved, 0, 5);
9335: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9336: }
9337: find->drive = _getdrive();
1.1.1.13 root 9338: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 9339: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
9340: find->nt_res = 0;
9341: msdos_find_file_conv_local_time(&fd);
9342: find->create_time_ms = 0;
9343: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9344: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9345: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9346: find->cluster_hi = find->cluster_lo = 0;
9347: find->file_size = fd.nFileSizeLow;
9348: REG8(AL) = 0x00;
1.1.1.14 root 9349: } else if(dtainfo->allowable_mask & 8) {
1.1 root 9350: if(ext_fcb->flag == 0xff) {
9351: ext_find->flag = 0xff;
9352: memset(ext_find->reserved, 0, 5);
9353: ext_find->attribute = 8;
9354: }
9355: find->drive = _getdrive();
9356: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
9357: find->attribute = 8;
9358: find->nt_res = 0;
9359: msdos_find_file_conv_local_time(&fd);
9360: find->create_time_ms = 0;
9361: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9362: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9363: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9364: find->cluster_hi = find->cluster_lo = 0;
9365: find->file_size = 0;
1.1.1.14 root 9366: dtainfo->allowable_mask &= ~8;
1.1 root 9367: REG8(AL) = 0x00;
9368: } else {
9369: REG8(AL) = 0xff;
9370: }
9371: }
9372:
9373: inline void msdos_int_21h_13h()
9374: {
1.1.1.3 root 9375: if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1 root 9376: REG8(AL) = 0xff;
9377: } else {
9378: REG8(AL) = 0x00;
9379: }
9380: }
9381:
1.1.1.16 root 9382: inline void msdos_int_21h_14h()
9383: {
9384: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9385: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9386: process_t *process = msdos_process_info_get(current_psp);
9387: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9388: DWORD num = 0;
9389:
9390: memset(mem + dta_laddr, 0, fcb->record_size);
9391: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9392: REG8(AL) = 1;
9393: } else {
9394: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9395: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9396: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9397: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
9398: }
9399: }
9400:
9401: inline void msdos_int_21h_15h()
1.1.1.14 root 9402: {
9403: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9404: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16 root 9405: process_t *process = msdos_process_info_get(current_psp);
9406: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9407: DWORD num = 0;
1.1.1.14 root 9408:
1.1.1.16 root 9409: if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9410: REG8(AL) = 1;
9411: } else {
9412: fcb->file_size = GetFileSize(fcb->handle, NULL);
9413: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9414: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9415: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9416: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
9417: }
9418: }
9419:
9420: inline void msdos_int_21h_16h()
9421: {
9422: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9423: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 9424: const char *path = msdos_fcb_path(fcb);
1.1.1.14 root 9425: HANDLE hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, ext_fcb->flag == 0xff ? ext_fcb->attribute : FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16 root 9426:
1.1.1.14 root 9427: if(hFile == INVALID_HANDLE_VALUE) {
9428: REG8(AL) = 0xff;
9429: } else {
9430: REG8(AL) = 0;
9431: fcb->current_block = 0;
9432: fcb->record_size = 128;
9433: fcb->file_size = 0;
9434: fcb->handle = hFile;
9435: fcb->cur_record = 0;
9436: }
9437: }
9438:
1.1.1.16 root 9439: inline void msdos_int_21h_17h()
9440: {
9441: ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9442: fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45 root 9443: // const char *path_src = msdos_fcb_path(fcb_src);
9444: char path_src[MAX_PATH];
9445: strcpy(path_src, msdos_fcb_path(fcb_src));
9446:
1.1.1.16 root 9447: ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
9448: fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45 root 9449: // const char *path_dst = msdos_fcb_path(fcb_dst);
9450: char path_dst[MAX_PATH];
9451: strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16 root 9452:
9453: if(rename(path_src, path_dst)) {
9454: REG8(AL) = 0xff;
9455: } else {
9456: REG8(AL) = 0;
9457: }
9458: }
9459:
1.1 root 9460: inline void msdos_int_21h_18h()
9461: {
9462: REG8(AL) = 0x00;
9463: }
9464:
9465: inline void msdos_int_21h_19h()
9466: {
9467: REG8(AL) = _getdrive() - 1;
9468: }
9469:
9470: inline void msdos_int_21h_1ah()
9471: {
9472: process_t *process = msdos_process_info_get(current_psp);
9473:
9474: process->dta.w.l = REG16(DX);
1.1.1.3 root 9475: process->dta.w.h = SREG(DS);
1.1.1.23 root 9476: msdos_sda_update(current_psp);
1.1 root 9477: }
9478:
9479: inline void msdos_int_21h_1bh()
9480: {
9481: int drive_num = _getdrive() - 1;
9482: UINT16 seg, ofs;
9483:
9484: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9485: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
9486: REG8(AL) = dpb->highest_sector_num + 1;
9487: REG16(CX) = dpb->bytes_per_sector;
9488: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 9489: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 9490: } else {
9491: REG8(AL) = 0xff;
1.1.1.3 root 9492: m_CF = 1;
1.1 root 9493: }
9494:
9495: }
9496:
9497: inline void msdos_int_21h_1ch()
9498: {
1.1.1.41 root 9499: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1 root 9500: UINT16 seg, ofs;
9501:
9502: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9503: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
9504: REG8(AL) = dpb->highest_sector_num + 1;
9505: REG16(CX) = dpb->bytes_per_sector;
9506: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 9507: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 9508: } else {
9509: REG8(AL) = 0xff;
1.1.1.3 root 9510: m_CF = 1;
1.1 root 9511: }
9512:
9513: }
9514:
9515: inline void msdos_int_21h_1dh()
9516: {
9517: REG8(AL) = 0;
9518: }
9519:
9520: inline void msdos_int_21h_1eh()
9521: {
9522: REG8(AL) = 0;
9523: }
9524:
9525: inline void msdos_int_21h_1fh()
9526: {
9527: int drive_num = _getdrive() - 1;
9528: UINT16 seg, ofs;
9529:
9530: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9531: REG8(AL) = 0;
1.1.1.3 root 9532: SREG(DS) = seg;
9533: i386_load_segment_descriptor(DS);
1.1 root 9534: REG16(BX) = ofs;
9535: } else {
9536: REG8(AL) = 0xff;
1.1.1.3 root 9537: m_CF = 1;
1.1 root 9538: }
9539: }
9540:
9541: inline void msdos_int_21h_20h()
9542: {
9543: REG8(AL) = 0;
9544: }
9545:
1.1.1.14 root 9546: inline void msdos_int_21h_21h()
9547: {
9548: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9549: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9550:
9551: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9552: REG8(AL) = 1;
9553: } else {
9554: process_t *process = msdos_process_info_get(current_psp);
9555: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9556: memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16 root 9557: DWORD num = 0;
1.1.1.14 root 9558: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9559: REG8(AL) = 1;
9560: } else {
9561: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9562: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 9563: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14 root 9564: }
9565: }
9566: }
9567:
9568: inline void msdos_int_21h_22h()
9569: {
9570: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9571: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9572:
9573: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9574: REG8(AL) = 0xff;
9575: } else {
9576: process_t *process = msdos_process_info_get(current_psp);
9577: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16 root 9578: DWORD num = 0;
1.1.1.14 root 9579: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
9580: fcb->file_size = GetFileSize(fcb->handle, NULL);
9581: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9582: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 9583: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14 root 9584: }
9585: }
9586:
1.1.1.16 root 9587: inline void msdos_int_21h_23h()
9588: {
9589: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9590: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45 root 9591: const char *path = msdos_fcb_path(fcb);
1.1.1.16 root 9592: HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
9593:
9594: if(hFile == INVALID_HANDLE_VALUE) {
9595: REG8(AL) = 0xff;
9596: } else {
9597: UINT32 size = GetFileSize(hFile, NULL);
9598: fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
9599: REG8(AL) = 0;
9600: }
9601: }
9602:
9603: inline void msdos_int_21h_24h()
9604: {
9605: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9606: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9607:
9608: fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
9609: }
9610:
1.1 root 9611: inline void msdos_int_21h_25h()
9612: {
9613: *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3 root 9614: *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1 root 9615: }
9616:
9617: inline void msdos_int_21h_26h()
9618: {
9619: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
9620:
9621: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48 root 9622: psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1 root 9623: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
9624: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
9625: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
9626: psp->parent_psp = 0;
9627: }
9628:
1.1.1.16 root 9629: inline void msdos_int_21h_27h()
9630: {
9631: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9632: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9633:
9634: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9635: REG8(AL) = 1;
9636: } else {
9637: process_t *process = msdos_process_info_get(current_psp);
9638: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9639: memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
9640: DWORD num = 0;
9641: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
9642: REG8(AL) = 1;
9643: } else {
9644: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9645: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
9646: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
9647: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
9648: }
9649: }
9650: }
9651:
9652: inline void msdos_int_21h_28h()
9653: {
9654: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9655: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9656:
9657: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9658: REG8(AL) = 0xff;
9659: } else {
9660: process_t *process = msdos_process_info_get(current_psp);
9661: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9662: DWORD num = 0;
9663: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
9664: fcb->file_size = GetFileSize(fcb->handle, NULL);
9665: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9666: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
9667: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
9668: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
9669: }
9670: }
9671:
1.1 root 9672: inline void msdos_int_21h_29h()
9673: {
1.1.1.20 root 9674: int ofs = 0;//SREG_BASE(DS) + REG16(SI);
9675: char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1 root 9676: UINT8 drv = 0;
9677: char sep_chars[] = ":.;,=+";
9678: char end_chars[] = "\\<>|/\"[]";
9679: char spc_chars[] = " \t";
9680:
1.1.1.20 root 9681: memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
9682: buffer[1023] = 0;
9683: memset(name, 0x20, sizeof(name));
9684: memset(ext, 0x20, sizeof(ext));
9685:
1.1 root 9686: if(REG8(AL) & 1) {
1.1.1.20 root 9687: ofs += strspn((char *)(buffer + ofs), spc_chars);
9688: if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1 root 9689: ofs++;
9690: }
9691: }
1.1.1.20 root 9692: ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1 root 9693:
1.1.1.24 root 9694: if(buffer[ofs + 1] == ':') {
9695: if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
9696: drv = buffer[ofs] - 'a' + 1;
1.1.1.20 root 9697: ofs += 2;
1.1.1.24 root 9698: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
9699: ofs++;
9700: }
9701: } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
9702: drv = buffer[ofs] - 'A' + 1;
1.1 root 9703: ofs += 2;
1.1.1.24 root 9704: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
9705: ofs++;
9706: }
1.1 root 9707: }
9708: }
1.1.1.20 root 9709: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
9710: UINT8 c = buffer[ofs];
9711: if(is_kanji) {
9712: is_kanji = 0;
9713: } else if(msdos_lead_byte_check(c)) {
9714: is_kanji = 1;
9715: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 9716: break;
9717: } else if(c >= 'a' && c <= 'z') {
9718: c -= 0x20;
9719: }
9720: ofs++;
9721: name[i] = c;
9722: }
1.1.1.20 root 9723: if(buffer[ofs] == '.') {
1.1 root 9724: ofs++;
1.1.1.20 root 9725: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
9726: UINT8 c = buffer[ofs];
9727: if(is_kanji) {
9728: is_kanji = 0;
9729: } else if(msdos_lead_byte_check(c)) {
9730: is_kanji = 1;
9731: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 9732: break;
9733: } else if(c >= 'a' && c <= 'z') {
9734: c -= 0x20;
9735: }
9736: ofs++;
9737: ext[i] = c;
9738: }
9739: }
1.1.1.20 root 9740: int si = REG16(SI) + ofs;
1.1.1.3 root 9741: int ds = SREG(DS);
1.1 root 9742: while(si > 0xffff) {
9743: si -= 0x10;
9744: ds++;
9745: }
9746: REG16(SI) = si;
1.1.1.3 root 9747: SREG(DS) = ds;
9748: i386_load_segment_descriptor(DS);
1.1 root 9749:
1.1.1.3 root 9750: UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20 root 9751: if(!(REG8(AL) & 2) || drv != 0) {
9752: fcb[0] = drv;
9753: }
9754: if(!(REG8(AL) & 4) || name[0] != 0x20) {
9755: memcpy(fcb + 1, name, 8);
9756: }
9757: if(!(REG8(AL) & 8) || ext[0] != 0x20) {
9758: memcpy(fcb + 9, ext, 3);
9759: }
9760: for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1 root 9761: if(fcb[i] == '*') {
9762: found_star = 1;
9763: }
9764: if(found_star) {
9765: fcb[i] = '?';
9766: }
9767: }
1.1.1.20 root 9768: for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1 root 9769: if(fcb[i] == '*') {
9770: found_star = 1;
9771: }
9772: if(found_star) {
9773: fcb[i] = '?';
9774: }
9775: }
9776:
1.1.1.44 root 9777: if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1 root 9778: if(memchr(fcb + 1, '?', 8 + 3)) {
9779: REG8(AL) = 0x01;
1.1.1.20 root 9780: } else {
9781: REG8(AL) = 0x00;
1.1 root 9782: }
9783: } else {
9784: REG8(AL) = 0xff;
9785: }
9786: }
9787:
9788: inline void msdos_int_21h_2ah()
9789: {
9790: SYSTEMTIME sTime;
9791:
9792: GetLocalTime(&sTime);
9793: REG16(CX) = sTime.wYear;
9794: REG8(DH) = (UINT8)sTime.wMonth;
9795: REG8(DL) = (UINT8)sTime.wDay;
9796: REG8(AL) = (UINT8)sTime.wDayOfWeek;
9797: }
9798:
9799: inline void msdos_int_21h_2bh()
9800: {
1.1.1.14 root 9801: REG8(AL) = 0xff;
1.1 root 9802: }
9803:
9804: inline void msdos_int_21h_2ch()
9805: {
9806: SYSTEMTIME sTime;
9807:
9808: GetLocalTime(&sTime);
9809: REG8(CH) = (UINT8)sTime.wHour;
9810: REG8(CL) = (UINT8)sTime.wMinute;
9811: REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14 root 9812: REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1 root 9813: }
9814:
9815: inline void msdos_int_21h_2dh()
9816: {
9817: REG8(AL) = 0x00;
9818: }
9819:
9820: inline void msdos_int_21h_2eh()
9821: {
9822: process_t *process = msdos_process_info_get(current_psp);
9823:
9824: process->verify = REG8(AL);
9825: }
9826:
9827: inline void msdos_int_21h_2fh()
9828: {
9829: process_t *process = msdos_process_info_get(current_psp);
9830:
9831: REG16(BX) = process->dta.w.l;
1.1.1.3 root 9832: SREG(ES) = process->dta.w.h;
9833: i386_load_segment_descriptor(ES);
1.1 root 9834: }
9835:
9836: inline void msdos_int_21h_30h()
9837: {
9838: // Version Flag / OEM
1.1.1.27 root 9839: if(REG8(AL) == 0x01) {
1.1.1.29 root 9840: #ifdef SUPPORT_HMA
9841: REG16(BX) = 0x0000;
9842: #else
9843: REG16(BX) = 0x1000; // DOS is in HMA
9844: #endif
1.1 root 9845: } else {
1.1.1.27 root 9846: // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
9847: // but this is not correct on Windows 98 SE
9848: // REG16(BX) = 0xff01; // OEM = Microsoft, PC/AT
9849: REG16(BX) = 0xff00; // OEM = Microsoft
1.1 root 9850: }
1.1.1.27 root 9851: REG16(CX) = 0x0000;
1.1.1.30 root 9852: REG8(AL) = dos_major_version; // 7
9853: REG8(AH) = dos_minor_version; // 10
1.1 root 9854: }
9855:
9856: inline void msdos_int_21h_31h()
9857: {
1.1.1.29 root 9858: try {
9859: msdos_mem_realloc(current_psp, REG16(DX), NULL);
9860: } catch(...) {
9861: // recover the broken mcb
9862: int mcb_seg = current_psp - 1;
9863: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 9864:
1.1.1.29 root 9865: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 9866: mcb->mz = 'M';
9867: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
9868:
1.1.1.29 root 9869: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 9870: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 9871: } else {
1.1.1.39 root 9872: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 9873: }
9874: } else {
9875: mcb->mz = 'Z';
1.1.1.30 root 9876: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 9877: }
9878: msdos_mem_realloc(current_psp, REG16(DX), NULL);
9879: }
1.1 root 9880: msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
9881: }
9882:
9883: inline void msdos_int_21h_32h()
9884: {
9885: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
9886: UINT16 seg, ofs;
9887:
9888: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9889: REG8(AL) = 0;
1.1.1.3 root 9890: SREG(DS) = seg;
9891: i386_load_segment_descriptor(DS);
1.1 root 9892: REG16(BX) = ofs;
9893: } else {
9894: REG8(AL) = 0xff;
1.1.1.3 root 9895: m_CF = 1;
1.1 root 9896: }
9897: }
9898:
9899: inline void msdos_int_21h_33h()
9900: {
9901: char path[MAX_PATH];
1.1.1.48 root 9902: char drive = 3; // C:
1.1 root 9903:
9904: switch(REG8(AL)) {
9905: case 0x00:
1.1.1.33 root 9906: REG8(DL) = ctrl_break_checking;
1.1 root 9907: break;
9908: case 0x01:
1.1.1.33 root 9909: ctrl_break_checking = REG8(DL);
9910: break;
9911: case 0x02:
9912: {
9913: UINT8 old = ctrl_break_checking;
9914: ctrl_break_checking = REG8(DL);
9915: REG8(DL) = old;
9916: }
9917: break;
9918: case 0x03:
9919: case 0x04:
9920: // DOS 4.0+ - Unused
1.1 root 9921: break;
9922: case 0x05:
1.1.1.48 root 9923: if(GetSystemDirectory(path, MAX_PATH) != 0) {
9924: if(path[0] >= 'a' && path[0] <= 'z') {
9925: drive = path[0] - 'a' + 1;
9926: } else if(path[0] >= 'A' && path[0] <= 'Z') {
9927: drive = path[0] - 'A' + 1;
9928: }
1.1 root 9929: }
1.1.1.48 root 9930: REG8(DL) = (UINT8)drive;
1.1 root 9931: break;
9932: case 0x06:
1.1.1.2 root 9933: // MS-DOS version (7.10)
1.1 root 9934: REG8(BL) = 7;
1.1.1.2 root 9935: REG8(BH) = 10;
1.1 root 9936: REG8(DL) = 0;
1.1.1.29 root 9937: #ifdef SUPPORT_HMA
9938: REG8(DH) = 0x00;
9939: #else
9940: REG8(DH) = 0x10; // DOS is in HMA
9941: #endif
1.1 root 9942: break;
1.1.1.6 root 9943: case 0x07:
9944: if(REG8(DL) == 0) {
9945: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
9946: } else if(REG8(DL) == 1) {
9947: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
9948: }
9949: break;
1.1 root 9950: default:
1.1.1.22 root 9951: 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 9952: // REG16(AX) = 0x01;
9953: // m_CF = 1;
9954: REG8(AL) = 0xff;
1.1 root 9955: break;
9956: }
9957: }
9958:
1.1.1.23 root 9959: inline void msdos_int_21h_34h()
9960: {
9961: SREG(ES) = SDA_TOP >> 4;
9962: i386_load_segment_descriptor(ES);
9963: REG16(BX) = offsetof(sda_t, indos_flag);;
9964: }
9965:
1.1 root 9966: inline void msdos_int_21h_35h()
9967: {
9968: REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3 root 9969: SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
9970: i386_load_segment_descriptor(ES);
1.1 root 9971: }
9972:
9973: inline void msdos_int_21h_36h()
9974: {
9975: struct _diskfree_t df = {0};
9976:
9977: if(_getdiskfree(REG8(DL), &df) == 0) {
9978: REG16(AX) = (UINT16)df.sectors_per_cluster;
9979: REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13 root 9980: REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
9981: REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1 root 9982: } else {
9983: REG16(AX) = 0xffff;
9984: }
9985: }
9986:
9987: inline void msdos_int_21h_37h()
9988: {
1.1.1.22 root 9989: static UINT8 dev_flag = 0xff;
1.1 root 9990:
9991: switch(REG8(AL)) {
9992: case 0x00:
1.1.1.22 root 9993: {
9994: process_t *process = msdos_process_info_get(current_psp);
9995: REG8(AL) = 0x00;
9996: REG8(DL) = process->switchar;
9997: }
1.1 root 9998: break;
9999: case 0x01:
1.1.1.22 root 10000: {
10001: process_t *process = msdos_process_info_get(current_psp);
10002: REG8(AL) = 0x00;
10003: process->switchar = REG8(DL);
1.1.1.23 root 10004: msdos_sda_update(current_psp);
1.1.1.22 root 10005: }
10006: break;
10007: case 0x02:
10008: REG8(DL) = dev_flag;
10009: break;
10010: case 0x03:
10011: dev_flag = REG8(DL);
10012: break;
10013: case 0xd0:
10014: case 0xd1:
10015: case 0xd2:
10016: case 0xd3:
10017: case 0xd4:
10018: case 0xd5:
10019: case 0xd6:
10020: case 0xd7:
10021: case 0xdc:
10022: case 0xdd:
10023: case 0xde:
10024: case 0xdf:
1.1.1.48 root 10025: // DIET v1.43e
10026: // REG16(AX) = 1;
10027: REG8(AL) = 0xff;
1.1 root 10028: break;
10029: default:
1.1.1.22 root 10030: 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 10031: // REG16(AX) = 1;
10032: REG8(AL) = 0xff;
1.1 root 10033: break;
10034: }
10035: }
10036:
1.1.1.42 root 10037: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17 root 10038: {
10039: char LCdata[80];
10040:
1.1.1.19 root 10041: ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42 root 10042: GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17 root 10043: ci->currency_dec_digits = atoi(LCdata);
1.1.1.42 root 10044: GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 10045: ci->currency_format = *LCdata - '0';
1.1.1.42 root 10046: GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 10047: ci->date_format = *LCdata - '0';
1.1.1.42 root 10048: GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 10049: memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42 root 10050: GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 10051: *ci->date_sep = *LCdata;
1.1.1.42 root 10052: GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17 root 10053: *ci->dec_sep = *LCdata;
1.1.1.42 root 10054: GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17 root 10055: *ci->list_sep = *LCdata;
1.1.1.42 root 10056: GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17 root 10057: *ci->thou_sep = *LCdata;
1.1.1.42 root 10058: GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17 root 10059: *ci->time_sep = *LCdata;
1.1.1.42 root 10060: GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17 root 10061: if(strchr(LCdata, 'H') != NULL) {
10062: ci->time_format = 1;
10063: }
1.1.1.49 root 10064: ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
10065: ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42 root 10066: GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17 root 10067: return atoi(LCdata);
10068: }
10069:
1.1.1.42 root 10070: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
10071: {
10072: return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
10073: }
10074:
10075: int get_country_info(country_info_t *ci)
10076: {
10077: return get_country_info(ci, LOCALE_USER_DEFAULT);
10078: }
10079:
1.1.1.43 root 10080: void set_country_info(country_info_t *ci, int size)
10081: {
10082: char LCdata[80];
10083:
10084: if(size >= 0x00 + 2) {
10085: memset(LCdata, 0, sizeof(LCdata));
10086: *LCdata = '0' + ci->date_format;
10087: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
10088: }
10089: if(size >= 0x02 + 5) {
10090: memset(LCdata, 0, sizeof(LCdata));
10091: memcpy(LCdata, &ci->currency_symbol, 4);
10092: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
10093: }
10094: if(size >= 0x07 + 2) {
10095: memset(LCdata, 0, sizeof(LCdata));
10096: *LCdata = *ci->thou_sep;
10097: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
10098: }
10099: if(size >= 0x09 + 2) {
10100: memset(LCdata, 0, sizeof(LCdata));
10101: *LCdata = *ci->dec_sep;
10102: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
10103: }
10104: if(size >= 0x0b + 2) {
10105: memset(LCdata, 0, sizeof(LCdata));
10106: *LCdata = *ci->date_sep;
10107: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
10108: }
10109: if(size >= 0x0d + 2) {
10110: memset(LCdata, 0, sizeof(LCdata));
10111: *LCdata = *ci->time_sep;
10112: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
10113: }
10114: if(size >= 0x0f + 1) {
10115: memset(LCdata, 0, sizeof(LCdata));
10116: *LCdata = '0' + ci->currency_format;
10117: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
10118: }
10119: if(size >= 0x10 + 1) {
10120: sprintf(LCdata, "%d", ci->currency_dec_digits);
10121: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
10122: }
10123: if(size >= 0x11 + 1) {
10124: // FIXME: is time format always H/h:mm:ss ???
10125: if(ci->time_format & 1) {
10126: sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
10127: } else {
10128: sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
10129: }
10130: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
10131: }
10132: if(size >= 0x12 + 4) {
10133: // 12h DWORD address of case map routine
10134: // (FAR CALL, AL = character to map to upper case [>= 80h])
10135: }
10136: if(size >= 0x16 + 2) {
10137: memset(LCdata, 0, sizeof(LCdata));
10138: *LCdata = *ci->list_sep;
10139: SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
10140: }
10141: }
10142:
1.1.1.42 root 10143: #ifndef SUBLANG_SWAHILI
10144: #define SUBLANG_SWAHILI 0x01
10145: #endif
10146: #ifndef SUBLANG_TSWANA_BOTSWANA
10147: #define SUBLANG_TSWANA_BOTSWANA 0x02
10148: #endif
10149: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
10150: #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
10151: #endif
10152: #ifndef LANG_BANGLA
10153: #define LANG_BANGLA 0x45
10154: #endif
10155: #ifndef SUBLANG_BANGLA_BANGLADESH
10156: #define SUBLANG_BANGLA_BANGLADESH 0x02
10157: #endif
10158:
10159: static const struct {
10160: int code;
10161: USHORT usPrimaryLanguage;
10162: USHORT usSubLanguage;
10163: } country_table[] = {
10164: {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US}, // United States
10165: {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN}, // Canadian-French
10166: {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN}, // Canada (English)
10167: {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA}, // Russia
10168: {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT}, // Egypt
10169: {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA}, // South Africa
10170: // {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA}, // South Africa
10171: // {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA}, // South Africa
10172: // {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA}, // South Africa
10173: // {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA}, // South Africa
10174: // {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA}, // South Africa
10175: {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE}, // Greece
10176: {0x01F, LANG_DUTCH, SUBLANG_DUTCH}, // Netherlands
10177: // {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS}, // Netherlands
10178: {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN}, // Belgium
10179: // {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN}, // Belgium
10180: {0x021, LANG_FRENCH, SUBLANG_FRENCH}, // France
10181: {0x022, LANG_SPANISH, SUBLANG_SPANISH}, // Spain
10182: {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria???
10183: {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY}, // Hungary (not supported by DR DOS 5.0)
10184: {0x027, LANG_ITALIAN, SUBLANG_ITALIAN}, // Italy / San Marino / Vatican City
10185: {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA}, // Romania
10186: {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS}, // Switzerland / Liechtenstein
10187: // {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS}, // Switzerland / Liechtenstein
10188: // {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS}, // Switzerland / Liechtenstein
10189: {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
10190: {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN}, // Austria (DR DOS 5.0)
10191: {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK}, // United Kingdom
10192: {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK}, // Denmark
10193: {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH}, // Sweden
10194: // {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN}, // Sweden
10195: // {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN}, // Sweden
10196: // {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN}, // Sweden
10197: {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL}, // Norway
10198: // {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK}, // Norway
10199: // {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY}, // Norway
10200: // {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY}, // Norway
10201: // {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY}, // Norway
10202: {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND}, // Poland (not supported by DR DOS 5.0)
10203: {0x031, LANG_GERMAN, SUBLANG_GERMAN}, // Germany
10204: {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU}, // Peru
10205: // {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU}, // Peru
10206: {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN}, // Mexico
10207: {0x035, LANG_SPANISH, SUBLANG_NEUTRAL}, // Cuba
10208: {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA}, // Argentina
10209: {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN}, // Brazil (not supported by DR DOS 5.0)
10210: {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE}, // Chile
10211: {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA}, // Columbia
10212: {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA}, // Venezuela
10213: {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA}, // Malaysia
10214: {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS}, // International English / Australia
10215: {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA}, // Indonesia / East Timor
10216: {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES}, // Philippines
10217: {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ}, // New Zealand
10218: {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE}, // Singapore
10219: // {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE}, // Singapore
10220: {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan???
10221: {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN}, // Japan (DR DOS 5.0, MS-DOS 5.0+)
10222: {0x052, LANG_KOREAN, SUBLANG_KOREAN}, // South Korea (DR DOS 5.0)
10223: {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM}, // Vietnam
10224: {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED}, // China (MS-DOS 5.0+)
10225: {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (MS-DOS 5.0+)
10226: {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY}, // Turkey (MS-DOS 5.0+)
10227: {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA}, // India
10228: {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN}, // Pakistan
10229: {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN}, // Afghanistan
10230: // {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN}, // Afghanistan
10231: {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA}, // Sri Lanka
10232: // {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA}, // Sri Lanka
10233: {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN}, // Iran
10234: {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS}, // Belarus
10235: {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND}, // Thailand
10236: {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO}, // Morocco
10237: {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA}, // Algeria
10238: {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA}, // Tunisia
10239: {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA}, // Libya
10240: {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL}, // Senegal
10241: // {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL}, // Senegal
10242: {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN}, // Nigeria
10243: // {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA}, // Nigeria
10244: // {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA}, // Nigeria
10245: {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA}, // Ethiopia
10246: // {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA}, // Ethiopia
10247: {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI}, // Kenya
10248: {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE}, // Zimbabwe
10249: {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA}, // Botswana
10250: {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS}, // Faroe Islands
10251: {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND}, // Greenland
10252: {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE}, // Portugal
10253: {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG}, // Luxembourg
10254: // {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG}, // Luxembourg
10255: // {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG}, // Luxembourg
10256: {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND}, // Ireland
10257: // {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND}, // Ireland
10258: {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND}, // Iceland
10259: {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA}, // Albania
10260: {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA}, // Malta
10261: {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND}, // Finland
10262: // {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND}, // Finland
10263: // {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND}, // Finland
10264: // {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND}, // Finland
10265: {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria
10266: {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA}, // Lithuania
10267: {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA}, // Latvia
10268: {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA}, // Estonia
10269: {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN}, // Serbia / Montenegro
10270: {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia???
10271: {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia
10272: {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA}, // Slovenia
10273: {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN}, // Bosnia-Herzegovina (Latin)
10274: {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC}, // Bosnia-Herzegovina (Cyrillic)
10275: {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA}, // FYR Macedonia
10276: {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC}, // Czech Republic
10277: {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Slovakia
10278: {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE}, // Belize
10279: {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA}, // Guatemala
10280: {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR}, // El Salvador
10281: {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS}, // Honduras
10282: {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA}, // Nicraragua
10283: {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA}, // Costa Rica
10284: {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA}, // Panama
10285: {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA}, // Bolivia
1.1.1.43 root 10286: // {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA}, // Bolivia
1.1.1.42 root 10287: {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR}, // Ecuador
10288: // {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR}, // Ecuador
10289: {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY}, // Paraguay
10290: {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY}, // Uruguay
10291: {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM}, // Brunei Darussalam
10292: {0x311, LANG_ARABIC, SUBLANG_NEUTRAL}, // Arabic (Middle East/Saudi Arabia/etc.)
10293: {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE}, // Ukraine
10294: {0x352, LANG_KOREAN, SUBLANG_KOREAN}, // North Korea
10295: {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG}, // Hong Kong
10296: {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU}, // Macao
10297: {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA}, // Cambodia
10298: {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH}, // Bangladesh
10299: {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (DOS 6.22+)
10300: {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES}, // Maldives
10301: {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON}, // Lebanon
10302: {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN}, // Jordan
10303: {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA}, // Syrian Arab Republic
10304: {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ}, // Ireq
10305: {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT}, // Kuwait
10306: {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA}, // Saudi Arabia
10307: {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN}, // Yemen
10308: {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN}, // Oman
10309: {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE}, // United Arab Emirates
10310: {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL}, // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
10311: {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN}, // Bahrain
10312: {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR}, // Qatar
10313: {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA}, // Mongolia
10314: // {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC}, // Mongolia
10315: {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL}, // Nepal
10316: {-1, 0, 0},
10317: };
10318:
1.1.1.14 root 10319: inline void msdos_int_21h_38h()
10320: {
10321: switch(REG8(AL)) {
10322: case 0x00:
1.1.1.19 root 10323: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14 root 10324: break;
10325: default:
1.1.1.42 root 10326: for(int i = 0;; i++) {
10327: if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
10328: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
10329: break;
10330: } else if(country_table[i].code == -1) {
10331: // 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));
10332: // REG16(AX) = 2;
10333: // m_CF = 1;
1.1.1.48 root 10334: // get current coutry info
1.1.1.42 root 10335: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
10336: break;
10337: }
10338: }
1.1.1.14 root 10339: break;
10340: }
10341: }
10342:
1.1 root 10343: inline void msdos_int_21h_39h(int lfn)
10344: {
1.1.1.3 root 10345: if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10346: REG16(AX) = errno;
1.1.1.3 root 10347: m_CF = 1;
1.1 root 10348: }
10349: }
10350:
10351: inline void msdos_int_21h_3ah(int lfn)
10352: {
1.1.1.3 root 10353: if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10354: REG16(AX) = errno;
1.1.1.3 root 10355: m_CF = 1;
1.1 root 10356: }
10357: }
10358:
10359: inline void msdos_int_21h_3bh(int lfn)
10360: {
1.1.1.45 root 10361: const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44 root 10362:
10363: if(_chdir(path)) {
1.1.1.17 root 10364: REG16(AX) = 3; // must be 3 (path not found)
1.1.1.3 root 10365: m_CF = 1;
1.1.1.44 root 10366: } else {
10367: int drv = _getdrive() - 1;
10368: if(path[1] == ':') {
10369: if(path[0] >= 'A' && path[0] <= 'Z') {
10370: drv = path[0] - 'A';
10371: } else if(path[0] >= 'a' && path[0] <= 'z') {
10372: drv = path[0] - 'a';
10373: }
10374: }
10375: msdos_cds_update(drv, path);
1.1 root 10376: }
10377: }
10378:
10379: inline void msdos_int_21h_3ch()
10380: {
1.1.1.45 root 10381: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 10382: int attr = GetFileAttributes(path);
1.1.1.37 root 10383: int fd = -1;
10384: int sio_port = 0;
10385: int lpt_port = 0;
1.1 root 10386:
1.1.1.45 root 10387: if(msdos_is_device_path(path)) {
10388: fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1 root 10389: } else {
10390: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
10391: }
10392: if(fd != -1) {
10393: if(attr == -1) {
10394: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
10395: }
10396: SetFileAttributes(path, attr);
10397: REG16(AX) = fd;
1.1.1.45 root 10398: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 10399: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 10400: } else {
10401: REG16(AX) = errno;
1.1.1.3 root 10402: m_CF = 1;
1.1 root 10403: }
10404: }
10405:
10406: inline void msdos_int_21h_3dh()
10407: {
1.1.1.45 root 10408: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 10409: int mode = REG8(AL) & 0x03;
1.1.1.37 root 10410: int fd = -1;
10411: int sio_port = 0;
10412: int lpt_port = 0;
1.1 root 10413:
10414: if(mode < 0x03) {
1.1.1.45 root 10415: if(msdos_is_device_path(path)) {
10416: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11 root 10417: } else {
1.1.1.13 root 10418: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 10419: }
1.1 root 10420: if(fd != -1) {
10421: REG16(AX) = fd;
1.1.1.45 root 10422: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 10423: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 10424: } else {
10425: REG16(AX) = errno;
1.1.1.3 root 10426: m_CF = 1;
1.1 root 10427: }
10428: } else {
10429: REG16(AX) = 0x0c;
1.1.1.3 root 10430: m_CF = 1;
1.1 root 10431: }
10432: }
10433:
10434: inline void msdos_int_21h_3eh()
10435: {
10436: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10437: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10438:
1.1.1.20 root 10439: if(fd < process->max_files && file_handler[fd].valid) {
10440: _close(fd);
10441: msdos_file_handler_close(fd);
10442: msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1 root 10443: } else {
10444: REG16(AX) = 0x06;
1.1.1.3 root 10445: m_CF = 1;
1.1 root 10446: }
10447: }
10448:
1.1.1.35 root 10449: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
10450: {
10451: UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
10452: int max = REG16(CX);
10453: int p = 0;
10454:
10455: while(max > p) {
10456: int chr = msdos_getch();
10457:
10458: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
10459: p = 0;
10460: buf[p++] = 0x0d;
10461: if(max > p) {
10462: buf[p++] = 0x0a;
10463: }
10464: msdos_putch(0x03);
10465: msdos_putch(0x0d);
10466: msdos_putch(0x0a);
10467: break;
10468: } else if(ctrl_break_pressed) {
10469: // skip this byte
10470: } else if(chr == 0x00) {
10471: // skip 2nd byte
10472: msdos_getch();
10473: } else if(chr == 0x0d) {
10474: // carriage return
10475: buf[p++] = 0x0d;
10476: if(max > p) {
10477: buf[p++] = 0x0a;
10478: }
10479: msdos_putch('\n');
10480: break;
10481: } else if(chr == 0x08) {
10482: // back space
10483: if(p > 0) {
10484: p--;
10485: if(msdos_ctrl_code_check(buf[p])) {
10486: msdos_putch(0x08);
10487: msdos_putch(0x08);
10488: msdos_putch(0x20);
10489: msdos_putch(0x20);
10490: msdos_putch(0x08);
10491: msdos_putch(0x08);
1.1.1.36 root 10492: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
10493: p--;
10494: msdos_putch(0x08);
10495: msdos_putch(0x08);
10496: msdos_putch(0x20);
10497: msdos_putch(0x20);
10498: msdos_putch(0x08);
10499: msdos_putch(0x08);
1.1.1.35 root 10500: } else {
10501: msdos_putch(0x08);
10502: msdos_putch(0x20);
10503: msdos_putch(0x08);
10504: }
10505: }
10506: } else if(chr == 0x1b) {
10507: // escape
10508: while(p > 0) {
10509: p--;
10510: if(msdos_ctrl_code_check(buf[p])) {
10511: msdos_putch(0x08);
10512: msdos_putch(0x08);
10513: msdos_putch(0x20);
10514: msdos_putch(0x20);
10515: msdos_putch(0x08);
10516: msdos_putch(0x08);
10517: } else {
10518: msdos_putch(0x08);
10519: msdos_putch(0x20);
10520: msdos_putch(0x08);
10521: }
10522: }
10523: } else {
10524: buf[p++] = chr;
10525: msdos_putch(chr);
10526: }
10527: }
10528: REG16(AX) = p;
10529:
10530: #ifdef USE_SERVICE_THREAD
10531: service_exit = true;
10532: #endif
10533: return(0);
10534: }
10535:
1.1 root 10536: inline void msdos_int_21h_3fh()
10537: {
10538: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10539: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10540:
1.1.1.20 root 10541: if(fd < process->max_files && file_handler[fd].valid) {
10542: if(file_mode[file_handler[fd].mode].in) {
10543: if(file_handler[fd].atty) {
1.1 root 10544: // BX is stdin or is redirected to stdin
1.1.1.35 root 10545: if(REG16(CX) != 0) {
10546: #ifdef USE_SERVICE_THREAD
1.1.1.51! root 10547: if(!in_service && !in_service_29h &&
! 10548: *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50 root 10549: *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
10550: // msdos_putch() will be used in this service
10551: // if int 29h is hooked, run this service in main thread to call int 29h
10552: start_service_loop(msdos_int_21h_3fh_thread);
10553: } else {
10554: #endif
10555: msdos_int_21h_3fh_thread(NULL);
10556: REQUEST_HARDWRE_UPDATE();
10557: #ifdef USE_SERVICE_THREAD
10558: }
1.1.1.35 root 10559: #endif
10560: } else {
10561: REG16(AX) = 0;
1.1 root 10562: }
10563: } else {
1.1.1.37 root 10564: REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 10565: }
10566: } else {
10567: REG16(AX) = 0x05;
1.1.1.3 root 10568: m_CF = 1;
1.1 root 10569: }
10570: } else {
10571: REG16(AX) = 0x06;
1.1.1.3 root 10572: m_CF = 1;
1.1 root 10573: }
10574: }
10575:
10576: inline void msdos_int_21h_40h()
10577: {
10578: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10579: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10580:
1.1.1.20 root 10581: if(fd < process->max_files && file_handler[fd].valid) {
10582: if(file_mode[file_handler[fd].mode].out) {
1.1 root 10583: if(REG16(CX)) {
1.1.1.20 root 10584: if(file_handler[fd].atty) {
1.1 root 10585: // BX is stdout/stderr or is redirected to stdout
10586: for(int i = 0; i < REG16(CX); i++) {
1.1.1.3 root 10587: msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1 root 10588: }
10589: REG16(AX) = REG16(CX);
10590: } else {
1.1.1.20 root 10591: REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 10592: }
10593: } else {
1.1.1.20 root 10594: UINT32 pos = _tell(fd);
10595: _lseek(fd, 0, SEEK_END);
10596: UINT32 size = _tell(fd);
1.1.1.12 root 10597: if(pos < size) {
1.1.1.20 root 10598: _lseek(fd, pos, SEEK_SET);
10599: SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12 root 10600: } else {
10601: for(UINT32 i = size; i < pos; i++) {
10602: UINT8 tmp = 0;
1.1.1.23 root 10603: msdos_write(fd, &tmp, 1);
1.1.1.12 root 10604: }
1.1.1.20 root 10605: _lseek(fd, pos, SEEK_SET);
1.1 root 10606: }
1.1.1.23 root 10607: REG16(AX) = 0;
1.1 root 10608: }
10609: } else {
10610: REG16(AX) = 0x05;
1.1.1.3 root 10611: m_CF = 1;
1.1 root 10612: }
10613: } else {
10614: REG16(AX) = 0x06;
1.1.1.3 root 10615: m_CF = 1;
1.1 root 10616: }
10617: }
10618:
10619: inline void msdos_int_21h_41h(int lfn)
10620: {
1.1.1.3 root 10621: if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10622: REG16(AX) = errno;
1.1.1.3 root 10623: m_CF = 1;
1.1 root 10624: }
10625: }
10626:
10627: inline void msdos_int_21h_42h()
10628: {
10629: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10630: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10631:
1.1.1.20 root 10632: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 10633: if(REG8(AL) < 0x03) {
1.1.1.35 root 10634: static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20 root 10635: _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
10636: UINT32 pos = _tell(fd);
1.1 root 10637: REG16(AX) = pos & 0xffff;
10638: REG16(DX) = (pos >> 16);
10639: } else {
10640: REG16(AX) = 0x01;
1.1.1.3 root 10641: m_CF = 1;
1.1 root 10642: }
10643: } else {
10644: REG16(AX) = 0x06;
1.1.1.3 root 10645: m_CF = 1;
1.1 root 10646: }
10647: }
10648:
10649: inline void msdos_int_21h_43h(int lfn)
10650: {
1.1.1.45 root 10651: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1 root 10652: int attr;
10653:
1.1.1.14 root 10654: if(!lfn && REG8(AL) > 2) {
10655: REG16(AX) = 0x01;
10656: m_CF = 1;
10657: return;
10658: }
10659: switch(REG8(lfn ? BL : AL)) {
1.1 root 10660: case 0x00:
10661: if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14 root 10662: REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
10663: } else {
10664: REG16(AX) = (UINT16)GetLastError();
10665: m_CF = 1;
10666: }
10667: break;
10668: case 0x01:
10669: if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
10670: REG16(AX) = (UINT16)GetLastError();
10671: m_CF = 1;
10672: }
10673: break;
10674: case 0x02:
10675: {
1.1.1.45 root 10676: DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
10677: if(compressed_size != INVALID_FILE_SIZE) {
10678: if(compressed_size != 0) {
10679: HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
10680: if(hFile != INVALID_HANDLE_VALUE) {
10681: file_size = GetFileSize(hFile, NULL);
10682: CloseHandle(hFile);
10683: }
10684: if(compressed_size == file_size) {
10685: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
10686: // this isn't correct if the file is in the NTFS MFT
10687: if(GetDiskFreeSpace(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
10688: compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
10689: }
1.1.1.14 root 10690: }
10691: }
1.1.1.45 root 10692: REG16(AX) = LOWORD(compressed_size);
10693: REG16(DX) = HIWORD(compressed_size);
1.1.1.14 root 10694: } else {
10695: REG16(AX) = (UINT16)GetLastError();
10696: m_CF = 1;
1.1 root 10697: }
1.1.1.14 root 10698: }
10699: break;
10700: case 0x03:
10701: case 0x05:
10702: case 0x07:
1.1.1.48 root 10703: if(lfn) {
1.1.1.14 root 10704: HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
10705: if(hFile != INVALID_HANDLE_VALUE) {
10706: FILETIME local, time;
10707: DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
10708: if(REG8(BL) == 7) {
10709: ULARGE_INTEGER hund;
10710: hund.LowPart = local.dwLowDateTime;
10711: hund.HighPart = local.dwHighDateTime;
10712: hund.QuadPart += REG16(SI) * 100000;
10713: local.dwLowDateTime = hund.LowPart;
10714: local.dwHighDateTime = hund.HighPart;
10715: }
10716: LocalFileTimeToFileTime(&local, &time);
10717: if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
10718: REG8(BL) == 0x05 ? &time : NULL,
10719: REG8(BL) == 0x03 ? &time : NULL)) {
10720: REG16(AX) = (UINT16)GetLastError();
10721: m_CF = 1;
10722: }
10723: CloseHandle(hFile);
10724: } else {
10725: REG16(AX) = (UINT16)GetLastError();
10726: m_CF = 1;
1.1 root 10727: }
1.1.1.48 root 10728: } else {
10729: // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
10730: // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
10731: // 214307 DR DOS 6.0 - Set File Owner
10732: // 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));
10733: REG16(AX) = 0x01;
10734: m_CF = 1;
1.1.1.14 root 10735: }
10736: break;
10737: case 0x04:
10738: case 0x06:
10739: case 0x08:
1.1.1.48 root 10740: if(lfn) {
1.1.1.14 root 10741: WIN32_FILE_ATTRIBUTE_DATA fad;
10742: if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
10743: FILETIME *time, local;
10744: time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
10745: 0x06 ? &fad.ftLastAccessTime :
10746: &fad.ftCreationTime;
10747: FileTimeToLocalFileTime(time, &local);
10748: FileTimeToDosDateTime(&local, ®16(DI), ®16(CX));
10749: if(REG8(BL) == 0x08) {
10750: ULARGE_INTEGER hund;
10751: hund.LowPart = local.dwLowDateTime;
10752: hund.HighPart = local.dwHighDateTime;
10753: hund.QuadPart /= 100000;
10754: REG16(SI) = (UINT16)(hund.QuadPart % 200);
10755: }
10756: } else {
10757: REG16(AX) = (UINT16)GetLastError();
10758: m_CF = 1;
1.1 root 10759: }
1.1.1.48 root 10760: } else {
10761: // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
10762: // 214306 DR DOS 6.0 - Get File Owner
10763: // 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));
10764: REG16(AX) = 0x01;
10765: m_CF = 1;
1.1.1.14 root 10766: }
10767: break;
1.1.1.43 root 10768: case 0xff:
1.1.1.48 root 10769: if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43 root 10770: if(REG8(CL) == 0x39) {
10771: msdos_int_21h_39h(1);
10772: break;
10773: } else if(REG8(CL) == 0x56) {
10774: msdos_int_21h_56h(1);
10775: break;
10776: }
10777: }
1.1.1.14 root 10778: default:
1.1.1.22 root 10779: 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 10780: REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14 root 10781: m_CF = 1;
10782: break;
10783: }
10784: }
10785:
10786: inline void msdos_int_21h_44h()
10787: {
1.1.1.22 root 10788: static UINT16 iteration_count = 0;
10789:
1.1.1.44 root 10790: process_t *process;
10791: int fd, drv;
1.1.1.14 root 10792:
10793: switch(REG8(AL)) {
10794: case 0x00:
10795: case 0x01:
10796: case 0x02:
10797: case 0x03:
10798: case 0x04:
10799: case 0x05:
10800: case 0x06:
10801: case 0x07:
1.1.1.44 root 10802: process = msdos_process_info_get(current_psp);
10803: fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20 root 10804: if(fd >= process->max_files || !file_handler[fd].valid) {
10805: REG16(AX) = 0x06;
10806: m_CF = 1;
10807: return;
1.1.1.14 root 10808: }
10809: break;
10810: case 0x08:
10811: case 0x09:
1.1.1.44 root 10812: drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
10813: if(!msdos_is_valid_drive(drv)) {
10814: // invalid drive
1.1.1.14 root 10815: REG16(AX) = 0x0f;
10816: m_CF = 1;
10817: return;
1.1 root 10818: }
10819: break;
10820: }
10821: switch(REG8(AL)) {
1.1.1.48 root 10822: case 0x00: // Get Device Information
1.1.1.20 root 10823: REG16(DX) = file_handler[fd].info;
1.1 root 10824: break;
1.1.1.48 root 10825: case 0x01: // Set Device Information
1.1.1.45 root 10826: if(REG8(DH) != 0) {
10827: // REG16(AX) = 0x0d; // data invalid
10828: // m_CF = 1;
10829: file_handler[fd].info = REG16(DX);
10830: } else {
10831: file_handler[fd].info &= 0xff00;
10832: file_handler[fd].info |= REG8(DL);
10833: }
1.1 root 10834: break;
1.1.1.48 root 10835: case 0x02: // Read From Character Device Control Channel
1.1.1.45 root 10836: if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
10837: // from DOSBox
10838: switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
10839: case 0x00:
10840: if(REG16(CX) >= 6) {
10841: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
10842: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
10843: REG16(AX) = 6; // number of bytes actually read
10844: } else {
10845: REG16(AX) = 0x0d; // data invalid
10846: m_CF = 1;
10847: }
10848: break;
10849: case 0x01:
10850: if(REG16(CX) >= 6) {
10851: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004; // flags
10852: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d; // size of this structure
10853: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001; // version 1.0
10854: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
10855: for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
10856: if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
10857: int page = (addr - EMS_TOP) / 0x4000;
10858: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03; // frame type: EMS frame in 64k page
10859: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff; // owner: NONE
10860: *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff; // no logical page number
10861: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 4) = page; // physical EMS page number
10862: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00; // flags: EMS frame
10863: } else {
10864: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00; // frame type: NONE
10865: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff; // owner: NONE
10866: *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff; // non-EMS frame
10867: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff; // EMS page number (NONE)
10868: *(UINT8 *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa; // flags: direct mapping
10869: }
10870: }
10871: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74; // ??
10872: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00; // no UMB descriptors following
10873: *(UINT8 *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01; // 1 EMS handle info record
10874: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000; // system handle
10875: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
10876: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
10877: *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001; // system handle
10878: *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
10879:
10880: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
10881: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
10882: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
10883: REG16(AX) = 6; // number of bytes actually read
10884: } else {
10885: REG16(AX) = 0x0d; // data invalid
10886: m_CF = 1;
10887: }
10888: break;
10889: case 0x02:
10890: if(REG16(CX) >= 2) {
10891: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
10892: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
10893: REG16(AX) = 2; // number of bytes actually read
10894: } else {
10895: REG16(AX) = 0x0d; // data invalid
10896: m_CF = 1;
10897: }
10898: break;
10899: case 0x03:
10900: if(REG16(CX) >= 4) {
10901: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
10902: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
10903: REG16(AX) = 4; // number of bytes actually read
10904: } else {
10905: REG16(AX) = 0x0d; // data invalid
10906: m_CF = 1;
10907: }
10908: break;
10909: default:
10910: REG16(AX) = 0x01; // function number invalid
10911: m_CF = 1;
10912: }
10913: } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
10914: if(REG16(CX) >= 5) {
10915: memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
10916: REG16(AX) = 5; // number of bytes actually read
10917: } else {
10918: REG16(AX) = 0x0d; // data invalid
10919: m_CF = 1;
10920: }
10921: } else {
10922: // memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
10923: // REG16(AX) = REG16(CX);
10924: REG16(AX) = 0x05; // access denied
10925: m_CF = 1;
10926: }
10927: break;
1.1.1.48 root 10928: case 0x03: // Write To Character Device Control Channel
1.1.1.45 root 10929: // REG16(AX) = 0x05;
10930: // m_CF = 1;
10931: REG16(AX) = 0x00; // success
10932: break;
1.1.1.48 root 10933: case 0x04: // Read From Block Device Control Channel
10934: case 0x05: // Write To Block Device Control Channel
1.1 root 10935: REG16(AX) = 0x05;
1.1.1.3 root 10936: m_CF = 1;
1.1 root 10937: break;
1.1.1.48 root 10938: case 0x06: // Get Input Status
1.1.1.20 root 10939: if(file_mode[file_handler[fd].mode].in) {
10940: if(file_handler[fd].atty) {
1.1.1.14 root 10941: REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1 root 10942: } else {
1.1.1.20 root 10943: REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1 root 10944: }
1.1.1.14 root 10945: } else {
10946: REG8(AL) = 0x00;
1.1 root 10947: }
10948: break;
1.1.1.48 root 10949: case 0x07: // Get Output Status
1.1.1.20 root 10950: if(file_mode[file_handler[fd].mode].out) {
1.1.1.14 root 10951: REG8(AL) = 0xff;
10952: } else {
10953: REG8(AL) = 0x00;
1.1 root 10954: }
10955: break;
1.1.1.48 root 10956: case 0x08: // Check If Block Device Removable
1.1.1.44 root 10957: if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14 root 10958: // removable drive
10959: REG16(AX) = 0x00;
1.1 root 10960: } else {
1.1.1.14 root 10961: // fixed drive
10962: REG16(AX) = 0x01;
1.1 root 10963: }
10964: break;
1.1.1.48 root 10965: case 0x09: // Check If Block Device Remote
1.1.1.44 root 10966: if(msdos_is_remote_drive(drv)) {
1.1.1.14 root 10967: // remote drive
10968: REG16(DX) = 0x1000;
1.1.1.44 root 10969: } else if(msdos_is_subst_drive(drv)) {
10970: // subst drive
10971: REG16(DX) = 0x8000;
1.1 root 10972: } else {
1.1.1.14 root 10973: // local drive
1.1.1.44 root 10974: REG16(DX) = 0x0000;
1.1 root 10975: }
10976: break;
1.1.1.48 root 10977: case 0x0a: // Check If Handle Is Remote
1.1.1.45 root 10978: if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
10979: REG16(DX) = 0x8000;
10980: } else {
10981: REG16(DX) = 0x0000;
10982: }
1.1.1.21 root 10983: break;
1.1.1.48 root 10984: case 0x0b: // Set Sharing Retry Count
1.1 root 10985: break;
1.1.1.48 root 10986: case 0x0c: // Generic Character Device Request
1.1.1.22 root 10987: if(REG8(CL) == 0x45) {
10988: // set iteration (retry) count
10989: iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
10990: } else if(REG8(CL) == 0x4a) {
10991: // select code page
10992: active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
10993: msdos_nls_tables_update();
1.1.1.44 root 10994: } else if(REG8(CL) == 0x4c) {
10995: // start code-page preparation
10996: int ids[3] = {437, 0, 0}; // 437: US English
10997: int count = 1, offset = 0;
10998: if(active_code_page != 437) {
10999: ids[count++] = active_code_page;
11000: }
11001: if(system_code_page != 437 && system_code_page != active_code_page) {
11002: ids[count++] = system_code_page;
11003: }
11004: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
11005: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
11006: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11007: for(int i = 0; i < count; i++) {
11008: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11009: }
11010: } else if(REG8(CL) == 0x4d) {
11011: // end code-page preparation
11012: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
11013: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50 root 11014: } else if(REG8(CL) == 0x5f) {
11015: // set display information
11016: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
11017: int cur_width = *(UINT16 *)(mem + 0x44a) + 0;
11018: int cur_height = *(UINT8 *)(mem + 0x484) + 1;
11019: int new_width = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // character columns
11020: int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10); // character rows
11021:
11022: if(cur_width != new_width || cur_height != new_height) {
11023: pcbios_set_console_size(new_width, new_height, true);
11024: }
11025: }
1.1.1.22 root 11026: } else if(REG8(CL) == 0x65) {
11027: // get iteration (retry) count
11028: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
11029: } else if(REG8(CL) == 0x6a) {
11030: // query selected code page
11031: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
11032: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
11033:
11034: CPINFO info;
11035: GetCPInfo(active_code_page, &info);
11036:
11037: if(info.MaxCharSize != 1) {
11038: for(int i = 0;; i++) {
11039: UINT8 lo = info.LeadByte[2 * i + 0];
11040: UINT8 hi = info.LeadByte[2 * i + 1];
11041:
11042: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
11043: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
11044: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
11045:
11046: if(lo == 0 && hi == 0) {
11047: break;
11048: }
11049: }
11050: }
1.1.1.44 root 11051: } else if(REG8(CL) == 0x6b) {
11052: // query prepare list
11053: int ids[3] = {437, 0, 0}; // 437: US English
11054: int count = 1, offset = 0;
11055: if(active_code_page != 437) {
11056: ids[count++] = active_code_page;
11057: }
11058: if(system_code_page != 437 && system_code_page != active_code_page) {
11059: ids[count++] = system_code_page;
11060: }
11061: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
11062: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11063: for(int i = 0; i < count; i++) {
11064: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11065: }
11066: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
11067: for(int i = 0; i < count; i++) {
11068: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
11069: }
1.1.1.22 root 11070: } else if(REG8(CL) == 0x7f) {
1.1.1.44 root 11071: // get display information
1.1.1.50 root 11072: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0; // level (0 for DOS 4.x-6.0)
11073: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0; // reserved (0)
11074: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14; // length of following data (14)
11075: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1; // bit 0 set for blink, clear for intensity
11076: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1; // mode type (1=text, 2=graphics)
11077: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0; // reserved (0)
11078: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4; // 4 bits per pixel
11079: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) = 8 * (*(UINT16 *)(mem + 0x44a) + 0); // pixel columns
11080: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8 *)(mem + 0x484) + 1); // pixel rows
11081: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0; // character columns
11082: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8 *)(mem + 0x484) + 1; // character rows
1.1.1.22 root 11083: } else {
11084: 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));
11085: REG16(AX) = 0x01; // invalid function
11086: m_CF = 1;
11087: }
11088: break;
1.1.1.48 root 11089: case 0x0d: // Generic Block Device Request
1.1.1.22 root 11090: if(REG8(CL) == 0x40) {
11091: // set device parameters
1.1.1.48 root 11092: // } else if(REG8(CL) == 0x41) {
11093: // // write logical device track
11094: // } else if(REG8(CL) == 0x42) {
11095: // // format and verify logical device track
1.1.1.22 root 11096: } else if(REG8(CL) == 0x46) {
11097: // set volume serial number
1.1.1.48 root 11098: } else if(REG8(CL) == 0x47) {
11099: // set access flag
11100: // } else if(REG8(CL) == 0x48) {
11101: // // set media lock state
11102: // } else if(REG8(CL) == 0x49) {
11103: // // eject media in drive
1.1.1.22 root 11104: } else if(REG8(CL) == 0x4a) {
11105: // lock logical volume
11106: } else if(REG8(CL) == 0x4b) {
11107: // lock physical volume
11108: } else if(REG8(CL) == 0x60) {
11109: // get device parameters
1.1.1.42 root 11110: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 11111:
1.1.1.42 root 11112: if(pcbios_update_drive_param(drive_num, 1)) {
11113: drive_param_t *drive_param = &drive_params[drive_num];
11114: DISK_GEOMETRY *geo = &drive_param->geometry;
11115:
11116: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
11117: switch(geo->MediaType) {
11118: case F5_360_512:
11119: case F5_320_512:
11120: case F5_320_1024:
11121: case F5_180_512:
11122: case F5_160_512:
11123: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
11124: break;
11125: case F5_1Pt2_512:
11126: case F3_1Pt2_512:
11127: case F3_1Pt23_1024:
11128: case F5_1Pt23_1024:
11129: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
11130: break;
11131: case F3_720_512:
11132: case F3_640_512:
11133: case F5_640_512:
11134: case F5_720_512:
11135: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
11136: break;
11137: case F8_256_128:
11138: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
11139: break;
11140: case FixedMedia:
11141: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
11142: break;
11143: case F3_1Pt44_512:
11144: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
11145: break;
11146: case F3_2Pt88_512:
11147: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
11148: break;
11149: default:
11150: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
11151: // *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
11152: break;
1.1.1.22 root 11153: }
1.1.1.42 root 11154: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
11155: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
11156: switch(geo->MediaType) {
11157: case F5_360_512:
11158: case F5_320_512:
11159: case F5_320_1024:
11160: case F5_180_512:
11161: case F5_160_512:
11162: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
11163: break;
11164: default:
11165: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
11166: break;
11167: }
11168: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
11169: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
11170: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
11171: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
11172: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
11173: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
11174: switch(geo->MediaType) {
11175: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
11176: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
11177: break;
11178: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
11179: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
11180: break;
11181: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
11182: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
11183: break;
11184: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
11185: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
11186: break;
11187: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
11188: case F3_1Pt2_512:
11189: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
11190: case F5_720_512:
11191: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
11192: break;
11193: case FixedMedia: // hard disk
11194: case RemovableMedia:
11195: case Unknown:
11196: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
11197: break;
11198: default:
11199: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
11200: break;
11201: }
11202: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
11203: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
11204: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
11205: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
11206: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
11207: // 21h BYTE device type
11208: // 22h WORD device attributes (removable or not, etc)
1.1.1.22 root 11209: } else {
11210: REG16(AX) = 0x0f; // invalid drive
11211: m_CF = 1;
11212: }
1.1.1.48 root 11213: // } else if(REG8(CL) == 0x61) {
11214: // // read logical device track
11215: // } else if(REG8(CL) == 0x62) {
11216: // // verify logical device track
1.1.1.22 root 11217: } else if(REG8(CL) == 0x66) {
11218: // get volume serial number
11219: char path[] = "A:\\";
11220: char volume_label[MAX_PATH];
11221: DWORD serial_number = 0;
11222: char file_system[MAX_PATH];
11223:
11224: path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
11225:
11226: if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
11227: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
11228: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
11229: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
11230: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
11231: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20, 8);
11232: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
11233: } else {
11234: REG16(AX) = 0x0f; // invalid drive
11235: m_CF = 1;
11236: }
11237: } else if(REG8(CL) == 0x67) {
11238: // get access flag
11239: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
11240: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
11241: } else if(REG8(CL) == 0x68) {
11242: // sense media type
1.1.1.42 root 11243: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 11244:
1.1.1.42 root 11245: if(pcbios_update_drive_param(drive_num, 1)) {
11246: drive_param_t *drive_param = &drive_params[drive_num];
11247: DISK_GEOMETRY *geo = &drive_param->geometry;
11248:
11249: switch(geo->MediaType) {
11250: case F3_720_512:
11251: case F5_720_512:
11252: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11253: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
11254: break;
11255: case F3_1Pt44_512:
11256: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11257: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
11258: break;
11259: case F3_2Pt88_512:
11260: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
11261: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
11262: break;
11263: default:
11264: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
11265: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
11266: break;
1.1.1.22 root 11267: }
11268: } else {
11269: REG16(AX) = 0x0f; // invalid drive
11270: m_CF = 1;
11271: }
11272: } else if(REG8(CL) == 0x6a) {
11273: // unlock logical volume
11274: } else if(REG8(CL) == 0x6b) {
11275: // unlock physical volume
1.1.1.48 root 11276: // } else if(REG8(CL) == 0x6c) {
11277: // // get lock flag
11278: // } else if(REG8(CL) == 0x6d) {
11279: // // enumerate open files
11280: // } else if(REG8(CL) == 0x6e) {
11281: // // find swap file
11282: // } else if(REG8(CL) == 0x6f) {
11283: // // get drive map information
11284: // } else if(REG8(CL) == 0x70) {
11285: // // get current lock state
11286: // } else if(REG8(CL) == 0x71) {
11287: // // get first cluster
1.1.1.22 root 11288: } else {
11289: 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));
11290: REG16(AX) = 0x01; // invalid function
11291: m_CF = 1;
11292: }
11293: break;
1.1.1.48 root 11294: case 0x0e: // Get Lgical Drive Map
1.1.1.44 root 11295: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
11296: REG16(AX) = 0x0f; // invalid drive
11297: m_CF = 1;
11298: } else {
11299: REG8(AL) = 0;
1.1.1.22 root 11300: }
11301: break;
1.1.1.48 root 11302: case 0x0f: // Set Logical Drive Map
1.1.1.44 root 11303: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
11304: REG16(AX) = 0x0f; // invalid drive
11305: m_CF = 1;
1.1.1.22 root 11306: }
11307: break;
1.1.1.48 root 11308: case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22 root 11309: switch(REG8(CL)) {
11310: case 0x45:
11311: case 0x4a:
1.1.1.48 root 11312: case 0x4c:
11313: case 0x4d:
1.1.1.22 root 11314: case 0x65:
11315: case 0x6a:
1.1.1.48 root 11316: case 0x6b:
1.1.1.22 root 11317: case 0x7f:
11318: REG16(AX) = 0x0000; // supported
11319: break;
11320: default:
11321: REG8(AL) = 0x01; // ioctl capability not available
11322: m_CF = 1;
11323: break;
11324: }
11325: break;
1.1.1.48 root 11326: case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22 root 11327: switch(REG8(CL)) {
11328: case 0x40:
11329: case 0x46:
11330: case 0x4a:
11331: case 0x4b:
11332: case 0x60:
11333: case 0x66:
11334: case 0x67:
11335: case 0x68:
11336: case 0x6a:
11337: case 0x6b:
1.1.1.48 root 11338: if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
11339: // CH = 00h Unknown
11340: // CH = 01h COMn:
11341: // CH = 03h CON
11342: // CH = 05h LPTn:
11343: REG16(AX) = 0x0000; // supported
11344: break;
11345: }
1.1.1.22 root 11346: default:
11347: REG8(AL) = 0x01; // ioctl capability not available
11348: m_CF = 1;
11349: break;
11350: }
11351: break;
1.1.1.48 root 11352: case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
11353: case 0x14: // DR DOS 5.0-6.0 - Set Global Password
11354: case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
11355: case 0x51: // Concurrent DOS v3.2+ - Installation Check
11356: case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
11357: case 0x54: // DR DOS 3.41+ - Set Global Password
11358: case 0x56: // DR DOS 5.0+ - History Buffer Control
11359: case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
11360: case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
11361: case 0x59: // DR Multiuser DOS 5.0 - API
11362: REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22 root 11363: m_CF = 1;
11364: break;
1.1 root 11365: default:
1.1.1.22 root 11366: 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 11367: REG16(AX) = 0x01;
1.1.1.3 root 11368: m_CF = 1;
1.1 root 11369: break;
11370: }
11371: }
11372:
11373: inline void msdos_int_21h_45h()
11374: {
11375: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11376: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11377:
1.1.1.20 root 11378: if(fd < process->max_files && file_handler[fd].valid) {
11379: int dup_fd = _dup(fd);
11380: if(dup_fd != -1) {
11381: REG16(AX) = dup_fd;
11382: msdos_file_handler_dup(dup_fd, fd, current_psp);
11383: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
11384: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 11385: } else {
11386: REG16(AX) = errno;
1.1.1.3 root 11387: m_CF = 1;
1.1 root 11388: }
11389: } else {
11390: REG16(AX) = 0x06;
1.1.1.3 root 11391: m_CF = 1;
1.1 root 11392: }
11393: }
11394:
11395: inline void msdos_int_21h_46h()
11396: {
11397: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11398: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
11399: int dup_fd = REG16(CX);
11400: int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1 root 11401:
1.1.1.20 root 11402: if(REG16(BX) == REG16(CX)) {
11403: REG16(AX) = 0x06;
11404: m_CF = 1;
11405: } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
11406: if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
11407: _close(tmp_fd);
11408: msdos_file_handler_close(tmp_fd);
11409: msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
11410: }
11411: if(_dup2(fd, dup_fd) != -1) {
11412: msdos_file_handler_dup(dup_fd, fd, current_psp);
11413: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
11414: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 11415: } else {
11416: REG16(AX) = errno;
1.1.1.3 root 11417: m_CF = 1;
1.1 root 11418: }
11419: } else {
11420: REG16(AX) = 0x06;
1.1.1.3 root 11421: m_CF = 1;
1.1 root 11422: }
11423: }
11424:
11425: inline void msdos_int_21h_47h(int lfn)
11426: {
11427: char path[MAX_PATH];
11428:
11429: if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45 root 11430: if(!lfn) {
11431: strcpy(path, msdos_short_path(path));
11432: }
1.1 root 11433: if(path[1] == ':') {
11434: // the returned path does not include a drive or the initial backslash
1.1.1.45 root 11435: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1 root 11436: } else {
1.1.1.45 root 11437: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1 root 11438: }
11439: } else {
11440: REG16(AX) = errno;
1.1.1.3 root 11441: m_CF = 1;
1.1 root 11442: }
11443: }
11444:
11445: inline void msdos_int_21h_48h()
11446: {
1.1.1.19 root 11447: int seg, umb_linked;
1.1 root 11448:
1.1.1.8 root 11449: if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19 root 11450: // unlink umb not to allocate memory in umb
11451: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
11452: msdos_mem_unlink_umb();
11453: }
1.1.1.8 root 11454: if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
11455: REG16(AX) = seg;
11456: } else {
11457: REG16(AX) = 0x08;
11458: REG16(BX) = msdos_mem_get_free(first_mcb, 0);
11459: m_CF = 1;
11460: }
1.1.1.19 root 11461: if(umb_linked != 0) {
11462: msdos_mem_link_umb();
11463: }
1.1.1.8 root 11464: } else if((malloc_strategy & 0xf0) == 0x40) {
11465: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
11466: REG16(AX) = seg;
11467: } else {
11468: REG16(AX) = 0x08;
11469: REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
11470: m_CF = 1;
11471: }
11472: } else if((malloc_strategy & 0xf0) == 0x80) {
11473: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
11474: REG16(AX) = seg;
11475: } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
11476: REG16(AX) = seg;
11477: } else {
11478: REG16(AX) = 0x08;
11479: REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
11480: m_CF = 1;
11481: }
1.1 root 11482: }
11483: }
11484:
11485: inline void msdos_int_21h_49h()
11486: {
1.1.1.14 root 11487: int mcb_seg = SREG(ES) - 1;
11488: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
11489:
11490: if(mcb->mz == 'M' || mcb->mz == 'Z') {
11491: msdos_mem_free(SREG(ES));
11492: } else {
1.1.1.33 root 11493: REG16(AX) = 0x09; // illegal memory block address
1.1.1.14 root 11494: m_CF = 1;
11495: }
1.1 root 11496: }
11497:
11498: inline void msdos_int_21h_4ah()
11499: {
1.1.1.14 root 11500: int mcb_seg = SREG(ES) - 1;
11501: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1 root 11502: int max_paragraphs;
11503:
1.1.1.14 root 11504: if(mcb->mz == 'M' || mcb->mz == 'Z') {
11505: if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
11506: REG16(AX) = 0x08;
11507: REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
11508: m_CF = 1;
11509: }
11510: } else {
1.1.1.33 root 11511: REG16(AX) = 0x09; // illegal memory block address
1.1.1.3 root 11512: m_CF = 1;
1.1 root 11513: }
11514: }
11515:
11516: inline void msdos_int_21h_4bh()
11517: {
1.1.1.3 root 11518: char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
11519: param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1 root 11520:
11521: switch(REG8(AL)) {
11522: case 0x00:
11523: case 0x01:
11524: if(msdos_process_exec(command, param, REG8(AL))) {
11525: REG16(AX) = 0x02;
1.1.1.3 root 11526: m_CF = 1;
1.1 root 11527: }
11528: break;
1.1.1.14 root 11529: case 0x03:
11530: {
11531: int fd;
11532: if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
11533: REG16(AX) = 0x02;
11534: m_CF = 1;
11535: break;
11536: }
11537: int size = _read(fd, file_buffer, sizeof(file_buffer));
11538: _close(fd);
11539:
11540: UINT16 *overlay = (UINT16 *)param;
11541:
11542: // check exe header
11543: exe_header_t *header = (exe_header_t *)file_buffer;
11544: int header_size = 0;
11545: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
11546: header_size = header->header_size * 16;
11547: // relocation
11548: int start_seg = overlay[1];
11549: for(int i = 0; i < header->relocations; i++) {
11550: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
11551: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
11552: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
11553: }
11554: }
11555: memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
11556: }
11557: break;
1.1.1.48 root 11558: case 0x04:
11559: // Load And Execute In Background (European MS-DOS 4.0 only)
11560: // case 0x05:
11561: // // DOS 5+ - Set Execution State
11562: case 0x80:
11563: // DR DOS v3.41 - Run Already-Loaded Kernel File
11564: case 0xf0:
11565: case 0xf1:
11566: // DIET v1.10+
1.1.1.43 root 11567: case 0xfd:
11568: case 0xfe:
11569: // unknown function called in FreeCOM
11570: REG16(AX) = 0x01;
11571: m_CF = 1;
11572: break;
1.1 root 11573: default:
1.1.1.22 root 11574: 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 11575: REG16(AX) = 0x01;
1.1.1.3 root 11576: m_CF = 1;
1.1 root 11577: break;
11578: }
11579: }
11580:
11581: inline void msdos_int_21h_4ch()
11582: {
11583: msdos_process_terminate(current_psp, REG8(AL), 1);
11584: }
11585:
11586: inline void msdos_int_21h_4dh()
11587: {
11588: REG16(AX) = retval;
11589: }
11590:
11591: inline void msdos_int_21h_4eh()
11592: {
11593: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 11594: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
11595: find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45 root 11596: const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 11597: WIN32_FIND_DATA fd;
11598:
1.1.1.14 root 11599: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
11600: find->find_magic = FIND_MAGIC;
11601: find->dta_index = dtainfo - dtalist;
1.1 root 11602: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 11603: dtainfo->allowable_mask = REG8(CL);
11604: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 11605:
1.1.1.14 root 11606: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
11607: dtainfo->allowable_mask &= ~8;
1.1 root 11608: }
1.1.1.14 root 11609: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
11610: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 11611: !msdos_find_file_has_8dot3name(&fd)) {
11612: if(!FindNextFile(dtainfo->find_handle, &fd)) {
11613: FindClose(dtainfo->find_handle);
11614: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11615: break;
11616: }
11617: }
11618: }
1.1.1.13 root 11619: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 11620: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
11621: msdos_find_file_conv_local_time(&fd);
11622: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
11623: find->size = fd.nFileSizeLow;
1.1.1.13 root 11624: strcpy(find->name, msdos_short_name(&fd));
1.1 root 11625: REG16(AX) = 0;
1.1.1.14 root 11626: } else if(dtainfo->allowable_mask & 8) {
1.1 root 11627: find->attrib = 8;
11628: find->size = 0;
11629: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 11630: dtainfo->allowable_mask &= ~8;
1.1 root 11631: REG16(AX) = 0;
11632: } else {
11633: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 11634: m_CF = 1;
1.1 root 11635: }
11636: }
11637:
11638: inline void msdos_int_21h_4fh()
11639: {
11640: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 11641: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
11642: find_t *find = (find_t *)(mem + dta_laddr);
1.1 root 11643: WIN32_FIND_DATA fd;
11644:
1.1.1.14 root 11645: if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
11646: REG16(AX) = 0x12;
11647: m_CF = 1;
11648: return;
11649: }
11650: dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13 root 11651: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
11652: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 11653: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 11654: !msdos_find_file_has_8dot3name(&fd)) {
11655: if(!FindNextFile(dtainfo->find_handle, &fd)) {
11656: FindClose(dtainfo->find_handle);
11657: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11658: break;
11659: }
11660: }
11661: } else {
1.1.1.13 root 11662: FindClose(dtainfo->find_handle);
11663: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11664: }
11665: }
1.1.1.13 root 11666: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 11667: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
11668: msdos_find_file_conv_local_time(&fd);
11669: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
11670: find->size = fd.nFileSizeLow;
1.1.1.13 root 11671: strcpy(find->name, msdos_short_name(&fd));
1.1 root 11672: REG16(AX) = 0;
1.1.1.14 root 11673: } else if(dtainfo->allowable_mask & 8) {
1.1 root 11674: find->attrib = 8;
11675: find->size = 0;
11676: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 11677: dtainfo->allowable_mask &= ~8;
1.1 root 11678: REG16(AX) = 0;
11679: } else {
11680: REG16(AX) = 0x12;
1.1.1.3 root 11681: m_CF = 1;
1.1 root 11682: }
11683: }
11684:
11685: inline void msdos_int_21h_50h()
11686: {
1.1.1.8 root 11687: if(current_psp != REG16(BX)) {
11688: process_t *process = msdos_process_info_get(current_psp);
11689: if(process != NULL) {
11690: process->psp = REG16(BX);
11691: }
11692: current_psp = REG16(BX);
1.1.1.23 root 11693: msdos_sda_update(current_psp);
1.1.1.8 root 11694: }
1.1 root 11695: }
11696:
11697: inline void msdos_int_21h_51h()
11698: {
11699: REG16(BX) = current_psp;
11700: }
11701:
11702: inline void msdos_int_21h_52h()
11703: {
1.1.1.25 root 11704: SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3 root 11705: i386_load_segment_descriptor(ES);
1.1.1.25 root 11706: REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1 root 11707: }
11708:
1.1.1.43 root 11709: inline void msdos_int_21h_53h()
11710: {
11711: dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
11712: bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
11713:
11714: dpb->bytes_per_sector = bpb->bytes_per_sector;
11715: dpb->highest_sector_num = bpb->sectors_per_track - 1;
11716: dpb->shift_count = 0;
11717: dpb->reserved_sectors = 0;
11718: dpb->fat_num = bpb->fat_num;
11719: dpb->root_entries = bpb->root_entries;
11720: dpb->first_data_sector = 0;
11721: if(bpb->sectors_per_cluster != 0) {
11722: dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
11723: } else {
11724: dpb->highest_cluster_num = 0;
11725: }
11726: dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
11727: dpb->first_dir_sector = 0;
11728: dpb->device_driver_header = 0;
11729: dpb->media_type = bpb->media_type;
11730: dpb->drive_accessed = 0;
11731: dpb->next_dpb_ofs = 0xffff;
11732: dpb->next_dpb_seg = 0xffff;
11733: dpb->first_free_cluster = 0;
11734: dpb->free_clusters = 0xffff;
11735: }
11736:
1.1 root 11737: inline void msdos_int_21h_54h()
11738: {
11739: process_t *process = msdos_process_info_get(current_psp);
11740:
11741: REG8(AL) = process->verify;
11742: }
11743:
11744: inline void msdos_int_21h_55h()
11745: {
11746: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
11747:
11748: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
11749: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
11750: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
11751: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
11752: psp->parent_psp = current_psp;
11753: }
11754:
11755: inline void msdos_int_21h_56h(int lfn)
11756: {
11757: char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3 root 11758: strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
11759: strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1 root 11760:
11761: if(rename(src, dst)) {
11762: REG16(AX) = errno;
1.1.1.3 root 11763: m_CF = 1;
1.1 root 11764: }
11765: }
11766:
11767: inline void msdos_int_21h_57h()
11768: {
11769: FILETIME time, local;
1.1.1.14 root 11770: FILETIME *ctime, *atime, *mtime;
1.1.1.21 root 11771: HANDLE hHandle;
1.1 root 11772:
1.1.1.21 root 11773: if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14 root 11774: REG16(AX) = (UINT16)GetLastError();
11775: m_CF = 1;
11776: return;
11777: }
11778: ctime = atime = mtime = NULL;
11779:
1.1 root 11780: switch(REG8(AL)) {
11781: case 0x00:
1.1.1.6 root 11782: case 0x01:
1.1.1.14 root 11783: mtime = &time;
1.1.1.6 root 11784: break;
11785: case 0x04:
11786: case 0x05:
1.1.1.14 root 11787: atime = &time;
1.1 root 11788: break;
1.1.1.6 root 11789: case 0x06:
11790: case 0x07:
1.1.1.14 root 11791: ctime = &time;
11792: break;
11793: default:
1.1.1.22 root 11794: 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 11795: REG16(AX) = 0x01;
11796: m_CF = 1;
11797: return;
11798: }
11799: if(REG8(AL) & 1) {
1.1 root 11800: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
11801: LocalFileTimeToFileTime(&local, &time);
1.1.1.21 root 11802: if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1 root 11803: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 11804: m_CF = 1;
1.1 root 11805: }
1.1.1.14 root 11806: } else {
1.1.1.21 root 11807: if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14 root 11808: // assume a device and use the current time
11809: GetSystemTimeAsFileTime(&time);
11810: }
11811: FileTimeToLocalFileTime(&time, &local);
11812: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
1.1 root 11813: }
11814: }
11815:
11816: inline void msdos_int_21h_58h()
11817: {
11818: switch(REG8(AL)) {
11819: case 0x00:
1.1.1.7 root 11820: REG16(AX) = malloc_strategy;
11821: break;
11822: case 0x01:
1.1.1.24 root 11823: // switch(REG16(BX)) {
11824: switch(REG8(BL)) {
1.1.1.7 root 11825: case 0x0000:
11826: case 0x0001:
11827: case 0x0002:
11828: case 0x0040:
11829: case 0x0041:
11830: case 0x0042:
11831: case 0x0080:
11832: case 0x0081:
11833: case 0x0082:
11834: malloc_strategy = REG16(BX);
1.1.1.23 root 11835: msdos_sda_update(current_psp);
1.1.1.7 root 11836: break;
11837: default:
1.1.1.22 root 11838: 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 11839: REG16(AX) = 0x01;
11840: m_CF = 1;
11841: break;
11842: }
11843: break;
11844: case 0x02:
1.1.1.19 root 11845: REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7 root 11846: break;
11847: case 0x03:
1.1.1.24 root 11848: // switch(REG16(BX)) {
11849: switch(REG8(BL)) {
1.1.1.7 root 11850: case 0x0000:
1.1.1.19 root 11851: msdos_mem_unlink_umb();
11852: break;
1.1.1.7 root 11853: case 0x0001:
1.1.1.19 root 11854: msdos_mem_link_umb();
1.1.1.7 root 11855: break;
11856: default:
1.1.1.22 root 11857: 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 11858: REG16(AX) = 0x01;
11859: m_CF = 1;
11860: break;
11861: }
1.1 root 11862: break;
11863: default:
1.1.1.22 root 11864: 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 11865: REG16(AX) = 0x01;
1.1.1.3 root 11866: m_CF = 1;
1.1 root 11867: break;
11868: }
11869: }
11870:
11871: inline void msdos_int_21h_59h()
11872: {
1.1.1.47 root 11873: if(REG16(BX) == 0x0000) {
11874: sda_t *sda = (sda_t *)(mem + SDA_TOP);
11875:
11876: REG16(AX) = sda->extended_error_code;
11877: REG8(BH) = sda->error_class;
11878: REG8(BL) = sda->suggested_action;
11879: REG8(CH) = sda->locus_of_last_error;
11880: // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
11881: if(sda->int21h_5d0ah_called != 0) {
11882: REG8(CL) = sda->int21h_5d0ah_cl;
11883: REG16(DX) = sda->int21h_5d0ah_dx;
11884: // REG16(SI) = sda->int21h_5d0ah_si;
11885: REG16(DI) = sda->last_error_pointer.w.l;
11886: // SREG(DS) = sda->int21h_5d0ah_ds;
11887: // i386_load_segment_descriptor(DS);
11888: SREG(ES) = sda->last_error_pointer.w.h;
11889: i386_load_segment_descriptor(ES);
11890: }
11891: sda->int21h_5d0ah_called = 0;
11892: // } else if(REG16(BX) == 0x0001) {
11893: // // European MS-DOS 4.0 - Get Hard Error Information
11894: } else {
11895: 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));
11896: REG16(AX) = 0x01;
11897: m_CF = 1;
11898: }
1.1 root 11899: }
11900:
11901: inline void msdos_int_21h_5ah()
11902: {
1.1.1.3 root 11903: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 11904: int len = strlen(path);
11905: char tmp[MAX_PATH];
11906:
11907: if(GetTempFileName(path, "TMP", 0, tmp)) {
11908: int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
11909:
11910: SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
11911: REG16(AX) = fd;
11912: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 11913: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11914:
11915: strcpy(path, tmp);
11916: int dx = REG16(DX) + len;
1.1.1.3 root 11917: int ds = SREG(DS);
1.1 root 11918: while(dx > 0xffff) {
11919: dx -= 0x10;
11920: ds++;
11921: }
11922: REG16(DX) = dx;
1.1.1.3 root 11923: SREG(DS) = ds;
11924: i386_load_segment_descriptor(DS);
1.1 root 11925: } else {
11926: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 11927: m_CF = 1;
1.1 root 11928: }
11929: }
11930:
11931: inline void msdos_int_21h_5bh()
11932: {
1.1.1.45 root 11933: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 11934:
1.1.1.45 root 11935: // if(msdos_is_existing_file(path)) {
11936: if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1 root 11937: // already exists
11938: REG16(AX) = 0x50;
1.1.1.3 root 11939: m_CF = 1;
1.1 root 11940: } else {
11941: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
11942:
11943: if(fd != -1) {
11944: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
11945: REG16(AX) = fd;
11946: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 11947: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11948: } else {
11949: REG16(AX) = errno;
1.1.1.3 root 11950: m_CF = 1;
1.1 root 11951: }
11952: }
11953: }
11954:
11955: inline void msdos_int_21h_5ch()
11956: {
11957: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11958: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11959:
1.1.1.20 root 11960: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 11961: if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35 root 11962: static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20 root 11963: UINT32 pos = _tell(fd);
11964: _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
11965: if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1 root 11966: REG16(AX) = errno;
1.1.1.3 root 11967: m_CF = 1;
1.1 root 11968: }
1.1.1.20 root 11969: _lseek(fd, pos, SEEK_SET);
1.1.1.26 root 11970:
1.1 root 11971: // some seconds may be passed in _locking()
1.1.1.35 root 11972: REQUEST_HARDWRE_UPDATE();
1.1 root 11973: } else {
11974: REG16(AX) = 0x01;
1.1.1.3 root 11975: m_CF = 1;
1.1 root 11976: }
11977: } else {
11978: REG16(AX) = 0x06;
1.1.1.3 root 11979: m_CF = 1;
1.1 root 11980: }
11981: }
11982:
1.1.1.22 root 11983: inline void msdos_int_21h_5dh()
11984: {
11985: switch(REG8(AL)) {
1.1.1.45 root 11986: case 0x00:
11987: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
11988: // current system
11989: static bool reenter = false;
11990: if(!reenter) {
11991: UINT32 offset = SREG_BASE(DS) + REG16(DX);
11992: REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
11993: REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
11994: REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
11995: REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
11996: REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
11997: REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
11998: SREG(DS) = *(UINT16 *)(mem + offset + 0x0c);
11999: SREG(ES) = *(UINT16 *)(mem + offset + 0x0e);
12000: i386_load_segment_descriptor(DS);
12001: i386_load_segment_descriptor(ES);
12002: reenter = true;
12003: try {
12004: msdos_syscall(0x21);
12005: } catch(...) {
12006: }
12007: reenter = false;
12008: }
12009: } else {
12010: REG16(AX) = 0x49; // network software not installed
12011: m_CF = 1;
12012: }
12013: break;
1.1.1.22 root 12014: case 0x06: // get address of dos swappable data area
1.1.1.23 root 12015: SREG(DS) = (SDA_TOP >> 4);
12016: i386_load_segment_descriptor(DS);
12017: REG16(SI) = offsetof(sda_t, crit_error_flag);
12018: REG16(CX) = 0x80;
12019: REG16(DX) = 0x1a;
12020: break;
1.1.1.45 root 12021: case 0x07: // get redirected printer mode
12022: case 0x08: // set redirected printer mode
12023: case 0x09: // flush redirected printer output
12024: REG16(AX) = 0x49; // network software not installed
12025: m_CF = 1;
12026: break;
1.1.1.43 root 12027: case 0x0a: // set extended error information
12028: {
12029: sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47 root 12030: sda->int21h_5d0ah_called = 1;
1.1.1.43 root 12031: sda->extended_error_code = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47 root 12032: // XXX: which one is correct ???
12033: #if 1
12034: // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43 root 12035: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
12036: sda->error_class = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47 root 12037: sda->int21h_5d0ah_cl = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43 root 12038: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47 root 12039: #else
12040: // PC DOS 7 Technical Update
12041: sda->error_class = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
12042: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
12043: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
12044: sda->int21h_5d0ah_cl = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
12045: #endif
12046: sda->int21h_5d0ah_dx = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
12047: // sda->int21h_5d0ah_si = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43 root 12048: sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47 root 12049: // sda->int21h_5d0ah_ds = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43 root 12050: sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
12051: }
12052: break;
1.1.1.23 root 12053: case 0x0b: // get dos swappable data areas
1.1.1.22 root 12054: REG16(AX) = 0x01;
12055: m_CF = 1;
12056: break;
12057: default:
12058: 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));
12059: REG16(AX) = 0x01;
12060: m_CF = 1;
12061: break;
12062: }
12063: }
12064:
1.1.1.42 root 12065: inline void msdos_int_21h_5eh()
12066: {
12067: switch(REG8(AL)) {
12068: case 0x00:
12069: {
12070: char name[256] = {0};
12071: DWORD dwSize = 256;
12072:
12073: if(GetComputerName(name, &dwSize)) {
12074: char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
12075: for(int i = 0; i < 15; i++) {
12076: dest[i] = (i < strlen(name)) ? name[i] : ' ';
12077: }
12078: dest[15] = '\0';
12079: REG8(CH) = 0x01; // nonzero valid
12080: REG8(CL) = 0x01; // NetBIOS number for machine name ???
12081: } else {
12082: REG16(AX) = 0x01;
12083: m_CF = 1;
12084: }
12085: }
12086: break;
12087: default:
1.1.1.45 root 12088: // 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));
12089: // REG16(AX) = 0x01;
12090: REG16(AX) = 0x49; // network software not installed
1.1.1.42 root 12091: m_CF = 1;
12092: break;
12093: }
12094: }
12095:
1.1.1.30 root 12096: inline void msdos_int_21h_5fh()
12097: {
12098: switch(REG8(AL)) {
1.1.1.42 root 12099: case 0x05:
1.1.1.44 root 12100: REG16(BP) = 0;
12101: for(int i = 0; i < 26; i++) {
12102: if(msdos_is_remote_drive(i)) {
12103: REG16(BP)++;
1.1.1.42 root 12104: }
12105: }
1.1.1.30 root 12106: case 0x02:
1.1.1.44 root 12107: for(int i = 0, index = 0; i < 26; i++) {
12108: if(msdos_is_remote_drive(i)) {
12109: if(index == REG16(BX)) {
12110: char volume[] = "A:";
1.1.1.30 root 12111: volume[0] = 'A' + i;
1.1.1.44 root 12112: DWORD dwSize = 128;
12113: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
12114: WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
12115: REG8(BH) = 0x00; // valid
12116: REG8(BL) = 0x04; // disk drive
12117: REG16(CX) = 0x00; // LANtastic
12118: return;
1.1.1.30 root 12119: }
1.1.1.44 root 12120: index++;
1.1.1.30 root 12121: }
12122: }
12123: REG16(AX) = 0x12; // no more files
12124: m_CF = 1;
12125: break;
1.1.1.44 root 12126: case 0x07:
12127: if(msdos_is_valid_drive(REG8(DL))) {
12128: msdos_cds_update(REG8(DL));
12129: } else {
12130: REG16(AX) = 0x0f; // invalid drive
12131: m_CF = 1;
12132: }
12133: break;
12134: case 0x08:
12135: if(msdos_is_valid_drive(REG8(DL))) {
12136: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
12137: cds->drive_attrib = 0x0000;
12138: } else {
12139: REG16(AX) = 0x0f; // invalid drive
12140: m_CF = 1;
12141: }
12142: break;
1.1.1.30 root 12143: default:
1.1.1.45 root 12144: // 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));
12145: // REG16(AX) = 0x01;
12146: REG16(AX) = 0x49; // network software not installed
1.1.1.30 root 12147: m_CF = 1;
12148: break;
12149: }
12150: }
12151:
1.1 root 12152: inline void msdos_int_21h_60h(int lfn)
12153: {
1.1.1.45 root 12154: char full[MAX_PATH];
12155: const char *path = NULL;
1.1.1.14 root 12156:
1.1 root 12157: if(lfn) {
1.1.1.14 root 12158: char *name;
12159: *full = '\0';
1.1.1.3 root 12160: GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14 root 12161: switch(REG8(CL)) {
12162: case 1:
12163: GetShortPathName(full, full, MAX_PATH);
12164: my_strupr(full);
12165: break;
12166: case 2:
12167: GetLongPathName(full, full, MAX_PATH);
12168: break;
12169: }
12170: path = full;
12171: } else {
12172: path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
12173: }
12174: if(*path != '\0') {
12175: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1 root 12176: } else {
1.1.1.14 root 12177: REG16(AX) = (UINT16)GetLastError();
12178: m_CF = 1;
1.1 root 12179: }
12180: }
12181:
12182: inline void msdos_int_21h_61h()
12183: {
12184: REG8(AL) = 0;
12185: }
12186:
12187: inline void msdos_int_21h_62h()
12188: {
12189: REG16(BX) = current_psp;
12190: }
12191:
12192: inline void msdos_int_21h_63h()
12193: {
12194: switch(REG8(AL)) {
12195: case 0x00:
1.1.1.3 root 12196: SREG(DS) = (DBCS_TABLE >> 4);
12197: i386_load_segment_descriptor(DS);
1.1 root 12198: REG16(SI) = (DBCS_TABLE & 0x0f);
12199: REG8(AL) = 0x00;
12200: break;
1.1.1.22 root 12201: case 0x01: // set korean input mode
12202: case 0x02: // get korean input mode
12203: REG8(AL) = 0xff; // not supported
12204: break;
1.1 root 12205: default:
1.1.1.22 root 12206: 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 12207: REG16(AX) = 0x01;
1.1.1.3 root 12208: m_CF = 1;
1.1 root 12209: break;
12210: }
12211: }
12212:
1.1.1.25 root 12213: UINT16 get_extended_country_info(UINT8 func)
1.1 root 12214: {
1.1.1.25 root 12215: switch(func) {
1.1.1.17 root 12216: case 0x01:
12217: if(REG16(CX) >= 5) {
1.1.1.19 root 12218: UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17 root 12219: if(REG16(CX) > sizeof(data)) // cx = actual transfer size
12220: REG16(CX) = sizeof(data);
12221: ZeroMemory(data, sizeof(data));
12222: data[0] = 0x01;
12223: *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19 root 12224: *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17 root 12225: *(UINT16 *)(data + 5) = active_code_page;
12226: memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25 root 12227: // REG16(AX) = active_code_page;
1.1.1.17 root 12228: } else {
1.1.1.25 root 12229: return(0x08); // insufficient memory
1.1.1.17 root 12230: }
12231: break;
12232: case 0x02:
12233: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
12234: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
12235: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25 root 12236: // REG16(AX) = active_code_page;
1.1.1.17 root 12237: REG16(CX) = 0x05;
12238: break;
1.1.1.23 root 12239: case 0x03:
12240: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
12241: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
12242: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25 root 12243: // REG16(AX) = active_code_page;
1.1.1.23 root 12244: REG16(CX) = 0x05;
12245: break;
1.1.1.17 root 12246: case 0x04:
12247: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
12248: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
12249: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25 root 12250: // REG16(AX) = active_code_page;
1.1.1.17 root 12251: REG16(CX) = 0x05;
12252: break;
12253: case 0x05:
12254: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
12255: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
12256: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25 root 12257: // REG16(AX) = active_code_page;
1.1.1.17 root 12258: REG16(CX) = 0x05;
12259: break;
12260: case 0x06:
12261: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
12262: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
12263: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25 root 12264: // REG16(AX) = active_code_page;
1.1.1.17 root 12265: REG16(CX) = 0x05;
12266: break;
1.1 root 12267: case 0x07:
1.1.1.3 root 12268: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
12269: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
12270: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25 root 12271: // REG16(AX) = active_code_page;
1.1 root 12272: REG16(CX) = 0x05;
12273: break;
1.1.1.25 root 12274: default:
12275: return(0x01); // function number invalid
12276: }
12277: return(0x00);
12278: }
12279:
12280: inline void msdos_int_21h_65h()
12281: {
12282: char tmp[0x10000];
12283:
12284: switch(REG8(AL)) {
1.1.1.43 root 12285: case 0x00:
12286: if(REG16(CX) >= 7) {
12287: set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
12288: REG16(AX) = system_code_page;
12289: } else {
12290: REG16(AX) = 0x0c;
12291: m_CF = 1;
12292: }
12293: break;
1.1.1.25 root 12294: case 0x01:
12295: case 0x02:
12296: case 0x03:
12297: case 0x04:
12298: case 0x05:
12299: case 0x06:
12300: case 0x07:
12301: {
12302: UINT16 result = get_extended_country_info(REG8(AL));
12303: if(result) {
12304: REG16(AX) = result;
12305: m_CF = 1;
12306: } else {
12307: REG16(AX) = active_code_page; // FIXME: is this correct???
12308: }
12309: }
12310: break;
1.1 root 12311: case 0x20:
1.1.1.25 root 12312: case 0xa0:
1.1.1.19 root 12313: memset(tmp, 0, sizeof(tmp));
12314: tmp[0] = REG8(DL);
1.1 root 12315: my_strupr(tmp);
12316: REG8(DL) = tmp[0];
12317: break;
12318: case 0x21:
1.1.1.25 root 12319: case 0xa1:
1.1 root 12320: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 12321: memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 12322: my_strupr(tmp);
1.1.1.3 root 12323: memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1 root 12324: break;
12325: case 0x22:
1.1.1.25 root 12326: case 0xa2:
1.1.1.3 root 12327: my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1 root 12328: break;
1.1.1.25 root 12329: case 0x23:
12330: // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45 root 12331: if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25 root 12332: // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45 root 12333: REG16(AX) = 0x00;
12334: } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
12335: // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25 root 12336: REG16(AX) = 0x01;
12337: } else {
12338: REG16(AX) = 0x02;
12339: }
12340: break;
1.1 root 12341: default:
1.1.1.22 root 12342: 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 12343: REG16(AX) = 0x01;
1.1.1.3 root 12344: m_CF = 1;
1.1 root 12345: break;
12346: }
12347: }
12348:
12349: inline void msdos_int_21h_66h()
12350: {
12351: switch(REG8(AL)) {
12352: case 0x01:
12353: REG16(BX) = active_code_page;
12354: REG16(DX) = system_code_page;
12355: break;
12356: case 0x02:
12357: if(active_code_page == REG16(BX)) {
12358: REG16(AX) = 0xeb41;
12359: } else if(_setmbcp(REG16(BX)) == 0) {
12360: active_code_page = REG16(BX);
1.1.1.17 root 12361: msdos_nls_tables_update();
1.1 root 12362: REG16(AX) = 0xeb41;
1.1.1.32 root 12363: SetConsoleCP(active_code_page);
12364: SetConsoleOutputCP(active_code_page);
1.1 root 12365: } else {
12366: REG16(AX) = 0x25;
1.1.1.3 root 12367: m_CF = 1;
1.1 root 12368: }
12369: break;
12370: default:
1.1.1.22 root 12371: 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 12372: REG16(AX) = 0x01;
1.1.1.3 root 12373: m_CF = 1;
1.1 root 12374: break;
12375: }
12376: }
12377:
12378: inline void msdos_int_21h_67h()
12379: {
12380: process_t *process = msdos_process_info_get(current_psp);
12381:
12382: if(REG16(BX) <= MAX_FILES) {
12383: process->max_files = max(REG16(BX), 20);
12384: } else {
12385: REG16(AX) = 0x08;
1.1.1.3 root 12386: m_CF = 1;
1.1 root 12387: }
12388: }
12389:
12390: inline void msdos_int_21h_68h()
12391: {
12392: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 12393: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 12394:
1.1.1.20 root 12395: if(fd < process->max_files && file_handler[fd].valid) {
12396: // fflush(_fdopen(fd, ""));
1.1 root 12397: } else {
12398: REG16(AX) = 0x06;
1.1.1.3 root 12399: m_CF = 1;
1.1 root 12400: }
12401: }
12402:
12403: inline void msdos_int_21h_69h()
12404: {
1.1.1.3 root 12405: drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12406: char path[] = "A:\\";
12407: char volume_label[MAX_PATH];
12408: DWORD serial_number = 0;
12409: char file_system[MAX_PATH];
12410:
12411: if(REG8(BL) == 0) {
12412: path[0] = 'A' + _getdrive() - 1;
12413: } else {
12414: path[0] = 'A' + REG8(BL) - 1;
12415: }
12416:
12417: switch(REG8(AL)) {
12418: case 0x00:
12419: if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
12420: info->info_level = 0;
12421: info->serial_number = serial_number;
12422: memset(info->volume_label, 0x20, 11);
12423: memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
12424: memset(info->file_system, 0x20, 8);
12425: memcpy(info->file_system, file_system, min(strlen(file_system), 8));
12426: } else {
12427: REG16(AX) = errno;
1.1.1.3 root 12428: m_CF = 1;
1.1 root 12429: }
12430: break;
12431: case 0x01:
12432: REG16(AX) = 0x03;
1.1.1.3 root 12433: m_CF = 1;
1.1.1.45 root 12434: break;
12435: default:
12436: 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));
12437: REG16(AX) = 0x01;
12438: m_CF = 1;
12439: break;
1.1 root 12440: }
12441: }
12442:
12443: inline void msdos_int_21h_6ah()
12444: {
12445: REG8(AH) = 0x68;
12446: msdos_int_21h_68h();
12447: }
12448:
12449: inline void msdos_int_21h_6bh()
12450: {
1.1.1.45 root 12451: REG8(AL) = 0x00;
1.1 root 12452: }
12453:
12454: inline void msdos_int_21h_6ch(int lfn)
12455: {
1.1.1.45 root 12456: const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1 root 12457: int mode = REG8(BL) & 0x03;
12458:
12459: if(mode < 0x03) {
1.1.1.29 root 12460: if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1 root 12461: // file exists
12462: if(REG8(DL) & 1) {
1.1.1.37 root 12463: int fd = -1;
12464: int sio_port = 0;
12465: int lpt_port = 0;
1.1 root 12466:
1.1.1.45 root 12467: if(msdos_is_device_path(path)) {
12468: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11 root 12469: } else {
1.1.1.13 root 12470: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 12471: }
1.1 root 12472: if(fd != -1) {
12473: REG16(AX) = fd;
12474: REG16(CX) = 1;
1.1.1.45 root 12475: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 12476: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 12477: } else {
12478: REG16(AX) = errno;
1.1.1.3 root 12479: m_CF = 1;
1.1 root 12480: }
12481: } else if(REG8(DL) & 2) {
12482: int attr = GetFileAttributes(path);
1.1.1.37 root 12483: int fd = -1;
12484: int sio_port = 0;
12485: int lpt_port = 0;
1.1 root 12486:
1.1.1.45 root 12487: if(msdos_is_device_path(path)) {
12488: fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1 root 12489: } else {
12490: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
12491: }
12492: if(fd != -1) {
12493: if(attr == -1) {
12494: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
12495: }
12496: SetFileAttributes(path, attr);
12497: REG16(AX) = fd;
12498: REG16(CX) = 3;
1.1.1.45 root 12499: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20 root 12500: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 12501: } else {
12502: REG16(AX) = errno;
1.1.1.3 root 12503: m_CF = 1;
1.1 root 12504: }
12505: } else {
12506: REG16(AX) = 0x50;
1.1.1.3 root 12507: m_CF = 1;
1.1 root 12508: }
12509: } else {
12510: // file not exists
12511: if(REG8(DL) & 0x10) {
12512: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
12513:
12514: if(fd != -1) {
12515: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
12516: REG16(AX) = fd;
12517: REG16(CX) = 2;
12518: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 12519: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 12520: } else {
12521: REG16(AX) = errno;
1.1.1.3 root 12522: m_CF = 1;
1.1 root 12523: }
12524: } else {
12525: REG16(AX) = 0x02;
1.1.1.3 root 12526: m_CF = 1;
1.1 root 12527: }
12528: }
12529: } else {
12530: REG16(AX) = 0x0c;
1.1.1.3 root 12531: m_CF = 1;
1.1 root 12532: }
12533: }
12534:
1.1.1.43 root 12535: inline void msdos_int_21h_70h()
12536: {
12537: switch(REG8(AL)) {
1.1.1.48 root 12538: case 0x00: // get ??? info
12539: case 0x01: // set above info
12540: // 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));
12541: REG16(AX) = 0x7000;
12542: m_CF = 1;
12543: break;
12544: case 0x02: // set general internationalization info
1.1.1.43 root 12545: if(REG16(CX) >= 7) {
12546: active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
12547: msdos_nls_tables_update();
12548: set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
12549: REG16(AX) = system_code_page;
12550: } else {
12551: REG16(AX) = 0x0c;
12552: m_CF = 1;
12553: }
12554: break;
12555: default:
12556: 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 12557: REG16(AX) = 0x7000;
1.1.1.43 root 12558: m_CF = 1;
12559: break;
12560: }
12561: }
12562:
1.1 root 12563: inline void msdos_int_21h_710dh()
12564: {
12565: // reset drive
12566: }
12567:
1.1.1.48 root 12568: inline void msdos_int_21h_7141h()
1.1.1.17 root 12569: {
12570: if(REG16(SI) == 0) {
1.1.1.48 root 12571: msdos_int_21h_41h(1);
1.1.1.17 root 12572: return;
12573: }
12574: if(REG16(SI) != 1) {
12575: REG16(AX) = 5;
12576: m_CF = 1;
12577: }
12578: /* wild card and matching attributes... */
12579: char tmp[MAX_PATH * 2];
12580: // copy search pathname (and quick check overrun)
12581: ZeroMemory(tmp, sizeof(tmp));
12582: tmp[MAX_PATH - 1] = '\0';
12583: tmp[MAX_PATH] = 1;
12584: strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
12585:
12586: if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
12587: REG16(AX) = 1;
12588: m_CF = 1;
12589: return;
12590: }
12591: for(char *s = tmp; *s; ++s) {
12592: if(*s == '/') {
12593: *s = '\\';
12594: }
12595: }
12596: char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
12597: if(tmp_name) {
12598: ++tmp_name;
12599: } else {
12600: tmp_name = strchr(tmp, ':');
12601: tmp_name = tmp_name ? tmp_name + 1 : tmp;
12602: }
12603:
12604: WIN32_FIND_DATAA fd;
12605: HANDLE fh = FindFirstFileA(tmp, &fd);
12606: if(fh == INVALID_HANDLE_VALUE) {
12607: REG16(AX) = 2;
12608: m_CF = 1;
12609: return;
12610: }
12611: do {
12612: if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
12613: strcpy(tmp_name, fd.cFileName);
1.1.1.48 root 12614: if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17 root 12615: REG16(AX) = 5;
12616: m_CF = 1;
12617: break;
12618: }
12619: }
12620: } while(FindNextFileA(fh, &fd));
12621: if(!m_CF) {
12622: if(GetLastError() != ERROR_NO_MORE_FILES) {
12623: m_CF = 1;
12624: REG16(AX) = 2;
12625: }
12626: }
12627: FindClose(fh);
12628: }
12629:
1.1 root 12630: inline void msdos_int_21h_714eh()
12631: {
12632: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 12633: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
12634: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12635: WIN32_FIND_DATA fd;
12636:
1.1.1.13 root 12637: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
12638: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
12639: FindClose(dtainfo->find_handle);
12640: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12641: }
12642: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 12643: dtainfo->allowable_mask = REG8(CL);
12644: dtainfo->required_mask = REG8(CH);
12645: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 12646:
1.1.1.14 root 12647: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
12648: dtainfo->allowable_mask &= ~8;
1.1 root 12649: }
1.1.1.14 root 12650: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
12651: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13 root 12652: if(!FindNextFile(dtainfo->find_handle, &fd)) {
12653: FindClose(dtainfo->find_handle);
12654: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12655: break;
12656: }
12657: }
12658: }
1.1.1.13 root 12659: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 12660: find->attrib = fd.dwFileAttributes;
12661: msdos_find_file_conv_local_time(&fd);
12662: if(REG16(SI) == 0) {
12663: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
12664: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
12665: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
12666: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
12667: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
12668: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
12669: } else {
12670: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
12671: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
12672: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
12673: }
12674: find->size_hi = fd.nFileSizeHigh;
12675: find->size_lo = fd.nFileSizeLow;
12676: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 12677: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 12678: REG16(AX) = dtainfo - dtalist + 1;
12679: } else if(dtainfo->allowable_mask & 8) {
1.1 root 12680: // volume label
12681: find->attrib = 8;
12682: find->size_hi = find->size_lo = 0;
12683: strcpy(find->full_name, process->volume_label);
12684: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 12685: dtainfo->allowable_mask &= ~8;
12686: REG16(AX) = dtainfo - dtalist + 1;
1.1 root 12687: } else {
12688: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 12689: m_CF = 1;
1.1 root 12690: }
12691: }
12692:
12693: inline void msdos_int_21h_714fh()
12694: {
12695: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 12696: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 12697: WIN32_FIND_DATA fd;
12698:
1.1.1.14 root 12699: if(REG16(BX) - 1u >= MAX_DTAINFO) {
12700: REG16(AX) = 6;
1.1.1.13 root 12701: m_CF = 1;
12702: return;
12703: }
1.1.1.14 root 12704: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 12705: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
12706: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 12707: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13 root 12708: if(!FindNextFile(dtainfo->find_handle, &fd)) {
12709: FindClose(dtainfo->find_handle);
12710: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12711: break;
12712: }
12713: }
12714: } else {
1.1.1.13 root 12715: FindClose(dtainfo->find_handle);
12716: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12717: }
12718: }
1.1.1.13 root 12719: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 12720: find->attrib = fd.dwFileAttributes;
12721: msdos_find_file_conv_local_time(&fd);
12722: if(REG16(SI) == 0) {
12723: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
12724: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
12725: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
12726: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
12727: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
12728: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
12729: } else {
12730: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
12731: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
12732: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
12733: }
12734: find->size_hi = fd.nFileSizeHigh;
12735: find->size_lo = fd.nFileSizeLow;
12736: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 12737: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 12738: } else if(dtainfo->allowable_mask & 8) {
1.1 root 12739: // volume label
12740: find->attrib = 8;
12741: find->size_hi = find->size_lo = 0;
12742: strcpy(find->full_name, process->volume_label);
12743: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 12744: dtainfo->allowable_mask &= ~8;
1.1 root 12745: } else {
12746: REG16(AX) = 0x12;
1.1.1.3 root 12747: m_CF = 1;
1.1 root 12748: }
12749: }
12750:
12751: inline void msdos_int_21h_71a0h()
12752: {
12753: DWORD max_component_len, file_sys_flag;
12754:
1.1.1.14 root 12755: if(GetVolumeInformation((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, REG16(CX) == 0 ? NULL : (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
12756: REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
12757: REG16(BX) |= 0x4000; // supports LFN functions
1.1 root 12758: REG16(CX) = (UINT16)max_component_len; // 255
12759: REG16(DX) = (UINT16)max_component_len + 5; // 260
12760: } else {
12761: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12762: m_CF = 1;
1.1 root 12763: }
12764: }
12765:
12766: inline void msdos_int_21h_71a1h()
12767: {
1.1.1.14 root 12768: if(REG16(BX) - 1u >= MAX_DTAINFO) {
12769: REG16(AX) = 6;
1.1.1.13 root 12770: m_CF = 1;
12771: return;
12772: }
1.1.1.14 root 12773: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 12774: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
12775: FindClose(dtainfo->find_handle);
12776: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 12777: }
12778: }
12779:
12780: inline void msdos_int_21h_71a6h()
12781: {
12782: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 12783: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
12784:
1.1.1.3 root 12785: UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12786: struct _stat64 status;
12787: DWORD serial_number = 0;
12788:
1.1.1.20 root 12789: if(fd < process->max_files && file_handler[fd].valid) {
12790: if(_fstat64(fd, &status) == 0) {
12791: if(file_handler[fd].path[1] == ':') {
1.1 root 12792: // NOTE: we need to consider the network file path "\\host\share\"
12793: char volume[] = "A:\\";
1.1.1.20 root 12794: volume[0] = file_handler[fd].path[1];
1.1 root 12795: GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
12796: }
1.1.1.20 root 12797: *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1 root 12798: *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
12799: *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
12800: *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
12801: *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
12802: *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
12803: *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
12804: *(UINT32 *)(buffer + 0x1c) = serial_number;
12805: *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
12806: *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
12807: *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14 root 12808: // this is dummy id and it will be changed when it is reopened...
1.1 root 12809: *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20 root 12810: *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1 root 12811: } else {
12812: REG16(AX) = errno;
1.1.1.3 root 12813: m_CF = 1;
1.1 root 12814: }
12815: } else {
12816: REG16(AX) = 0x06;
1.1.1.3 root 12817: m_CF = 1;
1.1 root 12818: }
12819: }
12820:
12821: inline void msdos_int_21h_71a7h()
12822: {
12823: switch(REG8(BL)) {
12824: case 0x00:
1.1.1.3 root 12825: if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), ®16(DX), ®16(CX))) {
1.1 root 12826: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12827: m_CF = 1;
1.1 root 12828: }
12829: break;
12830: case 0x01:
12831: // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3 root 12832: if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1 root 12833: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12834: m_CF = 1;
1.1 root 12835: }
12836: break;
12837: default:
1.1.1.22 root 12838: 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 12839: REG16(AX) = 0x7100;
1.1.1.3 root 12840: m_CF = 1;
1.1 root 12841: break;
12842: }
12843: }
12844:
12845: inline void msdos_int_21h_71a8h()
12846: {
12847: if(REG8(DH) == 0) {
12848: char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3 root 12849: strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 12850: memset(fcb, 0x20, sizeof(fcb));
12851: int len = strlen(tmp);
1.1.1.21 root 12852: for(int i = 0, pos = 0; i < len; i++) {
1.1 root 12853: if(tmp[i] == '.') {
12854: pos = 8;
12855: } else {
12856: if(msdos_lead_byte_check(tmp[i])) {
12857: fcb[pos++] = tmp[i++];
12858: }
12859: fcb[pos++] = tmp[i];
12860: }
12861: }
1.1.1.3 root 12862: memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1 root 12863: } else {
1.1.1.3 root 12864: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 12865: }
12866: }
12867:
1.1.1.22 root 12868: inline void msdos_int_21h_71aah()
12869: {
12870: char drv[] = "A:", path[MAX_PATH];
12871: char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
12872:
12873: if(REG8(BL) == 0) {
12874: drv[0] = 'A' + _getdrive() - 1;
12875: } else {
12876: drv[0] = 'A' + REG8(BL) - 1;
12877: }
12878: switch(REG8(BH)) {
12879: case 0x00:
1.1.1.44 root 12880: if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
12881: REG16(AX) = 0x0f; // invalid drive
12882: m_CF = 1;
12883: } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
12884: REG16(AX) = 0x03; // path not found
1.1.1.22 root 12885: m_CF = 1;
12886: }
12887: break;
12888: case 0x01:
1.1.1.44 root 12889: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
12890: REG16(AX) = 0x0f; // invalid drive
12891: m_CF = 1;
12892: } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22 root 12893: REG16(AX) = 0x0f; // invalid drive
12894: m_CF = 1;
12895: }
12896: break;
12897: case 0x02:
1.1.1.44 root 12898: if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
12899: REG16(AX) = 0x0f; // invalid drive
12900: m_CF = 1;
12901: } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22 root 12902: REG16(AX) = 0x0f; // invalid drive
12903: m_CF = 1;
12904: } else if(strncmp(path, "\\??\\", 4) != 0) {
12905: REG16(AX) = 0x0f; // invalid drive
12906: m_CF = 1;
12907: } else {
12908: strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
12909: }
12910: break;
12911: default:
12912: 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 12913: REG16(AX) = 0x7100;
1.1.1.22 root 12914: m_CF = 1;
12915: break;
12916: }
12917: }
12918:
1.1.1.14 root 12919: inline void msdos_int_21h_7300h()
12920: {
1.1.1.44 root 12921: REG8(AL) = REG8(CL);
12922: REG8(AH) = 0;
1.1.1.14 root 12923: }
12924:
12925: inline void msdos_int_21h_7302h()
12926: {
12927: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
12928: UINT16 seg, ofs;
12929:
12930: if(REG16(CX) < 0x3f) {
12931: REG8(AL) = 0x18;
12932: m_CF = 1;
12933: } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
12934: REG8(AL) = 0xff;
12935: m_CF = 1;
12936: } else {
12937: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
12938: }
12939: }
12940:
1.1 root 12941: inline void msdos_int_21h_7303h()
12942: {
1.1.1.3 root 12943: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
12944: ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 12945: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
12946:
12947: if(GetDiskFreeSpace(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
12948: info->size_of_structure = sizeof(ext_space_info_t);
12949: info->structure_version = 0;
12950: info->sectors_per_cluster = sectors_per_cluster;
12951: info->bytes_per_sector = bytes_per_sector;
12952: info->available_clusters_on_drive = free_clusters;
12953: info->total_clusters_on_drive = total_clusters;
12954: info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
12955: info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
12956: info->available_allocation_units = free_clusters; // ???
12957: info->total_allocation_units = total_clusters; // ???
12958: } else {
12959: REG16(AX) = errno;
1.1.1.3 root 12960: m_CF = 1;
1.1 root 12961: }
12962: }
12963:
1.1.1.30 root 12964: inline void msdos_int_21h_dbh()
12965: {
12966: // Novell NetWare - Workstation - Get Number of Local Drives
12967: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
12968: REG8(AL) = dos_info->last_drive;
12969: }
12970:
12971: inline void msdos_int_21h_dch()
12972: {
12973: // Novell NetWare - Connection Services - Get Connection Number
12974: REG8(AL) = 0x00;
12975: }
12976:
1.1.1.32 root 12977: inline void msdos_int_24h()
12978: {
12979: const char *message = NULL;
12980: int key = 0;
12981:
12982: for(int i = 0; i < array_length(critical_error_table); i++) {
12983: if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
12984: if(active_code_page == 932) {
12985: message = critical_error_table[i].message_japanese;
12986: }
12987: if(message == NULL) {
12988: message = critical_error_table[i].message_english;
12989: }
12990: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
12991: strcpy((char *)(mem + WORK_TOP + 1), message);
12992:
12993: SREG(ES) = WORK_TOP >> 4;
12994: i386_load_segment_descriptor(ES);
12995: REG16(DI) = 0x0000;
12996: break;
12997: }
12998: }
12999: fprintf(stderr, "\n%s", message);
13000: if(!(REG8(AH) & 0x80)) {
13001: if(REG8(AH) & 0x01) {
13002: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
13003: } else {
13004: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
13005: }
13006: }
13007: fprintf(stderr, "\n");
13008:
1.1.1.33 root 13009: {
1.1.1.32 root 13010: fprintf(stderr, "%s", (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33 root 13011: }
1.1.1.32 root 13012: if(REG8(AH) & 0x10) {
13013: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (R)" : "Retry");
13014: }
13015: if(REG8(AH) & 0x20) {
13016: fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
13017: }
13018: if(REG8(AH) & 0x08) {
13019: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
13020: }
13021: fprintf(stderr, "? ");
13022:
13023: while(1) {
13024: while(!_kbhit()) {
13025: Sleep(10);
13026: }
13027: key = _getch();
13028:
13029: if(key == 'I' || key == 'i') {
13030: if(REG8(AH) & 0x20) {
13031: REG8(AL) = 0;
13032: break;
13033: }
13034: } else if(key == 'R' || key == 'r') {
13035: if(REG8(AH) & 0x10) {
13036: REG8(AL) = 1;
13037: break;
13038: }
13039: } else if(key == 'A' || key == 'a') {
13040: REG8(AL) = 2;
13041: break;
13042: } else if(key == 'F' || key == 'f') {
13043: if(REG8(AH) & 0x08) {
13044: REG8(AL) = 3;
13045: break;
13046: }
13047: }
13048: }
13049: fprintf(stderr, "%c\n", key);
13050: }
13051:
1.1 root 13052: inline void msdos_int_25h()
13053: {
13054: UINT16 seg, ofs;
13055: DWORD dwSize;
13056:
1.1.1.3 root 13057: #if defined(HAS_I386)
13058: I386OP(pushf)();
13059: #else
13060: PREFIX86(_pushf());
13061: #endif
1.1 root 13062:
13063: if(!(REG8(AL) < 26)) {
13064: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 13065: m_CF = 1;
1.1 root 13066: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
13067: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13068: m_CF = 1;
1.1 root 13069: } else {
13070: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
13071: char dev[64];
13072: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
13073:
13074: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
13075: if(hFile == INVALID_HANDLE_VALUE) {
13076: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13077: m_CF = 1;
1.1 root 13078: } else {
1.1.1.19 root 13079: UINT32 top_sector = REG16(DX);
13080: UINT16 sector_num = REG16(CX);
13081: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
13082:
13083: if(sector_num == 0xffff) {
13084: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
13085: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
13086: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
13087: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
13088: buffer_addr = (seg << 4) + ofs;
13089: }
13090: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
13091: // REG8(AL) = 0x02; // drive not ready
13092: // m_CF = 1;
13093: // } else
13094: if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 13095: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 13096: m_CF = 1;
1.1.1.19 root 13097: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 13098: REG8(AL) = 0x0b; // read error
1.1.1.3 root 13099: m_CF = 1;
1.1 root 13100: }
13101: CloseHandle(hFile);
13102: }
13103: }
13104: }
13105:
13106: inline void msdos_int_26h()
13107: {
1.1.1.42 root 13108: // this operation may cause serious damage for drives, so support only floppy disk...
1.1 root 13109: UINT16 seg, ofs;
13110: DWORD dwSize;
13111:
1.1.1.3 root 13112: #if defined(HAS_I386)
13113: I386OP(pushf)();
13114: #else
13115: PREFIX86(_pushf());
13116: #endif
1.1 root 13117:
13118: if(!(REG8(AL) < 26)) {
13119: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 13120: m_CF = 1;
1.1 root 13121: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
13122: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13123: m_CF = 1;
1.1 root 13124: } else {
13125: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
13126: char dev[64];
13127: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
13128:
13129: if(dpb->media_type == 0xf8) {
13130: // this drive is not a floppy
1.1.1.6 root 13131: // if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
13132: // fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
13133: // }
1.1 root 13134: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13135: m_CF = 1;
1.1 root 13136: } else {
13137: HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
13138: if(hFile == INVALID_HANDLE_VALUE) {
13139: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13140: m_CF = 1;
1.1 root 13141: } else {
1.1.1.19 root 13142: UINT32 top_sector = REG16(DX);
13143: UINT16 sector_num = REG16(CX);
13144: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
13145:
13146: if(sector_num == 0xffff) {
13147: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
13148: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
13149: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
13150: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
13151: buffer_addr = (seg << 4) + ofs;
13152: }
1.1 root 13153: if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
13154: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 13155: m_CF = 1;
1.1.1.19 root 13156: } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 13157: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 13158: m_CF = 1;
1.1.1.19 root 13159: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 13160: REG8(AL) = 0x0a; // write error
1.1.1.3 root 13161: m_CF = 1;
1.1 root 13162: }
13163: CloseHandle(hFile);
13164: }
13165: }
13166: }
13167: }
13168:
13169: inline void msdos_int_27h()
13170: {
1.1.1.29 root 13171: int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
13172: try {
13173: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
13174: } catch(...) {
13175: // recover the broken mcb
13176: int mcb_seg = SREG(CS) - 1;
13177: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 13178:
1.1.1.29 root 13179: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 13180: mcb->mz = 'M';
13181: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
13182:
1.1.1.29 root 13183: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 13184: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 13185: } else {
1.1.1.39 root 13186: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 13187: }
13188: } else {
13189: mcb->mz = 'Z';
1.1.1.30 root 13190: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 13191: }
13192: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
13193: }
1.1.1.3 root 13194: msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1 root 13195: }
13196:
13197: inline void msdos_int_29h()
13198: {
1.1.1.50 root 13199: msdos_putch_fast(REG8(AL));
1.1 root 13200: }
13201:
13202: inline void msdos_int_2eh()
13203: {
13204: char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
13205: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 13206: strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1 root 13207: char *token = my_strtok(tmp, " ");
13208: strcpy(command, token);
13209: strcpy(opt, token + strlen(token) + 1);
13210:
13211: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
13212: param->env_seg = 0;
13213: param->cmd_line.w.l = 44;
13214: param->cmd_line.w.h = (WORK_TOP >> 4);
13215: param->fcb1.w.l = 24;
13216: param->fcb1.w.h = (WORK_TOP >> 4);
13217: param->fcb2.w.l = 24;
13218: param->fcb2.w.h = (WORK_TOP >> 4);
13219:
13220: memset(mem + WORK_TOP + 24, 0x20, 20);
13221:
13222: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
13223: cmd_line->len = strlen(opt);
13224: strcpy(cmd_line->cmd, opt);
13225: cmd_line->cmd[cmd_line->len] = 0x0d;
13226:
1.1.1.28 root 13227: try {
13228: if(msdos_process_exec(command, param, 0)) {
13229: REG16(AX) = 0xffff; // error before processing command
13230: } else {
13231: // set flag to set retval to ax when the started process is terminated
13232: process_t *process = msdos_process_info_get(current_psp);
13233: process->called_by_int2eh = true;
13234: }
13235: } catch(...) {
13236: REG16(AX) = 0xffff; // error before processing command
13237: }
1.1 root 13238: }
13239:
1.1.1.29 root 13240: inline void msdos_int_2fh_05h()
13241: {
13242: switch(REG8(AL)) {
13243: case 0x00:
1.1.1.49 root 13244: // critical error handler is installed
1.1.1.32 root 13245: REG8(AL) = 0xff;
13246: break;
13247: case 0x01:
13248: case 0x02:
13249: for(int i = 0; i < array_length(standard_error_table); i++) {
13250: if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
13251: const char *message = NULL;
13252: if(active_code_page == 932) {
13253: message = standard_error_table[i].message_japanese;
13254: }
13255: if(message == NULL) {
13256: message = standard_error_table[i].message_english;
13257: }
13258: strcpy((char *)(mem + WORK_TOP), message);
13259:
13260: SREG(ES) = WORK_TOP >> 4;
13261: i386_load_segment_descriptor(ES);
13262: REG16(DI) = 0x0000;
13263: REG8(AL) = 0x01;
13264: break;
13265: }
13266: }
1.1.1.29 root 13267: break;
13268: default:
13269: 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 13270: REG16(AX) = 0x01;
1.1.1.29 root 13271: m_CF = 1;
13272: }
13273: }
13274:
1.1.1.44 root 13275: inline void msdos_int_2fh_06h()
13276: {
13277: switch(REG8(AL)) {
13278: case 0x00:
13279: // ASSIGN is not installed
1.1.1.49 root 13280: // REG8(AL) = 0x00;
1.1.1.44 root 13281: break;
13282: case 0x01:
13283: // this call is available from within MIRROR.COM even if ASSIGN is not installed
13284: REG16(AX) = 0x01;
13285: m_CF = 1;
13286: break;
13287: default:
13288: 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));
13289: REG16(AX) = 0x01;
13290: m_CF = 1;
13291: break;
13292: }
13293: }
13294:
1.1.1.22 root 13295: inline void msdos_int_2fh_11h()
13296: {
13297: switch(REG8(AL)) {
13298: case 0x00:
1.1.1.29 root 13299: if(i386_read_stack() == 0xdada) {
13300: // MSCDEX is not installed
13301: // REG8(AL) = 0x00;
13302: } else {
13303: // Network Redirector is not installed
13304: // REG8(AL) = 0x00;
13305: }
1.1.1.22 root 13306: break;
13307: default:
1.1.1.43 root 13308: // 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 13309: REG16(AX) = 0x49; // network software not installed
1.1.1.22 root 13310: m_CF = 1;
13311: break;
13312: }
13313: }
13314:
1.1.1.21 root 13315: inline void msdos_int_2fh_12h()
13316: {
13317: switch(REG8(AL)) {
1.1.1.22 root 13318: case 0x00:
1.1.1.29 root 13319: // DOS 3.0+ internal functions are installed
1.1.1.22 root 13320: REG8(AL) = 0xff;
13321: break;
1.1.1.29 root 13322: // case 0x01: // DOS 3.0+ internal - Close Current File
13323: case 0x02:
13324: {
13325: UINT16 stack = i386_read_stack();
13326: REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
13327: SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
13328: i386_load_segment_descriptor(ES);
13329: }
13330: break;
1.1.1.30 root 13331: case 0x03:
13332: SREG(DS) = (DEVICE_TOP >> 4);
13333: i386_load_segment_descriptor(DS);
13334: break;
1.1.1.29 root 13335: case 0x04:
13336: {
13337: UINT16 stack = i386_read_stack();
13338: REG8(AL) = (stack == '/') ? '\\' : stack;
13339: #if defined(HAS_I386)
13340: m_ZF = (REG8(AL) == '\\');
13341: #else
13342: m_ZeroVal = (REG8(AL) != '\\');
13343: #endif
13344: }
13345: break;
13346: case 0x05:
1.1.1.49 root 13347: {
13348: UINT16 c = i386_read_stack();
13349: if((c >> 0) & 0xff) {
13350: msdos_putch((c >> 0) & 0xff);
13351: }
13352: if((c >> 8) & 0xff) {
13353: msdos_putch((c >> 8) & 0xff);
13354: }
13355: }
1.1.1.29 root 13356: break;
1.1.1.49 root 13357: // case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29 root 13358: // case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
13359: // case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49 root 13360: // case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29 root 13361: // case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
13362: // case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
13363: // case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
13364: case 0x0d:
13365: {
13366: SYSTEMTIME time;
13367: FILETIME file_time;
13368: WORD dos_date, dos_time;
13369: GetLocalTime(&time);
13370: SystemTimeToFileTime(&time, &file_time);
13371: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
13372: REG16(AX) = dos_date;
13373: REG16(DX) = dos_time;
13374: }
13375: break;
13376: // case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
13377: // case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
13378: // case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
13379: case 0x11:
13380: {
13381: char path[MAX_PATH], *p;
13382: strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
13383: my_strupr(path);
13384: while((p = my_strchr(path, '/')) != NULL) {
13385: *p = '\\';
13386: }
13387: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
13388: }
13389: break;
13390: case 0x12:
13391: REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
13392: break;
13393: case 0x13:
13394: {
13395: char tmp[2] = {0};
13396: tmp[0] = i386_read_stack();
13397: my_strupr(tmp);
13398: REG8(AL) = tmp[0];
13399: }
13400: break;
13401: case 0x14:
13402: #if defined(HAS_I386)
13403: m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
13404: #else
13405: m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
13406: #endif
13407: m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
13408: break;
13409: // case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21 root 13410: case 0x16:
13411: if(REG16(BX) < 20) {
13412: SREG(ES) = SFT_TOP >> 4;
13413: i386_load_segment_descriptor(ES);
13414: REG16(DI) = 6 + 0x3b * REG16(BX);
13415:
13416: // update system file table
13417: UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
13418: if(file_handler[REG16(BX)].valid) {
13419: int count = 0;
13420: for(int i = 0; i < 20; i++) {
13421: if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
13422: count++;
13423: }
13424: }
13425: *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
13426: *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
13427: _lseek(REG16(BX), 0, SEEK_END);
13428: *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
13429: _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
13430: } else {
13431: memset(sft, 0, 0x3b);
13432: }
13433: } else {
13434: REG16(AX) = 0x06;
13435: m_CF = 1;
13436: }
13437: break;
1.1.1.49 root 13438: case 0x17:
13439: {
13440: UINT16 drive = i386_read_stack();
13441: if(msdos_is_valid_drive(drive)) {
13442: msdos_cds_update(drive);
13443: }
13444: REG16(SI) = 88 * drive;
13445: SREG(DS) = (CDS_TOP >> 4);
13446: i386_load_segment_descriptor(DS);
13447: }
13448: break;
1.1.1.29 root 13449: // case 0x18: // DOS 3.0+ internal - Get Caller's Registers
13450: // case 0x19: // DOS 3.0+ internal - Set Drive???
13451: case 0x1a:
13452: {
13453: char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
13454: if(path[1] == ':') {
13455: if(path[0] >= 'a' && path[0] <= 'z') {
13456: REG8(AL) = path[0] - 'a' + 1;
13457: } else if(path[0] >= 'A' && path[0] <= 'Z') {
13458: REG8(AL) = path[0] - 'A' + 1;
13459: } else {
13460: REG8(AL) = 0xff; // invalid
13461: }
13462: strcpy(full, path);
13463: strcpy(path, full + 2);
13464: } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
13465: if(full[0] >= 'a' && full[0] <= 'z') {
13466: REG8(AL) = full[0] - 'a' + 1;
13467: } else if(full[0] >= 'A' && full[0] <= 'Z') {
13468: REG8(AL) = full[0] - 'A' + 1;
13469: } else {
13470: REG8(AL) = 0xff; // invalid
13471: }
13472: } else {
13473: REG8(AL) = 0x00; // default
13474: }
13475: }
13476: break;
13477: case 0x1b:
13478: {
13479: int year = REG16(CX) + 1980;
13480: REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
13481: }
13482: break;
13483: // case 0x1c: // DOS 3.0+ internal - Check Sum Memory
13484: // case 0x1d: // DOS 3.0+ internal - Sum Memory
13485: case 0x1e:
13486: {
13487: char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
13488: char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
13489: if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
13490: #if defined(HAS_I386)
13491: m_ZF = (strcmp(full_1st, full_2nd) == 0);
13492: #else
13493: m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
13494: #endif
13495: } else {
13496: #if defined(HAS_I386)
13497: m_ZF = (strcmp(path_1st, path_2nd) == 0);
13498: #else
13499: m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
13500: #endif
13501: }
13502: }
13503: break;
1.1.1.49 root 13504: case 0x1f:
13505: {
13506: UINT16 drive = i386_read_stack();
13507: if(msdos_is_valid_drive(drive)) {
13508: msdos_cds_update(drive);
13509: }
13510: REG16(SI) = 88 * drive;
13511: SREG(ES) = (CDS_TOP >> 4);
13512: i386_load_segment_descriptor(ES);
13513: }
13514: break;
1.1.1.21 root 13515: case 0x20:
13516: {
13517: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
13518:
13519: if(fd < 20) {
13520: SREG(ES) = current_psp;
13521: i386_load_segment_descriptor(ES);
13522: REG16(DI) = offsetof(psp_t, file_table) + fd;
13523: } else {
13524: REG16(AX) = 0x06;
13525: m_CF = 1;
13526: }
13527: }
13528: break;
1.1.1.29 root 13529: case 0x21:
13530: msdos_int_21h_60h(0);
13531: break;
1.1.1.49 root 13532: case 0x22:
13533: {
13534: sda_t *sda = (sda_t *)(mem + SDA_TOP);
13535: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
13536: sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
13537: }
13538: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
13539: sda->error_class = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
13540: }
13541: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
13542: sda->suggested_action = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
13543: }
13544: if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
13545: sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
13546: }
13547: }
13548: break;
1.1.1.29 root 13549: // case 0x23: // DOS 3.0+ internal - Check If Character Device
13550: // case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
13551: case 0x25:
13552: REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
13553: break;
13554: case 0x26:
13555: REG8(AL) = REG8(CL);
13556: msdos_int_21h_3dh();
13557: break;
13558: case 0x27:
13559: msdos_int_21h_3eh();
13560: break;
13561: case 0x28:
13562: REG16(AX) = REG16(BP);
13563: msdos_int_21h_42h();
13564: break;
13565: case 0x29:
13566: msdos_int_21h_3fh();
13567: break;
13568: // case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
13569: case 0x2b:
13570: REG16(AX) = REG16(BP);
13571: msdos_int_21h_44h();
13572: break;
13573: case 0x2c:
13574: REG16(BX) = DEVICE_TOP >> 4;
13575: REG16(AX) = 22;
13576: break;
13577: case 0x2d:
13578: {
13579: sda_t *sda = (sda_t *)(mem + SDA_TOP);
13580: REG16(AX) = sda->extended_error_code;
13581: }
13582: break;
13583: case 0x2e:
13584: if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32 root 13585: SREG(ES) = 0x0001;
13586: i386_load_segment_descriptor(ES);
13587: REG16(DI) = 0x00;
13588: } else if(REG8(DL) == 0x08) {
1.1.1.49 root 13589: // dummy parameter error message read routine is at fffc:0010
13590: SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22 root 13591: i386_load_segment_descriptor(ES);
1.1.1.32 root 13592: REG16(DI) = 0x0010;
1.1.1.22 root 13593: }
13594: break;
1.1.1.29 root 13595: case 0x2f:
13596: if(REG16(DX) != 0) {
1.1.1.30 root 13597: dos_major_version = REG8(DL);
13598: dos_minor_version = REG8(DH);
1.1.1.29 root 13599: } else {
13600: REG8(DL) = 7;
13601: REG8(DH) = 10;
13602: }
13603: break;
13604: // case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
13605: // case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22 root 13606: default:
13607: 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));
13608: REG16(AX) = 0x01;
13609: m_CF = 1;
13610: break;
13611: }
13612: }
13613:
1.1.1.30 root 13614: inline void msdos_int_2fh_13h()
13615: {
13616: static UINT16 prevDS = 0, prevDX = 0;
13617: static UINT16 prevES = 0, prevBX = 0;
13618: UINT16 tmp;
13619:
13620: tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
13621: i386_load_segment_descriptor(DS);
13622: tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
13623:
13624: tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
13625: i386_load_segment_descriptor(ES);
13626: tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
13627: }
13628:
1.1.1.22 root 13629: inline void msdos_int_2fh_14h()
13630: {
13631: switch(REG8(AL)) {
13632: case 0x00:
1.1.1.29 root 13633: // NLSFUNC.COM is installed
13634: REG8(AL) = 0xff;
1.1.1.25 root 13635: break;
13636: case 0x01:
13637: case 0x03:
13638: REG8(AL) = 0x00;
13639: active_code_page = REG16(BX);
13640: msdos_nls_tables_update();
13641: break;
13642: case 0x02:
13643: REG8(AL) = get_extended_country_info(REG16(BP));
13644: break;
13645: case 0x04:
1.1.1.42 root 13646: for(int i = 0;; i++) {
13647: if(country_table[i].code == REG16(DX)) {
13648: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
13649: break;
13650: } else if(country_table[i].code == -1) {
13651: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
13652: break;
13653: }
13654: }
1.1.1.25 root 13655: REG8(AL) = 0x00;
1.1.1.22 root 13656: break;
13657: default:
13658: 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));
13659: REG16(AX) = 0x01;
13660: m_CF = 1;
13661: break;
13662: }
13663: }
13664:
13665: inline void msdos_int_2fh_15h()
13666: {
13667: switch(REG8(AL)) {
1.1.1.29 root 13668: case 0x00: // CD-ROM - Installation Check
13669: if(REG16(BX) == 0x0000) {
1.1.1.43 root 13670: #if 0
13671: // MSCDEX is installed
13672: REG16(BX) = 0;
13673: for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44 root 13674: if(msdos_is_cdrom_drive(i)) {
13675: if(REG16(BX) == 0) {
13676: REG16(CX) = i;
1.1.1.43 root 13677: }
1.1.1.44 root 13678: REG16(BX)++;
1.1.1.43 root 13679: }
13680: }
13681: #else
1.1.1.29 root 13682: // MSCDEX is not installed
13683: // REG8(AL) = 0x00;
1.1.1.43 root 13684: #endif
1.1.1.29 root 13685: } else {
13686: // GRAPHICS.COM is not installed
13687: // REG8(AL) = 0x00;
13688: }
1.1.1.22 root 13689: break;
1.1.1.43 root 13690: case 0x0b:
1.1.1.44 root 13691: // this call is available from within DOSSHELL even if MSCDEX is not installed
13692: REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
13693: REG16(BX) = 0xadad;
1.1.1.43 root 13694: break;
13695: case 0x0d:
1.1.1.44 root 13696: for(int i = 0, n = 0; i < 26; i++) {
13697: if(msdos_is_cdrom_drive(i)) {
13698: mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43 root 13699: }
13700: }
13701: break;
1.1.1.22 root 13702: case 0xff:
1.1.1.29 root 13703: if(REG16(BX) == 0x0000) {
13704: // CORELCDX is not installed
13705: } else {
13706: 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));
13707: REG16(AX) = 0x01;
13708: m_CF = 1;
13709: }
1.1.1.22 root 13710: break;
1.1.1.21 root 13711: default:
1.1.1.22 root 13712: 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 13713: REG16(AX) = 0x01;
13714: m_CF = 1;
13715: break;
13716: }
13717: }
13718:
1.1 root 13719: inline void msdos_int_2fh_16h()
13720: {
13721: switch(REG8(AL)) {
13722: case 0x00:
1.1.1.14 root 13723: if(no_windows) {
1.1.1.29 root 13724: // neither Windows 3.x enhanced mode nor Windows/386 2.x running
13725: // REG8(AL) = 0x00;
1.1.1.14 root 13726: } else {
1.1.1.30 root 13727: REG8(AL) = win_major_version;
13728: REG8(AH) = win_minor_version;
1.1 root 13729: }
13730: break;
1.1.1.43 root 13731: case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30 root 13732: // from DOSBox
13733: i386_set_a20_line(1);
13734: break;
1.1.1.49 root 13735: case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43 root 13736: case 0x08: // Windows Enhanced Mode Init Complete Broadcast
13737: case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
13738: break;
13739: case 0x07:
13740: // Virtual Device Call API
13741: break;
1.1.1.22 root 13742: case 0x0a:
13743: if(!no_windows) {
13744: REG16(AX) = 0x0000;
1.1.1.30 root 13745: REG8(BH) = win_major_version;
13746: REG8(BL) = win_minor_version;
1.1.1.49 root 13747: // REG16(CX) = 0x0002; // standard
1.1.1.22 root 13748: REG16(CX) = 0x0003; // enhanced
13749: }
13750: break;
1.1.1.30 root 13751: case 0x0b:
13752: // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22 root 13753: case 0x0e:
13754: case 0x0f:
1.1.1.30 root 13755: case 0x10:
1.1.1.22 root 13756: case 0x11:
13757: case 0x12:
13758: case 0x13:
13759: case 0x14:
1.1.1.30 root 13760: case 0x15:
1.1.1.43 root 13761: case 0x81:
13762: case 0x82:
1.1.1.44 root 13763: case 0x84:
1.1.1.49 root 13764: case 0x85:
1.1.1.33 root 13765: case 0x86:
1.1.1.22 root 13766: case 0x87:
1.1.1.30 root 13767: case 0x89:
1.1.1.33 root 13768: case 0x8a:
1.1.1.22 root 13769: // function not supported, do not clear AX
13770: break;
1.1.1.14 root 13771: case 0x80:
13772: Sleep(10);
1.1.1.35 root 13773: REQUEST_HARDWRE_UPDATE();
1.1.1.29 root 13774: REG8(AL) = 0x00;
1.1.1.14 root 13775: break;
1.1.1.33 root 13776: case 0x83:
13777: REG16(BX) = 0x01; // system vm id
13778: break;
1.1.1.22 root 13779: case 0x8e:
13780: REG16(AX) = 0x00; // failed
13781: break;
1.1.1.20 root 13782: case 0x8f:
13783: switch(REG8(DH)) {
13784: case 0x01:
1.1.1.49 root 13785: // REG16(AX) = 0x0000; // close command selected but not yet acknowledged
13786: // REG16(AX) = 0x0001; // close command issued and acknowledged
13787: REG16(AX) = 0x168f; // close command not selected -- application should continue
13788: break;
13789: default:
13790: REG16(AX) = 0x0000; // successful
1.1.1.20 root 13791: break;
13792: }
13793: break;
1.1 root 13794: default:
1.1.1.22 root 13795: 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));
13796: REG16(AX) = 0x01;
13797: m_CF = 1;
13798: break;
13799: }
13800: }
13801:
13802: inline void msdos_int_2fh_19h()
13803: {
13804: switch(REG8(AL)) {
13805: case 0x00:
1.1.1.29 root 13806: // SHELLB.COM is not installed
13807: // REG8(AL) = 0x00;
1.1.1.22 root 13808: break;
13809: case 0x01:
13810: case 0x02:
13811: case 0x03:
13812: case 0x04:
13813: REG16(AX) = 0x01;
13814: m_CF = 1;
13815: break;
1.1.1.29 root 13816: case 0x80:
13817: // IBM ROM-DOS v4.0 is not installed
13818: // REG8(AL) = 0x00;
13819: break;
1.1.1.22 root 13820: default:
13821: 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 13822: REG16(AX) = 0x01;
1.1.1.3 root 13823: m_CF = 1;
1.1 root 13824: break;
13825: }
13826: }
13827:
13828: inline void msdos_int_2fh_1ah()
13829: {
13830: switch(REG8(AL)) {
13831: case 0x00:
1.1.1.29 root 13832: // ANSI.SYS is installed
1.1 root 13833: REG8(AL) = 0xff;
13834: break;
1.1.1.49 root 13835: case 0x01:
1.1.1.50 root 13836: if(REG8(CL) == 0x5f) {
13837: // set display information
13838: if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
13839: int cur_width = *(UINT16 *)(mem + 0x44a) + 0;
13840: int cur_height = *(UINT8 *)(mem + 0x484) + 1;
13841: int new_width = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // character columns
13842: int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10); // character rows
13843:
13844: if(cur_width != new_width || cur_height != new_height) {
13845: pcbios_set_console_size(new_width, new_height, true);
13846: }
13847: }
13848: } else if(REG8(CL) == 0x7f) {
1.1.1.49 root 13849: // get display information
1.1.1.50 root 13850: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0; // level (0 for DOS 4.x-6.0)
13851: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0; // reserved (0)
13852: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14; // length of following data (14)
13853: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1; // bit 0 set for blink, clear for intensity
13854: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1; // mode type (1=text, 2=graphics)
13855: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0; // reserved (0)
13856: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4; // 4 bits per pixel
13857: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) = 8 * (*(UINT16 *)(mem + 0x44a) + 0); // pixel columns
13858: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8 *)(mem + 0x484) + 1); // pixel rows
13859: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0; // character columns
13860: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8 *)(mem + 0x484) + 1; // character rows
1.1.1.49 root 13861: } else {
13862: 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));
13863: REG16(AX) = 0x01;
13864: m_CF = 1;
13865: }
13866: break;
1.1 root 13867: default:
1.1.1.22 root 13868: 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));
13869: REG16(AX) = 0x01;
13870: m_CF = 1;
13871: break;
13872: }
13873: }
13874:
1.1.1.30 root 13875: inline void msdos_int_2fh_40h()
1.1.1.22 root 13876: {
13877: switch(REG8(AL)) {
13878: case 0x00:
1.1.1.30 root 13879: // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
13880: REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22 root 13881: break;
1.1.1.43 root 13882: case 0x10:
13883: // OS/2 v2.0+ - Installation Check
13884: REG16(AX) = 0x01;
13885: m_CF = 1;
13886: break;
1.1.1.22 root 13887: default:
13888: 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 13889: REG16(AX) = 0x01;
1.1.1.3 root 13890: m_CF = 1;
1.1 root 13891: break;
13892: }
13893: }
13894:
13895: inline void msdos_int_2fh_43h()
13896: {
13897: switch(REG8(AL)) {
13898: case 0x00:
1.1.1.29 root 13899: // XMS is installed ?
1.1.1.19 root 13900: #ifdef SUPPORT_XMS
13901: if(support_xms) {
13902: REG8(AL) = 0x80;
1.1.1.44 root 13903: }
13904: #endif
13905: break;
13906: case 0x08:
13907: #ifdef SUPPORT_XMS
13908: if(support_xms) {
13909: REG8(AL) = 0x43;
13910: REG8(BL) = 0x01; // IBM PC/AT
13911: REG8(BH) = 0x01; // Fast AT A20 switch time
13912: }
1.1.1.19 root 13913: #endif
13914: break;
13915: case 0x10:
13916: SREG(ES) = XMS_TOP >> 4;
13917: i386_load_segment_descriptor(ES);
1.1.1.26 root 13918: REG16(BX) = 0x15;
1.1 root 13919: break;
1.1.1.44 root 13920: case 0xe0:
13921: // DOS Protected Mode Services (DPMS) v1.0 is not installed
13922: if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
13923: break;
13924: }
1.1 root 13925: default:
1.1.1.22 root 13926: 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));
13927: REG16(AX) = 0x01;
13928: m_CF = 1;
13929: break;
13930: }
13931: }
13932:
13933: inline void msdos_int_2fh_46h()
13934: {
13935: switch(REG8(AL)) {
13936: case 0x80:
1.1.1.29 root 13937: // Windows v3.0 is not installed
13938: // REG8(AL) = 0x00;
1.1.1.22 root 13939: break;
13940: default:
13941: 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));
13942: REG16(AX) = 0x01;
13943: m_CF = 1;
13944: break;
13945: }
13946: }
13947:
13948: inline void msdos_int_2fh_48h()
13949: {
13950: switch(REG8(AL)) {
13951: case 0x00:
1.1.1.29 root 13952: // DOSKEY is not installed
13953: // REG8(AL) = 0x00;
1.1.1.22 root 13954: break;
13955: case 0x10:
13956: msdos_int_21h_0ah();
13957: REG16(AX) = 0x00;
13958: break;
13959: default:
13960: 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 13961: REG16(AX) = 0x01;
1.1.1.3 root 13962: m_CF = 1;
1.1 root 13963: break;
13964: }
13965: }
13966:
13967: inline void msdos_int_2fh_4ah()
13968: {
13969: switch(REG8(AL)) {
1.1.1.29 root 13970: #ifdef SUPPORT_HMA
13971: case 0x01: // DOS 5.0+ - Query Free HMA Space
13972: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
13973: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13974: // restore first free mcb in high memory area
13975: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13976: }
13977: int offset = 0xffff;
13978: if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
13979: REG16(DI) = offset + 0x10;
13980: } else {
13981: REG16(DI) = 0xffff;
13982: }
13983: } else {
13984: // HMA is already used
13985: REG16(BX) = 0;
13986: REG16(DI) = 0xffff;
13987: }
13988: SREG(ES) = 0xffff;
13989: i386_load_segment_descriptor(ES);
13990: break;
13991: case 0x02: // DOS 5.0+ - Allocate HMA Space
13992: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
13993: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13994: // restore first free mcb in high memory area
13995: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13996: }
13997: int size = REG16(BX), offset;
13998: if((size % 16) != 0) {
13999: size &= ~15;
14000: size += 16;
14001: }
14002: if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
14003: REG16(BX) = size;
14004: REG16(DI) = offset + 0x10;
14005: is_hma_used_by_int_2fh = true;
14006: } else {
14007: REG16(BX) = 0;
14008: REG16(DI) = 0xffff;
14009: }
14010: } else {
14011: // HMA is already used
14012: REG16(BX) = 0;
14013: REG16(DI) = 0xffff;
14014: }
14015: SREG(ES) = 0xffff;
14016: i386_load_segment_descriptor(ES);
14017: break;
14018: case 0x03: // Windows95 - (De)Allocate HMA Memory Block
14019: if(REG8(DL) == 0x00) {
14020: if(!is_hma_used_by_xms) {
14021: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14022: // restore first free mcb in high memory area
14023: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14024: is_hma_used_by_int_2fh = false;
14025: }
14026: int size = REG16(BX), offset;
14027: if((size % 16) != 0) {
14028: size &= ~15;
14029: size += 16;
14030: }
14031: if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
14032: // REG16(BX) = size;
14033: SREG(ES) = 0xffff;
14034: i386_load_segment_descriptor(ES);
14035: REG16(DI) = offset + 0x10;
14036: is_hma_used_by_int_2fh = true;
14037: } else {
14038: REG16(DI) = 0xffff;
14039: }
14040: } else {
14041: REG16(DI) = 0xffff;
14042: }
14043: } else if(REG8(DL) == 0x01) {
14044: if(!is_hma_used_by_xms) {
14045: int size = REG16(BX);
14046: if((size % 16) != 0) {
14047: size &= ~15;
14048: size += 16;
14049: }
14050: if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
14051: // memory block address is not changed
14052: } else {
14053: REG16(DI) = 0xffff;
14054: }
14055: } else {
14056: REG16(DI) = 0xffff;
14057: }
14058: } else if(REG8(DL) == 0x02) {
14059: if(!is_hma_used_by_xms) {
14060: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14061: // restore first free mcb in high memory area
14062: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14063: is_hma_used_by_int_2fh = false;
14064: } else {
14065: msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
14066: if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
14067: is_hma_used_by_int_2fh = false;
14068: }
14069: }
14070: }
14071: } else {
14072: 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));
14073: REG16(AX) = 0x01;
14074: m_CF = 1;
14075: }
14076: break;
14077: case 0x04: // Windows95 - Get Start of HMA Memory Chain
14078: if(!is_hma_used_by_xms) {
14079: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
14080: // restore first free mcb in high memory area
14081: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14082: is_hma_used_by_int_2fh = false;
14083: }
14084: REG16(AX) = 0x0000;
14085: SREG(ES) = 0xffff;
14086: i386_load_segment_descriptor(ES);
14087: REG16(DI) = 0x10;
14088: }
14089: break;
14090: #else
1.1 root 14091: case 0x01:
14092: case 0x02:
1.1.1.29 root 14093: // HMA is already used
1.1.1.27 root 14094: REG16(BX) = 0x0000;
1.1.1.3 root 14095: SREG(ES) = 0xffff;
14096: i386_load_segment_descriptor(ES);
1.1 root 14097: REG16(DI) = 0xffff;
14098: break;
1.1.1.19 root 14099: case 0x03:
14100: // unable to allocate
14101: REG16(DI) = 0xffff;
14102: break;
14103: case 0x04:
14104: // function not supported, do not clear AX
14105: break;
1.1.1.29 root 14106: #endif
14107: case 0x10:
1.1.1.42 root 14108: switch(REG16(BX)) {
14109: case 0x0000:
14110: case 0x0001:
14111: case 0x0002:
14112: case 0x0003:
14113: case 0x0004:
14114: case 0x0005:
14115: case 0x0006:
14116: case 0x0007:
14117: case 0x0008:
14118: case 0x000a:
14119: case 0x1234:
14120: // SMARTDRV v4.00+ is not installed
14121: break;
14122: default:
1.1.1.29 root 14123: 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));
14124: REG16(AX) = 0x01;
14125: m_CF = 1;
1.1.1.42 root 14126: break;
1.1.1.29 root 14127: }
14128: break;
14129: case 0x11:
1.1.1.42 root 14130: switch(REG16(BX)) {
14131: case 0x0000:
14132: case 0x0001:
14133: case 0x0002:
14134: case 0x0003:
14135: case 0x0004:
14136: case 0x0005:
14137: case 0x0006:
14138: case 0x0007:
14139: case 0x0008:
14140: case 0x0009:
14141: case 0x000a:
14142: case 0x000b:
14143: case 0xfffe:
14144: case 0xffff:
1.1.1.29 root 14145: // DBLSPACE.BIN is not installed
1.1.1.42 root 14146: break;
14147: default:
14148: 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));
14149: REG16(AX) = 0x01;
14150: m_CF = 1;
14151: break;
14152: }
14153: break;
14154: case 0x12:
14155: if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
14156: // Microsoft Realtime Compression Interface (MRCI) is not installed
14157: } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
14158: // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29 root 14159: } else {
14160: 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));
14161: REG16(AX) = 0x01;
14162: m_CF = 1;
14163: }
1.1.1.22 root 14164: break;
1.1.1.42 root 14165: case 0x13:
14166: // DBLSPACE.BIN is not installed
14167: break;
1.1.1.22 root 14168: default:
14169: 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));
14170: REG16(AX) = 0x01;
14171: m_CF = 1;
14172: break;
14173: }
14174: }
14175:
14176: inline void msdos_int_2fh_4bh()
14177: {
14178: switch(REG8(AL)) {
1.1.1.24 root 14179: case 0x01:
1.1.1.22 root 14180: case 0x02:
1.1.1.29 root 14181: // Task Switcher is not installed
1.1.1.24 root 14182: break;
14183: case 0x03:
14184: // this call is available from within DOSSHELL even if the task switcher is not installed
14185: REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22 root 14186: break;
1.1.1.30 root 14187: case 0x04:
14188: REG16(BX) = 0x0000; // free switcher id successfully
14189: break;
1.1.1.43 root 14190: case 0x05:
14191: REG16(BX) = 0x0000; // no instance data chain
14192: SREG(ES) = 0x0000;
14193: i386_load_segment_descriptor(ES);
14194: break;
1.1 root 14195: default:
1.1.1.22 root 14196: 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 14197: REG16(AX) = 0x01;
1.1.1.3 root 14198: m_CF = 1;
1.1 root 14199: break;
14200: }
14201: }
14202:
1.1.1.44 root 14203: inline void msdos_int_2fh_4dh()
14204: {
14205: switch(REG8(AL)) {
14206: case 0x00:
14207: // KKCFUNC is not installed ???
14208: break;
14209: default:
14210: // 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));
14211: REG16(AX) = 0x01; // invalid function
14212: m_CF = 1;
14213: break;
14214: }
14215: }
14216:
1.1 root 14217: inline void msdos_int_2fh_4fh()
14218: {
14219: switch(REG8(AL)) {
14220: case 0x00:
1.1.1.29 root 14221: // BILING is installed
1.1.1.27 root 14222: REG16(AX) = 0x0000;
14223: REG8(DL) = 0x01; // major version
14224: REG8(DH) = 0x00; // minor version
1.1 root 14225: break;
14226: case 0x01:
1.1.1.27 root 14227: REG16(AX) = 0x0000;
1.1 root 14228: REG16(BX) = active_code_page;
14229: break;
14230: default:
1.1.1.22 root 14231: 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));
14232: REG16(AX) = 0x01;
14233: m_CF = 1;
14234: break;
14235: }
14236: }
14237:
14238: inline void msdos_int_2fh_55h()
14239: {
14240: switch(REG8(AL)) {
14241: case 0x00:
14242: case 0x01:
14243: // 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));
14244: break;
14245: default:
14246: 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 14247: REG16(AX) = 0x01;
1.1.1.3 root 14248: m_CF = 1;
1.1 root 14249: break;
14250: }
14251: }
14252:
1.1.1.44 root 14253: inline void msdos_int_2fh_56h()
14254: {
14255: switch(REG8(AL)) {
14256: case 0x00:
14257: // INTERLNK is not installed
14258: break;
14259: case 0x01:
14260: // this call is available from within SCANDISK even if INTERLNK is not installed
14261: // if(msdos_is_remote_drive(REG8(BH))) {
14262: // REG8(AL) = 0x00;
14263: // }
14264: break;
14265: default:
14266: 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));
14267: REG16(AX) = 0x01;
14268: m_CF = 1;
14269: break;
14270: }
14271: }
14272:
1.1.1.24 root 14273: inline void msdos_int_2fh_adh()
14274: {
14275: switch(REG8(AL)) {
14276: case 0x00:
1.1.1.29 root 14277: // DISPLAY.SYS is installed
1.1.1.24 root 14278: REG8(AL) = 0xff;
14279: REG16(BX) = 0x100; // ???
14280: break;
14281: case 0x01:
14282: active_code_page = REG16(BX);
14283: msdos_nls_tables_update();
14284: REG16(AX) = 0x01;
14285: break;
14286: case 0x02:
14287: REG16(BX) = active_code_page;
14288: break;
14289: case 0x03:
14290: // FIXME
14291: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
14292: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
14293: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
14294: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
14295: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
14296: break;
14297: case 0x80:
1.1.1.49 root 14298: // KEYB.COM is not installed
14299: break;
1.1.1.24 root 14300: default:
14301: 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));
14302: REG16(AX) = 0x01;
14303: m_CF = 1;
14304: break;
14305: }
14306: }
14307:
1.1 root 14308: inline void msdos_int_2fh_aeh()
14309: {
14310: switch(REG8(AL)) {
14311: case 0x00:
1.1.1.28 root 14312: // FIXME: we need to check the given command line
14313: REG8(AL) = 0x00; // the command should be executed as usual
14314: // REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1 root 14315: break;
14316: case 0x01:
14317: {
14318: char command[MAX_PATH];
14319: memset(command, 0, sizeof(command));
1.1.1.3 root 14320: memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1 root 14321:
14322: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
14323: param->env_seg = 0;
14324: param->cmd_line.w.l = 44;
14325: param->cmd_line.w.h = (WORK_TOP >> 4);
14326: param->fcb1.w.l = 24;
14327: param->fcb1.w.h = (WORK_TOP >> 4);
14328: param->fcb2.w.l = 24;
14329: param->fcb2.w.h = (WORK_TOP >> 4);
14330:
14331: memset(mem + WORK_TOP + 24, 0x20, 20);
14332:
14333: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3 root 14334: cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
14335: memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1 root 14336: cmd_line->cmd[cmd_line->len] = 0x0d;
14337:
1.1.1.28 root 14338: try {
14339: msdos_process_exec(command, param, 0);
14340: } catch(...) {
14341: fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1 root 14342: }
14343: }
14344: break;
14345: default:
1.1.1.22 root 14346: 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 14347: REG16(AX) = 0x01;
1.1.1.3 root 14348: m_CF = 1;
1.1 root 14349: break;
14350: }
14351: }
14352:
1.1.1.34 root 14353: inline void msdos_int_2fh_b7h()
14354: {
14355: switch(REG8(AL)) {
14356: case 0x00:
14357: // APPEND is not installed
14358: // REG8(AL) = 0x00;
14359: break;
1.1.1.44 root 14360: case 0x06:
14361: REG16(BX) = 0x0000;
14362: break;
1.1.1.34 root 14363: case 0x07:
1.1.1.43 root 14364: case 0x11:
1.1.1.34 root 14365: // COMMAND.COM calls this service without checking APPEND is installed
14366: break;
14367: default:
14368: 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));
14369: REG16(AX) = 0x01;
14370: m_CF = 1;
14371: break;
14372: }
14373: }
14374:
1.1.1.24 root 14375: inline void msdos_int_33h_0000h()
14376: {
14377: REG16(AX) = 0xffff; // hardware/driver installed
14378: REG16(BX) = MAX_MOUSE_BUTTONS;
14379: }
14380:
14381: inline void msdos_int_33h_0001h()
14382: {
1.1.1.34 root 14383: if(mouse.hidden > 0) {
14384: mouse.hidden--;
14385: }
14386: if(mouse.hidden == 0) {
1.1.1.24 root 14387: if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
14388: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
14389: }
14390: pic[1].imr &= ~0x10; // enable irq12
14391: }
14392: }
14393:
14394: inline void msdos_int_33h_0002h()
14395: {
1.1.1.34 root 14396: mouse.hidden++;
14397: // SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
14398: pic[1].imr |= 0x10; // enable irq12
1.1.1.24 root 14399: }
14400:
14401: inline void msdos_int_33h_0003h()
14402: {
1.1.1.34 root 14403: // if(mouse.hidden > 0) {
14404: update_console_input();
14405: // }
1.1.1.24 root 14406: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 14407: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
14408: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
14409: }
14410:
14411: inline void msdos_int_33h_0004h()
14412: {
14413: mouse.position.x = REG16(CX);
14414: mouse.position.x = REG16(DX);
1.1.1.24 root 14415: }
14416:
14417: inline void msdos_int_33h_0005h()
14418: {
1.1.1.34 root 14419: // if(mouse.hidden > 0) {
14420: update_console_input();
14421: // }
1.1.1.24 root 14422: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
14423: int idx = REG16(BX);
1.1.1.34 root 14424: REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
14425: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
14426: 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 14427: mouse.buttons[idx].pressed_times = 0;
14428: } else {
14429: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
14430: }
14431: REG16(AX) = mouse.get_buttons();
14432: }
14433:
14434: inline void msdos_int_33h_0006h()
14435: {
1.1.1.34 root 14436: // if(mouse.hidden > 0) {
14437: update_console_input();
14438: // }
1.1.1.24 root 14439: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
14440: int idx = REG16(BX);
1.1.1.34 root 14441: REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
14442: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
14443: 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 14444: mouse.buttons[idx].released_times = 0;
14445: } else {
14446: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
14447: }
14448: REG16(AX) = mouse.get_buttons();
14449: }
14450:
14451: inline void msdos_int_33h_0007h()
14452: {
14453: mouse.min_position.x = min(REG16(CX), REG16(DX));
14454: mouse.max_position.x = max(REG16(CX), REG16(DX));
14455: }
14456:
14457: inline void msdos_int_33h_0008h()
14458: {
14459: mouse.min_position.y = min(REG16(CX), REG16(DX));
14460: mouse.max_position.y = max(REG16(CX), REG16(DX));
14461: }
14462:
14463: inline void msdos_int_33h_0009h()
14464: {
14465: mouse.hot_spot[0] = REG16(BX);
14466: mouse.hot_spot[1] = REG16(CX);
14467: }
14468:
1.1.1.49 root 14469: inline void msdos_int_33h_000ah()
14470: {
14471: mouse.screen_mask = REG16(CX);
14472: mouse.cursor_mask = REG16(DX);
14473: }
14474:
1.1.1.24 root 14475: inline void msdos_int_33h_000bh()
14476: {
1.1.1.34 root 14477: // if(mouse.hidden > 0) {
14478: update_console_input();
14479: // }
1.1.1.24 root 14480: int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
14481: int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
14482: mouse.prev_position.x = mouse.position.x;
14483: mouse.prev_position.y = mouse.position.y;
14484: REG16(CX) = dx;
14485: REG16(DX) = dy;
14486: }
14487:
14488: inline void msdos_int_33h_000ch()
14489: {
14490: mouse.call_mask = REG16(CX);
14491: mouse.call_addr.w.l = REG16(DX);
14492: mouse.call_addr.w.h = SREG(ES);
14493: }
14494:
14495: inline void msdos_int_33h_000fh()
14496: {
14497: mouse.mickey.x = REG16(CX);
14498: mouse.mickey.y = REG16(DX);
14499: }
14500:
14501: inline void msdos_int_33h_0011h()
14502: {
14503: REG16(AX) = 0xffff;
14504: REG16(BX) = MAX_MOUSE_BUTTONS;
14505: }
14506:
14507: inline void msdos_int_33h_0014h()
14508: {
14509: UINT16 old_mask = mouse.call_mask;
14510: UINT16 old_ofs = mouse.call_addr.w.l;
14511: UINT16 old_seg = mouse.call_addr.w.h;
14512:
14513: mouse.call_mask = REG16(CX);
14514: mouse.call_addr.w.l = REG16(DX);
14515: mouse.call_addr.w.h = SREG(ES);
14516:
14517: REG16(CX) = old_mask;
14518: REG16(DX) = old_ofs;
14519: SREG(ES) = old_seg;
14520: i386_load_segment_descriptor(ES);
14521: }
14522:
14523: inline void msdos_int_33h_0015h()
14524: {
14525: REG16(BX) = sizeof(mouse);
14526: }
14527:
14528: inline void msdos_int_33h_0016h()
14529: {
14530: memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
14531: }
14532:
14533: inline void msdos_int_33h_0017h()
14534: {
14535: memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
14536: }
14537:
1.1.1.43 root 14538: inline void msdos_int_33h_0018h()
14539: {
14540: for(int i = 0; i < 8; i++) {
14541: if(REG16(CX) & (1 << i)) {
14542: if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
14543: // event handler already exists
14544: REG16(AX) = 0xffff;
14545: break;
14546: }
14547: mouse.call_addr_alt[i].w.l = REG16(DX);
14548: mouse.call_addr_alt[i].w.h = SREG(ES);
14549: }
14550: }
14551: }
14552:
14553: inline void msdos_int_33h_0019h()
14554: {
14555: UINT16 call_mask = REG16(CX);
14556:
14557: REG16(CX) = 0;
14558:
14559: for(int i = 0; i < 8; i++) {
14560: if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
14561: for(int j = 0; j < 8; j++) {
14562: if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
14563: REG16(CX) |= (1 << j);
14564: }
14565: }
14566: REG16(DX) = mouse.call_addr_alt[i].w.l;
14567: REG16(BX) = mouse.call_addr_alt[i].w.h;
14568: break;
14569: }
14570: }
14571: }
14572:
1.1.1.24 root 14573: inline void msdos_int_33h_001ah()
14574: {
14575: mouse.sensitivity[0] = REG16(BX);
14576: mouse.sensitivity[1] = REG16(CX);
14577: mouse.sensitivity[2] = REG16(DX);
14578: }
14579:
14580: inline void msdos_int_33h_001bh()
14581: {
14582: REG16(BX) = mouse.sensitivity[0];
14583: REG16(CX) = mouse.sensitivity[1];
14584: REG16(DX) = mouse.sensitivity[2];
14585: }
14586:
14587: inline void msdos_int_33h_001dh()
14588: {
14589: mouse.display_page = REG16(BX);
14590: }
14591:
14592: inline void msdos_int_33h_001eh()
14593: {
14594: REG16(BX) = mouse.display_page;
14595: }
14596:
1.1.1.34 root 14597: inline void msdos_int_33h_001fh()
14598: {
14599: // from DOSBox
14600: REG16(BX) = 0x0000;
14601: SREG(ES) = 0x0000;
14602: i386_load_segment_descriptor(ES);
14603: mouse.enabled = false;
14604: mouse.old_hidden = mouse.hidden;
14605: mouse.hidden = 1;
14606: }
14607:
14608: inline void msdos_int_33h_0020h()
14609: {
14610: // from DOSBox
14611: mouse.enabled = true;
14612: mouse.hidden = mouse.old_hidden;
14613: }
14614:
1.1.1.24 root 14615: inline void msdos_int_33h_0021h()
14616: {
14617: REG16(AX) = 0xffff;
14618: REG16(BX) = MAX_MOUSE_BUTTONS;
14619: }
14620:
14621: inline void msdos_int_33h_0022h()
14622: {
14623: mouse.language = REG16(BX);
14624: }
14625:
14626: inline void msdos_int_33h_0023h()
14627: {
14628: REG16(BX) = mouse.language;
14629: }
14630:
14631: inline void msdos_int_33h_0024h()
14632: {
14633: REG16(BX) = 0x0805; // V8.05
14634: REG16(CX) = 0x0400; // PS/2
14635: }
14636:
1.1.1.49 root 14637: inline void msdos_int_33h_0025h()
14638: {
14639: REG16(AX) = 0x8000; // driver (not TSR), software text cursor
14640: }
14641:
1.1.1.24 root 14642: inline void msdos_int_33h_0026h()
14643: {
14644: REG16(BX) = 0x0000;
14645: REG16(CX) = mouse.max_position.x;
14646: REG16(DX) = mouse.max_position.y;
14647: }
14648:
1.1.1.49 root 14649: inline void msdos_int_33h_0027h()
14650: {
14651: // if(mouse.hidden > 0) {
14652: update_console_input();
14653: // }
14654: int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
14655: int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
14656: mouse.prev_position.x = mouse.position.x;
14657: mouse.prev_position.y = mouse.position.y;
14658: REG16(AX) = mouse.screen_mask;
14659: REG16(BX) = mouse.cursor_mask;
14660: REG16(CX) = dx;
14661: REG16(DX) = dy;
14662: }
14663:
14664: inline void msdos_int_33h_0028h()
14665: {
14666: if(REG16(CX) != 0) {
14667: UINT8 tmp = REG8(AL);
14668: REG8(AL) = REG8(CL);
14669: pcbios_int_10h_00h();
14670: REG8(AL) = tmp;
14671: }
14672: REG8(CL) = 0x00; // successful
14673: }
14674:
14675: inline void msdos_int_33h_0029h()
14676: {
14677: switch(REG16(CX)) {
14678: case 0x0000:
14679: REG16(CX) = 0x0003;
14680: sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
14681: break;
14682: case 0x0003:
14683: REG16(CX) = 0x0070;
14684: sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
14685: break;
14686: case 0x0070:
14687: REG16(CX) = 0x0071;
14688: sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
14689: break;
14690: case 0x0071:
14691: REG16(CX) = 0x0073;
14692: sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
14693: break;
14694: default:
14695: REG16(CX) = 0x0000;
14696: break;
14697: }
14698: if(REG16(CX) != 0) {
14699: SREG(DS) = (WORK_TOP >> 4);
14700: } else {
14701: SREG(DS) = 0x0000;
14702: }
14703: i386_load_segment_descriptor(DS);
14704: REG16(DX) = 0x0000;
14705: }
14706:
1.1.1.24 root 14707: inline void msdos_int_33h_002ah()
14708: {
1.1.1.34 root 14709: REG16(AX) = -mouse.hidden;
1.1.1.24 root 14710: REG16(BX) = mouse.hot_spot[0];
14711: REG16(CX) = mouse.hot_spot[1];
14712: REG16(DX) = 4; // PS/2
14713: }
14714:
14715: inline void msdos_int_33h_0031h()
14716: {
14717: REG16(AX) = mouse.min_position.x;
14718: REG16(BX) = mouse.min_position.y;
14719: REG16(CX) = mouse.max_position.x;
14720: REG16(DX) = mouse.max_position.y;
14721: }
14722:
14723: inline void msdos_int_33h_0032h()
14724: {
14725: REG16(AX) = 0;
1.1.1.49 root 14726: REG16(AX) |= 0x8000; // 0025h
1.1.1.24 root 14727: REG16(AX) |= 0x4000; // 0026h
1.1.1.49 root 14728: REG16(AX) |= 0x2000; // 0027h
1.1.1.24 root 14729: // REG16(AX) |= 0x1000; // 0028h
14730: // REG16(AX) |= 0x0800; // 0029h
14731: REG16(AX) |= 0x0400; // 002ah
14732: // REG16(AX) |= 0x0200; // 002bh
14733: // REG16(AX) |= 0x0100; // 002ch
14734: // REG16(AX) |= 0x0080; // 002dh
14735: // REG16(AX) |= 0x0040; // 002eh
14736: REG16(AX) |= 0x0020; // 002fh
14737: // REG16(AX) |= 0x0010; // 0030h
14738: REG16(AX) |= 0x0008; // 0031h
14739: REG16(AX) |= 0x0004; // 0032h
14740: // REG16(AX) |= 0x0002; // 0033h
14741: // REG16(AX) |= 0x0001; // 0034h
14742: }
14743:
1.1.1.49 root 14744: inline void msdos_int_33h_004dh()
14745: {
14746: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
14747: }
14748:
14749: inline void msdos_int_33h_006dh()
14750: {
14751: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
14752: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
14753: }
14754:
1.1.1.19 root 14755: inline void msdos_int_67h_40h()
14756: {
14757: if(!support_ems) {
14758: REG8(AH) = 0x84;
14759: } else {
14760: REG8(AH) = 0x00;
14761: }
14762: }
14763:
14764: inline void msdos_int_67h_41h()
14765: {
14766: if(!support_ems) {
14767: REG8(AH) = 0x84;
14768: } else {
14769: REG8(AH) = 0x00;
14770: REG16(BX) = EMS_TOP >> 4;
14771: }
14772: }
14773:
14774: inline void msdos_int_67h_42h()
14775: {
14776: if(!support_ems) {
14777: REG8(AH) = 0x84;
14778: } else {
14779: REG8(AH) = 0x00;
14780: REG16(BX) = free_ems_pages;
14781: REG16(DX) = MAX_EMS_PAGES;
14782: }
14783: }
14784:
14785: inline void msdos_int_67h_43h()
14786: {
14787: if(!support_ems) {
14788: REG8(AH) = 0x84;
14789: } else if(REG16(BX) > MAX_EMS_PAGES) {
14790: REG8(AH) = 0x87;
14791: } else if(REG16(BX) > free_ems_pages) {
14792: REG8(AH) = 0x88;
14793: } else if(REG16(BX) == 0) {
14794: REG8(AH) = 0x89;
14795: } else {
1.1.1.31 root 14796: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14797: if(!ems_handles[i].allocated) {
14798: ems_allocate_pages(i, REG16(BX));
14799: REG8(AH) = 0x00;
14800: REG16(DX) = i;
14801: return;
14802: }
14803: }
14804: REG8(AH) = 0x85;
14805: }
14806: }
14807:
14808: inline void msdos_int_67h_44h()
14809: {
14810: if(!support_ems) {
14811: REG8(AH) = 0x84;
1.1.1.31 root 14812: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14813: REG8(AH) = 0x83;
14814: } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
14815: REG8(AH) = 0x8a;
14816: // } else if(!(REG8(AL) < 4)) {
14817: // REG8(AH) = 0x8b;
14818: } else if(REG16(BX) == 0xffff) {
14819: ems_unmap_page(REG8(AL) & 3);
14820: REG8(AH) = 0x00;
14821: } else {
14822: ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
14823: REG8(AH) = 0x00;
14824: }
14825: }
14826:
14827: inline void msdos_int_67h_45h()
14828: {
14829: if(!support_ems) {
14830: REG8(AH) = 0x84;
1.1.1.31 root 14831: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14832: REG8(AH) = 0x83;
14833: } else {
14834: ems_release_pages(REG16(DX));
14835: REG8(AH) = 0x00;
14836: }
14837: }
14838:
14839: inline void msdos_int_67h_46h()
14840: {
14841: if(!support_ems) {
14842: REG8(AH) = 0x84;
14843: } else {
1.1.1.29 root 14844: // REG16(AX) = 0x0032; // EMS 3.2
14845: REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19 root 14846: }
14847: }
14848:
14849: inline void msdos_int_67h_47h()
14850: {
14851: // NOTE: the map data should be stored in the specified ems page, not process data
14852: process_t *process = msdos_process_info_get(current_psp);
14853:
14854: if(!support_ems) {
14855: REG8(AH) = 0x84;
1.1.1.31 root 14856: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14857: // REG8(AH) = 0x83;
14858: } else if(process->ems_pages_stored) {
14859: REG8(AH) = 0x8d;
14860: } else {
14861: for(int i = 0; i < 4; i++) {
14862: process->ems_pages[i].handle = ems_pages[i].handle;
14863: process->ems_pages[i].page = ems_pages[i].page;
14864: process->ems_pages[i].mapped = ems_pages[i].mapped;
14865: }
14866: process->ems_pages_stored = true;
14867: REG8(AH) = 0x00;
14868: }
14869: }
14870:
14871: inline void msdos_int_67h_48h()
14872: {
14873: // NOTE: the map data should be restored from the specified ems page, not process data
14874: process_t *process = msdos_process_info_get(current_psp);
14875:
14876: if(!support_ems) {
14877: REG8(AH) = 0x84;
1.1.1.31 root 14878: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14879: // REG8(AH) = 0x83;
14880: } else if(!process->ems_pages_stored) {
14881: REG8(AH) = 0x8e;
14882: } else {
14883: for(int i = 0; i < 4; i++) {
14884: if(process->ems_pages[i].mapped) {
14885: ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
14886: } else {
14887: ems_unmap_page(i);
14888: }
14889: }
14890: process->ems_pages_stored = false;
14891: REG8(AH) = 0x00;
14892: }
14893: }
14894:
14895: inline void msdos_int_67h_4bh()
14896: {
14897: if(!support_ems) {
14898: REG8(AH) = 0x84;
14899: } else {
14900: REG8(AH) = 0x00;
14901: REG16(BX) = 0;
1.1.1.31 root 14902: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14903: if(ems_handles[i].allocated) {
14904: REG16(BX)++;
14905: }
14906: }
14907: }
14908: }
14909:
14910: inline void msdos_int_67h_4ch()
14911: {
14912: if(!support_ems) {
14913: REG8(AH) = 0x84;
1.1.1.31 root 14914: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14915: REG8(AH) = 0x83;
14916: } else {
14917: REG8(AH) = 0x00;
14918: REG16(BX) = ems_handles[REG16(DX)].pages;
14919: }
14920: }
14921:
14922: inline void msdos_int_67h_4dh()
14923: {
14924: if(!support_ems) {
14925: REG8(AH) = 0x84;
14926: } else {
14927: REG8(AH) = 0x00;
14928: REG16(BX) = 0;
1.1.1.31 root 14929: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14930: if(ems_handles[i].allocated) {
14931: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
14932: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
14933: REG16(BX)++;
14934: }
14935: }
14936: }
14937: }
14938:
1.1.1.20 root 14939: inline void msdos_int_67h_4eh()
14940: {
14941: if(!support_ems) {
14942: REG8(AH) = 0x84;
14943: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
14944: if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
14945: // save page map
14946: for(int i = 0; i < 4; i++) {
14947: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
14948: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
14949: }
14950: }
14951: if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
14952: // restore page map
14953: for(int i = 0; i < 4; i++) {
14954: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
14955: UINT16 page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
14956:
1.1.1.31 root 14957: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20 root 14958: ems_map_page(i, handle, page);
14959: } else {
14960: ems_unmap_page(i);
14961: }
14962: }
14963: }
14964: REG8(AH) = 0x00;
14965: } else if(REG8(AL) == 0x03) {
14966: REG8(AH) = 0x00;
1.1.1.21 root 14967: REG8(AL) = 4 * 4;
14968: } else {
1.1.1.22 root 14969: 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 14970: REG8(AH) = 0x8f;
14971: }
14972: }
14973:
14974: inline void msdos_int_67h_4fh()
14975: {
14976: if(!support_ems) {
14977: REG8(AH) = 0x84;
14978: } else if(REG8(AL) == 0x00) {
14979: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
14980:
14981: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
14982: for(int i = 0; i < count; i++) {
14983: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
14984: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
14985:
14986: // if(!(physical < 4)) {
14987: // REG8(AH) = 0x8b;
14988: // return;
14989: // }
14990: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41 root 14991: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
14992: *(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 14993: }
14994: REG8(AH) = 0x00;
14995: } else if(REG8(AL) == 0x01) {
14996: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
14997:
14998: for(int i = 0; i < count; i++) {
14999: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
15000: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
15001: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
15002: UINT16 logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
15003:
15004: // if(!(physical < 4)) {
15005: // REG8(AH) = 0x8b;
15006: // return;
15007: // } else
1.1.1.41 root 15008: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21 root 15009: ems_map_page(physical & 3, handle, logical);
15010: } else {
1.1.1.41 root 15011: ems_unmap_page(physical & 3);
1.1.1.21 root 15012: }
15013: }
15014: REG8(AH) = 0x00;
15015: } else if(REG8(AL) == 0x02) {
15016: REG8(AH) = 0x00;
15017: REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20 root 15018: } else {
1.1.1.22 root 15019: 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 15020: REG8(AH) = 0x8f;
15021: }
15022: }
15023:
15024: inline void msdos_int_67h_50h()
15025: {
15026: if(!support_ems) {
15027: REG8(AH) = 0x84;
1.1.1.31 root 15028: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20 root 15029: REG8(AH) = 0x83;
15030: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15031: for(int i = 0; i < REG16(CX); i++) {
15032: int logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
15033: int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
15034:
15035: if(REG8(AL) == 0x01) {
15036: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15037: }
15038: // if(!(physical < 4)) {
15039: // REG8(AH) = 0x8b;
15040: // return;
15041: // } else
15042: if(logical == 0xffff) {
15043: ems_unmap_page(physical & 3);
15044: } else if(logical < ems_handles[REG16(DX)].pages) {
15045: ems_map_page(physical & 3, REG16(DX), logical);
15046: } else {
15047: REG8(AH) = 0x8a;
15048: return;
15049: }
15050: }
15051: REG8(AH) = 0x00;
15052: } else {
1.1.1.22 root 15053: 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 15054: REG8(AH) = 0x8f;
15055: }
15056: }
15057:
1.1.1.19 root 15058: inline void msdos_int_67h_51h()
15059: {
15060: if(!support_ems) {
15061: REG8(AH) = 0x84;
1.1.1.31 root 15062: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15063: REG8(AH) = 0x83;
15064: } else if(REG16(BX) > MAX_EMS_PAGES) {
15065: REG8(AH) = 0x87;
15066: } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
15067: REG8(AH) = 0x88;
15068: } else {
15069: ems_reallocate_pages(REG16(DX), REG16(BX));
15070: REG8(AH) = 0x00;
15071: }
15072: }
15073:
1.1.1.20 root 15074: inline void msdos_int_67h_52h()
15075: {
15076: if(!support_ems) {
15077: REG8(AH) = 0x84;
1.1.1.31 root 15078: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15079: // REG8(AH) = 0x83;
1.1.1.20 root 15080: } else if(REG8(AL) == 0x00) {
15081: REG8(AL) = 0x00; // handle is volatile
15082: REG8(AH) = 0x00;
15083: } else if(REG8(AL) == 0x01) {
15084: if(REG8(BL) == 0x00) {
15085: REG8(AH) = 0x00;
15086: } else {
15087: REG8(AH) = 0x90; // undefined attribute type
15088: }
15089: } else if(REG8(AL) == 0x02) {
15090: REG8(AL) = 0x00; // only volatile handles supported
15091: REG8(AH) = 0x00;
15092: } else {
1.1.1.22 root 15093: 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 15094: REG8(AH) = 0x8f;
15095: }
15096: }
15097:
1.1.1.19 root 15098: inline void msdos_int_67h_53h()
15099: {
15100: if(!support_ems) {
15101: REG8(AH) = 0x84;
1.1.1.31 root 15102: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 15103: REG8(AH) = 0x83;
15104: } else if(REG8(AL) == 0x00) {
15105: memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
15106: REG8(AH) = 0x00;
15107: } else if(REG8(AL) == 0x01) {
1.1.1.31 root 15108: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15109: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
15110: REG8(AH) = 0xa1;
15111: return;
15112: }
15113: }
15114: REG8(AH) = 0x00;
15115: memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
15116: } else {
1.1.1.22 root 15117: 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 15118: REG8(AH) = 0x8f;
1.1.1.19 root 15119: }
15120: }
15121:
15122: inline void msdos_int_67h_54h()
15123: {
15124: if(!support_ems) {
15125: REG8(AH) = 0x84;
15126: } else if(REG8(AL) == 0x00) {
1.1.1.31 root 15127: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15128: if(ems_handles[i].allocated) {
15129: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
15130: } else {
15131: memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
15132: }
15133: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
15134: }
15135: REG8(AH) = 0x00;
15136: REG8(AL) = MAX_EMS_HANDLES;
15137: } else if(REG8(AL) == 0x01) {
15138: REG8(AH) = 0xa0; // not found
1.1.1.31 root 15139: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 15140: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
15141: REG8(AH) = 0x00;
15142: REG16(DX) = i;
15143: break;
15144: }
15145: }
15146: } else if(REG8(AL) == 0x02) {
15147: REG8(AH) = 0x00;
15148: REG16(BX) = MAX_EMS_HANDLES;
15149: } else {
1.1.1.22 root 15150: 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 15151: REG8(AH) = 0x8f;
15152: }
15153: }
15154:
1.1.1.49 root 15155: inline void msdos_int_67h_55h()
15156: {
15157: if(!support_ems) {
15158: REG8(AH) = 0x84;
15159: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15160: REG8(AH) = 0x83;
15161: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15162: UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
15163: UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
15164: UINT8 entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
15165: UINT16 map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
15166: UINT16 map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
15167:
15168: for(int i = 0; i < (int)entries; i++) {
15169: int logical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
15170: int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
15171:
15172: if(REG8(AL) == 0x01) {
15173: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15174: }
15175: // if(!(physical < 4)) {
15176: // REG8(AH) = 0x8b;
15177: // return;
15178: // } else
15179: if(logical == 0xffff) {
15180: ems_unmap_page(physical & 3);
15181: } else if(logical < ems_handles[REG16(DX)].pages) {
15182: ems_map_page(physical & 3, REG16(DX), logical);
15183: } else {
15184: REG8(AH) = 0x8a;
15185: return;
15186: }
15187: }
15188: i386_jmp_far(jump_seg, jump_ofs);
15189: REG8(AH) = 0x00;
15190: } else {
15191: 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));
15192: REG8(AH) = 0x8f;
15193: }
15194: }
15195:
15196: inline void msdos_int_67h_56h()
15197: {
15198: if(!support_ems) {
15199: REG8(AH) = 0x84;
15200: } else if(REG8(AL) == 0x02) {
15201: REG16(BX) = (2 + 2) * 4;
15202: REG8(AH) = 0x00;
15203: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
15204: REG8(AH) = 0x83;
15205: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15206: UINT16 call_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
15207: UINT16 call_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
15208: UINT8 new_entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
15209: UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
15210: UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
15211: #if 0
15212: UINT8 old_entries = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 9);
15213: UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
15214: UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
15215: #endif
15216: UINT16 handles[4], pages[4];
15217:
15218: // alter page map and call routine is at fffc:001f
15219: if(!(call_seg == 0 && call_ofs == 0)) {
15220: mem[DUMMY_TOP + 0x1f] = 0x9a; // call far
15221: mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
15222: mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
15223: mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
15224: mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
15225: } else {
15226: // invalid call addr :-(
15227: mem[DUMMY_TOP + 0x1f] = 0x90; // nop
15228: mem[DUMMY_TOP + 0x20] = 0x90; // nop
15229: mem[DUMMY_TOP + 0x21] = 0x90; // nop
15230: mem[DUMMY_TOP + 0x22] = 0x90; // nop
15231: mem[DUMMY_TOP + 0x23] = 0x90; // nop
15232: }
15233: // do call far (push cs/ip) in old mapping
15234: i386_call_far(DUMMY_TOP >> 4, 0x001f);
15235:
15236: // get old mapping data
15237: #if 0
15238: for(int i = 0; i < (int)old_entries; i++) {
15239: int logical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
15240: int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
15241:
15242: if(REG8(AL) == 0x01) {
15243: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15244: }
15245: // if(!(physical < 4)) {
15246: // REG8(AH) = 0x8b;
15247: // return;
15248: // } else
15249: if(logical == 0xffff) {
15250: ems_unmap_page(physical & 3);
15251: } else if(logical < ems_handles[REG16(DX)].pages) {
15252: ems_map_page(physical & 3, REG16(DX), logical);
15253: } else {
15254: REG8(AH) = 0x8a;
15255: return;
15256: }
15257: }
15258: #endif
15259: for(int i = 0; i < 4; i++) {
15260: handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
15261: pages [i] = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
15262: }
15263:
15264: // set new mapping
15265: for(int i = 0; i < (int)new_entries; i++) {
15266: int logical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
15267: int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
15268:
15269: if(REG8(AL) == 0x01) {
15270: physical = ((physical << 4) - EMS_TOP) / 0x4000;
15271: }
15272: // if(!(physical < 4)) {
15273: // REG8(AH) = 0x8b;
15274: // return;
15275: // } else
15276: if(logical == 0xffff) {
15277: ems_unmap_page(physical & 3);
15278: } else if(logical < ems_handles[REG16(DX)].pages) {
15279: ems_map_page(physical & 3, REG16(DX), logical);
15280: } else {
15281: REG8(AH) = 0x8a;
15282: return;
15283: }
15284: }
15285:
15286: // push old mapping data in new mapping
15287: for(int i = 0; i < 4; i++) {
15288: i386_push16(handles[i]);
15289: i386_push16(pages [i]);
15290: }
15291: REG8(AH) = 0x00;
15292: } else {
15293: 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));
15294: REG8(AH) = 0x8f;
15295: }
15296: }
15297:
1.1.1.20 root 15298: inline void msdos_int_67h_57h_tmp()
15299: {
15300: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
15301: UINT8 src_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
15302: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
15303: UINT16 src_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
15304: UINT16 src_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
15305: UINT8 dest_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
15306: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
15307: UINT16 dest_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
15308: UINT16 dest_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
15309:
1.1.1.32 root 15310: UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20 root 15311: UINT32 src_addr, dest_addr;
15312: UINT32 src_addr_max, dest_addr_max;
15313:
15314: if(src_type == 0) {
15315: src_buffer = mem;
15316: src_addr = (src_seg << 4) + src_ofs;
15317: src_addr_max = MAX_MEM;
15318: } else {
1.1.1.31 root 15319: if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20 root 15320: REG8(AH) = 0x83;
15321: return;
15322: } else if(!(src_seg < ems_handles[src_handle].pages)) {
15323: REG8(AH) = 0x8a;
15324: return;
15325: }
1.1.1.32 root 15326: if(ems_handles[src_handle].buffer != NULL) {
15327: src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
15328: }
1.1.1.20 root 15329: src_addr = src_ofs;
1.1.1.32 root 15330: src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20 root 15331: }
15332: if(dest_type == 0) {
15333: dest_buffer = mem;
15334: dest_addr = (dest_seg << 4) + dest_ofs;
15335: dest_addr_max = MAX_MEM;
15336: } else {
1.1.1.31 root 15337: if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20 root 15338: REG8(AH) = 0x83;
15339: return;
15340: } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
15341: REG8(AH) = 0x8a;
15342: return;
15343: }
1.1.1.32 root 15344: if(ems_handles[dest_handle].buffer != NULL) {
15345: dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
15346: }
1.1.1.20 root 15347: dest_addr = dest_ofs;
1.1.1.32 root 15348: dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20 root 15349: }
1.1.1.32 root 15350: if(src_buffer != NULL && dest_buffer != NULL) {
15351: for(int i = 0; i < copy_length; i++) {
15352: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
15353: if(REG8(AL) == 0x00) {
15354: dest_buffer[dest_addr++] = src_buffer[src_addr++];
15355: } else if(REG8(AL) == 0x01) {
15356: UINT8 tmp = dest_buffer[dest_addr];
15357: dest_buffer[dest_addr++] = src_buffer[src_addr];
15358: src_buffer[src_addr++] = tmp;
15359: }
15360: } else {
15361: REG8(AH) = 0x93;
15362: return;
1.1.1.20 root 15363: }
15364: }
1.1.1.32 root 15365: REG8(AH) = 0x00;
15366: } else {
15367: REG8(AH) = 0x80;
1.1.1.20 root 15368: }
15369: }
15370:
15371: inline void msdos_int_67h_57h()
15372: {
15373: if(!support_ems) {
15374: REG8(AH) = 0x84;
15375: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
15376: struct {
15377: UINT16 handle;
15378: UINT16 page;
15379: bool mapped;
15380: } tmp_pages[4];
15381:
15382: // unmap pages to copy memory data to ems buffer
15383: for(int i = 0; i < 4; i++) {
15384: tmp_pages[i].handle = ems_pages[i].handle;
15385: tmp_pages[i].page = ems_pages[i].page;
15386: tmp_pages[i].mapped = ems_pages[i].mapped;
15387: ems_unmap_page(i);
15388: }
15389:
15390: // run move/exchange operation
15391: msdos_int_67h_57h_tmp();
15392:
15393: // restore unmapped pages
15394: for(int i = 0; i < 4; i++) {
15395: if(tmp_pages[i].mapped) {
15396: ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
15397: }
15398: }
15399: } else {
1.1.1.22 root 15400: 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 15401: REG8(AH) = 0x8f;
15402: }
15403: }
15404:
15405: inline void msdos_int_67h_58h()
15406: {
15407: if(!support_ems) {
15408: REG8(AH) = 0x84;
15409: } else if(REG8(AL) == 0x00) {
15410: for(int i = 0; i < 4; i++) {
1.1.1.30 root 15411: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
15412: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20 root 15413: }
15414: REG8(AH) = 0x00;
15415: REG16(CX) = 4;
15416: } else if(REG8(AL) == 0x01) {
15417: REG8(AH) = 0x00;
15418: REG16(CX) = 4;
15419: } else {
1.1.1.22 root 15420: 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 15421: REG8(AH) = 0x8f;
15422: }
15423: }
15424:
1.1.1.42 root 15425: inline void msdos_int_67h_59h()
15426: {
15427: if(!support_ems) {
15428: REG8(AH) = 0x84;
15429: } else if(REG8(AL) == 0x00) {
1.1.1.49 root 15430: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
15431: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
15432: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
15433: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
15434: REG8(AH) = 0x00;
15435: // REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42 root 15436: } else if(REG8(AL) == 0x01) {
15437: REG8(AH) = 0x00;
15438: REG16(BX) = free_ems_pages;
15439: REG16(DX) = MAX_EMS_PAGES;
15440: } else {
15441: 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));
15442: REG8(AH) = 0x8f;
15443: }
15444: }
15445:
1.1.1.20 root 15446: inline void msdos_int_67h_5ah()
15447: {
15448: if(!support_ems) {
1.1.1.19 root 15449: REG8(AH) = 0x84;
1.1.1.20 root 15450: } else if(REG16(BX) > MAX_EMS_PAGES) {
15451: REG8(AH) = 0x87;
15452: } else if(REG16(BX) > free_ems_pages) {
15453: REG8(AH) = 0x88;
15454: // } else if(REG16(BX) == 0) {
15455: // REG8(AH) = 0x89;
15456: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31 root 15457: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20 root 15458: if(!ems_handles[i].allocated) {
15459: ems_allocate_pages(i, REG16(BX));
15460: REG8(AH) = 0x00;
15461: REG16(DX) = i;
15462: return;
15463: }
15464: }
15465: REG8(AH) = 0x85;
15466: } else {
1.1.1.22 root 15467: 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 15468: REG8(AH) = 0x8f;
1.1.1.19 root 15469: }
15470: }
15471:
1.1.1.49 root 15472: inline void msdos_int_67h_5bh()
15473: {
15474: static UINT8 stored_bl = 0x00;
15475: static UINT16 stored_es = 0x0000;
15476: static UINT16 stored_di = 0x0000;
15477:
15478: if(!support_ems) {
15479: REG8(AH) = 0x84;
15480: } else if(REG8(AL) == 0x00) {
15481: if(stored_bl == 0x00) {
15482: if(!(stored_es == 0 && stored_di == 0)) {
15483: for(int i = 0; i < 4; i++) {
15484: *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
15485: *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
15486: }
15487: }
15488: SREG(ES) = stored_es;
15489: i386_load_segment_descriptor(ES);
15490: REG16(DI) = stored_di;
15491: } else {
15492: REG8(BL) = stored_bl;
15493: }
15494: REG8(AH) = 0x00;
15495: } else if(REG8(AL) == 0x01) {
15496: if(REG8(BL) == 0x00) {
15497: if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
15498: for(int i = 0; i < 4; i++) {
15499: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
15500: UINT16 page = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
15501:
15502: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
15503: ems_map_page(i, handle, page);
15504: } else {
15505: ems_unmap_page(i);
15506: }
15507: }
15508: }
15509: }
15510: stored_bl = REG8(BL);
15511: stored_es = SREG(ES);
15512: stored_di = REG16(DI);
15513: REG8(AH) = 0x00;
15514: } else if(REG8(AL) == 0x02) {
15515: REG16(DX) = 4 * 4;
15516: REG8(AH) = 0x00;
15517: } else if(REG8(AL) == 0x03) {
15518: REG8(BL) = 0x00; // not supported
15519: REG8(AH) = 0x00;
15520: } else if(REG8(AL) == 0x04) {
15521: REG8(AH) = 0x00;
15522: } else if(REG8(AL) == 0x05) {
15523: REG8(BL) = 0x00; // not supported
15524: REG8(AH) = 0x00;
15525: } else {
15526: 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));
15527: REG8(AH) = 0x8f;
15528: }
15529: }
15530:
1.1.1.43 root 15531: inline void msdos_int_67h_5dh()
15532: {
15533: if(!support_ems) {
15534: REG8(AH) = 0x84;
15535: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
15536: REG8(AH) = 0xa4; // operating system denied access
15537: } else {
15538: 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));
15539: REG8(AH) = 0x8f;
15540: }
15541: }
15542:
1.1.1.49 root 15543: inline void msdos_int_67h_70h()
15544: {
15545: if(!support_ems) {
15546: REG8(AH) = 0x84;
15547: } else if(REG8(AL) == 0x00) {
15548: REG8(AL) = 0x00;
15549: REG8(AH) = 0x00;
15550: } else if(REG8(AL) == 0x01) {
15551: REG8(AL) = 0x00;
15552: // REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
15553: REG8(AH) = 0x00;
15554: } else {
15555: 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));
15556: REG8(AH) = 0x8f;
15557: }
15558: }
15559:
1.1.1.30 root 15560: inline void msdos_int_67h_deh()
15561: {
15562: REG8(AH) = 0x84;
15563: }
15564:
1.1.1.19 root 15565: #ifdef SUPPORT_XMS
15566:
1.1.1.32 root 15567: void msdos_xms_init()
1.1.1.26 root 15568: {
1.1.1.30 root 15569: emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
15570: emb_handle_top->address = EMB_TOP;
15571: emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26 root 15572: xms_a20_local_enb_count = 0;
15573: }
15574:
1.1.1.32 root 15575: void msdos_xms_finish()
15576: {
15577: msdos_xms_release();
15578: }
15579:
15580: void msdos_xms_release()
1.1.1.30 root 15581: {
15582: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
15583: emb_handle_t *next_handle = emb_handle->next;
15584: free(emb_handle);
15585: emb_handle = next_handle;
15586: }
15587: }
15588:
15589: emb_handle_t *msdos_xms_get_emb_handle(int handle)
15590: {
15591: if(handle != 0) {
15592: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
15593: if(emb_handle->handle == handle) {
15594: return(emb_handle);
15595: }
15596: }
15597: }
15598: return(NULL);
15599: }
15600:
15601: int msdos_xms_get_unused_emb_handle_id()
15602: {
15603: for(int handle = 1;; handle++) {
15604: if(msdos_xms_get_emb_handle(handle) == NULL) {
15605: return(handle);
15606: }
15607: }
15608: return(0);
15609: }
15610:
15611: int msdos_xms_get_unused_emb_handle_count()
15612: {
15613: int count = 64; //255;
15614:
15615: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
15616: if(emb_handle->handle != 0) {
15617: if(--count == 1) {
15618: break;
15619: }
15620: }
15621: }
15622: return(count);
15623: }
15624:
15625: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
15626: {
15627: if(emb_handle->size_kb > size_kb) {
15628: emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
15629:
15630: new_handle->address = emb_handle->address + size_kb * 1024;
15631: new_handle->size_kb = emb_handle->size_kb - size_kb;
15632: emb_handle->size_kb = size_kb;
15633:
15634: new_handle->prev = emb_handle;
15635: new_handle->next = emb_handle->next;
15636: if(emb_handle->next != NULL) {
15637: emb_handle->next->prev = new_handle;
15638: }
15639: emb_handle->next = new_handle;
15640: }
15641: }
15642:
15643: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
15644: {
15645: emb_handle_t *next_handle = emb_handle->next;
15646:
15647: if(next_handle != NULL) {
15648: emb_handle->size_kb += next_handle->size_kb;
15649:
15650: if(next_handle->next != NULL) {
15651: next_handle->next->prev = emb_handle;
15652: }
15653: emb_handle->next = next_handle->next;
15654: free(next_handle);
15655: }
15656: }
15657:
15658: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
15659: {
15660: emb_handle_t *target_handle = NULL;
15661:
15662: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
15663: if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
15664: if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
15665: target_handle = emb_handle;
15666: }
15667: }
15668: }
15669: if(target_handle != NULL) {
15670: if(target_handle->size_kb > size_kb) {
15671: msdos_xms_split_emb_handle(target_handle, size_kb);
15672: }
15673: // target_handle->handle = msdos_xms_get_unused_emb_handle_id();
15674: return(target_handle);
15675: }
15676: return(NULL);
15677: }
15678:
15679: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
15680: {
15681: emb_handle_t *prev_handle = emb_handle->prev;
15682: emb_handle_t *next_handle = emb_handle->next;
15683:
15684: if(prev_handle != NULL && prev_handle->handle == 0) {
15685: msdos_xms_combine_emb_handles(prev_handle);
15686: emb_handle = prev_handle;
15687: }
15688: if(next_handle != NULL && next_handle->handle == 0) {
15689: msdos_xms_combine_emb_handles(emb_handle);
15690: }
15691: emb_handle->handle = 0;
15692: }
15693:
1.1.1.19 root 15694: inline void msdos_call_xms_00h()
15695: {
1.1.1.29 root 15696: #if defined(HAS_I386)
15697: REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45 root 15698: // REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29 root 15699: REG16(BX) = 0x035f; // V3.95 (Driver Revision)
15700: #else
15701: REG16(AX) = 0x0200; // V2.00 (XMS Version)
15702: REG16(BX) = 0x0270; // V2.70 (Driver Revision)
15703: #endif
15704: // REG16(DX) = 0x0000; // HMA does not exist
15705: REG16(DX) = 0x0001; // HMA does exist
1.1.1.19 root 15706: }
15707:
15708: inline void msdos_call_xms_01h()
15709: {
1.1.1.29 root 15710: if(REG8(AL) == 0x40) {
15711: // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
15712: // DX=KB free extended memory returned by last call of function 08h
15713: REG16(AX) = 0x0000;
15714: REG8(BL) = 0x91;
15715: REG16(DX) = xms_dx_after_call_08h;
15716: } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
15717: REG16(AX) = 0x0000;
15718: REG8(BL) = 0x81; // Vdisk was detected
15719: #ifdef SUPPORT_HMA
15720: } else if(is_hma_used_by_int_2fh) {
15721: REG16(AX) = 0x0000;
15722: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
15723: } else if(is_hma_used_by_xms) {
15724: REG16(AX) = 0x0000;
15725: REG8(BL) = 0x91; // HMA is already in use
15726: } else {
15727: REG16(AX) = 0x0001;
15728: is_hma_used_by_xms = true;
15729: #else
15730: } else {
15731: REG16(AX) = 0x0000;
15732: REG8(BL) = 0x91; // HMA is already in use
15733: #endif
15734: }
1.1.1.19 root 15735: }
15736:
15737: inline void msdos_call_xms_02h()
15738: {
1.1.1.29 root 15739: if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
15740: REG16(AX) = 0x0000;
15741: REG8(BL) = 0x81; // Vdisk was detected
15742: #ifdef SUPPORT_HMA
15743: } else if(is_hma_used_by_int_2fh) {
15744: REG16(AX) = 0x0000;
15745: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
15746: } else if(!is_hma_used_by_xms) {
15747: REG16(AX) = 0x0000;
15748: REG8(BL) = 0x93; // HMA is not allocated
15749: } else {
15750: REG16(AX) = 0x0001;
15751: is_hma_used_by_xms = false;
15752: // restore first free mcb in high memory area
15753: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
15754: #else
15755: } else {
15756: REG16(AX) = 0x0000;
15757: REG8(BL) = 0x91; // HMA is already in use
15758: #endif
15759: }
1.1.1.19 root 15760: }
15761:
15762: inline void msdos_call_xms_03h()
15763: {
15764: i386_set_a20_line(1);
15765: REG16(AX) = 0x0001;
15766: REG8(BL) = 0x00;
15767: }
15768:
15769: inline void msdos_call_xms_04h()
15770: {
1.1.1.21 root 15771: i386_set_a20_line(0);
15772: REG16(AX) = 0x0001;
15773: REG8(BL) = 0x00;
1.1.1.19 root 15774: }
15775:
15776: inline void msdos_call_xms_05h()
15777: {
15778: i386_set_a20_line(1);
15779: REG16(AX) = 0x0001;
15780: REG8(BL) = 0x00;
1.1.1.21 root 15781: xms_a20_local_enb_count++;
1.1.1.19 root 15782: }
15783:
15784: void msdos_call_xms_06h()
15785: {
1.1.1.21 root 15786: if(xms_a20_local_enb_count > 0) {
1.1.1.45 root 15787: if(--xms_a20_local_enb_count == 0) {
15788: i386_set_a20_line(0);
15789: REG16(AX) = 0x0001;
15790: REG8(BL) = 0x00;
15791: } else {
15792: REG16(AX) = 0x0000;
15793: REG8(BL) = 0x94;
15794: }
1.1.1.21 root 15795: } else {
1.1.1.45 root 15796: i386_set_a20_line(0);
1.1.1.21 root 15797: REG16(AX) = 0x0001;
15798: REG8(BL) = 0x00;
1.1.1.19 root 15799: }
15800: }
15801:
15802: inline void msdos_call_xms_07h()
15803: {
15804: REG16(AX) = (m_a20_mask >> 20) & 1;
15805: REG8(BL) = 0x00;
15806: }
15807:
15808: inline void msdos_call_xms_08h()
15809: {
1.1.1.45 root 15810: UINT32 eax = 0, edx = 0;
1.1.1.19 root 15811:
1.1.1.30 root 15812: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
15813: if(emb_handle->handle == 0) {
1.1.1.45 root 15814: if(eax < emb_handle->size_kb) {
15815: eax = emb_handle->size_kb;
1.1.1.19 root 15816: }
1.1.1.45 root 15817: edx += emb_handle->size_kb;
1.1.1.19 root 15818: }
15819: }
1.1.1.45 root 15820: if(eax > 65535) {
15821: eax = 65535;
15822: }
15823: if(edx > 65535) {
15824: edx = 65535;
15825: }
15826: if(eax == 0 && edx == 0) {
1.1.1.19 root 15827: REG8(BL) = 0xa0;
15828: } else {
15829: REG8(BL) = 0x00;
15830: }
1.1.1.45 root 15831: #if defined(HAS_I386)
15832: REG32(EAX) = eax;
15833: REG32(EDX) = edx;
15834: #else
15835: REG16(AX) = (UINT16)eax;
15836: REG16(DX) = (UINT16)edx;
15837: #endif
1.1.1.29 root 15838: xms_dx_after_call_08h = REG16(DX);
1.1.1.19 root 15839: }
15840:
1.1.1.30 root 15841: void msdos_call_xms_09h(int size_kb)
1.1.1.19 root 15842: {
1.1.1.30 root 15843: emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
15844:
15845: if(emb_handle != NULL) {
15846: emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
15847:
15848: REG16(AX) = 0x0001;
15849: REG16(DX) = emb_handle->handle;
15850: REG8(BL) = 0x00;
15851: } else {
15852: REG16(AX) = REG16(DX) = 0x0000;
15853: REG8(BL) = 0xa0;
1.1.1.19 root 15854: }
1.1.1.30 root 15855: }
15856:
15857: inline void msdos_call_xms_09h()
15858: {
15859: msdos_call_xms_09h(REG16(DX));
1.1.1.19 root 15860: }
15861:
15862: inline void msdos_call_xms_0ah()
15863: {
1.1.1.30 root 15864: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
15865:
15866: if(emb_handle == NULL) {
1.1.1.19 root 15867: REG16(AX) = 0x0000;
15868: REG8(BL) = 0xa2;
1.1.1.45 root 15869: // } else if(emb_handle->lock > 0) {
15870: // REG16(AX) = 0x0000;
15871: // REG8(BL) = 0xab;
1.1.1.19 root 15872: } else {
1.1.1.30 root 15873: msdos_xms_free_emb_handle(emb_handle);
1.1.1.19 root 15874:
15875: REG16(AX) = 0x0001;
15876: REG8(BL) = 0x00;
15877: }
15878: }
15879:
15880: inline void msdos_call_xms_0bh()
15881: {
15882: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
15883: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
15884: UINT32 src_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
15885: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
15886: UINT32 dest_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
15887:
15888: UINT8 *src_buffer, *dest_buffer;
15889: UINT32 src_addr_max, dest_addr_max;
1.1.1.30 root 15890: emb_handle_t *emb_handle;
1.1.1.19 root 15891:
15892: if(src_handle == 0) {
15893: src_buffer = mem;
15894: src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
15895: src_addr_max = MAX_MEM;
15896: } else {
1.1.1.30 root 15897: if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19 root 15898: REG16(AX) = 0x0000;
15899: REG8(BL) = 0xa3;
15900: return;
15901: }
1.1.1.30 root 15902: src_buffer = mem + emb_handle->address;
15903: src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 15904: }
15905: if(dest_handle == 0) {
15906: dest_buffer = mem;
15907: dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
15908: dest_addr_max = MAX_MEM;
15909: } else {
1.1.1.30 root 15910: if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19 root 15911: REG16(AX) = 0x0000;
15912: REG8(BL) = 0xa5;
15913: return;
15914: }
1.1.1.30 root 15915: dest_buffer = mem + emb_handle->address;
15916: dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 15917: }
15918: for(int i = 0; i < copy_length; i++) {
15919: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
15920: dest_buffer[dest_addr++] = src_buffer[src_addr++];
15921: } else {
15922: break;
15923: }
15924: }
15925: REG16(AX) = 0x0001;
15926: REG8(BL) = 0x00;
15927: }
15928:
15929: inline void msdos_call_xms_0ch()
15930: {
1.1.1.30 root 15931: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
15932:
15933: if(emb_handle == NULL) {
1.1.1.19 root 15934: REG16(AX) = 0x0000;
15935: REG8(BL) = 0xa2;
15936: } else {
1.1.1.45 root 15937: if(emb_handle->lock < 255) {
15938: emb_handle->lock++;
15939: }
1.1.1.19 root 15940: REG16(AX) = 0x0001;
1.1.1.30 root 15941: REG16(DX) = (emb_handle->address >> 16) & 0xffff;
15942: REG16(BX) = (emb_handle->address ) & 0xffff;
1.1.1.19 root 15943: }
15944: }
15945:
15946: inline void msdos_call_xms_0dh()
15947: {
1.1.1.30 root 15948: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
15949:
15950: if(emb_handle == NULL) {
1.1.1.19 root 15951: REG16(AX) = 0x0000;
15952: REG8(BL) = 0xa2;
1.1.1.30 root 15953: } else if(!(emb_handle->lock > 0)) {
1.1.1.19 root 15954: REG16(AX) = 0x0000;
15955: REG8(BL) = 0xaa;
15956: } else {
1.1.1.30 root 15957: emb_handle->lock--;
1.1.1.19 root 15958: REG16(AX) = 0x0001;
15959: REG8(BL) = 0x00;
15960: }
15961: }
15962:
15963: inline void msdos_call_xms_0eh()
15964: {
1.1.1.30 root 15965: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
15966:
15967: if(emb_handle == NULL) {
1.1.1.19 root 15968: REG16(AX) = 0x0000;
15969: REG8(BL) = 0xa2;
15970: } else {
15971: REG16(AX) = 0x0001;
1.1.1.30 root 15972: REG8(BH) = emb_handle->lock;
15973: REG8(BL) = msdos_xms_get_unused_emb_handle_count();
15974: REG16(DX) = emb_handle->size_kb;
1.1.1.19 root 15975: }
15976: }
15977:
1.1.1.30 root 15978: void msdos_call_xms_0fh(int size_kb)
1.1.1.19 root 15979: {
1.1.1.30 root 15980: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
15981:
15982: if(emb_handle == NULL) {
1.1.1.19 root 15983: REG16(AX) = 0x0000;
15984: REG8(BL) = 0xa2;
1.1.1.30 root 15985: } else if(emb_handle->lock > 0) {
1.1.1.19 root 15986: REG16(AX) = 0x0000;
15987: REG8(BL) = 0xab;
15988: } else {
1.1.1.30 root 15989: if(emb_handle->size_kb < size_kb) {
15990: if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
15991: msdos_xms_combine_emb_handles(emb_handle);
15992: if(emb_handle->size_kb > size_kb) {
15993: msdos_xms_split_emb_handle(emb_handle, size_kb);
15994: }
15995: } else {
15996: int old_handle = emb_handle->handle;
15997: int old_size_kb = emb_handle->size_kb;
15998: UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
15999:
16000: memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
16001: msdos_xms_free_emb_handle(emb_handle);
16002:
16003: if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
16004: emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
16005: }
16006: emb_handle->handle = old_handle;
16007: memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
16008: free(buffer);
16009: }
16010: } else if(emb_handle->size_kb > size_kb) {
16011: msdos_xms_split_emb_handle(emb_handle, size_kb);
16012: }
16013: if(emb_handle->size_kb != size_kb) {
16014: REG16(AX) = 0x0000;
16015: REG8(BL) = 0xa0;
16016: } else {
16017: REG16(AX) = 0x0001;
16018: REG8(BL) = 0x00;
16019: }
1.1.1.19 root 16020: }
16021: }
16022:
1.1.1.30 root 16023: inline void msdos_call_xms_0fh()
16024: {
16025: msdos_call_xms_0fh(REG16(BX));
16026: }
16027:
1.1.1.19 root 16028: inline void msdos_call_xms_10h()
16029: {
16030: int seg;
16031:
16032: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
16033: REG16(AX) = 0x0001;
16034: REG16(BX) = seg;
16035: } else {
16036: REG16(AX) = 0x0000;
16037: REG8(BL) = 0xb0;
16038: REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
16039: }
16040: }
16041:
16042: inline void msdos_call_xms_11h()
16043: {
16044: int mcb_seg = REG16(DX) - 1;
16045: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
16046:
16047: if(mcb->mz == 'M' || mcb->mz == 'Z') {
16048: msdos_mem_free(REG16(DX));
16049: REG16(AX) = 0x0001;
16050: REG8(BL) = 0x00;
16051: } else {
16052: REG16(AX) = 0x0000;
16053: REG8(BL) = 0xb2;
16054: }
16055: }
16056:
16057: inline void msdos_call_xms_12h()
16058: {
16059: int mcb_seg = REG16(DX) - 1;
16060: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
16061: int max_paragraphs;
16062:
16063: if(mcb->mz == 'M' || mcb->mz == 'Z') {
16064: if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
16065: REG16(AX) = 0x0001;
16066: REG8(BL) = 0x00;
16067: } else {
16068: REG16(AX) = 0x0000;
16069: REG8(BL) = 0xb0;
16070: REG16(DX) = max_paragraphs;
16071: }
16072: } else {
16073: REG16(AX) = 0x0000;
16074: REG8(BL) = 0xb2;
16075: }
16076: }
16077:
1.1.1.29 root 16078: #if defined(HAS_I386)
16079:
16080: inline void msdos_call_xms_88h()
16081: {
16082: REG32(EAX) = REG32(EDX) = 0x0000;
16083:
1.1.1.30 root 16084: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
16085: if(emb_handle->handle == 0) {
16086: if(REG32(EAX) < emb_handle->size_kb) {
16087: REG32(EAX) = emb_handle->size_kb;
1.1.1.29 root 16088: }
1.1.1.30 root 16089: REG32(EDX) += emb_handle->size_kb;
1.1.1.29 root 16090: }
16091: }
16092: if(REG32(EAX) == 0 && REG32(EDX) == 0) {
16093: REG8(BL) = 0xa0;
16094: } else {
16095: REG8(BL) = 0x00;
16096: }
16097: REG32(ECX) = EMB_END - 1;
16098: }
16099:
16100: inline void msdos_call_xms_89h()
16101: {
1.1.1.30 root 16102: msdos_call_xms_09h(REG32(EDX));
1.1.1.29 root 16103: }
16104:
16105: inline void msdos_call_xms_8eh()
16106: {
1.1.1.30 root 16107: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
16108:
16109: if(emb_handle == NULL) {
1.1.1.29 root 16110: REG16(AX) = 0x0000;
16111: REG8(BL) = 0xa2;
16112: } else {
16113: REG16(AX) = 0x0001;
1.1.1.30 root 16114: REG8(BH) = emb_handle->lock;
16115: REG16(CX) = msdos_xms_get_unused_emb_handle_count();
16116: REG32(EDX) = emb_handle->size_kb;
1.1.1.29 root 16117: }
16118: }
16119:
16120: inline void msdos_call_xms_8fh()
16121: {
1.1.1.30 root 16122: msdos_call_xms_0fh(REG32(EBX));
1.1.1.29 root 16123: }
16124:
16125: #endif
1.1.1.19 root 16126: #endif
16127:
1.1.1.26 root 16128: UINT16 msdos_get_equipment()
16129: {
16130: static UINT16 equip = 0;
16131:
16132: if(equip == 0) {
16133: #ifdef SUPPORT_FPU
16134: equip |= (1 << 1); // 80x87 coprocessor installed
16135: #endif
16136: equip |= (1 << 2); // pointing device installed (PS/2)
16137: equip |= (2 << 4); // initial video mode (80x25 color)
16138: // equip |= (1 << 8); // 0 if DMA installed
16139: equip |= (2 << 9); // number of serial ports
16140: 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 16141:
16142: // check only A: and B: if it is floppy drive
16143: int n = 0;
16144: for(int i = 0; i < 2; i++) {
1.1.1.44 root 16145: if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
16146: n++;
1.1.1.28 root 16147: }
16148: }
16149: if(n != 0) {
16150: equip |= (1 << 0); // floppy disk(s) installed
16151: n--;
16152: equip |= (n << 6); // number of floppies installed less 1
16153: }
16154: // if(joyGetNumDevs() != 0) {
16155: // equip |= (1 << 12); // game port installed
16156: // }
1.1.1.26 root 16157: }
16158: return(equip);
16159: }
16160:
1.1 root 16161: void msdos_syscall(unsigned num)
16162: {
1.1.1.22 root 16163: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43 root 16164: if(num == 0x08 || num == 0x1c) {
16165: // don't log the timer interrupts
1.1.1.45 root 16166: // 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 16167: } else if(num == 0x30) {
16168: // dummy interrupt for call 0005h (call near)
16169: 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.43 root 16170: } else if(num == 0x68) {
1.1.1.22 root 16171: // dummy interrupt for EMS (int 67h)
1.1.1.33 root 16172: 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.22 root 16173: } else if(num == 0x69) {
16174: // dummy interrupt for XMS (call far)
1.1.1.33 root 16175: 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.50 root 16176: } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45 root 16177: // dummy interrupt
1.1.1.22 root 16178: } else {
1.1.1.33 root 16179: 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 16180: }
16181: #endif
1.1.1.36 root 16182: // update cursor position
16183: if(cursor_moved) {
16184: pcbios_update_cursor_position();
16185: cursor_moved = false;
16186: }
1.1.1.50 root 16187: #ifdef USE_SERVICE_THREAD
16188: // this is called from dummy loop to wait until a serive that waits input is done
16189: if(!in_service)
16190: #endif
1.1.1.33 root 16191: ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22 root 16192:
1.1 root 16193: switch(num) {
16194: case 0x00:
1.1.1.28 root 16195: try {
16196: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16197: error("division by zero\n");
16198: } catch(...) {
16199: fatalerror("division by zero detected, and failed to terminate current process\n");
16200: }
1.1 root 16201: break;
16202: case 0x04:
1.1.1.28 root 16203: try {
16204: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16205: error("overflow\n");
16206: } catch(...) {
16207: fatalerror("overflow detected, and failed to terminate current process\n");
16208: }
1.1 root 16209: break;
16210: case 0x06:
16211: // NOTE: ish.com has illegal instruction...
1.1.1.14 root 16212: if(!ignore_illegal_insn) {
1.1.1.28 root 16213: try {
16214: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16215: error("illegal instruction\n");
16216: } catch(...) {
16217: fatalerror("illegal instruction detected, and failed to terminate current process\n");
16218: }
1.1.1.14 root 16219: } else {
16220: #if defined(HAS_I386)
1.1.1.39 root 16221: m_eip = m_int6h_skip_eip;
16222: #elif defined(HAS_I286)
16223: m_pc = m_int6h_skip_pc;
1.1.1.14 root 16224: #else
1.1.1.39 root 16225: // 8086/80186 ignore an invalid opcode
1.1.1.14 root 16226: #endif
16227: }
1.1 root 16228: break;
1.1.1.33 root 16229: case 0x09:
16230: // ctrl-break is pressed
16231: if(raise_int_1bh) {
16232: #if defined(HAS_I386)
16233: m_ext = 0; // not an external interrupt
16234: i386_trap(0x1b, 1, 0);
16235: m_ext = 1;
16236: #else
16237: PREFIX86(_interrupt)(0x1b);
16238: #endif
16239: raise_int_1bh = false;
16240: }
1.1.1.8 root 16241: case 0x08:
1.1.1.14 root 16242: // pcbios_irq0(); // this causes too slow emulation...
1.1.1.8 root 16243: case 0x0b:
16244: case 0x0c:
16245: case 0x0d:
16246: case 0x0e:
16247: case 0x0f:
16248: // EOI
16249: pic[0].isr &= ~(1 << (num - 0x08));
16250: pic_update();
16251: break;
1.1 root 16252: case 0x10:
16253: // PC BIOS - Video
1.1.1.14 root 16254: if(!restore_console_on_exit) {
1.1.1.15 root 16255: change_console_size(scr_width, scr_height);
1.1 root 16256: }
1.1.1.3 root 16257: m_CF = 0;
1.1 root 16258: switch(REG8(AH)) {
1.1.1.16 root 16259: case 0x00: pcbios_int_10h_00h(); break;
1.1 root 16260: case 0x01: pcbios_int_10h_01h(); break;
16261: case 0x02: pcbios_int_10h_02h(); break;
16262: case 0x03: pcbios_int_10h_03h(); break;
16263: case 0x05: pcbios_int_10h_05h(); break;
16264: case 0x06: pcbios_int_10h_06h(); break;
16265: case 0x07: pcbios_int_10h_07h(); break;
16266: case 0x08: pcbios_int_10h_08h(); break;
16267: case 0x09: pcbios_int_10h_09h(); break;
16268: case 0x0a: pcbios_int_10h_0ah(); break;
16269: case 0x0b: break;
1.1.1.40 root 16270: case 0x0c: pcbios_int_10h_0ch(); break;
16271: case 0x0d: pcbios_int_10h_0dh(); break;
1.1 root 16272: case 0x0e: pcbios_int_10h_0eh(); break;
16273: case 0x0f: pcbios_int_10h_0fh(); break;
16274: case 0x10: break;
1.1.1.14 root 16275: case 0x11: pcbios_int_10h_11h(); break;
16276: case 0x12: pcbios_int_10h_12h(); break;
1.1 root 16277: case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30 root 16278: case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14 root 16279: case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24 root 16280: case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
16281: case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1 root 16282: case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24 root 16283: case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
16284: case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22 root 16285: case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30 root 16286: case 0x6f: break;
1.1.1.22 root 16287: case 0x80: m_CF = 1; break; // unknown
16288: case 0x81: m_CF = 1; break; // unknown
1.1 root 16289: case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22 root 16290: case 0x83: pcbios_int_10h_83h(); break;
16291: case 0x8b: break;
16292: case 0x8c: m_CF = 1; break; // unknown
16293: case 0x8d: m_CF = 1; break; // unknown
16294: case 0x8e: m_CF = 1; break; // unknown
16295: case 0x90: pcbios_int_10h_90h(); break;
16296: case 0x91: pcbios_int_10h_91h(); break;
16297: case 0x92: break;
16298: case 0x93: break;
16299: case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24 root 16300: case 0xfa: break; // ega register interface library is not installed
1.1 root 16301: case 0xfe: pcbios_int_10h_feh(); break;
16302: case 0xff: pcbios_int_10h_ffh(); break;
16303: default:
1.1.1.22 root 16304: 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));
16305: m_CF = 1;
1.1 root 16306: break;
16307: }
16308: break;
16309: case 0x11:
16310: // PC BIOS - Get Equipment List
1.1.1.26 root 16311: REG16(AX) = msdos_get_equipment();
1.1 root 16312: break;
16313: case 0x12:
16314: // PC BIOS - Get Memory Size
1.1.1.33 root 16315: REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1 root 16316: break;
16317: case 0x13:
1.1.1.42 root 16318: // PC BIOS - Disk I/O
16319: {
16320: static UINT8 last = 0x00;
16321: switch(REG8(AH)) {
16322: case 0x00: pcbios_int_13h_00h(); break;
16323: case 0x01: // get last status
16324: REG8(AH) = last;
16325: break;
16326: case 0x02: pcbios_int_13h_02h(); break;
16327: case 0x03: pcbios_int_13h_03h(); break;
16328: case 0x04: pcbios_int_13h_04h(); break;
16329: case 0x08: pcbios_int_13h_08h(); break;
16330: case 0x0a: pcbios_int_13h_02h(); break;
16331: case 0x0b: pcbios_int_13h_03h(); break;
16332: case 0x0d: pcbios_int_13h_00h(); break;
16333: case 0x10: pcbios_int_13h_10h(); break;
16334: case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43 root 16335: case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42 root 16336: case 0x05: // format
16337: case 0x06:
16338: case 0x07:
16339: REG8(AH) = 0x0c; // unsupported track or invalid media
16340: m_CF = 1;
16341: break;
16342: case 0x09:
16343: case 0x0c: // seek
16344: case 0x11: // recalib
16345: case 0x14:
16346: case 0x17:
16347: REG8(AH) = 0x00; // successful completion
16348: break;
1.1.1.43 root 16349: case 0x21: // QUICKCACHE II v4.20 - Flush Cache
16350: case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
16351: REG8(AH) = 0x01; // invalid function
16352: m_CF = 1;
16353: break;
1.1.1.42 root 16354: default:
16355: 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));
16356: REG8(AH) = 0x01; // invalid function
16357: m_CF = 1;
16358: break;
16359: }
16360: last = REG8(AH);
16361: }
1.1 root 16362: break;
16363: case 0x14:
16364: // PC BIOS - Serial I/O
1.1.1.25 root 16365: switch(REG8(AH)) {
16366: case 0x00: pcbios_int_14h_00h(); break;
16367: case 0x01: pcbios_int_14h_01h(); break;
16368: case 0x02: pcbios_int_14h_02h(); break;
16369: case 0x03: pcbios_int_14h_03h(); break;
16370: case 0x04: pcbios_int_14h_04h(); break;
16371: case 0x05: pcbios_int_14h_05h(); break;
16372: default:
16373: 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));
16374: break;
16375: }
1.1 root 16376: break;
16377: case 0x15:
16378: // PC BIOS
1.1.1.3 root 16379: m_CF = 0;
1.1 root 16380: switch(REG8(AH)) {
1.1.1.14 root 16381: case 0x10: pcbios_int_15h_10h(); break;
1.1 root 16382: case 0x23: pcbios_int_15h_23h(); break;
16383: case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24 root 16384: case 0x41: break;
1.1 root 16385: case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22 root 16386: case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30 root 16387: case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43 root 16388: case 0x84: pcbios_int_15h_84h(); break;
1.1 root 16389: case 0x86: pcbios_int_15h_86h(); break;
16390: case 0x87: pcbios_int_15h_87h(); break;
16391: case 0x88: pcbios_int_15h_88h(); break;
16392: case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21 root 16393: case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22 root 16394: case 0xc0: // PS/2 ???
16395: case 0xc1:
16396: case 0xc2:
1.1.1.30 root 16397: case 0xc3: // PS50+ ???
16398: case 0xc4:
1.1.1.22 root 16399: REG8(AH) = 0x86;
16400: m_CF = 1;
16401: break;
1.1.1.3 root 16402: #if defined(HAS_I386)
1.1 root 16403: case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3 root 16404: #endif
1.1 root 16405: case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22 root 16406: case 0xe8: pcbios_int_15h_e8h(); break;
1.1 root 16407: default:
1.1.1.22 root 16408: 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));
16409: REG8(AH) = 0x86;
1.1.1.3 root 16410: m_CF = 1;
1.1 root 16411: break;
16412: }
16413: break;
16414: case 0x16:
16415: // PC BIOS - Keyboard
1.1.1.3 root 16416: m_CF = 0;
1.1 root 16417: switch(REG8(AH)) {
16418: case 0x00: pcbios_int_16h_00h(); break;
16419: case 0x01: pcbios_int_16h_01h(); break;
16420: case 0x02: pcbios_int_16h_02h(); break;
16421: case 0x03: pcbios_int_16h_03h(); break;
16422: case 0x05: pcbios_int_16h_05h(); break;
16423: case 0x10: pcbios_int_16h_00h(); break;
16424: case 0x11: pcbios_int_16h_01h(); break;
16425: case 0x12: pcbios_int_16h_12h(); break;
16426: case 0x13: pcbios_int_16h_13h(); break;
16427: case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24 root 16428: case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30 root 16429: case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22 root 16430: case 0xda: break; // unknown
1.1.1.43 root 16431: case 0xdb: break; // unknown
1.1.1.22 root 16432: case 0xff: break; // unknown
1.1 root 16433: default:
1.1.1.22 root 16434: 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 16435: break;
16436: }
16437: break;
16438: case 0x17:
16439: // PC BIOS - Printer
1.1.1.37 root 16440: m_CF = 0;
16441: switch(REG8(AH)) {
16442: case 0x00: pcbios_int_17h_00h(); break;
16443: case 0x01: pcbios_int_17h_01h(); break;
16444: case 0x02: pcbios_int_17h_02h(); break;
16445: case 0x03: pcbios_int_17h_03h(); break;
16446: case 0x50: pcbios_int_17h_50h(); break;
16447: case 0x51: pcbios_int_17h_51h(); break;
16448: case 0x52: pcbios_int_17h_52h(); break;
16449: case 0x84: pcbios_int_17h_84h(); break;
16450: case 0x85: pcbios_int_17h_85h(); break;
16451: default:
16452: 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));
16453: break;
16454: }
1.1 root 16455: break;
16456: case 0x1a:
16457: // PC BIOS - Timer
1.1.1.3 root 16458: m_CF = 0;
1.1 root 16459: switch(REG8(AH)) {
16460: case 0x00: pcbios_int_1ah_00h(); break;
16461: case 0x01: break;
16462: case 0x02: pcbios_int_1ah_02h(); break;
16463: case 0x03: break;
16464: case 0x04: pcbios_int_1ah_04h(); break;
16465: case 0x05: break;
16466: case 0x0a: pcbios_int_1ah_0ah(); break;
16467: case 0x0b: break;
1.1.1.14 root 16468: case 0x35: break; // Word Perfect Third Party Interface?
16469: case 0x36: break; // Word Perfect Third Party Interface
16470: case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44 root 16471: case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43 root 16472: case 0xb1: break; // PCI BIOS v2.0c+
16473: case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1 root 16474: default:
1.1.1.22 root 16475: 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 16476: break;
16477: }
16478: break;
1.1.1.33 root 16479: case 0x1b:
16480: mem[0x471] = 0x00;
16481: break;
1.1 root 16482: case 0x20:
1.1.1.28 root 16483: try {
16484: msdos_process_terminate(SREG(CS), retval, 1);
16485: } catch(...) {
16486: fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
16487: }
1.1 root 16488: break;
1.1.1.49 root 16489: case 0x30:
1.1.1.46 root 16490: // dummy interrupt for case map routine pointed in the country info
16491: // if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
16492: // REG8(AL) = 0x00;
16493: // break;
16494: // }
1.1 root 16495: case 0x21:
16496: // MS-DOS System Call
1.1.1.3 root 16497: m_CF = 0;
1.1.1.28 root 16498: try {
1.1.1.46 root 16499: switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28 root 16500: case 0x00: msdos_int_21h_00h(); break;
16501: case 0x01: msdos_int_21h_01h(); break;
16502: case 0x02: msdos_int_21h_02h(); break;
16503: case 0x03: msdos_int_21h_03h(); break;
16504: case 0x04: msdos_int_21h_04h(); break;
16505: case 0x05: msdos_int_21h_05h(); break;
16506: case 0x06: msdos_int_21h_06h(); break;
16507: case 0x07: msdos_int_21h_07h(); break;
16508: case 0x08: msdos_int_21h_08h(); break;
16509: case 0x09: msdos_int_21h_09h(); break;
16510: case 0x0a: msdos_int_21h_0ah(); break;
16511: case 0x0b: msdos_int_21h_0bh(); break;
16512: case 0x0c: msdos_int_21h_0ch(); break;
16513: case 0x0d: msdos_int_21h_0dh(); break;
16514: case 0x0e: msdos_int_21h_0eh(); break;
16515: case 0x0f: msdos_int_21h_0fh(); break;
16516: case 0x10: msdos_int_21h_10h(); break;
16517: case 0x11: msdos_int_21h_11h(); break;
16518: case 0x12: msdos_int_21h_12h(); break;
16519: case 0x13: msdos_int_21h_13h(); break;
16520: case 0x14: msdos_int_21h_14h(); break;
16521: case 0x15: msdos_int_21h_15h(); break;
16522: case 0x16: msdos_int_21h_16h(); break;
16523: case 0x17: msdos_int_21h_17h(); break;
16524: case 0x18: msdos_int_21h_18h(); break;
16525: case 0x19: msdos_int_21h_19h(); break;
16526: case 0x1a: msdos_int_21h_1ah(); break;
16527: case 0x1b: msdos_int_21h_1bh(); break;
16528: case 0x1c: msdos_int_21h_1ch(); break;
16529: case 0x1d: msdos_int_21h_1dh(); break;
16530: case 0x1e: msdos_int_21h_1eh(); break;
16531: case 0x1f: msdos_int_21h_1fh(); break;
16532: case 0x20: msdos_int_21h_20h(); break;
16533: case 0x21: msdos_int_21h_21h(); break;
16534: case 0x22: msdos_int_21h_22h(); break;
16535: case 0x23: msdos_int_21h_23h(); break;
16536: case 0x24: msdos_int_21h_24h(); break;
16537: case 0x25: msdos_int_21h_25h(); break;
16538: case 0x26: msdos_int_21h_26h(); break;
16539: case 0x27: msdos_int_21h_27h(); break;
16540: case 0x28: msdos_int_21h_28h(); break;
16541: case 0x29: msdos_int_21h_29h(); break;
16542: case 0x2a: msdos_int_21h_2ah(); break;
16543: case 0x2b: msdos_int_21h_2bh(); break;
16544: case 0x2c: msdos_int_21h_2ch(); break;
16545: case 0x2d: msdos_int_21h_2dh(); break;
16546: case 0x2e: msdos_int_21h_2eh(); break;
16547: case 0x2f: msdos_int_21h_2fh(); break;
16548: case 0x30: msdos_int_21h_30h(); break;
16549: case 0x31: msdos_int_21h_31h(); break;
16550: case 0x32: msdos_int_21h_32h(); break;
16551: case 0x33: msdos_int_21h_33h(); break;
16552: case 0x34: msdos_int_21h_34h(); break;
16553: case 0x35: msdos_int_21h_35h(); break;
16554: case 0x36: msdos_int_21h_36h(); break;
16555: case 0x37: msdos_int_21h_37h(); break;
16556: case 0x38: msdos_int_21h_38h(); break;
16557: case 0x39: msdos_int_21h_39h(0); break;
16558: case 0x3a: msdos_int_21h_3ah(0); break;
16559: case 0x3b: msdos_int_21h_3bh(0); break;
16560: case 0x3c: msdos_int_21h_3ch(); break;
16561: case 0x3d: msdos_int_21h_3dh(); break;
16562: case 0x3e: msdos_int_21h_3eh(); break;
16563: case 0x3f: msdos_int_21h_3fh(); break;
16564: case 0x40: msdos_int_21h_40h(); break;
16565: case 0x41: msdos_int_21h_41h(0); break;
16566: case 0x42: msdos_int_21h_42h(); break;
16567: case 0x43: msdos_int_21h_43h(0); break;
16568: case 0x44: msdos_int_21h_44h(); break;
16569: case 0x45: msdos_int_21h_45h(); break;
16570: case 0x46: msdos_int_21h_46h(); break;
16571: case 0x47: msdos_int_21h_47h(0); break;
16572: case 0x48: msdos_int_21h_48h(); break;
16573: case 0x49: msdos_int_21h_49h(); break;
16574: case 0x4a: msdos_int_21h_4ah(); break;
16575: case 0x4b: msdos_int_21h_4bh(); break;
16576: case 0x4c: msdos_int_21h_4ch(); break;
16577: case 0x4d: msdos_int_21h_4dh(); break;
16578: case 0x4e: msdos_int_21h_4eh(); break;
16579: case 0x4f: msdos_int_21h_4fh(); break;
16580: case 0x50: msdos_int_21h_50h(); break;
16581: case 0x51: msdos_int_21h_51h(); break;
16582: case 0x52: msdos_int_21h_52h(); break;
1.1.1.43 root 16583: case 0x53: msdos_int_21h_53h(); break;
1.1.1.28 root 16584: case 0x54: msdos_int_21h_54h(); break;
16585: case 0x55: msdos_int_21h_55h(); break;
16586: case 0x56: msdos_int_21h_56h(0); break;
16587: case 0x57: msdos_int_21h_57h(); break;
16588: case 0x58: msdos_int_21h_58h(); break;
16589: case 0x59: msdos_int_21h_59h(); break;
16590: case 0x5a: msdos_int_21h_5ah(); break;
16591: case 0x5b: msdos_int_21h_5bh(); break;
16592: case 0x5c: msdos_int_21h_5ch(); break;
16593: case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42 root 16594: case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30 root 16595: case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28 root 16596: case 0x60: msdos_int_21h_60h(0); break;
16597: case 0x61: msdos_int_21h_61h(); break;
16598: case 0x62: msdos_int_21h_62h(); break;
16599: case 0x63: msdos_int_21h_63h(); break;
1.1.1.33 root 16600: // 0x64: Set Device Driver Lockahead Flag
1.1.1.28 root 16601: case 0x65: msdos_int_21h_65h(); break;
16602: case 0x66: msdos_int_21h_66h(); break;
16603: case 0x67: msdos_int_21h_67h(); break;
16604: case 0x68: msdos_int_21h_68h(); break;
16605: case 0x69: msdos_int_21h_69h(); break;
16606: case 0x6a: msdos_int_21h_6ah(); break;
16607: case 0x6b: msdos_int_21h_6bh(); break;
16608: case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48 root 16609: case 0x6d: // Find First ROM Program
16610: case 0x6e: // Find Next ROM Program
16611: case 0x6f: // Get/Set ROM Scan Start Address
16612: REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
16613: break;
1.1.1.43 root 16614: case 0x70: msdos_int_21h_70h(); break;
1.1.1.48 root 16615: case 0x71: // Windows95 - Long Filename Functions
1.1.1.28 root 16616: switch(REG8(AL)) {
16617: case 0x0d: msdos_int_21h_710dh(); break;
16618: case 0x39: msdos_int_21h_39h(1); break;
16619: case 0x3a: msdos_int_21h_3ah(1); break;
16620: case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48 root 16621: case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28 root 16622: case 0x43: msdos_int_21h_43h(1); break;
16623: case 0x47: msdos_int_21h_47h(1); break;
16624: case 0x4e: msdos_int_21h_714eh(); break;
16625: case 0x4f: msdos_int_21h_714fh(); break;
16626: case 0x56: msdos_int_21h_56h(1); break;
16627: case 0x60: msdos_int_21h_60h(1); break;
16628: case 0x6c: msdos_int_21h_6ch(1); break;
16629: case 0xa0: msdos_int_21h_71a0h(); break;
16630: case 0xa1: msdos_int_21h_71a1h(); break;
16631: case 0xa6: msdos_int_21h_71a6h(); break;
16632: case 0xa7: msdos_int_21h_71a7h(); break;
16633: case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45 root 16634: case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28 root 16635: case 0xaa: msdos_int_21h_71aah(); break;
16636: default:
16637: 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));
16638: REG16(AX) = 0x7100;
16639: m_CF = 1;
16640: break;
16641: }
16642: break;
1.1.1.48 root 16643: case 0x72: // Windows95 beta - LFN FindClose
16644: // 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));
16645: REG16(AX) = 0x7200;
16646: m_CF = 1;
16647: break;
16648: case 0x73: // Windows95 - FAT32 Functions
1.1.1.28 root 16649: switch(REG8(AL)) {
16650: case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33 root 16651: // 0x01: Set Drive Locking ???
1.1.1.28 root 16652: case 0x02: msdos_int_21h_7302h(); break;
16653: case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33 root 16654: // 0x04: Set DPB to Use for Formatting
16655: // 0x05: Extended Absolute Disk Read/Write
1.1.1.28 root 16656: default:
16657: 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));
16658: REG16(AX) = 0x7300;
16659: m_CF = 1;
16660: break;
16661: }
1.1 root 16662: break;
1.1.1.30 root 16663: case 0xdb: msdos_int_21h_dbh(); break;
16664: case 0xdc: msdos_int_21h_dch(); break;
1.1 root 16665: default:
1.1.1.22 root 16666: 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 16667: REG16(AX) = 0x01;
1.1.1.3 root 16668: m_CF = 1;
1.1 root 16669: break;
16670: }
1.1.1.28 root 16671: } catch(int error) {
16672: REG16(AX) = error;
16673: m_CF = 1;
16674: } catch(...) {
16675: REG16(AX) = 0x1f; // general failure
1.1.1.3 root 16676: m_CF = 1;
1.1 root 16677: }
1.1.1.3 root 16678: if(m_CF) {
1.1.1.23 root 16679: sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47 root 16680: sda->int21h_5d0ah_called = 0;
1.1.1.23 root 16681: sda->extended_error_code = REG16(AX);
16682: switch(sda->extended_error_code) {
16683: case 4: // Too many open files
16684: case 8: // Insufficient memory
16685: sda->error_class = 1; // Out of resource
16686: break;
16687: case 5: // Access denied
16688: sda->error_class = 3; // Authorization
16689: break;
16690: case 7: // Memory control block destroyed
16691: sda->error_class = 4; // Internal
16692: break;
16693: case 2: // File not found
16694: case 3: // Path not found
16695: case 15: // Invaid drive specified
16696: case 18: // No more files
16697: sda->error_class = 8; // Not found
16698: break;
16699: case 32: // Sharing violation
16700: case 33: // Lock violation
16701: sda->error_class = 10; // Locked
16702: break;
16703: // case 16: // Removal of current directory attempted
16704: case 19: // Attempted write on protected disk
16705: case 21: // Drive not ready
16706: // case 29: // Write failure
16707: // case 30: // Read failure
16708: // case 82: // Cannot create subdirectory
16709: sda->error_class = 11; // Media
16710: break;
16711: case 80: // File already exists
16712: sda->error_class = 12; // Already exist
16713: break;
16714: default:
16715: sda->error_class = 13; // Unknown
16716: break;
16717: }
16718: sda->suggested_action = 1; // Retry
16719: sda->locus_of_last_error = 1; // Unknown
1.1 root 16720: }
1.1.1.33 root 16721: if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26 root 16722: // raise int 23h
16723: #if defined(HAS_I386)
16724: m_ext = 0; // not an external interrupt
16725: i386_trap(0x23, 1, 0);
16726: m_ext = 1;
16727: #else
16728: PREFIX86(_interrupt)(0x23);
16729: #endif
16730: }
1.1 root 16731: break;
16732: case 0x22:
16733: fatalerror("int 22h (terminate address)\n");
16734: case 0x23:
1.1.1.28 root 16735: try {
16736: msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
16737: } catch(...) {
16738: fatalerror("failed to terminate the current process by int 23h\n");
16739: }
1.1 root 16740: break;
16741: case 0x24:
1.1.1.32 root 16742: /*
1.1.1.28 root 16743: try {
16744: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
16745: } catch(...) {
16746: fatalerror("failed to terminate the current process by int 24h\n");
16747: }
1.1.1.32 root 16748: */
16749: msdos_int_24h();
1.1 root 16750: break;
16751: case 0x25:
16752: msdos_int_25h();
16753: break;
16754: case 0x26:
16755: msdos_int_26h();
16756: break;
16757: case 0x27:
1.1.1.28 root 16758: try {
16759: msdos_int_27h();
16760: } catch(...) {
16761: fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
16762: }
1.1 root 16763: break;
16764: case 0x28:
16765: Sleep(10);
1.1.1.35 root 16766: REQUEST_HARDWRE_UPDATE();
1.1 root 16767: break;
16768: case 0x29:
16769: msdos_int_29h();
16770: break;
16771: case 0x2e:
16772: msdos_int_2eh();
16773: break;
16774: case 0x2f:
16775: // multiplex interrupt
16776: switch(REG8(AH)) {
1.1.1.22 root 16777: case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44 root 16778: case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22 root 16779: case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21 root 16780: case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30 root 16781: case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22 root 16782: case 0x14: msdos_int_2fh_14h(); break;
16783: case 0x15: msdos_int_2fh_15h(); break;
1.1 root 16784: case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22 root 16785: case 0x19: msdos_int_2fh_19h(); break;
1.1 root 16786: case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30 root 16787: case 0x40: msdos_int_2fh_40h(); break;
1.1 root 16788: case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22 root 16789: case 0x46: msdos_int_2fh_46h(); break;
16790: case 0x48: msdos_int_2fh_48h(); break;
1.1 root 16791: case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22 root 16792: case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44 root 16793: case 0x4d: msdos_int_2fh_4dh(); break;
1.1 root 16794: case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22 root 16795: case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44 root 16796: case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24 root 16797: case 0xad: msdos_int_2fh_adh(); break;
1.1 root 16798: case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34 root 16799: case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43 root 16800: default:
1.1.1.30 root 16801: switch(REG8(AL)) {
16802: case 0x00:
16803: // This is not installed
16804: // REG8(AL) = 0x00;
16805: break;
1.1.1.33 root 16806: case 0x01:
1.1.1.42 root 16807: // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
16808: if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
16809: break;
16810: }
1.1.1.33 root 16811: // Banyan VINES v4+ is not installed
16812: if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
16813: break;
16814: }
1.1.1.42 root 16815: // Quarterdeck QDPMI.SYS v1.0 is not installed
16816: if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
16817: break;
16818: }
1.1.1.30 root 16819: default:
1.1.1.42 root 16820: // NORTON UTILITIES 5.0+
16821: if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
16822: break;
16823: }
1.1.1.30 root 16824: 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 16825: REG16(AX) = 0x01; // invalid function
1.1.1.30 root 16826: m_CF = 1;
16827: break;
16828: }
16829: break;
1.1 root 16830: }
16831: break;
1.1.1.24 root 16832: case 0x33:
16833: switch(REG8(AH)) {
16834: case 0x00:
16835: // Mouse
1.1.1.49 root 16836: switch(REG16(AX)) {
16837: case 0x0000: msdos_int_33h_0000h(); break;
16838: case 0x0001: msdos_int_33h_0001h(); break;
16839: case 0x0002: msdos_int_33h_0002h(); break;
16840: case 0x0003: msdos_int_33h_0003h(); break;
16841: case 0x0004: msdos_int_33h_0004h(); break;
16842: case 0x0005: msdos_int_33h_0005h(); break;
16843: case 0x0006: msdos_int_33h_0006h(); break;
16844: case 0x0007: msdos_int_33h_0007h(); break;
16845: case 0x0008: msdos_int_33h_0008h(); break;
16846: case 0x0009: msdos_int_33h_0009h(); break;
16847: case 0x000a: msdos_int_33h_000ah(); break;
16848: case 0x000b: msdos_int_33h_000bh(); break;
16849: case 0x000c: msdos_int_33h_000ch(); break;
16850: case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
16851: case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
16852: case 0x000f: msdos_int_33h_000fh(); break;
16853: case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
16854: case 0x0011: msdos_int_33h_0011h(); break;
16855: case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
16856: case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
16857: case 0x0014: msdos_int_33h_0014h(); break;
16858: case 0x0015: msdos_int_33h_0015h(); break;
16859: case 0x0016: msdos_int_33h_0016h(); break;
16860: case 0x0017: msdos_int_33h_0017h(); break;
16861: case 0x0018: msdos_int_33h_0018h(); break;
16862: case 0x0019: msdos_int_33h_0019h(); break;
16863: case 0x001a: msdos_int_33h_001ah(); break;
16864: case 0x001b: msdos_int_33h_001bh(); break;
16865: case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
16866: case 0x001d: msdos_int_33h_001dh(); break;
16867: case 0x001e: msdos_int_33h_001eh(); break;
16868: case 0x001f: msdos_int_33h_001fh(); break;
16869: case 0x0020: msdos_int_33h_0020h(); break;
16870: case 0x0021: msdos_int_33h_0021h(); break;
16871: case 0x0022: msdos_int_33h_0022h(); break;
16872: case 0x0023: msdos_int_33h_0023h(); break;
16873: case 0x0024: msdos_int_33h_0024h(); break;
16874: case 0x0025: msdos_int_33h_0025h(); break;
16875: case 0x0026: msdos_int_33h_0026h(); break;
16876: case 0x0027: msdos_int_33h_0027h(); break;
16877: case 0x0028: msdos_int_33h_0028h(); break;
16878: case 0x0029: msdos_int_33h_0029h(); break;
16879: case 0x002a: msdos_int_33h_002ah(); break;
16880: // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
16881: // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
16882: // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
16883: // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
16884: case 0x002f: break; // Mouse Hardware Reset
16885: // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
16886: case 0x0031: msdos_int_33h_0031h(); break;
16887: case 0x0032: msdos_int_33h_0032h(); break;
16888: // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
16889: // 0x0034: MS MOUSE v8.0+ - Get Initialization File
16890: // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
16891: case 0x004d: msdos_int_33h_004dh(); break;
16892: case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24 root 16893: default:
16894: 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));
16895: break;
16896: }
16897: break;
16898: default:
16899: 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));
16900: break;
16901: }
16902: break;
1.1.1.50 root 16903: /*
16904: case 0x67:
16905: // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
16906: // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
16907: break;
16908: */
1.1.1.19 root 16909: case 0x68:
16910: // dummy interrupt for EMS (int 67h)
16911: switch(REG8(AH)) {
16912: case 0x40: msdos_int_67h_40h(); break;
16913: case 0x41: msdos_int_67h_41h(); break;
16914: case 0x42: msdos_int_67h_42h(); break;
16915: case 0x43: msdos_int_67h_43h(); break;
16916: case 0x44: msdos_int_67h_44h(); break;
16917: case 0x45: msdos_int_67h_45h(); break;
16918: case 0x46: msdos_int_67h_46h(); break;
16919: case 0x47: msdos_int_67h_47h(); break;
16920: case 0x48: msdos_int_67h_48h(); break;
1.1.1.20 root 16921: // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
16922: // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19 root 16923: case 0x4b: msdos_int_67h_4bh(); break;
16924: case 0x4c: msdos_int_67h_4ch(); break;
16925: case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20 root 16926: case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21 root 16927: case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20 root 16928: case 0x50: msdos_int_67h_50h(); break;
1.1.1.19 root 16929: case 0x51: msdos_int_67h_51h(); break;
1.1.1.20 root 16930: case 0x52: msdos_int_67h_52h(); break;
1.1.1.19 root 16931: case 0x53: msdos_int_67h_53h(); break;
16932: case 0x54: msdos_int_67h_54h(); break;
1.1.1.49 root 16933: case 0x55: msdos_int_67h_55h(); break;
16934: case 0x56: msdos_int_67h_56h(); break;
1.1.1.20 root 16935: case 0x57: msdos_int_67h_57h(); break;
16936: case 0x58: msdos_int_67h_58h(); break;
1.1.1.42 root 16937: case 0x59: msdos_int_67h_59h(); break;
1.1.1.20 root 16938: case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49 root 16939: case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43 root 16940: // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
16941: case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49 root 16942: case 0x70: msdos_int_67h_70h(); break;
1.1.1.31 root 16943: // 0xde: VCPI
1.1.1.30 root 16944: case 0xde: msdos_int_67h_deh(); break;
1.1.1.19 root 16945: default:
1.1.1.22 root 16946: 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 16947: REG8(AH) = 0x84;
16948: break;
16949: }
16950: break;
16951: #ifdef SUPPORT_XMS
16952: case 0x69:
16953: // dummy interrupt for XMS (call far)
1.1.1.28 root 16954: try {
16955: switch(REG8(AH)) {
16956: case 0x00: msdos_call_xms_00h(); break;
16957: case 0x01: msdos_call_xms_01h(); break;
16958: case 0x02: msdos_call_xms_02h(); break;
16959: case 0x03: msdos_call_xms_03h(); break;
16960: case 0x04: msdos_call_xms_04h(); break;
16961: case 0x05: msdos_call_xms_05h(); break;
16962: case 0x06: msdos_call_xms_06h(); break;
16963: case 0x07: msdos_call_xms_07h(); break;
16964: case 0x08: msdos_call_xms_08h(); break;
16965: case 0x09: msdos_call_xms_09h(); break;
16966: case 0x0a: msdos_call_xms_0ah(); break;
16967: case 0x0b: msdos_call_xms_0bh(); break;
16968: case 0x0c: msdos_call_xms_0ch(); break;
16969: case 0x0d: msdos_call_xms_0dh(); break;
16970: case 0x0e: msdos_call_xms_0eh(); break;
16971: case 0x0f: msdos_call_xms_0fh(); break;
16972: case 0x10: msdos_call_xms_10h(); break;
16973: case 0x11: msdos_call_xms_11h(); break;
16974: case 0x12: msdos_call_xms_12h(); break;
1.1.1.29 root 16975: #if defined(HAS_I386)
16976: case 0x88: msdos_call_xms_88h(); break;
16977: case 0x89: msdos_call_xms_89h(); break;
16978: case 0x8e: msdos_call_xms_8eh(); break;
16979: case 0x8f: msdos_call_xms_8fh(); break;
16980: #endif
1.1.1.28 root 16981: default:
16982: 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));
16983: REG16(AX) = 0x0000;
16984: REG8(BL) = 0x80; // function not implemented
16985: break;
16986: }
16987: } catch(...) {
1.1.1.19 root 16988: REG16(AX) = 0x0000;
1.1.1.28 root 16989: REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19 root 16990: }
16991: break;
16992: #endif
16993: case 0x6a:
1.1.1.24 root 16994: // irq12 (mouse)
16995: mouse_push_ax = REG16(AX);
16996: mouse_push_bx = REG16(BX);
16997: mouse_push_cx = REG16(CX);
16998: mouse_push_dx = REG16(DX);
16999: mouse_push_si = REG16(SI);
17000: mouse_push_di = REG16(DI);
17001:
1.1.1.43 root 17002: if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24 root 17003: REG16(AX) = mouse.status_irq;
17004: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 17005: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
17006: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24 root 17007: REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
17008: REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
17009:
1.1.1.49 root 17010: mem[DUMMY_TOP + 0x02] = 0x9a; // call far
17011: mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
17012: mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
17013: mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
17014: mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43 root 17015: break;
1.1.1.24 root 17016: }
1.1.1.43 root 17017: for(int i = 0; i < 8; i++) {
17018: if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
17019: REG16(AX) = mouse.status_irq_alt;
17020: REG16(BX) = mouse.get_buttons();
17021: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
17022: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
17023: REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
17024: REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
17025:
1.1.1.49 root 17026: mem[DUMMY_TOP + 0x02] = 0x9a; // call far
17027: mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
17028: mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
17029: mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
17030: mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43 root 17031: break;
17032: }
17033: }
17034: // invalid call addr :-(
1.1.1.49 root 17035: mem[DUMMY_TOP + 0x02] = 0x90; // nop
17036: mem[DUMMY_TOP + 0x03] = 0x90; // nop
17037: mem[DUMMY_TOP + 0x04] = 0x90; // nop
17038: mem[DUMMY_TOP + 0x05] = 0x90; // nop
17039: mem[DUMMY_TOP + 0x06] = 0x90; // nop
1.1.1.24 root 17040: break;
17041: case 0x6b:
17042: // end of irq12 (mouse)
17043: REG16(AX) = mouse_push_ax;
17044: REG16(BX) = mouse_push_bx;
17045: REG16(CX) = mouse_push_cx;
17046: REG16(DX) = mouse_push_dx;
17047: REG16(SI) = mouse_push_si;
17048: REG16(DI) = mouse_push_di;
17049:
17050: // EOI
17051: if((pic[1].isr &= ~(1 << 4)) == 0) {
17052: pic[0].isr &= ~(1 << 2); // master
17053: }
17054: pic_update();
17055: break;
17056: case 0x6c:
1.1.1.19 root 17057: // dummy interrupt for case map routine pointed in the country info
17058: if(REG8(AL) >= 0x80) {
17059: char tmp[2] = {0};
17060: tmp[0] = REG8(AL);
17061: my_strupr(tmp);
17062: REG8(AL) = tmp[0];
17063: }
17064: break;
1.1.1.27 root 17065: case 0x6d:
17066: // dummy interrupt for font read routine pointed by int 15h, ax=5000h
17067: REG8(AL) = 0x86; // not supported
17068: m_CF = 1;
17069: break;
1.1.1.32 root 17070: case 0x6e:
17071: // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
17072: {
17073: UINT16 code = REG16(AX);
17074: if(code & 0xf0) {
17075: code = (code & 7) | ((code & 0x10) >> 1);
17076: }
17077: for(int i = 0; i < array_length(param_error_table); i++) {
17078: if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
17079: const char *message = NULL;
17080: if(active_code_page == 932) {
17081: message = param_error_table[i].message_japanese;
17082: }
17083: if(message == NULL) {
17084: message = param_error_table[i].message_english;
17085: }
17086: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
17087: strcpy((char *)(mem + WORK_TOP + 1), message);
17088:
17089: SREG(ES) = WORK_TOP >> 4;
17090: i386_load_segment_descriptor(ES);
17091: REG16(DI) = 0x0000;
17092: break;
17093: }
17094: }
17095: }
17096: break;
1.1.1.49 root 17097: case 0x6f:
17098: // dummy interrupt for end of alter page map and call
17099: {
17100: UINT16 handles[4], pages[4];
17101:
17102: // pop old mapping data in new mapping
17103: for(int i = 0; i < 4; i++) {
17104: pages [3 - i] = i386_pop16();
17105: handles[3 - i] = i386_pop16();
17106: }
17107:
17108: // restore old mapping
17109: for(int i = 0; i < 4; i++) {
17110: UINT16 handle = handles[i];
17111: UINT16 page = pages [i];
17112:
17113: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
17114: ems_map_page(i, handle, page);
17115: } else {
17116: ems_unmap_page(i);
17117: }
17118: }
17119: // do ret_far (pop cs/ip) in old mapping
17120: }
17121: break;
1.1.1.8 root 17122: case 0x70:
17123: case 0x71:
17124: case 0x72:
17125: case 0x73:
17126: case 0x74:
17127: case 0x75:
17128: case 0x76:
17129: case 0x77:
17130: // EOI
17131: if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
17132: pic[0].isr &= ~(1 << 2); // master
17133: }
17134: pic_update();
17135: break;
1.1 root 17136: default:
1.1.1.22 root 17137: // 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 17138: break;
17139: }
17140:
17141: // update cursor position
17142: if(cursor_moved) {
1.1.1.36 root 17143: pcbios_update_cursor_position();
1.1 root 17144: cursor_moved = false;
17145: }
17146: }
17147:
17148: // init
17149:
17150: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
17151: {
17152: // init file handler
17153: memset(file_handler, 0, sizeof(file_handler));
17154: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
17155: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
17156: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21 root 17157: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 17158: if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21 root 17159: #else
17160: if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
17161: #endif
17162: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1 root 17163: }
1.1.1.21 root 17164: if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45 root 17165: // msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
17166: msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21 root 17167: }
1.1 root 17168: _dup2(0, DUP_STDIN);
17169: _dup2(1, DUP_STDOUT);
17170: _dup2(2, DUP_STDERR);
1.1.1.21 root 17171: _dup2(3, DUP_STDAUX);
17172: _dup2(4, DUP_STDPRN);
1.1 root 17173:
1.1.1.24 root 17174: // init mouse
17175: memset(&mouse, 0, sizeof(mouse));
1.1.1.34 root 17176: mouse.enabled = true; // from DOSBox
17177: mouse.hidden = 1; // hidden in default ???
17178: mouse.old_hidden = 1; // from DOSBox
17179: mouse.max_position.x = 8 * (scr_width - 1);
17180: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 17181: mouse.mickey.x = 8;
17182: mouse.mickey.y = 16;
17183:
1.1.1.26 root 17184: #ifdef SUPPORT_XMS
17185: // init xms
17186: msdos_xms_init();
17187: #endif
17188:
1.1 root 17189: // init process
17190: memset(process, 0, sizeof(process));
17191:
1.1.1.13 root 17192: // init dtainfo
17193: msdos_dta_info_init();
17194:
1.1 root 17195: // init memory
17196: memset(mem, 0, sizeof(mem));
17197:
17198: // bios data area
1.1.1.23 root 17199: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 17200: CONSOLE_SCREEN_BUFFER_INFO csbi;
17201: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14 root 17202: CONSOLE_FONT_INFO cfi;
17203: GetCurrentConsoleFont(hStdout, FALSE, &cfi);
17204:
17205: int regen = min(scr_width * scr_height * 2, 0x8000);
17206: text_vram_top_address = TEXT_VRAM_TOP;
17207: text_vram_end_address = text_vram_top_address + regen;
17208: shadow_buffer_top_address = SHADOW_BUF_TOP;
17209: shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51! root 17210: cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14 root 17211:
17212: if(regen > 0x4000) {
17213: regen = 0x8000;
17214: vram_pages = 1;
17215: } else if(regen > 0x2000) {
17216: regen = 0x4000;
17217: vram_pages = 2;
17218: } else if(regen > 0x1000) {
17219: regen = 0x2000;
17220: vram_pages = 4;
17221: } else {
17222: regen = 0x1000;
17223: vram_pages = 8;
17224: }
1.1 root 17225:
1.1.1.25 root 17226: *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
17227: *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29 root 17228: *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
17229: *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25 root 17230: *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37 root 17231: *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
17232: *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32 root 17233: #ifdef EXT_BIOS_TOP
1.1.1.25 root 17234: *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32 root 17235: #endif
1.1.1.26 root 17236: *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33 root 17237: *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41 root 17238: *(UINT16 *)(mem + 0x41a) = 0x1e;
17239: *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1 root 17240: *(UINT8 *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14 root 17241: *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
17242: *(UINT16 *)(mem + 0x44c) = regen;
1.1 root 17243: *(UINT16 *)(mem + 0x44e) = 0;
17244: *(UINT8 *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14 root 17245: *(UINT8 *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1 root 17246: *(UINT8 *)(mem + 0x460) = 7;
17247: *(UINT8 *)(mem + 0x461) = 7;
17248: *(UINT8 *)(mem + 0x462) = 0;
17249: *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19 root 17250: *(UINT8 *)(mem + 0x465) = 0x09;
17251: *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40 root 17252: *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41 root 17253: *(UINT16 *)(mem + 0x480) = 0x1e;
17254: *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14 root 17255: *(UINT8 *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
17256: *(UINT8 *)(mem + 0x485) = cfi.dwFontSize.Y;
17257: *(UINT8 *)(mem + 0x487) = 0x60;
17258: *(UINT8 *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32 root 17259: #ifdef EXT_BIOS_TOP
1.1.1.25 root 17260: *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32 root 17261: #endif
1.1.1.14 root 17262:
17263: // initial screen
17264: SMALL_RECT rect;
17265: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
17266: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
17267: for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
17268: for(int x = 0; x < scr_width; x++) {
17269: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
17270: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
17271: }
17272: }
1.1 root 17273:
1.1.1.19 root 17274: // init mcb
1.1 root 17275: int seg = MEMORY_TOP >> 4;
1.1.1.19 root 17276:
17277: // iret table
17278: // note: int 2eh vector should address the routine in command.com,
17279: // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
17280: // so move iret table into allocated memory block
17281: // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33 root 17282: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19 root 17283: IRET_TOP = seg << 4;
17284: seg += IRET_SIZE >> 4;
1.1.1.25 root 17285: memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19 root 17286:
17287: // dummy xms/ems device
1.1.1.33 root 17288: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19 root 17289: XMS_TOP = seg << 4;
17290: seg += XMS_SIZE >> 4;
17291:
17292: // environment
1.1.1.33 root 17293: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1 root 17294: int env_seg = seg;
17295: int ofs = 0;
1.1.1.32 root 17296: char env_append[ENV_SIZE] = {0}, append_added = 0;
17297: char comspec_added = 0;
1.1.1.33 root 17298: char lastdrive_added = 0;
1.1.1.32 root 17299: char env_msdos_path[ENV_SIZE] = {0};
17300: char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33 root 17301: char prompt_added = 0;
1.1.1.32 root 17302: char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33 root 17303: char tz_added = 0;
1.1.1.45 root 17304: const char *path, *short_path;
1.1.1.32 root 17305:
17306: if((path = getenv("MSDOS_APPEND")) != NULL) {
17307: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17308: strcpy(env_append, short_path);
17309: }
17310: }
17311: if((path = getenv("APPEND")) != NULL) {
17312: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17313: if(env_append[0] != '\0') {
17314: strcat(env_append, ";");
17315: }
17316: strcat(env_append, short_path);
17317: }
17318: }
17319:
17320: if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
17321: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17322: strcpy(comspec_path, short_path);
17323: }
17324: }
17325: if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
17326: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17327: strcpy(comspec_path, short_path);
17328: }
17329: }
1.1 root 17330:
1.1.1.28 root 17331: if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32 root 17332: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17333: strcpy(env_msdos_path, short_path);
17334: strcpy(env_path, short_path);
1.1.1.14 root 17335: }
17336: }
1.1.1.28 root 17337: if((path = getenv("PATH")) != NULL) {
1.1.1.32 root 17338: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17339: if(env_path[0] != '\0') {
17340: strcat(env_path, ";");
17341: }
17342: strcat(env_path, short_path);
1.1.1.9 root 17343: }
17344: }
1.1.1.32 root 17345:
17346: if(GetTempPath(ENV_SIZE, env_temp) != 0) {
17347: strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15 root 17348: }
1.1.1.32 root 17349: for(int i = 0; i < 4; i++) {
17350: static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
17351: if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
17352: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
17353: strcpy(env_temp, short_path);
17354: break;
17355: }
17356: }
1.1.1.24 root 17357: }
1.1.1.32 root 17358:
1.1.1.9 root 17359: for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1 root 17360: // lower to upper
1.1.1.28 root 17361: char tmp[ENV_SIZE], name[ENV_SIZE];
1.1 root 17362: strcpy(tmp, *p);
17363: for(int i = 0;; i++) {
17364: if(tmp[i] == '=') {
17365: tmp[i] = '\0';
17366: sprintf(name, ";%s;", tmp);
1.1.1.25 root 17367: my_strupr(name);
1.1 root 17368: tmp[i] = '=';
17369: break;
17370: } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28 root 17371: tmp[i] = (tmp[i] - 'a') + 'A';
1.1 root 17372: }
17373: }
1.1.1.33 root 17374: if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
17375: // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
17376: } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18 root 17377: // ignore non standard environments
17378: } else {
1.1.1.33 root 17379: if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32 root 17380: if(env_append[0] != '\0') {
17381: sprintf(tmp, "APPEND=%s", env_append);
17382: } else {
17383: sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
17384: }
17385: append_added = 1;
17386: } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14 root 17387: strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32 root 17388: comspec_added = 1;
1.1.1.33 root 17389: } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
17390: char *env = getenv("MSDOS_LASTDRIVE");
17391: if(env != NULL) {
17392: sprintf(tmp, "LASTDRIVE=%s", env);
17393: }
17394: lastdrive_added = 1;
17395: } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28 root 17396: if(env_msdos_path[0] != '\0') {
17397: sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
17398: } else {
17399: sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
17400: }
1.1.1.33 root 17401: } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28 root 17402: if(env_path[0] != '\0') {
17403: sprintf(tmp, "PATH=%s", env_path);
17404: } else {
17405: sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
17406: }
1.1.1.32 root 17407: path_added = 1;
1.1.1.33 root 17408: } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
17409: prompt_added = 1;
1.1.1.28 root 17410: } else if(strncmp(tmp, "TEMP=", 5) == 0) {
17411: if(env_temp[0] != '\0') {
17412: sprintf(tmp, "TEMP=%s", env_temp);
17413: } else {
17414: sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
17415: }
1.1.1.32 root 17416: temp_added = 1;
1.1.1.33 root 17417: } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28 root 17418: if(env_temp[0] != '\0') {
17419: sprintf(tmp, "TMP=%s", env_temp);
17420: } else {
17421: sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1 root 17422: }
1.1.1.32 root 17423: tmp_added = 1;
1.1.1.33 root 17424: } else if(strncmp(tmp, "TZ=", 3) == 0) {
17425: char *env = getenv("MSDOS_TZ");
17426: if(env != NULL) {
17427: sprintf(tmp, "TZ=%s", env);
17428: }
17429: tz_added = 1;
1.1 root 17430: }
17431: int len = strlen(tmp);
1.1.1.14 root 17432: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1 root 17433: fatalerror("too many environments\n");
17434: }
17435: memcpy(mem + (seg << 4) + ofs, tmp, len);
17436: ofs += len + 1;
17437: }
17438: }
1.1.1.32 root 17439: if(!append_added && env_append[0] != '\0') {
17440: #define SET_ENV(name, value) { \
17441: char tmp[ENV_SIZE]; \
17442: sprintf(tmp, "%s=%s", name, value); \
17443: int len = strlen(tmp); \
17444: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
17445: fatalerror("too many environments\n"); \
17446: } \
17447: memcpy(mem + (seg << 4) + ofs, tmp, len); \
17448: ofs += len + 1; \
17449: }
17450: SET_ENV("APPEND", env_append);
17451: }
17452: if(!comspec_added) {
17453: SET_ENV("COMSPEC", "C:\\COMMAND.COM");
17454: }
1.1.1.33 root 17455: if(!lastdrive_added) {
17456: SET_ENV("LASTDRIVE", "Z");
17457: }
1.1.1.32 root 17458: if(!path_added) {
17459: SET_ENV("PATH", env_path);
17460: }
1.1.1.33 root 17461: if(!prompt_added) {
17462: SET_ENV("PROMPT", "$P$G");
17463: }
1.1.1.32 root 17464: if(!temp_added) {
17465: SET_ENV("TEMP", env_temp);
17466: }
17467: if(!tmp_added) {
17468: SET_ENV("TMP", env_temp);
17469: }
1.1.1.33 root 17470: if(!tz_added) {
17471: TIME_ZONE_INFORMATION tzi;
17472: HKEY hKey, hSubKey;
17473: char tzi_std_name[64];
17474: char tz_std[8] = "GMT";
17475: char tz_dlt[8] = "GST";
17476: char tz_value[32];
17477:
17478: // timezone name from GetTimeZoneInformation may not be english
17479: bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
17480: setlocale(LC_CTYPE, "");
17481: wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
17482:
17483: // get english timezone name from registry
17484: if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
17485: for(DWORD i = 0; !tz_added; i++) {
17486: char reg_name[256], sub_key[1024], std_name[256];
17487: DWORD size;
17488: FILETIME ftTime;
17489: LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
17490:
17491: if(result == ERROR_SUCCESS) {
17492: sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
17493: if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
17494: if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
17495: // search english timezone name from table
1.1.1.37 root 17496: if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33 root 17497: for(int j = 0; j < array_length(tz_table); j++) {
17498: if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
17499: if(tz_table[j].std != NULL) {
17500: strcpy(tz_std, tz_table[j].std);
17501: }
17502: if(tz_table[j].dlt != NULL) {
17503: strcpy(tz_dlt, tz_table[j].dlt);
17504: }
17505: tz_added = 1;
17506: break;
17507: }
17508: }
17509: }
17510: }
17511: RegCloseKey(hSubKey);
17512: }
17513: } else if(result == ERROR_NO_MORE_ITEMS) {
17514: break;
17515: }
17516: }
17517: RegCloseKey(hKey);
17518: }
17519: if((tzi.Bias % 60) != 0) {
17520: sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
17521: } else {
17522: sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
17523: }
17524: if(daylight) {
17525: strcat(tz_value, tz_dlt);
17526: }
17527: SET_ENV("TZ", tz_value);
17528: }
1.1 root 17529: seg += (ENV_SIZE >> 4);
17530:
17531: // psp
1.1.1.33 root 17532: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1 root 17533: current_psp = seg;
1.1.1.35 root 17534: psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
17535: psp->parent_psp = current_psp;
1.1 root 17536: seg += (PSP_SIZE >> 4);
17537:
1.1.1.19 root 17538: // first free mcb in conventional memory
1.1.1.33 root 17539: msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8 root 17540: first_mcb = seg;
17541:
1.1.1.19 root 17542: // dummy mcb to link to umb
1.1.1.33 root 17543: #if 0
1.1.1.39 root 17544: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33 root 17545: #else
1.1.1.39 root 17546: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33 root 17547: #endif
1.1.1.19 root 17548:
17549: // first free mcb in upper memory block
1.1.1.8 root 17550: msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1 root 17551:
1.1.1.29 root 17552: #ifdef SUPPORT_HMA
17553: // first free mcb in high memory area
17554: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
17555: #endif
17556:
1.1.1.26 root 17557: // interrupt vector
17558: for(int i = 0; i < 0x80; i++) {
17559: *(UINT16 *)(mem + 4 * i + 0) = i;
17560: *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
17561: }
1.1.1.49 root 17562: *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018; // fffc:0018 irq0 (system timer)
17563: *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26 root 17564: *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000; // ffff:0000 boot
17565: *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
17566: *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012; // xxxx:0012 ems
17567: *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49 root 17568: *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000; // fffc:0000 irq12 (mouse)
17569: *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26 root 17570:
1.1.1.29 root 17571: // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26 root 17572: static const struct {
17573: UINT16 attributes;
17574: char *dev_name;
17575: } dummy_devices[] = {
17576: {0x8013, "CON "},
17577: {0x8000, "AUX "},
17578: {0xa0c0, "PRN "},
17579: {0x8008, "CLOCK$ "},
17580: {0x8000, "COM1 "},
17581: {0xa0c0, "LPT1 "},
17582: {0xa0c0, "LPT2 "},
17583: {0xa0c0, "LPT3 "},
17584: {0x8000, "COM2 "},
17585: {0x8000, "COM3 "},
17586: {0x8000, "COM4 "},
1.1.1.30 root 17587: // {0xc000, "CONFIG$ "},
17588: {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26 root 17589: };
17590: static const UINT8 dummy_device_routine[] = {
17591: // from NUL device of Windows 98 SE
17592: // or word ptr ES:[BX+03],0100
17593: 0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
17594: // retf
17595: 0xcb,
17596: };
1.1.1.29 root 17597: device_t *last = NULL;
1.1.1.32 root 17598: for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26 root 17599: device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29 root 17600: device->next_driver.w.l = 22 + 18 * (i + 1);
17601: device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 17602: device->attributes = dummy_devices[i].attributes;
1.1.1.29 root 17603: device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
17604: device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26 root 17605: memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29 root 17606: last = device;
17607: }
17608: if(last != NULL) {
17609: last->next_driver.w.l = 0;
17610: last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26 root 17611: }
1.1.1.29 root 17612: memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26 root 17613:
1.1.1.25 root 17614: // dos info
17615: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
17616: dos_info->magic_word = 1;
17617: dos_info->first_mcb = MEMORY_TOP >> 4;
17618: dos_info->first_dpb.w.l = 0;
17619: dos_info->first_dpb.w.h = DPB_TOP >> 4;
17620: dos_info->first_sft.w.l = 0;
17621: dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41 root 17622: dos_info->clock_device.w.l = 22 + 18 * 3; // CLOCK$ is the 4th device in IO.SYS
1.1.1.25 root 17623: dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 17624: dos_info->con_device.w.l = 22 + 18 * 0; // CON is the 1st device in IO.SYS
1.1.1.25 root 17625: dos_info->con_device.w.h = DEVICE_TOP >> 4;
17626: dos_info->max_sector_len = 512;
17627: dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
17628: dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
17629: dos_info->cds.w.l = 0;
17630: dos_info->cds.w.h = CDS_TOP >> 4;
17631: dos_info->fcb_table.w.l = 0;
17632: dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
17633: dos_info->last_drive = 'Z' - 'A' + 1;
17634: dos_info->buffers_x = 20;
17635: dos_info->buffers_y = 0;
17636: dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29 root 17637: dos_info->nul_device.next_driver.w.l = 22;
17638: dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25 root 17639: dos_info->nul_device.attributes = 0x8004;
1.1.1.29 root 17640: dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
17641: dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 17642: memcpy(dos_info->nul_device.dev_name, "NUL ", 8);
17643: dos_info->disk_buf_heads.w.l = 0;
17644: dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39 root 17645: dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25 root 17646: dos_info->first_umb_fcb = UMB_TOP >> 4;
17647: dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29 root 17648: memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25 root 17649:
17650: char *env;
17651: if((env = getenv("LASTDRIVE")) != NULL) {
17652: if(env[0] >= 'A' && env[0] <= 'Z') {
17653: dos_info->last_drive = env[0] - 'A' + 1;
17654: } else if(env[0] >= 'a' && env[0] <= 'z') {
17655: dos_info->last_drive = env[0] - 'a' + 1;
17656: }
17657: }
17658: if((env = getenv("windir")) != NULL) {
17659: if(env[0] >= 'A' && env[0] <= 'Z') {
17660: dos_info->boot_drive = env[0] - 'A' + 1;
17661: } else if(env[0] >= 'a' && env[0] <= 'z') {
17662: dos_info->boot_drive = env[0] - 'a' + 1;
17663: }
17664: }
17665: #if defined(HAS_I386)
17666: dos_info->i386_or_later = 1;
17667: #else
17668: dos_info->i386_or_later = 0;
17669: #endif
17670: dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
17671:
1.1.1.27 root 17672: // ems (int 67h) and xms
1.1.1.25 root 17673: device_t *xms_device = (device_t *)(mem + XMS_TOP);
17674: xms_device->next_driver.w.l = 0xffff;
17675: xms_device->next_driver.w.h = 0xffff;
17676: xms_device->attributes = 0xc000;
1.1.1.29 root 17677: xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
17678: xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 17679: memcpy(xms_device->dev_name, "EMMXXXX0", 8);
17680:
1.1.1.26 root 17681: mem[XMS_TOP + 0x12] = 0xcd; // int 68h (dummy)
17682: mem[XMS_TOP + 0x13] = 0x68;
17683: mem[XMS_TOP + 0x14] = 0xcf; // iret
1.1.1.19 root 17684: #ifdef SUPPORT_XMS
17685: if(support_xms) {
1.1.1.26 root 17686: mem[XMS_TOP + 0x15] = 0xcd; // int 69h (dummy)
17687: mem[XMS_TOP + 0x16] = 0x69;
17688: mem[XMS_TOP + 0x17] = 0xcb; // retf
1.1.1.19 root 17689: } else
17690: #endif
1.1.1.26 root 17691: mem[XMS_TOP + 0x15] = 0xcb; // retf
1.1.1.29 root 17692: memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19 root 17693:
1.1.1.26 root 17694: // irq12 routine (mouse)
1.1.1.49 root 17695: mem[DUMMY_TOP + 0x00] = 0xcd; // int 6ah (dummy)
17696: mem[DUMMY_TOP + 0x01] = 0x6a;
17697: mem[DUMMY_TOP + 0x02] = 0x9a; // call far mouse
17698: mem[DUMMY_TOP + 0x03] = 0xff;
17699: mem[DUMMY_TOP + 0x04] = 0xff;
17700: mem[DUMMY_TOP + 0x05] = 0xff;
17701: mem[DUMMY_TOP + 0x06] = 0xff;
17702: mem[DUMMY_TOP + 0x07] = 0xcd; // int 6bh (dummy)
17703: mem[DUMMY_TOP + 0x08] = 0x6b;
17704: mem[DUMMY_TOP + 0x09] = 0xcf; // iret
1.1.1.24 root 17705:
1.1.1.27 root 17706: // case map routine
1.1.1.49 root 17707: mem[DUMMY_TOP + 0x0a] = 0xcd; // int 6ch (dummy)
17708: mem[DUMMY_TOP + 0x0b] = 0x6c;
17709: mem[DUMMY_TOP + 0x0c] = 0xcb; // retf
1.1.1.27 root 17710:
17711: // font read routine
1.1.1.49 root 17712: mem[DUMMY_TOP + 0x0d] = 0xcd; // int 6dh (dummy)
17713: mem[DUMMY_TOP + 0x0e] = 0x6d;
17714: mem[DUMMY_TOP + 0x0f] = 0xcb; // retf
1.1.1.19 root 17715:
1.1.1.32 root 17716: // error message read routine
1.1.1.49 root 17717: mem[DUMMY_TOP + 0x10] = 0xcd; // int 6eh (dummy)
17718: mem[DUMMY_TOP + 0x11] = 0x6e;
17719: mem[DUMMY_TOP + 0x12] = 0xcb; // retf
1.1.1.32 root 17720:
1.1.1.35 root 17721: // dummy loop to wait BIOS/DOS service is done
1.1.1.49 root 17722: mem[DUMMY_TOP + 0x13] = 0xe6; // out f7h, al
17723: mem[DUMMY_TOP + 0x14] = 0xf7;
17724: mem[DUMMY_TOP + 0x15] = 0x78; // js/jns -4
17725: mem[DUMMY_TOP + 0x16] = 0xfc;
17726: mem[DUMMY_TOP + 0x17] = 0xcb; // retf
1.1.1.35 root 17727:
1.1.1.50 root 17728: // irq0 routine (system timer)
1.1.1.49 root 17729: mem[DUMMY_TOP + 0x18] = 0xcd; // int 1ch
17730: mem[DUMMY_TOP + 0x19] = 0x1c;
17731: mem[DUMMY_TOP + 0x1a] = 0xea; // jmp far (IRET_TOP >> 4):0008
17732: mem[DUMMY_TOP + 0x1b] = 0x08;
17733: mem[DUMMY_TOP + 0x1c] = 0x00;
17734: mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4) ) & 0xff;
17735: mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
17736:
17737: // alter page map and call routine
17738: mem[DUMMY_TOP + 0x1f] = 0x9a; // call far
17739: mem[DUMMY_TOP + 0x20] = 0xff;
17740: mem[DUMMY_TOP + 0x21] = 0xff;
17741: mem[DUMMY_TOP + 0x22] = 0xff;
17742: mem[DUMMY_TOP + 0x23] = 0xff;
17743: mem[DUMMY_TOP + 0x24] = 0xcd; // int 6fh (dummy)
17744: mem[DUMMY_TOP + 0x25] = 0x6f;
17745: mem[DUMMY_TOP + 0x26] = 0xcb; // retf
1.1.1.14 root 17746:
1.1.1.50 root 17747: // call int 29h routine
17748: mem[DUMMY_TOP + 0x27] = 0xcd; // int 29h
17749: mem[DUMMY_TOP + 0x28] = 0x29;
17750: mem[DUMMY_TOP + 0x29] = 0xcb; // retf
17751:
1.1.1.26 root 17752: // boot routine
1.1.1.49 root 17753: mem[0xffff0 + 0x00] = 0xf4; // halt
17754: mem[0xffff0 + 0x05] = '0'; // rom date
17755: mem[0xffff0 + 0x06] = '2';
17756: mem[0xffff0 + 0x07] = '/';
17757: mem[0xffff0 + 0x08] = '2';
17758: mem[0xffff0 + 0x09] = '2';
17759: mem[0xffff0 + 0x0a] = '/';
17760: mem[0xffff0 + 0x0b] = '0';
17761: mem[0xffff0 + 0x0c] = '6';
17762: mem[0xffff0 + 0x0e] = 0xfc; // machine id
17763: mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24 root 17764:
1.1 root 17765: // param block
17766: // + 0: param block (22bytes)
17767: // +24: fcb1/2 (20bytes)
17768: // +44: command tail (128bytes)
17769: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
17770: param->env_seg = 0;
17771: param->cmd_line.w.l = 44;
17772: param->cmd_line.w.h = (WORK_TOP >> 4);
17773: param->fcb1.w.l = 24;
17774: param->fcb1.w.h = (WORK_TOP >> 4);
17775: param->fcb2.w.l = 24;
17776: param->fcb2.w.h = (WORK_TOP >> 4);
17777:
17778: memset(mem + WORK_TOP + 24, 0x20, 20);
17779:
17780: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
17781: if(argc > 1) {
17782: sprintf(cmd_line->cmd, " %s", argv[1]);
17783: for(int i = 2; i < argc; i++) {
17784: char tmp[128];
17785: sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
17786: strcpy(cmd_line->cmd, tmp);
17787: }
17788: cmd_line->len = (UINT8)strlen(cmd_line->cmd);
17789: } else {
17790: cmd_line->len = 0;
17791: }
17792: cmd_line->cmd[cmd_line->len] = 0x0d;
17793:
17794: // system file table
1.1.1.21 root 17795: *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
17796: *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1 root 17797:
1.1.1.19 root 17798: // disk buffer header (from DOSBox)
17799: *(UINT16 *)(mem + DISK_BUF_TOP + 0) = 0xffff; // forward ptr
17800: *(UINT16 *)(mem + DISK_BUF_TOP + 2) = 0xffff; // backward ptr
17801: *(UINT8 *)(mem + DISK_BUF_TOP + 4) = 0xff; // not in use
17802: *(UINT8 *)(mem + DISK_BUF_TOP + 10) = 0x01; // number of FATs
17803: *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff; // pointer to DPB
17804:
1.1 root 17805: // fcb table
17806: *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14 root 17807: *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1 root 17808:
1.1.1.41 root 17809: // drive parameter block
1.1.1.42 root 17810: for(int i = 0; i < 2; i++) {
1.1.1.43 root 17811: // may be a floppy drive
1.1.1.44 root 17812: cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
17813: sprintf(cds->path_name, "%c:\\", 'A' + i);
17814: cds->drive_attrib = 0x4000; // physical drive
17815: cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
17816: cds->dpb_ptr.w.h = DPB_TOP >> 4;
17817: cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
17818: cds->bs_offset = 2;
17819:
1.1.1.41 root 17820: dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
17821: dpb->drive_num = i;
17822: dpb->unit_num = i;
1.1.1.43 root 17823: dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
17824: dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42 root 17825: }
17826: for(int i = 2; i < 26; i++) {
1.1.1.44 root 17827: msdos_cds_update(i);
1.1.1.42 root 17828: UINT16 seg, ofs;
17829: msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41 root 17830: }
17831:
1.1.1.17 root 17832: // nls stuff
17833: msdos_nls_tables_init();
1.1 root 17834:
17835: // execute command
1.1.1.28 root 17836: try {
17837: if(msdos_process_exec(argv[0], param, 0)) {
17838: fatalerror("'%s' not found\n", argv[0]);
17839: }
17840: } catch(...) {
17841: // we should not reach here :-(
17842: fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1 root 17843: }
17844: retval = 0;
17845: return(0);
17846: }
17847:
17848: #define remove_std_file(path) { \
17849: int fd = _open(path, _O_RDONLY | _O_BINARY); \
17850: if(fd != -1) { \
17851: _lseek(fd, 0, SEEK_END); \
17852: int size = _tell(fd); \
17853: _close(fd); \
17854: if(size == 0) { \
17855: remove(path); \
17856: } \
17857: } \
17858: }
17859:
17860: void msdos_finish()
17861: {
17862: for(int i = 0; i < MAX_FILES; i++) {
17863: if(file_handler[i].valid) {
17864: _close(i);
17865: }
17866: }
1.1.1.21 root 17867: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 17868: remove_std_file("stdaux.txt");
1.1.1.21 root 17869: #endif
1.1.1.30 root 17870: #ifdef SUPPORT_XMS
17871: msdos_xms_finish();
17872: #endif
1.1 root 17873: msdos_dbcs_table_finish();
17874: }
17875:
17876: /* ----------------------------------------------------------------------------
17877: PC/AT hardware emulation
17878: ---------------------------------------------------------------------------- */
17879:
17880: void hardware_init()
17881: {
1.1.1.3 root 17882: CPU_INIT_CALL(CPU_MODEL);
1.1 root 17883: CPU_RESET_CALL(CPU_MODEL);
1.1.1.14 root 17884: m_IF = 1;
1.1.1.3 root 17885: #if defined(HAS_I386)
1.1 root 17886: cpu_type = (REG32(EDX) >> 8) & 0x0f;
17887: cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3 root 17888: #endif
17889: i386_set_a20_line(0);
1.1.1.14 root 17890:
1.1.1.19 root 17891: ems_init();
1.1.1.25 root 17892: dma_init();
1.1 root 17893: pic_init();
1.1.1.25 root 17894: pio_init();
1.1.1.8 root 17895: #ifdef PIT_ALWAYS_RUNNING
17896: pit_init();
17897: #else
1.1 root 17898: pit_active = 0;
17899: #endif
1.1.1.25 root 17900: sio_init();
1.1.1.8 root 17901: cmos_init();
17902: kbd_init();
1.1 root 17903: }
17904:
1.1.1.10 root 17905: void hardware_finish()
17906: {
17907: #if defined(HAS_I386)
17908: vtlb_free(m_vtlb);
17909: #endif
1.1.1.19 root 17910: ems_finish();
1.1.1.37 root 17911: pio_finish();
1.1.1.25 root 17912: sio_finish();
1.1.1.10 root 17913: }
17914:
1.1.1.28 root 17915: void hardware_release()
17916: {
17917: // release hardware resources when this program will be terminated abnormally
17918: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 17919: if(fp_debug_log != NULL) {
17920: fclose(fp_debug_log);
17921: fp_debug_log = NULL;
1.1.1.28 root 17922: }
17923: #endif
17924: #if defined(HAS_I386)
17925: vtlb_free(m_vtlb);
17926: #endif
17927: ems_release();
1.1.1.37 root 17928: pio_release();
1.1.1.28 root 17929: sio_release();
17930: }
17931:
1.1 root 17932: void hardware_run()
17933: {
1.1.1.22 root 17934: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28 root 17935: // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33 root 17936: fp_debug_log = fopen("debug.log", "w");
1.1.1.22 root 17937: #endif
1.1.1.51! root 17938: #ifdef USE_DEBUGGER
! 17939: m_int_num = -1;
! 17940: #endif
1.1.1.3 root 17941: while(!m_halted) {
1.1.1.50 root 17942: hardware_run_cpu();
1.1 root 17943: }
1.1.1.22 root 17944: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 17945: if(fp_debug_log != NULL) {
17946: fclose(fp_debug_log);
17947: fp_debug_log = NULL;
1.1.1.28 root 17948: }
1.1.1.22 root 17949: #endif
1.1 root 17950: }
17951:
1.1.1.50 root 17952: inline void hardware_run_cpu()
17953: {
17954: #if defined(HAS_I386)
17955: CPU_EXECUTE_CALL(i386);
17956: if(m_eip != m_prev_eip) {
17957: idle_ops++;
17958: }
17959: #else
17960: CPU_EXECUTE_CALL(CPU_MODEL);
17961: if(m_pc != m_prevpc) {
17962: idle_ops++;
17963: }
17964: #endif
17965: #ifdef USE_DEBUGGER
17966: // Disallow reentering CPU_EXECUTE() in msdos_syscall()
17967: if(m_int_num >= 0) {
17968: unsigned num = (unsigned)m_int_num;
17969: m_int_num = -1;
17970: msdos_syscall(num);
17971: }
17972: #endif
17973: if(++update_ops == UPDATE_OPS) {
17974: update_ops = 0;
17975: hardware_update();
17976: }
17977: }
17978:
1.1 root 17979: void hardware_update()
17980: {
1.1.1.8 root 17981: static UINT32 prev_time = 0;
17982: UINT32 cur_time = timeGetTime();
17983:
17984: if(prev_time != cur_time) {
17985: // update pit and raise irq0
17986: #ifndef PIT_ALWAYS_RUNNING
17987: if(pit_active)
17988: #endif
17989: {
17990: if(pit_run(0, cur_time)) {
17991: pic_req(0, 0, 1);
17992: }
17993: pit_run(1, cur_time);
17994: pit_run(2, cur_time);
17995: }
1.1.1.24 root 17996:
1.1.1.25 root 17997: // update sio and raise irq4/3
1.1.1.29 root 17998: for(int c = 0; c < 4; c++) {
1.1.1.25 root 17999: sio_update(c);
18000: }
18001:
1.1.1.24 root 18002: // update keyboard and mouse
1.1.1.14 root 18003: static UINT32 prev_tick = 0;
18004: UINT32 cur_tick = cur_time / 32;
1.1.1.25 root 18005:
1.1.1.14 root 18006: if(prev_tick != cur_tick) {
18007: // update keyboard flags
18008: UINT8 state;
1.1.1.24 root 18009: state = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
18010: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
18011: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
18012: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
18013: state |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
18014: state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
18015: state |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
18016: state |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
1.1.1.14 root 18017: mem[0x417] = state;
18018: state = (GetAsyncKeyState(VK_INSERT ) & 0x8000) ? 0x80 : 0;
18019: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
18020: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
18021: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
1.1.1.24 root 18022: // state |= (GetAsyncKeyState(VK_PAUSE ) & 0x0001) ? 0x08 : 0;
18023: // state |= (GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x04 : 0;
1.1.1.14 root 18024: state |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
18025: state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
18026: mem[0x418] = state;
18027:
1.1.1.24 root 18028: // update console input if needed
1.1.1.34 root 18029: if(!key_changed || mouse.hidden == 0) {
1.1.1.24 root 18030: update_console_input();
18031: }
18032:
18033: // raise irq1 if key is pressed/released
18034: if(key_changed) {
1.1.1.8 root 18035: pic_req(0, 1, 1);
1.1.1.24 root 18036: key_changed = false;
18037: }
18038:
18039: // raise irq12 if mouse status is changed
1.1.1.43 root 18040: if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
18041: mouse.status_irq = mouse.status & mouse.call_mask;
18042: mouse.status_irq_alt = 0; // ???
1.1.1.24 root 18043: mouse.status &= ~mouse.call_mask;
1.1.1.43 root 18044: pic_req(1, 4, 1);
18045: } else {
18046: for(int i = 0; i < 8; i++) {
18047: if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
18048: mouse.status_irq = 0; // ???
18049: mouse.status_irq_alt = 0;
18050: for(int j = 0; j < 8; j++) {
18051: if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
18052: mouse.status_irq_alt |= (1 << j);
18053: mouse.status_alt &= ~(1 << j);
18054: }
18055: }
18056: pic_req(1, 4, 1);
18057: break;
18058: }
18059: }
1.1.1.8 root 18060: }
1.1.1.24 root 18061:
1.1.1.14 root 18062: prev_tick = cur_tick;
1.1.1.8 root 18063: }
1.1.1.24 root 18064:
1.1.1.19 root 18065: // update daily timer counter
18066: pcbios_update_daily_timer_counter(cur_time);
1.1.1.25 root 18067:
1.1.1.8 root 18068: prev_time = cur_time;
1.1 root 18069: }
18070: }
18071:
1.1.1.19 root 18072: // ems
18073:
18074: void ems_init()
18075: {
18076: memset(ems_handles, 0, sizeof(ems_handles));
18077: memset(ems_pages, 0, sizeof(ems_pages));
18078: free_ems_pages = MAX_EMS_PAGES;
18079: }
18080:
18081: void ems_finish()
18082: {
1.1.1.28 root 18083: ems_release();
18084: }
18085:
18086: void ems_release()
18087: {
1.1.1.31 root 18088: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28 root 18089: if(ems_handles[i].buffer != NULL) {
1.1.1.19 root 18090: free(ems_handles[i].buffer);
18091: ems_handles[i].buffer = NULL;
18092: }
18093: }
18094: }
18095:
18096: void ems_allocate_pages(int handle, int pages)
18097: {
18098: if(pages > 0) {
18099: ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
18100: } else {
18101: ems_handles[handle].buffer = NULL;
18102: }
18103: ems_handles[handle].pages = pages;
18104: ems_handles[handle].allocated = true;
18105: free_ems_pages -= pages;
18106: }
18107:
18108: void ems_reallocate_pages(int handle, int pages)
18109: {
18110: if(ems_handles[handle].allocated) {
18111: if(ems_handles[handle].pages != pages) {
18112: UINT8 *new_buffer = NULL;
18113:
18114: if(pages > 0) {
18115: new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
18116: }
1.1.1.32 root 18117: if(ems_handles[handle].buffer != NULL) {
18118: if(new_buffer != NULL) {
1.1.1.19 root 18119: if(pages > ems_handles[handle].pages) {
18120: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
18121: } else {
18122: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
18123: }
18124: }
18125: free(ems_handles[handle].buffer);
18126: ems_handles[handle].buffer = NULL;
18127: }
18128: free_ems_pages += ems_handles[handle].pages;
18129:
18130: ems_handles[handle].buffer = new_buffer;
18131: ems_handles[handle].pages = pages;
18132: free_ems_pages -= pages;
18133: }
18134: } else {
18135: ems_allocate_pages(handle, pages);
18136: }
18137: }
18138:
18139: void ems_release_pages(int handle)
18140: {
18141: if(ems_handles[handle].allocated) {
1.1.1.32 root 18142: if(ems_handles[handle].buffer != NULL) {
1.1.1.19 root 18143: free(ems_handles[handle].buffer);
18144: ems_handles[handle].buffer = NULL;
18145: }
18146: free_ems_pages += ems_handles[handle].pages;
18147: ems_handles[handle].allocated = false;
18148: }
18149: }
18150:
18151: void ems_map_page(int physical, int handle, int logical)
18152: {
18153: if(ems_pages[physical].mapped) {
18154: if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
18155: return;
18156: }
18157: ems_unmap_page(physical);
18158: }
1.1.1.32 root 18159: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 18160: memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
18161: }
18162: ems_pages[physical].handle = handle;
18163: ems_pages[physical].page = logical;
18164: ems_pages[physical].mapped = true;
18165: }
18166:
18167: void ems_unmap_page(int physical)
18168: {
18169: if(ems_pages[physical].mapped) {
18170: int handle = ems_pages[physical].handle;
18171: int logical = ems_pages[physical].page;
18172:
1.1.1.32 root 18173: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 18174: memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
18175: }
18176: ems_pages[physical].mapped = false;
18177: }
18178: }
18179:
1.1.1.25 root 18180: // dma
1.1 root 18181:
1.1.1.25 root 18182: void dma_init()
1.1 root 18183: {
1.1.1.26 root 18184: memset(dma, 0, sizeof(dma));
1.1.1.25 root 18185: for(int c = 0; c < 2; c++) {
1.1.1.26 root 18186: // for(int ch = 0; ch < 4; ch++) {
18187: // dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
18188: // }
1.1.1.25 root 18189: dma_reset(c);
18190: }
1.1 root 18191: }
18192:
1.1.1.25 root 18193: void dma_reset(int c)
1.1 root 18194: {
1.1.1.25 root 18195: dma[c].low_high = false;
18196: dma[c].cmd = dma[c].req = dma[c].tc = 0;
18197: dma[c].mask = 0xff;
18198: }
18199:
18200: void dma_write(int c, UINT32 addr, UINT8 data)
18201: {
18202: int ch = (addr >> 1) & 3;
18203: UINT8 bit = 1 << (data & 3);
18204:
18205: switch(addr & 0x0f) {
18206: case 0x00: case 0x02: case 0x04: case 0x06:
18207: if(dma[c].low_high) {
18208: dma[c].ch[ch].bareg.b.h = data;
1.1 root 18209: } else {
1.1.1.25 root 18210: dma[c].ch[ch].bareg.b.l = data;
18211: }
18212: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
18213: dma[c].low_high = !dma[c].low_high;
18214: break;
18215: case 0x01: case 0x03: case 0x05: case 0x07:
18216: if(dma[c].low_high) {
18217: dma[c].ch[ch].bcreg.b.h = data;
18218: } else {
18219: dma[c].ch[ch].bcreg.b.l = data;
18220: }
18221: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
18222: dma[c].low_high = !dma[c].low_high;
18223: break;
18224: case 0x08:
18225: // command register
18226: dma[c].cmd = data;
18227: break;
18228: case 0x09:
18229: // dma[c].request register
18230: if(data & 4) {
18231: if(!(dma[c].req & bit)) {
18232: dma[c].req |= bit;
18233: // dma_run(c, ch);
18234: }
18235: } else {
18236: dma[c].req &= ~bit;
18237: }
18238: break;
18239: case 0x0a:
18240: // single mask register
18241: if(data & 4) {
18242: dma[c].mask |= bit;
18243: } else {
18244: dma[c].mask &= ~bit;
18245: }
18246: break;
18247: case 0x0b:
18248: // mode register
18249: dma[c].ch[data & 3].mode = data;
18250: break;
18251: case 0x0c:
18252: dma[c].low_high = false;
18253: break;
18254: case 0x0d:
18255: // clear master
18256: dma_reset(c);
18257: break;
18258: case 0x0e:
18259: // clear mask register
18260: dma[c].mask = 0;
18261: break;
18262: case 0x0f:
18263: // all mask register
18264: dma[c].mask = data & 0x0f;
18265: break;
18266: }
18267: }
18268:
18269: UINT8 dma_read(int c, UINT32 addr)
18270: {
18271: int ch = (addr >> 1) & 3;
18272: UINT8 val = 0xff;
18273:
18274: switch(addr & 0x0f) {
18275: case 0x00: case 0x02: case 0x04: case 0x06:
18276: if(dma[c].low_high) {
18277: val = dma[c].ch[ch].areg.b.h;
18278: } else {
18279: val = dma[c].ch[ch].areg.b.l;
18280: }
18281: dma[c].low_high = !dma[c].low_high;
18282: return(val);
18283: case 0x01: case 0x03: case 0x05: case 0x07:
18284: if(dma[c].low_high) {
18285: val = dma[c].ch[ch].creg.b.h;
18286: } else {
18287: val = dma[c].ch[ch].creg.b.l;
18288: }
18289: dma[c].low_high = !dma[c].low_high;
18290: return(val);
18291: case 0x08:
18292: // status register
18293: val = (dma[c].req << 4) | dma[c].tc;
18294: dma[c].tc = 0;
18295: return(val);
18296: case 0x0d:
1.1.1.26 root 18297: // temporary register (intel 82374 does not support)
1.1.1.25 root 18298: return(dma[c].tmp & 0xff);
1.1.1.26 root 18299: case 0x0f:
18300: // mask register (intel 82374 does support)
18301: return(dma[c].mask);
1.1.1.25 root 18302: }
18303: return(0xff);
18304: }
18305:
18306: void dma_page_write(int c, int ch, UINT8 data)
18307: {
18308: dma[c].ch[ch].pagereg = data;
18309: }
18310:
18311: UINT8 dma_page_read(int c, int ch)
18312: {
18313: return(dma[c].ch[ch].pagereg);
18314: }
18315:
18316: void dma_run(int c, int ch)
18317: {
18318: UINT8 bit = 1 << ch;
18319:
18320: if((dma[c].req & bit) && !(dma[c].mask & bit)) {
18321: // execute dma
18322: while(dma[c].req & bit) {
18323: if(ch == 0 && (dma[c].cmd & 0x01)) {
18324: // memory -> memory
18325: UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
18326: UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
18327:
18328: if(c == 0) {
18329: dma[c].tmp = read_byte(saddr);
18330: write_byte(daddr, dma[c].tmp);
18331: } else {
18332: dma[c].tmp = read_word(saddr << 1);
18333: write_word(daddr << 1, dma[c].tmp);
18334: }
18335: if(!(dma[c].cmd & 0x02)) {
18336: if(dma[c].ch[0].mode & 0x20) {
18337: dma[c].ch[0].areg.w--;
18338: if(dma[c].ch[0].areg.w == 0xffff) {
18339: dma[c].ch[0].pagereg--;
18340: }
18341: } else {
18342: dma[c].ch[0].areg.w++;
18343: if(dma[c].ch[0].areg.w == 0) {
18344: dma[c].ch[0].pagereg++;
18345: }
18346: }
18347: }
18348: if(dma[c].ch[1].mode & 0x20) {
18349: dma[c].ch[1].areg.w--;
18350: if(dma[c].ch[1].areg.w == 0xffff) {
18351: dma[c].ch[1].pagereg--;
18352: }
18353: } else {
18354: dma[c].ch[1].areg.w++;
18355: if(dma[c].ch[1].areg.w == 0) {
18356: dma[c].ch[1].pagereg++;
18357: }
18358: }
18359:
18360: // check dma condition
18361: if(dma[c].ch[0].creg.w-- == 0) {
18362: if(dma[c].ch[0].mode & 0x10) {
18363: // self initialize
18364: dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
18365: dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
18366: } else {
18367: // dma[c].mask |= bit;
18368: }
18369: }
18370: if(dma[c].ch[1].creg.w-- == 0) {
18371: // terminal count
18372: if(dma[c].ch[1].mode & 0x10) {
18373: // self initialize
18374: dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
18375: dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
18376: } else {
18377: dma[c].mask |= bit;
18378: }
18379: dma[c].req &= ~bit;
18380: dma[c].tc |= bit;
18381: }
18382: } else {
18383: UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
18384:
18385: if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
18386: // verify
18387: } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
18388: // io -> memory
18389: if(c == 0) {
18390: dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
18391: write_byte(addr, dma[c].tmp);
18392: } else {
18393: dma[c].tmp = read_io_word(dma[c].ch[ch].port);
18394: write_word(addr << 1, dma[c].tmp);
18395: }
18396: } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
18397: // memory -> io
18398: if(c == 0) {
18399: dma[c].tmp = read_byte(addr);
18400: write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
18401: } else {
18402: dma[c].tmp = read_word(addr << 1);
18403: write_io_word(dma[c].ch[ch].port, dma[c].tmp);
18404: }
18405: }
18406: if(dma[c].ch[ch].mode & 0x20) {
18407: dma[c].ch[ch].areg.w--;
18408: if(dma[c].ch[ch].areg.w == 0xffff) {
18409: dma[c].ch[ch].pagereg--;
18410: }
18411: } else {
18412: dma[c].ch[ch].areg.w++;
18413: if(dma[c].ch[ch].areg.w == 0) {
18414: dma[c].ch[ch].pagereg++;
18415: }
18416: }
18417:
18418: // check dma condition
18419: if(dma[c].ch[ch].creg.w-- == 0) {
18420: // terminal count
18421: if(dma[c].ch[ch].mode & 0x10) {
18422: // self initialize
18423: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
18424: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
18425: } else {
18426: dma[c].mask |= bit;
18427: }
18428: dma[c].req &= ~bit;
18429: dma[c].tc |= bit;
18430: } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
18431: // single mode
18432: break;
18433: }
18434: }
18435: }
18436: }
18437: }
18438:
18439: // pic
18440:
18441: void pic_init()
18442: {
18443: memset(pic, 0, sizeof(pic));
18444: pic[0].imr = pic[1].imr = 0xff;
18445:
18446: // from bochs bios
18447: pic_write(0, 0, 0x11); // icw1 = 11h
18448: pic_write(0, 1, 0x08); // icw2 = 08h
18449: pic_write(0, 1, 0x04); // icw3 = 04h
18450: pic_write(0, 1, 0x01); // icw4 = 01h
18451: pic_write(0, 1, 0xb8); // ocw1 = b8h
18452: pic_write(1, 0, 0x11); // icw1 = 11h
18453: pic_write(1, 1, 0x70); // icw2 = 70h
18454: pic_write(1, 1, 0x02); // icw3 = 02h
18455: pic_write(1, 1, 0x01); // icw4 = 01h
18456: }
18457:
18458: void pic_write(int c, UINT32 addr, UINT8 data)
18459: {
18460: if(addr & 1) {
18461: if(pic[c].icw2_r) {
18462: // icw2
18463: pic[c].icw2 = data;
18464: pic[c].icw2_r = 0;
18465: } else if(pic[c].icw3_r) {
18466: // icw3
18467: pic[c].icw3 = data;
18468: pic[c].icw3_r = 0;
18469: } else if(pic[c].icw4_r) {
18470: // icw4
18471: pic[c].icw4 = data;
18472: pic[c].icw4_r = 0;
18473: } else {
18474: // ocw1
1.1 root 18475: pic[c].imr = data;
18476: }
18477: } else {
18478: if(data & 0x10) {
18479: // icw1
18480: pic[c].icw1 = data;
18481: pic[c].icw2_r = 1;
18482: pic[c].icw3_r = (data & 2) ? 0 : 1;
18483: pic[c].icw4_r = data & 1;
18484: pic[c].irr = 0;
18485: pic[c].isr = 0;
18486: pic[c].imr = 0;
18487: pic[c].prio = 0;
18488: if(!(pic[c].icw1 & 1)) {
18489: pic[c].icw4 = 0;
18490: }
18491: pic[c].ocw3 = 0;
18492: } else if(data & 8) {
18493: // ocw3
18494: if(!(data & 2)) {
18495: data = (data & ~1) | (pic[c].ocw3 & 1);
18496: }
18497: if(!(data & 0x40)) {
18498: data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
18499: }
18500: pic[c].ocw3 = data;
18501: } else {
18502: // ocw2
18503: int level = 0;
18504: if(data & 0x40) {
18505: level = data & 7;
18506: } else {
18507: if(!pic[c].isr) {
18508: return;
18509: }
18510: level = pic[c].prio;
18511: while(!(pic[c].isr & (1 << level))) {
18512: level = (level + 1) & 7;
18513: }
18514: }
18515: if(data & 0x80) {
18516: pic[c].prio = (level + 1) & 7;
18517: }
18518: if(data & 0x20) {
18519: pic[c].isr &= ~(1 << level);
18520: }
18521: }
18522: }
18523: pic_update();
18524: }
18525:
18526: UINT8 pic_read(int c, UINT32 addr)
18527: {
18528: if(addr & 1) {
18529: return(pic[c].imr);
18530: } else {
18531: // polling mode is not supported...
18532: //if(pic[c].ocw3 & 4) {
18533: // return ???;
18534: //}
18535: if(pic[c].ocw3 & 1) {
18536: return(pic[c].isr);
18537: } else {
18538: return(pic[c].irr);
18539: }
18540: }
18541: }
18542:
18543: void pic_req(int c, int level, int signal)
18544: {
18545: if(signal) {
18546: pic[c].irr |= (1 << level);
18547: } else {
18548: pic[c].irr &= ~(1 << level);
18549: }
18550: pic_update();
18551: }
18552:
18553: int pic_ack()
18554: {
18555: // ack (INTA=L)
18556: pic[pic_req_chip].isr |= pic_req_bit;
18557: pic[pic_req_chip].irr &= ~pic_req_bit;
18558: if(pic_req_chip > 0) {
18559: // update isr and irr of master
18560: UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
18561: pic[pic_req_chip - 1].isr |= slave;
18562: pic[pic_req_chip - 1].irr &= ~slave;
18563: }
18564: //if(pic[pic_req_chip].icw4 & 1) {
18565: // 8086 mode
18566: int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
18567: //} else {
18568: // // 8080 mode
18569: // UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
18570: // if(pic[pic_req_chip].icw1 & 4) {
18571: // addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
18572: // } else {
18573: // addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
18574: // }
18575: // vector = 0xcd | (addr << 8);
18576: //}
18577: if(pic[pic_req_chip].icw4 & 2) {
18578: // auto eoi
18579: pic[pic_req_chip].isr &= ~pic_req_bit;
18580: }
18581: return(vector);
18582: }
18583:
18584: void pic_update()
18585: {
18586: for(int c = 0; c < 2; c++) {
18587: UINT8 irr = pic[c].irr;
18588: if(c + 1 < 2) {
18589: // this is master
18590: if(pic[c + 1].irr & (~pic[c + 1].imr)) {
18591: // request from slave
18592: irr |= 1 << (pic[c + 1].icw3 & 7);
18593: }
18594: }
18595: irr &= (~pic[c].imr);
18596: if(!irr) {
18597: break;
18598: }
18599: if(!(pic[c].ocw3 & 0x20)) {
18600: irr |= pic[c].isr;
18601: }
18602: int level = pic[c].prio;
18603: UINT8 bit = 1 << level;
18604: while(!(irr & bit)) {
18605: level = (level + 1) & 7;
18606: bit = 1 << level;
18607: }
18608: if((c + 1 < 2) && (pic[c].icw3 & bit)) {
18609: // check slave
18610: continue;
18611: }
18612: if(pic[c].isr & bit) {
18613: break;
18614: }
18615: // interrupt request
18616: pic_req_chip = c;
18617: pic_req_level = level;
18618: pic_req_bit = bit;
1.1.1.3 root 18619: i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1 root 18620: return;
18621: }
1.1.1.3 root 18622: i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2 root 18623: }
1.1 root 18624:
1.1.1.25 root 18625: // pio
18626:
18627: void pio_init()
18628: {
1.1.1.38 root 18629: // bool conv_mode = (GetConsoleCP() == 932);
18630:
1.1.1.26 root 18631: memset(pio, 0, sizeof(pio));
1.1.1.37 root 18632:
1.1.1.25 root 18633: for(int c = 0; c < 2; c++) {
1.1.1.37 root 18634: pio[c].stat = 0xdf;
1.1.1.25 root 18635: pio[c].ctrl = 0x0c;
1.1.1.38 root 18636: // pio[c].conv_mode = conv_mode;
1.1.1.25 root 18637: }
18638: }
18639:
1.1.1.37 root 18640: void pio_finish()
18641: {
18642: pio_release();
18643: }
18644:
18645: void pio_release()
18646: {
18647: for(int c = 0; c < 2; c++) {
18648: if(pio[c].fp != NULL) {
1.1.1.38 root 18649: if(pio[c].jis_mode) {
18650: fputc(0x1c, pio[c].fp);
18651: fputc(0x2e, pio[c].fp);
18652: }
1.1.1.37 root 18653: fclose(pio[c].fp);
18654: pio[c].fp = NULL;
18655: }
18656: }
18657: }
18658:
1.1.1.25 root 18659: void pio_write(int c, UINT32 addr, UINT8 data)
18660: {
18661: switch(addr & 3) {
18662: case 0:
18663: pio[c].data = data;
18664: break;
18665: case 2:
1.1.1.37 root 18666: if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
18667: // strobe H -> L
18668: if(pio[c].data == 0x0d && (data & 0x02)) {
18669: // auto feed
18670: printer_out(c, 0x0d);
18671: printer_out(c, 0x0a);
18672: } else {
18673: printer_out(c, pio[c].data);
18674: }
18675: pio[c].stat &= ~0x40; // set ack
18676: }
1.1.1.25 root 18677: pio[c].ctrl = data;
18678: break;
18679: }
18680: }
18681:
18682: UINT8 pio_read(int c, UINT32 addr)
18683: {
18684: switch(addr & 3) {
18685: case 0:
1.1.1.37 root 18686: if(pio[c].ctrl & 0x20) {
18687: // input mode
18688: return(0xff);
18689: }
1.1.1.25 root 18690: return(pio[c].data);
18691: case 1:
1.1.1.37 root 18692: {
18693: UINT8 stat = pio[c].stat;
18694: pio[c].stat |= 0x40; // clear ack
18695: return(stat);
18696: }
1.1.1.25 root 18697: case 2:
18698: return(pio[c].ctrl);
18699: }
18700: return(0xff);
18701: }
18702:
1.1.1.37 root 18703: void printer_out(int c, UINT8 data)
18704: {
18705: SYSTEMTIME time;
1.1.1.38 root 18706: bool jis_mode = false;
1.1.1.37 root 18707:
18708: GetLocalTime(&time);
18709:
18710: if(pio[c].fp != NULL) {
18711: // if at least 1000ms passed from last written, close the current file
18712: FILETIME ftime1;
18713: FILETIME ftime2;
18714: SystemTimeToFileTime(&pio[c].time, &ftime1);
18715: SystemTimeToFileTime(&time, &ftime2);
18716: INT64 *time1 = (INT64 *)&ftime1;
18717: INT64 *time2 = (INT64 *)&ftime2;
18718: INT64 msec = (*time2 - *time1) / 10000;
18719:
18720: if(msec >= 1000) {
1.1.1.38 root 18721: if(pio[c].jis_mode) {
18722: fputc(0x1c, pio[c].fp);
18723: fputc(0x2e, pio[c].fp);
18724: jis_mode = true;
18725: }
1.1.1.37 root 18726: fclose(pio[c].fp);
18727: pio[c].fp = NULL;
18728: }
18729: }
18730: if(pio[c].fp == NULL) {
18731: // create a new file in the temp folder
18732: char file_name[MAX_PATH];
18733:
18734: 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);
18735: if(GetTempPath(MAX_PATH, pio[c].path)) {
18736: strcat(pio[c].path, file_name);
18737: } else {
18738: strcpy(pio[c].path, file_name);
18739: }
1.1.1.38 root 18740: pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37 root 18741: }
18742: if(pio[c].fp != NULL) {
1.1.1.38 root 18743: if(jis_mode) {
18744: fputc(0x1c, pio[c].fp);
18745: fputc(0x26, pio[c].fp);
18746: }
1.1.1.37 root 18747: fputc(data, pio[c].fp);
1.1.1.38 root 18748:
18749: // reopen file if 1ch 26h 1ch 2eh (kanji-on kanji-off) are written at the top
18750: if(data == 0x2e && ftell(pio[c].fp) == 4) {
18751: UINT8 buffer[4];
18752: fseek(pio[c].fp, 0, SEEK_SET);
18753: fread(buffer, 4, 1, pio[c].fp);
18754: if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
18755: fclose(pio[c].fp);
18756: pio[c].fp = fopen(pio[c].path, "w+b");
18757: }
18758: }
1.1.1.37 root 18759: pio[c].time = time;
18760: }
18761: }
18762:
1.1 root 18763: // pit
18764:
1.1.1.22 root 18765: #define PIT_FREQ 1193182ULL
1.1 root 18766: #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)
18767:
18768: void pit_init()
18769: {
1.1.1.8 root 18770: memset(pit, 0, sizeof(pit));
1.1 root 18771: for(int ch = 0; ch < 3; ch++) {
18772: pit[ch].count = 0x10000;
18773: pit[ch].ctrl_reg = 0x34;
18774: pit[ch].mode = 3;
18775: }
18776:
18777: // from bochs bios
18778: pit_write(3, 0x34);
18779: pit_write(0, 0x00);
18780: pit_write(0, 0x00);
18781: }
18782:
18783: void pit_write(int ch, UINT8 val)
18784: {
1.1.1.8 root 18785: #ifndef PIT_ALWAYS_RUNNING
1.1 root 18786: if(!pit_active) {
18787: pit_active = 1;
18788: pit_init();
18789: }
1.1.1.8 root 18790: #endif
1.1 root 18791: switch(ch) {
18792: case 0:
18793: case 1:
18794: case 2:
18795: // write count register
18796: if(!pit[ch].low_write && !pit[ch].high_write) {
18797: if(pit[ch].ctrl_reg & 0x10) {
18798: pit[ch].low_write = 1;
18799: }
18800: if(pit[ch].ctrl_reg & 0x20) {
18801: pit[ch].high_write = 1;
18802: }
18803: }
18804: if(pit[ch].low_write) {
18805: pit[ch].count_reg = val;
18806: pit[ch].low_write = 0;
18807: } else if(pit[ch].high_write) {
18808: if((pit[ch].ctrl_reg & 0x30) == 0x20) {
18809: pit[ch].count_reg = val << 8;
18810: } else {
18811: pit[ch].count_reg |= val << 8;
18812: }
18813: pit[ch].high_write = 0;
18814: }
18815: // start count
1.1.1.8 root 18816: if(!pit[ch].low_write && !pit[ch].high_write) {
18817: if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
18818: pit[ch].count = PIT_COUNT_VALUE(ch);
18819: pit[ch].prev_time = timeGetTime();
18820: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 18821: }
18822: }
18823: break;
18824: case 3: // ctrl reg
18825: if((val & 0xc0) == 0xc0) {
18826: // i8254 read-back command
18827: for(ch = 0; ch < 3; ch++) {
18828: if(!(val & 0x10) && !pit[ch].status_latched) {
18829: pit[ch].status = pit[ch].ctrl_reg & 0x3f;
18830: pit[ch].status_latched = 1;
18831: }
18832: if(!(val & 0x20) && !pit[ch].count_latched) {
18833: pit_latch_count(ch);
18834: }
18835: }
18836: break;
18837: }
18838: ch = (val >> 6) & 3;
18839: if(val & 0x30) {
1.1.1.35 root 18840: static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1 root 18841: pit[ch].mode = modes[(val >> 1) & 7];
18842: pit[ch].count_latched = 0;
18843: pit[ch].low_read = pit[ch].high_read = 0;
18844: pit[ch].low_write = pit[ch].high_write = 0;
18845: pit[ch].ctrl_reg = val;
18846: // stop count
1.1.1.8 root 18847: pit[ch].prev_time = pit[ch].expired_time = 0;
1.1 root 18848: pit[ch].count_reg = 0;
18849: } else if(!pit[ch].count_latched) {
18850: pit_latch_count(ch);
18851: }
18852: break;
18853: }
18854: }
18855:
18856: UINT8 pit_read(int ch)
18857: {
1.1.1.8 root 18858: #ifndef PIT_ALWAYS_RUNNING
1.1 root 18859: if(!pit_active) {
18860: pit_active = 1;
18861: pit_init();
18862: }
1.1.1.8 root 18863: #endif
1.1 root 18864: switch(ch) {
18865: case 0:
18866: case 1:
18867: case 2:
18868: if(pit[ch].status_latched) {
18869: pit[ch].status_latched = 0;
18870: return(pit[ch].status);
18871: }
18872: // if not latched, through current count
18873: if(!pit[ch].count_latched) {
18874: if(!pit[ch].low_read && !pit[ch].high_read) {
18875: pit_latch_count(ch);
18876: }
18877: }
18878: // return latched count
18879: if(pit[ch].low_read) {
18880: pit[ch].low_read = 0;
18881: if(!pit[ch].high_read) {
18882: pit[ch].count_latched = 0;
18883: }
18884: return(pit[ch].latch & 0xff);
18885: } else if(pit[ch].high_read) {
18886: pit[ch].high_read = 0;
18887: pit[ch].count_latched = 0;
18888: return((pit[ch].latch >> 8) & 0xff);
18889: }
18890: }
18891: return(0xff);
18892: }
18893:
1.1.1.8 root 18894: int pit_run(int ch, UINT32 cur_time)
1.1 root 18895: {
1.1.1.8 root 18896: if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1 root 18897: pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8 root 18898: pit[ch].prev_time = pit[ch].expired_time;
18899: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
18900: if(cur_time >= pit[ch].expired_time) {
18901: pit[ch].prev_time = cur_time;
18902: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 18903: }
1.1.1.8 root 18904: return(1);
1.1 root 18905: }
1.1.1.8 root 18906: return(0);
1.1 root 18907: }
18908:
18909: void pit_latch_count(int ch)
18910: {
1.1.1.8 root 18911: if(pit[ch].expired_time != 0) {
1.1.1.26 root 18912: UINT32 cur_time = timeGetTime();
1.1.1.8 root 18913: pit_run(ch, cur_time);
18914: UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26 root 18915: UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
18916:
18917: if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
18918: // decrement counter in 1msec period
18919: if(pit[ch].next_latch == 0) {
18920: tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
18921: pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
18922: }
18923: if(pit[ch].latch > pit[ch].next_latch) {
18924: pit[ch].latch--;
18925: }
18926: } else {
18927: pit[ch].prev_latch = pit[ch].latch = latch;
18928: pit[ch].next_latch = 0;
18929: }
1.1.1.8 root 18930: } else {
18931: pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26 root 18932: pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1 root 18933: }
18934: pit[ch].count_latched = 1;
18935: if((pit[ch].ctrl_reg & 0x30) == 0x10) {
18936: // lower byte
18937: pit[ch].low_read = 1;
18938: pit[ch].high_read = 0;
18939: } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
18940: // upper byte
18941: pit[ch].low_read = 0;
18942: pit[ch].high_read = 1;
18943: } else {
18944: // lower -> upper
1.1.1.14 root 18945: pit[ch].low_read = pit[ch].high_read = 1;
1.1 root 18946: }
18947: }
18948:
1.1.1.8 root 18949: int pit_get_expired_time(int ch)
1.1 root 18950: {
1.1.1.22 root 18951: pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
18952: UINT64 val = pit[ch].accum >> 10;
18953: pit[ch].accum -= val << 10;
18954: return((val != 0) ? val : 1);
1.1.1.8 root 18955: }
18956:
1.1.1.25 root 18957: // sio
18958:
18959: void sio_init()
18960: {
1.1.1.26 root 18961: memset(sio, 0, sizeof(sio));
18962: memset(sio_mt, 0, sizeof(sio_mt));
18963:
1.1.1.29 root 18964: for(int c = 0; c < 4; c++) {
1.1.1.25 root 18965: sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
18966: sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
18967:
18968: sio[c].divisor.w = 12; // 115200Hz / 9600Baud
18969: sio[c].line_ctrl = 0x03; // 8bit, stop 1bit, non parity
1.1.1.26 root 18970: sio[c].modem_ctrl = 0x03; // rts=on, dtr=on
18971: sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25 root 18972: sio[c].line_stat_buf = 0x60; // send/recv buffers are empty
18973: sio[c].irq_identify = 0x01; // no pending irq
18974:
18975: InitializeCriticalSection(&sio_mt[c].csSendData);
18976: InitializeCriticalSection(&sio_mt[c].csRecvData);
18977: InitializeCriticalSection(&sio_mt[c].csLineCtrl);
18978: InitializeCriticalSection(&sio_mt[c].csLineStat);
18979: InitializeCriticalSection(&sio_mt[c].csModemCtrl);
18980: InitializeCriticalSection(&sio_mt[c].csModemStat);
18981:
1.1.1.26 root 18982: if(sio_port_number[c] != 0) {
1.1.1.25 root 18983: sio[c].channel = c;
18984: sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
18985: }
18986: }
18987: }
18988:
18989: void sio_finish()
18990: {
1.1.1.29 root 18991: for(int c = 0; c < 4; c++) {
1.1.1.25 root 18992: if(sio_mt[c].hThread != NULL) {
18993: WaitForSingleObject(sio_mt[c].hThread, INFINITE);
18994: CloseHandle(sio_mt[c].hThread);
1.1.1.28 root 18995: sio_mt[c].hThread = NULL;
1.1.1.25 root 18996: }
18997: DeleteCriticalSection(&sio_mt[c].csSendData);
18998: DeleteCriticalSection(&sio_mt[c].csRecvData);
18999: DeleteCriticalSection(&sio_mt[c].csLineCtrl);
19000: DeleteCriticalSection(&sio_mt[c].csLineStat);
19001: DeleteCriticalSection(&sio_mt[c].csModemCtrl);
19002: DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28 root 19003: }
19004: sio_release();
19005: }
19006:
19007: void sio_release()
19008: {
1.1.1.29 root 19009: for(int c = 0; c < 4; c++) {
1.1.1.28 root 19010: // sio_thread() may access the resources :-(
1.1.1.32 root 19011: bool running = (sio_mt[c].hThread != NULL);
19012:
19013: if(running) {
19014: EnterCriticalSection(&sio_mt[c].csSendData);
19015: }
19016: if(sio[c].send_buffer != NULL) {
19017: sio[c].send_buffer->release();
19018: delete sio[c].send_buffer;
19019: sio[c].send_buffer = NULL;
19020: }
19021: if(running) {
19022: LeaveCriticalSection(&sio_mt[c].csSendData);
19023: EnterCriticalSection(&sio_mt[c].csRecvData);
19024: }
19025: if(sio[c].recv_buffer != NULL) {
19026: sio[c].recv_buffer->release();
19027: delete sio[c].recv_buffer;
19028: sio[c].recv_buffer = NULL;
19029: }
19030: if(running) {
19031: LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28 root 19032: }
1.1.1.25 root 19033: }
19034: }
19035:
19036: void sio_write(int c, UINT32 addr, UINT8 data)
19037: {
19038: switch(addr & 7) {
19039: case 0:
19040: if(sio[c].selector & 0x80) {
19041: if(sio[c].divisor.b.l != data) {
19042: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19043: sio[c].divisor.b.l = data;
19044: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19045: }
19046: } else {
19047: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19048: if(sio[c].send_buffer != NULL) {
19049: sio[c].send_buffer->write(data);
19050: }
1.1.1.25 root 19051: // transmitter holding/shift registers are not empty
19052: sio[c].line_stat_buf &= ~0x60;
19053: LeaveCriticalSection(&sio_mt[c].csSendData);
19054:
19055: if(sio[c].irq_enable & 0x02) {
19056: sio_update_irq(c);
19057: }
19058: }
19059: break;
19060: case 1:
19061: if(sio[c].selector & 0x80) {
19062: if(sio[c].divisor.b.h != data) {
19063: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19064: sio[c].divisor.b.h = data;
19065: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19066: }
19067: } else {
19068: if(sio[c].irq_enable != data) {
19069: sio[c].irq_enable = data;
19070: sio_update_irq(c);
19071: }
19072: }
19073: break;
19074: case 3:
19075: {
19076: UINT8 line_ctrl = data & 0x3f;
19077: bool set_brk = ((data & 0x40) != 0);
19078:
19079: if(sio[c].line_ctrl != line_ctrl) {
19080: EnterCriticalSection(&sio_mt[c].csLineCtrl);
19081: sio[c].line_ctrl = line_ctrl;
19082: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
19083: }
19084: if(sio[c].set_brk != set_brk) {
19085: EnterCriticalSection(&sio_mt[c].csModemCtrl);
19086: sio[c].set_brk = set_brk;
19087: LeaveCriticalSection(&sio_mt[c].csModemCtrl);
19088: }
19089: }
19090: sio[c].selector = data;
19091: break;
19092: case 4:
19093: {
19094: bool set_dtr = ((data & 0x01) != 0);
19095: bool set_rts = ((data & 0x02) != 0);
19096:
19097: if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26 root 19098: // EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25 root 19099: sio[c].set_dtr = set_dtr;
19100: sio[c].set_rts = set_rts;
1.1.1.26 root 19101: // LeaveCriticalSection(&sio_mt[c].csModemCtrl);
19102:
19103: bool state_changed = false;
19104:
19105: EnterCriticalSection(&sio_mt[c].csModemStat);
19106: if(set_dtr) {
19107: sio[c].modem_stat |= 0x20; // dsr on
19108: } else {
19109: sio[c].modem_stat &= ~0x20; // dsr off
19110: }
19111: if(set_rts) {
19112: sio[c].modem_stat |= 0x10; // cts on
19113: } else {
19114: sio[c].modem_stat &= ~0x10; // cts off
19115: }
19116: if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
19117: if(!(sio[c].modem_stat & 0x02)) {
19118: if(sio[c].irq_enable & 0x08) {
19119: state_changed = true;
19120: }
19121: sio[c].modem_stat |= 0x02;
19122: }
19123: }
19124: if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
19125: if(!(sio[c].modem_stat & 0x01)) {
19126: if(sio[c].irq_enable & 0x08) {
19127: state_changed = true;
19128: }
19129: sio[c].modem_stat |= 0x01;
19130: }
19131: }
19132: LeaveCriticalSection(&sio_mt[c].csModemStat);
19133:
19134: if(state_changed) {
19135: sio_update_irq(c);
19136: }
1.1.1.25 root 19137: }
19138: }
19139: sio[c].modem_ctrl = data;
19140: break;
19141: case 7:
19142: sio[c].scratch = data;
19143: break;
19144: }
19145: }
19146:
19147: UINT8 sio_read(int c, UINT32 addr)
19148: {
19149: switch(addr & 7) {
19150: case 0:
19151: if(sio[c].selector & 0x80) {
19152: return(sio[c].divisor.b.l);
19153: } else {
19154: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19155: UINT8 data = 0;
19156: if(sio[c].recv_buffer != NULL) {
19157: data = sio[c].recv_buffer->read();
19158: }
1.1.1.25 root 19159: // data is not ready
19160: sio[c].line_stat_buf &= ~0x01;
19161: LeaveCriticalSection(&sio_mt[c].csRecvData);
19162:
19163: if(sio[c].irq_enable & 0x01) {
19164: sio_update_irq(c);
19165: }
19166: return(data);
19167: }
19168: case 1:
19169: if(sio[c].selector & 0x80) {
19170: return(sio[c].divisor.b.h);
19171: } else {
19172: return(sio[c].irq_enable);
19173: }
19174: case 2:
19175: return(sio[c].irq_identify);
19176: case 3:
19177: return(sio[c].selector);
19178: case 4:
19179: return(sio[c].modem_ctrl);
19180: case 5:
19181: {
19182: EnterCriticalSection(&sio_mt[c].csLineStat);
19183: UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
19184: sio[c].line_stat_err = 0x00;
19185: LeaveCriticalSection(&sio_mt[c].csLineStat);
19186:
19187: bool state_changed = false;
19188:
19189: if((sio[c].line_stat_buf & 0x60) == 0x00) {
19190: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19191: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 19192: // transmitter holding register will be empty first
19193: if(sio[c].irq_enable & 0x02) {
19194: state_changed = true;
19195: }
19196: sio[c].line_stat_buf |= 0x20;
19197: }
19198: LeaveCriticalSection(&sio_mt[c].csSendData);
19199: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
19200: // transmitter shift register will be empty later
19201: sio[c].line_stat_buf |= 0x40;
19202: }
19203: if(!(sio[c].line_stat_buf & 0x01)) {
19204: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19205: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 19206: // data is ready
19207: if(sio[c].irq_enable & 0x01) {
19208: state_changed = true;
19209: }
19210: sio[c].line_stat_buf |= 0x01;
19211: }
19212: LeaveCriticalSection(&sio_mt[c].csRecvData);
19213: }
19214: if(state_changed) {
19215: sio_update_irq(c);
19216: }
19217: return(val);
19218: }
19219: case 6:
19220: {
19221: EnterCriticalSection(&sio_mt[c].csModemStat);
19222: UINT8 val = sio[c].modem_stat;
19223: sio[c].modem_stat &= 0xf0;
19224: sio[c].prev_modem_stat = sio[c].modem_stat;
19225: LeaveCriticalSection(&sio_mt[c].csModemStat);
19226:
19227: if(sio[c].modem_ctrl & 0x10) {
19228: // loop-back
19229: val &= 0x0f;
19230: val |= (sio[c].modem_ctrl & 0x0c) << 4;
19231: val |= (sio[c].modem_ctrl & 0x01) << 5;
19232: val |= (sio[c].modem_ctrl & 0x02) << 3;
19233: }
19234: return(val);
19235: }
19236: case 7:
19237: return(sio[c].scratch);
19238: }
19239: return(0xff);
19240: }
19241:
19242: void sio_update(int c)
19243: {
19244: if((sio[c].line_stat_buf & 0x60) == 0x00) {
19245: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 19246: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 19247: // transmitter holding/shift registers will be empty
19248: sio[c].line_stat_buf |= 0x60;
19249: }
19250: LeaveCriticalSection(&sio_mt[c].csSendData);
19251: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
19252: // transmitter shift register will be empty
19253: sio[c].line_stat_buf |= 0x40;
19254: }
19255: if(!(sio[c].line_stat_buf & 0x01)) {
19256: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 19257: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 19258: // data is ready
19259: sio[c].line_stat_buf |= 0x01;
19260: }
19261: LeaveCriticalSection(&sio_mt[c].csRecvData);
19262: }
19263: sio_update_irq(c);
19264: }
19265:
19266: void sio_update_irq(int c)
19267: {
19268: int level = -1;
19269:
19270: if(sio[c].irq_enable & 0x08) {
19271: EnterCriticalSection(&sio_mt[c].csModemStat);
19272: if((sio[c].modem_stat & 0x0f) != 0) {
19273: level = 0;
19274: }
19275: EnterCriticalSection(&sio_mt[c].csModemStat);
19276: }
19277: if(sio[c].irq_enable & 0x02) {
19278: if(sio[c].line_stat_buf & 0x20) {
19279: level = 1;
19280: }
19281: }
19282: if(sio[c].irq_enable & 0x01) {
19283: if(sio[c].line_stat_buf & 0x01) {
19284: level = 2;
19285: }
19286: }
19287: if(sio[c].irq_enable & 0x04) {
19288: EnterCriticalSection(&sio_mt[c].csLineStat);
19289: if(sio[c].line_stat_err != 0) {
19290: level = 3;
19291: }
19292: LeaveCriticalSection(&sio_mt[c].csLineStat);
19293: }
1.1.1.29 root 19294:
19295: // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25 root 19296: if(level != -1) {
19297: sio[c].irq_identify = level << 1;
1.1.1.29 root 19298: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25 root 19299: } else {
19300: sio[c].irq_identify = 1;
1.1.1.29 root 19301: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25 root 19302: }
19303: }
19304:
19305: DWORD WINAPI sio_thread(void *lpx)
19306: {
19307: volatile sio_t *p = (sio_t *)lpx;
19308: sio_mt_t *q = &sio_mt[p->channel];
19309:
19310: char name[] = "COM1";
1.1.1.26 root 19311: name[3] = '0' + sio_port_number[p->channel];
19312: HANDLE hComm = NULL;
19313: COMMPROP commProp;
19314: DCB dcb;
19315: DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
19316: BYTE bytBuffer[SIO_BUFFER_SIZE];
19317:
19318: if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
19319: if(GetCommProperties(hComm, &commProp)) {
19320: dwSettableBaud = commProp.dwSettableBaud;
19321: }
1.1.1.25 root 19322: EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26 root 19323: // EscapeCommFunction(hComm, SETRTS);
19324: // EscapeCommFunction(hComm, SETDTR);
1.1.1.25 root 19325:
19326: while(!m_halted) {
19327: // setup comm port
19328: bool comm_state_changed = false;
19329:
19330: EnterCriticalSection(&q->csLineCtrl);
19331: if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
19332: p->prev_divisor = p->divisor.w;
19333: p->prev_line_ctrl = p->line_ctrl;
19334: comm_state_changed = true;
19335: }
19336: LeaveCriticalSection(&q->csLineCtrl);
19337:
19338: if(comm_state_changed) {
1.1.1.26 root 19339: if(GetCommState(hComm, &dcb)) {
19340: // dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
19341: DWORD baud = 115200 / p->prev_divisor;
19342: dcb.BaudRate = 9600; // default
19343:
19344: if((dwSettableBaud & BAUD_075 ) && baud >= 75 ) dcb.BaudRate = 75;
19345: if((dwSettableBaud & BAUD_110 ) && baud >= 110 ) dcb.BaudRate = 110;
19346: // 134.5bps is not supported ???
19347: // if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
19348: if((dwSettableBaud & BAUD_150 ) && baud >= 150 ) dcb.BaudRate = 150;
19349: if((dwSettableBaud & BAUD_300 ) && baud >= 300 ) dcb.BaudRate = 300;
19350: if((dwSettableBaud & BAUD_600 ) && baud >= 600 ) dcb.BaudRate = 600;
19351: if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
19352: if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
19353: if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
19354: if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
19355: if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
19356: if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
19357: // if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
19358: // if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
19359: // if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
19360:
19361: switch(p->prev_line_ctrl & 0x03) {
19362: case 0x00: dcb.ByteSize = 5; break;
19363: case 0x01: dcb.ByteSize = 6; break;
19364: case 0x02: dcb.ByteSize = 7; break;
19365: case 0x03: dcb.ByteSize = 8; break;
19366: }
19367: switch(p->prev_line_ctrl & 0x04) {
19368: case 0x00: dcb.StopBits = ONESTOPBIT; break;
19369: case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
19370: }
19371: switch(p->prev_line_ctrl & 0x38) {
19372: case 0x08: dcb.Parity = ODDPARITY; break;
19373: case 0x18: dcb.Parity = EVENPARITY; break;
19374: case 0x28: dcb.Parity = MARKPARITY; break;
19375: case 0x38: dcb.Parity = SPACEPARITY; break;
19376: default: dcb.Parity = NOPARITY; break;
19377: }
19378: dcb.fBinary = TRUE;
19379: dcb.fParity = (dcb.Parity != NOPARITY);
19380: dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
19381: dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
19382: dcb.fDsrSensitivity = FALSE;//TRUE;
19383: dcb.fTXContinueOnXoff = TRUE;
19384: dcb.fOutX = dcb.fInX = FALSE;
19385: dcb.fErrorChar = FALSE;
19386: dcb.fNull = FALSE;
19387: dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
19388: dcb.fAbortOnError = FALSE;
19389:
19390: SetCommState(hComm, &dcb);
1.1.1.25 root 19391: }
19392:
19393: // check again to apply all comm state changes
19394: Sleep(10);
19395: continue;
19396: }
19397:
19398: // set comm pins
19399: bool change_brk = false;
1.1.1.26 root 19400: // bool change_rts = false;
19401: // bool change_dtr = false;
1.1.1.25 root 19402:
19403: EnterCriticalSection(&q->csModemCtrl);
19404: if(p->prev_set_brk != p->set_brk) {
19405: p->prev_set_brk = p->set_brk;
19406: change_brk = true;
19407: }
1.1.1.26 root 19408: // if(p->prev_set_rts != p->set_rts) {
19409: // p->prev_set_rts = p->set_rts;
19410: // change_rts = true;
19411: // }
19412: // if(p->prev_set_dtr != p->set_dtr) {
19413: // p->prev_set_dtr = p->set_dtr;
19414: // change_dtr = true;
19415: // }
1.1.1.25 root 19416: LeaveCriticalSection(&q->csModemCtrl);
19417:
19418: if(change_brk) {
1.1.1.26 root 19419: static UINT32 clear_time = 0;
19420: if(p->prev_set_brk) {
19421: EscapeCommFunction(hComm, SETBREAK);
19422: clear_time = timeGetTime() + 200;
19423: } else {
19424: // keep break for at least 200msec
19425: UINT32 cur_time = timeGetTime();
19426: if(clear_time > cur_time) {
19427: Sleep(clear_time - cur_time);
19428: }
19429: EscapeCommFunction(hComm, CLRBREAK);
19430: }
1.1.1.25 root 19431: }
1.1.1.26 root 19432: // if(change_rts) {
19433: // if(p->prev_set_rts) {
19434: // EscapeCommFunction(hComm, SETRTS);
19435: // } else {
19436: // EscapeCommFunction(hComm, CLRRTS);
19437: // }
19438: // }
19439: // if(change_dtr) {
19440: // if(p->prev_set_dtr) {
19441: // EscapeCommFunction(hComm, SETDTR);
19442: // } else {
19443: // EscapeCommFunction(hComm, CLRDTR);
19444: // }
19445: // }
1.1.1.25 root 19446:
19447: // get comm pins
19448: DWORD dwModemStat = 0;
19449:
19450: if(GetCommModemStatus(hComm, &dwModemStat)) {
19451: EnterCriticalSection(&q->csModemStat);
19452: if(dwModemStat & MS_RLSD_ON) {
19453: p->modem_stat |= 0x80;
19454: } else {
19455: p->modem_stat &= ~0x80;
19456: }
19457: if(dwModemStat & MS_RING_ON) {
19458: p->modem_stat |= 0x40;
19459: } else {
19460: p->modem_stat &= ~0x40;
19461: }
1.1.1.26 root 19462: // if(dwModemStat & MS_DSR_ON) {
19463: // p->modem_stat |= 0x20;
19464: // } else {
19465: // p->modem_stat &= ~0x20;
19466: // }
19467: // if(dwModemStat & MS_CTS_ON) {
19468: // p->modem_stat |= 0x10;
19469: // } else {
19470: // p->modem_stat &= ~0x10;
19471: // }
1.1.1.25 root 19472: if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
19473: p->modem_stat |= 0x08;
19474: }
19475: if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
19476: p->modem_stat |= 0x04;
19477: }
1.1.1.26 root 19478: // if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
19479: // p->modem_stat |= 0x02;
19480: // }
19481: // if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
19482: // p->modem_stat |= 0x01;
19483: // }
1.1.1.25 root 19484: LeaveCriticalSection(&q->csModemStat);
19485: }
19486:
19487: // send data
19488: DWORD dwSend = 0;
19489:
19490: EnterCriticalSection(&q->csSendData);
1.1.1.32 root 19491: while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25 root 19492: bytBuffer[dwSend++] = p->send_buffer->read();
19493: }
19494: LeaveCriticalSection(&q->csSendData);
19495:
19496: if(dwSend != 0) {
19497: DWORD dwWritten = 0;
19498: WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
19499: }
19500:
19501: // get line status and recv data
19502: DWORD dwLineStat = 0;
19503: COMSTAT comStat;
19504:
19505: if(ClearCommError(hComm, &dwLineStat, &comStat)) {
19506: EnterCriticalSection(&q->csLineStat);
19507: if(dwLineStat & CE_BREAK) {
19508: p->line_stat_err |= 0x10;
19509: }
19510: if(dwLineStat & CE_FRAME) {
19511: p->line_stat_err |= 0x08;
19512: }
19513: if(dwLineStat & CE_RXPARITY) {
19514: p->line_stat_err |= 0x04;
19515: }
19516: if(dwLineStat & CE_OVERRUN) {
19517: p->line_stat_err |= 0x02;
19518: }
19519: LeaveCriticalSection(&q->csLineStat);
19520:
19521: if(comStat.cbInQue != 0) {
19522: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 19523: DWORD dwRecv = 0;
19524: if(p->recv_buffer != NULL) {
19525: dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
19526: }
1.1.1.25 root 19527: LeaveCriticalSection(&q->csRecvData);
19528:
19529: if(dwRecv != 0) {
19530: DWORD dwRead = 0;
19531: if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
19532: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 19533: if(p->recv_buffer != NULL) {
19534: for(int i = 0; i < dwRead; i++) {
19535: p->recv_buffer->write(bytBuffer[i]);
19536: }
1.1.1.25 root 19537: }
19538: LeaveCriticalSection(&q->csRecvData);
19539: }
19540: }
19541: }
19542: }
19543: Sleep(10);
19544: }
19545: CloseHandle(hComm);
19546: }
19547: return 0;
19548: }
19549:
1.1.1.8 root 19550: // cmos
19551:
19552: void cmos_init()
19553: {
19554: memset(cmos, 0, sizeof(cmos));
19555: cmos_addr = 0;
1.1 root 19556:
1.1.1.8 root 19557: // from DOSBox
19558: cmos_write(0x0a, 0x26);
19559: cmos_write(0x0b, 0x02);
19560: cmos_write(0x0d, 0x80);
1.1 root 19561: }
19562:
1.1.1.8 root 19563: void cmos_write(int addr, UINT8 val)
1.1 root 19564: {
1.1.1.8 root 19565: cmos[addr & 0x7f] = val;
19566: }
19567:
19568: #define CMOS_GET_TIME() { \
19569: UINT32 cur_sec = timeGetTime() / 1000 ; \
19570: if(prev_sec != cur_sec) { \
19571: GetLocalTime(&time); \
19572: prev_sec = cur_sec; \
19573: } \
1.1 root 19574: }
1.1.1.8 root 19575: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1 root 19576:
1.1.1.8 root 19577: UINT8 cmos_read(int addr)
1.1 root 19578: {
1.1.1.8 root 19579: static SYSTEMTIME time;
19580: static UINT32 prev_sec = 0;
1.1 root 19581:
1.1.1.8 root 19582: switch(addr & 0x7f) {
19583: case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
19584: case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
19585: case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
19586: case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
19587: case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
19588: case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
19589: case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
19590: // case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0)); // 2msec
19591: case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0)); // precision of timeGetTime() may not be 1msec
19592: case 0x15: return((MEMORY_END >> 10) & 0xff);
19593: case 0x16: return((MEMORY_END >> 18) & 0xff);
19594: case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
19595: case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
19596: case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
19597: case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
19598: case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1 root 19599: }
1.1.1.8 root 19600: return(cmos[addr & 0x7f]);
1.1 root 19601: }
19602:
1.1.1.7 root 19603: // kbd (a20)
19604:
19605: void kbd_init()
19606: {
1.1.1.8 root 19607: kbd_data = kbd_command = 0;
1.1.1.7 root 19608: kbd_status = 0x18;
19609: }
19610:
19611: UINT8 kbd_read_data()
19612: {
1.1.1.8 root 19613: kbd_status &= ~1;
1.1.1.7 root 19614: return(kbd_data);
19615: }
19616:
19617: void kbd_write_data(UINT8 val)
19618: {
19619: switch(kbd_command) {
19620: case 0xd1:
19621: i386_set_a20_line((val >> 1) & 1);
19622: break;
19623: }
19624: kbd_command = 0;
1.1.1.8 root 19625: kbd_status &= ~8;
1.1.1.7 root 19626: }
19627:
19628: UINT8 kbd_read_status()
19629: {
19630: return(kbd_status);
19631: }
19632:
19633: void kbd_write_command(UINT8 val)
19634: {
19635: switch(val) {
19636: case 0xd0:
19637: kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8 root 19638: kbd_status |= 1;
1.1.1.7 root 19639: break;
19640: case 0xdd:
19641: i386_set_a20_line(0);
19642: break;
19643: case 0xdf:
19644: i386_set_a20_line(1);
19645: break;
1.1.1.26 root 19646: case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
19647: case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7 root 19648: if(!(val & 1)) {
1.1.1.8 root 19649: if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7 root 19650: // reset pic
19651: pic_init();
19652: pic[0].irr = pic[1].irr = 0x00;
19653: pic[0].imr = pic[1].imr = 0xff;
19654: }
19655: CPU_RESET_CALL(CPU_MODEL);
1.1.1.40 root 19656: UINT16 address = *(UINT16 *)(mem + 0x467);
19657: UINT16 selector = *(UINT16 *)(mem + 0x469);
19658: i386_jmp_far(selector, address);
1.1.1.7 root 19659: }
19660: i386_set_a20_line((val >> 1) & 1);
19661: break;
19662: }
19663: kbd_command = val;
1.1.1.8 root 19664: kbd_status |= 8;
1.1.1.7 root 19665: }
19666:
1.1.1.9 root 19667: // vga
19668:
19669: UINT8 vga_read_status()
19670: {
19671: // 60hz
19672: static const int period[3] = {16, 17, 17};
19673: static int index = 0;
19674: UINT32 time = timeGetTime() % period[index];
19675:
19676: index = (index + 1) % 3;
1.1.1.14 root 19677: return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9 root 19678: }
19679:
1.1 root 19680: // i/o bus
19681:
1.1.1.29 root 19682: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
19683: //#define SW1US_PATCH
19684:
1.1.1.25 root 19685: UINT8 read_io_byte(offs_t addr)
1.1.1.33 root 19686: #ifdef USE_DEBUGGER
1.1.1.25 root 19687: {
1.1.1.33 root 19688: if(now_debugging) {
19689: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
19690: if(in_break_point.table[i].status == 1) {
19691: if(addr == in_break_point.table[i].addr) {
19692: in_break_point.hit = i + 1;
19693: now_suspended = true;
19694: break;
19695: }
19696: }
19697: }
1.1.1.25 root 19698: }
1.1.1.33 root 19699: return(debugger_read_io_byte(addr));
1.1.1.25 root 19700: }
1.1.1.33 root 19701: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25 root 19702: #endif
1.1 root 19703: {
1.1.1.33 root 19704: UINT8 val = 0xff;
19705:
1.1 root 19706: switch(addr) {
1.1.1.29 root 19707: #ifdef SW1US_PATCH
19708: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
19709: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33 root 19710: val = sio_read(0, addr - 1);
19711: break;
1.1.1.29 root 19712: #else
1.1.1.25 root 19713: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
19714: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33 root 19715: val = dma_read(0, addr);
19716: break;
1.1.1.29 root 19717: #endif
1.1.1.25 root 19718: case 0x20: case 0x21:
1.1.1.33 root 19719: val = pic_read(0, addr);
19720: break;
1.1.1.25 root 19721: case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33 root 19722: val = pit_read(addr & 0x03);
19723: break;
1.1.1.7 root 19724: case 0x60:
1.1.1.33 root 19725: val = kbd_read_data();
19726: break;
1.1.1.9 root 19727: case 0x61:
1.1.1.33 root 19728: val = system_port;
19729: break;
1.1.1.7 root 19730: case 0x64:
1.1.1.33 root 19731: val = kbd_read_status();
19732: break;
1.1 root 19733: case 0x71:
1.1.1.33 root 19734: val = cmos_read(cmos_addr);
19735: break;
1.1.1.25 root 19736: case 0x81:
1.1.1.33 root 19737: val = dma_page_read(0, 2);
19738: break;
1.1.1.25 root 19739: case 0x82:
1.1.1.33 root 19740: val = dma_page_read(0, 3);
19741: break;
1.1.1.25 root 19742: case 0x83:
1.1.1.33 root 19743: val = dma_page_read(0, 1);
19744: break;
1.1.1.25 root 19745: case 0x87:
1.1.1.33 root 19746: val = dma_page_read(0, 0);
19747: break;
1.1.1.25 root 19748: case 0x89:
1.1.1.33 root 19749: val = dma_page_read(1, 2);
19750: break;
1.1.1.25 root 19751: case 0x8a:
1.1.1.33 root 19752: val = dma_page_read(1, 3);
19753: break;
1.1.1.25 root 19754: case 0x8b:
1.1.1.33 root 19755: val = dma_page_read(1, 1);
19756: break;
1.1.1.25 root 19757: case 0x8f:
1.1.1.33 root 19758: val = dma_page_read(1, 0);
19759: break;
1.1 root 19760: case 0x92:
1.1.1.33 root 19761: val = (m_a20_mask >> 19) & 2;
19762: break;
1.1.1.25 root 19763: case 0xa0: case 0xa1:
1.1.1.33 root 19764: val = pic_read(1, addr);
19765: break;
1.1.1.25 root 19766: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
19767: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33 root 19768: val = dma_read(1, (addr - 0xc0) >> 1);
19769: break;
1.1.1.37 root 19770: case 0x278: case 0x279: case 0x27a:
19771: val = pio_read(1, addr);
19772: break;
1.1.1.29 root 19773: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33 root 19774: val = sio_read(3, addr);
19775: break;
1.1.1.25 root 19776: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33 root 19777: val = sio_read(1, addr);
19778: break;
1.1.1.25 root 19779: case 0x378: case 0x379: case 0x37a:
1.1.1.33 root 19780: val = pio_read(0, addr);
19781: break;
1.1.1.25 root 19782: case 0x3ba: case 0x3da:
1.1.1.33 root 19783: val = vga_read_status();
19784: break;
1.1.1.37 root 19785: case 0x3bc: case 0x3bd: case 0x3be:
19786: val = pio_read(2, addr);
19787: break;
1.1.1.29 root 19788: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33 root 19789: val = sio_read(2, addr);
19790: break;
1.1.1.25 root 19791: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33 root 19792: val = sio_read(0, addr);
19793: break;
1.1 root 19794: default:
1.1.1.33 root 19795: // fatalerror("unknown inb %4x\n", addr);
1.1 root 19796: break;
19797: }
1.1.1.33 root 19798: #ifdef ENABLE_DEBUG_IOPORT
19799: if(fp_debug_log != NULL) {
19800: fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
19801: }
19802: #endif
19803: return(val);
1.1 root 19804: }
19805:
19806: UINT16 read_io_word(offs_t addr)
19807: {
19808: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
19809: }
19810:
1.1.1.33 root 19811: #ifdef USE_DEBUGGER
19812: UINT16 debugger_read_io_word(offs_t addr)
19813: {
19814: return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
19815: }
19816: #endif
19817:
1.1 root 19818: UINT32 read_io_dword(offs_t addr)
19819: {
19820: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
19821: }
19822:
1.1.1.33 root 19823: #ifdef USE_DEBUGGER
19824: UINT32 debugger_read_io_dword(offs_t addr)
19825: {
19826: 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));
19827: }
19828: #endif
19829:
1.1 root 19830: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33 root 19831: #ifdef USE_DEBUGGER
19832: {
19833: if(now_debugging) {
19834: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
19835: if(out_break_point.table[i].status == 1) {
19836: if(addr == out_break_point.table[i].addr) {
19837: out_break_point.hit = i + 1;
19838: now_suspended = true;
19839: break;
19840: }
19841: }
19842: }
19843: }
19844: debugger_write_io_byte(addr, val);
19845: }
19846: void debugger_write_io_byte(offs_t addr, UINT8 val)
19847: #endif
1.1 root 19848: {
1.1.1.25 root 19849: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33 root 19850: if(fp_debug_log != NULL) {
1.1.1.43 root 19851: #ifdef USE_SERVICE_THREAD
19852: if(addr != 0xf7)
19853: #endif
1.1.1.33 root 19854: fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25 root 19855: }
19856: #endif
1.1 root 19857: switch(addr) {
1.1.1.29 root 19858: #ifdef SW1US_PATCH
19859: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
19860: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
19861: sio_write(0, addr - 1, val);
19862: break;
19863: #else
1.1.1.25 root 19864: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
19865: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
19866: dma_write(0, addr, val);
19867: break;
1.1.1.29 root 19868: #endif
1.1.1.25 root 19869: case 0x20: case 0x21:
1.1 root 19870: pic_write(0, addr, val);
19871: break;
1.1.1.25 root 19872: case 0x40: case 0x41: case 0x42: case 0x43:
1.1 root 19873: pit_write(addr & 0x03, val);
19874: break;
1.1.1.7 root 19875: case 0x60:
19876: kbd_write_data(val);
19877: break;
1.1.1.9 root 19878: case 0x61:
19879: if((system_port & 3) != 3 && (val & 3) == 3) {
19880: // beep on
19881: // MessageBeep(-1);
19882: } else if((system_port & 3) == 3 && (val & 3) != 3) {
19883: // beep off
19884: }
19885: system_port = val;
19886: break;
1.1 root 19887: case 0x64:
1.1.1.7 root 19888: kbd_write_command(val);
1.1 root 19889: break;
19890: case 0x70:
19891: cmos_addr = val;
19892: break;
19893: case 0x71:
1.1.1.8 root 19894: cmos_write(cmos_addr, val);
1.1 root 19895: break;
1.1.1.25 root 19896: case 0x81:
19897: dma_page_write(0, 2, val);
19898: case 0x82:
19899: dma_page_write(0, 3, val);
19900: case 0x83:
19901: dma_page_write(0, 1, val);
19902: case 0x87:
19903: dma_page_write(0, 0, val);
19904: case 0x89:
19905: dma_page_write(1, 2, val);
19906: case 0x8a:
19907: dma_page_write(1, 3, val);
19908: case 0x8b:
19909: dma_page_write(1, 1, val);
19910: case 0x8f:
19911: dma_page_write(1, 0, val);
1.1 root 19912: case 0x92:
1.1.1.7 root 19913: i386_set_a20_line((val >> 1) & 1);
1.1 root 19914: break;
1.1.1.25 root 19915: case 0xa0: case 0xa1:
1.1 root 19916: pic_write(1, addr, val);
19917: break;
1.1.1.25 root 19918: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
19919: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26 root 19920: dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25 root 19921: break;
1.1.1.35 root 19922: #ifdef USE_SERVICE_THREAD
19923: case 0xf7:
19924: // dummy i/o for BIOS/DOS service
1.1.1.36 root 19925: if(in_service && cursor_moved) {
19926: // update cursor position before service is done
19927: pcbios_update_cursor_position();
19928: cursor_moved = false;
19929: }
1.1.1.35 root 19930: finish_service_loop();
19931: break;
19932: #endif
1.1.1.37 root 19933: case 0x278: case 0x279: case 0x27a:
19934: pio_write(1, addr, val);
19935: break;
1.1.1.29 root 19936: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
19937: sio_write(3, addr, val);
19938: break;
1.1.1.25 root 19939: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
19940: sio_write(1, addr, val);
19941: break;
19942: case 0x378: case 0x379: case 0x37a:
19943: pio_write(0, addr, val);
19944: break;
1.1.1.37 root 19945: case 0x3bc: case 0x3bd: case 0x3be:
19946: pio_write(2, addr, val);
19947: break;
1.1.1.29 root 19948: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
19949: sio_write(2, addr, val);
19950: break;
1.1.1.25 root 19951: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
19952: sio_write(0, addr, val);
19953: break;
1.1 root 19954: default:
1.1.1.33 root 19955: // fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1 root 19956: break;
19957: }
19958: }
19959:
19960: void write_io_word(offs_t addr, UINT16 val)
19961: {
19962: write_io_byte(addr + 0, (val >> 0) & 0xff);
19963: write_io_byte(addr + 1, (val >> 8) & 0xff);
19964: }
19965:
1.1.1.33 root 19966: #ifdef USE_DEBUGGER
19967: void debugger_write_io_word(offs_t addr, UINT16 val)
19968: {
19969: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
19970: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
19971: }
19972: #endif
19973:
1.1 root 19974: void write_io_dword(offs_t addr, UINT32 val)
19975: {
19976: write_io_byte(addr + 0, (val >> 0) & 0xff);
19977: write_io_byte(addr + 1, (val >> 8) & 0xff);
19978: write_io_byte(addr + 2, (val >> 16) & 0xff);
19979: write_io_byte(addr + 3, (val >> 24) & 0xff);
19980: }
1.1.1.33 root 19981:
19982: #ifdef USE_DEBUGGER
19983: void debugger_write_io_dword(offs_t addr, UINT32 val)
19984: {
19985: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
19986: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
19987: debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
19988: debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
19989: }
19990: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.