|
|
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
351: // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.35 root 352: if(key_buf_char != NULL && key_buf_scan != NULL) {
353: #ifdef USE_SERVICE_THREAD
354: EnterCriticalSection(&key_buf_crit_sect);
355: #endif
1.1.1.41 root 356: int count = min(key_buf_char->count(), 15);
357: // write to top of key buffer
358: for(int i = 0; i < count; i++) {
359: mem[0x41e + 2 * i + 0] = key_buf_char->read_not_remove(i);
360: mem[0x41e + 2 * i + 1] = key_buf_scan->read_not_remove(i);
361: }
1.1.1.35 root 362: #ifdef USE_SERVICE_THREAD
363: LeaveCriticalSection(&key_buf_crit_sect);
364: #endif
365: if(count == 0) {
1.1.1.32 root 366: maybe_idle();
367: }
1.1.1.41 root 368: return (UINT16)(0x1e + 2 * count);
1.1.1.14 root 369: }
1.1.1.41 root 370: return 0x1e;
1.1.1.14 root 371: }
1.1.1.4 root 372: #if defined(HAS_I386)
1.1 root 373: if(byteaddress < MAX_MEM - 1) {
374: return *(UINT16 *)(mem + byteaddress);
1.1.1.3 root 375: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
376: // return read_word(byteaddress & 0xfffff);
1.1 root 377: }
378: return 0;
1.1.1.4 root 379: #else
380: return *(UINT16 *)(mem + byteaddress);
381: #endif
1.1 root 382: }
383:
384: UINT32 read_dword(offs_t byteaddress)
1.1.1.33 root 385: #ifdef USE_DEBUGGER
386: {
387: if(now_debugging) {
388: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
389: if(rd_break_point.table[i].status == 1) {
390: if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
391: rd_break_point.hit = i + 1;
392: now_suspended = true;
393: break;
394: }
395: }
396: }
397: }
398: return(debugger_read_dword(byteaddress));
399: }
400: UINT32 debugger_read_dword(offs_t byteaddress)
401: #endif
1.1 root 402: {
1.1.1.4 root 403: #if defined(HAS_I386)
1.1 root 404: if(byteaddress < MAX_MEM - 3) {
405: return *(UINT32 *)(mem + byteaddress);
1.1.1.3 root 406: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
407: // return read_dword(byteaddress & 0xfffff);
1.1 root 408: }
409: return 0;
1.1.1.4 root 410: #else
411: return *(UINT32 *)(mem + byteaddress);
412: #endif
1.1 root 413: }
414:
415: // write accessors
1.1.1.35 root 416: #ifdef USE_VRAM_THREAD
1.1.1.14 root 417: void vram_flush_char()
418: {
419: if(vram_length_char != 0) {
420: DWORD num;
1.1.1.23 root 421: WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14 root 422: vram_length_char = vram_last_length_char = 0;
423: }
424: }
425:
426: void vram_flush_attr()
427: {
428: if(vram_length_attr != 0) {
429: DWORD num;
1.1.1.23 root 430: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14 root 431: vram_length_attr = vram_last_length_attr = 0;
432: }
433: }
434:
435: void vram_flush()
436: {
437: if(vram_length_char != 0 || vram_length_attr != 0) {
438: EnterCriticalSection(&vram_crit_sect);
439: vram_flush_char();
440: vram_flush_attr();
441: LeaveCriticalSection(&vram_crit_sect);
442: }
443: }
444: #endif
445:
446: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8 root 447: {
1.1.1.35 root 448: #ifdef USE_VRAM_THREAD
1.1.1.14 root 449: static offs_t first_offset_char, last_offset_char;
450:
451: if(vram_length_char != 0) {
452: if(offset <= last_offset_char && offset >= first_offset_char) {
453: scr_char[(offset - first_offset_char) >> 1] = data;
454: return;
455: }
456: if(offset != last_offset_char + 2) {
457: vram_flush_char();
458: }
459: }
460: if(vram_length_char == 0) {
461: first_offset_char = offset;
462: vram_coord_char.X = (offset >> 1) % scr_width;
463: vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
464: }
465: scr_char[vram_length_char++] = data;
466: last_offset_char = offset;
467: #else
1.1.1.8 root 468: COORD co;
469: DWORD num;
470:
1.1.1.14 root 471: co.X = (offset >> 1) % scr_width;
472: co.Y = (offset >> 1) / scr_width;
473: scr_char[0] = data;
1.1.1.23 root 474: WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14 root 475: #endif
476: }
477:
478: void write_text_vram_attr(offs_t offset, UINT8 data)
479: {
1.1.1.35 root 480: #ifdef USE_VRAM_THREAD
1.1.1.14 root 481: static offs_t first_offset_attr, last_offset_attr;
482:
483: if(vram_length_attr != 0) {
484: if(offset <= last_offset_attr && offset >= first_offset_attr) {
485: scr_attr[(offset - first_offset_attr) >> 1] = data;
486: return;
487: }
488: if(offset != last_offset_attr + 2) {
489: vram_flush_attr();
490: }
491: }
492: if(vram_length_attr == 0) {
493: first_offset_attr = offset;
494: vram_coord_attr.X = (offset >> 1) % scr_width;
495: vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
496: }
497: scr_attr[vram_length_attr++] = data;
498: last_offset_attr = offset;
499: #else
500: COORD co;
501: DWORD num;
1.1.1.8 root 502:
1.1.1.14 root 503: co.X = (offset >> 1) % scr_width;
504: co.Y = (offset >> 1) / scr_width;
505: scr_attr[0] = data;
1.1.1.23 root 506: WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14 root 507: #endif
508: }
509:
510: void write_text_vram_byte(offs_t offset, UINT8 data)
511: {
1.1.1.35 root 512: #ifdef USE_VRAM_THREAD
1.1.1.14 root 513: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 514: #endif
1.1.1.8 root 515: if(offset & 1) {
1.1.1.14 root 516: write_text_vram_attr(offset, data);
1.1.1.8 root 517: } else {
1.1.1.14 root 518: write_text_vram_char(offset, data);
1.1.1.8 root 519: }
1.1.1.35 root 520: #ifdef USE_VRAM_THREAD
1.1.1.14 root 521: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 522: #endif
1.1.1.8 root 523: }
524:
525: void write_text_vram_word(offs_t offset, UINT16 data)
526: {
1.1.1.35 root 527: #ifdef USE_VRAM_THREAD
1.1.1.14 root 528: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 529: #endif
1.1.1.8 root 530: if(offset & 1) {
1.1.1.14 root 531: write_text_vram_attr(offset , (data ) & 0xff);
532: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 533: } else {
1.1.1.14 root 534: write_text_vram_char(offset , (data ) & 0xff);
535: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8 root 536: }
1.1.1.35 root 537: #ifdef USE_VRAM_THREAD
1.1.1.14 root 538: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 539: #endif
1.1.1.8 root 540: }
541:
542: void write_text_vram_dword(offs_t offset, UINT32 data)
543: {
1.1.1.35 root 544: #ifdef USE_VRAM_THREAD
1.1.1.14 root 545: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 546: #endif
1.1.1.8 root 547: if(offset & 1) {
1.1.1.14 root 548: write_text_vram_attr(offset , (data ) & 0xff);
549: write_text_vram_char(offset + 1, (data >> 8) & 0xff);
550: write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
551: write_text_vram_char(offset + 3, (data >> 24) & 0xff);
552: } else {
553: write_text_vram_char(offset , (data ) & 0xff);
554: write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
555: write_text_vram_char(offset + 2, (data >> 16) & 0xff);
556: write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8 root 557: }
1.1.1.35 root 558: #ifdef USE_VRAM_THREAD
1.1.1.14 root 559: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 560: #endif
1.1.1.8 root 561: }
562:
1.1 root 563: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33 root 564: #ifdef USE_DEBUGGER
565: {
566: if(now_debugging) {
567: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
568: if(wr_break_point.table[i].status == 1) {
569: if(byteaddress == wr_break_point.table[i].addr) {
570: wr_break_point.hit = i + 1;
571: now_suspended = true;
572: break;
573: }
574: }
575: }
576: }
577: debugger_write_byte(byteaddress, data);
578: }
579: void debugger_write_byte(offs_t byteaddress, UINT8 data)
580: #endif
1.1 root 581: {
1.1.1.8 root 582: if(byteaddress < MEMORY_END) {
1.1.1.3 root 583: mem[byteaddress] = data;
1.1.1.8 root 584: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 585: if(!restore_console_on_exit) {
586: change_console_size(scr_width, scr_height);
1.1.1.12 root 587: }
1.1.1.8 root 588: write_text_vram_byte(byteaddress - text_vram_top_address, data);
589: mem[byteaddress] = data;
590: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
591: if(int_10h_feh_called && !int_10h_ffh_called) {
592: write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1 root 593: }
594: mem[byteaddress] = data;
1.1.1.4 root 595: #if defined(HAS_I386)
1.1.1.3 root 596: } else if(byteaddress < MAX_MEM) {
1.1.1.4 root 597: #else
598: } else {
599: #endif
1.1.1.3 root 600: mem[byteaddress] = data;
1.1 root 601: }
602: }
603:
604: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33 root 605: #ifdef USE_DEBUGGER
606: {
607: if(now_debugging) {
608: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
609: if(wr_break_point.table[i].status == 1) {
610: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
611: wr_break_point.hit = i + 1;
612: now_suspended = true;
613: break;
614: }
615: }
616: }
617: }
618: debugger_write_word(byteaddress, data);
619: }
620: void debugger_write_word(offs_t byteaddress, UINT16 data)
621: #endif
1.1 root 622: {
1.1.1.8 root 623: if(byteaddress < MEMORY_END) {
1.1.1.14 root 624: if(byteaddress == 0x450 + mem[0x462] * 2) {
625: COORD co;
626: co.X = data & 0xff;
627: co.Y = (data >> 8) + scr_top;
1.1.1.23 root 628: SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14 root 629: }
1.1.1.3 root 630: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8 root 631: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 632: if(!restore_console_on_exit) {
633: change_console_size(scr_width, scr_height);
1.1.1.12 root 634: }
1.1.1.8 root 635: write_text_vram_word(byteaddress - text_vram_top_address, data);
636: *(UINT16 *)(mem + byteaddress) = data;
637: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
638: if(int_10h_feh_called && !int_10h_ffh_called) {
639: write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1 root 640: }
641: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4 root 642: #if defined(HAS_I386)
1.1.1.3 root 643: } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4 root 644: #else
645: } else {
646: #endif
1.1.1.3 root 647: *(UINT16 *)(mem + byteaddress) = data;
1.1 root 648: }
649: }
650:
651: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33 root 652: #ifdef USE_DEBUGGER
653: {
654: if(now_debugging) {
655: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
656: if(wr_break_point.table[i].status == 1) {
657: if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
658: wr_break_point.hit = i + 1;
659: now_suspended = true;
660: break;
661: }
662: }
663: }
664: }
665: debugger_write_dword(byteaddress, data);
666: }
667: void debugger_write_dword(offs_t byteaddress, UINT32 data)
668: #endif
1.1 root 669: {
1.1.1.8 root 670: if(byteaddress < MEMORY_END) {
1.1.1.3 root 671: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8 root 672: } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14 root 673: if(!restore_console_on_exit) {
674: change_console_size(scr_width, scr_height);
1.1.1.12 root 675: }
1.1.1.8 root 676: write_text_vram_dword(byteaddress - text_vram_top_address, data);
677: *(UINT32 *)(mem + byteaddress) = data;
678: } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
679: if(int_10h_feh_called && !int_10h_ffh_called) {
680: write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1 root 681: }
682: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4 root 683: #if defined(HAS_I386)
1.1.1.3 root 684: } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4 root 685: #else
686: } else {
687: #endif
1.1.1.3 root 688: *(UINT32 *)(mem + byteaddress) = data;
1.1 root 689: }
690: }
691:
692: #define read_decrypted_byte read_byte
693: #define read_decrypted_word read_word
694: #define read_decrypted_dword read_dword
695:
1.1.1.3 root 696: #define read_raw_byte read_byte
697: #define write_raw_byte write_byte
698:
699: #define read_word_unaligned read_word
700: #define write_word_unaligned write_word
701:
702: #define read_io_word_unaligned read_io_word
703: #define write_io_word_unaligned write_io_word
704:
1.1 root 705: UINT8 read_io_byte(offs_t byteaddress);
706: UINT16 read_io_word(offs_t byteaddress);
707: UINT32 read_io_dword(offs_t byteaddress);
708:
709: void write_io_byte(offs_t byteaddress, UINT8 data);
710: void write_io_word(offs_t byteaddress, UINT16 data);
711: void write_io_dword(offs_t byteaddress, UINT32 data);
712:
713: /*****************************************************************************/
714: /* src/osd/osdcomm.h */
715:
716: /* Highly useful macro for compile-time knowledge of an array size */
717: #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
718:
1.1.1.3 root 719: #if defined(HAS_I386)
1.1.1.10 root 720: static CPU_TRANSLATE(i386);
721: #include "mame/lib/softfloat/softfloat.c"
722: #include "mame/emu/cpu/i386/i386.c"
1.1.1.12 root 723: #include "mame/emu/cpu/vtlb.c"
1.1.1.3 root 724: #elif defined(HAS_I286)
1.1.1.10 root 725: #include "mame/emu/cpu/i86/i286.c"
1.1.1.3 root 726: #else
1.1.1.10 root 727: #include "mame/emu/cpu/i86/i86.c"
1.1.1.3 root 728: #endif
1.1.1.33 root 729: #ifdef USE_DEBUGGER
1.1.1.10 root 730: #include "mame/emu/cpu/i386/i386dasm.c"
1.1 root 731: #endif
732:
1.1.1.3 root 733: #if defined(HAS_I386)
734: #define SREG(x) m_sreg[x].selector
735: #define SREG_BASE(x) m_sreg[x].base
736: int cpu_type, cpu_step;
737: #else
738: #define REG8(x) m_regs.b[x]
739: #define REG16(x) m_regs.w[x]
740: #define SREG(x) m_sregs[x]
741: #define SREG_BASE(x) m_base[x]
1.1.1.33 root 742: #define m_eip (m_pc - m_base[CS])
1.1.1.3 root 743: #define m_CF m_CarryVal
744: #define m_a20_mask AMASK
745: #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
746: #if defined(HAS_I286)
747: #define i386_set_a20_line(x) i80286_set_a20_line(x)
748: #else
749: #define i386_set_a20_line(x)
750: #endif
751: #define i386_set_irq_line(x, y) set_irq_line(x, y)
752: #endif
1.1 root 753:
754: void i386_jmp_far(UINT16 selector, UINT32 address)
755: {
1.1.1.3 root 756: #if defined(HAS_I386)
1.1 root 757: if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3 root 758: i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1 root 759: } else {
1.1.1.3 root 760: SREG(CS) = selector;
761: m_performed_intersegment_jump = 1;
762: i386_load_segment_descriptor(CS);
763: m_eip = address;
764: CHANGE_PC(m_eip);
1.1 root 765: }
1.1.1.3 root 766: #elif defined(HAS_I286)
767: i80286_code_descriptor(selector, address, 1);
768: #else
769: SREG(CS) = selector;
770: i386_load_segment_descriptor(CS);
771: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
772: #endif
1.1 root 773: }
774:
1.1.1.35 root 775: #ifdef USE_SERVICE_THREAD
1.1.1.24 root 776: void i386_call_far(UINT16 selector, UINT32 address)
777: {
778: #if defined(HAS_I386)
779: if(PROTECTED_MODE && !V8086_MODE) {
780: i386_protected_mode_call(selector, address, 1, m_operand_size);
781: } else {
782: PUSH16(SREG(CS));
783: PUSH16(m_eip);
784: SREG(CS) = selector;
785: m_performed_intersegment_jump = 1;
786: i386_load_segment_descriptor(CS);
787: m_eip = address;
788: CHANGE_PC(m_eip);
789: }
790: #else
791: UINT16 ip = m_pc - SREG_BASE(CS);
792: UINT16 cs = SREG(CS);
793: #if defined(HAS_I286)
794: i80286_code_descriptor(selector, address, 2);
795: #else
796: SREG(CS) = selector;
797: i386_load_segment_descriptor(CS);
798: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
799: #endif
800: PUSH(cs);
801: PUSH(ip);
802: CHANGE_PC(m_pc);
803: #endif
804: }
1.1.1.35 root 805: #endif
1.1.1.24 root 806:
1.1.1.29 root 807: UINT16 i386_read_stack()
808: {
809: #if defined(HAS_I386)
810: UINT32 ea, new_esp;
811: if( STACK_32BIT ) {
812: new_esp = REG32(ESP) + 2;
813: ea = i386_translate(SS, new_esp - 2, 0);
814: } else {
815: new_esp = REG16(SP) + 2;
816: ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
817: }
818: return READ16(ea);
819: #else
820: UINT16 sp = m_regs.w[SP] + 2;
821: return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
822: #endif
823: }
824:
1.1 root 825: /* ----------------------------------------------------------------------------
1.1.1.33 root 826: debugger
827: ---------------------------------------------------------------------------- */
828:
829: #ifdef USE_DEBUGGER
830: #define TELNET_BLUE 0x0004 // text color contains blue.
831: #define TELNET_GREEN 0x0002 // text color contains green.
832: #define TELNET_RED 0x0001 // text color contains red.
833: #define TELNET_INTENSITY 0x0008 // text color is intensified.
834:
835: int svr_socket = 0;
836: int cli_socket = 0;
837:
838: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
839:
840: void debugger_init()
841: {
842: now_debugging = false;
843: now_going = false;
844: now_suspended = false;
845: force_suspend = false;
846:
847: memset(&break_point, 0, sizeof(break_point_t));
848: memset(&rd_break_point, 0, sizeof(break_point_t));
849: memset(&wr_break_point, 0, sizeof(break_point_t));
850: memset(&in_break_point, 0, sizeof(break_point_t));
851: memset(&out_break_point, 0, sizeof(break_point_t));
852: memset(&int_break_point, 0, sizeof(int_break_point_t));
853: }
854:
855: void telnet_send(char *string)
856: {
857: char buffer[8192], *ptr;
858: strcpy(buffer, string);
859: while((ptr = strstr(buffer, "\n")) != NULL) {
860: char tmp[8192];
861: *ptr = '\0';
862: sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
863: strcpy(buffer, tmp);
864: }
865:
866: int len = strlen(buffer), res;
867: ptr = buffer;
868: while(len > 0) {
869: if((res = send(cli_socket, ptr, len, 0)) > 0) {
870: len -= res;
871: ptr += res;
872: }
873: }
874: }
875:
876: void telnet_command(const char *format, ...)
877: {
878: char buffer[1024];
879: va_list ap;
880: va_start(ap, format);
881: vsprintf(buffer, format, ap);
882: va_end(ap);
883:
884: telnet_send(buffer);
885: }
886:
887: void telnet_printf(const char *format, ...)
888: {
889: char buffer[1024];
890: va_list ap;
891: va_start(ap, format);
892: vsprintf(buffer, format, ap);
893: va_end(ap);
894:
895: if(fp_debugger != NULL) {
896: fprintf(fp_debugger, "%s", buffer);
897: }
898: telnet_send(buffer);
899: }
900:
901: bool telnet_gets(char *str, int n)
902: {
903: char buffer[1024];
904: int ptr = 0;
905:
906: telnet_command("\033[12l"); // local echo on
907: telnet_command("\033[2l"); // key unlock
908:
909: while(!m_halted) {
910: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
911:
912: if(len > 0 && buffer[0] != 0xff) {
913: for(int i = 0; i < len; i++) {
914: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
915: str[ptr] = 0;
916: telnet_command("\033[2h"); // key lock
917: telnet_command("\033[12h"); // local echo off
918: return(!m_halted);
919: } else if(buffer[i] == 0x08) {
920: if(ptr > 0) {
921: telnet_command("\033[0K"); // erase from cursor position
922: ptr--;
923: } else {
924: telnet_command("\033[1C"); // move cursor forward
925: }
926: } else if(ptr < n - 1) {
1.1.1.37 root 927: if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33 root 928: str[ptr++] = buffer[i];
929: }
930: } else {
931: telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
932: }
933: }
934: } else if(len == -1) {
935: if(WSAGetLastError() != WSAEWOULDBLOCK) {
936: return(false);
937: }
938: } else if(len == 0) {
939: return(false);
940: }
941: Sleep(10);
942: }
943: return(!m_halted);
944: }
945:
946: bool telnet_kbhit()
947: {
948: char buffer[1024];
949:
950: if(!m_halted) {
951: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
952:
953: if(len > 0) {
954: for(int i = 0; i < len; i++) {
955: if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
956: return(true);
957: }
958: }
959: } else if(len == 0) {
960: return(true); // disconnected
961: }
962: }
963: return(false);
964: }
965:
966: bool telnet_disconnected()
967: {
968: char buffer[1024];
969: int len = recv(cli_socket, buffer, sizeof(buffer), 0);
970:
971: if(len == 0) {
972: return(true);
973: } else if(len == -1) {
974: if(WSAGetLastError() != WSAEWOULDBLOCK) {
975: return(true);
976: }
977: }
978: return(false);
979: }
980:
981: void telnet_set_color(int color)
982: {
983: telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
984: }
985:
986: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
987: {
988: // UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
989: UINT8 ops[16];
990: for(int i = 0; i < 16; i++) {
991: ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
992: }
993: UINT8 *oprom = ops;
994:
995: #if defined(HAS_I386)
996: if(m_operand_size) {
997: return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
998: } else
999: #endif
1000: return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
1001: }
1002:
1003: void debugger_regs_info(char *buffer)
1004: {
1005: #if defined(HAS_I386)
1006: UINT32 flags = get_flags();
1007: #else
1008: UINT32 flags = CompressFlags();
1009: #endif
1010: #if defined(HAS_I386)
1011: if(m_operand_size) {
1012: 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",
1013: 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),
1014: PROTECTED_MODE ? "PE" : "--",
1015: (flags & 0x40000) ? 'A' : '-',
1016: (flags & 0x20000) ? 'V' : '-',
1017: (flags & 0x10000) ? 'R' : '-',
1018: (flags & 0x04000) ? 'N' : '-',
1019: (flags & 0x02000) ? '1' : '0',
1020: (flags & 0x01000) ? '1' : '0',
1021: (flags & 0x00800) ? 'O' : '-',
1022: (flags & 0x00400) ? 'D' : '-',
1023: (flags & 0x00200) ? 'I' : '-',
1024: (flags & 0x00100) ? 'T' : '-',
1025: (flags & 0x00080) ? 'S' : '-',
1026: (flags & 0x00040) ? 'Z' : '-',
1027: (flags & 0x00010) ? 'A' : '-',
1028: (flags & 0x00004) ? 'P' : '-',
1029: (flags & 0x00001) ? 'C' : '-');
1030: } else {
1031: #endif
1032: 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",
1033: 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),
1034: #if defined(HAS_I386)
1035: PROTECTED_MODE ? "PE" : "--",
1036: #else
1037: "--",
1038: #endif
1039: (flags & 0x40000) ? 'A' : '-',
1040: (flags & 0x20000) ? 'V' : '-',
1041: (flags & 0x10000) ? 'R' : '-',
1042: (flags & 0x04000) ? 'N' : '-',
1043: (flags & 0x02000) ? '1' : '0',
1044: (flags & 0x01000) ? '1' : '0',
1045: (flags & 0x00800) ? 'O' : '-',
1046: (flags & 0x00400) ? 'D' : '-',
1047: (flags & 0x00200) ? 'I' : '-',
1048: (flags & 0x00100) ? 'T' : '-',
1049: (flags & 0x00080) ? 'S' : '-',
1050: (flags & 0x00040) ? 'Z' : '-',
1051: (flags & 0x00010) ? 'A' : '-',
1052: (flags & 0x00004) ? 'P' : '-',
1053: (flags & 0x00001) ? 'C' : '-');
1054: #if defined(HAS_I386)
1055: }
1056: #endif
1057: }
1058:
1059: void debugger_process_info(char *buffer)
1060: {
1061: UINT16 psp_seg = current_psp;
1062: process_t *process;
1063: bool check[0x10000] = {0};
1064:
1065: buffer[0] = '\0';
1066:
1067: while(!check[psp_seg] && (process = msdos_process_info_get(psp_seg, false)) != NULL) {
1068: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
1069: char *file = process->module_path, *s;
1070: char tmp[8192];
1071:
1072: while((s = strstr(file, "\\")) != NULL) {
1073: file = s + 1;
1074: }
1075: 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));
1076: strcat(tmp, buffer);
1077: strcpy(buffer, tmp);
1078:
1079: check[psp_seg] = true;
1080: psp_seg = psp->parent_psp;
1081: }
1082: }
1083:
1084: UINT32 debugger_get_val(const char *str)
1085: {
1086: char tmp[1024];
1087:
1088: if(str == NULL || strlen(str) == 0) {
1089: return(0);
1090: }
1091: strcpy(tmp, str);
1092:
1093: if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
1094: // ank
1095: return(tmp[1] & 0xff);
1096: } else if(tmp[0] == '%') {
1097: // decimal
1098: return(strtoul(tmp + 1, NULL, 10));
1099: }
1100: return(strtoul(tmp, NULL, 16));
1101: }
1102:
1103: UINT32 debugger_get_seg(const char *str, UINT32 val)
1104: {
1105: char tmp[1024], *s;
1106:
1107: if(str == NULL || strlen(str) == 0) {
1108: return(val);
1109: }
1110: strcpy(tmp, str);
1111:
1112: if((s = strstr(tmp, ":")) != NULL) {
1113: // 0000:0000
1114: *s = '\0';
1115: return(debugger_get_val(tmp));
1116: }
1117: return(val);
1118: }
1119:
1120: UINT32 debugger_get_ofs(const char *str)
1121: {
1122: char tmp[1024], *s;
1123:
1124: if(str == NULL || strlen(str) == 0) {
1125: return(0);
1126: }
1127: strcpy(tmp, str);
1128:
1129: if((s = strstr(tmp, ":")) != NULL) {
1130: // 0000:0000
1131: return(debugger_get_val(s + 1));
1132: }
1133: return(debugger_get_val(tmp));
1134: }
1135:
1136: void debugger_main()
1137: {
1138: telnet_command("\033[20h"); // cr-lf
1139:
1140: force_suspend = true;
1141: now_going = false;
1142: now_debugging = true;
1143: Sleep(100);
1144:
1145: if(!m_halted && !now_suspended) {
1146: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1147: telnet_printf("waiting until cpu is suspended...\n");
1148: }
1149: while(!m_halted && !now_suspended) {
1150: if(telnet_disconnected()) {
1151: break;
1152: }
1153: Sleep(10);
1154: }
1155:
1156: char buffer[8192];
1157:
1158: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1159: debugger_process_info(buffer);
1160: telnet_printf("%s", buffer);
1161: debugger_regs_info(buffer);
1162: telnet_printf("%s", buffer);
1163: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1164: telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
1165: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1166: debugger_dasm(buffer, SREG(CS), m_eip);
1167: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1168: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1169:
1170: #define MAX_COMMAND_LEN 64
1171:
1172: char command[MAX_COMMAND_LEN + 1];
1173: char prev_command[MAX_COMMAND_LEN + 1] = {0};
1174:
1175: UINT32 data_seg = SREG(DS);
1176: UINT32 data_ofs = 0;
1177: UINT32 dasm_seg = SREG(CS);
1178: UINT32 dasm_ofs = m_eip;
1179:
1180: while(!m_halted) {
1181: telnet_printf("- ");
1182: command[0] = '\0';
1183:
1184: if(fi_debugger != NULL) {
1185: while(command[0] == '\0') {
1186: if(fgets(command, sizeof(command), fi_debugger) == NULL) {
1187: break;
1188: }
1189: while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
1190: command[strlen(command) - 1] = '\0';
1191: }
1192: }
1193: if(command[0] != '\0') {
1194: telnet_command("%s\n", command);
1195: }
1196: }
1197: if(command[0] == '\0') {
1198: if(!telnet_gets(command, sizeof(command))) {
1199: break;
1200: }
1201: }
1202: if(command[0] == '\0') {
1203: strcpy(command, prev_command);
1204: } else {
1205: strcpy(prev_command, command);
1206: }
1207: if(fp_debugger != NULL) {
1208: fprintf(fp_debugger, "%s\n", command);
1209: }
1210:
1211: if(!m_halted && command[0] != 0) {
1212: char *params[32], *token = NULL;
1213: int num = 0;
1214:
1215: if((token = strtok(command, " ")) != NULL) {
1216: params[num++] = token;
1217: while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
1218: params[num++] = token;
1219: }
1220: }
1221: if(stricmp(params[0], "D") == 0) {
1222: if(num <= 3) {
1223: if(num >= 2) {
1224: data_seg = debugger_get_seg(params[1], data_seg);
1225: data_ofs = debugger_get_ofs(params[1]);
1226: }
1227: UINT32 end_seg = data_seg;
1228: UINT32 end_ofs = data_ofs + 8 * 16 - 1;
1229: if(num == 3) {
1230: end_seg = debugger_get_seg(params[2], data_seg);
1231: end_ofs = debugger_get_ofs(params[2]);
1232: }
1233: UINT64 start_addr = (data_seg << 4) + data_ofs;
1234: UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37 root 1235: // bool is_sjis = false;
1.1.1.33 root 1236:
1237: for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
1238: if((addr & 0x0f) == 0) {
1239: if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
1240: data_seg += 0x1000;
1241: data_ofs -= 0x10000;
1242: }
1243: telnet_printf("%06X:%04X ", data_seg, data_ofs);
1244: memset(buffer, 0, sizeof(buffer));
1245: }
1246: if(addr < start_addr || addr > end_addr) {
1247: telnet_printf(" ");
1248: buffer[addr & 0x0f] = ' ';
1249: } else {
1250: UINT8 data = debugger_read_byte(addr & ADDR_MASK);
1251: telnet_printf(" %02X", data);
1.1.1.37 root 1252: // if(is_sjis) {
1.1.1.33 root 1253: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1254: // is_sjis = false;
1.1.1.33 root 1255: // } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
1256: // buffer[addr & 0x0f] = data;
1.1.1.37 root 1257: // is_sjis = true;
1.1.1.33 root 1258: // } else
1259: if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
1260: buffer[addr & 0x0f] = data;
1261: } else {
1262: buffer[addr & 0x0f] = '.';
1263: }
1264: }
1265: if((addr & 0x0f) == 0x0f) {
1266: telnet_printf(" %s\n", buffer);
1267: }
1268: }
1269: if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
1270: data_seg += 0x1000;
1271: data_ofs -= 0x10000;
1272: }
1273: prev_command[1] = '\0'; // remove parameters to dump continuously
1274: } else {
1275: telnet_printf("invalid parameter number\n");
1276: }
1277: } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
1278: if(num >= 3) {
1279: UINT32 seg = debugger_get_seg(params[1], data_seg);
1280: UINT32 ofs = debugger_get_ofs(params[1]);
1281: for(int i = 2, j = 0; i < num; i++, j++) {
1282: debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
1283: }
1284: } else {
1285: telnet_printf("invalid parameter number\n");
1286: }
1287: } else if(stricmp(params[0], "EW") == 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 += 2) {
1292: debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
1293: }
1294: } else {
1295: telnet_printf("invalid parameter number\n");
1296: }
1297: } else if(stricmp(params[0], "ED") == 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 += 4) {
1302: debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
1303: }
1304: } else {
1305: telnet_printf("invalid parameter number\n");
1306: }
1307: } else if(stricmp(params[0], "EA") == 0) {
1308: if(num >= 3) {
1309: UINT32 seg = debugger_get_seg(params[1], data_seg);
1310: UINT32 ofs = debugger_get_ofs(params[1]);
1311: strcpy(buffer, prev_command);
1312: if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
1313: int len = strlen(token);
1314: for(int i = 0; i < len; i++) {
1315: debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
1316: }
1317: } else {
1318: telnet_printf("invalid parameter\n");
1319: }
1320: } else {
1321: telnet_printf("invalid parameter number\n");
1322: }
1323: } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
1324: if(num == 2) {
1325: telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
1326: } else {
1327: telnet_printf("invalid parameter number\n");
1328: }
1329: } else if(stricmp(params[0], "IW") == 0) {
1330: if(num == 2) {
1331: telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
1332: } else {
1333: telnet_printf("invalid parameter number\n");
1334: }
1335: } else if(stricmp(params[0], "ID") == 0) {
1336: if(num == 2) {
1337: telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
1338: } else {
1339: telnet_printf("invalid parameter number\n");
1340: }
1341: } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
1342: if(num == 3) {
1343: debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
1344: } else {
1345: telnet_printf("invalid parameter number\n");
1346: }
1347: } else if(stricmp(params[0], "OW") == 0) {
1348: if(num == 3) {
1349: debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
1350: } else {
1351: telnet_printf("invalid parameter number\n");
1352: }
1353: } else if(stricmp(params[0], "OD") == 0) {
1354: if(num == 3) {
1355: debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
1356: } else {
1357: telnet_printf("invalid parameter number\n");
1358: }
1359: } else if(stricmp(params[0], "R") == 0) {
1360: if(num == 1) {
1361: debugger_regs_info(buffer);
1362: telnet_printf("%s", buffer);
1363: } else if(num == 3) {
1364: #if defined(HAS_I386)
1365: if(stricmp(params[1], "EAX") == 0) {
1366: REG32(EAX) = debugger_get_val(params[2]);
1367: } else if(stricmp(params[1], "EBX") == 0) {
1368: REG32(EBX) = debugger_get_val(params[2]);
1369: } else if(stricmp(params[1], "ECX") == 0) {
1370: REG32(ECX) = debugger_get_val(params[2]);
1371: } else if(stricmp(params[1], "EDX") == 0) {
1372: REG32(EDX) = debugger_get_val(params[2]);
1373: } else if(stricmp(params[1], "ESP") == 0) {
1374: REG32(ESP) = debugger_get_val(params[2]);
1375: } else if(stricmp(params[1], "EBP") == 0) {
1376: REG32(EBP) = debugger_get_val(params[2]);
1377: } else if(stricmp(params[1], "ESI") == 0) {
1378: REG32(ESI) = debugger_get_val(params[2]);
1379: } else if(stricmp(params[1], "EDI") == 0) {
1380: REG32(EDI) = debugger_get_val(params[2]);
1381: } else
1382: #endif
1383: if(stricmp(params[1], "AX") == 0) {
1384: REG16(AX) = debugger_get_val(params[2]);
1385: } else if(stricmp(params[1], "BX") == 0) {
1386: REG16(BX) = debugger_get_val(params[2]);
1387: } else if(stricmp(params[1], "CX") == 0) {
1388: REG16(CX) = debugger_get_val(params[2]);
1389: } else if(stricmp(params[1], "DX") == 0) {
1390: REG16(DX) = debugger_get_val(params[2]);
1391: } else if(stricmp(params[1], "SP") == 0) {
1392: REG16(SP) = debugger_get_val(params[2]);
1393: } else if(stricmp(params[1], "BP") == 0) {
1394: REG16(BP) = debugger_get_val(params[2]);
1395: } else if(stricmp(params[1], "SI") == 0) {
1396: REG16(SI) = debugger_get_val(params[2]);
1397: } else if(stricmp(params[1], "DI") == 0) {
1398: REG16(DI) = debugger_get_val(params[2]);
1399: } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
1400: #if defined(HAS_I386)
1401: if(m_operand_size) {
1402: m_eip = debugger_get_val(params[2]);
1403: } else {
1404: m_eip = debugger_get_val(params[2]) & 0xffff;
1405: }
1406: CHANGE_PC(m_eip);
1407: #else
1408: m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
1409: CHANGE_PC(m_pc);
1410: #endif
1411: } else if(stricmp(params[1], "AL") == 0) {
1412: REG8(AL) = debugger_get_val(params[2]);
1413: } else if(stricmp(params[1], "AH") == 0) {
1414: REG8(AH) = debugger_get_val(params[2]);
1415: } else if(stricmp(params[1], "BL") == 0) {
1416: REG8(BL) = debugger_get_val(params[2]);
1417: } else if(stricmp(params[1], "BH") == 0) {
1418: REG8(BH) = debugger_get_val(params[2]);
1419: } else if(stricmp(params[1], "CL") == 0) {
1420: REG8(CL) = debugger_get_val(params[2]);
1421: } else if(stricmp(params[1], "CH") == 0) {
1422: REG8(CH) = debugger_get_val(params[2]);
1423: } else if(stricmp(params[1], "DL") == 0) {
1424: REG8(DL) = debugger_get_val(params[2]);
1425: } else if(stricmp(params[1], "DH") == 0) {
1426: REG8(DH) = debugger_get_val(params[2]);
1427: } else {
1428: telnet_printf("unknown register %s\n", params[1]);
1429: }
1430: } else {
1431: telnet_printf("invalid parameter number\n");
1432: }
1433: } else if(_tcsicmp(params[0], "S") == 0) {
1434: if(num >= 4) {
1435: UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
1436: UINT32 cur_ofs = debugger_get_ofs(params[1]);
1437: UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
1438: UINT32 end_ofs = debugger_get_ofs(params[2]);
1439: UINT8 list[32];
1440:
1441: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1442: list[j] = debugger_get_val(params[i]);
1443: }
1444: while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
1445: bool found = true;
1446: for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
1447: if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
1448: found = false;
1449: break;
1450: }
1451: }
1452: if(found) {
1453: telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
1454: }
1455: if((cur_ofs += 1) > 0xffff) {
1456: cur_seg += 0x1000;
1457: cur_ofs -= 0x10000;
1458: }
1459: }
1460: } else {
1461: telnet_printf("invalid parameter number\n");
1462: }
1463: } else if(stricmp(params[0], "U") == 0) {
1464: if(num <= 3) {
1465: if(num >= 2) {
1466: dasm_seg = debugger_get_seg(params[1], dasm_seg);
1467: dasm_ofs = debugger_get_ofs(params[1]);
1468: }
1469: if(num == 3) {
1470: UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
1471: UINT32 end_ofs = debugger_get_ofs(params[2]);
1472:
1473: while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
1474: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1475: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1476: for(int i = 0; i < len; i++) {
1477: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1478: }
1479: for(int i = len; i < 8; i++) {
1480: telnet_printf(" ");
1481: }
1482: telnet_printf(" %s\n", buffer);
1483: if((dasm_ofs += len) > 0xffff) {
1484: dasm_seg += 0x1000;
1485: dasm_ofs -= 0x10000;
1486: }
1487: }
1488: } else {
1489: for(int i = 0; i < 16; i++) {
1490: int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
1491: telnet_printf("%04X:%04X ", dasm_seg, dasm_ofs);
1492: for(int i = 0; i < len; i++) {
1493: telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
1494: }
1495: for(int i = len; i < 8; i++) {
1496: telnet_printf(" ");
1497: }
1498: telnet_printf(" %s\n", buffer);
1499: if((dasm_ofs += len) > 0xffff) {
1500: dasm_seg += 0x1000;
1501: dasm_ofs -= 0x10000;
1502: }
1503: }
1504: }
1505: prev_command[1] = '\0'; // remove parameters to disassemble continuously
1506: } else {
1507: telnet_printf("invalid parameter number\n");
1508: }
1509: } else if(stricmp(params[0], "H") == 0) {
1510: if(num == 3) {
1511: UINT32 l = debugger_get_val(params[1]);
1512: UINT32 r = debugger_get_val(params[2]);
1513: telnet_printf("%08X %08X\n", l + r, l - r);
1514: } else {
1515: telnet_printf("invalid parameter number\n");
1516: }
1517: } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
1518: break_point_t *break_point_ptr;
1519: #define GET_BREAK_POINT_PTR() { \
1520: if(params[0][0] == 'R') { \
1521: break_point_ptr = &rd_break_point; \
1522: } else if(params[0][0] == 'W') { \
1523: break_point_ptr = &wr_break_point; \
1524: } else if(params[0][0] == 'I') { \
1525: break_point_ptr = &in_break_point; \
1526: } else if(params[0][0] == 'O') { \
1527: break_point_ptr = &out_break_point; \
1528: } else { \
1529: break_point_ptr = &break_point; \
1530: } \
1531: }
1532: GET_BREAK_POINT_PTR();
1533: if(num == 2) {
1534: UINT32 seg = debugger_get_seg(params[1], SREG(CS));
1535: UINT32 ofs = debugger_get_ofs(params[1]);
1536: bool found = false;
1537: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1538: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
1539: break_point_ptr->table[i].addr = (seg << 4) + ofs;
1540: break_point_ptr->table[i].seg = seg;
1541: break_point_ptr->table[i].ofs = ofs;
1542: break_point_ptr->table[i].status = 1;
1543: found = true;
1544: }
1545: }
1546: if(!found) {
1547: telnet_printf("too many break points\n");
1548: }
1549: } else {
1550: telnet_printf("invalid parameter number\n");
1551: }
1552: } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
1553: break_point_t *break_point_ptr;
1554: GET_BREAK_POINT_PTR();
1555: if(num == 2) {
1556: UINT32 addr = debugger_get_val(params[1]);
1557: bool found = false;
1558: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1559: if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
1560: break_point_ptr->table[i].addr = addr;
1561: break_point_ptr->table[i].status = 1;
1562: found = true;
1563: }
1564: }
1565: if(!found) {
1566: telnet_printf("too many break points\n");
1567: }
1568: } else {
1569: telnet_printf("invalid parameter number\n");
1570: }
1571: } 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) {
1572: break_point_t *break_point_ptr;
1573: GET_BREAK_POINT_PTR();
1574: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1575: memset(break_point_ptr, 0, sizeof(break_point_t));
1576: } else if(num >= 2) {
1577: for(int i = 1; i < num; i++) {
1578: int index = debugger_get_val(params[i]);
1579: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1580: telnet_printf("invalid index %x\n", index);
1581: } else {
1582: break_point_ptr->table[index - 1].addr = 0;
1583: break_point_ptr->table[index - 1].seg = 0;
1584: break_point_ptr->table[index - 1].ofs = 0;
1585: break_point_ptr->table[index - 1].status = 0;
1586: }
1587: }
1588: } else {
1589: telnet_printf("invalid parameter number\n");
1590: }
1591: } 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 ||
1592: 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) {
1593: break_point_t *break_point_ptr;
1594: GET_BREAK_POINT_PTR();
1595: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1596: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1597: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1598: if(break_point_ptr->table[i].status != 0) {
1599: break_point_ptr->table[i].status = enabled ? 1 : -1;
1600: }
1601: }
1602: } else if(num >= 2) {
1603: for(int i = 1; i < num; i++) {
1604: int index = debugger_get_val(params[i]);
1605: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1606: telnet_printf("invalid index %x\n", index);
1607: } else if(break_point_ptr->table[index - 1].status == 0) {
1608: telnet_printf("break point %x is null\n", index);
1609: } else {
1610: break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
1611: }
1612: }
1613: } else {
1614: telnet_printf("invalid parameter number\n");
1615: }
1616: } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
1617: break_point_t *break_point_ptr;
1618: GET_BREAK_POINT_PTR();
1619: if(num == 1) {
1620: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1621: if(break_point_ptr->table[i].status) {
1622: 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);
1623: }
1624: }
1625: } else {
1626: telnet_printf("invalid parameter number\n");
1627: }
1628: } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
1629: break_point_t *break_point_ptr;
1630: GET_BREAK_POINT_PTR();
1631: if(num == 1) {
1632: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1633: if(break_point_ptr->table[i].status) {
1634: telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
1635: }
1636: }
1637: } else {
1638: telnet_printf("invalid parameter number\n");
1639: }
1640: } else if(stricmp(params[0], "INTBP") == 0) {
1641: if(num >= 2 && num <= 4) {
1642: int int_num = debugger_get_val(params[1]);
1643: UINT8 ah = 0, ah_registered = 0;
1644: UINT8 al = 0, al_registered = 0;
1645: if(num >= 3) {
1646: ah = debugger_get_val(params[2]);
1647: ah_registered = 1;
1648: }
1649: if(num == 4) {
1650: al = debugger_get_val(params[3]);
1651: al_registered = 1;
1652: }
1653: bool found = false;
1654: for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
1655: if(int_break_point.table[i].status == 0 || (
1656: int_break_point.table[i].int_num == int_num &&
1657: int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
1658: int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
1659: int_break_point.table[i].int_num = int_num;
1660: int_break_point.table[i].ah = ah;
1661: int_break_point.table[i].ah_registered = ah_registered;
1662: int_break_point.table[i].al = al;
1663: int_break_point.table[i].al_registered = al_registered;
1664: int_break_point.table[i].status = 1;
1665: found = true;
1666: }
1667: }
1668: if(!found) {
1669: telnet_printf("too many break points\n");
1670: }
1671: } else {
1672: telnet_printf("invalid parameter number\n");
1673: }
1674: } else if(stricmp(params[0], "INTBC") == 0) {
1675: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1676: memset(&int_break_point, 0, sizeof(int_break_point_t));
1677: } else if(num >= 2) {
1678: for(int i = 1; i < num; i++) {
1679: int index = debugger_get_val(params[i]);
1680: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1681: telnet_printf("invalid index %x\n", index);
1682: } else {
1683: int_break_point.table[index - 1].int_num = 0;
1684: int_break_point.table[index - 1].ah = 0;
1685: int_break_point.table[index - 1].ah_registered = 0;
1686: int_break_point.table[index - 1].al = 0;
1687: int_break_point.table[index - 1].al_registered = 0;
1688: int_break_point.table[index - 1].status = 0;
1689: }
1690: }
1691: } else {
1692: telnet_printf("invalid parameter number\n");
1693: }
1694: } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
1695: bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
1696: if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
1697: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1698: if(int_break_point.table[i].status != 0) {
1699: int_break_point.table[i].status = enabled ? 1 : -1;
1700: }
1701: }
1702: } else if(num >= 2) {
1703: for(int i = 1; i < num; i++) {
1704: int index = debugger_get_val(params[i]);
1705: if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
1706: telnet_printf("invalid index %x\n", index);
1707: } else if(int_break_point.table[index - 1].status == 0) {
1708: telnet_printf("break point %x is null\n", index);
1709: } else {
1710: int_break_point.table[index - 1].status = enabled ? 1 : -1;
1711: }
1712: }
1713: } else {
1714: telnet_printf("invalid parameter number\n");
1715: }
1716: } else if(stricmp(params[0], "INTBL") == 0) {
1717: if(num == 1) {
1718: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
1719: if(int_break_point.table[i].status) {
1720: telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
1721: if(int_break_point.table[i].ah_registered) {
1722: telnet_printf(" %02X", int_break_point.table[i].ah);
1723: }
1724: if(int_break_point.table[i].al_registered) {
1725: telnet_printf(" %02X", int_break_point.table[i].al);
1726: }
1727: telnet_printf("\n");
1728: }
1729: }
1730: } else {
1731: telnet_printf("invalid parameter number\n");
1732: }
1733: } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
1734: if(num == 1 || num == 2) {
1735: break_point_t break_point_stored;
1736: bool break_points_stored = false;
1737:
1738: if(stricmp(params[0], "P") == 0) {
1739: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1740: memset(&break_point, 0, sizeof(break_point_t));
1741: break_points_stored = true;
1742:
1743: break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
1744: break_point.table[0].status = 1;
1745: } else if(num >= 2) {
1746: memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
1747: memset(&break_point, 0, sizeof(break_point_t));
1748: break_points_stored = true;
1749:
1750: UINT32 seg = debugger_get_seg(params[1], SREG(CS));
1751: UINT32 ofs = debugger_get_ofs(params[1]);
1752: break_point.table[0].addr = (seg << 4) + ofs;
1753: break_point.table[0].seg = seg;
1754: break_point.table[0].ofs = ofs;
1755: break_point.table[0].status = 1;
1756: }
1757: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1758: now_going = true;
1759: now_suspended = false;
1760:
1761: telnet_command("\033[2l"); // key unlock
1762: while(!m_halted && !now_suspended) {
1763: if(telnet_kbhit()) {
1764: break;
1765: }
1766: Sleep(10);
1767: }
1768: now_going = false;
1769: telnet_command("\033[2h"); // key lock
1770:
1771: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1772: Sleep(100);
1773: if(!m_halted && !now_suspended) {
1774: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1775: telnet_printf("waiting until cpu is suspended...\n");
1776: }
1777: }
1778: while(!m_halted && !now_suspended) {
1779: if(telnet_disconnected()) {
1780: break;
1781: }
1782: Sleep(10);
1783: }
1784: dasm_seg = SREG(CS);
1785: dasm_ofs = m_eip;
1786:
1787: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1788: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1789: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1790:
1791: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1792: debugger_regs_info(buffer);
1793: telnet_printf("%s", buffer);
1794:
1795: if(break_point.hit) {
1796: if(stricmp(params[0], "G") == 0 && num == 1) {
1797: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1798: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1799: }
1800: } else if(rd_break_point.hit) {
1801: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1802: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1803: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1804: m_prev_cs, m_prev_eip);
1805: } else if(wr_break_point.hit) {
1806: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1807: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1808: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1809: m_prev_cs, m_prev_eip);
1810: } else if(in_break_point.hit) {
1811: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1812: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
1813: in_break_point.table[in_break_point.hit - 1].addr,
1814: m_prev_cs, m_prev_eip);
1815: } else if(out_break_point.hit) {
1816: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1817: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
1818: out_break_point.table[out_break_point.hit - 1].addr,
1819: m_prev_cs, m_prev_eip);
1820: } else if(int_break_point.hit) {
1821: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1822: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
1823: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
1824: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
1825: }
1826: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
1827: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
1828: }
1829: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
1830: } else {
1831: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1832: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
1833: }
1834: if(break_points_stored) {
1835: memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
1836: }
1837: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1838: debugger_dasm(buffer, SREG(CS), m_eip);
1839: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1840: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1841: } else {
1842: telnet_printf("invalid parameter number\n");
1843: }
1844: } else if(stricmp(params[0], "T") == 0) {
1845: if(num == 1 || num == 2) {
1846: int steps = 1;
1847: if(num >= 2) {
1848: steps = debugger_get_val(params[1]);
1849: }
1850:
1851: telnet_command("\033[2l"); // key unlock
1852: while(steps-- > 0) {
1853: break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
1854: now_going = false;
1855: now_suspended = false;
1856:
1857: while(!m_halted && !now_suspended) {
1858: if(telnet_disconnected()) {
1859: break;
1860: }
1861: Sleep(10);
1862: }
1863: dasm_seg = SREG(CS);
1864: dasm_ofs = m_eip;
1865:
1866: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1867: debugger_dasm(buffer, m_prev_cs, m_prev_eip);
1868: telnet_printf("done\t%04X:%04X %s\n", m_prev_cs, m_prev_eip, buffer);
1869:
1870: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1871: debugger_regs_info(buffer);
1872: telnet_printf("%s", buffer);
1873:
1874: 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()) {
1875: break;
1876: }
1877: }
1878: telnet_command("\033[2h"); // key lock
1879:
1880: if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
1881: Sleep(100);
1882: if(!m_halted && !now_suspended) {
1883: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1884: telnet_printf("waiting until cpu is suspended...\n");
1885: }
1886: }
1887: while(!m_halted && !now_suspended) {
1888: if(telnet_disconnected()) {
1889: break;
1890: }
1891: Sleep(10);
1892: }
1893: if(break_point.hit) {
1894: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1895: telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
1896: } else if(rd_break_point.hit) {
1897: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1898: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
1899: rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
1900: m_prev_cs, m_prev_eip);
1901: } else if(wr_break_point.hit) {
1902: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1903: telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
1904: wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
1905: m_prev_cs, m_prev_eip);
1906: } else if(in_break_point.hit) {
1907: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1908: telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
1909: in_break_point.table[in_break_point.hit - 1].addr,
1910: m_prev_cs, m_prev_eip);
1911: } else if(out_break_point.hit) {
1912: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1913: telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
1914: out_break_point.table[out_break_point.hit - 1].addr,
1915: m_prev_cs, m_prev_eip);
1916: } else if(int_break_point.hit) {
1917: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1918: telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
1919: if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
1920: telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
1921: }
1922: if(int_break_point.table[int_break_point.hit - 1].al_registered) {
1923: telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
1924: }
1925: telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
1926: } else if(steps > 0) {
1927: telnet_set_color(TELNET_RED | TELNET_INTENSITY);
1928: telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
1929: }
1930: telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1931: debugger_dasm(buffer, SREG(CS), m_eip);
1932: telnet_printf("next\t%04X:%04X %s\n", SREG(CS), m_eip, buffer);
1933: telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
1934: } else {
1935: telnet_printf("invalid parameter number\n");
1936: }
1937: } else if(stricmp(params[0], "Q") == 0) {
1938: break;
1939: } else if(stricmp(params[0], "X") == 0) {
1940: debugger_process_info(buffer);
1941: telnet_printf("%s", buffer);
1942: } else if(stricmp(params[0], ">") == 0) {
1943: if(num == 2) {
1944: if(fp_debugger != NULL) {
1945: fclose(fp_debugger);
1946: fp_debugger = NULL;
1947: }
1948: fp_debugger = fopen(params[1], "w");
1949: } else {
1950: telnet_printf("invalid parameter number\n");
1951: }
1952: } else if(stricmp(params[0], "<") == 0) {
1953: if(num == 2) {
1954: if(fi_debugger != NULL) {
1955: fclose(fi_debugger);
1956: fi_debugger = NULL;
1957: }
1958: fi_debugger = fopen(params[1], "r");
1959: } else {
1960: telnet_printf("invalid parameter number\n");
1961: }
1962: } else if(stricmp(params[0], "?") == 0) {
1963: telnet_printf("D [<start> [<end>]] - dump memory\n");
1964: telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
1965: telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
1966: telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
1967: telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
1968:
1969: telnet_printf("R - show registers\n");
1970: telnet_printf("R <reg> <value> - edit register\n");
1971: telnet_printf("S <start> <end> <list> - search\n");
1972: telnet_printf("U [<start> [<end>]] - unassemble\n");
1973:
1974: telnet_printf("H <value> <value> - hexadd\n");
1975:
1976: telnet_printf("BP <address> - set breakpoint\n");
1977: telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
1978: telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
1979: telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
1980: telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
1981: telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
1982:
1983: telnet_printf("G - go (press enter key to break)\n");
1984: telnet_printf("G <address> - go and break at address\n");
1985: telnet_printf("P - trace one opcode (step over)\n");
1986: telnet_printf("T [<count>] - trace (step in)\n");
1987: telnet_printf("Q - quit\n");
1988: telnet_printf("X - show dos process info\n");
1989:
1990: telnet_printf("> <filename> - output logfile\n");
1991: telnet_printf("< <filename> - input commands from file\n");
1992:
1993: telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
1994: telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
1995: } else {
1996: telnet_printf("unknown command %s\n", params[0]);
1997: }
1998: }
1999: }
2000: if(fp_debugger != NULL) {
2001: fclose(fp_debugger);
2002: fp_debugger = NULL;
2003: }
2004: if(fi_debugger != NULL) {
2005: fclose(fi_debugger);
2006: fi_debugger = NULL;
2007: }
2008: now_debugging = now_going = now_suspended = force_suspend = false;
2009: closesocket(cli_socket);
2010: }
2011:
2012: const char *debugger_get_ttermpro_path()
2013: {
2014: static char path[MAX_PATH] = {0};
2015:
2016: if(getenv("ProgramFiles")) {
2017: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
2018: }
2019: return(path);
2020: }
2021:
2022: const char *debugger_get_ttermpro_x86_path()
2023: {
2024: static char path[MAX_PATH] = {0};
2025:
2026: if(getenv("ProgramFiles(x86)")) {
2027: sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
2028: }
2029: return(path);
2030: }
2031:
2032: const char *debugger_get_putty_path()
2033: {
2034: static char path[MAX_PATH] = {0};
2035:
2036: if(getenv("ProgramFiles")) {
2037: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
2038: }
2039: return(path);
2040: }
2041:
2042: const char *debugger_get_putty_x86_path()
2043: {
2044: static char path[MAX_PATH] = {0};
2045:
2046: if(getenv("ProgramFiles(x86)")) {
2047: sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
2048: }
2049: return(path);
2050: }
2051:
2052: const char *debugger_get_telnet_path()
2053: {
2054: // NOTE: When you run 32bit version of msdos.exe on Windows x64,
2055: // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
2056: // But 32bit version of telnet.exe will not be installed in SysWOW64
2057: // and 64bit version of telnet.exe will be installed in System32.
2058: static char path[MAX_PATH] = {0};
2059:
2060: if(getenv("windir") != NULL) {
2061: sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
2062: }
2063: return(path);
2064: }
2065:
2066: DWORD WINAPI debugger_thread(LPVOID)
2067: {
2068: WSADATA was_data;
2069: struct sockaddr_in svr_addr;
2070: struct sockaddr_in cli_addr;
2071: int cli_addr_len = sizeof(cli_addr);
2072: int port = 23;
2073: int bind_stat = SOCKET_ERROR;
2074: struct timeval timeout;
2075:
2076: WSAStartup(MAKEWORD(2,0), &was_data);
2077:
2078: if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
2079: memset(&svr_addr, 0, sizeof(svr_addr));
2080: svr_addr.sin_family = AF_INET;
2081: svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
2082:
2083: while(!m_halted && port < 10000) {
2084: svr_addr.sin_port = htons(port);
2085: if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
2086: break;
2087: } else {
2088: port = (port == 23) ? 9000 : (port + 1);
2089: }
2090: }
2091: if(bind_stat == 0) {
2092: timeout.tv_sec = 1;
2093: timeout.tv_usec = 0;
2094: setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
2095:
2096: listen(svr_socket, 1);
2097:
2098: char command[MAX_PATH] = {0};
2099: STARTUPINFO si;
2100: PROCESS_INFORMATION pi;
2101:
2102: if(_access(debugger_get_ttermpro_path(), 0) == 0) {
2103: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
2104: } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
2105: sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
2106: } else if(_access(debugger_get_putty_path(), 0) == 0) {
2107: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
2108: } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
2109: sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
2110: } else if(_access(debugger_get_telnet_path(), 0) == 0) {
2111: sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
2112: }
2113: if(command[0] != '\0') {
2114: memset(&si, 0, sizeof(STARTUPINFO));
2115: memset(&pi, 0, sizeof(PROCESS_INFORMATION));
2116: CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
2117: }
2118:
2119: while(!m_halted) {
2120: if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
2121: u_long val = 1;
2122: ioctlsocket(cli_socket, FIONBIO, &val);
2123: debugger_main();
2124: }
2125: }
2126: }
2127: }
2128: WSACleanup();
2129: return(0);
2130: }
2131: #endif
2132:
2133: /* ----------------------------------------------------------------------------
1.1 root 2134: main
2135: ---------------------------------------------------------------------------- */
2136:
1.1.1.28 root 2137: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
2138: {
2139: if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33 root 2140: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 2141: #ifdef USE_SERVICE_THREAD
2142: EnterCriticalSection(&key_buf_crit_sect);
2143: #endif
1.1.1.33 root 2144: key_buf_char->clear();
2145: key_buf_scan->clear();
1.1.1.35 root 2146: #ifdef USE_SERVICE_THREAD
2147: LeaveCriticalSection(&key_buf_crit_sect);
2148: #endif
1.1.1.33 root 2149: }
2150: // key_code = key_recv = 0;
1.1.1.28 root 2151: return TRUE;
2152: } else if(dwCtrlType == CTRL_C_EVENT) {
2153: return TRUE;
2154: } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
2155: // this program will be terminated abnormally, do minimum end process
2156: exit_handler();
2157: exit(1);
2158: }
2159: return FALSE;
2160: }
2161:
2162: void exit_handler()
2163: {
2164: if(temp_file_created) {
2165: DeleteFile(temp_file_path);
2166: temp_file_created = false;
2167: }
2168: if(key_buf_char != NULL) {
2169: key_buf_char->release();
2170: delete key_buf_char;
2171: key_buf_char = NULL;
2172: }
2173: if(key_buf_scan != NULL) {
2174: key_buf_scan->release();
2175: delete key_buf_scan;
2176: key_buf_scan = NULL;
2177: }
1.1.1.32 root 2178: #ifdef SUPPORT_XMS
2179: msdos_xms_release();
2180: #endif
1.1.1.28 root 2181: hardware_release();
2182: }
2183:
1.1.1.35 root 2184: #ifdef USE_VRAM_THREAD
1.1.1.28 root 2185: DWORD WINAPI vram_thread(LPVOID)
2186: {
2187: while(!m_halted) {
2188: EnterCriticalSection(&vram_crit_sect);
2189: if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
2190: vram_flush_char();
2191: }
2192: if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
2193: vram_flush_attr();
2194: }
2195: vram_last_length_char = vram_length_char;
2196: vram_last_length_attr = vram_length_attr;
2197: LeaveCriticalSection(&vram_crit_sect);
2198: // this is about half the maximum keyboard repeat rate - any
2199: // lower tends to be jerky, any higher misses updates
2200: Sleep(15);
2201: }
2202: return 0;
2203: }
2204: #endif
2205:
2206: long get_section_in_exec_file(FILE *fp, char *name)
2207: {
2208: UINT8 header[0x400];
2209:
2210: long position = ftell(fp);
2211: fseek(fp, 0, SEEK_SET);
2212: fread(header, sizeof(header), 1, fp);
2213: fseek(fp, position, SEEK_SET);
2214:
2215: try {
2216: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2217: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2218: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2219: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2220: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2221: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2222:
2223: for(int i = 0; i < coffHeader->NumberOfSections; i++) {
2224: _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
2225: if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
2226: return(sectionHeader->PointerToRawData);
2227: }
2228: }
2229: } catch(...) {
2230: }
2231: return(0);
2232: }
2233:
1.1.1.10 root 2234: bool is_started_from_command_prompt()
2235: {
1.1.1.18 root 2236: bool ret = false;
2237:
2238: HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
2239: if(hLibrary) {
2240: typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
2241: GetConsoleProcessListFunction lpfnGetConsoleProcessList;
2242: lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
2243: if(lpfnGetConsoleProcessList) {
2244: DWORD pl;
2245: ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
2246: FreeLibrary(hLibrary);
2247: return(ret);
2248: }
2249: FreeLibrary(hLibrary);
2250: }
2251:
2252: HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2253: if(hSnapshot != INVALID_HANDLE_VALUE) {
2254: DWORD dwParentProcessID = 0;
2255: PROCESSENTRY32 pe32;
2256: pe32.dwSize = sizeof(PROCESSENTRY32);
2257: if(Process32First(hSnapshot, &pe32)) {
2258: do {
2259: if(pe32.th32ProcessID == GetCurrentProcessId()) {
2260: dwParentProcessID = pe32.th32ParentProcessID;
2261: break;
2262: }
2263: } while(Process32Next(hSnapshot, &pe32));
2264: }
2265: CloseHandle(hSnapshot);
2266: if(dwParentProcessID != 0) {
2267: HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
2268: if(hProcess != NULL) {
2269: HMODULE hMod;
2270: DWORD cbNeeded;
2271: if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
2272: char module_name[MAX_PATH];
2273: if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
2274: ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
2275: }
2276: }
2277: CloseHandle(hProcess);
2278: }
2279: }
2280: }
2281: return(ret);
1.1.1.14 root 2282: }
2283:
2284: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
2285: {
1.1.1.24 root 2286: // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14 root 2287: OSVERSIONINFOEX osvi;
2288: DWORDLONG dwlConditionMask = 0;
2289: int op = VER_GREATER_EQUAL;
2290:
2291: // Initialize the OSVERSIONINFOEX structure.
2292: ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
2293: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
2294: osvi.dwMajorVersion = dwMajorVersion;
2295: osvi.dwMinorVersion = dwMinorVersion;
2296: osvi.wServicePackMajor = wServicePackMajor;
2297: osvi.wServicePackMinor = wServicePackMinor;
2298:
2299: // Initialize the condition mask.
2300: VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
2301: VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
2302: VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
2303: VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
2304:
2305: // Perform the test.
2306: return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
2307: }
2308:
1.1.1.27 root 2309: void get_sio_port_numbers()
2310: {
2311: SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
2312: HDEVINFO hDevInfo = 0;
2313: HKEY hKey = 0;
2314: if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
2315: for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
2316: if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
2317: char chData[256];
2318: DWORD dwType = 0;
2319: DWORD dwSize = sizeof(chData);
2320: int port_number = 0;
2321:
2322: if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
2323: if(_strnicmp(chData, "COM", 3) == 0) {
2324: port_number = atoi(chData + 3);
2325: }
2326: }
2327: RegCloseKey(hKey);
2328:
1.1.1.29 root 2329: 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 2330: continue;
2331: }
2332: if(sio_port_number[0] == 0) {
2333: sio_port_number[0] = port_number;
2334: } else if(sio_port_number[1] == 0) {
2335: sio_port_number[1] = port_number;
1.1.1.29 root 2336: } else if(sio_port_number[2] == 0) {
2337: sio_port_number[2] = port_number;
2338: } else if(sio_port_number[3] == 0) {
2339: sio_port_number[3] = port_number;
1.1.1.27 root 2340: }
1.1.1.29 root 2341: 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 2342: break;
2343: }
2344: }
2345: }
2346: }
2347: }
2348:
1.1.1.28 root 2349: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
2350:
1.1 root 2351: int main(int argc, char *argv[], char *envp[])
2352: {
1.1.1.9 root 2353: int arg_offset = 0;
2354: int standard_env = 0;
1.1.1.14 root 2355: int buf_width = 0, buf_height = 0;
1.1.1.28 root 2356: bool get_console_info_success = false;
2357: bool screen_size_changed = false;
2358:
2359: _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
2360: GetModuleFileName(NULL, path, MAX_PATH);
2361: GetFullPathName(path, MAX_PATH, full, &name);
1.1 root 2362:
1.1.1.27 root 2363: char dummy_argv_0[] = "msdos.exe";
2364: char dummy_argv_1[MAX_PATH];
2365: char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
2366: char new_exec_file[MAX_PATH];
2367: bool convert_cmd_file = false;
1.1.1.28 root 2368: unsigned int code_page = 0;
1.1.1.27 root 2369:
2370: if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28 root 2371: // check if command file is embedded to this execution file
2372: // if this execution file name is msdos.exe, don't check
1.1.1.27 root 2373: FILE* fp = fopen(full, "rb");
1.1.1.28 root 2374: long offset = get_section_in_exec_file(fp, ".msdos");
2375: if(offset != 0) {
1.1.1.30 root 2376: UINT8 buffer[16];
1.1.1.28 root 2377: fseek(fp, offset, SEEK_SET);
2378: fread(buffer, sizeof(buffer), 1, fp);
2379:
2380: // restore flags
2381: stay_busy = ((buffer[0] & 0x01) != 0);
2382: no_windows = ((buffer[0] & 0x02) != 0);
2383: standard_env = ((buffer[0] & 0x04) != 0);
2384: ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
2385: limit_max_memory = ((buffer[0] & 0x10) != 0);
2386: if((buffer[0] & 0x20) != 0) {
2387: get_sio_port_numbers();
2388: }
2389: if((buffer[0] & 0x40) != 0) {
2390: UMB_TOP = EMS_TOP + EMS_SIZE;
2391: support_ems = true;
1.1.1.30 root 2392: }
1.1.1.27 root 2393: #ifdef SUPPORT_XMS
1.1.1.30 root 2394: if((buffer[0] & 0x80) != 0) {
1.1.1.28 root 2395: support_xms = true;
2396: }
1.1.1.30 root 2397: #endif
1.1.1.28 root 2398: if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
2399: buf_width = buffer[1] | (buffer[2] << 8);
2400: buf_height = buffer[3] | (buffer[4] << 8);
2401: }
2402: if(buffer[5] != 0) {
1.1.1.30 root 2403: dos_major_version = buffer[5];
2404: dos_minor_version = buffer[6];
2405: }
2406: if(buffer[7] != 0) {
2407: win_major_version = buffer[7];
2408: win_minor_version = buffer[8];
1.1.1.28 root 2409: }
1.1.1.30 root 2410: if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28 root 2411: SetConsoleCP(code_page);
2412: SetConsoleOutputCP(code_page);
2413: }
1.1.1.30 root 2414: int name_len = buffer[11];
2415: int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28 root 2416:
2417: // restore command file name
2418: memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
2419: fread(dummy_argv_1, name_len, 1, fp);
2420:
2421: if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
2422: // if original command file exists, create a temporary file name
2423: if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
2424: // create a temporary command file in the current director
2425: DeleteFile(dummy_argv_1);
1.1.1.27 root 2426: } else {
1.1.1.28 root 2427: // create a temporary command file in the temporary folder
2428: GetTempPath(MAX_PATH, path);
2429: if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
2430: DeleteFile(dummy_argv_1);
2431: } else {
2432: sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
2433: }
1.1.1.27 root 2434: }
1.1.1.28 root 2435: // check the command file type
2436: fread(buffer, 2, 1, fp);
2437: fseek(fp, -2, SEEK_CUR);
2438: if(memcmp(buffer, "MZ", 2) != 0) {
2439: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
2440: } else {
2441: memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27 root 2442: }
2443: }
1.1.1.28 root 2444:
2445: // restore command file
2446: FILE* fo = fopen(dummy_argv_1, "wb");
2447: for(int i = 0; i < file_len; i++) {
2448: fputc(fgetc(fp), fo);
2449: }
2450: fclose(fo);
2451:
2452: GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
2453: temp_file_created = true;
2454: SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
2455:
2456: // adjust argc/argv
2457: for(int i = 1; i < argc && (i + 1) < 256; i++) {
2458: dummy_argv[i + 1] = argv[i];
2459: }
2460: argc++;
2461: argv = dummy_argv;
1.1.1.27 root 2462: }
2463: fclose(fp);
2464: }
1.1.1.9 root 2465: for(int i = 1; i < argc; i++) {
1.1.1.25 root 2466: if(_strnicmp(argv[i], "-b", 2) == 0) {
2467: stay_busy = true;
2468: arg_offset++;
1.1.1.27 root 2469: } else if(_strnicmp(argv[i], "-c", 2) == 0) {
2470: if(argv[i][2] != '\0') {
2471: strcpy(new_exec_file, &argv[i][2]);
2472: } else {
2473: strcpy(new_exec_file, "new_exec_file.exe");
2474: }
2475: convert_cmd_file = true;
2476: arg_offset++;
1.1.1.28 root 2477: } else if(_strnicmp(argv[i], "-p", 2) == 0) {
2478: if(IS_NUMERIC(argv[i][2])) {
2479: code_page = atoi(&argv[i][2]);
2480: } else {
2481: code_page = GetConsoleCP();
2482: }
2483: arg_offset++;
1.1.1.25 root 2484: } else if(_strnicmp(argv[i], "-d", 2) == 0) {
2485: no_windows = true;
2486: arg_offset++;
2487: } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9 root 2488: standard_env = 1;
2489: arg_offset++;
1.1.1.14 root 2490: } else if(_strnicmp(argv[i], "-i", 2) == 0) {
2491: ignore_illegal_insn = true;
2492: arg_offset++;
2493: } else if(_strnicmp(argv[i], "-m", 2) == 0) {
2494: limit_max_memory = true;
2495: arg_offset++;
2496: } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17 root 2497: if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
2498: buf_width = buf_height = 0;
2499: }
2500: if(buf_width <= 0 || buf_width > 0x7fff) {
2501: buf_width = 80;
2502: }
2503: if(buf_height <= 0 || buf_height > 0x7fff) {
2504: buf_height = 25;
2505: }
1.1.1.14 root 2506: arg_offset++;
1.1.1.25 root 2507: } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28 root 2508: if(IS_NUMERIC(argv[i][2])) {
1.1.1.29 root 2509: char *p0 = &argv[i][2], *p1, *p2, *p3;
2510: if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
2511: sio_port_number[1] = atoi(p1 + 1);
2512: if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
2513: sio_port_number[2] = atoi(p2 + 1);
2514: if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
2515: sio_port_number[3] = atoi(p3 + 1);
2516: }
2517: }
1.1.1.25 root 2518: }
1.1.1.29 root 2519: sio_port_number[0] = atoi(p0);
1.1.1.25 root 2520: }
1.1.1.29 root 2521: 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 2522: get_sio_port_numbers();
1.1.1.25 root 2523: }
2524: arg_offset++;
1.1.1.9 root 2525: } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17 root 2526: 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 2527: dos_major_version = argv[i][2] - '0';
2528: dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
2529: }
2530: arg_offset++;
2531: } else if(_strnicmp(argv[i], "-w", 2) == 0) {
2532: 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]))) {
2533: win_major_version = argv[i][2] - '0';
2534: win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9 root 2535: }
2536: arg_offset++;
1.1.1.25 root 2537: } else if(_strnicmp(argv[i], "-x", 2) == 0) {
2538: UMB_TOP = EMS_TOP + EMS_SIZE;
2539: support_ems = true;
2540: #ifdef SUPPORT_XMS
2541: support_xms = true;
2542: #endif
2543: arg_offset++;
1.1.1.9 root 2544: } else {
2545: break;
2546: }
2547: }
2548: if(argc < 2 + arg_offset) {
1.1 root 2549: #ifdef _WIN64
1.1.1.14 root 2550: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1 root 2551: #else
1.1.1.14 root 2552: fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1 root 2553: #endif
1.1.1.25 root 2554: fprintf(stderr,
1.1.1.28 root 2555: "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30 root 2556: " [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25 root 2557: "\n"
2558: "\t-b\tstay busy during keyboard polling\n"
1.1.1.28 root 2559: #ifdef _WIN64
1.1.1.27 root 2560: "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28 root 2561: #else
1.1.1.27 root 2562: "\t-c\tconvert command file to 32bit execution file\n"
2563: #endif
1.1.1.28 root 2564: "\t-p\trecord current code page when convert command file\n"
1.1.1.25 root 2565: "\t-d\tpretend running under straight DOS, not Windows\n"
2566: "\t-e\tuse a reduced environment block\n"
2567: "\t-i\tignore invalid instructions\n"
2568: "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
2569: "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
2570: "\t-s\tenable serial I/O and set host's COM port numbers\n"
2571: "\t-v\tset the DOS version\n"
1.1.1.30 root 2572: "\t-w\tset the Windows version\n"
1.1.1.19 root 2573: #ifdef SUPPORT_XMS
1.1.1.28 root 2574: "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19 root 2575: #else
1.1.1.28 root 2576: "\t-x\tenable LIM EMS\n"
1.1.1.19 root 2577: #endif
2578: );
1.1.1.10 root 2579:
2580: if(!is_started_from_command_prompt()) {
2581: fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
2582: while(!_kbhit()) {
2583: Sleep(10);
2584: }
2585: }
1.1.1.20 root 2586: #ifdef _DEBUG
2587: _CrtDumpMemoryLeaks();
2588: #endif
1.1 root 2589: return(EXIT_FAILURE);
2590: }
1.1.1.27 root 2591: if(convert_cmd_file) {
2592: retval = EXIT_FAILURE;
1.1.1.28 root 2593: if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27 root 2594: FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28 root 2595: int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27 root 2596:
1.1.1.28 root 2597: if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
2598: fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
2599: } else if((fp = fopen(full, "rb")) == NULL) {
2600: fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27 root 2601: } else {
1.1.1.28 root 2602: long offset = get_section_in_exec_file(fp, ".msdos");
2603: if(offset != 0) {
2604: UINT8 buffer[14];
2605: fseek(fp, offset, SEEK_SET);
2606: fread(buffer, sizeof(buffer), 1, fp);
2607: memset(path, 0, sizeof(path));
2608: fread(path, buffer[9], 1, fp);
2609: fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
2610: } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
2611: fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
2612: } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
2613: fprintf(stderr, "Can't open '%s'\n", new_exec_file);
2614: } else {
2615: // read pe header of msdos.exe
2616: UINT8 header[0x400];
2617: fseek(fp, 0, SEEK_SET);
2618: fread(header, sizeof(header), 1, fp);
2619:
2620: _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
2621: DWORD dwTopOfSignature = dosHeader->e_lfanew;
2622: DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
2623: _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
2624: DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
2625: _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
2626: DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
2627:
2628: _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
2629: DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
2630: DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
2631: DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
2632: if(dwExtraLastSectionBytes != 0) {
2633: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
2634: dwLastSectionSize += dwRemain;
2635: }
2636: DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
2637:
2638: // store msdos.exe
2639: fseek(fp, 0, SEEK_SET);
2640: for(int i = 0; i < dwEndOfFile; i++) {
2641: if((data = fgetc(fp)) != EOF) {
2642: fputc(data, fo);
2643: } else {
2644: // we should not reach here :-(
2645: fputc(0, fo);
2646: }
2647: }
2648:
2649: // store options
2650: UINT8 flags = 0;
2651: if(stay_busy) {
2652: flags |= 0x01;
2653: }
2654: if(no_windows) {
2655: flags |= 0x02;
2656: }
2657: if(standard_env) {
2658: flags |= 0x04;
2659: }
2660: if(ignore_illegal_insn) {
2661: flags |= 0x08;
2662: }
2663: if(limit_max_memory) {
2664: flags |= 0x10;
2665: }
1.1.1.29 root 2666: 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 2667: flags |= 0x20;
2668: }
2669: if(support_ems) {
2670: flags |= 0x40;
2671: }
1.1.1.30 root 2672: #ifdef SUPPORT_XMS
2673: if(support_xms) {
2674: flags |= 0x80;
2675: }
2676: #endif
1.1.1.28 root 2677:
2678: fputc(flags, fo);
2679: fputc((buf_width >> 0) & 0xff, fo);
2680: fputc((buf_width >> 8) & 0xff, fo);
2681: fputc((buf_height >> 0) & 0xff, fo);
2682: fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30 root 2683: fputc(dos_major_version, fo);
2684: fputc(dos_minor_version, fo);
2685: fputc(win_major_version, fo);
2686: fputc(win_minor_version, fo);
1.1.1.28 root 2687: fputc((code_page >> 0) & 0xff, fo);
2688: fputc((code_page >> 8) & 0xff, fo);
2689:
2690: // store command file info
2691: GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
2692: int name_len = strlen(name);
2693: fseek(fs, 0, SEEK_END);
2694: long file_size = ftell(fs);
2695:
2696: fputc(name_len, fo);
2697: fputc((file_size >> 0) & 0xff, fo);
2698: fputc((file_size >> 8) & 0xff, fo);
2699: fputc((file_size >> 16) & 0xff, fo);
2700: fputc((file_size >> 24) & 0xff, fo);
2701: fwrite(name, name_len, 1, fo);
2702:
2703: // store command file
2704: fseek(fs, 0, SEEK_SET);
2705: for(int i = 0; i < file_size; i++) {
2706: if((data = fgetc(fs)) != EOF) {
2707: fputc(data, fo);
2708: } else {
2709: // we should not reach here :-(
2710: fputc(0, fo);
2711: }
2712: }
2713:
2714: // store padding data and update pe header
1.1.1.29 root 2715: _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
2716: coffHeader->NumberOfSections++;
2717: memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
2718: memcpy(newSectionHeader->Name, ".msdos", 6);
2719: newSectionHeader->VirtualAddress = dwVirtualAddress;
2720: newSectionHeader->PointerToRawData = dwEndOfFile;
2721: newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28 root 2722: newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
2723: DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
2724: if(dwExtraRawBytes != 0) {
1.1.1.29 root 2725: static const char padding[] = "PADDINGXXPADDING";
1.1.1.28 root 2726: DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
2727: for(int i = 0; i < dwRemain; i++) {
1.1.1.29 root 2728: if(i < 2) {
2729: fputc(padding[i & 15], fo);
2730: } else {
2731: fputc(padding[(i - 2) & 15], fo);
2732: }
1.1.1.28 root 2733: }
2734: newSectionHeader->SizeOfRawData += dwRemain;
2735: }
2736: newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
2737:
2738: DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
2739: DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
2740: if(dwExtraNewSectionBytes != 0) {
2741: DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
2742: dwNewSectionSize += dwRemain;
2743: }
2744: optionalHeader->SizeOfImage += dwNewSectionSize;
2745:
2746: fseek(fo, 0, SEEK_SET);
2747: fwrite(header, sizeof(header), 1, fo);
2748:
2749: fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
2750: retval = EXIT_SUCCESS;
1.1.1.27 root 2751: }
2752: }
2753: if(fp != NULL) {
2754: fclose(fp);
2755: }
2756: if(fs != NULL) {
2757: fclose(fs);
2758: }
2759: if(fo != NULL) {
2760: fclose(fo);
2761: }
2762: }
2763: #ifdef _DEBUG
2764: _CrtDumpMemoryLeaks();
2765: #endif
2766: return(retval);
2767: }
1.1 root 2768:
1.1.1.14 root 2769: is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
2770:
1.1.1.23 root 2771: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 2772: CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14 root 2773: CONSOLE_CURSOR_INFO ci;
1.1.1.23 root 2774:
1.1.1.28 root 2775: get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14 root 2776: GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24 root 2777: GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1 root 2778:
1.1.1.14 root 2779: for(int y = 0; y < SCR_BUF_WIDTH; y++) {
2780: for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
2781: SCR_BUF(y,x).Char.AsciiChar = ' ';
2782: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 2783: }
2784: }
1.1.1.28 root 2785: if(get_console_info_success) {
1.1.1.12 root 2786: scr_width = csbi.dwSize.X;
1.1.1.14 root 2787: scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
2788:
1.1.1.28 root 2789: // v-text shadow buffer size must be lesser than 0x7fd0
2790: if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14 root 2791: (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
2792: scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
2793: scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28 root 2794: if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14 root 2795: scr_width = 80;
2796: scr_height = 25;
2797: }
1.1.1.28 root 2798: screen_size_changed = true;
1.1.1.14 root 2799: }
1.1.1.12 root 2800: } else {
2801: // for a proof (not a console)
2802: scr_width = 80;
2803: scr_height = 25;
2804: }
1.1.1.14 root 2805: scr_buf_size.X = scr_width;
2806: scr_buf_size.Y = scr_height;
2807: scr_buf_pos.X = scr_buf_pos.Y = 0;
2808: scr_top = csbi.srWindow.Top;
1.1 root 2809: cursor_moved = false;
2810:
1.1.1.35 root 2811: #ifdef USE_SERVICE_THREAD
2812: InitializeCriticalSection(&input_crit_sect);
2813: InitializeCriticalSection(&key_buf_crit_sect);
2814: InitializeCriticalSection(&putch_crit_sect);
2815: #endif
1.1.1.25 root 2816: key_buf_char = new FIFO(256);
2817: key_buf_scan = new FIFO(256);
1.1 root 2818:
2819: hardware_init();
2820:
1.1.1.33 root 2821: #ifdef USE_DEBUGGER
2822: debugger_init();
2823: #endif
2824:
1.1.1.9 root 2825: if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1 root 2826: retval = EXIT_FAILURE;
2827: } else {
1.1.1.27 root 2828: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14 root 2829: _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
2830: #endif
2831: SetConsoleCtrlHandler(ctrl_handler, TRUE);
2832:
1.1.1.28 root 2833: if(screen_size_changed) {
1.1.1.24 root 2834: change_console_size(scr_width, scr_height);
2835: }
1.1.1.8 root 2836: TIMECAPS caps;
2837: timeGetDevCaps(&caps, sizeof(TIMECAPS));
2838: timeBeginPeriod(caps.wPeriodMin);
1.1.1.35 root 2839: #ifdef USE_VRAM_THREAD
1.1.1.14 root 2840: InitializeCriticalSection(&vram_crit_sect);
2841: CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
2842: #endif
1.1.1.33 root 2843: #ifdef USE_DEBUGGER
2844: CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
2845: // wait until telnet client starts and connects to me
2846: if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
2847: _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
2848: _access(debugger_get_putty_path(), 0) == 0 ||
2849: _access(debugger_get_putty_x86_path(), 0) == 0 ||
2850: _access(debugger_get_telnet_path(), 0) == 0) {
2851: for(int i = 0; i < 100 && cli_socket == 0; i++) {
2852: Sleep(100);
2853: }
2854: }
2855: #endif
1.1 root 2856: hardware_run();
1.1.1.35 root 2857: #ifdef USE_VRAM_THREAD
1.1.1.14 root 2858: vram_flush();
2859: DeleteCriticalSection(&vram_crit_sect);
2860: #endif
1.1.1.24 root 2861: timeEndPeriod(caps.wPeriodMin);
1.1.1.14 root 2862:
1.1.1.24 root 2863: // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28 root 2864: if(get_console_info_success) {
1.1.1.23 root 2865: hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 2866: if(restore_console_on_exit) {
1.1.1.14 root 2867: // window can't be bigger than buffer,
2868: // buffer can't be smaller than window,
2869: // so make a tiny window,
2870: // set the required buffer,
2871: // then set the required window
2872: SMALL_RECT rect;
2873: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
2874: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12 root 2875: SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14 root 2876: SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12 root 2877: SetConsoleWindowInfo(hStdout, TRUE, &rect);
2878: }
1.1.1.14 root 2879: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
2880: SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12 root 2881: }
1.1.1.24 root 2882: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
2883:
1.1 root 2884: msdos_finish();
1.1.1.14 root 2885:
2886: SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1 root 2887: }
1.1.1.35 root 2888: if(temp_file_created) {
2889: DeleteFile(temp_file_path);
2890: temp_file_created = false;
2891: }
1.1.1.10 root 2892: hardware_finish();
2893:
1.1.1.28 root 2894: if(key_buf_char != NULL) {
2895: key_buf_char->release();
2896: delete key_buf_char;
2897: key_buf_char = NULL;
2898: }
2899: if(key_buf_scan != NULL) {
2900: key_buf_scan->release();
2901: delete key_buf_scan;
2902: key_buf_scan = NULL;
2903: }
1.1.1.35 root 2904: #ifdef USE_SERVICE_THREAD
2905: DeleteCriticalSection(&input_crit_sect);
2906: DeleteCriticalSection(&key_buf_crit_sect);
2907: DeleteCriticalSection(&putch_crit_sect);
2908: #endif
1.1.1.20 root 2909: #ifdef _DEBUG
2910: _CrtDumpMemoryLeaks();
2911: #endif
1.1 root 2912: return(retval);
2913: }
2914:
1.1.1.20 root 2915: /* ----------------------------------------------------------------------------
2916: console
2917: ---------------------------------------------------------------------------- */
2918:
1.1.1.14 root 2919: void change_console_size(int width, int height)
1.1.1.12 root 2920: {
1.1.1.23 root 2921: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12 root 2922: CONSOLE_SCREEN_BUFFER_INFO csbi;
2923: SMALL_RECT rect;
2924: COORD co;
2925:
2926: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14 root 2927: if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
2928: if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
2929: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
2930: SET_RECT(rect, 0, 0, width - 1, height - 1);
2931: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
2932: } else if(csbi.dwCursorPosition.Y > height - 1) {
2933: SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
2934: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
2935: SET_RECT(rect, 0, 0, width - 1, height - 1);
2936: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12 root 2937: }
2938: }
1.1.1.14 root 2939: if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12 root 2940: co.X = csbi.dwCursorPosition.X;
1.1.1.14 root 2941: co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12 root 2942: SetConsoleCursorPosition(hStdout, co);
2943: cursor_moved = true;
2944: }
1.1.1.14 root 2945:
2946: // window can't be bigger than buffer,
2947: // buffer can't be smaller than window,
2948: // so make a tiny window,
2949: // set the required buffer,
2950: // then set the required window
2951: SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12 root 2952: SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14 root 2953: co.X = width;
2954: co.Y = height;
1.1.1.12 root 2955: SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14 root 2956: SET_RECT(rect, 0, 0, width - 1, height - 1);
2957: SetConsoleWindowInfo(hStdout, TRUE, &rect);
2958:
2959: scr_width = scr_buf_size.X = width;
2960: scr_height = scr_buf_size.Y = height;
2961: scr_top = 0;
2962:
2963: clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
2964:
2965: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15 root 2966: text_vram_end_address = text_vram_top_address + regen;
2967: shadow_buffer_end_address = shadow_buffer_top_address + regen;
2968:
1.1.1.14 root 2969: if(regen > 0x4000) {
2970: regen = 0x8000;
2971: vram_pages = 1;
2972: } else if(regen > 0x2000) {
2973: regen = 0x4000;
2974: vram_pages = 2;
2975: } else if(regen > 0x1000) {
2976: regen = 0x2000;
2977: vram_pages = 4;
2978: } else {
2979: regen = 0x1000;
2980: vram_pages = 8;
2981: }
1.1.1.15 root 2982: *(UINT16 *)(mem + 0x44a) = scr_width;
2983: *(UINT16 *)(mem + 0x44c) = regen;
2984: *(UINT8 *)(mem + 0x484) = scr_height - 1;
2985:
1.1.1.24 root 2986: mouse.min_position.x = 0;
2987: mouse.min_position.y = 0;
1.1.1.34 root 2988: mouse.max_position.x = 8 * (scr_width - 1);
2989: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 2990:
1.1.1.15 root 2991: restore_console_on_exit = true;
1.1.1.14 root 2992: }
2993:
2994: void clear_scr_buffer(WORD attr)
2995: {
2996: for(int y = 0; y < scr_height; y++) {
2997: for(int x = 0; x < scr_width; x++) {
2998: SCR_BUF(y,x).Char.AsciiChar = ' ';
2999: SCR_BUF(y,x).Attributes = attr;
3000: }
3001: }
1.1.1.12 root 3002: }
3003:
1.1.1.24 root 3004: bool update_console_input()
1.1 root 3005: {
1.1.1.35 root 3006: #ifdef USE_SERVICE_THREAD
3007: EnterCriticalSection(&input_crit_sect);
3008: #endif
1.1.1.23 root 3009: HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8 root 3010: DWORD dwNumberOfEvents = 0;
1.1 root 3011: DWORD dwRead;
3012: INPUT_RECORD ir[16];
1.1.1.24 root 3013: CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
3014: bool result = false;
1.1 root 3015:
1.1.1.8 root 3016: if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
3017: if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
3018: for(int i = 0; i < dwRead; i++) {
1.1.1.24 root 3019: if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34 root 3020: if(mouse.hidden == 0) {
3021: // NOTE: if restore_console_on_exit, console is not scrolled
3022: if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
3023: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
3024: }
3025: // FIXME: character size is always 8x8 ???
3026: int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
3027: int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
3028:
3029: if(mouse.position.x != x || mouse.position.y != y) {
3030: mouse.position.x = x;
3031: mouse.position.y = y;
3032: mouse.status |= 1;
3033: }
3034: }
3035: if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
3036: for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
3037: static const DWORD bits[] = {
3038: FROM_LEFT_1ST_BUTTON_PRESSED, // left
3039: RIGHTMOST_BUTTON_PRESSED, // right
3040: FROM_LEFT_2ND_BUTTON_PRESSED, // middle
3041: };
3042: bool prev_status = mouse.buttons[i].status;
3043: mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
3044:
3045: if(!prev_status && mouse.buttons[i].status) {
3046: mouse.buttons[i].pressed_times++;
3047: mouse.buttons[i].pressed_position.x = mouse.position.x;
3048: mouse.buttons[i].pressed_position.y = mouse.position.x;
3049: mouse.status |= 2 << (i * 2);
3050: } else if(prev_status && !mouse.buttons[i].status) {
3051: mouse.buttons[i].released_times++;
3052: mouse.buttons[i].released_position.x = mouse.position.x;
3053: mouse.buttons[i].released_position.y = mouse.position.x;
3054: mouse.status |= 4 << (i * 2);
1.1.1.14 root 3055: }
3056: }
3057: }
1.1.1.24 root 3058: } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33 root 3059: // update keyboard flags in bios data area
1.1.1.35 root 3060: if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
3061: mem[0x417] |= 0x40;
1.1.1.33 root 3062: } else {
1.1.1.35 root 3063: mem[0x417] &= ~0x40;
1.1.1.33 root 3064: }
1.1.1.35 root 3065: if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
3066: mem[0x417] |= 0x20;
1.1.1.33 root 3067: } else {
1.1.1.35 root 3068: mem[0x417] &= ~0x20;
3069: }
3070: if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
3071: mem[0x417] |= 0x10;
3072: } else {
3073: mem[0x417] &= ~0x10;
1.1.1.33 root 3074: }
3075: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3076: mem[0x417] |= 0x08;
3077: } else {
3078: mem[0x417] &= ~0x08;
3079: }
1.1.1.35 root 3080: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
3081: mem[0x417] |= 0x04;
1.1.1.33 root 3082: } else {
1.1.1.35 root 3083: mem[0x417] &= ~0x04;
1.1.1.33 root 3084: }
3085: if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
3086: if(!(mem[0x417] & 0x03)) {
3087: mem[0x417] |= 0x02; // left shift
3088: }
3089: } else {
3090: mem[0x417] &= ~0x03;
3091: }
1.1.1.35 root 3092: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
3093: mem[0x418] |= 0x02;
3094: } else {
3095: mem[0x418] &= ~0x02;
3096: }
3097: if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
3098: mem[0x418] |= 0x01;
3099: } else {
3100: mem[0x418] &= ~0x01;
3101: }
1.1.1.33 root 3102:
1.1.1.28 root 3103: // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24 root 3104: kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33 root 3105: kbd_status |= 1;
3106:
3107: // update dos key buffer
3108: UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
3109: UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
3110:
3111: if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28 root 3112: // make
1.1.1.24 root 3113: kbd_data &= 0x7f;
3114:
1.1.1.33 root 3115: if(chr == 0x00) {
1.1.1.24 root 3116: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3117: if(scn >= 0x3b && scn <= 0x44) {
3118: scn += 0x68 - 0x3b; // F1 to F10
3119: } else if(scn == 0x57 || scn == 0x58) {
3120: scn += 0x8b - 0x57; // F11 & F12
3121: } else if(scn >= 0x47 && scn <= 0x53) {
3122: scn += 0x97 - 0x47; // edit/arrow clusters
3123: } else if(scn == 0x35) {
3124: scn = 0xa4; // keypad /
3125: }
3126: } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
3127: if(scn == 0x07) {
3128: chr = 0x1e; // Ctrl+^
3129: } else if(scn == 0x0c) {
3130: chr = 0x1f; // Ctrl+_
3131: } else if(scn >= 0x35 && scn <= 0x58) {
3132: static const UINT8 ctrl_map[] = {
3133: 0x95, // keypad /
3134: 0,
3135: 0x96, // keypad *
3136: 0, 0, 0,
3137: 0x5e, // F1
3138: 0x5f, // F2
3139: 0x60, // F3
3140: 0x61, // F4
3141: 0x62, // F5
3142: 0x63, // F6
3143: 0x64, // F7
3144: 0x65, // F8
3145: 0x66, // F9
3146: 0x67, // F10
3147: 0,
3148: 0,
3149: 0x77, // Home
3150: 0x8d, // Up
3151: 0x84, // PgUp
3152: 0x8e, // keypad -
3153: 0x73, // Left
3154: 0x8f, // keypad center
3155: 0x74, // Right
3156: 0x90, // keyapd +
3157: 0x75, // End
3158: 0x91, // Down
3159: 0x76, // PgDn
3160: 0x92, // Insert
3161: 0x93, // Delete
3162: 0, 0, 0,
3163: 0x89, // F11
3164: 0x8a, // F12
3165: };
3166: scn = ctrl_map[scn - 0x35];
3167: }
3168: } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
3169: if(scn >= 0x3b && scn <= 0x44) {
3170: scn += 0x54 - 0x3b; // F1 to F10
3171: } else if(scn == 0x57 || scn == 0x58) {
3172: scn += 0x87 - 0x57; // F11 & F12
3173: }
3174: } else if(scn == 0x57 || scn == 0x58) {
3175: scn += 0x85 - 0x57;
3176: }
3177: // ignore shift, ctrl, alt, win and menu keys
3178: if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32 root 3179: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3180: #ifdef USE_SERVICE_THREAD
3181: EnterCriticalSection(&key_buf_crit_sect);
3182: #endif
1.1.1.32 root 3183: if(chr == 0) {
3184: key_buf_char->write(0x00);
3185: key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
3186: }
3187: key_buf_char->write(chr);
3188: key_buf_scan->write(scn);
1.1.1.35 root 3189: #ifdef USE_SERVICE_THREAD
3190: LeaveCriticalSection(&key_buf_crit_sect);
3191: #endif
1.1.1.24 root 3192: }
3193: }
3194: } else {
3195: if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
3196: chr = 0;
3197: if(scn >= 0x02 && scn <= 0x0e) {
3198: scn += 0x78 - 0x02; // 1 to 0 - =
3199: }
3200: }
1.1.1.32 root 3201: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3202: #ifdef USE_SERVICE_THREAD
3203: EnterCriticalSection(&key_buf_crit_sect);
3204: #endif
1.1.1.32 root 3205: key_buf_char->write(chr);
3206: key_buf_scan->write(scn);
1.1.1.35 root 3207: #ifdef USE_SERVICE_THREAD
3208: LeaveCriticalSection(&key_buf_crit_sect);
3209: #endif
1.1.1.32 root 3210: }
1.1.1.24 root 3211: }
1.1.1.33 root 3212: } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
3213: // ctrl-break, ctrl-c
3214: if(scn == 0x46) {
3215: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3216: #ifdef USE_SERVICE_THREAD
3217: EnterCriticalSection(&key_buf_crit_sect);
3218: #endif
1.1.1.33 root 3219: key_buf_char->write(0x00);
3220: key_buf_scan->write(0x00);
1.1.1.35 root 3221: #ifdef USE_SERVICE_THREAD
3222: LeaveCriticalSection(&key_buf_crit_sect);
3223: #endif
1.1.1.33 root 3224: }
3225: ctrl_break_pressed = true;
3226: mem[0x471] = 0x80;
3227: raise_int_1bh = true;
3228: } else {
3229: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 3230: #ifdef USE_SERVICE_THREAD
3231: EnterCriticalSection(&key_buf_crit_sect);
3232: #endif
1.1.1.33 root 3233: key_buf_char->write(chr);
3234: key_buf_scan->write(scn);
1.1.1.35 root 3235: #ifdef USE_SERVICE_THREAD
3236: LeaveCriticalSection(&key_buf_crit_sect);
3237: #endif
1.1.1.33 root 3238: }
3239: ctrl_c_pressed = (scn == 0x2e);
3240: }
3241: } else {
3242: // break
3243: kbd_data |= 0x80;
1.1 root 3244: }
1.1.1.24 root 3245: result = key_changed = true;
1.1.1.36 root 3246: // IME may be on and it may causes screen scroll up and cursor position change
3247: cursor_moved = true;
1.1 root 3248: }
3249: }
3250: }
3251: }
1.1.1.35 root 3252: #ifdef USE_SERVICE_THREAD
3253: LeaveCriticalSection(&input_crit_sect);
3254: #endif
1.1.1.24 root 3255: return(result);
1.1.1.8 root 3256: }
3257:
1.1.1.14 root 3258: bool update_key_buffer()
1.1.1.8 root 3259: {
1.1.1.35 root 3260: if(update_console_input()) {
3261: return(true);
3262: }
3263: if(key_buf_char != NULL && key_buf_scan != NULL) {
3264: #ifdef USE_SERVICE_THREAD
3265: EnterCriticalSection(&key_buf_crit_sect);
3266: #endif
3267: bool empty = key_buf_char->empty();
3268: #ifdef USE_SERVICE_THREAD
3269: LeaveCriticalSection(&key_buf_crit_sect);
3270: #endif
3271: if(!empty) return(true);
3272: }
3273: return(false);
1.1.1.8 root 3274: }
3275:
1.1.1.20 root 3276: /* ----------------------------------------------------------------------------
3277: MS-DOS virtual machine
3278: ---------------------------------------------------------------------------- */
3279:
1.1.1.32 root 3280: static const struct {
1.1.1.33 root 3281: char *name;
3282: DWORD lcid;
3283: char *std;
3284: char *dlt;
3285: } tz_table[] = {
3286: // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
3287: // 0 GMT Greenwich Mean Time GMT0
3288: {"GMT Standard Time", 0x0809, "GMT", "BST"}, // (UTC+00:00) GB London (en-gb)
3289: {"GMT Standard Time", 0x1809, "GMT", "IST"}, // (UTC+00:00) IE Dublin (en-ie)
3290: {"GMT Standard Time", 0x0000, "WET", "WES"}, // (UTC+00:00) PT Lisbon
3291: {"Greenwich Standard Time", 0x0000, "GMT", "GST"}, // (UTC+00:00) IS Reykjavik
3292: // 2 FST FDT Fernando De Noronha Std FST2FDT
3293: {"Mid-Atlantic Standard Time", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3294: {"UTC-02", 0x0416, "FST", "FDT"}, // (UTC-02:00) BR Noronha (pt-br)
3295: // 3 BST Brazil Standard Time BST3
3296: {"Bahia Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Bahia
3297: {"SA Eastern Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Fortaleza
3298: {"Tocantins Standard Time", 0x0000, "BST", "BDT"}, // (UTC-03:00) BR Palmas
3299: // 3 EST EDT Eastern Standard (Brazil) EST3EDT
3300: {"E. South America Standard Time", 0x0000, "EST", "EDT"}, // (UTC-03:00) BR Sao Paulo
3301: // 3 GST Greenland Standard Time GST3
3302: {"Greenland Standard Time", 0x0000, "GST", "GDT"}, // (UTC-03:00) GL Godthab
3303: // 3:30 NST NDT Newfoundland Standard Time NST3:30NDT
3304: {"Newfoundland Standard Time", 0x0000, "NST", "NDT"}, // (UTC-03:30) CA St.Johns
3305: // 4 AST ADT Atlantic Standard Time AST4ADT
3306: {"Atlantic Standard Time", 0x0000, "AST", "ADT"}, // (UTC-04:00) CA Halifax
3307: // 4 WST WDT Western Standard (Brazil) WST4WDT
3308: {"Central Brazilian Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Cuiaba
3309: {"SA Western Standard Time", 0x0000, "WST", "WDT"}, // (UTC-04:00) BR Manaus
3310: // 5 EST EDT Eastern Standard Time EST5EDT
3311: {"Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US New York
3312: {"Eastern Standard Time (Mexico)", 0x0000, "EST", "EDT"}, // (UTC-05:00) MX Cancun
3313: {"US Eastern Standard Time", 0x0000, "EST", "EDT"}, // (UTC-05:00) US Indianapolis
3314: // 5 CST CDT Chile Standard Time CST5CDT
3315: {"Pacific SA Standard Time", 0x0000, "CST", "CDT"}, // (UTC-04:00) CL Santiago
3316: // 5 AST ADT Acre Standard Time AST5ADT
3317: {"SA Pacific Standard Time", 0x0000, "AST", "ADT"}, // (UTC-05:00) BR Rio Branco
3318: // 5 CST CDT Cuba Standard Time CST5CDT
3319: {"Cuba Standard Time", 0x0000, "CST", "CDT"}, // (UTC-05:00) CU Havana
3320: // 6 CST CDT Central Standard Time CST6CDT
3321: {"Canada Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) CA Regina
3322: {"Central Standard Time", 0x0000, "CST", "CDT"}, // (UTC-06:00) US Chicago
3323: {"Central Standard Time (Mexico)", 0x0000, "CST", "CDT"}, // (UTC-06:00) MX Mexico City
3324: // 6 EST EDT Easter Island Standard EST6EDT
3325: {"Easter Island Standard Time", 0x0000, "EST", "EDT"}, // (UTC-06:00) CL Easter
3326: // 7 MST MDT Mountain Standard Time MST7MDT
3327: {"Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Denver
3328: {"Mountain Standard Time (Mexico)", 0x0000, "MST", "MDT"}, // (UTC-07:00) MX Chihuahua
3329: {"US Mountain Standard Time", 0x0000, "MST", "MDT"}, // (UTC-07:00) US Phoenix
3330: // 8 PST PDT Pacific Standard Time PST8PDT
3331: {"Pacific Standard Time", 0x0000, "PST", "PDT"}, // (UTC-08:00) US Los Angeles
3332: {"Pacific Standard Time (Mexico)", 0x0000, "PST", "PDT"}, // (UTC-08:00) MX Tijuana
3333: // 9 AKS AKD Alaska Standard Time AKS9AKD
3334: // 9 YST YDT Yukon Standard Time YST9YST
3335: {"Alaskan Standard Time", 0x0000, "AKS", "AKD"}, // (UTC-09:00) US Anchorage
3336: // 10 HST HDT Hawaii Standard Time HST10HDT
3337: {"Aleutian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Aleutian
3338: {"Hawaiian Standard Time", 0x0000, "HST", "HDT"}, // (UTC-10:00) US Honolulu
3339: // 11 SST Samoa Standard Time SST11
3340: {"Samoa Standard Time", 0x0000, "SST", "SDT"}, // (UTC-11:00) US Samoa
3341: // -12 NZS NZD New Zealand Standard Time NZS-12NZD
3342: {"New Zealand Standard Time", 0x0000, "NZS", "NZD"}, // (UTC+12:00) NZ Auckland
3343: // -10 GST Guam Standard Time GST-10
3344: {"West Pacific Standard Time", 0x0000, "GST", "GDT"}, // (UTC+10:00) GU Guam
3345: // -10 EAS EAD Eastern Australian Standard EAS-10EAD
3346: {"AUS Eastern Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Sydney
3347: {"E. Australia Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Brisbane
3348: {"Tasmania Standard Time", 0x0000, "EAS", "EAD"}, // (UTC+10:00) AU Hobart
3349: // -9:30 CAS CAD Central Australian Standard CAS-9:30CAD
3350: {"AUS Central Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Darwin
3351: {"Cen. Australia Standard Time", 0x0000, "CAS", "CAD"}, // (UTC+09:30) AU Adelaide
3352: // -9 JST Japan Standard Time JST-9
3353: {"Tokyo Standard Time", 0x0000, "JST", "JDT"}, // (UTC+09:00) JP Tokyo
3354: // -9 KST KDT Korean Standard Time KST-9KDT
3355: {"Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+09:00) KR Seoul
3356: {"North Korea Standard Time", 0x0000, "KST", "KDT"}, // (UTC+08:30) KP Pyongyang
3357: // -8 HKT Hong Kong Time HKT-8
3358: {"China Standard Time", 0x0C04, "HKT", "HKS"}, // (UTC+08:00) HK Hong Kong (zh-hk)
3359: // -8 CCT China Coast Time CCT-8
3360: {"China Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) CN Shanghai
3361: {"Taipei Standard Time", 0x0000, "CCT", "CDT"}, // (UTC+08:00) TW Taipei
3362: // -8 SST Singapore Standard Time SST-8
3363: {"Singapore Standard Time", 0x0000, "SST", "SDT"}, // (UTC+08:00) SG Singapore
3364: // -8 WAS WAD Western Australian Standard WAS-8WAD
3365: {"Aus Central W. Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:45) AU Eucla
3366: {"W. Australia Standard Time", 0x0000, "WAS", "WAD"}, // (UTC+08:00) AU Perth
3367: // -7:30 JT Java Standard Time JST-7:30
3368: // -7 NST North Sumatra Time NST-7
3369: {"SE Asia Standard Time", 0x0000, "NST", "NDT"}, // (UTC+07:00) ID Jakarta
3370: // -5:30 IST Indian Standard Time IST-5:30
3371: {"India Standard Time", 0x0000, "IST", "IDT"}, // (UTC+05:30) IN Calcutta
3372: // -3:30 IST IDT Iran Standard Time IST-3:30IDT
3373: {"Iran Standard Time", 0x0000, "IST", "IDT"}, // (UTC+03:30) IR Tehran
3374: // -3 MSK MSD Moscow Winter Time MSK-3MSD
3375: {"Belarus Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) BY Minsk
3376: {"Russian Standard Time", 0x0000, "MSK", "MSD"}, // (UTC+03:00) RU Moscow
3377: // -2 EET Eastern Europe Time EET-2
3378: {"E. Europe Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) MD Chisinau
3379: {"FLE Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) UA Kiev
3380: {"GTB Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RO Bucharest
3381: {"Kaliningrad Standard Time", 0x0000, "EET", "EES"}, // (UTC+02:00) RU Kaliningrad
3382: // -2 IST IDT Israel Standard Time IST-2IDT
3383: {"Israel Standard Time", 0x0000, "IST", "IDT"}, // (UTC+02:00) IL Jerusalem
3384: // -1 MEZ MES Middle European Time MEZ-1MES
3385: // -1 SWT SST Swedish Winter Time SWT-1SST
3386: // -1 FWT FST French Winter Time FWT-1FST
3387: // -1 CET CES Central European Time CET-1CES
3388: {"Central Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) HU Budapest
3389: {"Central European Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) PL Warsaw
3390: {"Romance Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) FR Paris
3391: {"W. Europe Standard Time", 0x0000, "CET", "CES"}, // (UTC+01:00) DE Berlin
3392: // -1 WAT West African Time WAT-1
3393: {"Namibia Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NA Windhoek
3394: {"W. Central Africa Standard Time", 0x0000, "WAT", "WAS"}, // (UTC+01:00) NG Lagos
3395: // 0 UTC Universal Coordinated Time UTC0
3396: {"UTC", 0x0000, "UTC", "" }, // (UTC+00:00) GMT+0
3397: {"UTC-02", 0x0000, "UTC", "" }, // (UTC-02:00) GMT+2
3398: {"UTC-08", 0x0000, "UTC", "" }, // (UTC-08:00) GMT+8
3399: {"UTC-09", 0x0000, "UTC", "" }, // (UTC-09:00) GMT+9
3400: {"UTC-11", 0x0000, "UTC", "" }, // (UTC-11:00) GMT+11
3401: {"UTC+12", 0x0000, "UTC", "" }, // (UTC+12:00) GMT-12
3402: };
3403:
3404: static const struct {
1.1.1.32 root 3405: UINT16 code;
3406: char *message_english;
3407: char *message_japanese;
3408: } standard_error_table[] = {
3409: {0x01, "Invalid function", "�����ȃt�@���N�V�����ł�."},
3410: {0x02, "File not found", "�t�@�C����������܂���."},
3411: {0x03, "Path not found", "�p�X��������܂���."},
3412: {0x04, "Too many open files", "�J����Ă���t�@�C�����������܂�."},
3413: {0x05, "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
3414: {0x06, "Invalid handle", "�����ȃn���h���ł�."},
3415: {0x07, "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
3416: {0x08, "Insufficient memory", "������������܂���."},
3417: {0x09, "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
3418: {0x0A, "Invalid Environment", "�����Ȋ��ł�."},
3419: {0x0B, "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
3420: {0x0C, "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
3421: {0x0D, "Invalid data", "�����ȃf�[�^�ł�."},
3422: {0x0F, "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
3423: {0x10, "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
3424: {0x11, "Not same device", "�����f�o�C�X�ł͂���܂���."},
3425: {0x12, "No more files", "�t�@�C���͂���ȏ゠��܂���."},
3426: {0x13, "Write protect error", "�������ݕی�G���[�ł�."},
3427: {0x14, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3428: {0x15, "Not ready", "�������ł��Ă��܂���."},
3429: {0x16, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3430: {0x17, "Data error", "�f�[�^�G���[�ł�."},
3431: {0x18, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3432: {0x19, "Seek error", "�V�[�N�G���[�ł�."},
3433: {0x1A, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3434: {0x1B, "Sector not found", "�Z�N�^��������܂���."},
3435: {0x1C, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3436: {0x1D, "Write fault error", "�������݃G���[�ł�."},
3437: {0x1E, "Read fault error", "�ǂݎ��G���[�ł�."},
3438: {0x1F, "General failure", "�G���[�ł�."},
3439: {0x20, "Sharing violation", "���L�ᔽ�ł�."},
3440: {0x21, "Lock violation", "���b�N�ᔽ�ł�."},
3441: {0x22, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3442: {0x23, "FCB unavailable", "FCB �͎g���܂���."},
3443: {0x24, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3444: {0x25, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3445: {0x26, "Out of input", "���͂��I���܂���."},
3446: {0x27, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3447: /*
3448: {0x32, "Network request not supported", NULL},
3449: {0x33, "Remote computer not listening", NULL},
3450: {0x34, "Duplicate name on network", NULL},
3451: {0x35, "Network name not found", NULL},
3452: {0x36, "Network busy", NULL},
3453: {0x37, "Network device no longer exists", NULL},
3454: {0x38, "Network BIOS command limit exceeded", NULL},
3455: {0x39, "Network adapter hardware error", NULL},
3456: {0x3A, "Incorrect response from network", NULL},
3457: {0x3B, "Unexpected network error", NULL},
3458: {0x3C, "Incompatible remote adapter", NULL},
3459: {0x3D, "Print queue full", NULL},
3460: {0x3E, "Queue not full", NULL},
3461: {0x3F, "Not enough space to print file", NULL},
3462: {0x40, "Network name was deleted", NULL},
3463: {0x41, "Network: Access denied", NULL},
3464: {0x42, "Network device type incorrect", NULL},
3465: {0x43, "Network name not found", NULL},
3466: {0x44, "Network name limit exceeded", NULL},
3467: {0x45, "Network BIOS session limit exceeded", NULL},
3468: {0x46, "Temporarily paused", NULL},
3469: {0x47, "Network request not accepted", NULL},
3470: {0x48, "Network print/disk redirection paused", NULL},
3471: {0x49, "Network software not installed", NULL},
3472: {0x4A, "Unexpected adapter close", NULL},
3473: */
3474: {0x50, "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33 root 3475: {0x52, "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32 root 3476: {0x53, "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
3477: {0x54, "Too many redirections", "���_�C���N�g���������܂�."},
3478: {0x55, "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
3479: {0x56, "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
3480: {0x57, "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
3481: {0x58, "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
3482: {0x59, "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
3483: {0x5A, "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
3484: /*
3485: {0x64, "Unknown error", "�s���ȃG���[�ł�."},
3486: {0x65, "Not ready", "�������ł��Ă��܂���."},
3487: {0x66, "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
3488: {0x67, "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
3489: {0x68, "Door open", "���o�[���܂��Ă��܂���."
3490: */
3491: {0xB0, "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
3492: {0xB1, "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
3493: {0xB2, "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
3494: {0xB4, "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
3495: {0xB5, "A valid eject request failed", "���o���Ɏ��s���܂���."},
3496: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3497: };
3498:
3499: static const struct {
3500: UINT16 code;
3501: char *message_english;
3502: char *message_japanese;
3503: } param_error_table[] = {
3504: {0x01, "Too many parameters", "�p�����[�^���������܂�."},
3505: {0x02, "Required parameter missing", "�p�����[�^������܂���."},
3506: {0x03, "Invalid switch", "�����ȃX�C�b�`�ł�."},
3507: {0x04, "Invalid keyword", "�����ȃL�[���[�h�ł�."},
3508: {0x06, "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂��Ă��܂�."},
3509: {0x07, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3510: {0x08, "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
3511: {0x09, "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
3512: {0x0A, "Invalid parameter", "�����ȃp�����[�^�ł�."},
3513: {0x0B, "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
3514: {-1 , "Unknown error", "�s���ȃG���[�ł�."},
3515: };
3516:
3517: static const struct {
3518: UINT16 code;
3519: char *message_english;
3520: char *message_japanese;
3521: } critical_error_table[] = {
3522: {0x00, "Write protect error", "�������ݕی�G���[�ł�."},
3523: {0x01, "Invalid unit", "�����ȃ��j�b�g�ł�."},
3524: {0x02, "Not ready", "�������ł��Ă��܂���."},
3525: {0x03, "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
3526: {0x04, "Data error", "�f�[�^�G���[�ł�."},
3527: {0x05, "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
3528: {0x06, "Seek error", "�V�[�N�G���[�ł�."},
3529: {0x07, "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
3530: {0x08, "Sector not found", "�Z�N�^��������܂���."},
3531: {0x09, "Printer out of paper error", "�v�����^�̗p��������܂���."},
3532: {0x0A, "Write fault error", "�������݃G���[�ł�."},
3533: {0x0B, "Read fault error", "�ǂݎ��G���[�ł�."},
3534: {0x0C, "General failure", "�G���[�ł�."},
3535: {0x0D, "Sharing violation", "���L�ᔽ�ł�."},
3536: {0x0E, "Lock violation", "���b�N�ᔽ�ł�."},
3537: {0x0F, "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
3538: {0x10, "FCB unavailable", "FCB �͎g���܂���."},
3539: {0x11, "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
3540: {0x12, "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
3541: {0x13, "Out of input", "���͂��I���܂���."},
3542: {0x14, "Insufficient disk space", "�f�B�X�N�̋̈悪����܂���."},
3543: {-1 , "Critical error", "�v���I�ȃG���[�ł�."},
3544: };
3545:
1.1.1.20 root 3546: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
3547: int msdos_psp_get_file_table(int fd, int psp_seg);
3548: void msdos_putch(UINT8 data);
1.1.1.35 root 3549: #ifdef USE_SERVICE_THREAD
3550: void msdos_putch_tmp(UINT8 data);
3551: #endif
1.1.1.20 root 3552:
1.1 root 3553: // process info
3554:
3555: process_t *msdos_process_info_create(UINT16 psp_seg)
3556: {
3557: for(int i = 0; i < MAX_PROCESS; i++) {
3558: if(process[i].psp == 0 || process[i].psp == psp_seg) {
3559: memset(&process[i], 0, sizeof(process_t));
3560: process[i].psp = psp_seg;
3561: return(&process[i]);
3562: }
3563: }
3564: fatalerror("too many processes\n");
3565: return(NULL);
3566: }
3567:
1.1.1.33 root 3568: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1 root 3569: {
3570: for(int i = 0; i < MAX_PROCESS; i++) {
3571: if(process[i].psp == psp_seg) {
3572: return(&process[i]);
3573: }
3574: }
1.1.1.33 root 3575: if(show_error) {
3576: fatalerror("invalid psp address\n");
3577: }
1.1 root 3578: return(NULL);
3579: }
3580:
1.1.1.33 root 3581: process_t *msdos_process_info_get(UINT16 psp_seg)
3582: {
3583: return(msdos_process_info_get(psp_seg, true));
3584: }
3585:
1.1.1.23 root 3586: void msdos_sda_update(int psp_seg)
3587: {
3588: sda_t *sda = (sda_t *)(mem + SDA_TOP);
3589:
3590: for(int i = 0; i < MAX_PROCESS; i++) {
3591: if(process[i].psp == psp_seg) {
3592: sda->switchar = process[i].switchar;
3593: sda->current_dta.w.l = process[i].dta.w.l;
3594: sda->current_dta.w.h = process[i].dta.w.h;
3595: sda->current_psp = process[i].psp;
3596: break;
3597: }
3598: }
3599: sda->malloc_strategy = malloc_strategy;
3600: sda->return_code = retval;
3601: sda->current_drive = _getdrive();
3602: }
3603:
1.1.1.13 root 3604: // dta info
3605:
3606: void msdos_dta_info_init()
3607: {
1.1.1.14 root 3608: for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13 root 3609: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3610: }
3611: }
3612:
3613: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
3614: {
3615: dtainfo_t *free_dta = NULL;
1.1.1.14 root 3616: for(int i = 0; i < MAX_DTAINFO; i++) {
3617: if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
3618: if(free_dta == NULL) {
1.1.1.13 root 3619: free_dta = &dtalist[i];
3620: }
1.1.1.14 root 3621: } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13 root 3622: return(&dtalist[i]);
3623: }
3624: }
1.1.1.14 root 3625: if(free_dta) {
1.1.1.13 root 3626: free_dta->psp = psp_seg;
3627: free_dta->dta = dta_laddr;
3628: return(free_dta);
3629: }
3630: fatalerror("too many dta\n");
3631: return(NULL);
3632: }
3633:
3634: void msdos_dta_info_free(UINT16 psp_seg)
3635: {
1.1.1.14 root 3636: for(int i = 0; i < MAX_DTAINFO; i++) {
3637: if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13 root 3638: FindClose(dtalist[i].find_handle);
3639: dtalist[i].find_handle = INVALID_HANDLE_VALUE;
3640: }
3641: }
3642: }
3643:
1.1 root 3644: void msdos_cds_update(int drv)
3645: {
3646: cds_t *cds = (cds_t *)(mem + CDS_TOP);
3647:
3648: memset(mem + CDS_TOP, 0, CDS_SIZE);
3649: sprintf(cds->path_name, "%c:\\", 'A' + drv);
3650: cds->drive_attrib = 0x4000; // physical drive
3651: cds->physical_drive_number = drv;
3652: }
3653:
1.1.1.17 root 3654: // nls information tables
3655:
3656: // uppercase table (func 6502h)
3657: void msdos_upper_table_update()
3658: {
3659: *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 3660: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 3661: UINT8 c[4];
1.1.1.33 root 3662: *(UINT32 *)c = 0; // reset internal conversion state
3663: CharUpperBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.17 root 3664: c[0] = 0x80 + i;
3665: DWORD rc = CharUpperBuffA((LPSTR)c, 1);
3666: mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
3667: }
3668: }
3669:
1.1.1.23 root 3670: // lowercase table (func 6503h)
3671: void msdos_lower_table_update()
3672: {
3673: *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
3674: for(unsigned i = 0; i < 0x80; ++i) {
3675: UINT8 c[4];
1.1.1.33 root 3676: *(UINT32 *)c = 0; // reset internal conversion state
3677: CharLowerBuffA((LPSTR)c, 4); // (workaround for MBCS codepage)
1.1.1.23 root 3678: c[0] = 0x80 + i;
3679: DWORD rc = CharLowerBuffA((LPSTR)c, 1);
3680: mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
3681: }
3682: }
3683:
1.1.1.17 root 3684: // filename uppercase table (func 6504h)
3685: void msdos_filename_upper_table_init()
3686: {
3687: // depended on (file)system, not on active codepage
3688: // temporary solution: just filling data
3689: *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22 root 3690: for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17 root 3691: mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
3692: }
3693: }
3694:
3695: // filaname terminator table (func 6505h)
3696: void msdos_filename_terminator_table_init()
3697: {
3698: const char illegal_chars[] = ".\"/\\[]:|<>+=;,"; // for standard MS-DOS fs.
3699: UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
3700:
3701: data[2] = 1; // marker? (permissible character value)
3702: data[3] = 0x00; // 00h...FFh
3703: data[4] = 0xff;
3704: data[5] = 0; // marker? (excluded character)
3705: data[6] = 0x00; // 00h...20h
3706: data[7] = 0x20;
3707: data[8] = 2; // marker? (illegal characters for filename)
3708: data[9] = (UINT8)strlen(illegal_chars);
3709: memcpy(data + 10, illegal_chars, data[9]);
3710:
3711: // total length
3712: *(UINT16 *)data = (10 - 2) + data[9];
3713: }
3714:
3715: // collating table (func 6506h)
3716: void msdos_collating_table_update()
3717: {
3718: // temporary solution: just filling data
3719: *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22 root 3720: for(unsigned i = 0; i < 256; ++i) {
1.1.1.17 root 3721: mem[COLLATING_TABLE_TOP + 2 + i] = i;
3722: }
3723: }
3724:
1.1 root 3725: // dbcs
3726:
3727: void msdos_dbcs_table_update()
3728: {
3729: UINT8 dbcs_data[DBCS_SIZE];
3730: memset(dbcs_data, 0, sizeof(dbcs_data));
3731:
3732: CPINFO info;
3733: GetCPInfo(active_code_page, &info);
3734:
3735: if(info.MaxCharSize != 1) {
3736: for(int i = 0;; i += 2) {
3737: UINT8 lo = info.LeadByte[i + 0];
3738: UINT8 hi = info.LeadByte[i + 1];
3739: dbcs_data[2 + i + 0] = lo;
3740: dbcs_data[2 + i + 1] = hi;
3741: if(lo == 0 && hi == 0) {
3742: dbcs_data[0] = i + 2;
3743: break;
3744: }
3745: }
3746: } else {
3747: dbcs_data[0] = 2; // ???
3748: }
3749: memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
3750: }
3751:
1.1.1.17 root 3752: void msdos_dbcs_table_finish()
3753: {
1.1.1.32 root 3754: if(system_code_page != _getmbcp()) {
1.1.1.17 root 3755: _setmbcp(system_code_page);
3756: }
1.1.1.32 root 3757: if(console_code_page != GetConsoleCP()) {
3758: SetConsoleCP(console_code_page);
3759: SetConsoleOutputCP(console_code_page);
3760: }
1.1.1.17 root 3761: }
3762:
3763: void msdos_nls_tables_init()
1.1 root 3764: {
1.1.1.32 root 3765: active_code_page = console_code_page = GetConsoleCP();
3766: system_code_page = _getmbcp();
3767:
3768: if(active_code_page != system_code_page) {
3769: if(_setmbcp(active_code_page) != 0) {
3770: active_code_page = system_code_page;
3771: }
3772: }
3773:
1.1.1.17 root 3774: msdos_upper_table_update();
1.1.1.23 root 3775: msdos_lower_table_update();
1.1.1.17 root 3776: msdos_filename_terminator_table_init();
3777: msdos_filename_upper_table_init();
3778: msdos_collating_table_update();
1.1 root 3779: msdos_dbcs_table_update();
3780: }
3781:
1.1.1.17 root 3782: void msdos_nls_tables_update()
1.1 root 3783: {
1.1.1.17 root 3784: msdos_dbcs_table_update();
3785: msdos_upper_table_update();
1.1.1.23 root 3786: msdos_lower_table_update();
3787: // msdos_collating_table_update();
1.1 root 3788: }
3789:
3790: int msdos_lead_byte_check(UINT8 code)
3791: {
3792: UINT8 *dbcs_table = mem + DBCS_TABLE;
3793:
3794: for(int i = 0;; i += 2) {
3795: UINT8 lo = dbcs_table[i + 0];
3796: UINT8 hi = dbcs_table[i + 1];
3797: if(lo == 0 && hi == 0) {
3798: break;
3799: }
3800: if(lo <= code && code <= hi) {
3801: return(1);
3802: }
3803: }
3804: return(0);
3805: }
3806:
1.1.1.20 root 3807: int msdos_ctrl_code_check(UINT8 code)
3808: {
1.1.1.22 root 3809: return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20 root 3810: }
3811:
1.1.1.36 root 3812: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
3813: {
3814: int is_kanji_1st = 0;
3815: int is_kanji_2nd = 0;
3816:
3817: for(int p = 0;; p++) {
3818: if(is_kanji_1st) {
3819: is_kanji_1st = 0;
3820: is_kanji_2nd = 1;
3821: } else if(msdos_lead_byte_check(buf[p])) {
3822: is_kanji_1st = 1;
3823: }
3824: if(p == n) {
3825: return(is_kanji_2nd);
3826: }
3827: is_kanji_2nd = 0;
3828: }
3829: }
3830:
1.1 root 3831: // file control
3832:
1.1.1.14 root 3833: char *msdos_remove_double_quote(char *path)
3834: {
3835: static char tmp[MAX_PATH];
3836:
3837: memset(tmp, 0, sizeof(tmp));
3838: if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
3839: memcpy(tmp, path + 1, strlen(path) - 2);
3840: } else {
3841: strcpy(tmp, path);
3842: }
3843: return(tmp);
3844: }
3845:
1.1.1.32 root 3846: char *msdos_remove_end_separator(char *path)
3847: {
3848: static char tmp[MAX_PATH];
3849:
3850: strcpy(tmp, path);
3851: int len = strlen(tmp);
3852: if(len > 3 && tmp[len - 1] == '\\') {
3853: tmp[len - 1] = '\0';
3854: }
3855: return(tmp);
3856: }
3857:
1.1.1.14 root 3858: char *msdos_combine_path(char *dir, const char *file)
3859: {
3860: static char tmp[MAX_PATH];
3861: char *tmp_dir = msdos_remove_double_quote(dir);
3862:
3863: if(strlen(tmp_dir) == 0) {
3864: strcpy(tmp, file);
3865: } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
3866: sprintf(tmp, "%s%s", tmp_dir, file);
3867: } else {
3868: sprintf(tmp, "%s\\%s", tmp_dir, file);
3869: }
3870: return(tmp);
3871: }
3872:
1.1 root 3873: char *msdos_trimmed_path(char *path, int lfn)
3874: {
3875: static char tmp[MAX_PATH];
3876:
3877: if(lfn) {
3878: strcpy(tmp, path);
3879: } else {
3880: // remove space in the path
3881: char *src = path, *dst = tmp;
3882:
3883: while(*src != '\0') {
3884: if(msdos_lead_byte_check(*src)) {
3885: *dst++ = *src++;
3886: *dst++ = *src++;
3887: } else if(*src != ' ') {
3888: *dst++ = *src++;
3889: } else {
3890: src++; // skip space
3891: }
3892: }
3893: *dst = '\0';
3894: }
1.1.1.14 root 3895: if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
3896: // redirect C:\COMMAND.COM to comspec_path
3897: strcpy(tmp, comspec_path);
3898: } else if(is_vista_or_later && _access(tmp, 0) != 0) {
3899: // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
3900: static int root_drive_protected = -1;
3901: char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
3902: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
3903:
3904: if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
3905: name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
3906: strcpy(name, name_temp);
3907: name_temp[0] = '\0';
3908:
3909: if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
3910: (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
3911: if(root_drive_protected == -1) {
3912: FILE *fp = NULL;
3913:
3914: sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
3915: root_drive_protected = 1;
3916: try {
3917: if((fp = fopen(temp, "w")) != NULL) {
3918: if(fprintf(fp, "TEST") == 4) {
3919: root_drive_protected = 0;
3920: }
3921: }
3922: } catch(...) {
3923: }
3924: if(fp != NULL) {
3925: fclose(fp);
3926: }
3927: if(_access(temp, 0) == 0) {
3928: remove(temp);
3929: }
3930: }
3931: if(root_drive_protected == 1) {
3932: if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
3933: GetEnvironmentVariable("TMP", temp, MAX_PATH) != 0) {
3934: strcpy(tmp, msdos_combine_path(temp, name));
3935: }
3936: }
3937: }
3938: }
3939: }
1.1 root 3940: return(tmp);
3941: }
3942:
1.1.1.28 root 3943: char *msdos_get_multiple_short_path(char *src)
3944: {
1.1.1.32 root 3945: // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28 root 3946: static char env_path[ENV_SIZE];
3947: char tmp[ENV_SIZE], *token;
3948:
3949: memset(env_path, 0, sizeof(env_path));
3950: strcpy(tmp, src);
3951: token = my_strtok(tmp, ";");
3952:
3953: while(token != NULL) {
3954: if(token[0] != '\0') {
3955: char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
1.1.1.32 root 3956: if(path != NULL && strlen(path) != 0) {
3957: if(env_path[0] != '\0') {
3958: strcat(env_path, ";");
3959: }
1.1.1.28 root 3960: if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32 root 3961: strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28 root 3962: } else {
3963: my_strupr(short_path);
1.1.1.32 root 3964: strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28 root 3965: }
3966: }
3967: }
3968: token = my_strtok(NULL, ";");
3969: }
3970: return(env_path);
3971: }
3972:
1.1 root 3973: bool match(char *text, char *pattern)
3974: {
1.1.1.24 root 3975: // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14 root 3976: switch(*pattern) {
1.1 root 3977: case '\0':
3978: return !*text;
3979: case '*':
1.1.1.14 root 3980: return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1 root 3981: case '?':
3982: return *text && match(text + 1, pattern + 1);
3983: default:
3984: return (*text == *pattern) && match(text + 1, pattern + 1);
3985: }
3986: }
3987:
3988: bool msdos_match_volume_label(char *path, char *volume)
3989: {
3990: char *p;
3991:
1.1.1.14 root 3992: if(!*volume) {
3993: return false;
3994: } else if((p = my_strchr(path, ':')) != NULL) {
1.1 root 3995: return msdos_match_volume_label(p + 1, volume);
3996: } else if((p = my_strchr(path, '\\')) != NULL) {
3997: return msdos_match_volume_label(p + 1, volume);
3998: } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14 root 3999: char tmp[MAX_PATH];
4000: sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
4001: return match(volume, tmp);
1.1 root 4002: } else {
4003: return match(volume, path);
4004: }
4005: }
4006:
4007: char *msdos_fcb_path(fcb_t *fcb)
4008: {
4009: static char tmp[MAX_PATH];
4010: char name[9], ext[4];
4011:
4012: memset(name, 0, sizeof(name));
4013: memcpy(name, fcb->file_name, 8);
4014: strcpy(name, msdos_trimmed_path(name, 0));
4015:
4016: memset(ext, 0, sizeof(ext));
4017: memcpy(ext, fcb->file_name + 8, 3);
4018: strcpy(ext, msdos_trimmed_path(ext, 0));
4019:
4020: if(name[0] == '\0' || strcmp(name, "????????") == 0) {
4021: strcpy(name, "*");
4022: }
4023: if(ext[0] == '\0') {
4024: strcpy(tmp, name);
4025: } else {
4026: if(strcmp(ext, "???") == 0) {
4027: strcpy(ext, "*");
4028: }
4029: sprintf(tmp, "%s.%s", name, ext);
4030: }
4031: return(tmp);
4032: }
4033:
4034: void msdos_set_fcb_path(fcb_t *fcb, char *path)
4035: {
4036: char *ext = my_strchr(path, '.');
4037:
4038: memset(fcb->file_name, 0x20, 8 + 3);
4039: if(ext != NULL && path[0] != '.') {
4040: *ext = '\0';
4041: memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
4042: }
4043: memcpy(fcb->file_name, path, strlen(path));
4044: }
4045:
4046: char *msdos_short_path(char *path)
4047: {
4048: static char tmp[MAX_PATH];
4049:
1.1.1.24 root 4050: if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
4051: strcpy(tmp, path);
4052: }
1.1 root 4053: my_strupr(tmp);
4054: return(tmp);
4055: }
4056:
1.1.1.13 root 4057: char *msdos_short_name(WIN32_FIND_DATA *fd)
4058: {
4059: static char tmp[MAX_PATH];
4060:
1.1.1.14 root 4061: if(fd->cAlternateFileName[0]) {
1.1.1.13 root 4062: strcpy(tmp, fd->cAlternateFileName);
4063: } else {
4064: strcpy(tmp, fd->cFileName);
4065: }
4066: my_strupr(tmp);
4067: return(tmp);
4068: }
4069:
1.1 root 4070: char *msdos_short_full_path(char *path)
4071: {
4072: static char tmp[MAX_PATH];
4073: char full[MAX_PATH], *name;
4074:
1.1.1.14 root 4075: // Full works with non-existent files, but Short does not
1.1 root 4076: GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14 root 4077: *tmp = '\0';
4078: if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
4079: name[-1] = '\0';
4080: DWORD len = GetShortPathName(full, tmp, MAX_PATH);
4081: if(len == 0) {
4082: strcpy(tmp, full);
4083: } else {
4084: tmp[len++] = '\\';
4085: strcpy(tmp + len, name);
4086: }
4087: }
1.1 root 4088: my_strupr(tmp);
4089: return(tmp);
4090: }
4091:
4092: char *msdos_short_full_dir(char *path)
4093: {
4094: static char tmp[MAX_PATH];
4095: char full[MAX_PATH], *name;
4096:
4097: GetFullPathName(path, MAX_PATH, full, &name);
4098: name[-1] = '\0';
1.1.1.24 root 4099: if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
4100: strcpy(tmp, full);
4101: }
1.1 root 4102: my_strupr(tmp);
4103: return(tmp);
4104: }
4105:
4106: char *msdos_local_file_path(char *path, int lfn)
4107: {
4108: char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14 root 4109: #if 0
4110: // I have forgotten the reason of this routine... :-(
1.1 root 4111: if(_access(trimmed, 0) != 0) {
4112: process_t *process = msdos_process_info_get(current_psp);
4113: static char tmp[MAX_PATH];
4114:
4115: sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
4116: if(_access(tmp, 0) == 0) {
4117: return(tmp);
4118: }
4119: }
1.1.1.14 root 4120: #endif
1.1 root 4121: return(trimmed);
4122: }
4123:
1.1.1.29 root 4124: bool msdos_is_device_path(char *path)
1.1.1.11 root 4125: {
4126: char full[MAX_PATH], *name;
4127:
1.1.1.24 root 4128: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4129: if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
4130: _stricmp(full, "\\\\.\\CON" ) == 0 ||
4131: _stricmp(full, "\\\\.\\NUL" ) == 0 ||
4132: _stricmp(full, "\\\\.\\PRN" ) == 0 ||
4133: _stricmp(full, "\\\\.\\COM1") == 0 ||
4134: _stricmp(full, "\\\\.\\COM2") == 0 ||
4135: _stricmp(full, "\\\\.\\COM3") == 0 ||
4136: _stricmp(full, "\\\\.\\COM4") == 0 ||
4137: _stricmp(full, "\\\\.\\COM5") == 0 ||
4138: _stricmp(full, "\\\\.\\COM6") == 0 ||
4139: _stricmp(full, "\\\\.\\COM7") == 0 ||
4140: _stricmp(full, "\\\\.\\COM8") == 0 ||
4141: _stricmp(full, "\\\\.\\COM9") == 0 ||
4142: _stricmp(full, "\\\\.\\LPT1") == 0 ||
4143: _stricmp(full, "\\\\.\\LPT2") == 0 ||
4144: _stricmp(full, "\\\\.\\LPT3") == 0 ||
4145: _stricmp(full, "\\\\.\\LPT4") == 0 ||
4146: _stricmp(full, "\\\\.\\LPT5") == 0 ||
4147: _stricmp(full, "\\\\.\\LPT6") == 0 ||
4148: _stricmp(full, "\\\\.\\LPT7") == 0 ||
4149: _stricmp(full, "\\\\.\\LPT8") == 0 ||
4150: _stricmp(full, "\\\\.\\LPT9") == 0) {
4151: return(true);
4152: } else if(name != NULL) {
4153: if(_stricmp(name, "CLOCK$" ) == 0 ||
4154: _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30 root 4155: _stricmp(name, "EMMXXXX0") == 0 ||
4156: _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29 root 4157: return(true);
4158: }
4159: }
1.1.1.24 root 4160: }
4161: return(false);
1.1.1.11 root 4162: }
4163:
1.1.1.29 root 4164: bool msdos_is_con_path(char *path)
1.1.1.8 root 4165: {
1.1.1.14 root 4166: char full[MAX_PATH], *name;
1.1.1.8 root 4167:
1.1.1.24 root 4168: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4169: return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24 root 4170: }
4171: return(false);
4172: }
4173:
1.1.1.29 root 4174: int msdos_is_comm_path(char *path)
1.1.1.24 root 4175: {
4176: char full[MAX_PATH], *name;
4177:
4178: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29 root 4179: if(_stricmp(full, "\\\\.\\COM1") == 0) {
4180: return(1);
4181: } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
4182: return(2);
4183: } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
4184: return(3);
4185: } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
4186: return(4);
1.1.1.24 root 4187: }
4188: }
1.1.1.29 root 4189: return(0);
4190: }
4191:
1.1.1.37 root 4192: void msdos_set_comm_params(int sio_port, char *path)
4193: {
4194: // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
4195: char *p = NULL;
4196:
4197: if((p = strstr(path, ":")) != NULL) {
4198: UINT8 selector = sio_read(sio_port - 1, 3);
4199:
4200: // baud rate
4201: int baud = max(110, min(9600, atoi(p + 1)));
4202: UINT16 divisor = 115200 / baud;
4203:
4204: if((p = strstr(p + 1, ",")) != NULL) {
4205: // parity
4206: if(p[1] == 'N' || p[1] == 'n') {
4207: selector = (selector & ~0x38) | 0x00;
4208: } else if(p[1] == 'O' || p[1] == 'o') {
4209: selector = (selector & ~0x38) | 0x08;
4210: } else if(p[1] == 'E' || p[1] == 'e') {
4211: selector = (selector & ~0x38) | 0x18;
4212: } else if(p[1] == 'M' || p[1] == 'm') {
4213: selector = (selector & ~0x38) | 0x28;
4214: } else if(p[1] == 'S' || p[1] == 's') {
4215: selector = (selector & ~0x38) | 0x38;
4216: }
4217: if((p = strstr(p + 1, ",")) != NULL) {
4218: // word length
4219: if(p[1] == '8') {
4220: selector = (selector & ~0x03) | 0x03;
4221: } else if(p[1] == '7') {
4222: selector = (selector & ~0x03) | 0x02;
4223: } else if(p[1] == '6') {
4224: selector = (selector & ~0x03) | 0x01;
4225: } else if(p[1] == '5') {
4226: selector = (selector & ~0x03) | 0x00;
4227: }
4228: if((p = strstr(p + 1, ",")) != NULL) {
4229: // stop bits
4230: float bits = atof(p + 1);
4231: if(bits > 1.0F) {
4232: selector |= 0x04;
4233: } else {
4234: selector &= ~0x04;
4235: }
4236: }
4237: }
4238: }
4239: sio_write(sio_port - 1, 3, selector | 0x80);
4240: sio_write(sio_port - 1, 0, divisor & 0xff);
4241: sio_write(sio_port - 1, 1, divisor >> 8);
4242: sio_write(sio_port - 1, 3, selector);
4243: }
4244: }
4245:
1.1.1.30 root 4246: int msdos_is_prn_path(char *path)
4247: {
4248: char full[MAX_PATH], *name;
4249:
4250: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
4251: if(_stricmp(full, "\\\\.\\PRN") == 0) {
4252: return(1);
4253: } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
4254: return(1);
4255: } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
4256: return(2);
4257: } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
4258: return(3);
4259: }
4260: }
4261: return(0);
4262: }
4263:
1.1.1.24 root 4264: bool msdos_is_existing_file(char *path)
4265: {
4266: // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
4267: WIN32_FIND_DATA FindData;
4268: HANDLE hFind;
4269:
4270: if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
4271: FindClose(hFind);
4272: return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
4273: }
4274: return(false);
1.1.1.8 root 4275: }
4276:
1.1.1.9 root 4277: char *msdos_search_command_com(char *command_path, char *env_path)
4278: {
4279: static char tmp[MAX_PATH];
1.1.1.28 root 4280: char path[ENV_SIZE], *file_name;
1.1.1.9 root 4281:
1.1.1.28 root 4282: // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9 root 4283: if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
4284: sprintf(file_name, "COMMAND.COM");
4285: if(_access(tmp, 0) == 0) {
4286: return(tmp);
4287: }
4288: }
1.1.1.28 root 4289:
4290: // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9 root 4291: if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
4292: sprintf(file_name, "COMMAND.COM");
4293: if(_access(tmp, 0) == 0) {
4294: return(tmp);
4295: }
4296: }
1.1.1.28 root 4297:
4298: // check if COMMAND.COM is in the current directory
1.1.1.9 root 4299: if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
4300: if(_access(tmp, 0) == 0) {
4301: return(tmp);
4302: }
4303: }
1.1.1.28 root 4304:
4305: // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
4306: strcpy(path, env_path);
4307: char *token = my_strtok(path, ";");
1.1.1.9 root 4308: while(token != NULL) {
1.1.1.14 root 4309: if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9 root 4310: strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
4311: if(_access(tmp, 0) == 0) {
4312: return(tmp);
4313: }
4314: }
4315: token = my_strtok(NULL, ";");
4316: }
4317: return(NULL);
4318: }
4319:
1.1.1.14 root 4320: int msdos_drive_number(const char *path)
1.1 root 4321: {
4322: char tmp[MAX_PATH], *name;
4323:
4324: GetFullPathName(path, MAX_PATH, tmp, &name);
4325: if(tmp[0] >= 'a' && tmp[0] <= 'z') {
4326: return(tmp[0] - 'a');
4327: } else {
4328: return(tmp[0] - 'A');
4329: }
4330: }
4331:
4332: char *msdos_volume_label(char *path)
4333: {
4334: static char tmp[MAX_PATH];
4335: char volume[] = "A:\\";
4336:
4337: if(path[1] == ':') {
4338: volume[0] = path[0];
4339: } else {
4340: volume[0] = 'A' + _getdrive() - 1;
4341: }
4342: if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
4343: memset(tmp, 0, sizeof(tmp));
4344: }
4345: return(tmp);
4346: }
4347:
4348: char *msdos_short_volume_label(char *label)
4349: {
4350: static char tmp[(8 + 1 + 3) + 1];
4351: char *src = label;
4352: int remain = strlen(label);
4353: char *dst_n = tmp;
4354: char *dst_e = tmp + 9;
4355:
4356: strcpy(tmp, " . ");
4357: for(int i = 0; i < 8 && remain > 0; i++) {
4358: if(msdos_lead_byte_check(*src)) {
4359: if(++i == 8) {
4360: break;
4361: }
4362: *dst_n++ = *src++;
4363: remain--;
4364: }
4365: *dst_n++ = *src++;
4366: remain--;
4367: }
4368: if(remain > 0) {
4369: for(int i = 0; i < 3 && remain > 0; i++) {
4370: if(msdos_lead_byte_check(*src)) {
4371: if(++i == 3) {
4372: break;
4373: }
4374: *dst_e++ = *src++;
4375: remain--;
4376: }
4377: *dst_e++ = *src++;
4378: remain--;
4379: }
4380: *dst_e = '\0';
4381: } else {
4382: *dst_n = '\0';
4383: }
4384: my_strupr(tmp);
4385: return(tmp);
4386: }
4387:
1.1.1.13 root 4388: errno_t msdos_maperr(unsigned long oserrno)
4389: {
4390: _doserrno = oserrno;
1.1.1.14 root 4391: switch(oserrno) {
1.1.1.13 root 4392: case ERROR_FILE_NOT_FOUND: // 2
4393: case ERROR_PATH_NOT_FOUND: // 3
4394: case ERROR_INVALID_DRIVE: // 15
4395: case ERROR_NO_MORE_FILES: // 18
4396: case ERROR_BAD_NETPATH: // 53
4397: case ERROR_BAD_NET_NAME: // 67
4398: case ERROR_BAD_PATHNAME: // 161
4399: case ERROR_FILENAME_EXCED_RANGE: // 206
4400: return ENOENT;
4401: case ERROR_TOO_MANY_OPEN_FILES: // 4
4402: return EMFILE;
4403: case ERROR_ACCESS_DENIED: // 5
4404: case ERROR_CURRENT_DIRECTORY: // 16
4405: case ERROR_NETWORK_ACCESS_DENIED: // 65
4406: case ERROR_CANNOT_MAKE: // 82
4407: case ERROR_FAIL_I24: // 83
4408: case ERROR_DRIVE_LOCKED: // 108
4409: case ERROR_SEEK_ON_DEVICE: // 132
4410: case ERROR_NOT_LOCKED: // 158
4411: case ERROR_LOCK_FAILED: // 167
4412: return EACCES;
4413: case ERROR_INVALID_HANDLE: // 6
4414: case ERROR_INVALID_TARGET_HANDLE: // 114
4415: case ERROR_DIRECT_ACCESS_HANDLE: // 130
4416: return EBADF;
4417: case ERROR_ARENA_TRASHED: // 7
4418: case ERROR_NOT_ENOUGH_MEMORY: // 8
4419: case ERROR_INVALID_BLOCK: // 9
4420: case ERROR_NOT_ENOUGH_QUOTA: // 1816
4421: return ENOMEM;
4422: case ERROR_BAD_ENVIRONMENT: // 10
4423: return E2BIG;
4424: case ERROR_BAD_FORMAT: // 11
4425: return ENOEXEC;
4426: case ERROR_NOT_SAME_DEVICE: // 17
4427: return EXDEV;
4428: case ERROR_FILE_EXISTS: // 80
4429: case ERROR_ALREADY_EXISTS: // 183
4430: return EEXIST;
4431: case ERROR_NO_PROC_SLOTS: // 89
4432: case ERROR_MAX_THRDS_REACHED: // 164
4433: case ERROR_NESTING_NOT_ALLOWED: // 215
4434: return EAGAIN;
4435: case ERROR_BROKEN_PIPE: // 109
4436: return EPIPE;
4437: case ERROR_DISK_FULL: // 112
4438: return ENOSPC;
4439: case ERROR_WAIT_NO_CHILDREN: // 128
4440: case ERROR_CHILD_NOT_COMPLETE: // 129
4441: return ECHILD;
4442: case ERROR_DIR_NOT_EMPTY: // 145
4443: return ENOTEMPTY;
4444: }
1.1.1.14 root 4445: if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13 root 4446: oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
4447: return EACCES;
4448: }
1.1.1.14 root 4449: if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13 root 4450: oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
4451: return ENOEXEC;
4452: }
4453: return EINVAL;
4454: }
4455:
4456: int msdos_open(const char *filename, int oflag)
4457: {
1.1.1.14 root 4458: if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13 root 4459: return _open(filename, oflag);
4460: }
1.1.1.14 root 4461:
4462: SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13 root 4463: DWORD disposition;
1.1.1.14 root 4464: switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
4465: default:
1.1.1.13 root 4466: case _O_EXCL:
4467: disposition = OPEN_EXISTING;
4468: break;
4469: case _O_CREAT:
4470: disposition = OPEN_ALWAYS;
4471: break;
4472: case _O_CREAT | _O_EXCL:
4473: case _O_CREAT | _O_TRUNC | _O_EXCL:
4474: disposition = CREATE_NEW;
4475: break;
4476: case _O_TRUNC:
4477: case _O_TRUNC | _O_EXCL:
4478: disposition = TRUNCATE_EXISTING;
4479: break;
4480: case _O_CREAT | _O_TRUNC:
4481: disposition = CREATE_ALWAYS;
4482: break;
4483: }
1.1.1.14 root 4484:
1.1.1.13 root 4485: HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
4486: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4487: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4488: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4489: // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
4490: // Retry without FILE_WRITE_ATTRIBUTES.
4491: h = CreateFile(filename, GENERIC_READ,
4492: FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
4493: FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14 root 4494: if(h == INVALID_HANDLE_VALUE) {
1.1.1.13 root 4495: errno = msdos_maperr(GetLastError());
4496: return -1;
4497: }
4498: }
1.1.1.14 root 4499:
1.1.1.13 root 4500: int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14 root 4501: if(fd == -1) {
1.1.1.13 root 4502: CloseHandle(h);
4503: }
4504: return fd;
4505: }
4506:
1.1.1.37 root 4507: 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 4508: {
4509: static int id = 0;
4510: char full[MAX_PATH], *name;
4511:
4512: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
4513: strcpy(file_handler[fd].path, full);
4514: } else {
4515: strcpy(file_handler[fd].path, path);
4516: }
1.1.1.14 root 4517: // isatty makes no distinction between CON & NUL
4518: // GetFileSize fails on CON, succeeds on NUL
4519: if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
4520: info = 0x8084;
4521: atty = 0;
4522: } else if(!atty && info == 0x80d3) {
4523: info = msdos_drive_number(".");
4524: }
1.1 root 4525: file_handler[fd].valid = 1;
4526: file_handler[fd].id = id++; // dummy id for int 21h ax=71a6h
1.1.1.37 root 4527: file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1 root 4528: file_handler[fd].mode = mode;
4529: file_handler[fd].info = info;
4530: file_handler[fd].psp = psp_seg;
1.1.1.37 root 4531: file_handler[fd].sio_port = sio_port;
4532: file_handler[fd].lpt_port = lpt_port;
1.1.1.21 root 4533:
4534: // init system file table
4535: if(fd < 20) {
4536: UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
4537:
4538: memset(sft, 0, 0x3b);
4539:
4540: *(UINT16 *)(sft + 0x00) = 1;
4541: *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
4542: *(UINT8 *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
4543: *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
4544:
4545: if(!(file_handler[fd].info & 0x80)) {
4546: *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
4547: *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
4548:
4549: FILETIME time, local;
4550: HANDLE hHandle;
4551: WORD dos_date = 0, dos_time = 0;
4552: DWORD file_size = 0;
4553: if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
4554: if(GetFileTime(hHandle, NULL, NULL, &time)) {
4555: FileTimeToLocalFileTime(&time, &local);
4556: FileTimeToDosDateTime(&local, &dos_date, &dos_time);
4557: }
4558: file_size = GetFileSize(hHandle, NULL);
4559: }
4560: *(UINT16 *)(sft + 0x0d) = dos_time;
4561: *(UINT16 *)(sft + 0x0f) = dos_date;
4562: *(UINT32 *)(sft + 0x11) = file_size;
4563: }
4564:
4565: char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
4566: _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
4567: my_strupr(fname);
4568: my_strupr(ext);
4569: memset(sft + 0x20, 0x20, 11);
4570: memcpy(sft + 0x20, fname, min(strlen(fname), 8));
4571: memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
4572:
4573: *(UINT16 *)(sft + 0x31) = psp_seg;
4574: }
1.1 root 4575: }
4576:
1.1.1.37 root 4577: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
4578: {
4579: msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
4580: }
4581:
1.1 root 4582: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
4583: {
4584: strcpy(file_handler[dst].path, file_handler[src].path);
4585: file_handler[dst].valid = 1;
4586: file_handler[dst].id = file_handler[src].id;
4587: file_handler[dst].atty = file_handler[src].atty;
4588: file_handler[dst].mode = file_handler[src].mode;
4589: file_handler[dst].info = file_handler[src].info;
4590: file_handler[dst].psp = psp_seg;
1.1.1.37 root 4591: file_handler[dst].sio_port = file_handler[src].sio_port;
4592: file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1 root 4593: }
4594:
1.1.1.20 root 4595: void msdos_file_handler_close(int fd)
1.1 root 4596: {
4597: file_handler[fd].valid = 0;
1.1.1.21 root 4598:
4599: if(fd < 20) {
4600: memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
4601: }
1.1 root 4602: }
4603:
1.1.1.14 root 4604: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1 root 4605: {
1.1.1.14 root 4606: return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
4607: FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
4608: FILE_ATTRIBUTE_DIRECTORY));
1.1 root 4609: }
4610:
4611: // find file
4612:
4613: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
4614: {
4615: if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
4616: return(0); // search directory only !!!
4617: } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
4618: return(0);
4619: } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
4620: return(0);
4621: } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
4622: return(0);
4623: } else if((attribute & required_mask) != required_mask) {
4624: return(0);
4625: } else {
4626: return(1);
4627: }
4628: }
4629:
1.1.1.13 root 4630: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
4631: {
1.1.1.14 root 4632: if(fd->cAlternateFileName[0]) {
1.1.1.42! root 4633: return(1);
1.1.1.13 root 4634: }
4635: size_t len = strlen(fd->cFileName);
1.1.1.14 root 4636: if(len > 12) {
1.1.1.42! root 4637: return(0);
1.1.1.13 root 4638: }
4639: const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14 root 4640: if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42! root 4641: return(0);
1.1.1.13 root 4642: }
1.1.1.42! root 4643: return(1);
1.1.1.13 root 4644: }
4645:
1.1 root 4646: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
4647: {
4648: FILETIME local;
4649:
4650: FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
4651: fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
4652: fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
4653:
4654: FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
4655: fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
4656: fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
4657:
4658: FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
4659: fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
4660: fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
4661: }
4662:
4663: // i/o
4664:
4665: void msdos_stdio_reopen()
4666: {
4667: if(!file_handler[0].valid) {
4668: _dup2(DUP_STDIN, 0);
4669: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
4670: }
4671: if(!file_handler[1].valid) {
4672: _dup2(DUP_STDOUT, 1);
4673: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
4674: }
4675: if(!file_handler[2].valid) {
4676: _dup2(DUP_STDERR, 2);
4677: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
4678: }
1.1.1.21 root 4679: if(!file_handler[3].valid) {
4680: _dup2(DUP_STDAUX, 3);
4681: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
4682: }
4683: if(!file_handler[4].valid) {
4684: _dup2(DUP_STDPRN, 4);
1.1.1.37 root 4685: msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21 root 4686: }
4687: for(int i = 0; i < 5; i++) {
4688: if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
4689: msdos_psp_set_file_table(i, i, current_psp);
4690: }
4691: }
1.1 root 4692: }
4693:
1.1.1.37 root 4694: int msdos_read(int fd, void *buffer, unsigned int count)
4695: {
4696: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
4697: // read from serial port
4698: int read = 0;
4699: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
4700: UINT8 *buf = (UINT8 *)buffer;
4701: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
4702: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 4703: DWORD timeout = timeGetTime() + 1000;
4704: while(read < count) {
4705: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
4706: buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
4707: timeout = timeGetTime() + 1000;
4708: } else {
4709: if(timeGetTime() > timeout) {
4710: break;
4711: }
4712: Sleep(10);
1.1.1.37 root 4713: }
4714: }
4715: sio_write(file_handler[fd].sio_port - 1, 3, selector);
4716: }
4717: return(read);
4718: }
4719: return(_read(fd, buffer, count));
4720: }
4721:
1.1 root 4722: int msdos_kbhit()
4723: {
4724: msdos_stdio_reopen();
4725:
1.1.1.20 root 4726: process_t *process = msdos_process_info_get(current_psp);
4727: int fd = msdos_psp_get_file_table(0, current_psp);
4728:
4729: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 4730: // stdin is redirected to file
1.1.1.20 root 4731: return(eof(fd) == 0);
1.1 root 4732: }
4733:
4734: // check keyboard status
1.1.1.35 root 4735: if(key_recv != 0) {
1.1 root 4736: return(1);
4737: }
1.1.1.35 root 4738: if(key_buf_char != NULL && key_buf_scan != NULL) {
4739: #ifdef USE_SERVICE_THREAD
4740: EnterCriticalSection(&key_buf_crit_sect);
4741: #endif
4742: bool empty = key_buf_char->empty();
4743: #ifdef USE_SERVICE_THREAD
4744: LeaveCriticalSection(&key_buf_crit_sect);
4745: #endif
4746: if(!empty) return(1);
4747: }
4748: return(_kbhit());
1.1 root 4749: }
4750:
4751: int msdos_getch_ex(int echo)
4752: {
4753: static char prev = 0;
4754:
4755: msdos_stdio_reopen();
4756:
1.1.1.20 root 4757: process_t *process = msdos_process_info_get(current_psp);
4758: int fd = msdos_psp_get_file_table(0, current_psp);
4759:
4760: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 4761: // stdin is redirected to file
4762: retry:
4763: char data;
1.1.1.37 root 4764: if(msdos_read(fd, &data, 1) == 1) {
1.1 root 4765: char tmp = data;
4766: if(data == 0x0a) {
4767: if(prev == 0x0d) {
4768: goto retry; // CRLF -> skip LF
4769: } else {
4770: data = 0x0d; // LF only -> CR
4771: }
4772: }
4773: prev = tmp;
4774: return(data);
4775: }
4776: return(EOF);
4777: }
4778:
4779: // input from console
1.1.1.5 root 4780: int key_char, key_scan;
1.1.1.33 root 4781: if(key_recv != 0) {
1.1.1.5 root 4782: key_char = (key_code >> 0) & 0xff;
4783: key_scan = (key_code >> 8) & 0xff;
4784: key_code >>= 16;
1.1.1.33 root 4785: key_recv >>= 16;
1.1.1.5 root 4786: } else {
1.1.1.35 root 4787: while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
4788: if(key_buf_char != NULL && key_buf_scan != NULL) {
4789: #ifdef USE_SERVICE_THREAD
4790: EnterCriticalSection(&key_buf_crit_sect);
4791: #endif
4792: bool empty = key_buf_char->empty();
4793: #ifdef USE_SERVICE_THREAD
4794: LeaveCriticalSection(&key_buf_crit_sect);
4795: #endif
4796: if(!empty) break;
4797: }
1.1.1.23 root 4798: if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
4799: // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
4800: if(_kbhit()) {
1.1.1.32 root 4801: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 4802: #ifdef USE_SERVICE_THREAD
4803: EnterCriticalSection(&key_buf_crit_sect);
4804: #endif
1.1.1.32 root 4805: key_buf_char->write(_getch());
1.1.1.35 root 4806: key_buf_scan->write(0x00);
4807: #ifdef USE_SERVICE_THREAD
4808: LeaveCriticalSection(&key_buf_crit_sect);
4809: #endif
1.1.1.32 root 4810: }
1.1.1.23 root 4811: } else {
4812: Sleep(10);
4813: }
4814: } else {
4815: if(!update_key_buffer()) {
4816: Sleep(10);
4817: }
1.1.1.14 root 4818: }
4819: }
4820: if(m_halted) {
1.1.1.33 root 4821: // insert CR to terminate input loops
1.1.1.14 root 4822: key_char = 0x0d;
4823: key_scan = 0;
1.1.1.32 root 4824: } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 4825: #ifdef USE_SERVICE_THREAD
4826: EnterCriticalSection(&key_buf_crit_sect);
4827: #endif
1.1.1.14 root 4828: key_char = key_buf_char->read();
4829: key_scan = key_buf_scan->read();
1.1.1.41 root 4830: // write to bottom of key buffer
4831: mem[0x43c] = (UINT8)key_char;
4832: mem[0x43d] = (UINT8)key_scan;
1.1.1.35 root 4833: #ifdef USE_SERVICE_THREAD
4834: LeaveCriticalSection(&key_buf_crit_sect);
4835: #endif
1.1.1.5 root 4836: }
1.1 root 4837: }
4838: if(echo && key_char) {
4839: msdos_putch(key_char);
4840: }
4841: return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
4842: }
4843:
4844: inline int msdos_getch()
4845: {
4846: return(msdos_getch_ex(0));
4847: }
4848:
4849: inline int msdos_getche()
4850: {
4851: return(msdos_getch_ex(1));
4852: }
4853:
4854: int msdos_write(int fd, const void *buffer, unsigned int count)
4855: {
1.1.1.37 root 4856: if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
4857: // write to serial port
1.1.1.38 root 4858: int written = 0;
1.1.1.37 root 4859: if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
4860: UINT8 *buf = (UINT8 *)buffer;
4861: UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
4862: sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38 root 4863: DWORD timeout = timeGetTime() + 1000;
4864: while(written < count) {
4865: if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
4866: sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
4867: timeout = timeGetTime() + 1000;
4868: } else {
4869: if(timeGetTime() > timeout) {
4870: break;
4871: }
4872: Sleep(10);
4873: }
1.1.1.37 root 4874: }
4875: sio_write(file_handler[fd].sio_port - 1, 3, selector);
4876: }
1.1.1.38 root 4877: return(written);
1.1.1.37 root 4878: } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
4879: // write to printer port
4880: UINT8 *buf = (UINT8 *)buffer;
4881: for(unsigned int i = 0; i < count; i++) {
4882: // printer_out(file_handler[fd].lpt_port - 1, buf[i]);
4883: pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
4884: }
4885: return(count);
4886: } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1 root 4887: // CR+LF -> LF
1.1.1.37 root 4888: static int is_cr = 0;
1.1 root 4889: UINT8 *buf = (UINT8 *)buffer;
4890: for(unsigned int i = 0; i < count; i++) {
4891: UINT8 data = buf[i];
4892: if(is_cr) {
4893: if(data != 0x0a) {
4894: UINT8 tmp = 0x0d;
4895: _write(1, &tmp, 1);
4896: }
4897: _write(1, &data, 1);
4898: is_cr = 0;
4899: } else if(data == 0x0d) {
4900: is_cr = 1;
4901: } else {
4902: _write(1, &data, 1);
4903: }
4904: }
4905: return(count);
4906: }
1.1.1.14 root 4907: vram_flush();
1.1 root 4908: return(_write(fd, buffer, count));
4909: }
4910:
4911: void msdos_putch(UINT8 data)
1.1.1.35 root 4912: #ifdef USE_SERVICE_THREAD
4913: {
4914: EnterCriticalSection(&putch_crit_sect);
4915: msdos_putch_tmp(data);
4916: LeaveCriticalSection(&putch_crit_sect);
4917: }
4918: void msdos_putch_tmp(UINT8 data)
4919: #endif
1.1 root 4920: {
1.1.1.34 root 4921: CONSOLE_SCREEN_BUFFER_INFO csbi;
4922: SMALL_RECT rect;
4923: COORD co;
1.1 root 4924: static int p = 0;
4925: static int is_kanji = 0;
4926: static int is_esc = 0;
4927: static int stored_x;
4928: static int stored_y;
4929: static WORD stored_a;
1.1.1.20 root 4930: static char tmp[64], out[64];
1.1 root 4931:
4932: msdos_stdio_reopen();
4933:
1.1.1.20 root 4934: process_t *process = msdos_process_info_get(current_psp);
4935: int fd = msdos_psp_get_file_table(1, current_psp);
4936:
4937: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 4938: // stdout is redirected to file
1.1.1.20 root 4939: msdos_write(fd, &data, 1);
1.1 root 4940: return;
4941: }
1.1.1.23 root 4942: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 4943:
4944: // output to console
4945: tmp[p++] = data;
4946:
1.1.1.14 root 4947: vram_flush();
4948:
1.1 root 4949: if(is_kanji) {
4950: // kanji character
4951: is_kanji = 0;
4952: } else if(is_esc) {
4953: // escape sequense
4954: if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
4955: p = is_esc = 0;
4956: } else if(tmp[1] == '=' && p == 4) {
4957: co.X = tmp[3] - 0x20;
1.1.1.14 root 4958: co.Y = tmp[2] - 0x20 + scr_top;
1.1 root 4959: SetConsoleCursorPosition(hStdout, co);
4960: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 4961: mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1 root 4962: cursor_moved = false;
4963: p = is_esc = 0;
4964: } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
4965: GetConsoleScreenBufferInfo(hStdout, &csbi);
4966: co.X = csbi.dwCursorPosition.X;
4967: co.Y = csbi.dwCursorPosition.Y;
4968: WORD wAttributes = csbi.wAttributes;
4969:
4970: if(tmp[1] == 'D') {
4971: co.Y++;
4972: } else if(tmp[1] == 'E') {
4973: co.X = 0;
4974: co.Y++;
4975: } else if(tmp[1] == 'M') {
4976: co.Y--;
4977: } else if(tmp[1] == '*') {
1.1.1.14 root 4978: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
4979: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
4980: co.X = 0;
4981: co.Y = csbi.srWindow.Top;
1.1 root 4982: } else if(tmp[1] == '[') {
4983: int param[256], params = 0;
4984: memset(param, 0, sizeof(param));
4985: for(int i = 2; i < p; i++) {
4986: if(tmp[i] >= '0' && tmp[i] <= '9') {
4987: param[params] *= 10;
4988: param[params] += tmp[i] - '0';
4989: } else {
4990: params++;
4991: }
4992: }
4993: if(data == 'A') {
1.1.1.14 root 4994: co.Y -= (params == 0) ? 1 : param[0];
1.1 root 4995: } else if(data == 'B') {
1.1.1.14 root 4996: co.Y += (params == 0) ? 1 : param[0];
1.1 root 4997: } else if(data == 'C') {
1.1.1.14 root 4998: co.X += (params == 0) ? 1 : param[0];
1.1 root 4999: } else if(data == 'D') {
1.1.1.14 root 5000: co.X -= (params == 0) ? 1 : param[0];
1.1 root 5001: } else if(data == 'H' || data == 'f') {
1.1.1.14 root 5002: co.X = (param[1] == 0 ? 1 : param[1]) - 1;
5003: co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1 root 5004: } else if(data == 'J') {
1.1.1.14 root 5005: clear_scr_buffer(csbi.wAttributes);
1.1 root 5006: if(param[0] == 0) {
5007: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5008: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5009: if(co.Y < csbi.srWindow.Bottom) {
5010: SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5011: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5012: }
5013: } else if(param[0] == 1) {
1.1.1.14 root 5014: if(co.Y > csbi.srWindow.Top) {
5015: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
5016: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5017: }
5018: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14 root 5019: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5020: } else if(param[0] == 2) {
1.1.1.14 root 5021: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5022: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5023: co.X = co.Y = 0;
5024: }
5025: } else if(data == 'K') {
1.1.1.14 root 5026: clear_scr_buffer(csbi.wAttributes);
1.1 root 5027: if(param[0] == 0) {
5028: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5029: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5030: } else if(param[0] == 1) {
5031: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14 root 5032: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5033: } else if(param[0] == 2) {
5034: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14 root 5035: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5036: }
5037: } else if(data == 'L') {
1.1.1.14 root 5038: if(params == 0) {
5039: param[0] = 1;
1.1 root 5040: }
1.1.1.14 root 5041: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5042: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5043: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5044: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5045: clear_scr_buffer(csbi.wAttributes);
1.1 root 5046: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14 root 5047: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5048: co.X = 0;
5049: } else if(data == 'M') {
1.1.1.14 root 5050: if(params == 0) {
5051: param[0] = 1;
5052: }
5053: if(co.Y + param[0] > csbi.srWindow.Bottom) {
5054: clear_scr_buffer(csbi.wAttributes);
5055: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5056: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 5057: } else {
1.1.1.14 root 5058: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5059: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5060: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
5061: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
5062: clear_scr_buffer(csbi.wAttributes);
1.1 root 5063: }
5064: co.X = 0;
5065: } else if(data == 'h') {
5066: if(tmp[2] == '>' && tmp[3] == '5') {
5067: CONSOLE_CURSOR_INFO cur;
5068: GetConsoleCursorInfo(hStdout, &cur);
5069: if(cur.bVisible) {
5070: cur.bVisible = FALSE;
1.1.1.14 root 5071: // SetConsoleCursorInfo(hStdout, &cur);
1.1 root 5072: }
5073: }
5074: } else if(data == 'l') {
5075: if(tmp[2] == '>' && tmp[3] == '5') {
5076: CONSOLE_CURSOR_INFO cur;
5077: GetConsoleCursorInfo(hStdout, &cur);
5078: if(!cur.bVisible) {
5079: cur.bVisible = TRUE;
1.1.1.14 root 5080: // SetConsoleCursorInfo(hStdout, &cur);
1.1 root 5081: }
5082: }
5083: } else if(data == 'm') {
5084: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
5085: int reverse = 0, hidden = 0;
5086: for(int i = 0; i < params; i++) {
5087: if(param[i] == 1) {
5088: wAttributes |= FOREGROUND_INTENSITY;
5089: } else if(param[i] == 4) {
5090: wAttributes |= COMMON_LVB_UNDERSCORE;
5091: } else if(param[i] == 7) {
5092: reverse = 1;
5093: } else if(param[i] == 8 || param[i] == 16) {
5094: hidden = 1;
5095: } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
5096: wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
5097: if(param[i] >= 17 && param[i] <= 23) {
5098: param[i] -= 16;
5099: } else {
5100: param[i] -= 30;
5101: }
5102: if(param[i] & 1) {
5103: wAttributes |= FOREGROUND_RED;
5104: }
5105: if(param[i] & 2) {
5106: wAttributes |= FOREGROUND_GREEN;
5107: }
5108: if(param[i] & 4) {
5109: wAttributes |= FOREGROUND_BLUE;
5110: }
5111: } else if(param[i] >= 40 && param[i] <= 47) {
5112: wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
5113: if((param[i] - 40) & 1) {
5114: wAttributes |= BACKGROUND_RED;
5115: }
5116: if((param[i] - 40) & 2) {
5117: wAttributes |= BACKGROUND_GREEN;
5118: }
5119: if((param[i] - 40) & 4) {
5120: wAttributes |= BACKGROUND_BLUE;
5121: }
5122: }
5123: }
5124: if(reverse) {
5125: wAttributes &= ~0xff;
5126: wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
5127: }
5128: if(hidden) {
5129: wAttributes &= ~0x0f;
5130: wAttributes |= (wAttributes >> 4) & 0x0f;
5131: }
5132: } else if(data == 'n') {
5133: if(param[0] == 6) {
5134: char tmp[16];
5135: sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
5136: int len = strlen(tmp);
1.1.1.32 root 5137: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 5138: #ifdef USE_SERVICE_THREAD
5139: EnterCriticalSection(&key_buf_crit_sect);
5140: #endif
1.1.1.32 root 5141: for(int i = 0; i < len; i++) {
5142: key_buf_char->write(tmp[i]);
5143: key_buf_scan->write(0x00);
5144: }
1.1.1.35 root 5145: #ifdef USE_SERVICE_THREAD
5146: LeaveCriticalSection(&key_buf_crit_sect);
5147: #endif
1.1 root 5148: }
5149: }
5150: } else if(data == 's') {
5151: stored_x = co.X;
5152: stored_y = co.Y;
5153: stored_a = wAttributes;
5154: } else if(data == 'u') {
5155: co.X = stored_x;
5156: co.Y = stored_y;
5157: wAttributes = stored_a;
5158: }
5159: }
5160: if(co.X < 0) {
5161: co.X = 0;
5162: } else if(co.X >= csbi.dwSize.X) {
5163: co.X = csbi.dwSize.X - 1;
5164: }
1.1.1.14 root 5165: if(co.Y < csbi.srWindow.Top) {
5166: co.Y = csbi.srWindow.Top;
5167: } else if(co.Y > csbi.srWindow.Bottom) {
5168: co.Y = csbi.srWindow.Bottom;
1.1 root 5169: }
5170: if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
5171: SetConsoleCursorPosition(hStdout, co);
5172: mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14 root 5173: mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1 root 5174: cursor_moved = false;
5175: }
5176: if(wAttributes != csbi.wAttributes) {
5177: SetConsoleTextAttribute(hStdout, wAttributes);
5178: }
5179: p = is_esc = 0;
5180: }
5181: return;
5182: } else {
5183: if(msdos_lead_byte_check(data)) {
5184: is_kanji = 1;
5185: return;
5186: } else if(data == 0x1b) {
5187: is_esc = 1;
5188: return;
5189: }
5190: }
1.1.1.20 root 5191:
5192: DWORD q = 0, num;
5193: is_kanji = 0;
5194: for(int i = 0; i < p; i++) {
5195: UINT8 c = tmp[i];
5196: if(is_kanji) {
5197: is_kanji = 0;
5198: } else if(msdos_lead_byte_check(data)) {
5199: is_kanji = 1;
5200: } else if(msdos_ctrl_code_check(data)) {
5201: out[q++] = '^';
5202: c += 'A' - 1;
5203: }
5204: out[q++] = c;
5205: }
1.1.1.34 root 5206: if(q == 1 && out[0] == 0x08) {
5207: // back space
5208: GetConsoleScreenBufferInfo(hStdout, &csbi);
5209: if(csbi.dwCursorPosition.X > 0) {
5210: co.X = csbi.dwCursorPosition.X - 1;
5211: co.Y = csbi.dwCursorPosition.Y;
5212: SetConsoleCursorPosition(hStdout, co);
5213: } else if(csbi.dwCursorPosition.Y > 0) {
5214: co.X = csbi.dwSize.X - 1;
5215: co.Y = csbi.dwCursorPosition.Y - 1;
5216: SetConsoleCursorPosition(hStdout, co);
5217: } else {
5218: WriteConsole(hStdout, out, q, &num, NULL); // to make sure
5219: }
5220: } else {
5221: WriteConsole(hStdout, out, q, &num, NULL);
5222: }
1.1 root 5223: p = 0;
1.1.1.14 root 5224:
1.1.1.15 root 5225: if(!restore_console_on_exit) {
5226: GetConsoleScreenBufferInfo(hStdout, &csbi);
5227: scr_top = csbi.srWindow.Top;
5228: }
1.1 root 5229: cursor_moved = true;
5230: }
5231:
5232: int msdos_aux_in()
5233: {
1.1.1.21 root 5234: msdos_stdio_reopen();
5235:
1.1.1.20 root 5236: process_t *process = msdos_process_info_get(current_psp);
5237: int fd = msdos_psp_get_file_table(3, current_psp);
5238:
5239: if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1 root 5240: char data = 0;
1.1.1.37 root 5241: msdos_read(fd, &data, 1);
1.1 root 5242: return(data);
5243: } else {
5244: return(EOF);
5245: }
5246: }
5247:
5248: void msdos_aux_out(char data)
5249: {
1.1.1.21 root 5250: msdos_stdio_reopen();
5251:
1.1.1.20 root 5252: process_t *process = msdos_process_info_get(current_psp);
5253: int fd = msdos_psp_get_file_table(3, current_psp);
5254:
5255: if(fd < process->max_files && file_handler[fd].valid) {
5256: msdos_write(fd, &data, 1);
1.1 root 5257: }
5258: }
5259:
5260: void msdos_prn_out(char data)
5261: {
1.1.1.21 root 5262: msdos_stdio_reopen();
5263:
1.1.1.20 root 5264: process_t *process = msdos_process_info_get(current_psp);
5265: int fd = msdos_psp_get_file_table(4, current_psp);
5266:
5267: if(fd < process->max_files && file_handler[fd].valid) {
5268: msdos_write(fd, &data, 1);
1.1 root 5269: }
5270: }
5271:
5272: // memory control
5273:
1.1.1.39 root 5274: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, char *prog_name)
1.1 root 5275: {
5276: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5277:
5278: mcb->mz = mz;
5279: mcb->psp = psp;
1.1.1.30 root 5280: mcb->paragraphs = paragraphs;
1.1.1.39 root 5281:
5282: if(prog_name != NULL) {
5283: memset(mcb->prog_name, 0, 8);
5284: memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
5285: }
1.1 root 5286: return(mcb);
5287: }
5288:
1.1.1.39 root 5289: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
5290: {
5291: return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
5292: }
5293:
1.1 root 5294: void msdos_mcb_check(mcb_t *mcb)
5295: {
5296: if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28 root 5297: #if 0
5298: // shutdown now !!!
5299: fatalerror("broken memory control block\n");
5300: #else
5301: // return error code and continue
5302: throw(0x07); // broken memory control block
5303: #endif
1.1 root 5304: }
5305: }
5306:
1.1.1.39 root 5307: void msdos_mem_split(int seg, int paragraphs)
1.1 root 5308: {
5309: int mcb_seg = seg - 1;
5310: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5311: msdos_mcb_check(mcb);
5312:
1.1.1.30 root 5313: if(mcb->paragraphs > paragraphs) {
1.1 root 5314: int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30 root 5315: int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1 root 5316:
5317: msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
5318: mcb->mz = 'M';
1.1.1.30 root 5319: mcb->paragraphs = paragraphs;
1.1 root 5320: }
5321: }
5322:
5323: void msdos_mem_merge(int seg)
5324: {
5325: int mcb_seg = seg - 1;
5326: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5327: msdos_mcb_check(mcb);
5328:
5329: while(1) {
5330: if(mcb->mz == 'Z') {
5331: break;
5332: }
1.1.1.30 root 5333: int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1 root 5334: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5335: msdos_mcb_check(next_mcb);
5336:
5337: if(next_mcb->psp != 0) {
5338: break;
5339: }
5340: mcb->mz = next_mcb->mz;
1.1.1.30 root 5341: mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1 root 5342: }
5343: }
5344:
1.1.1.8 root 5345: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1 root 5346: {
5347: while(1) {
5348: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5349: bool last_block;
1.1 root 5350:
1.1.1.14 root 5351: if(mcb->psp == 0) {
5352: msdos_mem_merge(mcb_seg + 1);
5353: } else {
5354: msdos_mcb_check(mcb);
5355: }
1.1.1.33 root 5356: if(!(last_block = (mcb->mz == 'Z'))) {
5357: // check if the next is dummy mcb to link to umb
5358: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5359: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5360: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5361: }
5362: if(!(new_process && !last_block)) {
1.1.1.30 root 5363: if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1 root 5364: msdos_mem_split(mcb_seg + 1, paragraphs);
5365: mcb->psp = current_psp;
5366: return(mcb_seg + 1);
5367: }
5368: }
5369: if(mcb->mz == 'Z') {
5370: break;
5371: }
1.1.1.30 root 5372: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5373: }
5374: return(-1);
5375: }
5376:
5377: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
5378: {
5379: int mcb_seg = seg - 1;
5380: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5381: msdos_mcb_check(mcb);
1.1.1.30 root 5382: int current_paragraphs = mcb->paragraphs;
1.1 root 5383:
5384: msdos_mem_merge(seg);
1.1.1.30 root 5385: if(paragraphs > mcb->paragraphs) {
1.1.1.14 root 5386: if(max_paragraphs) {
1.1.1.30 root 5387: *max_paragraphs = mcb->paragraphs;
1.1.1.14 root 5388: }
1.1 root 5389: msdos_mem_split(seg, current_paragraphs);
5390: return(-1);
5391: }
5392: msdos_mem_split(seg, paragraphs);
5393: return(0);
5394: }
5395:
5396: void msdos_mem_free(int seg)
5397: {
5398: int mcb_seg = seg - 1;
5399: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5400: msdos_mcb_check(mcb);
5401:
5402: mcb->psp = 0;
5403: msdos_mem_merge(seg);
5404: }
5405:
1.1.1.8 root 5406: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1 root 5407: {
5408: int max_paragraphs = 0;
5409:
5410: while(1) {
5411: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 5412: bool last_block;
5413:
1.1 root 5414: msdos_mcb_check(mcb);
5415:
1.1.1.33 root 5416: if(!(last_block = (mcb->mz == 'Z'))) {
5417: // check if the next is dummy mcb to link to umb
5418: int next_seg = mcb_seg + 1 + mcb->paragraphs;
5419: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
5420: last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
5421: }
5422: if(!(new_process && !last_block)) {
1.1.1.30 root 5423: if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
5424: max_paragraphs = mcb->paragraphs;
1.1 root 5425: }
5426: }
5427: if(mcb->mz == 'Z') {
5428: break;
5429: }
1.1.1.30 root 5430: mcb_seg += 1 + mcb->paragraphs;
1.1 root 5431: }
1.1.1.14 root 5432: return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1 root 5433: }
5434:
1.1.1.8 root 5435: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
5436: {
5437: int last_seg = -1;
5438:
5439: while(1) {
5440: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
5441: msdos_mcb_check(mcb);
5442:
1.1.1.14 root 5443: if(mcb->psp == psp) {
1.1.1.8 root 5444: last_seg = mcb_seg;
5445: }
1.1.1.14 root 5446: if(mcb->mz == 'Z') {
5447: break;
5448: }
1.1.1.30 root 5449: mcb_seg += 1 + mcb->paragraphs;
1.1.1.8 root 5450: }
5451: return(last_seg);
5452: }
5453:
1.1.1.19 root 5454: int msdos_mem_get_umb_linked()
5455: {
1.1.1.33 root 5456: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5457: msdos_mcb_check(mcb);
1.1.1.19 root 5458:
1.1.1.33 root 5459: if(mcb->mz == 'M') {
5460: return(-1);
1.1.1.19 root 5461: }
5462: return(0);
5463: }
5464:
1.1.1.33 root 5465: void msdos_mem_link_umb()
1.1.1.19 root 5466: {
1.1.1.33 root 5467: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5468: msdos_mcb_check(mcb);
1.1.1.19 root 5469:
1.1.1.33 root 5470: mcb->mz = 'M';
5471: mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39 root 5472:
5473: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19 root 5474: }
5475:
1.1.1.33 root 5476: void msdos_mem_unlink_umb()
1.1.1.19 root 5477: {
1.1.1.33 root 5478: mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
5479: msdos_mcb_check(mcb);
1.1.1.19 root 5480:
1.1.1.33 root 5481: mcb->mz = 'Z';
5482: mcb->paragraphs = 0;
1.1.1.39 root 5483:
5484: ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19 root 5485: }
5486:
1.1.1.29 root 5487: #ifdef SUPPORT_HMA
5488:
5489: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
5490: {
5491: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5492:
5493: mcb->ms[0] = 'M';
5494: mcb->ms[1] = 'S';
5495: mcb->owner = owner;
5496: mcb->size = size;
5497: mcb->next = next;
5498: return(mcb);
5499: }
5500:
5501: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
5502: {
5503: return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
5504: }
5505:
5506: int msdos_hma_mem_split(int offset, int size)
5507: {
5508: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5509:
5510: if(!msdos_is_hma_mcb_valid(mcb)) {
5511: return(-1);
5512: }
5513: if(mcb->size >= size + 0x10) {
5514: int new_offset = offset + 0x10 + size;
5515: int new_size = mcb->size - 0x10 - size;
5516:
5517: msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
5518: mcb->size = size;
5519: mcb->next = new_offset;
5520: return(0);
5521: }
5522: return(-1);
5523: }
5524:
5525: void msdos_hma_mem_merge(int offset)
5526: {
5527: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5528:
5529: if(!msdos_is_hma_mcb_valid(mcb)) {
5530: return;
5531: }
5532: while(1) {
5533: if(mcb->next == 0) {
5534: break;
5535: }
5536: hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
5537:
5538: if(!msdos_is_hma_mcb_valid(next_mcb)) {
5539: return;
5540: }
5541: if(next_mcb->owner != 0) {
5542: break;
5543: }
5544: mcb->size += 0x10 + next_mcb->size;
5545: mcb->next = next_mcb->next;
5546: }
5547: }
5548:
5549: int msdos_hma_mem_alloc(int size, UINT16 owner)
5550: {
5551: int offset = 0x10; // first mcb in HMA
5552:
5553: while(1) {
5554: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5555:
5556: if(!msdos_is_hma_mcb_valid(mcb)) {
5557: return(-1);
5558: }
5559: if(mcb->owner == 0) {
5560: msdos_hma_mem_merge(offset);
5561: }
5562: if(mcb->owner == 0 && mcb->size >= size) {
5563: msdos_hma_mem_split(offset, size);
5564: mcb->owner = owner;
5565: return(offset);
5566: }
5567: if(mcb->next == 0) {
5568: break;
5569: }
5570: offset = mcb->next;
5571: }
5572: return(-1);
5573: }
5574:
5575: int msdos_hma_mem_realloc(int offset, int size)
5576: {
5577: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5578:
5579: if(!msdos_is_hma_mcb_valid(mcb)) {
5580: return(-1);
5581: }
5582: if(mcb->size < size) {
5583: return(-1);
5584: }
5585: msdos_hma_mem_split(offset, size);
5586: return(0);
5587: }
5588:
5589: void msdos_hma_mem_free(int offset)
5590: {
5591: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5592:
5593: if(!msdos_is_hma_mcb_valid(mcb)) {
5594: return;
5595: }
5596: mcb->owner = 0;
5597: msdos_hma_mem_merge(offset);
5598: }
5599:
5600: int msdos_hma_mem_get_free(int *available_offset)
5601: {
5602: int offset = 0x10; // first mcb in HMA
5603: int size = 0;
5604:
5605: while(1) {
5606: hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
5607:
5608: if(!msdos_is_hma_mcb_valid(mcb)) {
5609: return(0);
5610: }
5611: if(mcb->owner == 0 && size < mcb->size) {
5612: if(available_offset != NULL) {
5613: *available_offset = offset;
5614: }
5615: size = mcb->size;
5616: }
5617: if(mcb->next == 0) {
5618: break;
5619: }
5620: offset = mcb->next;
5621: }
5622: return(size);
5623: }
5624:
5625: #endif
5626:
1.1 root 5627: // environment
5628:
5629: void msdos_env_set_argv(int env_seg, char *argv)
5630: {
5631: char *dst = (char *)(mem + (env_seg << 4));
5632:
5633: while(1) {
5634: if(dst[0] == 0) {
5635: break;
5636: }
5637: dst += strlen(dst) + 1;
5638: }
5639: *dst++ = 0; // end of environment
5640: *dst++ = 1; // top of argv[0]
5641: *dst++ = 0;
5642: memcpy(dst, argv, strlen(argv));
5643: dst += strlen(argv);
5644: *dst++ = 0;
5645: *dst++ = 0;
5646: }
5647:
5648: char *msdos_env_get_argv(int env_seg)
5649: {
5650: static char env[ENV_SIZE];
5651: char *src = env;
5652:
5653: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
5654: while(1) {
5655: if(src[0] == 0) {
5656: if(src[1] == 1) {
5657: return(src + 3);
5658: }
5659: break;
5660: }
5661: src += strlen(src) + 1;
5662: }
5663: return(NULL);
5664: }
5665:
5666: char *msdos_env_get(int env_seg, const char *name)
5667: {
5668: static char env[ENV_SIZE];
5669: char *src = env;
5670:
5671: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
5672: while(1) {
5673: if(src[0] == 0) {
5674: break;
5675: }
5676: int len = strlen(src);
5677: char *n = my_strtok(src, "=");
5678: char *v = src + strlen(n) + 1;
5679:
5680: if(_stricmp(name, n) == 0) {
5681: return(v);
5682: }
5683: src += len + 1;
5684: }
5685: return(NULL);
5686: }
5687:
5688: void msdos_env_set(int env_seg, char *name, char *value)
5689: {
5690: char env[ENV_SIZE];
5691: char *src = env;
5692: char *dst = (char *)(mem + (env_seg << 4));
5693: char *argv = msdos_env_get_argv(env_seg);
5694: int done = 0;
5695:
5696: memcpy(src, dst, ENV_SIZE);
5697: memset(dst, 0, ENV_SIZE);
5698: while(1) {
5699: if(src[0] == 0) {
5700: break;
5701: }
5702: int len = strlen(src);
5703: char *n = my_strtok(src, "=");
5704: char *v = src + strlen(n) + 1;
5705: char tmp[1024];
5706:
5707: if(_stricmp(name, n) == 0) {
5708: sprintf(tmp, "%s=%s", n, value);
5709: done = 1;
5710: } else {
5711: sprintf(tmp, "%s=%s", n, v);
5712: }
5713: memcpy(dst, tmp, strlen(tmp));
5714: dst += strlen(tmp) + 1;
5715: src += len + 1;
5716: }
5717: if(!done) {
5718: char tmp[1024];
5719:
5720: sprintf(tmp, "%s=%s", name, value);
5721: memcpy(dst, tmp, strlen(tmp));
5722: dst += strlen(tmp) + 1;
5723: }
5724: if(argv) {
5725: *dst++ = 0; // end of environment
5726: *dst++ = 1; // top of argv[0]
5727: *dst++ = 0;
5728: memcpy(dst, argv, strlen(argv));
5729: dst += strlen(argv);
5730: *dst++ = 0;
5731: *dst++ = 0;
5732: }
5733: }
5734:
5735: // process
5736:
1.1.1.8 root 5737: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1 root 5738: {
5739: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
5740:
5741: memset(psp, 0, PSP_SIZE);
5742: psp->exit[0] = 0xcd;
5743: psp->exit[1] = 0x20;
1.1.1.8 root 5744: psp->first_mcb = mcb_seg;
1.1 root 5745: psp->far_call = 0xea;
5746: psp->cpm_entry.w.l = 0xfff1; // int 21h, retf
5747: psp->cpm_entry.w.h = 0xf000;
5748: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
5749: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
5750: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
5751: psp->parent_psp = parent_psp;
1.1.1.20 root 5752: if(parent_psp == (UINT16)-1) {
5753: for(int i = 0; i < 20; i++) {
5754: if(file_handler[i].valid) {
5755: psp->file_table[i] = i;
5756: } else {
5757: psp->file_table[i] = 0xff;
5758: }
1.1 root 5759: }
1.1.1.20 root 5760: } else {
5761: memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1 root 5762: }
5763: psp->env_seg = env_seg;
5764: psp->stack.w.l = REG16(SP);
1.1.1.3 root 5765: psp->stack.w.h = SREG(SS);
1.1.1.14 root 5766: psp->file_table_size = 20;
5767: psp->file_table_ptr.w.l = 0x18;
5768: psp->file_table_ptr.w.h = psp_seg;
1.1 root 5769: psp->service[0] = 0xcd;
5770: psp->service[1] = 0x21;
5771: psp->service[2] = 0xcb;
5772: return(psp);
5773: }
5774:
1.1.1.20 root 5775: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
5776: {
5777: if(psp_seg && fd < 20) {
5778: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
5779: psp->file_table[fd] = value;
5780: }
5781: }
5782:
5783: int msdos_psp_get_file_table(int fd, int psp_seg)
5784: {
5785: if(psp_seg && fd < 20) {
5786: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
5787: fd = psp->file_table[fd];
5788: }
5789: return fd;
5790: }
5791:
1.1 root 5792: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
5793: {
5794: // load command file
5795: int fd = -1;
5796: int dos_command = 0;
1.1.1.24 root 5797: char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38 root 5798: char pipe_stdin_path[MAX_PATH] = {0};
5799: char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39 root 5800: char pipe_stderr_path[MAX_PATH] = {0};
1.1 root 5801:
5802: int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
5803: int opt_len = mem[opt_ofs];
5804: memset(opt, 0, sizeof(opt));
5805: memcpy(opt, mem + opt_ofs + 1, opt_len);
5806:
1.1.1.14 root 5807: if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
5808: // this is a batch file, run command.com
5809: char tmp[MAX_PATH];
5810: if(opt_len != 0) {
5811: sprintf(tmp, "/C %s %s", cmd, opt);
5812: } else {
5813: sprintf(tmp, "/C %s", cmd);
5814: }
5815: strcpy(opt, tmp);
5816: opt_len = strlen(opt);
5817: mem[opt_ofs] = opt_len;
5818: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
5819: strcpy(command, comspec_path);
5820: strcpy(name_tmp, "COMMAND.COM");
5821: } else {
5822: if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
5823: // redirect C:\COMMAND.COM to comspec_path
5824: strcpy(command, comspec_path);
5825: } else {
5826: strcpy(command, cmd);
5827: }
1.1.1.24 root 5828: if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
5829: return(-1);
5830: }
1.1.1.14 root 5831: memset(name_tmp, 0, sizeof(name_tmp));
5832: strcpy(name_tmp, name);
5833:
5834: // check command.com
1.1.1.38 root 5835: if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
5836: // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14 root 5837: if(opt_len == 0) {
5838: // process_t *current_process = msdos_process_info_get(current_psp);
5839: process_t *current_process = NULL;
5840: for(int i = 0; i < MAX_PROCESS; i++) {
5841: if(process[i].psp == current_psp) {
5842: current_process = &process[i];
5843: break;
5844: }
5845: }
5846: if(current_process != NULL) {
5847: param->cmd_line.dw = current_process->dta.dw;
5848: opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
5849: opt_len = mem[opt_ofs];
5850: memset(opt, 0, sizeof(opt));
5851: memcpy(opt, mem + opt_ofs + 1, opt_len);
5852: }
5853: }
5854: for(int i = 0; i < opt_len; i++) {
5855: if(opt[i] == ' ') {
5856: continue;
5857: }
5858: if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
5859: for(int j = i + 3; j < opt_len; j++) {
5860: if(opt[j] == ' ') {
5861: continue;
5862: }
5863: char *token = my_strtok(opt + j, " ");
5864:
1.1.1.38 root 5865: strcpy(command, token);
5866: char tmp[MAX_PATH];
5867: strcpy(tmp, token + strlen(token) + 1);
1.1.1.39 root 5868: strcpy(opt, "");
5869: for(int i = 0; i < strlen(tmp); i++) {
5870: if(tmp[i] != ' ') {
5871: strcpy(opt, tmp + i);
5872: break;
5873: }
5874: }
5875: strcpy(tmp, opt);
1.1.1.38 root 5876:
5877: if(al == 0x00) {
1.1.1.39 root 5878: #define GET_FILE_PATH() { \
5879: if(token[0] != '>' && token[0] != '<') { \
5880: token++; \
5881: } \
5882: token++; \
5883: while(*token == ' ') { \
5884: token++; \
5885: } \
5886: char *ptr = token; \
5887: while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
5888: ptr++; \
5889: } \
5890: *ptr = '\0'; \
5891: }
5892: if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
5893: GET_FILE_PATH();
1.1.1.38 root 5894: strcpy(pipe_stdin_path, token);
5895: strcpy(opt, tmp);
5896: }
1.1.1.39 root 5897: if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
5898: GET_FILE_PATH();
1.1.1.38 root 5899: strcpy(pipe_stdout_path, token);
5900: strcpy(opt, tmp);
5901: }
1.1.1.39 root 5902: if((token = strstr(opt, "2>")) != NULL) {
5903: GET_FILE_PATH();
5904: strcpy(pipe_stderr_path, token);
5905: strcpy(opt, tmp);
5906: }
5907: #undef GET_FILE_PATH
5908:
5909: if((token = strstr(opt, "0<")) != NULL) {
5910: *token = '\0';
5911: }
5912: if((token = strstr(opt, "1>")) != NULL) {
5913: *token = '\0';
5914: }
5915: if((token = strstr(opt, "2>")) != NULL) {
5916: *token = '\0';
5917: }
1.1.1.38 root 5918: if((token = strstr(opt, "<")) != NULL) {
5919: *token = '\0';
5920: }
5921: if((token = strstr(opt, ">")) != NULL) {
5922: *token = '\0';
5923: }
1.1.1.14 root 5924: }
1.1.1.39 root 5925: for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
5926: opt[i] = '\0';
5927: }
1.1.1.38 root 5928: opt_len = strlen(opt);
5929: mem[opt_ofs] = opt_len;
5930: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
5931: dos_command = 1;
1.1.1.14 root 5932: break;
1.1 root 5933: }
5934: }
1.1.1.14 root 5935: break;
1.1 root 5936: }
5937: }
5938: }
5939:
5940: // load command file
5941: strcpy(path, command);
5942: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
5943: sprintf(path, "%s.COM", command);
5944: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
5945: sprintf(path, "%s.EXE", command);
5946: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14 root 5947: sprintf(path, "%s.BAT", command);
5948: if(_access(path, 0) == 0) {
5949: // this is a batch file, run command.com
5950: char tmp[MAX_PATH];
5951: if(opt_len != 0) {
5952: sprintf(tmp, "/C %s %s", path, opt);
5953: } else {
5954: sprintf(tmp, "/C %s", path);
5955: }
5956: strcpy(opt, tmp);
5957: opt_len = strlen(opt);
5958: mem[opt_ofs] = opt_len;
5959: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
5960: strcpy(path, comspec_path);
5961: strcpy(name_tmp, "COMMAND.COM");
5962: fd = _open(path, _O_RDONLY | _O_BINARY);
5963: } else {
5964: // search path in parent environments
5965: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
5966: char *env = msdos_env_get(parent_psp->env_seg, "PATH");
5967: if(env != NULL) {
5968: char env_path[4096];
5969: strcpy(env_path, env);
5970: char *token = my_strtok(env_path, ";");
5971:
5972: while(token != NULL) {
5973: if(strlen(token) != 0) {
5974: sprintf(path, "%s", msdos_combine_path(token, command));
5975: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
5976: break;
5977: }
5978: sprintf(path, "%s.COM", msdos_combine_path(token, command));
5979: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
5980: break;
5981: }
5982: sprintf(path, "%s.EXE", msdos_combine_path(token, command));
5983: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
5984: break;
5985: }
5986: sprintf(path, "%s.BAT", msdos_combine_path(token, command));
5987: if(_access(path, 0) == 0) {
5988: // this is a batch file, run command.com
5989: char tmp[MAX_PATH];
5990: if(opt_len != 0) {
5991: sprintf(tmp, "/C %s %s", path, opt);
5992: } else {
5993: sprintf(tmp, "/C %s", path);
5994: }
5995: strcpy(opt, tmp);
5996: opt_len = strlen(opt);
5997: mem[opt_ofs] = opt_len;
5998: sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
5999: strcpy(path, comspec_path);
6000: strcpy(name_tmp, "COMMAND.COM");
6001: fd = _open(path, _O_RDONLY | _O_BINARY);
6002: break;
6003: }
1.1.1.8 root 6004: }
1.1.1.14 root 6005: token = my_strtok(NULL, ";");
1.1 root 6006: }
6007: }
6008: }
6009: }
6010: }
6011: }
6012: if(fd == -1) {
1.1.1.38 root 6013: // we can not find command.com in the path, so open comspec_path
6014: if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
6015: strcpy(command, comspec_path);
6016: strcpy(path, command);
6017: fd = _open(path, _O_RDONLY | _O_BINARY);
6018: }
6019: }
6020: if(fd == -1) {
1.1 root 6021: if(dos_command) {
6022: // may be dos command
6023: char tmp[MAX_PATH];
6024: sprintf(tmp, "%s %s", command, opt);
6025: system(tmp);
6026: return(0);
6027: } else {
6028: return(-1);
6029: }
6030: }
6031: _read(fd, file_buffer, sizeof(file_buffer));
6032: _close(fd);
6033:
6034: // copy environment
1.1.1.29 root 6035: int umb_linked, env_seg, psp_seg;
1.1 root 6036:
1.1.1.29 root 6037: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
6038: msdos_mem_unlink_umb();
6039: }
1.1.1.8 root 6040: if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29 root 6041: if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
6042: if(umb_linked != 0) {
6043: msdos_mem_link_umb();
6044: }
6045: return(-1);
6046: }
1.1 root 6047: }
6048: if(param->env_seg == 0) {
6049: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
6050: memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
6051: } else {
6052: memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
6053: }
6054: msdos_env_set_argv(env_seg, msdos_short_full_path(path));
6055:
6056: // check exe header
6057: exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8 root 6058: int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1 root 6059: UINT16 cs, ss, ip, sp;
6060:
6061: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
6062: // memory allocation
6063: int header_size = header->header_size * 16;
6064: int load_size = header->pages * 512 - header_size;
6065: if(header_size + load_size < 512) {
6066: load_size = 512 - header_size;
6067: }
6068: paragraphs = (PSP_SIZE + load_size) >> 4;
6069: if(paragraphs + header->min_alloc > free_paragraphs) {
6070: msdos_mem_free(env_seg);
6071: return(-1);
6072: }
6073: paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
6074: if(paragraphs > free_paragraphs) {
6075: paragraphs = free_paragraphs;
6076: }
1.1.1.8 root 6077: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6078: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6079: if(umb_linked != 0) {
6080: msdos_mem_link_umb();
6081: }
6082: msdos_mem_free(env_seg);
6083: return(-1);
6084: }
1.1 root 6085: }
6086: // relocation
6087: int start_seg = psp_seg + (PSP_SIZE >> 4);
6088: for(int i = 0; i < header->relocations; i++) {
6089: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
6090: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
6091: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
6092: }
6093: memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
6094: // segments
6095: cs = header->init_cs + start_seg;
6096: ss = header->init_ss + start_seg;
6097: ip = header->init_ip;
6098: sp = header->init_sp - 2; // for symdeb
6099: } else {
6100: // memory allocation
6101: paragraphs = free_paragraphs;
1.1.1.8 root 6102: if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29 root 6103: if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
6104: if(umb_linked != 0) {
6105: msdos_mem_link_umb();
6106: }
6107: msdos_mem_free(env_seg);
6108: return(-1);
6109: }
1.1 root 6110: }
6111: int start_seg = psp_seg + (PSP_SIZE >> 4);
6112: memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
6113: // segments
6114: cs = ss = psp_seg;
6115: ip = 0x100;
6116: sp = 0xfffe;
6117: }
1.1.1.29 root 6118: if(umb_linked != 0) {
6119: msdos_mem_link_umb();
6120: }
1.1 root 6121:
6122: // create psp
1.1.1.3 root 6123: *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
6124: *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1 root 6125: psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
6126: memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
6127: memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
6128: memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
6129:
6130: mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
6131: mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
6132: mcb_psp->psp = mcb_env->psp = psp_seg;
6133:
1.1.1.4 root 6134: for(int i = 0; i < 8; i++) {
6135: if(name_tmp[i] == '.') {
6136: mcb_psp->prog_name[i] = '\0';
6137: break;
6138: } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
6139: mcb_psp->prog_name[i] = name_tmp[i];
6140: i++;
6141: mcb_psp->prog_name[i] = name_tmp[i];
6142: } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
6143: mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
6144: } else {
6145: mcb_psp->prog_name[i] = name_tmp[i];
6146: }
6147: }
6148:
1.1 root 6149: // process info
6150: process_t *process = msdos_process_info_create(psp_seg);
6151: strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33 root 6152: #ifdef USE_DEBUGGER
6153: strcpy(process->module_path, path);
6154: #endif
1.1 root 6155: process->dta.w.l = 0x80;
6156: process->dta.w.h = psp_seg;
6157: process->switchar = '/';
6158: process->max_files = 20;
6159: process->parent_int_10h_feh_called = int_10h_feh_called;
6160: process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14 root 6161: process->parent_ds = SREG(DS);
1.1.1.31 root 6162: process->parent_es = SREG(ES);
1.1 root 6163:
6164: current_psp = psp_seg;
1.1.1.23 root 6165: msdos_sda_update(current_psp);
1.1 root 6166:
6167: if(al == 0x00) {
6168: int_10h_feh_called = int_10h_ffh_called = false;
6169:
1.1.1.38 root 6170: // pipe
6171: if(pipe_stdin_path[0] != '\0') {
6172: if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
6173: UINT16 info = msdos_drive_number(pipe_stdin_path);
6174: msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, info, current_psp);
6175: psp->file_table[0] = fd;
6176: msdos_psp_set_file_table(fd, fd, current_psp);
6177: }
6178: }
6179: if(pipe_stdout_path[0] != '\0') {
6180: if(_access(pipe_stdout_path, 0) == 0) {
6181: SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
6182: DeleteFile(pipe_stdout_path);
6183: }
6184: if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
6185: UINT16 info = msdos_drive_number(pipe_stdout_path);
6186: msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, info, current_psp);
6187: psp->file_table[1] = fd;
6188: msdos_psp_set_file_table(fd, fd, current_psp);
6189: }
6190: }
1.1.1.39 root 6191: if(pipe_stderr_path[0] != '\0') {
6192: if(_access(pipe_stderr_path, 0) == 0) {
6193: SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
6194: DeleteFile(pipe_stderr_path);
6195: }
6196: if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
6197: UINT16 info = msdos_drive_number(pipe_stderr_path);
6198: msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 2, info, current_psp);
6199: psp->file_table[2] = fd;
6200: msdos_psp_set_file_table(fd, fd, current_psp);
6201: }
6202: }
1.1.1.38 root 6203:
1.1 root 6204: // registers and segments
6205: REG16(AX) = REG16(BX) = 0x00;
6206: REG16(CX) = 0xff;
6207: REG16(DX) = psp_seg;
6208: REG16(SI) = ip;
6209: REG16(DI) = sp;
6210: REG16(SP) = sp;
1.1.1.3 root 6211: SREG(DS) = SREG(ES) = psp_seg;
6212: SREG(SS) = ss;
6213: i386_load_segment_descriptor(DS);
6214: i386_load_segment_descriptor(ES);
6215: i386_load_segment_descriptor(SS);
1.1 root 6216:
6217: *(UINT16 *)(mem + (ss << 4) + sp) = 0;
6218: i386_jmp_far(cs, ip);
6219: } else if(al == 0x01) {
6220: // copy ss:sp and cs:ip to param block
6221: param->sp = sp;
6222: param->ss = ss;
6223: param->ip = ip;
6224: param->cs = cs;
1.1.1.31 root 6225:
6226: // the AX value to be passed to the child program is put on top of the child's stack
6227: *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1 root 6228: }
6229: return(0);
6230: }
6231:
6232: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
6233: {
6234: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
6235:
6236: *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
6237: *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
6238: *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
6239:
1.1.1.3 root 6240: SREG(SS) = psp->stack.w.h;
6241: i386_load_segment_descriptor(SS);
1.1 root 6242: REG16(SP) = psp->stack.w.l;
6243: i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
6244:
1.1.1.28 root 6245: // process_t *current_process = msdos_process_info_get(psp_seg);
6246: process_t *current_process = NULL;
6247: for(int i = 0; i < MAX_PROCESS; i++) {
6248: if(process[i].psp == psp_seg) {
6249: current_process = &process[i];
6250: break;
6251: }
6252: }
6253: if(current_process == NULL) {
6254: throw(0x1f); // general failure
6255: }
6256: int_10h_feh_called = current_process->parent_int_10h_feh_called;
6257: int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
6258: if(current_process->called_by_int2eh) {
6259: REG16(AX) = ret;
6260: }
6261: SREG(DS) = current_process->parent_ds;
1.1.1.31 root 6262: SREG(ES) = current_process->parent_es;
1.1.1.14 root 6263: i386_load_segment_descriptor(DS);
1.1.1.31 root 6264: i386_load_segment_descriptor(ES);
1.1 root 6265:
6266: if(mem_free) {
1.1.1.8 root 6267: int mcb_seg;
6268: while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
6269: msdos_mem_free(mcb_seg + 1);
6270: }
6271: while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
6272: msdos_mem_free(mcb_seg + 1);
6273: }
1.1 root 6274:
6275: for(int i = 0; i < MAX_FILES; i++) {
6276: if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
6277: _close(i);
1.1.1.20 root 6278: msdos_file_handler_close(i);
6279: msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1 root 6280: }
6281: }
1.1.1.13 root 6282: msdos_dta_info_free(psp_seg);
1.1 root 6283: }
1.1.1.14 root 6284: msdos_stdio_reopen();
1.1 root 6285:
1.1.1.28 root 6286: memset(current_process, 0, sizeof(process_t));
1.1 root 6287:
6288: current_psp = psp->parent_psp;
6289: retval = ret;
1.1.1.23 root 6290: msdos_sda_update(current_psp);
1.1 root 6291: }
6292:
6293: // drive
6294:
1.1.1.42! root 6295: int pcbios_update_drive_param(int drive_num, int force_update);
! 6296:
1.1 root 6297: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
6298: {
1.1.1.41 root 6299: if(!(drive_num >= 0 && drive_num < 26)) {
6300: return(0);
6301: }
1.1.1.42! root 6302: pcbios_update_drive_param(drive_num, force_update);
! 6303:
! 6304: drive_param_t *drive_param = &drive_params[drive_num];
1.1 root 6305: *seg = DPB_TOP >> 4;
6306: *ofs = sizeof(dpb_t) * drive_num;
6307: dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
6308:
6309: memset(dpb, 0, sizeof(dpb_t));
6310:
1.1.1.41 root 6311: dpb->drive_num = drive_num;
6312: dpb->unit_num = drive_num;
1.1.1.42! root 6313:
! 6314: if(drive_param->valid) {
! 6315: DISK_GEOMETRY *geo = &drive_param->geometry;
! 6316:
! 6317: dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
! 6318: dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
! 6319: dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
! 6320: dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
! 6321: switch(geo->MediaType) {
! 6322: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
! 6323: dpb->media_type = 0xff;
! 6324: break;
! 6325: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
! 6326: dpb->media_type = 0xfe;
! 6327: break;
! 6328: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
! 6329: dpb->media_type = 0xfd;
! 6330: break;
! 6331: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
! 6332: dpb->media_type = 0xfc;
! 6333: break;
! 6334: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
! 6335: case F3_1Pt2_512:
! 6336: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
! 6337: case F5_720_512:
! 6338: dpb->media_type = 0xf9;
! 6339: break;
! 6340: case FixedMedia: // hard disk
! 6341: case RemovableMedia:
! 6342: case Unknown:
! 6343: dpb->media_type = 0xf8;
! 6344: break;
! 6345: default:
! 6346: dpb->media_type = 0xf0;
! 6347: break;
! 6348: }
! 6349: }
1.1.1.41 root 6350: dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
6351: dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42! root 6352: dpb->info_sector = 0xffff;
! 6353: dpb->backup_boot_sector = 0xffff;
! 6354: dpb->free_clusters = 0xffff;
! 6355: dpb->free_search_cluster = 0xffffffff;
! 6356:
! 6357: return(drive_param->valid);
1.1 root 6358: }
6359:
6360: // pc bios
6361:
1.1.1.35 root 6362: #ifdef USE_SERVICE_THREAD
6363: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
6364: {
6365: #if defined(HAS_I386)
6366: if(m_SF != 0) {
6367: m_SF = 0;
6368: mem[0xfffd0 + 0x15] = 0x79; // jns -4
6369: } else {
6370: m_SF = 1;
6371: mem[0xfffd0 + 0x15] = 0x78; // js -4
6372: }
6373: #else
6374: if(m_SignVal < 0) {
6375: m_SignVal = 0;
6376: mem[0xfffd0 + 0x15] = 0x79; // jns -4
6377: } else {
6378: m_SignVal = -1;
6379: mem[0xfffd0 + 0x15] = 0x78; // js -4
6380: }
6381: #endif
6382: i386_call_far(0xfffd, 0x0013);
6383: in_service = true;
6384: service_exit = false;
6385: CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
6386: }
6387:
6388: void finish_service_loop()
6389: {
6390: if(in_service && service_exit) {
6391: #if defined(HAS_I386)
6392: if(m_SF != 0) {
6393: m_SF = 0;
6394: } else {
6395: m_SF = 1;
6396: }
6397: #else
6398: if(m_SignVal < 0) {
6399: m_SignVal = 0;
6400: } else {
6401: m_SignVal = -1;
6402: }
6403: #endif
6404: in_service = false;
6405: }
6406: }
6407: #endif
6408:
1.1.1.19 root 6409: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
6410: {
6411: static unsigned __int64 start_msec_since_midnight = 0;
6412: static unsigned __int64 start_msec_since_hostboot = 0;
6413:
6414: if(start_msec_since_midnight == 0) {
6415: SYSTEMTIME time;
6416: GetLocalTime(&time);
6417: start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
6418: start_msec_since_hostboot = cur_msec;
6419: }
6420: unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
6421: unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
6422: return (UINT32)tick;
6423: }
6424:
6425: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
6426: {
6427: UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
6428: UINT32 next_tick = get_ticks_since_midnight(cur_msec);
6429:
6430: if(prev_tick > next_tick) {
6431: mem[0x470] = 1;
6432: }
6433: *(UINT32 *)(mem + 0x46c) = next_tick;
6434: }
6435:
1.1.1.14 root 6436: inline void pcbios_irq0()
6437: {
6438: //++*(UINT32 *)(mem + 0x46c);
1.1.1.19 root 6439: pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14 root 6440: }
6441:
1.1.1.16 root 6442: int pcbios_get_text_vram_address(int page)
1.1 root 6443: {
6444: if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6445: return TEXT_VRAM_TOP;
1.1 root 6446: } else {
1.1.1.14 root 6447: return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1 root 6448: }
6449: }
6450:
1.1.1.16 root 6451: int pcbios_get_shadow_buffer_address(int page)
1.1 root 6452: {
1.1.1.14 root 6453: if(!int_10h_feh_called) {
1.1.1.16 root 6454: return pcbios_get_text_vram_address(page);
1.1.1.14 root 6455: } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 6456: return SHADOW_BUF_TOP;
6457: } else {
1.1.1.14 root 6458: return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8 root 6459: }
6460: }
6461:
1.1.1.16 root 6462: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8 root 6463: {
1.1.1.16 root 6464: return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1 root 6465: }
6466:
1.1.1.16 root 6467: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1 root 6468: {
1.1.1.14 root 6469: // clear the existing screen, not just the new one
6470: int clr_height = max(height, scr_height);
6471:
1.1.1.16 root 6472: if(scr_width != width || scr_height != height) {
6473: change_console_size(width, height);
1.1.1.14 root 6474: }
6475: mem[0x462] = 0;
6476: *(UINT16 *)(mem + 0x44e) = 0;
6477:
1.1.1.16 root 6478: text_vram_top_address = pcbios_get_text_vram_address(0);
6479: text_vram_end_address = text_vram_top_address + width * height * 2;
6480: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
6481: shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1 root 6482:
1.1.1.23 root 6483: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16 root 6484: if(clr_screen) {
1.1.1.14 root 6485: for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
6486: mem[ofs++] = 0x20;
6487: mem[ofs++] = 0x07;
6488: }
6489:
1.1.1.35 root 6490: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6491: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6492: #endif
1.1.1.14 root 6493: for(int y = 0; y < clr_height; y++) {
6494: for(int x = 0; x < scr_width; x++) {
6495: SCR_BUF(y,x).Char.AsciiChar = ' ';
6496: SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1 root 6497: }
6498: }
6499: SMALL_RECT rect;
1.1.1.14 root 6500: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
6501: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6502: vram_length_char = vram_last_length_char = 0;
6503: vram_length_attr = vram_last_length_attr = 0;
1.1.1.35 root 6504: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6505: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 6506: #endif
1.1 root 6507: }
1.1.1.14 root 6508: COORD co;
6509: co.X = 0;
6510: co.Y = scr_top;
6511: SetConsoleCursorPosition(hStdout, co);
6512: cursor_moved = true;
6513: SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1 root 6514: }
6515:
1.1.1.36 root 6516: void pcbios_update_cursor_position()
6517: {
6518: CONSOLE_SCREEN_BUFFER_INFO csbi;
6519: GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
6520: if(!restore_console_on_exit) {
6521: scr_top = csbi.srWindow.Top;
6522: }
6523: mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
6524: mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
6525: }
6526:
1.1.1.16 root 6527: inline void pcbios_int_10h_00h()
6528: {
6529: switch(REG8(AL) & 0x7f) {
6530: case 0x70: // v-text mode
6531: case 0x71: // extended cga v-text mode
6532: pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
6533: break;
6534: default:
6535: pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
6536: break;
6537: }
6538: if(REG8(AL) & 0x80) {
6539: mem[0x487] |= 0x80;
6540: } else {
6541: mem[0x487] &= ~0x80;
6542: }
6543: mem[0x449] = REG8(AL) & 0x7f;
6544: }
6545:
1.1 root 6546: inline void pcbios_int_10h_01h()
6547: {
1.1.1.13 root 6548: mem[0x460] = REG8(CL);
6549: mem[0x461] = REG8(CH);
1.1.1.14 root 6550:
1.1.1.23 root 6551: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6552: CONSOLE_CURSOR_INFO ci;
6553: GetConsoleCursorInfo(hStdout, &ci);
6554: // ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
6555: // if(ci.bVisible) {
6556: int lines = max(8, REG8(CL) + 1);
6557: ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
6558: // }
6559: SetConsoleCursorInfo(hStdout, &ci);
1.1 root 6560: }
6561:
6562: inline void pcbios_int_10h_02h()
6563: {
1.1.1.14 root 6564: // continuously setting the cursor effectively stops it blinking
6565: if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1 root 6566: COORD co;
6567: co.X = REG8(DL);
1.1.1.14 root 6568: co.Y = REG8(DH) + scr_top;
6569:
6570: // some programs hide the cursor by moving it off screen
6571: static bool hidden = false;
1.1.1.23 root 6572: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6573: CONSOLE_CURSOR_INFO ci;
6574: GetConsoleCursorInfo(hStdout, &ci);
6575:
6576: if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
6577: if(ci.bVisible) {
6578: ci.bVisible = FALSE;
6579: // SetConsoleCursorInfo(hStdout, &ci);
6580: hidden = true;
6581: }
6582: } else if(hidden) {
6583: if(!ci.bVisible) {
6584: ci.bVisible = TRUE;
6585: // SetConsoleCursorInfo(hStdout, &ci);
6586: }
6587: hidden = false;
6588: }
1.1 root 6589: }
1.1.1.14 root 6590: mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
6591: mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1 root 6592: }
6593:
6594: inline void pcbios_int_10h_03h()
6595: {
1.1.1.14 root 6596: REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6597: REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 6598: REG8(CL) = mem[0x460];
6599: REG8(CH) = mem[0x461];
6600: }
6601:
6602: inline void pcbios_int_10h_05h()
6603: {
1.1.1.14 root 6604: if(REG8(AL) >= vram_pages) {
6605: return;
6606: }
6607: if(mem[0x462] != REG8(AL)) {
6608: vram_flush();
6609:
1.1.1.23 root 6610: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6611: SMALL_RECT rect;
1.1.1.14 root 6612: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6613: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6614:
1.1.1.16 root 6615: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14 root 6616: for(int x = 0; x < scr_width; x++) {
6617: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6618: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6619: }
6620: }
1.1.1.16 root 6621: for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14 root 6622: for(int x = 0; x < scr_width; x++) {
6623: SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
6624: SCR_BUF(y,x).Attributes = mem[ofs++];
1.1 root 6625: }
6626: }
1.1.1.14 root 6627: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6628:
6629: COORD co;
1.1.1.14 root 6630: co.X = mem[0x450 + REG8(AL) * 2];
6631: co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
6632: if(co.Y < scr_top + scr_height) {
6633: SetConsoleCursorPosition(hStdout, co);
6634: }
1.1 root 6635: }
1.1.1.14 root 6636: mem[0x462] = REG8(AL);
6637: *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
6638: int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16 root 6639: text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14 root 6640: text_vram_end_address = text_vram_top_address + regen;
1.1.1.16 root 6641: shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14 root 6642: shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1 root 6643: }
6644:
6645: inline void pcbios_int_10h_06h()
6646: {
1.1.1.14 root 6647: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
6648: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
6649: return;
6650: }
6651: vram_flush();
6652:
1.1.1.23 root 6653: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6654: SMALL_RECT rect;
1.1.1.14 root 6655: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6656: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6657:
6658: int right = min(REG8(DL), scr_width - 1);
6659: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 6660:
6661: if(REG8(AL) == 0) {
1.1.1.14 root 6662: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 6663: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6664: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
6665: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6666: }
6667: }
6668: } else {
1.1.1.14 root 6669: for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16 root 6670: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6671: if(y2 <= bottom) {
6672: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 6673: } else {
1.1.1.14 root 6674: SCR_BUF(y,x).Char.AsciiChar = ' ';
6675: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6676: }
1.1.1.14 root 6677: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6678: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6679: }
6680: }
6681: }
1.1.1.14 root 6682: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6683: }
6684:
6685: inline void pcbios_int_10h_07h()
6686: {
1.1.1.14 root 6687: if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
6688: REG8(CL) >= scr_width || REG8(CL) > REG8(DL)) {
6689: return;
6690: }
6691: vram_flush();
6692:
1.1.1.23 root 6693: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6694: SMALL_RECT rect;
1.1.1.14 root 6695: SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
6696: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
6697:
6698: int right = min(REG8(DL), scr_width - 1);
6699: int bottom = min(REG8(DH), scr_height - 1);
1.1 root 6700:
6701: if(REG8(AL) == 0) {
1.1.1.14 root 6702: for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16 root 6703: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6704: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
6705: mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6706: }
6707: }
6708: } else {
1.1.1.14 root 6709: for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16 root 6710: for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14 root 6711: if(y2 >= REG8(CH)) {
6712: SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1 root 6713: } else {
1.1.1.14 root 6714: SCR_BUF(y,x).Char.AsciiChar = ' ';
6715: SCR_BUF(y,x).Attributes = REG8(BH);
1.1 root 6716: }
1.1.1.14 root 6717: mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
6718: mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1 root 6719: }
6720: }
6721: }
1.1.1.14 root 6722: WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1 root 6723: }
6724:
6725: inline void pcbios_int_10h_08h()
6726: {
6727: COORD co;
6728: DWORD num;
6729:
1.1.1.14 root 6730: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6731: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1 root 6732:
6733: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 6734: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6735: co.Y += scr_top;
6736: vram_flush();
1.1 root 6737: ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
6738: ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
6739: REG8(AL) = scr_char[0];
6740: REG8(AH) = scr_attr[0];
6741: } else {
1.1.1.16 root 6742: REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1 root 6743: }
6744: }
6745:
6746: inline void pcbios_int_10h_09h()
6747: {
6748: COORD co;
6749:
1.1.1.14 root 6750: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6751: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
6752:
1.1.1.16 root 6753: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
6754: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 6755:
6756: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 6757: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6758: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6759: #endif
1.1.1.16 root 6760: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 6761: while(dest < end) {
6762: write_text_vram_char(dest - vram, REG8(AL));
6763: mem[dest++] = REG8(AL);
6764: write_text_vram_attr(dest - vram, REG8(BL));
6765: mem[dest++] = REG8(BL);
1.1 root 6766: }
1.1.1.35 root 6767: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6768: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 6769: #endif
1.1 root 6770: } else {
1.1.1.14 root 6771: while(dest < end) {
1.1 root 6772: mem[dest++] = REG8(AL);
6773: mem[dest++] = REG8(BL);
6774: }
6775: }
6776: }
6777:
6778: inline void pcbios_int_10h_0ah()
6779: {
6780: COORD co;
6781:
1.1.1.14 root 6782: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6783: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
6784:
1.1.1.16 root 6785: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
6786: int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1 root 6787:
6788: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 6789: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6790: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6791: #endif
1.1.1.16 root 6792: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 6793: while(dest < end) {
6794: write_text_vram_char(dest - vram, REG8(AL));
6795: mem[dest++] = REG8(AL);
6796: dest++;
1.1 root 6797: }
1.1.1.35 root 6798: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6799: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 6800: #endif
1.1 root 6801: } else {
1.1.1.14 root 6802: while(dest < end) {
1.1 root 6803: mem[dest++] = REG8(AL);
6804: dest++;
6805: }
6806: }
6807: }
6808:
1.1.1.40 root 6809: HDC get_console_window_device_context()
6810: {
6811: static HWND hwndFound = 0;
6812:
6813: if(hwndFound == 0) {
6814: // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
6815: char pszNewWindowTitle[1024];
6816: char pszOldWindowTitle[1024];
6817:
6818: GetConsoleTitle(pszOldWindowTitle, 1024);
6819: wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
6820: SetConsoleTitle(pszNewWindowTitle);
6821: Sleep(100);
6822: hwndFound = FindWindow(NULL, pszNewWindowTitle);
6823: SetConsoleTitle(pszOldWindowTitle);
6824: }
6825: return GetDC(hwndFound);
6826: }
6827:
6828: inline void pcbios_int_10h_0ch()
6829: {
6830: HDC hdc = get_console_window_device_context();
6831:
6832: if(hdc != NULL) {
6833: BYTE r = (REG8(AL) & 2) ? 255 : 0;
6834: BYTE g = (REG8(AL) & 4) ? 255 : 0;
6835: BYTE b = (REG8(AL) & 1) ? 255 : 0;
6836:
6837: if(REG8(AL) & 0x80) {
6838: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
6839: if(color != CLR_INVALID) {
6840: r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
6841: g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
6842: b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
6843: }
6844: }
6845: SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
6846: }
6847: }
6848:
6849: inline void pcbios_int_10h_0dh()
6850: {
6851: HDC hdc = get_console_window_device_context();
6852: BYTE r = 0;
6853: BYTE g = 0;
6854: BYTE b = 0;
6855:
6856: if(hdc != NULL) {
6857: COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
6858: if(color != CLR_INVALID) {
6859: r = ((DWORD)color & 0x0000ff) ? 255 : 0;
6860: g = ((DWORD)color & 0x00ff00) ? 255 : 0;
6861: b = ((DWORD)color & 0xff0000) ? 255 : 0;
6862: }
6863: }
6864: REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
6865: }
6866:
1.1 root 6867: inline void pcbios_int_10h_0eh()
6868: {
1.1.1.14 root 6869: DWORD num;
6870: COORD co;
6871:
6872: co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
6873: co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
6874:
6875: if(REG8(AL) == 7) {
6876: //MessageBeep(-1);
6877: } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
6878: if(REG8(AL) == 10) {
6879: vram_flush();
6880: }
1.1.1.23 root 6881: WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), ®8(AL), 1, &num, NULL);
1.1.1.14 root 6882: cursor_moved = true;
6883: } else {
1.1.1.16 root 6884: int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14 root 6885: if(mem[0x462] == REG8(BH)) {
1.1.1.35 root 6886: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6887: EnterCriticalSection(&vram_crit_sect);
1.1.1.35 root 6888: #endif
1.1.1.16 root 6889: int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14 root 6890: write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35 root 6891: #ifdef USE_VRAM_THREAD
1.1.1.14 root 6892: LeaveCriticalSection(&vram_crit_sect);
1.1.1.35 root 6893: #endif
1.1.1.14 root 6894:
1.1.1.23 root 6895: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 6896: if(++co.X == scr_width) {
6897: co.X = 0;
6898: if(++co.Y == scr_height) {
6899: vram_flush();
6900: WriteConsole(hStdout, "\n", 1, &num, NULL);
6901: cursor_moved = true;
6902: }
6903: }
6904: if(!cursor_moved) {
6905: co.Y += scr_top;
6906: SetConsoleCursorPosition(hStdout, co);
6907: cursor_moved = true;
6908: }
6909: }
6910: mem[dest] = REG8(AL);
6911: }
1.1 root 6912: }
6913:
6914: inline void pcbios_int_10h_0fh()
6915: {
6916: REG8(AL) = mem[0x449];
6917: REG8(AH) = mem[0x44a];
6918: REG8(BH) = mem[0x462];
6919: }
6920:
1.1.1.14 root 6921: inline void pcbios_int_10h_11h()
6922: {
6923: switch(REG8(AL)) {
1.1.1.16 root 6924: case 0x01:
1.1.1.14 root 6925: case 0x11:
1.1.1.16 root 6926: pcbios_set_console_size(80, 28, true);
1.1.1.14 root 6927: break;
1.1.1.16 root 6928: case 0x02:
1.1.1.14 root 6929: case 0x12:
1.1.1.16 root 6930: pcbios_set_console_size(80, 50, true);
1.1.1.14 root 6931: break;
1.1.1.16 root 6932: case 0x04:
1.1.1.14 root 6933: case 0x14:
1.1.1.16 root 6934: pcbios_set_console_size(80, 25, true);
6935: break;
6936: case 0x18:
6937: pcbios_set_console_size(80, 50, true);
1.1.1.14 root 6938: break;
6939: case 0x30:
6940: SREG(ES) = 0;
6941: i386_load_segment_descriptor(ES);
6942: REG16(BP) = 0;
6943: REG16(CX) = mem[0x485];
6944: REG8(DL) = mem[0x484];
6945: break;
6946: }
6947: }
6948:
6949: inline void pcbios_int_10h_12h()
6950: {
1.1.1.16 root 6951: switch(REG8(BL)) {
6952: case 0x10:
1.1.1.14 root 6953: REG16(BX) = 0x0003;
6954: REG16(CX) = 0x0009;
1.1.1.16 root 6955: break;
1.1.1.14 root 6956: }
6957: }
6958:
1.1 root 6959: inline void pcbios_int_10h_13h()
6960: {
1.1.1.3 root 6961: int ofs = SREG_BASE(ES) + REG16(BP);
1.1 root 6962: COORD co;
6963: DWORD num;
6964:
6965: co.X = REG8(DL);
1.1.1.14 root 6966: co.Y = REG8(DH) + scr_top;
6967:
6968: vram_flush();
1.1 root 6969:
6970: switch(REG8(AL)) {
6971: case 0x00:
6972: case 0x01:
6973: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 6974: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 6975: CONSOLE_SCREEN_BUFFER_INFO csbi;
6976: GetConsoleScreenBufferInfo(hStdout, &csbi);
6977: SetConsoleCursorPosition(hStdout, co);
6978:
6979: if(csbi.wAttributes != REG8(BL)) {
6980: SetConsoleTextAttribute(hStdout, REG8(BL));
6981: }
1.1.1.14 root 6982: WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
6983:
1.1 root 6984: if(csbi.wAttributes != REG8(BL)) {
6985: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
6986: }
6987: if(REG8(AL) == 0x00) {
1.1.1.15 root 6988: if(!restore_console_on_exit) {
6989: GetConsoleScreenBufferInfo(hStdout, &csbi);
6990: scr_top = csbi.srWindow.Top;
6991: }
1.1.1.14 root 6992: co.X = mem[0x450 + REG8(BH) * 2];
6993: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 6994: SetConsoleCursorPosition(hStdout, co);
6995: } else {
6996: cursor_moved = true;
6997: }
6998: } else {
1.1.1.3 root 6999: m_CF = 1;
1.1 root 7000: }
7001: break;
7002: case 0x02:
7003: case 0x03:
7004: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7005: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7006: CONSOLE_SCREEN_BUFFER_INFO csbi;
7007: GetConsoleScreenBufferInfo(hStdout, &csbi);
7008: SetConsoleCursorPosition(hStdout, co);
7009:
7010: WORD wAttributes = csbi.wAttributes;
7011: for(int i = 0; i < REG16(CX); i++, ofs += 2) {
7012: if(wAttributes != mem[ofs + 1]) {
7013: SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
7014: wAttributes = mem[ofs + 1];
7015: }
1.1.1.14 root 7016: WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1 root 7017: }
7018: if(csbi.wAttributes != wAttributes) {
7019: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
7020: }
7021: if(REG8(AL) == 0x02) {
1.1.1.14 root 7022: co.X = mem[0x450 + REG8(BH) * 2];
7023: co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1 root 7024: SetConsoleCursorPosition(hStdout, co);
7025: } else {
7026: cursor_moved = true;
7027: }
7028: } else {
1.1.1.3 root 7029: m_CF = 1;
1.1 root 7030: }
7031: break;
7032: case 0x10:
7033: case 0x11:
7034: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7035: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7036: ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
7037: ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
7038: for(int i = 0; i < num; i++) {
7039: mem[ofs++] = scr_char[i];
7040: mem[ofs++] = scr_attr[i];
7041: if(REG8(AL) == 0x11) {
7042: mem[ofs++] = 0;
7043: mem[ofs++] = 0;
7044: }
7045: }
7046: } else {
1.1.1.16 root 7047: 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 7048: mem[ofs++] = mem[src++];
7049: mem[ofs++] = mem[src++];
7050: if(REG8(AL) == 0x11) {
7051: mem[ofs++] = 0;
7052: mem[ofs++] = 0;
7053: }
1.1.1.14 root 7054: if(++co.X == scr_width) {
7055: if(++co.Y == scr_height) {
1.1 root 7056: break;
7057: }
7058: co.X = 0;
7059: }
7060: }
7061: }
7062: break;
7063: case 0x20:
7064: case 0x21:
7065: if(mem[0x462] == REG8(BH)) {
1.1.1.23 root 7066: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14 root 7067: int len = min(REG16(CX), scr_width * scr_height);
7068: for(int i = 0; i < len; i++) {
1.1 root 7069: scr_char[i] = mem[ofs++];
7070: scr_attr[i] = mem[ofs++];
7071: if(REG8(AL) == 0x21) {
7072: ofs += 2;
7073: }
7074: }
1.1.1.14 root 7075: WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
7076: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7077: } else {
1.1.1.16 root 7078: 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 7079: mem[dest++] = mem[ofs++];
7080: mem[dest++] = mem[ofs++];
7081: if(REG8(AL) == 0x21) {
7082: ofs += 2;
7083: }
1.1.1.14 root 7084: if(++co.X == scr_width) {
7085: if(++co.Y == scr_height) {
1.1 root 7086: break;
7087: }
7088: co.X = 0;
7089: }
7090: }
7091: }
7092: break;
7093: default:
1.1.1.22 root 7094: 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 7095: m_CF = 1;
1.1 root 7096: break;
7097: }
7098: }
7099:
1.1.1.30 root 7100: inline void pcbios_int_10h_18h()
7101: {
7102: switch(REG8(AL)) {
7103: case 0x00:
7104: case 0x01:
7105: // REG8(AL) = 0x86;
7106: REG8(AL) = 0x00;
7107: break;
7108: default:
7109: 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));
7110: m_CF = 1;
7111: break;
7112: }
7113: }
7114:
1.1.1.14 root 7115: inline void pcbios_int_10h_1ah()
7116: {
7117: switch(REG8(AL)) {
7118: case 0x00:
7119: REG8(AL) = 0x1a;
7120: REG8(BL) = 0x08;
7121: REG8(BH) = 0x00;
7122: break;
7123: default:
1.1.1.22 root 7124: 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 7125: m_CF = 1;
7126: break;
7127: }
7128: }
7129:
1.1 root 7130: inline void pcbios_int_10h_1dh()
7131: {
7132: switch(REG8(AL)) {
7133: case 0x01:
7134: break;
7135: case 0x02:
7136: REG16(BX) = 0;
7137: break;
7138: default:
1.1.1.22 root 7139: 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));
7140: m_CF = 1;
7141: break;
7142: }
7143: }
7144:
7145: inline void pcbios_int_10h_4fh()
7146: {
7147: switch(REG8(AL)) {
7148: case 0x00:
7149: REG8(AH) = 0x02; // not supported
7150: break;
7151: case 0x01:
7152: case 0x02:
7153: case 0x03:
7154: case 0x04:
7155: case 0x05:
7156: case 0x06:
7157: case 0x07:
7158: case 0x08:
7159: case 0x09:
7160: case 0x0a:
7161: case 0x0b:
7162: case 0x0c:
7163: REG8(AH) = 0x01; // failed
7164: break;
7165: default:
7166: 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 7167: m_CF = 1;
1.1 root 7168: break;
7169: }
7170: }
7171:
7172: inline void pcbios_int_10h_82h()
7173: {
7174: static UINT8 mode = 0;
7175:
7176: switch(REG8(AL)) {
1.1.1.22 root 7177: case 0x00:
1.1 root 7178: if(REG8(BL) != 0xff) {
7179: mode = REG8(BL);
7180: }
7181: REG8(AL) = mode;
7182: break;
7183: default:
1.1.1.22 root 7184: 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 7185: m_CF = 1;
1.1 root 7186: break;
7187: }
7188: }
7189:
1.1.1.22 root 7190: inline void pcbios_int_10h_83h()
7191: {
7192: static UINT8 mode = 0;
7193:
7194: switch(REG8(AL)) {
7195: case 0x00:
7196: REG16(AX) = 0; // offset???
7197: SREG(ES) = (SHADOW_BUF_TOP >> 4);
7198: i386_load_segment_descriptor(ES);
7199: REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
7200: break;
7201: default:
7202: 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));
7203: m_CF = 1;
7204: break;
7205: }
7206: }
7207:
7208: inline void pcbios_int_10h_90h()
7209: {
7210: REG8(AL) = mem[0x449];
7211: }
7212:
7213: inline void pcbios_int_10h_91h()
7214: {
7215: REG8(AL) = 0x04; // VGA
7216: }
7217:
7218: inline void pcbios_int_10h_efh()
7219: {
7220: REG16(DX) = 0xffff;
7221: }
7222:
1.1 root 7223: inline void pcbios_int_10h_feh()
7224: {
7225: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8 root 7226: SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3 root 7227: i386_load_segment_descriptor(ES);
1.1.1.8 root 7228: REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1 root 7229: }
7230: int_10h_feh_called = true;
7231: }
7232:
7233: inline void pcbios_int_10h_ffh()
7234: {
7235: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23 root 7236: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 7237: COORD co;
7238: DWORD num;
7239:
1.1.1.14 root 7240: vram_flush();
7241:
7242: co.X = (REG16(DI) >> 1) % scr_width;
7243: co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16 root 7244: int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
7245: int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14 root 7246: int len;
7247: for(len = 0; ofs < end; len++) {
7248: scr_char[len] = mem[ofs++];
7249: scr_attr[len] = mem[ofs++];
7250: }
7251: co.Y += scr_top;
7252: WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
7253: WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1 root 7254: }
7255: int_10h_ffh_called = true;
7256: }
7257:
1.1.1.42! root 7258: int pcbios_update_drive_param(int drive_num, int force_update)
! 7259: {
! 7260: if(drive_num >= 0 && drive_num < 26) {
! 7261: drive_param_t *drive_param = &drive_params[drive_num];
! 7262:
! 7263: if(force_update || !drive_param->initialized) {
! 7264: drive_param->valid = 0;
! 7265: char dev[64];
! 7266: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
! 7267:
! 7268: HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
! 7269: if(hFile != INVALID_HANDLE_VALUE) {
! 7270: DWORD dwSize;
! 7271: if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
! 7272: drive_param->valid = 1;
! 7273: }
! 7274: CloseHandle(hFile);
! 7275: }
! 7276: drive_param->initialized = 1;
! 7277: }
! 7278: return(drive_param->valid);
! 7279: }
! 7280: return(0);
! 7281: }
! 7282:
! 7283: inline void pcbios_int_13h_00h()
! 7284: {
! 7285: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7286:
! 7287: if(pcbios_update_drive_param(drive_num, 1)) {
! 7288: REG8(AH) = 0x00; // successful completion
! 7289: } else {
! 7290: if(REG8(DL) & 0x80) {
! 7291: REG8(AH) = 0x05; // reset failed (hard disk)
! 7292: } else {
! 7293: REG8(AH) = 0x80; // timeout (not ready)
! 7294: }
! 7295: m_CF = 1;
! 7296: }
! 7297: }
! 7298:
! 7299: inline void pcbios_int_13h_02h()
! 7300: {
! 7301: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7302:
! 7303: if(REG8(AL) == 0) {
! 7304: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
! 7305: m_CF = 1;
! 7306: } else if(!pcbios_update_drive_param(drive_num, 0)) {
! 7307: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7308: m_CF = 1;
! 7309: } else {
! 7310: drive_param_t *drive_param = &drive_params[drive_num];
! 7311: DISK_GEOMETRY *geo = &drive_param->geometry;
! 7312: char dev[64];
! 7313: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
! 7314:
! 7315: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
! 7316: if(hFile == INVALID_HANDLE_VALUE) {
! 7317: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7318: m_CF = 1;
! 7319: } else {
! 7320: UINT32 sector_num = REG8(AL);
! 7321: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
! 7322: UINT32 head = REG8(DH);
! 7323: UINT32 sector = REG8(CL) & 0x3f;
! 7324: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
! 7325: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
! 7326: DWORD dwSize;
! 7327:
! 7328: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
! 7329: // REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7330: // m_CF = 1;
! 7331: // } else
! 7332: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
! 7333: REG8(AH) = 0x04; // sector not found/read error
! 7334: m_CF = 1;
! 7335: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
! 7336: REG8(AH) = 0x04; // sector not found/read error
! 7337: m_CF = 1;
! 7338: } else {
! 7339: REG8(AH) = 0x00; // successful completion
! 7340: }
! 7341: CloseHandle(hFile);
! 7342: }
! 7343: }
! 7344: }
! 7345:
! 7346: inline void pcbios_int_13h_03h()
! 7347: {
! 7348: // this operation may cause serious damage for drives, so support only floppy disk...
! 7349: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7350:
! 7351: if(REG8(AL) == 0) {
! 7352: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
! 7353: m_CF = 1;
! 7354: } else if(!pcbios_update_drive_param(drive_num, 0)) {
! 7355: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7356: m_CF = 1;
! 7357: } else if(!drive_params[drive_num].is_fdd()) {
! 7358: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7359: m_CF = 1;
! 7360: } else {
! 7361: drive_param_t *drive_param = &drive_params[drive_num];
! 7362: DISK_GEOMETRY *geo = &drive_param->geometry;
! 7363: char dev[64];
! 7364: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
! 7365:
! 7366: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
! 7367: if(hFile == INVALID_HANDLE_VALUE) {
! 7368: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7369: m_CF = 1;
! 7370: } else {
! 7371: UINT32 sector_num = REG8(AL);
! 7372: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
! 7373: UINT32 head = REG8(DH);
! 7374: UINT32 sector = REG8(CL) & 0x3f;
! 7375: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
! 7376: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
! 7377: DWORD dwSize;
! 7378:
! 7379: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
! 7380: // REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7381: // m_CF = 1;
! 7382: // } else
! 7383: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
! 7384: REG8(AH) = 0x04; // sector not found/read error
! 7385: m_CF = 1;
! 7386: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
! 7387: REG8(AH) = 0x04; // sector not found/read error
! 7388: m_CF = 1;
! 7389: } else {
! 7390: REG8(AH) = 0x00; // successful completion
! 7391: }
! 7392: CloseHandle(hFile);
! 7393: }
! 7394: }
! 7395: }
! 7396:
! 7397: inline void pcbios_int_13h_04h()
! 7398: {
! 7399: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7400:
! 7401: if(REG8(AL) == 0) {
! 7402: REG8(AH) = 0x01; // invalid function in AH or invalid parameter
! 7403: m_CF = 1;
! 7404: } else if(!pcbios_update_drive_param(drive_num, 0)) {
! 7405: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7406: m_CF = 1;
! 7407: } else {
! 7408: drive_param_t *drive_param = &drive_params[drive_num];
! 7409: DISK_GEOMETRY *geo = &drive_param->geometry;
! 7410: char dev[64];
! 7411: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
! 7412:
! 7413: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
! 7414: if(hFile == INVALID_HANDLE_VALUE) {
! 7415: REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7416: m_CF = 1;
! 7417: } else {
! 7418: UINT32 sector_num = REG8(AL);
! 7419: UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
! 7420: UINT32 head = REG8(DH);
! 7421: UINT32 sector = REG8(CL) & 0x3f;
! 7422: UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
! 7423: UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
! 7424: DWORD dwSize;
! 7425: UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
! 7426:
! 7427: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
! 7428: // REG8(AH) = 0xff; // sense operation failed (hard disk)
! 7429: // m_CF = 1;
! 7430: // } else
! 7431: if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
! 7432: REG8(AH) = 0x04; // sector not found/read error
! 7433: m_CF = 1;
! 7434: } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
! 7435: REG8(AH) = 0x04; // sector not found/read error
! 7436: m_CF = 1;
! 7437: } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
! 7438: REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
! 7439: m_CF = 1;
! 7440: } else {
! 7441: REG8(AH) = 0x00; // successful completion
! 7442: }
! 7443: free(tmp_buffer);
! 7444: CloseHandle(hFile);
! 7445: }
! 7446: }
! 7447: }
! 7448:
! 7449: inline void pcbios_int_13h_08h()
! 7450: {
! 7451: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7452:
! 7453: if(pcbios_update_drive_param(drive_num, 1)) {
! 7454: drive_param_t *drive_param = &drive_params[drive_num];
! 7455: DISK_GEOMETRY *geo = &drive_param->geometry;
! 7456:
! 7457: REG16(AX) = 0x0000;
! 7458: switch(geo->MediaType) {
! 7459: case F5_360_512:
! 7460: case F5_320_512:
! 7461: case F5_320_1024:
! 7462: case F5_180_512:
! 7463: case F5_160_512:
! 7464: REG8(BL) = 0x01; // 320K/360K disk
! 7465: break;
! 7466: case F5_1Pt2_512:
! 7467: case F3_1Pt2_512:
! 7468: case F3_1Pt23_1024:
! 7469: case F5_1Pt23_1024:
! 7470: REG8(BL) = 0x02; // 1.2M disk
! 7471: break;
! 7472: case F3_720_512:
! 7473: case F3_640_512:
! 7474: case F5_640_512:
! 7475: case F5_720_512:
! 7476: REG8(BL) = 0x03; // 720K disk
! 7477: break;
! 7478: case F3_1Pt44_512:
! 7479: REG8(BL) = 0x04; // 1.44M disk
! 7480: break;
! 7481: case F3_2Pt88_512:
! 7482: REG8(BL) = 0x06; // 2.88M disk
! 7483: break;
! 7484: case RemovableMedia:
! 7485: REG8(BL) = 0x10; // ATAPI Removable Media Device
! 7486: break;
! 7487: default:
! 7488: REG8(BL) = 0x00; // unknown
! 7489: break;
! 7490: }
! 7491: if(REG8(DL) & 0x80) {
! 7492: switch(GetLogicalDrives() & 0x0c) {
! 7493: case 0x00: REG8(DL) = 0x00; break;
! 7494: case 0x04:
! 7495: case 0x08: REG8(DL) = 0x01; break;
! 7496: case 0x0c: REG8(DL) = 0x02; break;
! 7497: }
! 7498: } else {
! 7499: switch(GetLogicalDrives() & 0x03) {
! 7500: case 0x00: REG8(DL) = 0x00; break;
! 7501: case 0x01:
! 7502: case 0x02: REG8(DL) = 0x01; break;
! 7503: case 0x03: REG8(DL) = 0x02; break;
! 7504: }
! 7505: }
! 7506: REG8(DH) = drive_param->head_num();
! 7507: int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
! 7508: int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
! 7509: REG8(CH) = cyl & 0xff;
! 7510: REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
! 7511: } else {
! 7512: REG8(AH) = 0x07;
! 7513: m_CF = 1;
! 7514: }
! 7515: }
! 7516:
! 7517: inline void pcbios_int_13h_10h()
! 7518: {
! 7519: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7520:
! 7521: if(pcbios_update_drive_param(drive_num, 1)) {
! 7522: REG8(AH) = 0x00; // successful completion
! 7523: } else {
! 7524: if(REG8(DL) & 0x80) {
! 7525: REG8(AH) = 0xaa; // drive not ready (hard disk)
! 7526: } else {
! 7527: REG8(AH) = 0x80; // timeout (not ready)
! 7528: }
! 7529: m_CF = 1;
! 7530: }
! 7531: }
! 7532:
! 7533: inline void pcbios_int_13h_15h()
! 7534: {
! 7535: int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
! 7536:
! 7537: if(pcbios_update_drive_param(drive_num, 1)) {
! 7538: if(REG8(DL) & 0x80) {
! 7539: REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
! 7540: } else {
! 7541: REG8(AH) = 0x03; // hard disk
! 7542: }
! 7543: } else {
! 7544: REG8(AH) = 0x00; // no such drive
! 7545: }
! 7546: }
! 7547:
1.1.1.25 root 7548: inline void pcbios_int_14h_00h()
7549: {
1.1.1.29 root 7550: if(REG16(DX) < 4) {
1.1.1.25 root 7551: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
7552: UINT8 selector = sio_read(REG16(DX), 3);
7553: selector &= ~0x3f;
7554: selector |= REG8(AL) & 0x1f;
7555: UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
7556: sio_write(REG16(DX), 3, selector | 0x80);
7557: sio_write(REG16(DX), 0, divisor & 0xff);
7558: sio_write(REG16(DX), 1, divisor >> 8);
7559: sio_write(REG16(DX), 3, selector);
7560: REG8(AH) = sio_read(REG16(DX), 5);
7561: REG8(AL) = sio_read(REG16(DX), 6);
7562: } else {
7563: REG8(AH) = 0x80;
7564: }
7565: }
7566:
7567: inline void pcbios_int_14h_01h()
7568: {
1.1.1.29 root 7569: if(REG16(DX) < 4) {
1.1.1.25 root 7570: UINT8 selector = sio_read(REG16(DX), 3);
7571: sio_write(REG16(DX), 3, selector & ~0x80);
7572: sio_write(REG16(DX), 0, REG8(AL));
7573: sio_write(REG16(DX), 3, selector);
7574: REG8(AH) = sio_read(REG16(DX), 5);
7575: } else {
7576: REG8(AH) = 0x80;
7577: }
7578: }
7579:
7580: inline void pcbios_int_14h_02h()
7581: {
1.1.1.29 root 7582: if(REG16(DX) < 4) {
1.1.1.25 root 7583: UINT8 selector = sio_read(REG16(DX), 3);
7584: sio_write(REG16(DX), 3, selector & ~0x80);
7585: REG8(AL) = sio_read(REG16(DX), 0);
7586: sio_write(REG16(DX), 3, selector);
7587: REG8(AH) = sio_read(REG16(DX), 5);
7588: } else {
7589: REG8(AH) = 0x80;
7590: }
7591: }
7592:
7593: inline void pcbios_int_14h_03h()
7594: {
1.1.1.29 root 7595: if(REG16(DX) < 4) {
1.1.1.25 root 7596: REG8(AH) = sio_read(REG16(DX), 5);
7597: REG8(AL) = sio_read(REG16(DX), 6);
7598: } else {
7599: REG8(AH) = 0x80;
7600: }
7601: }
7602:
7603: inline void pcbios_int_14h_04h()
7604: {
1.1.1.29 root 7605: if(REG16(DX) < 4) {
1.1.1.25 root 7606: UINT8 selector = sio_read(REG16(DX), 3);
7607: if(REG8(CH) <= 0x03) {
7608: selector = (selector & ~0x03) | REG8(CH);
7609: }
7610: if(REG8(BL) == 0x00) {
7611: selector &= ~0x04;
7612: } else if(REG8(BL) == 0x01) {
7613: selector |= 0x04;
7614: }
7615: if(REG8(BH) == 0x00) {
7616: selector = (selector & ~0x38) | 0x00;
7617: } else if(REG8(BH) == 0x01) {
7618: selector = (selector & ~0x38) | 0x08;
7619: } else if(REG8(BH) == 0x02) {
7620: selector = (selector & ~0x38) | 0x18;
7621: } else if(REG8(BH) == 0x03) {
7622: selector = (selector & ~0x38) | 0x28;
7623: } else if(REG8(BH) == 0x04) {
7624: selector = (selector & ~0x38) | 0x38;
7625: }
7626: if(REG8(AL) == 0x00) {
7627: selector |= 0x40;
7628: } else if(REG8(AL) == 0x01) {
7629: selector &= ~0x40;
7630: }
7631: if(REG8(CL) <= 0x0b) {
7632: static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
7633: UINT16 divisor = 115200 / rate[REG8(CL)];
7634: sio_write(REG16(DX), 3, selector | 0x80);
7635: sio_write(REG16(DX), 0, divisor & 0xff);
7636: sio_write(REG16(DX), 1, divisor >> 8);
7637: }
7638: sio_write(REG16(DX), 3, selector);
7639: REG8(AH) = sio_read(REG16(DX), 5);
7640: REG8(AL) = sio_read(REG16(DX), 6);
7641: } else {
7642: REG8(AH) = 0x80;
7643: }
7644: }
7645:
7646: inline void pcbios_int_14h_05h()
7647: {
1.1.1.29 root 7648: if(REG16(DX) < 4) {
1.1.1.25 root 7649: if(REG8(AL) == 0x00) {
7650: REG8(BL) = sio_read(REG16(DX), 4);
7651: REG8(AH) = sio_read(REG16(DX), 5);
7652: REG8(AL) = sio_read(REG16(DX), 6);
7653: } else if(REG8(AL) == 0x01) {
7654: sio_write(REG16(DX), 4, REG8(BL));
7655: REG8(AH) = sio_read(REG16(DX), 5);
7656: REG8(AL) = sio_read(REG16(DX), 6);
7657: } else {
7658: 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));
7659: }
7660: } else {
7661: REG8(AH) = 0x80;
7662: }
7663: }
7664:
1.1.1.14 root 7665: inline void pcbios_int_15h_10h()
7666: {
1.1.1.22 root 7667: switch(REG8(AL)) {
7668: case 0x00:
1.1.1.14 root 7669: Sleep(10);
1.1.1.35 root 7670: REQUEST_HARDWRE_UPDATE();
1.1.1.22 root 7671: break;
7672: default:
7673: 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 7674: REG8(AH) = 0x86;
7675: m_CF = 1;
7676: }
7677: }
7678:
1.1 root 7679: inline void pcbios_int_15h_23h()
7680: {
7681: switch(REG8(AL)) {
1.1.1.22 root 7682: case 0x00:
1.1.1.8 root 7683: REG8(CL) = cmos_read(0x2d);
7684: REG8(CH) = cmos_read(0x2e);
1.1 root 7685: break;
1.1.1.22 root 7686: case 0x01:
1.1.1.8 root 7687: cmos_write(0x2d, REG8(CL));
7688: cmos_write(0x2e, REG8(CH));
1.1 root 7689: break;
7690: default:
1.1.1.22 root 7691: 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 7692: REG8(AH) = 0x86;
1.1.1.3 root 7693: m_CF = 1;
1.1 root 7694: break;
7695: }
7696: }
7697:
7698: inline void pcbios_int_15h_24h()
7699: {
7700: switch(REG8(AL)) {
1.1.1.22 root 7701: case 0x00:
1.1.1.3 root 7702: i386_set_a20_line(0);
1.1 root 7703: REG8(AH) = 0;
7704: break;
1.1.1.22 root 7705: case 0x01:
1.1.1.3 root 7706: i386_set_a20_line(1);
1.1 root 7707: REG8(AH) = 0;
7708: break;
1.1.1.22 root 7709: case 0x02:
1.1 root 7710: REG8(AH) = 0;
1.1.1.3 root 7711: REG8(AL) = (m_a20_mask >> 20) & 1;
1.1 root 7712: REG16(CX) = 0;
7713: break;
1.1.1.22 root 7714: case 0x03:
1.1 root 7715: REG16(AX) = 0;
7716: REG16(BX) = 0;
7717: break;
1.1.1.22 root 7718: default:
7719: 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));
7720: REG8(AH) = 0x86;
7721: m_CF = 1;
7722: break;
1.1 root 7723: }
7724: }
7725:
7726: inline void pcbios_int_15h_49h()
7727: {
1.1.1.27 root 7728: REG8(AH) = 0x00;
7729: REG8(BL) = 0x00; // DOS/V
1.1 root 7730: }
7731:
1.1.1.22 root 7732: inline void pcbios_int_15h_50h()
7733: {
7734: switch(REG8(AL)) {
7735: case 0x00:
7736: case 0x01:
7737: if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
7738: REG8(AH) = 0x01; // invalid font type in bh
7739: m_CF = 1;
1.1.1.27 root 7740: } else if(REG8(BL) != 0x00) {
1.1.1.22 root 7741: REG8(AH) = 0x02; // bl not zero
7742: m_CF = 1;
7743: } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
7744: REG8(AH) = 0x04; // invalid code page
7745: m_CF = 1;
1.1.1.27 root 7746: } else if(REG8(AL) == 0x01) {
7747: REG8(AH) = 0x06; // font is read only
1.1.1.22 root 7748: m_CF = 1;
1.1.1.27 root 7749: } else {
7750: // dummy font read routine is at fffd:000d
7751: SREG(ES) = 0xfffd;
7752: i386_load_segment_descriptor(ES);
1.1.1.32 root 7753: REG16(BX) = 0x000d;
1.1.1.27 root 7754: REG8(AH) = 0x00; // success
1.1.1.22 root 7755: }
7756: break;
7757: default:
7758: 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));
7759: REG8(AH) = 0x86;
7760: m_CF = 1;
7761: break;
7762: }
7763: }
7764:
1.1.1.30 root 7765: inline void pcbios_int_15h_53h()
7766: {
7767: switch(REG8(AL)) {
7768: case 0x00:
7769: // APM is not installed
7770: REG8(AH) = 0x86;
7771: m_CF = 1;
7772: break;
7773: default:
7774: 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));
7775: REG8(AH) = 0x86;
7776: m_CF = 1;
7777: break;
7778: }
7779: }
7780:
1.1.1.35 root 7781:
7782: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1 root 7783: {
7784: UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14 root 7785: UINT32 msec = usec / 1000;
7786:
1.1.1.35 root 7787: while(msec && !m_halted) {
1.1.1.14 root 7788: UINT32 tmp = min(msec, 100);
7789: if(msec - tmp < 10) {
7790: tmp = msec;
7791: }
7792: Sleep(tmp);
7793: msec -= tmp;
7794: }
1.1.1.35 root 7795:
7796: #ifdef USE_SERVICE_THREAD
7797: service_exit = true;
7798: #endif
7799: return(0);
7800: }
7801:
7802: inline void pcbios_int_15h_86h()
7803: {
7804: if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
7805: #ifdef USE_SERVICE_THREAD
7806: start_service_loop(pcbios_int_15h_86h_thread);
7807: #else
7808: pcbios_int_15h_86h_thread(NULL);
7809: REQUEST_HARDWRE_UPDATE();
7810: #endif
7811: }
1.1 root 7812: }
7813:
7814: inline void pcbios_int_15h_87h()
7815: {
7816: // copy extended memory (from DOSBox)
7817: int len = REG16(CX) * 2;
1.1.1.3 root 7818: int ofs = SREG_BASE(ES) + REG16(SI);
1.1 root 7819: int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
7820: int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
7821: memcpy(mem + dst, mem + src, len);
7822: REG16(AX) = 0x00;
7823: }
7824:
7825: inline void pcbios_int_15h_88h()
7826: {
1.1.1.17 root 7827: REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1 root 7828: }
7829:
7830: inline void pcbios_int_15h_89h()
7831: {
1.1.1.21 root 7832: #if defined(HAS_I286) || defined(HAS_I386)
1.1 root 7833: // switch to protected mode (from DOSBox)
7834: write_io_byte(0x20, 0x10);
7835: write_io_byte(0x21, REG8(BH));
7836: write_io_byte(0x21, 0x00);
7837: write_io_byte(0xa0, 0x10);
7838: write_io_byte(0xa1, REG8(BL));
7839: write_io_byte(0xa1, 0x00);
1.1.1.3 root 7840: i386_set_a20_line(1);
7841: int ofs = SREG_BASE(ES) + REG16(SI);
7842: m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
7843: m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
7844: m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
7845: m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
7846: #if defined(HAS_I386)
7847: m_cr[0] |= 1;
7848: #else
7849: m_msw |= 1;
7850: #endif
7851: SREG(DS) = 0x18;
7852: SREG(ES) = 0x20;
7853: SREG(SS) = 0x28;
7854: i386_load_segment_descriptor(DS);
7855: i386_load_segment_descriptor(ES);
7856: i386_load_segment_descriptor(SS);
1.1.1.21 root 7857: UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1 root 7858: REG16(SP) += 6;
1.1.1.3 root 7859: #if defined(HAS_I386)
1.1.1.21 root 7860: UINT32 flags = get_flags();
7861: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
7862: set_flags(flags);
1.1.1.3 root 7863: #else
1.1.1.21 root 7864: UINT32 flags = CompressFlags();
7865: flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
7866: ExpandFlags(flags);
1.1.1.3 root 7867: #endif
1.1 root 7868: REG16(AX) = 0x00;
1.1.1.21 root 7869: i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1 root 7870: #else
1.1.1.21 root 7871: // i86/i186/v30: protected mode is not supported
1.1 root 7872: REG8(AH) = 0x86;
1.1.1.3 root 7873: m_CF = 1;
1.1 root 7874: #endif
7875: }
7876:
1.1.1.21 root 7877: inline void pcbios_int_15h_8ah()
7878: {
7879: UINT32 size = MAX_MEM - 0x100000;
7880: REG16(AX) = size & 0xffff;
7881: REG16(DX) = size >> 16;
7882: }
7883:
1.1.1.3 root 7884: #if defined(HAS_I386)
1.1 root 7885: inline void pcbios_int_15h_c9h()
7886: {
7887: REG8(AH) = 0x00;
7888: REG8(CH) = cpu_type;
7889: REG8(CL) = cpu_step;
7890: }
1.1.1.3 root 7891: #endif
1.1 root 7892:
7893: inline void pcbios_int_15h_cah()
7894: {
7895: switch(REG8(AL)) {
1.1.1.22 root 7896: case 0x00:
1.1 root 7897: if(REG8(BL) > 0x3f) {
7898: REG8(AH) = 0x03;
1.1.1.3 root 7899: m_CF = 1;
1.1 root 7900: } else if(REG8(BL) < 0x0e) {
7901: REG8(AH) = 0x04;
1.1.1.3 root 7902: m_CF = 1;
1.1 root 7903: } else {
1.1.1.8 root 7904: REG8(CL) = cmos_read(REG8(BL));
1.1 root 7905: }
7906: break;
1.1.1.22 root 7907: case 0x01:
1.1 root 7908: if(REG8(BL) > 0x3f) {
7909: REG8(AH) = 0x03;
1.1.1.3 root 7910: m_CF = 1;
1.1 root 7911: } else if(REG8(BL) < 0x0e) {
7912: REG8(AH) = 0x04;
1.1.1.3 root 7913: m_CF = 1;
1.1 root 7914: } else {
1.1.1.8 root 7915: cmos_write(REG8(BL), REG8(CL));
1.1 root 7916: }
7917: break;
7918: default:
1.1.1.22 root 7919: 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 7920: REG8(AH) = 0x86;
1.1.1.3 root 7921: m_CF = 1;
1.1 root 7922: break;
7923: }
7924: }
7925:
1.1.1.22 root 7926: inline void pcbios_int_15h_e8h()
1.1.1.17 root 7927: {
1.1.1.22 root 7928: switch(REG8(AL)) {
7929: #if defined(HAS_I386)
7930: case 0x01:
7931: REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
7932: REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
7933: break;
1.1.1.17 root 7934: #endif
1.1.1.22 root 7935: default:
7936: 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));
7937: REG8(AH) = 0x86;
7938: m_CF = 1;
7939: break;
7940: }
7941: }
1.1.1.17 root 7942:
1.1.1.33 root 7943: void pcbios_update_key_code(bool wait)
1.1 root 7944: {
1.1.1.32 root 7945: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 7946: #ifdef USE_SERVICE_THREAD
7947: EnterCriticalSection(&key_buf_crit_sect);
7948: #endif
7949: bool empty = key_buf_char->empty();
7950: #ifdef USE_SERVICE_THREAD
7951: LeaveCriticalSection(&key_buf_crit_sect);
7952: #endif
7953: if(empty) {
1.1.1.32 root 7954: if(!update_key_buffer()) {
1.1.1.33 root 7955: if(wait) {
1.1.1.32 root 7956: Sleep(10);
7957: } else {
7958: maybe_idle();
7959: }
1.1.1.14 root 7960: }
7961: }
1.1.1.34 root 7962: }
7963: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 7964: #ifdef USE_SERVICE_THREAD
7965: EnterCriticalSection(&key_buf_crit_sect);
7966: #endif
1.1.1.32 root 7967: if(key_buf_char->count() != 0) {
1.1.1.41 root 7968: int key_char = key_buf_char->read();
7969: int key_scan = key_buf_scan->read();
7970: key_code = key_char << 0;
7971: key_code |= key_scan << 8;
1.1.1.35 root 7972: key_recv = 0x0000ffff;
1.1.1.41 root 7973: // write to bottom of key buffer
7974: mem[0x43c] = (UINT8)key_char;
7975: mem[0x43d] = (UINT8)key_scan;
1.1.1.32 root 7976: }
7977: if(key_buf_char->count() != 0) {
1.1.1.41 root 7978: int key_char = key_buf_char->read();
7979: int key_scan = key_buf_scan->read();
7980: key_code |= key_char << 16;
7981: key_code |= key_scan << 24;
1.1.1.33 root 7982: key_recv |= 0xffff0000;
1.1.1.41 root 7983: // write to bottom of key buffer
7984: mem[0x43c] = (UINT8)key_char;
7985: mem[0x43d] = (UINT8)key_scan;
1.1.1.32 root 7986: }
1.1.1.35 root 7987: #ifdef USE_SERVICE_THREAD
7988: LeaveCriticalSection(&key_buf_crit_sect);
7989: #endif
1.1 root 7990: }
7991: }
7992:
1.1.1.35 root 7993: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1 root 7994: {
1.1.1.33 root 7995: while(key_recv == 0 && !m_halted) {
7996: pcbios_update_key_code(true);
1.1 root 7997: }
1.1.1.33 root 7998: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
7999: if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
8000: if(REG8(AH) == 0x10) {
8001: key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
8002: } else {
8003: key_code = ((key_code >> 16) & 0xff00);
8004: }
8005: key_recv >>= 16;
1.1 root 8006: }
8007: }
8008: REG16(AX) = key_code & 0xffff;
8009: key_code >>= 16;
1.1.1.33 root 8010: key_recv >>= 16;
1.1.1.35 root 8011:
8012: #ifdef USE_SERVICE_THREAD
8013: service_exit = true;
8014: #endif
8015: return(0);
8016: }
8017:
8018: inline void pcbios_int_16h_00h()
8019: {
8020: #ifdef USE_SERVICE_THREAD
8021: start_service_loop(pcbios_int_16h_00h_thread);
8022: #else
8023: pcbios_int_16h_00h_thread(NULL);
8024: REQUEST_HARDWRE_UPDATE();
8025: #endif
1.1 root 8026: }
8027:
8028: inline void pcbios_int_16h_01h()
8029: {
1.1.1.33 root 8030: if(key_recv == 0) {
8031: pcbios_update_key_code(false);
1.1.1.5 root 8032: }
1.1.1.33 root 8033: if(key_recv != 0) {
8034: UINT32 key_code_tmp = key_code;
8035: if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
8036: if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
8037: if(REG8(AH) == 0x11) {
8038: key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
8039: } else {
8040: key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
8041: }
8042: }
1.1 root 8043: }
1.1.1.5 root 8044: REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3 root 8045: #if defined(HAS_I386)
1.1.1.33 root 8046: m_ZF = 0;
8047: #else
8048: m_ZeroVal = 1;
8049: #endif
8050: } else {
8051: #if defined(HAS_I386)
8052: m_ZF = 1;
1.1.1.3 root 8053: #else
1.1.1.33 root 8054: m_ZeroVal = 0;
1.1.1.3 root 8055: #endif
1.1.1.33 root 8056: }
1.1 root 8057: }
8058:
8059: inline void pcbios_int_16h_02h()
8060: {
8061: REG8(AL) = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
8062: REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
8063: REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
8064: REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
8065: REG8(AL) |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
8066: REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
8067: REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
8068: REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
8069: }
8070:
8071: inline void pcbios_int_16h_03h()
8072: {
8073: static UINT16 status = 0;
8074:
8075: switch(REG8(AL)) {
8076: case 0x05:
8077: status = REG16(BX);
8078: break;
8079: case 0x06:
8080: REG16(BX) = status;
8081: break;
8082: default:
1.1.1.3 root 8083: m_CF = 1;
1.1 root 8084: break;
8085: }
8086: }
8087:
8088: inline void pcbios_int_16h_05h()
8089: {
1.1.1.32 root 8090: if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35 root 8091: #ifdef USE_SERVICE_THREAD
8092: EnterCriticalSection(&key_buf_crit_sect);
8093: #endif
1.1.1.32 root 8094: key_buf_char->write(REG8(CL));
8095: key_buf_scan->write(REG8(CH));
1.1.1.35 root 8096: #ifdef USE_SERVICE_THREAD
8097: LeaveCriticalSection(&key_buf_crit_sect);
8098: #endif
1.1.1.32 root 8099: }
1.1 root 8100: REG8(AL) = 0x00;
8101: }
8102:
8103: inline void pcbios_int_16h_12h()
8104: {
8105: pcbios_int_16h_02h();
8106:
8107: REG8(AH) = 0;//(GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x80 : 0;
8108: REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
8109: REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
8110: REG8(AH) |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
8111: REG8(AH) |= (GetAsyncKeyState(VK_RMENU ) & 0x8000) ? 0x08 : 0;
8112: REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
8113: REG8(AH) |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
8114: REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
8115: }
8116:
8117: inline void pcbios_int_16h_13h()
8118: {
8119: static UINT16 status = 0;
8120:
8121: switch(REG8(AL)) {
8122: case 0x00:
8123: status = REG16(DX);
8124: break;
8125: case 0x01:
8126: REG16(DX) = status;
8127: break;
8128: default:
1.1.1.22 root 8129: 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 8130: m_CF = 1;
1.1 root 8131: break;
8132: }
8133: }
8134:
8135: inline void pcbios_int_16h_14h()
8136: {
8137: static UINT8 status = 0;
8138:
8139: switch(REG8(AL)) {
8140: case 0x00:
8141: case 0x01:
8142: status = REG8(AL);
8143: break;
8144: case 0x02:
8145: REG8(AL) = status;
8146: break;
8147: default:
1.1.1.22 root 8148: 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 8149: m_CF = 1;
1.1 root 8150: break;
8151: }
8152: }
8153:
1.1.1.24 root 8154: inline void pcbios_int_16h_55h()
8155: {
8156: switch(REG8(AL)) {
8157: case 0x00:
8158: // keyboard tsr is not present
8159: break;
8160: case 0xfe:
8161: // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
8162: break;
8163: case 0xff:
8164: break;
8165: default:
8166: 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));
8167: m_CF = 1;
8168: break;
8169: }
8170: }
8171:
1.1.1.30 root 8172: inline void pcbios_int_16h_6fh()
8173: {
8174: switch(REG8(AL)) {
8175: case 0x00:
8176: // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
8177: break;
8178: default:
8179: 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));
8180: m_CF = 1;
8181: break;
8182: }
8183: }
8184:
1.1.1.37 root 8185: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
8186: {
8187: UINT8 hi = jis >> 8;
8188: UINT8 lo = jis & 0xff;
8189:
8190: lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
8191: hi = (hi - 0x21) / 2 + 0x81;
8192: hi = (hi >= 0xa0) ? hi + 0x40 : hi;
8193: lo = (lo >= 0x7f) ? lo + 0x01 : lo;
8194:
8195: return((hi << 8) + lo);
8196: }
8197:
8198: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
8199: {
8200: UINT8 hi = sjis >> 8;
8201: UINT8 lo = sjis & 0xff;
8202:
8203: if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
8204: return(0x2121);
8205: }
8206: if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
8207: return(0x2121);
8208: }
8209: if(hi >= 0xf0 && hi <= 0xf3) {
8210: // gaiji
8211: if(lo >= 0x40 && lo <= 0x7e) {
8212: return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
8213: }
8214: if(lo >= 0x80 && lo <= 0x9e) {
8215: return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
8216: }
8217: if(lo >= 0x9f && lo <= 0xfc) {
8218: return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
8219: }
8220: }
8221: hi = (hi >= 0xe0) ? hi - 0x40 : hi;
8222: lo = (lo >= 0x80) ? lo - 0x01 : lo;
8223: hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
8224: lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
8225:
8226: return((hi << 8) + lo);
8227: }
8228:
1.1.1.38 root 8229: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
8230: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
8231: // 6. �R���g���[���R�[�h�̉��
1.1.1.37 root 8232:
8233: void pcbios_printer_out(int c, UINT8 data)
8234: {
8235: if(pio[c].conv_mode) {
8236: if(pio[c].sjis_hi != 0) {
8237: if(!pio[c].jis_mode) {
8238: printer_out(c, 0x1c);
8239: printer_out(c, 0x26);
8240: pio[c].jis_mode = true;
8241: }
8242: UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
8243: printer_out(c, jis >> 8);
8244: printer_out(c, jis & 0xff);
8245: pio[c].sjis_hi = 0;
8246: } else if(pio[c].esc_buf[0] == 0x1b) {
8247: printer_out(c, data);
8248: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
8249: pio[c].esc_buf[pio[c].esc_len] = data;
8250: }
8251: pio[c].esc_len++;
8252:
8253: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 8254: case 0x33: // 1Bh 33h XX
8255: case 0x4a: // 1Bh 4Ah XX
8256: case 0x4e: // 1Bh 4Eh XX
8257: case 0x51: // 1Bh 51h XX
8258: case 0x55: // 1Bh 55h XX
8259: case 0x6c: // 1Bh 6Ch XX
8260: case 0x71: // 1Bh 71h XX
8261: case 0x72: // 1Bh 72h XX
1.1.1.37 root 8262: if(pio[c].esc_len == 3) {
8263: pio[c].esc_buf[0] = 0x00;
8264: }
8265: break;
1.1.1.38 root 8266: case 0x24: // 1Bh 24h XX XX
8267: case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37 root 8268: if(pio[c].esc_len == 4) {
8269: pio[c].esc_buf[0] = 0x00;
8270: }
8271: break;
1.1.1.38 root 8272: case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37 root 8273: if(pio[c].esc_len >= 3) {
8274: switch(pio[c].esc_buf[2]) {
8275: case 0: case 1: case 2: case 3: case 4: case 6:
8276: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
8277: pio[c].esc_buf[0] = 0x00;
8278: }
8279: break;
8280: case 32: case 33: case 38: case 39: case 40:
8281: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
8282: pio[c].esc_buf[0] = 0x00;
8283: }
8284: break;
1.1.1.38 root 8285: case 71: case 72: case 73:
8286: if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
8287: pio[c].esc_buf[0] = 0x00;
8288: }
8289: break;
1.1.1.37 root 8290: default:
8291: pio[c].esc_buf[0] = 0x00;
8292: break;
8293: }
8294: }
8295: break;
1.1.1.38 root 8296: case 0x40: // 1Bh 40h
1.1.1.37 root 8297: if(pio[c].jis_mode) {
8298: printer_out(c, 0x1c);
8299: printer_out(c, 0x2e);
8300: pio[c].jis_mode = false;
8301: }
8302: pio[c].esc_buf[0] = 0x00;
8303: break;
1.1.1.38 root 8304: case 0x42: // 1Bh 42h data 00h
8305: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 8306: if(pio[c].esc_len >= 3 && data == 0) {
8307: pio[c].esc_buf[0] = 0x00;
8308: }
8309: break;
1.1.1.38 root 8310: case 0x43: // 1Bh 43h (00h) XX
1.1.1.37 root 8311: if(pio[c].esc_len >= 3 && data != 0) {
8312: pio[c].esc_buf[0] = 0x00;
8313: }
8314: break;
1.1.1.38 root 8315: default: // 1Bh XX
1.1.1.37 root 8316: pio[c].esc_buf[0] = 0x00;
8317: break;
8318: }
8319: } else if(pio[c].esc_buf[0] == 0x1c) {
8320: printer_out(c, data);
8321: if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
8322: pio[c].esc_buf[pio[c].esc_len] = data;
8323: }
8324: pio[c].esc_len++;
8325:
8326: switch(pio[c].esc_buf[1]) {
1.1.1.38 root 8327: case 0x21: // 1Ch 21h XX
8328: case 0x2d: // 1Ch 2Dh XX
8329: case 0x57: // 1Ch 57h XX
8330: case 0x6b: // 1Ch 6Bh XX
8331: case 0x72: // 1Ch 72h XX
8332: case 0x78: // 1Ch 78h XX
1.1.1.37 root 8333: if(pio[c].esc_len == 3) {
8334: pio[c].esc_buf[0] = 0x00;
8335: }
8336: break;
1.1.1.38 root 8337: case 0x26: // 1Ch 26h
1.1.1.37 root 8338: pio[c].jis_mode = true;
8339: pio[c].esc_buf[0] = 0x00;
8340: break;
1.1.1.38 root 8341: case 0x2e: // 1Ch 2Eh
1.1.1.37 root 8342: pio[c].jis_mode = false;
8343: pio[c].esc_buf[0] = 0x00;
8344: break;
1.1.1.38 root 8345: case 0x32: // 1Ch 32h XX XX data
1.1.1.37 root 8346: if(pio[c].esc_len == 76) {
8347: pio[c].esc_buf[0] = 0x00;
8348: }
8349: break;
1.1.1.38 root 8350: case 0x44: // 1Bh 44h data 00h
1.1.1.37 root 8351: if(pio[c].esc_len == 6) {
8352: pio[c].esc_buf[0] = 0x00;
8353: }
8354: break;
1.1.1.38 root 8355: case 0x53: // 1Ch 53h XX XX
8356: case 0x54: // 1Ch 54h XX XX
1.1.1.37 root 8357: if(pio[c].esc_len == 4) {
8358: pio[c].esc_buf[0] = 0x00;
8359: }
8360: break;
1.1.1.38 root 8361: default: // 1Ch XX
1.1.1.37 root 8362: pio[c].esc_buf[0] = 0x00;
8363: break;
8364: }
8365: } else if(data == 0x1b || data == 0x1c) {
8366: printer_out(c, data);
8367: pio[c].esc_buf[0] = data;
8368: pio[c].esc_len = 1;
8369: } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
8370: pio[c].sjis_hi = data;
8371: } else {
8372: if(pio[c].jis_mode) {
8373: printer_out(c, 0x1c);
8374: printer_out(c, 0x2e);
8375: pio[c].jis_mode = false;
8376: }
8377: printer_out(c, data);
8378: }
8379: } else {
8380: if(pio[c].jis_mode) {
8381: printer_out(c, 0x1c);
8382: printer_out(c, 0x2e);
8383: pio[c].jis_mode = false;
8384: }
8385: printer_out(c, data);
8386: }
8387: }
8388:
8389: inline void pcbios_int_17h_00h()
8390: {
8391: if(REG16(DX) < 3) {
8392: pcbios_printer_out(REG16(DX), REG8(AL));
8393: REG8(AH) = 0xd0;
8394: }
8395: }
8396:
8397: inline void pcbios_int_17h_01h()
8398: {
8399: if(REG16(DX) < 3) {
8400: REG8(AH) = 0xd0;
8401: }
8402: }
8403:
8404: inline void pcbios_int_17h_02h()
8405: {
8406: if(REG16(DX) < 3) {
8407: REG8(AH) = 0xd0;
8408: }
8409: }
8410:
8411: inline void pcbios_int_17h_03h()
8412: {
8413: switch(REG8(AL)) {
8414: case 0x00:
8415: if(REG16(DX) < 3) {
8416: if(pio[REG16(DX)].jis_mode) {
8417: printer_out(REG16(DX), 0x1c);
8418: printer_out(REG16(DX), 0x2e);
8419: pio[REG16(DX)].jis_mode = false;
8420: }
8421: for(UINT16 i = 0; i < REG16(CX); i++) {
8422: printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
8423: }
8424: REG16(CX) = 0x0000;
8425: REG8(AH) = 0xd0;
8426: }
8427: break;
8428: default:
8429: 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));
8430: break;
8431: }
8432: }
8433:
8434: inline void pcbios_int_17h_50h()
8435: {
8436: switch(REG8(AL)) {
8437: case 0x00:
8438: if(REG16(DX) < 3) {
8439: if(REG16(BX) = 0x0001) {
8440: pio[REG16(DX)].conv_mode = false;
8441: REG8(AL) = 0x00;
8442: } else if(REG16(BX) = 0x0051) {
8443: pio[REG16(DX)].conv_mode = true;
8444: REG8(AL) = 0x00;
8445: } else {
8446: REG8(AL) = 0x01;
8447: }
8448: } else {
8449: REG8(AL) = 0x02;
8450: }
8451: break;
8452: case 0x01:
8453: if(REG16(DX) < 3) {
8454: if(pio[REG16(DX)].conv_mode) {
8455: REG16(BX) = 0x0051;
8456: } else {
8457: REG16(BX) = 0x0001;
8458: }
8459: REG8(AL) = 0x00;
8460: } else {
8461: REG8(AL) = 0x02;
8462: }
8463: break;
8464: default:
8465: 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));
8466: break;
8467: }
8468: }
8469:
8470: inline void pcbios_int_17h_51h()
8471: {
8472: if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
8473: REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
8474: } else {
8475: REG16(DX) = 0x0000;
8476: }
8477: }
8478:
8479: inline void pcbios_int_17h_52h()
8480: {
8481: if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
8482: REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
8483: } else {
8484: REG16(DX) = 0x0000;
8485: }
8486: }
8487:
8488: inline void pcbios_int_17h_84h()
8489: {
8490: if(REG16(DX) < 3) {
8491: if(pio[REG16(DX)].jis_mode) {
8492: printer_out(REG16(DX), 0x1c);
8493: printer_out(REG16(DX), 0x2e);
8494: pio[REG16(DX)].jis_mode = false;
8495: }
8496: printer_out(REG16(DX), REG8(AL));
8497: REG8(AH) = 0xd0;
8498: }
8499: }
8500:
8501: inline void pcbios_int_17h_85h()
8502: {
8503: pio[0].conv_mode = (REG8(AL) == 0x00);
8504: }
8505:
1.1 root 8506: inline void pcbios_int_1ah_00h()
8507: {
1.1.1.19 root 8508: pcbios_update_daily_timer_counter(timeGetTime());
8509: REG16(CX) = *(UINT16 *)(mem + 0x46e);
8510: REG16(DX) = *(UINT16 *)(mem + 0x46c);
8511: REG8(AL) = mem[0x470];
8512: mem[0x470] = 0;
1.1 root 8513: }
8514:
8515: inline int to_bcd(int t)
8516: {
8517: int u = (t % 100) / 10;
8518: return (u << 4) | (t % 10);
8519: }
8520:
8521: inline void pcbios_int_1ah_02h()
8522: {
8523: SYSTEMTIME time;
8524:
8525: GetLocalTime(&time);
8526: REG8(CH) = to_bcd(time.wHour);
8527: REG8(CL) = to_bcd(time.wMinute);
8528: REG8(DH) = to_bcd(time.wSecond);
8529: REG8(DL) = 0x00;
8530: }
8531:
8532: inline void pcbios_int_1ah_04h()
8533: {
8534: SYSTEMTIME time;
8535:
8536: GetLocalTime(&time);
8537: REG8(CH) = to_bcd(time.wYear / 100);
8538: REG8(CL) = to_bcd(time.wYear);
8539: REG8(DH) = to_bcd(time.wMonth);
8540: REG8(DL) = to_bcd(time.wDay);
8541: }
8542:
8543: inline void pcbios_int_1ah_0ah()
8544: {
8545: SYSTEMTIME time;
8546: FILETIME file_time;
8547: WORD dos_date, dos_time;
8548:
8549: GetLocalTime(&time);
8550: SystemTimeToFileTime(&time, &file_time);
8551: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
8552: REG16(CX) = dos_date;
8553: }
8554:
8555: // msdos system call
8556:
8557: inline void msdos_int_21h_00h()
8558: {
1.1.1.3 root 8559: msdos_process_terminate(SREG(CS), retval, 1);
1.1 root 8560: }
8561:
1.1.1.35 root 8562: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1 root 8563: {
8564: REG8(AL) = msdos_getche();
1.1.1.33 root 8565: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 8566:
1.1.1.35 root 8567: #ifdef USE_SERVICE_THREAD
8568: service_exit = true;
8569: #endif
8570: return(0);
8571: }
8572:
8573: inline void msdos_int_21h_01h()
8574: {
8575: #ifdef USE_SERVICE_THREAD
8576: start_service_loop(msdos_int_21h_01h_thread);
8577: #else
8578: msdos_int_21h_01h_thread(NULL);
8579: REQUEST_HARDWRE_UPDATE();
8580: #endif
1.1 root 8581: }
8582:
8583: inline void msdos_int_21h_02h()
8584: {
1.1.1.33 root 8585: UINT8 data = REG8(DL);
8586: msdos_putch(data);
8587: REG8(AL) = data;
8588: ctrl_break_detected = ctrl_break_pressed;
1.1 root 8589: }
8590:
8591: inline void msdos_int_21h_03h()
8592: {
8593: REG8(AL) = msdos_aux_in();
8594: }
8595:
8596: inline void msdos_int_21h_04h()
8597: {
8598: msdos_aux_out(REG8(DL));
8599: }
8600:
8601: inline void msdos_int_21h_05h()
8602: {
8603: msdos_prn_out(REG8(DL));
8604: }
8605:
8606: inline void msdos_int_21h_06h()
8607: {
8608: if(REG8(DL) == 0xff) {
8609: if(msdos_kbhit()) {
8610: REG8(AL) = msdos_getch();
1.1.1.3 root 8611: #if defined(HAS_I386)
8612: m_ZF = 0;
8613: #else
8614: m_ZeroVal = 1;
8615: #endif
1.1 root 8616: } else {
8617: REG8(AL) = 0;
1.1.1.3 root 8618: #if defined(HAS_I386)
8619: m_ZF = 1;
8620: #else
8621: m_ZeroVal = 0;
8622: #endif
1.1.1.14 root 8623: maybe_idle();
1.1 root 8624: }
8625: } else {
1.1.1.33 root 8626: UINT8 data = REG8(DL);
8627: msdos_putch(data);
8628: REG8(AL) = data;
1.1 root 8629: }
8630: }
8631:
1.1.1.35 root 8632: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1 root 8633: {
8634: REG8(AL) = msdos_getch();
1.1.1.26 root 8635:
1.1.1.35 root 8636: #ifdef USE_SERVICE_THREAD
8637: service_exit = true;
8638: #endif
8639: return(0);
1.1 root 8640: }
8641:
1.1.1.35 root 8642: inline void msdos_int_21h_07h()
8643: {
8644: #ifdef USE_SERVICE_THREAD
8645: start_service_loop(msdos_int_21h_07h_thread);
8646: #else
8647: msdos_int_21h_07h_thread(NULL);
8648: REQUEST_HARDWRE_UPDATE();
8649: #endif
8650: }
8651:
8652: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1 root 8653: {
8654: REG8(AL) = msdos_getch();
1.1.1.33 root 8655: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 8656:
1.1.1.35 root 8657: #ifdef USE_SERVICE_THREAD
8658: service_exit = true;
8659: #endif
8660: return(0);
8661: }
8662:
8663: inline void msdos_int_21h_08h()
8664: {
8665: #ifdef USE_SERVICE_THREAD
8666: start_service_loop(msdos_int_21h_08h_thread);
8667: #else
8668: msdos_int_21h_08h_thread(NULL);
8669: REQUEST_HARDWRE_UPDATE();
8670: #endif
1.1 root 8671: }
8672:
8673: inline void msdos_int_21h_09h()
8674: {
1.1.1.21 root 8675: msdos_stdio_reopen();
8676:
1.1.1.20 root 8677: process_t *process = msdos_process_info_get(current_psp);
8678: int fd = msdos_psp_get_file_table(1, current_psp);
8679:
1.1.1.14 root 8680: char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
8681: int len = 0;
1.1 root 8682:
1.1.1.14 root 8683: while(str[len] != '$' && len < 0x10000) {
8684: len++;
8685: }
1.1.1.20 root 8686: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 8687: // stdout is redirected to file
1.1.1.20 root 8688: msdos_write(fd, str, len);
1.1 root 8689: } else {
8690: for(int i = 0; i < len; i++) {
1.1.1.14 root 8691: msdos_putch(str[i]);
1.1 root 8692: }
8693: }
1.1.1.33 root 8694: REG8(AL) = '$';
8695: ctrl_break_detected = ctrl_break_pressed;
1.1 root 8696: }
8697:
1.1.1.35 root 8698: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1 root 8699: {
1.1.1.3 root 8700: int ofs = SREG_BASE(DS) + REG16(DX);
1.1 root 8701: int max = mem[ofs] - 1;
8702: UINT8 *buf = mem + ofs + 2;
8703: int chr, p = 0;
8704:
8705: while((chr = msdos_getch()) != 0x0d) {
1.1.1.33 root 8706: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26 root 8707: p = 0;
1.1.1.33 root 8708: msdos_putch(0x03);
8709: msdos_putch(0x0d);
8710: msdos_putch(0x0a);
1.1.1.26 root 8711: break;
1.1.1.33 root 8712: } else if(ctrl_break_pressed) {
8713: // skip this byte
1.1.1.26 root 8714: } else if(chr == 0x00) {
1.1 root 8715: // skip 2nd byte
8716: msdos_getch();
8717: } else if(chr == 0x08) {
8718: // back space
8719: if(p > 0) {
8720: p--;
1.1.1.20 root 8721: if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34 root 8722: msdos_putch(0x08);
8723: msdos_putch(0x08);
8724: msdos_putch(0x20);
8725: msdos_putch(0x20);
8726: msdos_putch(0x08);
8727: msdos_putch(0x08);
1.1.1.36 root 8728: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
8729: p--;
8730: msdos_putch(0x08);
8731: msdos_putch(0x08);
8732: msdos_putch(0x20);
8733: msdos_putch(0x20);
8734: msdos_putch(0x08);
8735: msdos_putch(0x08);
1.1.1.34 root 8736: } else {
8737: msdos_putch(0x08);
8738: msdos_putch(0x20);
8739: msdos_putch(0x08);
8740: }
8741: }
8742: } else if(chr == 0x1b) {
8743: // escape
8744: while(p > 0) {
8745: p--;
8746: if(msdos_ctrl_code_check(buf[p])) {
8747: msdos_putch(0x08);
8748: msdos_putch(0x08);
8749: msdos_putch(0x20);
8750: msdos_putch(0x20);
8751: msdos_putch(0x08);
8752: msdos_putch(0x08);
1.1.1.20 root 8753: } else {
1.1.1.34 root 8754: msdos_putch(0x08);
8755: msdos_putch(0x20);
8756: msdos_putch(0x08);
1.1.1.20 root 8757: }
1.1 root 8758: }
8759: } else if(p < max) {
8760: buf[p++] = chr;
8761: msdos_putch(chr);
8762: }
8763: }
8764: buf[p] = 0x0d;
8765: mem[ofs + 1] = p;
1.1.1.33 root 8766: ctrl_break_detected = ctrl_break_pressed;
1.1.1.26 root 8767:
1.1.1.35 root 8768: #ifdef USE_SERVICE_THREAD
8769: service_exit = true;
8770: #endif
8771: return(0);
8772: }
8773:
8774: inline void msdos_int_21h_0ah()
8775: {
8776: if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
8777: #ifdef USE_SERVICE_THREAD
8778: start_service_loop(msdos_int_21h_0ah_thread);
8779: #else
8780: msdos_int_21h_0ah_thread(NULL);
8781: REQUEST_HARDWRE_UPDATE();
8782: #endif
8783: }
1.1 root 8784: }
8785:
8786: inline void msdos_int_21h_0bh()
8787: {
8788: if(msdos_kbhit()) {
8789: REG8(AL) = 0xff;
8790: } else {
8791: REG8(AL) = 0x00;
1.1.1.14 root 8792: maybe_idle();
1.1 root 8793: }
1.1.1.33 root 8794: ctrl_break_detected = ctrl_break_pressed;
1.1 root 8795: }
8796:
8797: inline void msdos_int_21h_0ch()
8798: {
8799: // clear key buffer
1.1.1.21 root 8800: msdos_stdio_reopen();
8801:
1.1.1.20 root 8802: process_t *process = msdos_process_info_get(current_psp);
8803: int fd = msdos_psp_get_file_table(0, current_psp);
8804:
8805: if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1 root 8806: // stdin is redirected to file
8807: } else {
8808: while(msdos_kbhit()) {
8809: msdos_getch();
8810: }
8811: }
8812:
8813: switch(REG8(AL)) {
8814: case 0x01:
8815: msdos_int_21h_01h();
8816: break;
8817: case 0x06:
8818: msdos_int_21h_06h();
8819: break;
8820: case 0x07:
8821: msdos_int_21h_07h();
8822: break;
8823: case 0x08:
8824: msdos_int_21h_08h();
8825: break;
8826: case 0x0a:
8827: msdos_int_21h_0ah();
8828: break;
8829: default:
1.1.1.22 root 8830: // 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));
8831: // REG16(AX) = 0x01;
8832: // m_CF = 1;
1.1 root 8833: break;
8834: }
8835: }
8836:
8837: inline void msdos_int_21h_0dh()
8838: {
8839: }
8840:
8841: inline void msdos_int_21h_0eh()
8842: {
8843: if(REG8(DL) < 26) {
8844: _chdrive(REG8(DL) + 1);
8845: msdos_cds_update(REG8(DL));
1.1.1.23 root 8846: msdos_sda_update(current_psp);
1.1 root 8847: }
8848: REG8(AL) = 26; // zdrive
8849: }
8850:
1.1.1.14 root 8851: inline void msdos_int_21h_0fh()
8852: {
8853: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
8854: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
8855: char *path = msdos_fcb_path(fcb);
8856: 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 8857:
1.1.1.14 root 8858: if(hFile == INVALID_HANDLE_VALUE) {
8859: REG8(AL) = 0xff;
8860: } else {
8861: REG8(AL) = 0;
8862: fcb->current_block = 0;
8863: fcb->record_size = 128;
8864: fcb->file_size = GetFileSize(hFile, NULL);
8865: fcb->handle = hFile;
8866: fcb->cur_record = 0;
8867: }
8868: }
8869:
8870: inline void msdos_int_21h_10h()
8871: {
8872: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
8873: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
8874:
8875: REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
8876: }
8877:
1.1 root 8878: inline void msdos_int_21h_11h()
8879: {
1.1.1.3 root 8880: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
8881: fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 8882:
8883: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 8884: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
8885: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
8886: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 8887: char *path = msdos_fcb_path(fcb);
8888: WIN32_FIND_DATA fd;
8889:
1.1.1.13 root 8890: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
8891: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
8892: FindClose(dtainfo->find_handle);
8893: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 8894: }
8895: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 8896: dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
8897: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 8898:
1.1.1.14 root 8899: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
8900: dtainfo->allowable_mask &= ~8;
1.1 root 8901: }
1.1.1.14 root 8902: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
8903: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 8904: !msdos_find_file_has_8dot3name(&fd)) {
8905: if(!FindNextFile(dtainfo->find_handle, &fd)) {
8906: FindClose(dtainfo->find_handle);
8907: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 8908: break;
8909: }
8910: }
8911: }
1.1.1.13 root 8912: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 8913: if(ext_fcb->flag == 0xff) {
8914: ext_find->flag = 0xff;
8915: memset(ext_find->reserved, 0, 5);
8916: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
8917: }
8918: find->drive = _getdrive();
1.1.1.13 root 8919: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 8920: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
8921: find->nt_res = 0;
8922: msdos_find_file_conv_local_time(&fd);
8923: find->create_time_ms = 0;
8924: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
8925: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
8926: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
8927: find->cluster_hi = find->cluster_lo = 0;
8928: find->file_size = fd.nFileSizeLow;
8929: REG8(AL) = 0x00;
1.1.1.14 root 8930: } else if(dtainfo->allowable_mask & 8) {
1.1 root 8931: if(ext_fcb->flag == 0xff) {
8932: ext_find->flag = 0xff;
8933: memset(ext_find->reserved, 0, 5);
8934: ext_find->attribute = 8;
8935: }
8936: find->drive = _getdrive();
8937: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
8938: find->attribute = 8;
8939: find->nt_res = 0;
8940: msdos_find_file_conv_local_time(&fd);
8941: find->create_time_ms = 0;
8942: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
8943: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
8944: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
8945: find->cluster_hi = find->cluster_lo = 0;
8946: find->file_size = 0;
1.1.1.14 root 8947: dtainfo->allowable_mask &= ~8;
1.1 root 8948: REG8(AL) = 0x00;
8949: } else {
8950: REG8(AL) = 0xff;
8951: }
8952: }
8953:
8954: inline void msdos_int_21h_12h()
8955: {
1.1.1.3 root 8956: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14 root 8957: // fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 8958:
8959: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 8960: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
8961: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
8962: find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 8963: WIN32_FIND_DATA fd;
8964:
1.1.1.13 root 8965: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
8966: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
8967: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 8968: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 8969: !msdos_find_file_has_8dot3name(&fd)) {
8970: if(!FindNextFile(dtainfo->find_handle, &fd)) {
8971: FindClose(dtainfo->find_handle);
8972: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 8973: break;
8974: }
8975: }
8976: } else {
1.1.1.13 root 8977: FindClose(dtainfo->find_handle);
8978: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 8979: }
8980: }
1.1.1.13 root 8981: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 8982: if(ext_fcb->flag == 0xff) {
8983: ext_find->flag = 0xff;
8984: memset(ext_find->reserved, 0, 5);
8985: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
8986: }
8987: find->drive = _getdrive();
1.1.1.13 root 8988: msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1 root 8989: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
8990: find->nt_res = 0;
8991: msdos_find_file_conv_local_time(&fd);
8992: find->create_time_ms = 0;
8993: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
8994: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
8995: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
8996: find->cluster_hi = find->cluster_lo = 0;
8997: find->file_size = fd.nFileSizeLow;
8998: REG8(AL) = 0x00;
1.1.1.14 root 8999: } else if(dtainfo->allowable_mask & 8) {
1.1 root 9000: if(ext_fcb->flag == 0xff) {
9001: ext_find->flag = 0xff;
9002: memset(ext_find->reserved, 0, 5);
9003: ext_find->attribute = 8;
9004: }
9005: find->drive = _getdrive();
9006: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
9007: find->attribute = 8;
9008: find->nt_res = 0;
9009: msdos_find_file_conv_local_time(&fd);
9010: find->create_time_ms = 0;
9011: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
9012: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
9013: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
9014: find->cluster_hi = find->cluster_lo = 0;
9015: find->file_size = 0;
1.1.1.14 root 9016: dtainfo->allowable_mask &= ~8;
1.1 root 9017: REG8(AL) = 0x00;
9018: } else {
9019: REG8(AL) = 0xff;
9020: }
9021: }
9022:
9023: inline void msdos_int_21h_13h()
9024: {
1.1.1.3 root 9025: if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1 root 9026: REG8(AL) = 0xff;
9027: } else {
9028: REG8(AL) = 0x00;
9029: }
9030: }
9031:
1.1.1.16 root 9032: inline void msdos_int_21h_14h()
9033: {
9034: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9035: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9036: process_t *process = msdos_process_info_get(current_psp);
9037: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9038: DWORD num = 0;
9039:
9040: memset(mem + dta_laddr, 0, fcb->record_size);
9041: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9042: REG8(AL) = 1;
9043: } else {
9044: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9045: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9046: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9047: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
9048: }
9049: }
9050:
9051: inline void msdos_int_21h_15h()
1.1.1.14 root 9052: {
9053: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9054: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16 root 9055: process_t *process = msdos_process_info_get(current_psp);
9056: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9057: DWORD num = 0;
1.1.1.14 root 9058:
1.1.1.16 root 9059: if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9060: REG8(AL) = 1;
9061: } else {
9062: fcb->file_size = GetFileSize(fcb->handle, NULL);
9063: UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
9064: fcb->current_block = (position & 0xffffff) / fcb->record_size;
9065: fcb->cur_record = (position & 0xffffff) % fcb->record_size;
9066: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
9067: }
9068: }
9069:
9070: inline void msdos_int_21h_16h()
9071: {
9072: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9073: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14 root 9074: char *path = msdos_fcb_path(fcb);
9075: 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 9076:
1.1.1.14 root 9077: if(hFile == INVALID_HANDLE_VALUE) {
9078: REG8(AL) = 0xff;
9079: } else {
9080: REG8(AL) = 0;
9081: fcb->current_block = 0;
9082: fcb->record_size = 128;
9083: fcb->file_size = 0;
9084: fcb->handle = hFile;
9085: fcb->cur_record = 0;
9086: }
9087: }
9088:
1.1.1.16 root 9089: inline void msdos_int_21h_17h()
9090: {
9091: ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9092: fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
9093: char *path_src = msdos_fcb_path(fcb_src);
9094: ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
9095: fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
9096: char *path_dst = msdos_fcb_path(fcb_dst);
9097:
9098: if(rename(path_src, path_dst)) {
9099: REG8(AL) = 0xff;
9100: } else {
9101: REG8(AL) = 0;
9102: }
9103: }
9104:
1.1 root 9105: inline void msdos_int_21h_18h()
9106: {
9107: REG8(AL) = 0x00;
9108: }
9109:
9110: inline void msdos_int_21h_19h()
9111: {
9112: REG8(AL) = _getdrive() - 1;
9113: }
9114:
9115: inline void msdos_int_21h_1ah()
9116: {
9117: process_t *process = msdos_process_info_get(current_psp);
9118:
9119: process->dta.w.l = REG16(DX);
1.1.1.3 root 9120: process->dta.w.h = SREG(DS);
1.1.1.23 root 9121: msdos_sda_update(current_psp);
1.1 root 9122: }
9123:
9124: inline void msdos_int_21h_1bh()
9125: {
9126: int drive_num = _getdrive() - 1;
9127: UINT16 seg, ofs;
9128:
9129: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9130: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
9131: REG8(AL) = dpb->highest_sector_num + 1;
9132: REG16(CX) = dpb->bytes_per_sector;
9133: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 9134: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 9135: } else {
9136: REG8(AL) = 0xff;
1.1.1.3 root 9137: m_CF = 1;
1.1 root 9138: }
9139:
9140: }
9141:
9142: inline void msdos_int_21h_1ch()
9143: {
1.1.1.41 root 9144: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1 root 9145: UINT16 seg, ofs;
9146:
9147: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9148: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
9149: REG8(AL) = dpb->highest_sector_num + 1;
9150: REG16(CX) = dpb->bytes_per_sector;
9151: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 9152: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 9153: } else {
9154: REG8(AL) = 0xff;
1.1.1.3 root 9155: m_CF = 1;
1.1 root 9156: }
9157:
9158: }
9159:
9160: inline void msdos_int_21h_1dh()
9161: {
9162: REG8(AL) = 0;
9163: }
9164:
9165: inline void msdos_int_21h_1eh()
9166: {
9167: REG8(AL) = 0;
9168: }
9169:
9170: inline void msdos_int_21h_1fh()
9171: {
9172: int drive_num = _getdrive() - 1;
9173: UINT16 seg, ofs;
9174:
9175: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9176: REG8(AL) = 0;
1.1.1.3 root 9177: SREG(DS) = seg;
9178: i386_load_segment_descriptor(DS);
1.1 root 9179: REG16(BX) = ofs;
9180: } else {
9181: REG8(AL) = 0xff;
1.1.1.3 root 9182: m_CF = 1;
1.1 root 9183: }
9184: }
9185:
9186: inline void msdos_int_21h_20h()
9187: {
9188: REG8(AL) = 0;
9189: }
9190:
1.1.1.14 root 9191: inline void msdos_int_21h_21h()
9192: {
9193: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9194: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9195:
9196: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9197: REG8(AL) = 1;
9198: } else {
9199: process_t *process = msdos_process_info_get(current_psp);
9200: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9201: memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16 root 9202: DWORD num = 0;
1.1.1.14 root 9203: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
9204: REG8(AL) = 1;
9205: } else {
9206: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9207: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 9208: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14 root 9209: }
9210: }
9211: }
9212:
9213: inline void msdos_int_21h_22h()
9214: {
9215: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9216: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9217:
9218: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9219: REG8(AL) = 0xff;
9220: } else {
9221: process_t *process = msdos_process_info_get(current_psp);
9222: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16 root 9223: DWORD num = 0;
1.1.1.14 root 9224: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
9225: fcb->file_size = GetFileSize(fcb->handle, NULL);
9226: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9227: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16 root 9228: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14 root 9229: }
9230: }
9231:
1.1.1.16 root 9232: inline void msdos_int_21h_23h()
9233: {
9234: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9235: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9236: char *path = msdos_fcb_path(fcb);
9237: HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
9238:
9239: if(hFile == INVALID_HANDLE_VALUE) {
9240: REG8(AL) = 0xff;
9241: } else {
9242: UINT32 size = GetFileSize(hFile, NULL);
9243: fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
9244: REG8(AL) = 0;
9245: }
9246: }
9247:
9248: inline void msdos_int_21h_24h()
9249: {
9250: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9251: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9252:
9253: fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
9254: }
9255:
1.1 root 9256: inline void msdos_int_21h_25h()
9257: {
9258: *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3 root 9259: *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1 root 9260: }
9261:
9262: inline void msdos_int_21h_26h()
9263: {
9264: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
9265:
9266: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
9267: psp->first_mcb = REG16(DX) + 16;
9268: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
9269: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
9270: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
9271: psp->parent_psp = 0;
9272: }
9273:
1.1.1.16 root 9274: inline void msdos_int_21h_27h()
9275: {
9276: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9277: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9278:
9279: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9280: REG8(AL) = 1;
9281: } else {
9282: process_t *process = msdos_process_info_get(current_psp);
9283: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9284: memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
9285: DWORD num = 0;
9286: if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
9287: REG8(AL) = 1;
9288: } else {
9289: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9290: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
9291: REG8(AL) = (num == fcb->record_size) ? 0 : 3;
9292: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
9293: }
9294: }
9295: }
9296:
9297: inline void msdos_int_21h_28h()
9298: {
9299: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
9300: fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
9301:
9302: if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
9303: REG8(AL) = 0xff;
9304: } else {
9305: process_t *process = msdos_process_info_get(current_psp);
9306: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
9307: DWORD num = 0;
9308: WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
9309: fcb->file_size = GetFileSize(fcb->handle, NULL);
9310: fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
9311: fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
9312: REG8(AL) = (num == fcb->record_size) ? 0 : 1;
9313: REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
9314: }
9315: }
9316:
1.1 root 9317: inline void msdos_int_21h_29h()
9318: {
1.1.1.20 root 9319: int ofs = 0;//SREG_BASE(DS) + REG16(SI);
9320: char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1 root 9321: UINT8 drv = 0;
9322: char sep_chars[] = ":.;,=+";
9323: char end_chars[] = "\\<>|/\"[]";
9324: char spc_chars[] = " \t";
9325:
1.1.1.20 root 9326: memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
9327: buffer[1023] = 0;
9328: memset(name, 0x20, sizeof(name));
9329: memset(ext, 0x20, sizeof(ext));
9330:
1.1 root 9331: if(REG8(AL) & 1) {
1.1.1.20 root 9332: ofs += strspn((char *)(buffer + ofs), spc_chars);
9333: if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1 root 9334: ofs++;
9335: }
9336: }
1.1.1.20 root 9337: ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1 root 9338:
1.1.1.24 root 9339: if(buffer[ofs + 1] == ':') {
9340: if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
9341: drv = buffer[ofs] - 'a' + 1;
1.1.1.20 root 9342: ofs += 2;
1.1.1.24 root 9343: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
9344: ofs++;
9345: }
9346: } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
9347: drv = buffer[ofs] - 'A' + 1;
1.1 root 9348: ofs += 2;
1.1.1.24 root 9349: if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
9350: ofs++;
9351: }
1.1 root 9352: }
9353: }
1.1.1.20 root 9354: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
9355: UINT8 c = buffer[ofs];
9356: if(is_kanji) {
9357: is_kanji = 0;
9358: } else if(msdos_lead_byte_check(c)) {
9359: is_kanji = 1;
9360: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 9361: break;
9362: } else if(c >= 'a' && c <= 'z') {
9363: c -= 0x20;
9364: }
9365: ofs++;
9366: name[i] = c;
9367: }
1.1.1.20 root 9368: if(buffer[ofs] == '.') {
1.1 root 9369: ofs++;
1.1.1.20 root 9370: for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
9371: UINT8 c = buffer[ofs];
9372: if(is_kanji) {
9373: is_kanji = 0;
9374: } else if(msdos_lead_byte_check(c)) {
9375: is_kanji = 1;
9376: } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1 root 9377: break;
9378: } else if(c >= 'a' && c <= 'z') {
9379: c -= 0x20;
9380: }
9381: ofs++;
9382: ext[i] = c;
9383: }
9384: }
1.1.1.20 root 9385: int si = REG16(SI) + ofs;
1.1.1.3 root 9386: int ds = SREG(DS);
1.1 root 9387: while(si > 0xffff) {
9388: si -= 0x10;
9389: ds++;
9390: }
9391: REG16(SI) = si;
1.1.1.3 root 9392: SREG(DS) = ds;
9393: i386_load_segment_descriptor(DS);
1.1 root 9394:
1.1.1.3 root 9395: UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20 root 9396: if(!(REG8(AL) & 2) || drv != 0) {
9397: fcb[0] = drv;
9398: }
9399: if(!(REG8(AL) & 4) || name[0] != 0x20) {
9400: memcpy(fcb + 1, name, 8);
9401: }
9402: if(!(REG8(AL) & 8) || ext[0] != 0x20) {
9403: memcpy(fcb + 9, ext, 3);
9404: }
9405: for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1 root 9406: if(fcb[i] == '*') {
9407: found_star = 1;
9408: }
9409: if(found_star) {
9410: fcb[i] = '?';
9411: }
9412: }
1.1.1.20 root 9413: for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1 root 9414: if(fcb[i] == '*') {
9415: found_star = 1;
9416: }
9417: if(found_star) {
9418: fcb[i] = '?';
9419: }
9420: }
9421:
9422: if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
9423: if(memchr(fcb + 1, '?', 8 + 3)) {
9424: REG8(AL) = 0x01;
1.1.1.20 root 9425: } else {
9426: REG8(AL) = 0x00;
1.1 root 9427: }
9428: } else {
9429: REG8(AL) = 0xff;
9430: }
9431: }
9432:
9433: inline void msdos_int_21h_2ah()
9434: {
9435: SYSTEMTIME sTime;
9436:
9437: GetLocalTime(&sTime);
9438: REG16(CX) = sTime.wYear;
9439: REG8(DH) = (UINT8)sTime.wMonth;
9440: REG8(DL) = (UINT8)sTime.wDay;
9441: REG8(AL) = (UINT8)sTime.wDayOfWeek;
9442: }
9443:
9444: inline void msdos_int_21h_2bh()
9445: {
1.1.1.14 root 9446: REG8(AL) = 0xff;
1.1 root 9447: }
9448:
9449: inline void msdos_int_21h_2ch()
9450: {
9451: SYSTEMTIME sTime;
9452:
9453: GetLocalTime(&sTime);
9454: REG8(CH) = (UINT8)sTime.wHour;
9455: REG8(CL) = (UINT8)sTime.wMinute;
9456: REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14 root 9457: REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1 root 9458: }
9459:
9460: inline void msdos_int_21h_2dh()
9461: {
9462: REG8(AL) = 0x00;
9463: }
9464:
9465: inline void msdos_int_21h_2eh()
9466: {
9467: process_t *process = msdos_process_info_get(current_psp);
9468:
9469: process->verify = REG8(AL);
9470: }
9471:
9472: inline void msdos_int_21h_2fh()
9473: {
9474: process_t *process = msdos_process_info_get(current_psp);
9475:
9476: REG16(BX) = process->dta.w.l;
1.1.1.3 root 9477: SREG(ES) = process->dta.w.h;
9478: i386_load_segment_descriptor(ES);
1.1 root 9479: }
9480:
9481: inline void msdos_int_21h_30h()
9482: {
9483: // Version Flag / OEM
1.1.1.27 root 9484: if(REG8(AL) == 0x01) {
1.1.1.29 root 9485: #ifdef SUPPORT_HMA
9486: REG16(BX) = 0x0000;
9487: #else
9488: REG16(BX) = 0x1000; // DOS is in HMA
9489: #endif
1.1 root 9490: } else {
1.1.1.27 root 9491: // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
9492: // but this is not correct on Windows 98 SE
9493: // REG16(BX) = 0xff01; // OEM = Microsoft, PC/AT
9494: REG16(BX) = 0xff00; // OEM = Microsoft
1.1 root 9495: }
1.1.1.27 root 9496: REG16(CX) = 0x0000;
1.1.1.30 root 9497: REG8(AL) = dos_major_version; // 7
9498: REG8(AH) = dos_minor_version; // 10
1.1 root 9499: }
9500:
9501: inline void msdos_int_21h_31h()
9502: {
1.1.1.29 root 9503: try {
9504: msdos_mem_realloc(current_psp, REG16(DX), NULL);
9505: } catch(...) {
9506: // recover the broken mcb
9507: int mcb_seg = current_psp - 1;
9508: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 9509:
1.1.1.29 root 9510: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 9511: mcb->mz = 'M';
9512: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
9513:
1.1.1.29 root 9514: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 9515: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 9516: } else {
1.1.1.39 root 9517: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 9518: }
9519: } else {
9520: mcb->mz = 'Z';
1.1.1.30 root 9521: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 9522: }
9523: msdos_mem_realloc(current_psp, REG16(DX), NULL);
9524: }
1.1 root 9525: msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
9526: }
9527:
9528: inline void msdos_int_21h_32h()
9529: {
9530: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
9531: UINT16 seg, ofs;
9532:
9533: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
9534: REG8(AL) = 0;
1.1.1.3 root 9535: SREG(DS) = seg;
9536: i386_load_segment_descriptor(DS);
1.1 root 9537: REG16(BX) = ofs;
9538: } else {
9539: REG8(AL) = 0xff;
1.1.1.3 root 9540: m_CF = 1;
1.1 root 9541: }
9542: }
9543:
9544: inline void msdos_int_21h_33h()
9545: {
9546: char path[MAX_PATH];
9547:
9548: switch(REG8(AL)) {
9549: case 0x00:
1.1.1.33 root 9550: REG8(DL) = ctrl_break_checking;
1.1 root 9551: break;
9552: case 0x01:
1.1.1.33 root 9553: ctrl_break_checking = REG8(DL);
9554: break;
9555: case 0x02:
9556: {
9557: UINT8 old = ctrl_break_checking;
9558: ctrl_break_checking = REG8(DL);
9559: REG8(DL) = old;
9560: }
9561: break;
9562: case 0x03:
9563: case 0x04:
9564: // DOS 4.0+ - Unused
1.1 root 9565: break;
9566: case 0x05:
9567: GetSystemDirectory(path, MAX_PATH);
9568: if(path[0] >= 'a' && path[0] <= 'z') {
9569: REG8(DL) = path[0] - 'a' + 1;
9570: } else {
9571: REG8(DL) = path[0] - 'A' + 1;
9572: }
9573: break;
9574: case 0x06:
1.1.1.2 root 9575: // MS-DOS version (7.10)
1.1 root 9576: REG8(BL) = 7;
1.1.1.2 root 9577: REG8(BH) = 10;
1.1 root 9578: REG8(DL) = 0;
1.1.1.29 root 9579: #ifdef SUPPORT_HMA
9580: REG8(DH) = 0x00;
9581: #else
9582: REG8(DH) = 0x10; // DOS is in HMA
9583: #endif
1.1 root 9584: break;
1.1.1.6 root 9585: case 0x07:
9586: if(REG8(DL) == 0) {
9587: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
9588: } else if(REG8(DL) == 1) {
9589: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
9590: }
9591: break;
1.1 root 9592: default:
1.1.1.22 root 9593: 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 9594: REG16(AX) = 0x01;
1.1.1.3 root 9595: m_CF = 1;
1.1 root 9596: break;
9597: }
9598: }
9599:
1.1.1.23 root 9600: inline void msdos_int_21h_34h()
9601: {
9602: SREG(ES) = SDA_TOP >> 4;
9603: i386_load_segment_descriptor(ES);
9604: REG16(BX) = offsetof(sda_t, indos_flag);;
9605: }
9606:
1.1 root 9607: inline void msdos_int_21h_35h()
9608: {
9609: REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3 root 9610: SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
9611: i386_load_segment_descriptor(ES);
1.1 root 9612: }
9613:
9614: inline void msdos_int_21h_36h()
9615: {
9616: struct _diskfree_t df = {0};
9617:
9618: if(_getdiskfree(REG8(DL), &df) == 0) {
9619: REG16(AX) = (UINT16)df.sectors_per_cluster;
9620: REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13 root 9621: REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
9622: REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1 root 9623: } else {
9624: REG16(AX) = 0xffff;
9625: }
9626: }
9627:
9628: inline void msdos_int_21h_37h()
9629: {
1.1.1.22 root 9630: static UINT8 dev_flag = 0xff;
1.1 root 9631:
9632: switch(REG8(AL)) {
9633: case 0x00:
1.1.1.22 root 9634: {
9635: process_t *process = msdos_process_info_get(current_psp);
9636: REG8(AL) = 0x00;
9637: REG8(DL) = process->switchar;
9638: }
1.1 root 9639: break;
9640: case 0x01:
1.1.1.22 root 9641: {
9642: process_t *process = msdos_process_info_get(current_psp);
9643: REG8(AL) = 0x00;
9644: process->switchar = REG8(DL);
1.1.1.23 root 9645: msdos_sda_update(current_psp);
1.1.1.22 root 9646: }
9647: break;
9648: case 0x02:
9649: REG8(DL) = dev_flag;
9650: break;
9651: case 0x03:
9652: dev_flag = REG8(DL);
9653: break;
9654: case 0xd0:
9655: case 0xd1:
9656: case 0xd2:
9657: case 0xd3:
9658: case 0xd4:
9659: case 0xd5:
9660: case 0xd6:
9661: case 0xd7:
9662: case 0xdc:
9663: case 0xdd:
9664: case 0xde:
9665: case 0xdf:
9666: // diet ???
9667: REG16(AX) = 1;
1.1 root 9668: break;
9669: default:
1.1.1.22 root 9670: 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 9671: REG16(AX) = 1;
9672: break;
9673: }
9674: }
9675:
1.1.1.42! root 9676: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17 root 9677: {
9678: char LCdata[80];
9679:
1.1.1.19 root 9680: ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42! root 9681: GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17 root 9682: ci->currency_dec_digits = atoi(LCdata);
1.1.1.42! root 9683: GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 9684: ci->currency_format = *LCdata - '0';
1.1.1.42! root 9685: GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 9686: ci->date_format = *LCdata - '0';
1.1.1.42! root 9687: GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17 root 9688: memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42! root 9689: GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17 root 9690: *ci->date_sep = *LCdata;
1.1.1.42! root 9691: GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17 root 9692: *ci->dec_sep = *LCdata;
1.1.1.42! root 9693: GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17 root 9694: *ci->list_sep = *LCdata;
1.1.1.42! root 9695: GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17 root 9696: *ci->thou_sep = *LCdata;
1.1.1.42! root 9697: GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17 root 9698: *ci->time_sep = *LCdata;
1.1.1.42! root 9699: GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17 root 9700: if(strchr(LCdata, 'H') != NULL) {
9701: ci->time_format = 1;
9702: }
1.1.1.27 root 9703: ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24 root 9704: ci->case_map.w.h = 0xfffd;
1.1.1.42! root 9705: GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17 root 9706: return atoi(LCdata);
9707: }
9708:
1.1.1.42! root 9709: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
! 9710: {
! 9711: return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
! 9712: }
! 9713:
! 9714: int get_country_info(country_info_t *ci)
! 9715: {
! 9716: return get_country_info(ci, LOCALE_USER_DEFAULT);
! 9717: }
! 9718:
! 9719: #ifndef SUBLANG_SWAHILI
! 9720: #define SUBLANG_SWAHILI 0x01
! 9721: #endif
! 9722: #ifndef SUBLANG_TSWANA_BOTSWANA
! 9723: #define SUBLANG_TSWANA_BOTSWANA 0x02
! 9724: #endif
! 9725: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
! 9726: #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
! 9727: #endif
! 9728: #ifndef LANG_BANGLA
! 9729: #define LANG_BANGLA 0x45
! 9730: #endif
! 9731: #ifndef SUBLANG_BANGLA_BANGLADESH
! 9732: #define SUBLANG_BANGLA_BANGLADESH 0x02
! 9733: #endif
! 9734:
! 9735: static const struct {
! 9736: int code;
! 9737: USHORT usPrimaryLanguage;
! 9738: USHORT usSubLanguage;
! 9739: } country_table[] = {
! 9740: {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US}, // United States
! 9741: {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN}, // Canadian-French
! 9742: {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN}, // Canada (English)
! 9743: {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA}, // Russia
! 9744: {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT}, // Egypt
! 9745: {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA}, // South Africa
! 9746: // {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA}, // South Africa
! 9747: // {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA}, // South Africa
! 9748: // {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA}, // South Africa
! 9749: // {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA}, // South Africa
! 9750: // {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA}, // South Africa
! 9751: {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE}, // Greece
! 9752: {0x01F, LANG_DUTCH, SUBLANG_DUTCH}, // Netherlands
! 9753: // {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS}, // Netherlands
! 9754: {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN}, // Belgium
! 9755: // {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN}, // Belgium
! 9756: {0x021, LANG_FRENCH, SUBLANG_FRENCH}, // France
! 9757: {0x022, LANG_SPANISH, SUBLANG_SPANISH}, // Spain
! 9758: {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria???
! 9759: {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY}, // Hungary (not supported by DR DOS 5.0)
! 9760: {0x027, LANG_ITALIAN, SUBLANG_ITALIAN}, // Italy / San Marino / Vatican City
! 9761: {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA}, // Romania
! 9762: {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS}, // Switzerland / Liechtenstein
! 9763: // {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS}, // Switzerland / Liechtenstein
! 9764: // {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS}, // Switzerland / Liechtenstein
! 9765: {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
! 9766: {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN}, // Austria (DR DOS 5.0)
! 9767: {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK}, // United Kingdom
! 9768: {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK}, // Denmark
! 9769: {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH}, // Sweden
! 9770: // {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN}, // Sweden
! 9771: // {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN}, // Sweden
! 9772: // {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN}, // Sweden
! 9773: {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL}, // Norway
! 9774: // {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK}, // Norway
! 9775: // {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY}, // Norway
! 9776: // {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY}, // Norway
! 9777: // {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY}, // Norway
! 9778: {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND}, // Poland (not supported by DR DOS 5.0)
! 9779: {0x031, LANG_GERMAN, SUBLANG_GERMAN}, // Germany
! 9780: {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU}, // Peru
! 9781: // {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU}, // Peru
! 9782: {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN}, // Mexico
! 9783: {0x035, LANG_SPANISH, SUBLANG_NEUTRAL}, // Cuba
! 9784: {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA}, // Argentina
! 9785: {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN}, // Brazil (not supported by DR DOS 5.0)
! 9786: {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE}, // Chile
! 9787: {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA}, // Columbia
! 9788: {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA}, // Venezuela
! 9789: {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA}, // Malaysia
! 9790: {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS}, // International English / Australia
! 9791: {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA}, // Indonesia / East Timor
! 9792: {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES}, // Philippines
! 9793: {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ}, // New Zealand
! 9794: {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE}, // Singapore
! 9795: // {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE}, // Singapore
! 9796: {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan???
! 9797: {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN}, // Japan (DR DOS 5.0, MS-DOS 5.0+)
! 9798: {0x052, LANG_KOREAN, SUBLANG_KOREAN}, // South Korea (DR DOS 5.0)
! 9799: {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM}, // Vietnam
! 9800: {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED}, // China (MS-DOS 5.0+)
! 9801: {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (MS-DOS 5.0+)
! 9802: {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY}, // Turkey (MS-DOS 5.0+)
! 9803: {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA}, // India
! 9804: {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN}, // Pakistan
! 9805: {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN}, // Afghanistan
! 9806: // {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN}, // Afghanistan
! 9807: {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA}, // Sri Lanka
! 9808: // {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA}, // Sri Lanka
! 9809: {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN}, // Iran
! 9810: {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS}, // Belarus
! 9811: {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND}, // Thailand
! 9812: {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO}, // Morocco
! 9813: {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA}, // Algeria
! 9814: {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA}, // Tunisia
! 9815: {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA}, // Libya
! 9816: {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL}, // Senegal
! 9817: // {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL}, // Senegal
! 9818: {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN}, // Nigeria
! 9819: // {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA}, // Nigeria
! 9820: // {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA}, // Nigeria
! 9821: {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA}, // Ethiopia
! 9822: // {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA}, // Ethiopia
! 9823: {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI}, // Kenya
! 9824: {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE}, // Zimbabwe
! 9825: {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA}, // Botswana
! 9826: {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS}, // Faroe Islands
! 9827: {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND}, // Greenland
! 9828: {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE}, // Portugal
! 9829: {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG}, // Luxembourg
! 9830: // {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG}, // Luxembourg
! 9831: // {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG}, // Luxembourg
! 9832: {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND}, // Ireland
! 9833: // {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND}, // Ireland
! 9834: {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND}, // Iceland
! 9835: {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA}, // Albania
! 9836: {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA}, // Malta
! 9837: {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND}, // Finland
! 9838: // {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND}, // Finland
! 9839: // {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND}, // Finland
! 9840: // {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND}, // Finland
! 9841: {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA}, // Bulgaria
! 9842: {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA}, // Lithuania
! 9843: {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA}, // Latvia
! 9844: {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA}, // Estonia
! 9845: {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN}, // Serbia / Montenegro
! 9846: {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia???
! 9847: {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA}, // Croatia
! 9848: {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA}, // Slovenia
! 9849: {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN}, // Bosnia-Herzegovina (Latin)
! 9850: {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC}, // Bosnia-Herzegovina (Cyrillic)
! 9851: {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA}, // FYR Macedonia
! 9852: {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC}, // Czech Republic
! 9853: {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA}, // Slovakia
! 9854: {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE}, // Belize
! 9855: {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA}, // Guatemala
! 9856: {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR}, // El Salvador
! 9857: {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS}, // Honduras
! 9858: {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA}, // Nicraragua
! 9859: {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA}, // Costa Rica
! 9860: {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA}, // Panama
! 9861: {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA}, // Bolivia
! 9862: {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA}, // Bolivia
! 9863: {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR}, // Ecuador
! 9864: // {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR}, // Ecuador
! 9865: {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY}, // Paraguay
! 9866: {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY}, // Uruguay
! 9867: {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM}, // Brunei Darussalam
! 9868: {0x311, LANG_ARABIC, SUBLANG_NEUTRAL}, // Arabic (Middle East/Saudi Arabia/etc.)
! 9869: {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE}, // Ukraine
! 9870: {0x352, LANG_KOREAN, SUBLANG_KOREAN}, // North Korea
! 9871: {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG}, // Hong Kong
! 9872: {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU}, // Macao
! 9873: {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA}, // Cambodia
! 9874: {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH}, // Bangladesh
! 9875: {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL}, // Taiwan (DOS 6.22+)
! 9876: {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES}, // Maldives
! 9877: {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON}, // Lebanon
! 9878: {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN}, // Jordan
! 9879: {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA}, // Syrian Arab Republic
! 9880: {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ}, // Ireq
! 9881: {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT}, // Kuwait
! 9882: {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA}, // Saudi Arabia
! 9883: {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN}, // Yemen
! 9884: {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN}, // Oman
! 9885: {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE}, // United Arab Emirates
! 9886: {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL}, // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
! 9887: {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN}, // Bahrain
! 9888: {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR}, // Qatar
! 9889: {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA}, // Mongolia
! 9890: // {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC}, // Mongolia
! 9891: {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL}, // Nepal
! 9892: {-1, 0, 0},
! 9893: };
! 9894:
1.1.1.14 root 9895: inline void msdos_int_21h_38h()
9896: {
9897: switch(REG8(AL)) {
9898: case 0x00:
1.1.1.19 root 9899: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14 root 9900: break;
9901: default:
1.1.1.42! root 9902: for(int i = 0;; i++) {
! 9903: if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
! 9904: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
! 9905: break;
! 9906: } else if(country_table[i].code == -1) {
! 9907: // 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));
! 9908: // REG16(AX) = 2;
! 9909: // m_CF = 1;
! 9910: REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
! 9911: break;
! 9912: }
! 9913: }
1.1.1.14 root 9914: break;
9915: }
9916: }
9917:
1.1 root 9918: inline void msdos_int_21h_39h(int lfn)
9919: {
1.1.1.3 root 9920: if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 9921: REG16(AX) = errno;
1.1.1.3 root 9922: m_CF = 1;
1.1 root 9923: }
9924: }
9925:
9926: inline void msdos_int_21h_3ah(int lfn)
9927: {
1.1.1.3 root 9928: if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 9929: REG16(AX) = errno;
1.1.1.3 root 9930: m_CF = 1;
1.1 root 9931: }
9932: }
9933:
9934: inline void msdos_int_21h_3bh(int lfn)
9935: {
1.1.1.3 root 9936: if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17 root 9937: REG16(AX) = 3; // must be 3 (path not found)
1.1.1.3 root 9938: m_CF = 1;
1.1 root 9939: }
9940: }
9941:
9942: inline void msdos_int_21h_3ch()
9943: {
1.1.1.3 root 9944: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 9945: int attr = GetFileAttributes(path);
1.1.1.37 root 9946: int fd = -1;
1.1.1.11 root 9947: UINT16 info;
1.1.1.37 root 9948: int sio_port = 0;
9949: int lpt_port = 0;
1.1 root 9950:
1.1.1.11 root 9951: if(msdos_is_con_path(path)) {
9952: fd = _open("CON", _O_WRONLY | _O_BINARY);
9953: info = 0x80d3;
1.1.1.37 root 9954: } else if((sio_port = msdos_is_comm_path(path)) != 0) {
1.1.1.38 root 9955: fd = _open("NUL", _O_RDWR | _O_BINARY);
1.1.1.14 root 9956: info = 0x80d3;
1.1.1.37 root 9957: msdos_set_comm_params(sio_port, path);
9958: } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
9959: fd = _open("NUL", _O_WRONLY | _O_BINARY);
9960: info = 0xa8c0;
1.1.1.29 root 9961: } else if(msdos_is_device_path(path)) {
1.1.1.20 root 9962: fd = _open("NUL", _O_WRONLY | _O_BINARY);
9963: info = 0x80d3;
1.1 root 9964: } else {
9965: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11 root 9966: info = msdos_drive_number(path);
1.1 root 9967: }
9968: if(fd != -1) {
9969: if(attr == -1) {
9970: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
9971: }
9972: SetFileAttributes(path, attr);
9973: REG16(AX) = fd;
1.1.1.37 root 9974: msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20 root 9975: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 9976: } else {
9977: REG16(AX) = errno;
1.1.1.3 root 9978: m_CF = 1;
1.1 root 9979: }
9980: }
9981:
9982: inline void msdos_int_21h_3dh()
9983: {
1.1.1.3 root 9984: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 9985: int mode = REG8(AL) & 0x03;
1.1.1.37 root 9986: int fd = -1;
1.1.1.11 root 9987: UINT16 info;
1.1.1.37 root 9988: int sio_port = 0;
9989: int lpt_port = 0;
1.1 root 9990:
9991: if(mode < 0x03) {
1.1.1.11 root 9992: if(msdos_is_con_path(path)) {
1.1.1.13 root 9993: fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11 root 9994: info = 0x80d3;
1.1.1.37 root 9995: } else if((sio_port = msdos_is_comm_path(path)) != 0) {
9996: fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14 root 9997: info = 0x80d3;
1.1.1.37 root 9998: msdos_set_comm_params(sio_port, path);
9999: } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
10000: fd = _open("NUL", file_mode[mode].mode);
10001: info = 0xa8c0;
1.1.1.29 root 10002: } else if(msdos_is_device_path(path)) {
1.1.1.20 root 10003: fd = msdos_open("NUL", file_mode[mode].mode);
10004: info = 0x80d3;
1.1.1.11 root 10005: } else {
1.1.1.13 root 10006: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 10007: info = msdos_drive_number(path);
10008: }
1.1 root 10009: if(fd != -1) {
10010: REG16(AX) = fd;
1.1.1.37 root 10011: msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20 root 10012: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 10013: } else {
10014: REG16(AX) = errno;
1.1.1.3 root 10015: m_CF = 1;
1.1 root 10016: }
10017: } else {
10018: REG16(AX) = 0x0c;
1.1.1.3 root 10019: m_CF = 1;
1.1 root 10020: }
10021: }
10022:
10023: inline void msdos_int_21h_3eh()
10024: {
10025: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10026: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10027:
1.1.1.20 root 10028: if(fd < process->max_files && file_handler[fd].valid) {
10029: _close(fd);
10030: msdos_file_handler_close(fd);
10031: msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1 root 10032: } else {
10033: REG16(AX) = 0x06;
1.1.1.3 root 10034: m_CF = 1;
1.1 root 10035: }
10036: }
10037:
1.1.1.35 root 10038: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
10039: {
10040: UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
10041: int max = REG16(CX);
10042: int p = 0;
10043:
10044: while(max > p) {
10045: int chr = msdos_getch();
10046:
10047: if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
10048: p = 0;
10049: buf[p++] = 0x0d;
10050: if(max > p) {
10051: buf[p++] = 0x0a;
10052: }
10053: msdos_putch(0x03);
10054: msdos_putch(0x0d);
10055: msdos_putch(0x0a);
10056: break;
10057: } else if(ctrl_break_pressed) {
10058: // skip this byte
10059: } else if(chr == 0x00) {
10060: // skip 2nd byte
10061: msdos_getch();
10062: } else if(chr == 0x0d) {
10063: // carriage return
10064: buf[p++] = 0x0d;
10065: if(max > p) {
10066: buf[p++] = 0x0a;
10067: }
10068: msdos_putch('\n');
10069: break;
10070: } else if(chr == 0x08) {
10071: // back space
10072: if(p > 0) {
10073: p--;
10074: if(msdos_ctrl_code_check(buf[p])) {
10075: msdos_putch(0x08);
10076: msdos_putch(0x08);
10077: msdos_putch(0x20);
10078: msdos_putch(0x20);
10079: msdos_putch(0x08);
10080: msdos_putch(0x08);
1.1.1.36 root 10081: } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
10082: p--;
10083: msdos_putch(0x08);
10084: msdos_putch(0x08);
10085: msdos_putch(0x20);
10086: msdos_putch(0x20);
10087: msdos_putch(0x08);
10088: msdos_putch(0x08);
1.1.1.35 root 10089: } else {
10090: msdos_putch(0x08);
10091: msdos_putch(0x20);
10092: msdos_putch(0x08);
10093: }
10094: }
10095: } else if(chr == 0x1b) {
10096: // escape
10097: while(p > 0) {
10098: p--;
10099: if(msdos_ctrl_code_check(buf[p])) {
10100: msdos_putch(0x08);
10101: msdos_putch(0x08);
10102: msdos_putch(0x20);
10103: msdos_putch(0x20);
10104: msdos_putch(0x08);
10105: msdos_putch(0x08);
10106: } else {
10107: msdos_putch(0x08);
10108: msdos_putch(0x20);
10109: msdos_putch(0x08);
10110: }
10111: }
10112: } else {
10113: buf[p++] = chr;
10114: msdos_putch(chr);
10115: }
10116: }
10117: REG16(AX) = p;
10118:
10119: #ifdef USE_SERVICE_THREAD
10120: service_exit = true;
10121: #endif
10122: return(0);
10123: }
10124:
1.1 root 10125: inline void msdos_int_21h_3fh()
10126: {
10127: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10128: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10129:
1.1.1.20 root 10130: if(fd < process->max_files && file_handler[fd].valid) {
10131: if(file_mode[file_handler[fd].mode].in) {
10132: if(file_handler[fd].atty) {
1.1 root 10133: // BX is stdin or is redirected to stdin
1.1.1.35 root 10134: if(REG16(CX) != 0) {
10135: #ifdef USE_SERVICE_THREAD
10136: start_service_loop(msdos_int_21h_3fh_thread);
10137: #else
10138: msdos_int_21h_3fh_thread(NULL);
10139: REQUEST_HARDWRE_UPDATE();
10140: #endif
10141: } else {
10142: REG16(AX) = 0;
1.1 root 10143: }
10144: } else {
1.1.1.37 root 10145: REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 10146: }
10147: } else {
10148: REG16(AX) = 0x05;
1.1.1.3 root 10149: m_CF = 1;
1.1 root 10150: }
10151: } else {
10152: REG16(AX) = 0x06;
1.1.1.3 root 10153: m_CF = 1;
1.1 root 10154: }
10155: }
10156:
10157: inline void msdos_int_21h_40h()
10158: {
10159: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10160: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10161:
1.1.1.20 root 10162: if(fd < process->max_files && file_handler[fd].valid) {
10163: if(file_mode[file_handler[fd].mode].out) {
1.1 root 10164: if(REG16(CX)) {
1.1.1.20 root 10165: if(file_handler[fd].atty) {
1.1 root 10166: // BX is stdout/stderr or is redirected to stdout
10167: for(int i = 0; i < REG16(CX); i++) {
1.1.1.3 root 10168: msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1 root 10169: }
10170: REG16(AX) = REG16(CX);
10171: } else {
1.1.1.20 root 10172: REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 10173: }
10174: } else {
1.1.1.20 root 10175: UINT32 pos = _tell(fd);
10176: _lseek(fd, 0, SEEK_END);
10177: UINT32 size = _tell(fd);
1.1.1.12 root 10178: if(pos < size) {
1.1.1.20 root 10179: _lseek(fd, pos, SEEK_SET);
10180: SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12 root 10181: } else {
10182: for(UINT32 i = size; i < pos; i++) {
10183: UINT8 tmp = 0;
1.1.1.23 root 10184: msdos_write(fd, &tmp, 1);
1.1.1.12 root 10185: }
1.1.1.20 root 10186: _lseek(fd, pos, SEEK_SET);
1.1 root 10187: }
1.1.1.23 root 10188: REG16(AX) = 0;
1.1 root 10189: }
10190: } else {
10191: REG16(AX) = 0x05;
1.1.1.3 root 10192: m_CF = 1;
1.1 root 10193: }
10194: } else {
10195: REG16(AX) = 0x06;
1.1.1.3 root 10196: m_CF = 1;
1.1 root 10197: }
10198: }
10199:
10200: inline void msdos_int_21h_41h(int lfn)
10201: {
1.1.1.3 root 10202: if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 10203: REG16(AX) = errno;
1.1.1.3 root 10204: m_CF = 1;
1.1 root 10205: }
10206: }
10207:
10208: inline void msdos_int_21h_42h()
10209: {
10210: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10211: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10212:
1.1.1.20 root 10213: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 10214: if(REG8(AL) < 0x03) {
1.1.1.35 root 10215: static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20 root 10216: _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
10217: UINT32 pos = _tell(fd);
1.1 root 10218: REG16(AX) = pos & 0xffff;
10219: REG16(DX) = (pos >> 16);
10220: } else {
10221: REG16(AX) = 0x01;
1.1.1.3 root 10222: m_CF = 1;
1.1 root 10223: }
10224: } else {
10225: REG16(AX) = 0x06;
1.1.1.3 root 10226: m_CF = 1;
1.1 root 10227: }
10228: }
10229:
10230: inline void msdos_int_21h_43h(int lfn)
10231: {
1.1.1.3 root 10232: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1 root 10233: int attr;
10234:
1.1.1.14 root 10235: if(!lfn && REG8(AL) > 2) {
10236: REG16(AX) = 0x01;
10237: m_CF = 1;
10238: return;
10239: }
10240: switch(REG8(lfn ? BL : AL)) {
1.1 root 10241: case 0x00:
10242: if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14 root 10243: REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
10244: } else {
10245: REG16(AX) = (UINT16)GetLastError();
10246: m_CF = 1;
10247: }
10248: break;
10249: case 0x01:
10250: if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
10251: REG16(AX) = (UINT16)GetLastError();
10252: m_CF = 1;
10253: }
10254: break;
10255: case 0x02:
10256: {
10257: DWORD size = GetCompressedFileSize(path, NULL);
10258: if(size != INVALID_FILE_SIZE) {
10259: if(size != 0 && size == GetFileSize(path, NULL)) {
10260: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
10261: // this isn't correct if the file is in the NTFS MFT
10262: if(GetDiskFreeSpace(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
10263: size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
10264: }
10265: }
10266: REG16(AX) = LOWORD(size);
10267: REG16(DX) = HIWORD(size);
10268: } else {
10269: REG16(AX) = (UINT16)GetLastError();
10270: m_CF = 1;
1.1 root 10271: }
1.1.1.14 root 10272: }
10273: break;
10274: case 0x03:
10275: case 0x05:
10276: case 0x07:
10277: {
10278: HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
10279: if(hFile != INVALID_HANDLE_VALUE) {
10280: FILETIME local, time;
10281: DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
10282: if(REG8(BL) == 7) {
10283: ULARGE_INTEGER hund;
10284: hund.LowPart = local.dwLowDateTime;
10285: hund.HighPart = local.dwHighDateTime;
10286: hund.QuadPart += REG16(SI) * 100000;
10287: local.dwLowDateTime = hund.LowPart;
10288: local.dwHighDateTime = hund.HighPart;
10289: }
10290: LocalFileTimeToFileTime(&local, &time);
10291: if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
10292: REG8(BL) == 0x05 ? &time : NULL,
10293: REG8(BL) == 0x03 ? &time : NULL)) {
10294: REG16(AX) = (UINT16)GetLastError();
10295: m_CF = 1;
10296: }
10297: CloseHandle(hFile);
10298: } else {
10299: REG16(AX) = (UINT16)GetLastError();
10300: m_CF = 1;
1.1 root 10301: }
1.1.1.14 root 10302: }
10303: break;
10304: case 0x04:
10305: case 0x06:
10306: case 0x08:
10307: {
10308: WIN32_FILE_ATTRIBUTE_DATA fad;
10309: if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
10310: FILETIME *time, local;
10311: time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
10312: 0x06 ? &fad.ftLastAccessTime :
10313: &fad.ftCreationTime;
10314: FileTimeToLocalFileTime(time, &local);
10315: FileTimeToDosDateTime(&local, ®16(DI), ®16(CX));
10316: if(REG8(BL) == 0x08) {
10317: ULARGE_INTEGER hund;
10318: hund.LowPart = local.dwLowDateTime;
10319: hund.HighPart = local.dwHighDateTime;
10320: hund.QuadPart /= 100000;
10321: REG16(SI) = (UINT16)(hund.QuadPart % 200);
10322: }
10323: } else {
10324: REG16(AX) = (UINT16)GetLastError();
10325: m_CF = 1;
1.1 root 10326: }
1.1.1.14 root 10327: }
10328: break;
10329: default:
1.1.1.22 root 10330: 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 10331: REG16(AX) = 0x01;
10332: m_CF = 1;
10333: break;
10334: }
10335: }
10336:
10337: inline void msdos_int_21h_44h()
10338: {
1.1.1.22 root 10339: static UINT16 iteration_count = 0;
10340:
1.1.1.20 root 10341: process_t *process = msdos_process_info_get(current_psp);
10342: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
10343:
1.1.1.14 root 10344: UINT32 val = DRIVE_NO_ROOT_DIR;
10345:
10346: switch(REG8(AL)) {
10347: case 0x00:
10348: case 0x01:
10349: case 0x02:
10350: case 0x03:
10351: case 0x04:
10352: case 0x05:
10353: case 0x06:
10354: case 0x07:
1.1.1.20 root 10355: if(fd >= process->max_files || !file_handler[fd].valid) {
10356: REG16(AX) = 0x06;
10357: m_CF = 1;
10358: return;
1.1.1.14 root 10359: }
10360: break;
10361: case 0x08:
10362: case 0x09:
10363: if(REG8(BL) >= ('Z' - 'A' + 1)) {
10364: // invalid drive number
10365: REG16(AX) = 0x0f;
10366: m_CF = 1;
10367: return;
10368: } else {
10369: if(REG8(BL) == 0) {
10370: val = GetDriveType(NULL);
10371: } else {
10372: char tmp[8];
10373: sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
10374: val = GetDriveType(tmp);
10375: }
10376: if(val == DRIVE_NO_ROOT_DIR) {
10377: // no drive
10378: REG16(AX) = 0x0f;
10379: m_CF = 1;
10380: return;
1.1 root 10381: }
10382: }
10383: break;
10384: }
10385: switch(REG8(AL)) {
10386: case 0x00: // get ioctrl data
1.1.1.20 root 10387: REG16(DX) = file_handler[fd].info;
1.1 root 10388: break;
10389: case 0x01: // set ioctrl data
1.1.1.20 root 10390: file_handler[fd].info |= REG8(DL);
1.1 root 10391: break;
10392: case 0x02: // recv from character device
10393: case 0x03: // send to character device
10394: case 0x04: // recv from block device
10395: case 0x05: // send to block device
10396: REG16(AX) = 0x05;
1.1.1.3 root 10397: m_CF = 1;
1.1 root 10398: break;
10399: case 0x06: // get read status
1.1.1.20 root 10400: if(file_mode[file_handler[fd].mode].in) {
10401: if(file_handler[fd].atty) {
1.1.1.14 root 10402: REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1 root 10403: } else {
1.1.1.20 root 10404: REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1 root 10405: }
1.1.1.14 root 10406: } else {
10407: REG8(AL) = 0x00;
1.1 root 10408: }
10409: break;
10410: case 0x07: // get write status
1.1.1.20 root 10411: if(file_mode[file_handler[fd].mode].out) {
1.1.1.14 root 10412: REG8(AL) = 0xff;
10413: } else {
10414: REG8(AL) = 0x00;
1.1 root 10415: }
10416: break;
10417: case 0x08: // check removable drive
1.1.1.14 root 10418: if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
10419: // removable drive
10420: REG16(AX) = 0x00;
1.1 root 10421: } else {
1.1.1.14 root 10422: // fixed drive
10423: REG16(AX) = 0x01;
1.1 root 10424: }
10425: break;
10426: case 0x09: // check remote drive
1.1.1.14 root 10427: if(val == DRIVE_REMOTE) {
10428: // remote drive
10429: REG16(DX) = 0x1000;
1.1 root 10430: } else {
1.1.1.14 root 10431: // local drive
10432: REG16(DX) = 0x00;
1.1 root 10433: }
10434: break;
1.1.1.21 root 10435: case 0x0a: // check remote handle
10436: REG16(DX) = 0x00; // FIXME
10437: break;
1.1 root 10438: case 0x0b: // set retry count
10439: break;
1.1.1.22 root 10440: case 0x0c: // generic character device request
10441: if(REG8(CL) == 0x45) {
10442: // set iteration (retry) count
10443: iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
10444: } else if(REG8(CL) == 0x4a) {
10445: // select code page
10446: active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
10447: msdos_nls_tables_update();
10448: } else if(REG8(CL) == 0x65) {
10449: // get iteration (retry) count
10450: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
10451: } else if(REG8(CL) == 0x6a) {
10452: // query selected code page
10453: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
10454: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
10455:
10456: CPINFO info;
10457: GetCPInfo(active_code_page, &info);
10458:
10459: if(info.MaxCharSize != 1) {
10460: for(int i = 0;; i++) {
10461: UINT8 lo = info.LeadByte[2 * i + 0];
10462: UINT8 hi = info.LeadByte[2 * i + 1];
10463:
10464: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
10465: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
10466: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
10467:
10468: if(lo == 0 && hi == 0) {
10469: break;
10470: }
10471: }
10472: }
10473: } else if(REG8(CL) == 0x7f) {
10474: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
10475: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
10476: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
10477: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
10478: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
10479: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
10480: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
10481: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) = 8 * (*(UINT16 *)(mem + 0x44a));
10482: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8 *)(mem + 0x484) + 1);
10483: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
10484: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8 *)(mem + 0x484) + 1;
10485: } else {
10486: 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));
10487: REG16(AX) = 0x01; // invalid function
10488: m_CF = 1;
10489: }
10490: break;
10491: case 0x0d: // generic block device request
10492: if(REG8(CL) == 0x40) {
10493: // set device parameters
10494: } else if(REG8(CL) == 0x46) {
10495: // set volume serial number
10496: } else if(REG8(CL) == 0x4a) {
10497: // lock logical volume
10498: } else if(REG8(CL) == 0x4b) {
10499: // lock physical volume
10500: } else if(REG8(CL) == 0x60) {
10501: // get device parameters
1.1.1.42! root 10502: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 10503:
1.1.1.42! root 10504: if(pcbios_update_drive_param(drive_num, 1)) {
! 10505: drive_param_t *drive_param = &drive_params[drive_num];
! 10506: DISK_GEOMETRY *geo = &drive_param->geometry;
! 10507:
! 10508: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
! 10509: switch(geo->MediaType) {
! 10510: case F5_360_512:
! 10511: case F5_320_512:
! 10512: case F5_320_1024:
! 10513: case F5_180_512:
! 10514: case F5_160_512:
! 10515: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
! 10516: break;
! 10517: case F5_1Pt2_512:
! 10518: case F3_1Pt2_512:
! 10519: case F3_1Pt23_1024:
! 10520: case F5_1Pt23_1024:
! 10521: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
! 10522: break;
! 10523: case F3_720_512:
! 10524: case F3_640_512:
! 10525: case F5_640_512:
! 10526: case F5_720_512:
! 10527: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
! 10528: break;
! 10529: case F8_256_128:
! 10530: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
! 10531: break;
! 10532: case FixedMedia:
! 10533: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
! 10534: break;
! 10535: case F3_1Pt44_512:
! 10536: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
! 10537: break;
! 10538: case F3_2Pt88_512:
! 10539: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
! 10540: break;
! 10541: default:
! 10542: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
! 10543: // *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
! 10544: break;
1.1.1.22 root 10545: }
1.1.1.42! root 10546: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
! 10547: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
! 10548: switch(geo->MediaType) {
! 10549: case F5_360_512:
! 10550: case F5_320_512:
! 10551: case F5_320_1024:
! 10552: case F5_180_512:
! 10553: case F5_160_512:
! 10554: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
! 10555: break;
! 10556: default:
! 10557: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
! 10558: break;
! 10559: }
! 10560: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
! 10561: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
! 10562: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
! 10563: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
! 10564: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
! 10565: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
! 10566: switch(geo->MediaType) {
! 10567: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
! 10568: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
! 10569: break;
! 10570: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
! 10571: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
! 10572: break;
! 10573: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
! 10574: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
! 10575: break;
! 10576: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
! 10577: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
! 10578: break;
! 10579: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
! 10580: case F3_1Pt2_512:
! 10581: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
! 10582: case F5_720_512:
! 10583: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
! 10584: break;
! 10585: case FixedMedia: // hard disk
! 10586: case RemovableMedia:
! 10587: case Unknown:
! 10588: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
! 10589: break;
! 10590: default:
! 10591: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
! 10592: break;
! 10593: }
! 10594: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
! 10595: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
! 10596: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
! 10597: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
! 10598: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
! 10599: // 21h BYTE device type
! 10600: // 22h WORD device attributes (removable or not, etc)
1.1.1.22 root 10601: } else {
10602: REG16(AX) = 0x0f; // invalid drive
10603: m_CF = 1;
10604: }
10605: } else if(REG8(CL) == 0x66) {
10606: // get volume serial number
10607: char path[] = "A:\\";
10608: char volume_label[MAX_PATH];
10609: DWORD serial_number = 0;
10610: char file_system[MAX_PATH];
10611:
10612: path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
10613:
10614: if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
10615: *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
10616: *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
10617: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
10618: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
10619: memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20, 8);
10620: memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
10621: } else {
10622: REG16(AX) = 0x0f; // invalid drive
10623: m_CF = 1;
10624: }
10625: } else if(REG8(CL) == 0x67) {
10626: // get access flag
10627: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
10628: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
10629: } else if(REG8(CL) == 0x68) {
10630: // sense media type
1.1.1.42! root 10631: int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22 root 10632:
1.1.1.42! root 10633: if(pcbios_update_drive_param(drive_num, 1)) {
! 10634: drive_param_t *drive_param = &drive_params[drive_num];
! 10635: DISK_GEOMETRY *geo = &drive_param->geometry;
! 10636:
! 10637: switch(geo->MediaType) {
! 10638: case F3_720_512:
! 10639: case F5_720_512:
! 10640: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
! 10641: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
! 10642: break;
! 10643: case F3_1Pt44_512:
! 10644: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
! 10645: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
! 10646: break;
! 10647: case F3_2Pt88_512:
! 10648: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
! 10649: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
! 10650: break;
! 10651: default:
! 10652: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
! 10653: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
! 10654: break;
1.1.1.22 root 10655: }
10656: } else {
10657: REG16(AX) = 0x0f; // invalid drive
10658: m_CF = 1;
10659: }
10660: } else if(REG8(CL) == 0x6a) {
10661: // unlock logical volume
10662: } else if(REG8(CL) == 0x6b) {
10663: // unlock physical volume
10664: } else {
10665: 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));
10666: REG16(AX) = 0x01; // invalid function
10667: m_CF = 1;
10668: }
10669: break;
10670: case 0x0e: // get logical drive map
10671: {
10672: DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
10673: if(!(GetLogicalDrives() & bits)) {
10674: REG16(AX) = 0x0f; // invalid drive
10675: m_CF = 1;
10676: } else {
10677: REG8(AL) = 0;
10678: }
10679: }
10680: break;
10681: case 0x0f: // set logical drive map
10682: {
10683: DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
10684: if(!(GetLogicalDrives() & bits)) {
10685: REG16(AX) = 0x0f; // invalid drive
10686: m_CF = 1;
10687: }
10688: }
10689: break;
10690: case 0x10: // query generic ioctrl capability (handle)
10691: switch(REG8(CL)) {
10692: case 0x45:
10693: case 0x4a:
10694: case 0x65:
10695: case 0x6a:
10696: case 0x7f:
10697: REG16(AX) = 0x0000; // supported
10698: break;
10699: default:
10700: REG8(AL) = 0x01; // ioctl capability not available
10701: m_CF = 1;
10702: break;
10703: }
10704: break;
10705: case 0x11: // query generic ioctrl capability (drive)
10706: switch(REG8(CL)) {
10707: case 0x40:
10708: case 0x46:
10709: case 0x4a:
10710: case 0x4b:
10711: case 0x60:
10712: case 0x66:
10713: case 0x67:
10714: case 0x68:
10715: case 0x6a:
10716: case 0x6b:
10717: REG16(AX) = 0x0000; // supported
10718: break;
10719: default:
10720: REG8(AL) = 0x01; // ioctl capability not available
10721: m_CF = 1;
10722: break;
10723: }
10724: break;
10725: case 0x12: // determine dos type
10726: case 0x51: // concurrent dos v3.2+ - installation check
10727: case 0x52: // determine dos type/get dr dos versuin
10728: REG16(AX) = 0x01; // this is not DR-DOS
10729: m_CF = 1;
10730: break;
1.1 root 10731: default:
1.1.1.22 root 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));
1.1 root 10733: REG16(AX) = 0x01;
1.1.1.3 root 10734: m_CF = 1;
1.1 root 10735: break;
10736: }
10737: }
10738:
10739: inline void msdos_int_21h_45h()
10740: {
10741: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10742: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 10743:
1.1.1.20 root 10744: if(fd < process->max_files && file_handler[fd].valid) {
10745: int dup_fd = _dup(fd);
10746: if(dup_fd != -1) {
10747: REG16(AX) = dup_fd;
10748: msdos_file_handler_dup(dup_fd, fd, current_psp);
10749: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
10750: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 10751: } else {
10752: REG16(AX) = errno;
1.1.1.3 root 10753: m_CF = 1;
1.1 root 10754: }
10755: } else {
10756: REG16(AX) = 0x06;
1.1.1.3 root 10757: m_CF = 1;
1.1 root 10758: }
10759: }
10760:
10761: inline void msdos_int_21h_46h()
10762: {
10763: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 10764: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
10765: int dup_fd = REG16(CX);
10766: int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1 root 10767:
1.1.1.20 root 10768: if(REG16(BX) == REG16(CX)) {
10769: REG16(AX) = 0x06;
10770: m_CF = 1;
10771: } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
10772: if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
10773: _close(tmp_fd);
10774: msdos_file_handler_close(tmp_fd);
10775: msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
10776: }
10777: if(_dup2(fd, dup_fd) != -1) {
10778: msdos_file_handler_dup(dup_fd, fd, current_psp);
10779: // msdos_psp_set_file_table(dup_fd, fd, current_psp);
10780: msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1 root 10781: } else {
10782: REG16(AX) = errno;
1.1.1.3 root 10783: m_CF = 1;
1.1 root 10784: }
10785: } else {
10786: REG16(AX) = 0x06;
1.1.1.3 root 10787: m_CF = 1;
1.1 root 10788: }
10789: }
10790:
10791: inline void msdos_int_21h_47h(int lfn)
10792: {
10793: char path[MAX_PATH];
10794:
10795: if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
10796: if(path[1] == ':') {
10797: // the returned path does not include a drive or the initial backslash
1.1.1.3 root 10798: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1 root 10799: } else {
1.1.1.3 root 10800: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1 root 10801: }
10802: } else {
10803: REG16(AX) = errno;
1.1.1.3 root 10804: m_CF = 1;
1.1 root 10805: }
10806: }
10807:
10808: inline void msdos_int_21h_48h()
10809: {
1.1.1.19 root 10810: int seg, umb_linked;
1.1 root 10811:
1.1.1.8 root 10812: if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19 root 10813: // unlink umb not to allocate memory in umb
10814: if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
10815: msdos_mem_unlink_umb();
10816: }
1.1.1.8 root 10817: if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
10818: REG16(AX) = seg;
10819: } else {
10820: REG16(AX) = 0x08;
10821: REG16(BX) = msdos_mem_get_free(first_mcb, 0);
10822: m_CF = 1;
10823: }
1.1.1.19 root 10824: if(umb_linked != 0) {
10825: msdos_mem_link_umb();
10826: }
1.1.1.8 root 10827: } else if((malloc_strategy & 0xf0) == 0x40) {
10828: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
10829: REG16(AX) = seg;
10830: } else {
10831: REG16(AX) = 0x08;
10832: REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
10833: m_CF = 1;
10834: }
10835: } else if((malloc_strategy & 0xf0) == 0x80) {
10836: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
10837: REG16(AX) = seg;
10838: } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
10839: REG16(AX) = seg;
10840: } else {
10841: REG16(AX) = 0x08;
10842: REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
10843: m_CF = 1;
10844: }
1.1 root 10845: }
10846: }
10847:
10848: inline void msdos_int_21h_49h()
10849: {
1.1.1.14 root 10850: int mcb_seg = SREG(ES) - 1;
10851: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
10852:
10853: if(mcb->mz == 'M' || mcb->mz == 'Z') {
10854: msdos_mem_free(SREG(ES));
10855: } else {
1.1.1.33 root 10856: REG16(AX) = 0x09; // illegal memory block address
1.1.1.14 root 10857: m_CF = 1;
10858: }
1.1 root 10859: }
10860:
10861: inline void msdos_int_21h_4ah()
10862: {
1.1.1.14 root 10863: int mcb_seg = SREG(ES) - 1;
10864: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1 root 10865: int max_paragraphs;
10866:
1.1.1.14 root 10867: if(mcb->mz == 'M' || mcb->mz == 'Z') {
10868: if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
10869: REG16(AX) = 0x08;
10870: REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
10871: m_CF = 1;
10872: }
10873: } else {
1.1.1.33 root 10874: REG16(AX) = 0x09; // illegal memory block address
1.1.1.3 root 10875: m_CF = 1;
1.1 root 10876: }
10877: }
10878:
10879: inline void msdos_int_21h_4bh()
10880: {
1.1.1.3 root 10881: char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
10882: param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1 root 10883:
10884: switch(REG8(AL)) {
10885: case 0x00:
10886: case 0x01:
10887: if(msdos_process_exec(command, param, REG8(AL))) {
10888: REG16(AX) = 0x02;
1.1.1.3 root 10889: m_CF = 1;
1.1 root 10890: }
10891: break;
1.1.1.14 root 10892: case 0x03:
10893: {
10894: int fd;
10895: if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
10896: REG16(AX) = 0x02;
10897: m_CF = 1;
10898: break;
10899: }
10900: int size = _read(fd, file_buffer, sizeof(file_buffer));
10901: _close(fd);
10902:
10903: UINT16 *overlay = (UINT16 *)param;
10904:
10905: // check exe header
10906: exe_header_t *header = (exe_header_t *)file_buffer;
10907: int header_size = 0;
10908: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
10909: header_size = header->header_size * 16;
10910: // relocation
10911: int start_seg = overlay[1];
10912: for(int i = 0; i < header->relocations; i++) {
10913: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
10914: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
10915: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
10916: }
10917: }
10918: memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
10919: }
10920: break;
1.1 root 10921: default:
1.1.1.22 root 10922: 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 10923: REG16(AX) = 0x01;
1.1.1.3 root 10924: m_CF = 1;
1.1 root 10925: break;
10926: }
10927: }
10928:
10929: inline void msdos_int_21h_4ch()
10930: {
10931: msdos_process_terminate(current_psp, REG8(AL), 1);
10932: }
10933:
10934: inline void msdos_int_21h_4dh()
10935: {
10936: REG16(AX) = retval;
10937: }
10938:
10939: inline void msdos_int_21h_4eh()
10940: {
10941: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 10942: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
10943: find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3 root 10944: char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 10945: WIN32_FIND_DATA fd;
10946:
1.1.1.14 root 10947: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
10948: find->find_magic = FIND_MAGIC;
10949: find->dta_index = dtainfo - dtalist;
1.1 root 10950: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 10951: dtainfo->allowable_mask = REG8(CL);
10952: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 10953:
1.1.1.14 root 10954: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
10955: dtainfo->allowable_mask &= ~8;
1.1 root 10956: }
1.1.1.14 root 10957: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
10958: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 10959: !msdos_find_file_has_8dot3name(&fd)) {
10960: if(!FindNextFile(dtainfo->find_handle, &fd)) {
10961: FindClose(dtainfo->find_handle);
10962: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 10963: break;
10964: }
10965: }
10966: }
1.1.1.13 root 10967: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 10968: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
10969: msdos_find_file_conv_local_time(&fd);
10970: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
10971: find->size = fd.nFileSizeLow;
1.1.1.13 root 10972: strcpy(find->name, msdos_short_name(&fd));
1.1 root 10973: REG16(AX) = 0;
1.1.1.14 root 10974: } else if(dtainfo->allowable_mask & 8) {
1.1 root 10975: find->attrib = 8;
10976: find->size = 0;
10977: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 10978: dtainfo->allowable_mask &= ~8;
1.1 root 10979: REG16(AX) = 0;
10980: } else {
10981: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 10982: m_CF = 1;
1.1 root 10983: }
10984: }
10985:
10986: inline void msdos_int_21h_4fh()
10987: {
10988: process_t *process = msdos_process_info_get(current_psp);
1.1.1.13 root 10989: UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
10990: find_t *find = (find_t *)(mem + dta_laddr);
1.1 root 10991: WIN32_FIND_DATA fd;
10992:
1.1.1.14 root 10993: if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
10994: REG16(AX) = 0x12;
10995: m_CF = 1;
10996: return;
10997: }
10998: dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13 root 10999: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
11000: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 11001: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13 root 11002: !msdos_find_file_has_8dot3name(&fd)) {
11003: if(!FindNextFile(dtainfo->find_handle, &fd)) {
11004: FindClose(dtainfo->find_handle);
11005: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11006: break;
11007: }
11008: }
11009: } else {
1.1.1.13 root 11010: FindClose(dtainfo->find_handle);
11011: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11012: }
11013: }
1.1.1.13 root 11014: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 11015: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
11016: msdos_find_file_conv_local_time(&fd);
11017: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
11018: find->size = fd.nFileSizeLow;
1.1.1.13 root 11019: strcpy(find->name, msdos_short_name(&fd));
1.1 root 11020: REG16(AX) = 0;
1.1.1.14 root 11021: } else if(dtainfo->allowable_mask & 8) {
1.1 root 11022: find->attrib = 8;
11023: find->size = 0;
11024: strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 11025: dtainfo->allowable_mask &= ~8;
1.1 root 11026: REG16(AX) = 0;
11027: } else {
11028: REG16(AX) = 0x12;
1.1.1.3 root 11029: m_CF = 1;
1.1 root 11030: }
11031: }
11032:
11033: inline void msdos_int_21h_50h()
11034: {
1.1.1.8 root 11035: if(current_psp != REG16(BX)) {
11036: process_t *process = msdos_process_info_get(current_psp);
11037: if(process != NULL) {
11038: process->psp = REG16(BX);
11039: }
11040: current_psp = REG16(BX);
1.1.1.23 root 11041: msdos_sda_update(current_psp);
1.1.1.8 root 11042: }
1.1 root 11043: }
11044:
11045: inline void msdos_int_21h_51h()
11046: {
11047: REG16(BX) = current_psp;
11048: }
11049:
11050: inline void msdos_int_21h_52h()
11051: {
1.1.1.25 root 11052: SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3 root 11053: i386_load_segment_descriptor(ES);
1.1.1.25 root 11054: REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1 root 11055: }
11056:
11057: inline void msdos_int_21h_54h()
11058: {
11059: process_t *process = msdos_process_info_get(current_psp);
11060:
11061: REG8(AL) = process->verify;
11062: }
11063:
11064: inline void msdos_int_21h_55h()
11065: {
11066: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
11067:
11068: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
11069: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
11070: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
11071: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
11072: psp->parent_psp = current_psp;
11073: }
11074:
11075: inline void msdos_int_21h_56h(int lfn)
11076: {
11077: char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3 root 11078: strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
11079: strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1 root 11080:
11081: if(rename(src, dst)) {
11082: REG16(AX) = errno;
1.1.1.3 root 11083: m_CF = 1;
1.1 root 11084: }
11085: }
11086:
11087: inline void msdos_int_21h_57h()
11088: {
11089: FILETIME time, local;
1.1.1.14 root 11090: FILETIME *ctime, *atime, *mtime;
1.1.1.21 root 11091: HANDLE hHandle;
1.1 root 11092:
1.1.1.21 root 11093: if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14 root 11094: REG16(AX) = (UINT16)GetLastError();
11095: m_CF = 1;
11096: return;
11097: }
11098: ctime = atime = mtime = NULL;
11099:
1.1 root 11100: switch(REG8(AL)) {
11101: case 0x00:
1.1.1.6 root 11102: case 0x01:
1.1.1.14 root 11103: mtime = &time;
1.1.1.6 root 11104: break;
11105: case 0x04:
11106: case 0x05:
1.1.1.14 root 11107: atime = &time;
1.1 root 11108: break;
1.1.1.6 root 11109: case 0x06:
11110: case 0x07:
1.1.1.14 root 11111: ctime = &time;
11112: break;
11113: default:
1.1.1.22 root 11114: 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 11115: REG16(AX) = 0x01;
11116: m_CF = 1;
11117: return;
11118: }
11119: if(REG8(AL) & 1) {
1.1 root 11120: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
11121: LocalFileTimeToFileTime(&local, &time);
1.1.1.21 root 11122: if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1 root 11123: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 11124: m_CF = 1;
1.1 root 11125: }
1.1.1.14 root 11126: } else {
1.1.1.21 root 11127: if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14 root 11128: // assume a device and use the current time
11129: GetSystemTimeAsFileTime(&time);
11130: }
11131: FileTimeToLocalFileTime(&time, &local);
11132: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
1.1 root 11133: }
11134: }
11135:
11136: inline void msdos_int_21h_58h()
11137: {
11138: switch(REG8(AL)) {
11139: case 0x00:
1.1.1.7 root 11140: REG16(AX) = malloc_strategy;
11141: break;
11142: case 0x01:
1.1.1.24 root 11143: // switch(REG16(BX)) {
11144: switch(REG8(BL)) {
1.1.1.7 root 11145: case 0x0000:
11146: case 0x0001:
11147: case 0x0002:
11148: case 0x0040:
11149: case 0x0041:
11150: case 0x0042:
11151: case 0x0080:
11152: case 0x0081:
11153: case 0x0082:
11154: malloc_strategy = REG16(BX);
1.1.1.23 root 11155: msdos_sda_update(current_psp);
1.1.1.7 root 11156: break;
11157: default:
1.1.1.22 root 11158: 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 11159: REG16(AX) = 0x01;
11160: m_CF = 1;
11161: break;
11162: }
11163: break;
11164: case 0x02:
1.1.1.19 root 11165: REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7 root 11166: break;
11167: case 0x03:
1.1.1.24 root 11168: // switch(REG16(BX)) {
11169: switch(REG8(BL)) {
1.1.1.7 root 11170: case 0x0000:
1.1.1.19 root 11171: msdos_mem_unlink_umb();
11172: break;
1.1.1.7 root 11173: case 0x0001:
1.1.1.19 root 11174: msdos_mem_link_umb();
1.1.1.7 root 11175: break;
11176: default:
1.1.1.22 root 11177: 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 11178: REG16(AX) = 0x01;
11179: m_CF = 1;
11180: break;
11181: }
1.1 root 11182: break;
11183: default:
1.1.1.22 root 11184: 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 11185: REG16(AX) = 0x01;
1.1.1.3 root 11186: m_CF = 1;
1.1 root 11187: break;
11188: }
11189: }
11190:
11191: inline void msdos_int_21h_59h()
11192: {
1.1.1.23 root 11193: sda_t *sda = (sda_t *)(mem + SDA_TOP);
11194:
11195: REG16(AX) = sda->extended_error_code;
11196: REG8(BH) = sda->error_class;
11197: REG8(BL) = sda->suggested_action;
11198: REG8(CH) = sda->locus_of_last_error;
1.1 root 11199: }
11200:
11201: inline void msdos_int_21h_5ah()
11202: {
1.1.1.3 root 11203: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 11204: int len = strlen(path);
11205: char tmp[MAX_PATH];
11206:
11207: if(GetTempFileName(path, "TMP", 0, tmp)) {
11208: int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
11209:
11210: SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
11211: REG16(AX) = fd;
11212: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 11213: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11214:
11215: strcpy(path, tmp);
11216: int dx = REG16(DX) + len;
1.1.1.3 root 11217: int ds = SREG(DS);
1.1 root 11218: while(dx > 0xffff) {
11219: dx -= 0x10;
11220: ds++;
11221: }
11222: REG16(DX) = dx;
1.1.1.3 root 11223: SREG(DS) = ds;
11224: i386_load_segment_descriptor(DS);
1.1 root 11225: } else {
11226: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 11227: m_CF = 1;
1.1 root 11228: }
11229: }
11230:
11231: inline void msdos_int_21h_5bh()
11232: {
1.1.1.3 root 11233: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 11234:
1.1.1.24 root 11235: if(msdos_is_existing_file(path)) {
1.1 root 11236: // already exists
11237: REG16(AX) = 0x50;
1.1.1.3 root 11238: m_CF = 1;
1.1 root 11239: } else {
11240: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
11241:
11242: if(fd != -1) {
11243: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
11244: REG16(AX) = fd;
11245: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 11246: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11247: } else {
11248: REG16(AX) = errno;
1.1.1.3 root 11249: m_CF = 1;
1.1 root 11250: }
11251: }
11252: }
11253:
11254: inline void msdos_int_21h_5ch()
11255: {
11256: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11257: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11258:
1.1.1.20 root 11259: if(fd < process->max_files && file_handler[fd].valid) {
1.1 root 11260: if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35 root 11261: static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20 root 11262: UINT32 pos = _tell(fd);
11263: _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
11264: if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1 root 11265: REG16(AX) = errno;
1.1.1.3 root 11266: m_CF = 1;
1.1 root 11267: }
1.1.1.20 root 11268: _lseek(fd, pos, SEEK_SET);
1.1.1.26 root 11269:
1.1 root 11270: // some seconds may be passed in _locking()
1.1.1.35 root 11271: REQUEST_HARDWRE_UPDATE();
1.1 root 11272: } else {
11273: REG16(AX) = 0x01;
1.1.1.3 root 11274: m_CF = 1;
1.1 root 11275: }
11276: } else {
11277: REG16(AX) = 0x06;
1.1.1.3 root 11278: m_CF = 1;
1.1 root 11279: }
11280: }
11281:
1.1.1.22 root 11282: inline void msdos_int_21h_5dh()
11283: {
11284: switch(REG8(AL)) {
11285: case 0x06: // get address of dos swappable data area
1.1.1.23 root 11286: SREG(DS) = (SDA_TOP >> 4);
11287: i386_load_segment_descriptor(DS);
11288: REG16(SI) = offsetof(sda_t, crit_error_flag);
11289: REG16(CX) = 0x80;
11290: REG16(DX) = 0x1a;
11291: break;
11292: case 0x0b: // get dos swappable data areas
1.1.1.22 root 11293: REG16(AX) = 0x01;
11294: m_CF = 1;
11295: break;
11296: case 0x08: // set redirected printer mode
11297: case 0x09: // flush redirected printer output
11298: case 0x0a: // set extended error information
11299: break;
11300: default:
11301: unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
11302: REG16(AX) = 0x01;
11303: m_CF = 1;
11304: break;
11305: }
11306: }
11307:
1.1.1.42! root 11308: inline void msdos_int_21h_5eh()
! 11309: {
! 11310: switch(REG8(AL)) {
! 11311: case 0x00:
! 11312: {
! 11313: char name[256] = {0};
! 11314: DWORD dwSize = 256;
! 11315:
! 11316: if(GetComputerName(name, &dwSize)) {
! 11317: char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
! 11318: for(int i = 0; i < 15; i++) {
! 11319: dest[i] = (i < strlen(name)) ? name[i] : ' ';
! 11320: }
! 11321: dest[15] = '\0';
! 11322: REG8(CH) = 0x01; // nonzero valid
! 11323: REG8(CL) = 0x01; // NetBIOS number for machine name ???
! 11324: } else {
! 11325: REG16(AX) = 0x01;
! 11326: m_CF = 1;
! 11327: }
! 11328: }
! 11329: break;
! 11330: default:
! 11331: 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));
! 11332: REG16(AX) = 0x01;
! 11333: m_CF = 1;
! 11334: break;
! 11335: }
! 11336: }
! 11337:
1.1.1.30 root 11338: inline void msdos_int_21h_5fh()
11339: {
11340: switch(REG8(AL)) {
1.1.1.42! root 11341: case 0x05:
! 11342: {
! 11343: DWORD drives = GetLogicalDrives();
! 11344: UINT16 count = 0;
! 11345: for(int i = 0; i < 26; i++) {
! 11346: if(drives & (1 << i)) {
! 11347: char volume[] = "A:\\";
! 11348: volume[0] = 'A' + i;
! 11349: if(GetDriveType(volume) == DRIVE_REMOTE) {
! 11350: count++;
! 11351: }
! 11352: }
! 11353: }
! 11354: REG16(BP) = count;
! 11355: }
1.1.1.30 root 11356: case 0x02:
11357: {
11358: DWORD drives = GetLogicalDrives();
11359: for(int i = 0, index = 0; i < 26; i++) {
11360: if(drives & (1 << i)) {
11361: char volume[] = "A:\\";
11362: volume[0] = 'A' + i;
11363: if(GetDriveType(volume) == DRIVE_REMOTE) {
11364: if(index == REG16(BX)) {
11365: DWORD dwSize = 128;
11366: volume[2] = '\0';
11367: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
11368: WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
11369: REG8(BH) = 0x00; // valid
11370: REG8(BL) = 0x04; // disk drive
1.1.1.42! root 11371: REG16(CX) = 0x00; // LANtastic
1.1.1.30 root 11372: return;
11373: }
11374: index++;
11375: }
11376: }
11377: }
11378: }
11379: REG16(AX) = 0x12; // no more files
11380: m_CF = 1;
11381: break;
11382: default:
11383: 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));
11384: REG16(AX) = 0x01;
11385: m_CF = 1;
11386: break;
11387: }
11388: }
11389:
1.1 root 11390: inline void msdos_int_21h_60h(int lfn)
11391: {
1.1.1.14 root 11392: char full[MAX_PATH], *path;
11393:
1.1 root 11394: if(lfn) {
1.1.1.14 root 11395: char *name;
11396: *full = '\0';
1.1.1.3 root 11397: GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14 root 11398: switch(REG8(CL)) {
11399: case 1:
11400: GetShortPathName(full, full, MAX_PATH);
11401: my_strupr(full);
11402: break;
11403: case 2:
11404: GetLongPathName(full, full, MAX_PATH);
11405: break;
11406: }
11407: path = full;
11408: } else {
11409: path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
11410: }
11411: if(*path != '\0') {
11412: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1 root 11413: } else {
1.1.1.14 root 11414: REG16(AX) = (UINT16)GetLastError();
11415: m_CF = 1;
1.1 root 11416: }
11417: }
11418:
11419: inline void msdos_int_21h_61h()
11420: {
11421: REG8(AL) = 0;
11422: }
11423:
11424: inline void msdos_int_21h_62h()
11425: {
11426: REG16(BX) = current_psp;
11427: }
11428:
11429: inline void msdos_int_21h_63h()
11430: {
11431: switch(REG8(AL)) {
11432: case 0x00:
1.1.1.3 root 11433: SREG(DS) = (DBCS_TABLE >> 4);
11434: i386_load_segment_descriptor(DS);
1.1 root 11435: REG16(SI) = (DBCS_TABLE & 0x0f);
11436: REG8(AL) = 0x00;
11437: break;
1.1.1.22 root 11438: case 0x01: // set korean input mode
11439: case 0x02: // get korean input mode
11440: REG8(AL) = 0xff; // not supported
11441: break;
1.1 root 11442: default:
1.1.1.22 root 11443: 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 11444: REG16(AX) = 0x01;
1.1.1.3 root 11445: m_CF = 1;
1.1 root 11446: break;
11447: }
11448: }
11449:
1.1.1.25 root 11450: UINT16 get_extended_country_info(UINT8 func)
1.1 root 11451: {
1.1.1.25 root 11452: switch(func) {
1.1.1.17 root 11453: case 0x01:
11454: if(REG16(CX) >= 5) {
1.1.1.19 root 11455: UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17 root 11456: if(REG16(CX) > sizeof(data)) // cx = actual transfer size
11457: REG16(CX) = sizeof(data);
11458: ZeroMemory(data, sizeof(data));
11459: data[0] = 0x01;
11460: *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19 root 11461: *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17 root 11462: *(UINT16 *)(data + 5) = active_code_page;
11463: memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25 root 11464: // REG16(AX) = active_code_page;
1.1.1.17 root 11465: } else {
1.1.1.25 root 11466: return(0x08); // insufficient memory
1.1.1.17 root 11467: }
11468: break;
11469: case 0x02:
11470: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
11471: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
11472: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25 root 11473: // REG16(AX) = active_code_page;
1.1.1.17 root 11474: REG16(CX) = 0x05;
11475: break;
1.1.1.23 root 11476: case 0x03:
11477: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
11478: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
11479: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25 root 11480: // REG16(AX) = active_code_page;
1.1.1.23 root 11481: REG16(CX) = 0x05;
11482: break;
1.1.1.17 root 11483: case 0x04:
11484: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
11485: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
11486: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25 root 11487: // REG16(AX) = active_code_page;
1.1.1.17 root 11488: REG16(CX) = 0x05;
11489: break;
11490: case 0x05:
11491: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
11492: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
11493: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25 root 11494: // REG16(AX) = active_code_page;
1.1.1.17 root 11495: REG16(CX) = 0x05;
11496: break;
11497: case 0x06:
11498: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
11499: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
11500: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25 root 11501: // REG16(AX) = active_code_page;
1.1.1.17 root 11502: REG16(CX) = 0x05;
11503: break;
1.1 root 11504: case 0x07:
1.1.1.3 root 11505: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
11506: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
11507: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25 root 11508: // REG16(AX) = active_code_page;
1.1 root 11509: REG16(CX) = 0x05;
11510: break;
1.1.1.25 root 11511: default:
11512: return(0x01); // function number invalid
11513: }
11514: return(0x00);
11515: }
11516:
11517: inline void msdos_int_21h_65h()
11518: {
11519: char tmp[0x10000];
11520:
11521: switch(REG8(AL)) {
11522: case 0x01:
11523: case 0x02:
11524: case 0x03:
11525: case 0x04:
11526: case 0x05:
11527: case 0x06:
11528: case 0x07:
11529: {
11530: UINT16 result = get_extended_country_info(REG8(AL));
11531: if(result) {
11532: REG16(AX) = result;
11533: m_CF = 1;
11534: } else {
11535: REG16(AX) = active_code_page; // FIXME: is this correct???
11536: }
11537: }
11538: break;
1.1 root 11539: case 0x20:
1.1.1.25 root 11540: case 0xa0:
1.1.1.19 root 11541: memset(tmp, 0, sizeof(tmp));
11542: tmp[0] = REG8(DL);
1.1 root 11543: my_strupr(tmp);
11544: REG8(DL) = tmp[0];
11545: break;
11546: case 0x21:
1.1.1.25 root 11547: case 0xa1:
1.1 root 11548: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 11549: memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 11550: my_strupr(tmp);
1.1.1.3 root 11551: memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1 root 11552: break;
11553: case 0x22:
1.1.1.25 root 11554: case 0xa2:
1.1.1.3 root 11555: my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1 root 11556: break;
1.1.1.25 root 11557: case 0x23:
11558: // FIXME: need to check multi-byte (kanji) charactre?
11559: if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
11560: // 8278h/8299h: multi-byte (kanji) Y and y
11561: REG16(AX) = 0x00;
11562: } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
11563: // 826dh/828eh: multi-byte (kanji) N and n
11564: REG16(AX) = 0x01;
11565: } else {
11566: REG16(AX) = 0x02;
11567: }
11568: break;
1.1 root 11569: default:
1.1.1.22 root 11570: 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 11571: REG16(AX) = 0x01;
1.1.1.3 root 11572: m_CF = 1;
1.1 root 11573: break;
11574: }
11575: }
11576:
11577: inline void msdos_int_21h_66h()
11578: {
11579: switch(REG8(AL)) {
11580: case 0x01:
11581: REG16(BX) = active_code_page;
11582: REG16(DX) = system_code_page;
11583: break;
11584: case 0x02:
11585: if(active_code_page == REG16(BX)) {
11586: REG16(AX) = 0xeb41;
11587: } else if(_setmbcp(REG16(BX)) == 0) {
11588: active_code_page = REG16(BX);
1.1.1.17 root 11589: msdos_nls_tables_update();
1.1 root 11590: REG16(AX) = 0xeb41;
1.1.1.32 root 11591: SetConsoleCP(active_code_page);
11592: SetConsoleOutputCP(active_code_page);
1.1 root 11593: } else {
11594: REG16(AX) = 0x25;
1.1.1.3 root 11595: m_CF = 1;
1.1 root 11596: }
11597: break;
11598: default:
1.1.1.22 root 11599: 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 11600: REG16(AX) = 0x01;
1.1.1.3 root 11601: m_CF = 1;
1.1 root 11602: break;
11603: }
11604: }
11605:
11606: inline void msdos_int_21h_67h()
11607: {
11608: process_t *process = msdos_process_info_get(current_psp);
11609:
11610: if(REG16(BX) <= MAX_FILES) {
11611: process->max_files = max(REG16(BX), 20);
11612: } else {
11613: REG16(AX) = 0x08;
1.1.1.3 root 11614: m_CF = 1;
1.1 root 11615: }
11616: }
11617:
11618: inline void msdos_int_21h_68h()
11619: {
11620: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 11621: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1 root 11622:
1.1.1.20 root 11623: if(fd < process->max_files && file_handler[fd].valid) {
11624: // fflush(_fdopen(fd, ""));
1.1 root 11625: } else {
11626: REG16(AX) = 0x06;
1.1.1.3 root 11627: m_CF = 1;
1.1 root 11628: }
11629: }
11630:
11631: inline void msdos_int_21h_69h()
11632: {
1.1.1.3 root 11633: drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 11634: char path[] = "A:\\";
11635: char volume_label[MAX_PATH];
11636: DWORD serial_number = 0;
11637: char file_system[MAX_PATH];
11638:
11639: if(REG8(BL) == 0) {
11640: path[0] = 'A' + _getdrive() - 1;
11641: } else {
11642: path[0] = 'A' + REG8(BL) - 1;
11643: }
11644:
11645: switch(REG8(AL)) {
11646: case 0x00:
11647: if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
11648: info->info_level = 0;
11649: info->serial_number = serial_number;
11650: memset(info->volume_label, 0x20, 11);
11651: memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
11652: memset(info->file_system, 0x20, 8);
11653: memcpy(info->file_system, file_system, min(strlen(file_system), 8));
11654: } else {
11655: REG16(AX) = errno;
1.1.1.3 root 11656: m_CF = 1;
1.1 root 11657: }
11658: break;
11659: case 0x01:
11660: REG16(AX) = 0x03;
1.1.1.3 root 11661: m_CF = 1;
1.1 root 11662: }
11663: }
11664:
11665: inline void msdos_int_21h_6ah()
11666: {
11667: REG8(AH) = 0x68;
11668: msdos_int_21h_68h();
11669: }
11670:
11671: inline void msdos_int_21h_6bh()
11672: {
11673: REG8(AL) = 0;
11674: }
11675:
11676: inline void msdos_int_21h_6ch(int lfn)
11677: {
1.1.1.3 root 11678: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1 root 11679: int mode = REG8(BL) & 0x03;
11680:
11681: if(mode < 0x03) {
1.1.1.29 root 11682: if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1 root 11683: // file exists
11684: if(REG8(DL) & 1) {
1.1.1.37 root 11685: int fd = -1;
1.1.1.11 root 11686: UINT16 info;
1.1.1.37 root 11687: int sio_port = 0;
11688: int lpt_port = 0;
1.1 root 11689:
1.1.1.11 root 11690: if(msdos_is_con_path(path)) {
1.1.1.13 root 11691: fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11 root 11692: info = 0x80d3;
1.1.1.37 root 11693: } else if((sio_port = msdos_is_comm_path(path)) != 0) {
11694: fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14 root 11695: info = 0x80d3;
1.1.1.37 root 11696: msdos_set_comm_params(sio_port, path);
11697: } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
11698: fd = msdos_open("NUL", file_mode[mode].mode);
11699: info = 0xa8c0;
1.1.1.29 root 11700: } else if(msdos_is_device_path(path)) {
1.1.1.20 root 11701: fd = msdos_open("NUL", file_mode[mode].mode);
11702: info = 0x80d3;
1.1.1.11 root 11703: } else {
1.1.1.13 root 11704: fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11 root 11705: info = msdos_drive_number(path);
11706: }
1.1 root 11707: if(fd != -1) {
11708: REG16(AX) = fd;
11709: REG16(CX) = 1;
1.1.1.37 root 11710: msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20 root 11711: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11712: } else {
11713: REG16(AX) = errno;
1.1.1.3 root 11714: m_CF = 1;
1.1 root 11715: }
11716: } else if(REG8(DL) & 2) {
11717: int attr = GetFileAttributes(path);
1.1.1.37 root 11718: int fd = -1;
1.1.1.11 root 11719: UINT16 info;
1.1.1.37 root 11720: int sio_port = 0;
11721: int lpt_port = 0;
1.1 root 11722:
1.1.1.11 root 11723: if(msdos_is_con_path(path)) {
1.1.1.13 root 11724: fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11 root 11725: info = 0x80d3;
1.1.1.37 root 11726: } else if((sio_port = msdos_is_comm_path(path)) != 0) {
11727: fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14 root 11728: info = 0x80d3;
1.1.1.37 root 11729: msdos_set_comm_params(sio_port, path);
11730: } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
11731: fd = msdos_open("NUL", file_mode[mode].mode);
11732: info = 0xa8c0;
1.1.1.29 root 11733: } else if(msdos_is_device_path(path)) {
1.1.1.20 root 11734: fd = msdos_open("NUL", file_mode[mode].mode);
11735: info = 0x80d3;
1.1 root 11736: } else {
11737: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11 root 11738: info = msdos_drive_number(path);
1.1 root 11739: }
11740: if(fd != -1) {
11741: if(attr == -1) {
11742: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
11743: }
11744: SetFileAttributes(path, attr);
11745: REG16(AX) = fd;
11746: REG16(CX) = 3;
1.1.1.37 root 11747: msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20 root 11748: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11749: } else {
11750: REG16(AX) = errno;
1.1.1.3 root 11751: m_CF = 1;
1.1 root 11752: }
11753: } else {
11754: REG16(AX) = 0x50;
1.1.1.3 root 11755: m_CF = 1;
1.1 root 11756: }
11757: } else {
11758: // file not exists
11759: if(REG8(DL) & 0x10) {
11760: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
11761:
11762: if(fd != -1) {
11763: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
11764: REG16(AX) = fd;
11765: REG16(CX) = 2;
11766: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20 root 11767: msdos_psp_set_file_table(fd, fd, current_psp);
1.1 root 11768: } else {
11769: REG16(AX) = errno;
1.1.1.3 root 11770: m_CF = 1;
1.1 root 11771: }
11772: } else {
11773: REG16(AX) = 0x02;
1.1.1.3 root 11774: m_CF = 1;
1.1 root 11775: }
11776: }
11777: } else {
11778: REG16(AX) = 0x0c;
1.1.1.3 root 11779: m_CF = 1;
1.1 root 11780: }
11781: }
11782:
11783: inline void msdos_int_21h_710dh()
11784: {
11785: // reset drive
11786: }
11787:
1.1.1.17 root 11788: inline void msdos_int_21h_7141h(int lfn)
11789: {
11790: if(REG16(SI) == 0) {
11791: msdos_int_21h_41h(lfn);
11792: return;
11793: }
11794: if(REG16(SI) != 1) {
11795: REG16(AX) = 5;
11796: m_CF = 1;
11797: }
11798: /* wild card and matching attributes... */
11799: char tmp[MAX_PATH * 2];
11800: // copy search pathname (and quick check overrun)
11801: ZeroMemory(tmp, sizeof(tmp));
11802: tmp[MAX_PATH - 1] = '\0';
11803: tmp[MAX_PATH] = 1;
11804: strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
11805:
11806: if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
11807: REG16(AX) = 1;
11808: m_CF = 1;
11809: return;
11810: }
11811: for(char *s = tmp; *s; ++s) {
11812: if(*s == '/') {
11813: *s = '\\';
11814: }
11815: }
11816: char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
11817: if(tmp_name) {
11818: ++tmp_name;
11819: } else {
11820: tmp_name = strchr(tmp, ':');
11821: tmp_name = tmp_name ? tmp_name + 1 : tmp;
11822: }
11823:
11824: WIN32_FIND_DATAA fd;
11825: HANDLE fh = FindFirstFileA(tmp, &fd);
11826: if(fh == INVALID_HANDLE_VALUE) {
11827: REG16(AX) = 2;
11828: m_CF = 1;
11829: return;
11830: }
11831: do {
11832: if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
11833: strcpy(tmp_name, fd.cFileName);
11834: if(remove(msdos_trimmed_path(tmp, lfn))) {
11835: REG16(AX) = 5;
11836: m_CF = 1;
11837: break;
11838: }
11839: }
11840: } while(FindNextFileA(fh, &fd));
11841: if(!m_CF) {
11842: if(GetLastError() != ERROR_NO_MORE_FILES) {
11843: m_CF = 1;
11844: REG16(AX) = 2;
11845: }
11846: }
11847: FindClose(fh);
11848: }
11849:
1.1 root 11850: inline void msdos_int_21h_714eh()
11851: {
11852: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 11853: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
11854: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 11855: WIN32_FIND_DATA fd;
11856:
1.1.1.13 root 11857: dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
11858: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
11859: FindClose(dtainfo->find_handle);
11860: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11861: }
11862: strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14 root 11863: dtainfo->allowable_mask = REG8(CL);
11864: dtainfo->required_mask = REG8(CH);
11865: bool label_only = (dtainfo->allowable_mask == 8);
1.1 root 11866:
1.1.1.14 root 11867: if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
11868: dtainfo->allowable_mask &= ~8;
1.1 root 11869: }
1.1.1.14 root 11870: if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
11871: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13 root 11872: if(!FindNextFile(dtainfo->find_handle, &fd)) {
11873: FindClose(dtainfo->find_handle);
11874: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11875: break;
11876: }
11877: }
11878: }
1.1.1.13 root 11879: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 11880: find->attrib = fd.dwFileAttributes;
11881: msdos_find_file_conv_local_time(&fd);
11882: if(REG16(SI) == 0) {
11883: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
11884: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
11885: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
11886: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
11887: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
11888: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
11889: } else {
11890: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
11891: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
11892: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
11893: }
11894: find->size_hi = fd.nFileSizeHigh;
11895: find->size_lo = fd.nFileSizeLow;
11896: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 11897: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 11898: REG16(AX) = dtainfo - dtalist + 1;
11899: } else if(dtainfo->allowable_mask & 8) {
1.1 root 11900: // volume label
11901: find->attrib = 8;
11902: find->size_hi = find->size_lo = 0;
11903: strcpy(find->full_name, process->volume_label);
11904: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 11905: dtainfo->allowable_mask &= ~8;
11906: REG16(AX) = dtainfo - dtalist + 1;
1.1 root 11907: } else {
11908: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 11909: m_CF = 1;
1.1 root 11910: }
11911: }
11912:
11913: inline void msdos_int_21h_714fh()
11914: {
11915: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 11916: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 11917: WIN32_FIND_DATA fd;
11918:
1.1.1.14 root 11919: if(REG16(BX) - 1u >= MAX_DTAINFO) {
11920: REG16(AX) = 6;
1.1.1.13 root 11921: m_CF = 1;
11922: return;
11923: }
1.1.1.14 root 11924: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 11925: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
11926: if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14 root 11927: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13 root 11928: if(!FindNextFile(dtainfo->find_handle, &fd)) {
11929: FindClose(dtainfo->find_handle);
11930: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11931: break;
11932: }
11933: }
11934: } else {
1.1.1.13 root 11935: FindClose(dtainfo->find_handle);
11936: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11937: }
11938: }
1.1.1.13 root 11939: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1 root 11940: find->attrib = fd.dwFileAttributes;
11941: msdos_find_file_conv_local_time(&fd);
11942: if(REG16(SI) == 0) {
11943: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
11944: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
11945: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
11946: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
11947: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
11948: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
11949: } else {
11950: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
11951: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
11952: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
11953: }
11954: find->size_hi = fd.nFileSizeHigh;
11955: find->size_lo = fd.nFileSizeLow;
11956: strcpy(find->full_name, fd.cFileName);
1.1.1.13 root 11957: strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14 root 11958: } else if(dtainfo->allowable_mask & 8) {
1.1 root 11959: // volume label
11960: find->attrib = 8;
11961: find->size_hi = find->size_lo = 0;
11962: strcpy(find->full_name, process->volume_label);
11963: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14 root 11964: dtainfo->allowable_mask &= ~8;
1.1 root 11965: } else {
11966: REG16(AX) = 0x12;
1.1.1.3 root 11967: m_CF = 1;
1.1 root 11968: }
11969: }
11970:
11971: inline void msdos_int_21h_71a0h()
11972: {
11973: DWORD max_component_len, file_sys_flag;
11974:
1.1.1.14 root 11975: 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))) {
11976: REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
11977: REG16(BX) |= 0x4000; // supports LFN functions
1.1 root 11978: REG16(CX) = (UINT16)max_component_len; // 255
11979: REG16(DX) = (UINT16)max_component_len + 5; // 260
11980: } else {
11981: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 11982: m_CF = 1;
1.1 root 11983: }
11984: }
11985:
11986: inline void msdos_int_21h_71a1h()
11987: {
1.1.1.14 root 11988: if(REG16(BX) - 1u >= MAX_DTAINFO) {
11989: REG16(AX) = 6;
1.1.1.13 root 11990: m_CF = 1;
11991: return;
11992: }
1.1.1.14 root 11993: dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13 root 11994: if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
11995: FindClose(dtainfo->find_handle);
11996: dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1 root 11997: }
11998: }
11999:
12000: inline void msdos_int_21h_71a6h()
12001: {
12002: process_t *process = msdos_process_info_get(current_psp);
1.1.1.20 root 12003: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
12004:
1.1.1.3 root 12005: UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 12006: struct _stat64 status;
12007: DWORD serial_number = 0;
12008:
1.1.1.20 root 12009: if(fd < process->max_files && file_handler[fd].valid) {
12010: if(_fstat64(fd, &status) == 0) {
12011: if(file_handler[fd].path[1] == ':') {
1.1 root 12012: // NOTE: we need to consider the network file path "\\host\share\"
12013: char volume[] = "A:\\";
1.1.1.20 root 12014: volume[0] = file_handler[fd].path[1];
1.1 root 12015: GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
12016: }
1.1.1.20 root 12017: *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1 root 12018: *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
12019: *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
12020: *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
12021: *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
12022: *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
12023: *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
12024: *(UINT32 *)(buffer + 0x1c) = serial_number;
12025: *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
12026: *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
12027: *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14 root 12028: // this is dummy id and it will be changed when it is reopened...
1.1 root 12029: *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20 root 12030: *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1 root 12031: } else {
12032: REG16(AX) = errno;
1.1.1.3 root 12033: m_CF = 1;
1.1 root 12034: }
12035: } else {
12036: REG16(AX) = 0x06;
1.1.1.3 root 12037: m_CF = 1;
1.1 root 12038: }
12039: }
12040:
12041: inline void msdos_int_21h_71a7h()
12042: {
12043: switch(REG8(BL)) {
12044: case 0x00:
1.1.1.3 root 12045: if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), ®16(DX), ®16(CX))) {
1.1 root 12046: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12047: m_CF = 1;
1.1 root 12048: }
12049: break;
12050: case 0x01:
12051: // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3 root 12052: if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1 root 12053: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 12054: m_CF = 1;
1.1 root 12055: }
12056: break;
12057: default:
1.1.1.22 root 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));
1.1 root 12059: REG16(AX) = 0x01;
1.1.1.3 root 12060: m_CF = 1;
1.1 root 12061: break;
12062: }
12063: }
12064:
12065: inline void msdos_int_21h_71a8h()
12066: {
12067: if(REG8(DH) == 0) {
12068: char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3 root 12069: strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 12070: memset(fcb, 0x20, sizeof(fcb));
12071: int len = strlen(tmp);
1.1.1.21 root 12072: for(int i = 0, pos = 0; i < len; i++) {
1.1 root 12073: if(tmp[i] == '.') {
12074: pos = 8;
12075: } else {
12076: if(msdos_lead_byte_check(tmp[i])) {
12077: fcb[pos++] = tmp[i++];
12078: }
12079: fcb[pos++] = tmp[i];
12080: }
12081: }
1.1.1.3 root 12082: memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1 root 12083: } else {
1.1.1.3 root 12084: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 12085: }
12086: }
12087:
1.1.1.22 root 12088: inline void msdos_int_21h_71aah()
12089: {
12090: char drv[] = "A:", path[MAX_PATH];
12091: char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
12092:
12093: if(REG8(BL) == 0) {
12094: drv[0] = 'A' + _getdrive() - 1;
12095: } else {
12096: drv[0] = 'A' + REG8(BL) - 1;
12097: }
12098: switch(REG8(BH)) {
12099: case 0x00:
12100: if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
12101: DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
12102: if(GetLogicalDrives() & bits) {
12103: REG16(AX) = 0x0f; // invalid drive
12104: } else {
12105: REG16(AX) = 0x03; // path not found
12106: }
12107: m_CF = 1;
12108: }
12109: break;
12110: case 0x01:
12111: if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
12112: REG16(AX) = 0x0f; // invalid drive
12113: m_CF = 1;
12114: }
12115: break;
12116: case 0x02:
12117: if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
12118: REG16(AX) = 0x0f; // invalid drive
12119: m_CF = 1;
12120: } else if(strncmp(path, "\\??\\", 4) != 0) {
12121: REG16(AX) = 0x0f; // invalid drive
12122: m_CF = 1;
12123: } else {
12124: strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
12125: }
12126: break;
12127: default:
12128: 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));
12129: REG16(AX) = 0x01;
12130: m_CF = 1;
12131: break;
12132: }
12133: }
12134:
1.1.1.14 root 12135: inline void msdos_int_21h_7300h()
12136: {
12137: if(REG8(AL) == 0) {
12138: REG8(AL) = REG8(CL);
12139: REG8(AH) = 0;
12140: } else {
12141: REG16(AX) = 0x01;
12142: m_CF = 1;
12143: }
12144: }
12145:
12146: inline void msdos_int_21h_7302h()
12147: {
12148: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
12149: UINT16 seg, ofs;
12150:
12151: if(REG16(CX) < 0x3f) {
12152: REG8(AL) = 0x18;
12153: m_CF = 1;
12154: } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
12155: REG8(AL) = 0xff;
12156: m_CF = 1;
12157: } else {
12158: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
12159: }
12160: }
12161:
1.1 root 12162: inline void msdos_int_21h_7303h()
12163: {
1.1.1.3 root 12164: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
12165: ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 12166: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
12167:
12168: if(GetDiskFreeSpace(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
12169: info->size_of_structure = sizeof(ext_space_info_t);
12170: info->structure_version = 0;
12171: info->sectors_per_cluster = sectors_per_cluster;
12172: info->bytes_per_sector = bytes_per_sector;
12173: info->available_clusters_on_drive = free_clusters;
12174: info->total_clusters_on_drive = total_clusters;
12175: info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
12176: info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
12177: info->available_allocation_units = free_clusters; // ???
12178: info->total_allocation_units = total_clusters; // ???
12179: } else {
12180: REG16(AX) = errno;
1.1.1.3 root 12181: m_CF = 1;
1.1 root 12182: }
12183: }
12184:
1.1.1.30 root 12185: inline void msdos_int_21h_dbh()
12186: {
12187: // Novell NetWare - Workstation - Get Number of Local Drives
12188: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
12189: REG8(AL) = dos_info->last_drive;
12190: }
12191:
12192: inline void msdos_int_21h_dch()
12193: {
12194: // Novell NetWare - Connection Services - Get Connection Number
12195: REG8(AL) = 0x00;
12196: }
12197:
1.1.1.32 root 12198: inline void msdos_int_24h()
12199: {
12200: const char *message = NULL;
12201: int key = 0;
12202:
12203: for(int i = 0; i < array_length(critical_error_table); i++) {
12204: if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
12205: if(active_code_page == 932) {
12206: message = critical_error_table[i].message_japanese;
12207: }
12208: if(message == NULL) {
12209: message = critical_error_table[i].message_english;
12210: }
12211: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
12212: strcpy((char *)(mem + WORK_TOP + 1), message);
12213:
12214: SREG(ES) = WORK_TOP >> 4;
12215: i386_load_segment_descriptor(ES);
12216: REG16(DI) = 0x0000;
12217: break;
12218: }
12219: }
12220: fprintf(stderr, "\n%s", message);
12221: if(!(REG8(AH) & 0x80)) {
12222: if(REG8(AH) & 0x01) {
12223: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
12224: } else {
12225: fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
12226: }
12227: }
12228: fprintf(stderr, "\n");
12229:
1.1.1.33 root 12230: {
1.1.1.32 root 12231: fprintf(stderr, "%s", (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33 root 12232: }
1.1.1.32 root 12233: if(REG8(AH) & 0x10) {
12234: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (R)" : "Retry");
12235: }
12236: if(REG8(AH) & 0x20) {
12237: fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
12238: }
12239: if(REG8(AH) & 0x08) {
12240: fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
12241: }
12242: fprintf(stderr, "? ");
12243:
12244: while(1) {
12245: while(!_kbhit()) {
12246: Sleep(10);
12247: }
12248: key = _getch();
12249:
12250: if(key == 'I' || key == 'i') {
12251: if(REG8(AH) & 0x20) {
12252: REG8(AL) = 0;
12253: break;
12254: }
12255: } else if(key == 'R' || key == 'r') {
12256: if(REG8(AH) & 0x10) {
12257: REG8(AL) = 1;
12258: break;
12259: }
12260: } else if(key == 'A' || key == 'a') {
12261: REG8(AL) = 2;
12262: break;
12263: } else if(key == 'F' || key == 'f') {
12264: if(REG8(AH) & 0x08) {
12265: REG8(AL) = 3;
12266: break;
12267: }
12268: }
12269: }
12270: fprintf(stderr, "%c\n", key);
12271: }
12272:
1.1 root 12273: inline void msdos_int_25h()
12274: {
12275: UINT16 seg, ofs;
12276: DWORD dwSize;
12277:
1.1.1.3 root 12278: #if defined(HAS_I386)
12279: I386OP(pushf)();
12280: #else
12281: PREFIX86(_pushf());
12282: #endif
1.1 root 12283:
12284: if(!(REG8(AL) < 26)) {
12285: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 12286: m_CF = 1;
1.1 root 12287: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
12288: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12289: m_CF = 1;
1.1 root 12290: } else {
12291: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
12292: char dev[64];
12293: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
12294:
12295: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
12296: if(hFile == INVALID_HANDLE_VALUE) {
12297: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12298: m_CF = 1;
1.1 root 12299: } else {
1.1.1.19 root 12300: UINT32 top_sector = REG16(DX);
12301: UINT16 sector_num = REG16(CX);
12302: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
12303:
12304: if(sector_num == 0xffff) {
12305: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
12306: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
12307: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
12308: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
12309: buffer_addr = (seg << 4) + ofs;
12310: }
12311: // if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
12312: // REG8(AL) = 0x02; // drive not ready
12313: // m_CF = 1;
12314: // } else
12315: if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 12316: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 12317: m_CF = 1;
1.1.1.19 root 12318: } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 12319: REG8(AL) = 0x0b; // read error
1.1.1.3 root 12320: m_CF = 1;
1.1 root 12321: }
12322: CloseHandle(hFile);
12323: }
12324: }
12325: }
12326:
12327: inline void msdos_int_26h()
12328: {
1.1.1.42! root 12329: // this operation may cause serious damage for drives, so support only floppy disk...
1.1 root 12330: UINT16 seg, ofs;
12331: DWORD dwSize;
12332:
1.1.1.3 root 12333: #if defined(HAS_I386)
12334: I386OP(pushf)();
12335: #else
12336: PREFIX86(_pushf());
12337: #endif
1.1 root 12338:
12339: if(!(REG8(AL) < 26)) {
12340: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 12341: m_CF = 1;
1.1 root 12342: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
12343: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12344: m_CF = 1;
1.1 root 12345: } else {
12346: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
12347: char dev[64];
12348: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
12349:
12350: if(dpb->media_type == 0xf8) {
12351: // this drive is not a floppy
1.1.1.6 root 12352: // if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
12353: // fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
12354: // }
1.1 root 12355: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12356: m_CF = 1;
1.1 root 12357: } else {
12358: HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
12359: if(hFile == INVALID_HANDLE_VALUE) {
12360: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12361: m_CF = 1;
1.1 root 12362: } else {
1.1.1.19 root 12363: UINT32 top_sector = REG16(DX);
12364: UINT16 sector_num = REG16(CX);
12365: UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
12366:
12367: if(sector_num == 0xffff) {
12368: top_sector = *(UINT32 *)(mem + buffer_addr + 0);
12369: sector_num = *(UINT16 *)(mem + buffer_addr + 4);
12370: UINT16 ofs = *(UINT16 *)(mem + buffer_addr + 6);
12371: UINT16 seg = *(UINT16 *)(mem + buffer_addr + 8);
12372: buffer_addr = (seg << 4) + ofs;
12373: }
1.1 root 12374: if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
12375: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 12376: m_CF = 1;
1.1.1.19 root 12377: } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1 root 12378: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 12379: m_CF = 1;
1.1.1.19 root 12380: } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 12381: REG8(AL) = 0x0a; // write error
1.1.1.3 root 12382: m_CF = 1;
1.1 root 12383: }
12384: CloseHandle(hFile);
12385: }
12386: }
12387: }
12388: }
12389:
12390: inline void msdos_int_27h()
12391: {
1.1.1.29 root 12392: int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
12393: try {
12394: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
12395: } catch(...) {
12396: // recover the broken mcb
12397: int mcb_seg = SREG(CS) - 1;
12398: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33 root 12399:
1.1.1.29 root 12400: if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33 root 12401: mcb->mz = 'M';
12402: mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
12403:
1.1.1.29 root 12404: if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39 root 12405: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29 root 12406: } else {
1.1.1.39 root 12407: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29 root 12408: }
12409: } else {
12410: mcb->mz = 'Z';
1.1.1.30 root 12411: mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29 root 12412: }
12413: msdos_mem_realloc(SREG(CS), paragraphs, NULL);
12414: }
1.1.1.3 root 12415: msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1 root 12416: }
12417:
12418: inline void msdos_int_29h()
12419: {
1.1.1.14 root 12420: #if 1
12421: // need to check escape sequences
1.1 root 12422: msdos_putch(REG8(AL));
1.1.1.14 root 12423: #else
12424: DWORD num;
12425: vram_flush();
1.1.1.23 root 12426: WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), ®8(AL), 1, &num, NULL);
1.1.1.14 root 12427: cursor_moved = true;
12428: #endif
1.1 root 12429: }
12430:
12431: inline void msdos_int_2eh()
12432: {
12433: char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
12434: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 12435: strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1 root 12436: char *token = my_strtok(tmp, " ");
12437: strcpy(command, token);
12438: strcpy(opt, token + strlen(token) + 1);
12439:
12440: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
12441: param->env_seg = 0;
12442: param->cmd_line.w.l = 44;
12443: param->cmd_line.w.h = (WORK_TOP >> 4);
12444: param->fcb1.w.l = 24;
12445: param->fcb1.w.h = (WORK_TOP >> 4);
12446: param->fcb2.w.l = 24;
12447: param->fcb2.w.h = (WORK_TOP >> 4);
12448:
12449: memset(mem + WORK_TOP + 24, 0x20, 20);
12450:
12451: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
12452: cmd_line->len = strlen(opt);
12453: strcpy(cmd_line->cmd, opt);
12454: cmd_line->cmd[cmd_line->len] = 0x0d;
12455:
1.1.1.28 root 12456: try {
12457: if(msdos_process_exec(command, param, 0)) {
12458: REG16(AX) = 0xffff; // error before processing command
12459: } else {
12460: // set flag to set retval to ax when the started process is terminated
12461: process_t *process = msdos_process_info_get(current_psp);
12462: process->called_by_int2eh = true;
12463: }
12464: } catch(...) {
12465: REG16(AX) = 0xffff; // error before processing command
12466: }
1.1 root 12467: }
12468:
1.1.1.29 root 12469: inline void msdos_int_2fh_05h()
12470: {
12471: switch(REG8(AL)) {
12472: case 0x00:
1.1.1.32 root 12473: REG8(AL) = 0xff;
12474: break;
12475: case 0x01:
12476: case 0x02:
12477: for(int i = 0; i < array_length(standard_error_table); i++) {
12478: if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
12479: const char *message = NULL;
12480: if(active_code_page == 932) {
12481: message = standard_error_table[i].message_japanese;
12482: }
12483: if(message == NULL) {
12484: message = standard_error_table[i].message_english;
12485: }
12486: strcpy((char *)(mem + WORK_TOP), message);
12487:
12488: SREG(ES) = WORK_TOP >> 4;
12489: i386_load_segment_descriptor(ES);
12490: REG16(DI) = 0x0000;
12491: REG8(AL) = 0x01;
12492: break;
12493: }
12494: }
1.1.1.29 root 12495: break;
12496: default:
12497: 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));
12498: m_CF = 1;
12499: }
12500: }
12501:
1.1.1.22 root 12502: inline void msdos_int_2fh_11h()
12503: {
12504: switch(REG8(AL)) {
12505: case 0x00:
1.1.1.29 root 12506: if(i386_read_stack() == 0xdada) {
12507: // MSCDEX is not installed
12508: // REG8(AL) = 0x00;
12509: } else {
12510: // Network Redirector is not installed
12511: // REG8(AL) = 0x00;
12512: }
1.1.1.22 root 12513: break;
12514: default:
12515: 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 12516: REG16(AX) = 0x49; // network software not installed
1.1.1.22 root 12517: m_CF = 1;
12518: break;
12519: }
12520: }
12521:
1.1.1.21 root 12522: inline void msdos_int_2fh_12h()
12523: {
12524: switch(REG8(AL)) {
1.1.1.22 root 12525: case 0x00:
1.1.1.29 root 12526: // DOS 3.0+ internal functions are installed
1.1.1.22 root 12527: REG8(AL) = 0xff;
12528: break;
1.1.1.29 root 12529: // case 0x01: // DOS 3.0+ internal - Close Current File
12530: case 0x02:
12531: {
12532: UINT16 stack = i386_read_stack();
12533: REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
12534: SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
12535: i386_load_segment_descriptor(ES);
12536: }
12537: break;
1.1.1.30 root 12538: case 0x03:
12539: SREG(DS) = (DEVICE_TOP >> 4);
12540: i386_load_segment_descriptor(DS);
12541: break;
1.1.1.29 root 12542: case 0x04:
12543: {
12544: UINT16 stack = i386_read_stack();
12545: REG8(AL) = (stack == '/') ? '\\' : stack;
12546: #if defined(HAS_I386)
12547: m_ZF = (REG8(AL) == '\\');
12548: #else
12549: m_ZeroVal = (REG8(AL) != '\\');
12550: #endif
12551: }
12552: break;
12553: case 0x05:
12554: msdos_putch(i386_read_stack());
12555: break;
12556: // case 0x06: // DOS 3.0+ internal - Invole Critical Error
12557: // case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
12558: // case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
12559: // case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
12560: // case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
12561: // case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
12562: // case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
12563: case 0x0d:
12564: {
12565: SYSTEMTIME time;
12566: FILETIME file_time;
12567: WORD dos_date, dos_time;
12568: GetLocalTime(&time);
12569: SystemTimeToFileTime(&time, &file_time);
12570: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
12571: REG16(AX) = dos_date;
12572: REG16(DX) = dos_time;
12573: }
12574: break;
12575: // case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
12576: // case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
12577: // case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
12578: case 0x11:
12579: {
12580: char path[MAX_PATH], *p;
12581: strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
12582: my_strupr(path);
12583: while((p = my_strchr(path, '/')) != NULL) {
12584: *p = '\\';
12585: }
12586: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
12587: }
12588: break;
12589: case 0x12:
12590: REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
12591: break;
12592: case 0x13:
12593: {
12594: char tmp[2] = {0};
12595: tmp[0] = i386_read_stack();
12596: my_strupr(tmp);
12597: REG8(AL) = tmp[0];
12598: }
12599: break;
12600: case 0x14:
12601: #if defined(HAS_I386)
12602: m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
12603: #else
12604: m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
12605: #endif
12606: m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
12607: break;
12608: // case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21 root 12609: case 0x16:
12610: if(REG16(BX) < 20) {
12611: SREG(ES) = SFT_TOP >> 4;
12612: i386_load_segment_descriptor(ES);
12613: REG16(DI) = 6 + 0x3b * REG16(BX);
12614:
12615: // update system file table
12616: UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
12617: if(file_handler[REG16(BX)].valid) {
12618: int count = 0;
12619: for(int i = 0; i < 20; i++) {
12620: if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
12621: count++;
12622: }
12623: }
12624: *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
12625: *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
12626: _lseek(REG16(BX), 0, SEEK_END);
12627: *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
12628: _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
12629: } else {
12630: memset(sft, 0, 0x3b);
12631: }
12632: } else {
12633: REG16(AX) = 0x06;
12634: m_CF = 1;
12635: }
12636: break;
1.1.1.29 root 12637: // case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
12638: // case 0x18: // DOS 3.0+ internal - Get Caller's Registers
12639: // case 0x19: // DOS 3.0+ internal - Set Drive???
12640: case 0x1a:
12641: {
12642: char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
12643: if(path[1] == ':') {
12644: if(path[0] >= 'a' && path[0] <= 'z') {
12645: REG8(AL) = path[0] - 'a' + 1;
12646: } else if(path[0] >= 'A' && path[0] <= 'Z') {
12647: REG8(AL) = path[0] - 'A' + 1;
12648: } else {
12649: REG8(AL) = 0xff; // invalid
12650: }
12651: strcpy(full, path);
12652: strcpy(path, full + 2);
12653: } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
12654: if(full[0] >= 'a' && full[0] <= 'z') {
12655: REG8(AL) = full[0] - 'a' + 1;
12656: } else if(full[0] >= 'A' && full[0] <= 'Z') {
12657: REG8(AL) = full[0] - 'A' + 1;
12658: } else {
12659: REG8(AL) = 0xff; // invalid
12660: }
12661: } else {
12662: REG8(AL) = 0x00; // default
12663: }
12664: }
12665: break;
12666: case 0x1b:
12667: {
12668: int year = REG16(CX) + 1980;
12669: REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
12670: }
12671: break;
12672: // case 0x1c: // DOS 3.0+ internal - Check Sum Memory
12673: // case 0x1d: // DOS 3.0+ internal - Sum Memory
12674: case 0x1e:
12675: {
12676: char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
12677: char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
12678: if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
12679: #if defined(HAS_I386)
12680: m_ZF = (strcmp(full_1st, full_2nd) == 0);
12681: #else
12682: m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
12683: #endif
12684: } else {
12685: #if defined(HAS_I386)
12686: m_ZF = (strcmp(path_1st, path_2nd) == 0);
12687: #else
12688: m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
12689: #endif
12690: }
12691: }
12692: break;
12693: // case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21 root 12694: case 0x20:
12695: {
12696: int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
12697:
12698: if(fd < 20) {
12699: SREG(ES) = current_psp;
12700: i386_load_segment_descriptor(ES);
12701: REG16(DI) = offsetof(psp_t, file_table) + fd;
12702: } else {
12703: REG16(AX) = 0x06;
12704: m_CF = 1;
12705: }
12706: }
12707: break;
1.1.1.29 root 12708: case 0x21:
12709: msdos_int_21h_60h(0);
12710: break;
12711: // case 0x22: // DOS 3.0+ internal - Set Extended Error Info
12712: // case 0x23: // DOS 3.0+ internal - Check If Character Device
12713: // case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
12714: case 0x25:
12715: REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
12716: break;
12717: case 0x26:
12718: REG8(AL) = REG8(CL);
12719: msdos_int_21h_3dh();
12720: break;
12721: case 0x27:
12722: msdos_int_21h_3eh();
12723: break;
12724: case 0x28:
12725: REG16(AX) = REG16(BP);
12726: msdos_int_21h_42h();
12727: break;
12728: case 0x29:
12729: msdos_int_21h_3fh();
12730: break;
12731: // case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
12732: case 0x2b:
12733: REG16(AX) = REG16(BP);
12734: msdos_int_21h_44h();
12735: break;
12736: case 0x2c:
12737: REG16(BX) = DEVICE_TOP >> 4;
12738: REG16(AX) = 22;
12739: break;
12740: case 0x2d:
12741: {
12742: sda_t *sda = (sda_t *)(mem + SDA_TOP);
12743: REG16(AX) = sda->extended_error_code;
12744: }
12745: break;
12746: case 0x2e:
12747: if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32 root 12748: SREG(ES) = 0x0001;
12749: i386_load_segment_descriptor(ES);
12750: REG16(DI) = 0x00;
12751: } else if(REG8(DL) == 0x08) {
12752: // dummy parameter error message read routine is at fffd:0010
12753: SREG(ES) = 0xfffd;
1.1.1.22 root 12754: i386_load_segment_descriptor(ES);
1.1.1.32 root 12755: REG16(DI) = 0x0010;
1.1.1.22 root 12756: }
12757: break;
1.1.1.29 root 12758: case 0x2f:
12759: if(REG16(DX) != 0) {
1.1.1.30 root 12760: dos_major_version = REG8(DL);
12761: dos_minor_version = REG8(DH);
1.1.1.29 root 12762: } else {
12763: REG8(DL) = 7;
12764: REG8(DH) = 10;
12765: }
12766: break;
12767: // case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
12768: // case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22 root 12769: default:
12770: 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));
12771: REG16(AX) = 0x01;
12772: m_CF = 1;
12773: break;
12774: }
12775: }
12776:
1.1.1.30 root 12777: inline void msdos_int_2fh_13h()
12778: {
12779: static UINT16 prevDS = 0, prevDX = 0;
12780: static UINT16 prevES = 0, prevBX = 0;
12781: UINT16 tmp;
12782:
12783: tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
12784: i386_load_segment_descriptor(DS);
12785: tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
12786:
12787: tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
12788: i386_load_segment_descriptor(ES);
12789: tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
12790: }
12791:
1.1.1.22 root 12792: inline void msdos_int_2fh_14h()
12793: {
12794: switch(REG8(AL)) {
12795: case 0x00:
1.1.1.29 root 12796: // NLSFUNC.COM is installed
12797: REG8(AL) = 0xff;
1.1.1.25 root 12798: break;
12799: case 0x01:
12800: case 0x03:
12801: REG8(AL) = 0x00;
12802: active_code_page = REG16(BX);
12803: msdos_nls_tables_update();
12804: break;
12805: case 0x02:
12806: REG8(AL) = get_extended_country_info(REG16(BP));
12807: break;
12808: case 0x04:
1.1.1.42! root 12809: for(int i = 0;; i++) {
! 12810: if(country_table[i].code == REG16(DX)) {
! 12811: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
! 12812: break;
! 12813: } else if(country_table[i].code == -1) {
! 12814: get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
! 12815: break;
! 12816: }
! 12817: }
1.1.1.25 root 12818: REG8(AL) = 0x00;
1.1.1.22 root 12819: break;
12820: default:
12821: 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));
12822: REG16(AX) = 0x01;
12823: m_CF = 1;
12824: break;
12825: }
12826: }
12827:
12828: inline void msdos_int_2fh_15h()
12829: {
12830: switch(REG8(AL)) {
1.1.1.29 root 12831: case 0x00: // CD-ROM - Installation Check
12832: if(REG16(BX) == 0x0000) {
12833: // MSCDEX is not installed
12834: // REG8(AL) = 0x00;
12835: } else {
12836: // GRAPHICS.COM is not installed
12837: // REG8(AL) = 0x00;
12838: }
1.1.1.22 root 12839: break;
12840: case 0xff:
1.1.1.29 root 12841: if(REG16(BX) == 0x0000) {
12842: // CORELCDX is not installed
12843: } else {
12844: 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));
12845: REG16(AX) = 0x01;
12846: m_CF = 1;
12847: }
1.1.1.22 root 12848: break;
1.1.1.21 root 12849: default:
1.1.1.22 root 12850: 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 12851: REG16(AX) = 0x01;
12852: m_CF = 1;
12853: break;
12854: }
12855: }
12856:
1.1 root 12857: inline void msdos_int_2fh_16h()
12858: {
12859: switch(REG8(AL)) {
12860: case 0x00:
1.1.1.14 root 12861: if(no_windows) {
1.1.1.29 root 12862: // neither Windows 3.x enhanced mode nor Windows/386 2.x running
12863: // REG8(AL) = 0x00;
1.1.1.14 root 12864: } else {
1.1.1.30 root 12865: REG8(AL) = win_major_version;
12866: REG8(AH) = win_minor_version;
1.1 root 12867: }
12868: break;
1.1.1.30 root 12869: case 0x05:
12870: // from DOSBox
12871: i386_set_a20_line(1);
12872: break;
1.1.1.22 root 12873: case 0x0a:
12874: if(!no_windows) {
12875: REG16(AX) = 0x0000;
1.1.1.30 root 12876: REG8(BH) = win_major_version;
12877: REG8(BL) = win_minor_version;
1.1.1.22 root 12878: REG16(CX) = 0x0003; // enhanced
12879: }
12880: break;
1.1.1.30 root 12881: case 0x0b:
12882: // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22 root 12883: case 0x0e:
12884: case 0x0f:
1.1.1.30 root 12885: case 0x10:
1.1.1.22 root 12886: case 0x11:
12887: case 0x12:
12888: case 0x13:
12889: case 0x14:
1.1.1.30 root 12890: case 0x15:
1.1.1.33 root 12891: case 0x86:
1.1.1.22 root 12892: case 0x87:
1.1.1.30 root 12893: case 0x89:
1.1.1.33 root 12894: case 0x8a:
1.1.1.22 root 12895: // function not supported, do not clear AX
12896: break;
1.1.1.14 root 12897: case 0x80:
12898: Sleep(10);
1.1.1.35 root 12899: REQUEST_HARDWRE_UPDATE();
1.1.1.29 root 12900: REG8(AL) = 0x00;
1.1.1.14 root 12901: break;
1.1.1.33 root 12902: case 0x83:
12903: REG16(BX) = 0x01; // system vm id
12904: break;
1.1.1.22 root 12905: case 0x8e:
12906: REG16(AX) = 0x00; // failed
12907: break;
1.1.1.20 root 12908: case 0x8f:
12909: switch(REG8(DH)) {
12910: case 0x00:
12911: case 0x02:
12912: case 0x03:
12913: REG16(AX) = 0x00;
12914: break;
12915: case 0x01:
12916: REG16(AX) = 0x168f;
12917: break;
12918: }
12919: break;
1.1 root 12920: default:
1.1.1.22 root 12921: 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));
12922: REG16(AX) = 0x01;
12923: m_CF = 1;
12924: break;
12925: }
12926: }
12927:
12928: inline void msdos_int_2fh_19h()
12929: {
12930: switch(REG8(AL)) {
12931: case 0x00:
1.1.1.29 root 12932: // SHELLB.COM is not installed
12933: // REG8(AL) = 0x00;
1.1.1.22 root 12934: break;
12935: case 0x01:
12936: case 0x02:
12937: case 0x03:
12938: case 0x04:
12939: REG16(AX) = 0x01;
12940: m_CF = 1;
12941: break;
1.1.1.29 root 12942: case 0x80:
12943: // IBM ROM-DOS v4.0 is not installed
12944: // REG8(AL) = 0x00;
12945: break;
1.1.1.22 root 12946: default:
12947: 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 12948: REG16(AX) = 0x01;
1.1.1.3 root 12949: m_CF = 1;
1.1 root 12950: break;
12951: }
12952: }
12953:
12954: inline void msdos_int_2fh_1ah()
12955: {
12956: switch(REG8(AL)) {
12957: case 0x00:
1.1.1.29 root 12958: // ANSI.SYS is installed
1.1 root 12959: REG8(AL) = 0xff;
12960: break;
12961: default:
1.1.1.22 root 12962: 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));
12963: REG16(AX) = 0x01;
12964: m_CF = 1;
12965: break;
12966: }
12967: }
12968:
1.1.1.30 root 12969: inline void msdos_int_2fh_40h()
1.1.1.22 root 12970: {
12971: switch(REG8(AL)) {
12972: case 0x00:
1.1.1.30 root 12973: // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
12974: REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22 root 12975: break;
12976: default:
12977: 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 12978: REG16(AX) = 0x01;
1.1.1.3 root 12979: m_CF = 1;
1.1 root 12980: break;
12981: }
12982: }
12983:
12984: inline void msdos_int_2fh_43h()
12985: {
12986: switch(REG8(AL)) {
12987: case 0x00:
1.1.1.29 root 12988: // XMS is installed ?
1.1.1.19 root 12989: #ifdef SUPPORT_XMS
12990: if(support_xms) {
12991: REG8(AL) = 0x80;
12992: } else
12993: #endif
12994: REG8(AL) = 0x00;
12995: break;
12996: case 0x10:
12997: SREG(ES) = XMS_TOP >> 4;
12998: i386_load_segment_descriptor(ES);
1.1.1.26 root 12999: REG16(BX) = 0x15;
1.1 root 13000: break;
13001: default:
1.1.1.22 root 13002: 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));
13003: REG16(AX) = 0x01;
13004: m_CF = 1;
13005: break;
13006: }
13007: }
13008:
13009: inline void msdos_int_2fh_46h()
13010: {
13011: switch(REG8(AL)) {
13012: case 0x80:
1.1.1.29 root 13013: // Windows v3.0 is not installed
13014: // REG8(AL) = 0x00;
1.1.1.22 root 13015: break;
13016: default:
13017: 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));
13018: REG16(AX) = 0x01;
13019: m_CF = 1;
13020: break;
13021: }
13022: }
13023:
13024: inline void msdos_int_2fh_48h()
13025: {
13026: switch(REG8(AL)) {
13027: case 0x00:
1.1.1.29 root 13028: // DOSKEY is not installed
13029: // REG8(AL) = 0x00;
1.1.1.22 root 13030: break;
13031: case 0x10:
13032: msdos_int_21h_0ah();
13033: REG16(AX) = 0x00;
13034: break;
13035: default:
13036: 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 13037: REG16(AX) = 0x01;
1.1.1.3 root 13038: m_CF = 1;
1.1 root 13039: break;
13040: }
13041: }
13042:
13043: inline void msdos_int_2fh_4ah()
13044: {
13045: switch(REG8(AL)) {
1.1.1.29 root 13046: #ifdef SUPPORT_HMA
13047: case 0x01: // DOS 5.0+ - Query Free HMA Space
13048: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
13049: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13050: // restore first free mcb in high memory area
13051: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13052: }
13053: int offset = 0xffff;
13054: if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
13055: REG16(DI) = offset + 0x10;
13056: } else {
13057: REG16(DI) = 0xffff;
13058: }
13059: } else {
13060: // HMA is already used
13061: REG16(BX) = 0;
13062: REG16(DI) = 0xffff;
13063: }
13064: SREG(ES) = 0xffff;
13065: i386_load_segment_descriptor(ES);
13066: break;
13067: case 0x02: // DOS 5.0+ - Allocate HMA Space
13068: if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
13069: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13070: // restore first free mcb in high memory area
13071: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13072: }
13073: int size = REG16(BX), offset;
13074: if((size % 16) != 0) {
13075: size &= ~15;
13076: size += 16;
13077: }
13078: if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
13079: REG16(BX) = size;
13080: REG16(DI) = offset + 0x10;
13081: is_hma_used_by_int_2fh = true;
13082: } else {
13083: REG16(BX) = 0;
13084: REG16(DI) = 0xffff;
13085: }
13086: } else {
13087: // HMA is already used
13088: REG16(BX) = 0;
13089: REG16(DI) = 0xffff;
13090: }
13091: SREG(ES) = 0xffff;
13092: i386_load_segment_descriptor(ES);
13093: break;
13094: case 0x03: // Windows95 - (De)Allocate HMA Memory Block
13095: if(REG8(DL) == 0x00) {
13096: if(!is_hma_used_by_xms) {
13097: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13098: // restore first free mcb in high memory area
13099: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13100: is_hma_used_by_int_2fh = false;
13101: }
13102: int size = REG16(BX), offset;
13103: if((size % 16) != 0) {
13104: size &= ~15;
13105: size += 16;
13106: }
13107: if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
13108: // REG16(BX) = size;
13109: SREG(ES) = 0xffff;
13110: i386_load_segment_descriptor(ES);
13111: REG16(DI) = offset + 0x10;
13112: is_hma_used_by_int_2fh = true;
13113: } else {
13114: REG16(DI) = 0xffff;
13115: }
13116: } else {
13117: REG16(DI) = 0xffff;
13118: }
13119: } else if(REG8(DL) == 0x01) {
13120: if(!is_hma_used_by_xms) {
13121: int size = REG16(BX);
13122: if((size % 16) != 0) {
13123: size &= ~15;
13124: size += 16;
13125: }
13126: if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
13127: // memory block address is not changed
13128: } else {
13129: REG16(DI) = 0xffff;
13130: }
13131: } else {
13132: REG16(DI) = 0xffff;
13133: }
13134: } else if(REG8(DL) == 0x02) {
13135: if(!is_hma_used_by_xms) {
13136: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13137: // restore first free mcb in high memory area
13138: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13139: is_hma_used_by_int_2fh = false;
13140: } else {
13141: msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
13142: if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
13143: is_hma_used_by_int_2fh = false;
13144: }
13145: }
13146: }
13147: } else {
13148: 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));
13149: REG16(AX) = 0x01;
13150: m_CF = 1;
13151: }
13152: break;
13153: case 0x04: // Windows95 - Get Start of HMA Memory Chain
13154: if(!is_hma_used_by_xms) {
13155: if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
13156: // restore first free mcb in high memory area
13157: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
13158: is_hma_used_by_int_2fh = false;
13159: }
13160: REG16(AX) = 0x0000;
13161: SREG(ES) = 0xffff;
13162: i386_load_segment_descriptor(ES);
13163: REG16(DI) = 0x10;
13164: }
13165: break;
13166: #else
1.1 root 13167: case 0x01:
13168: case 0x02:
1.1.1.29 root 13169: // HMA is already used
1.1.1.27 root 13170: REG16(BX) = 0x0000;
1.1.1.3 root 13171: SREG(ES) = 0xffff;
13172: i386_load_segment_descriptor(ES);
1.1 root 13173: REG16(DI) = 0xffff;
13174: break;
1.1.1.19 root 13175: case 0x03:
13176: // unable to allocate
13177: REG16(DI) = 0xffff;
13178: break;
13179: case 0x04:
13180: // function not supported, do not clear AX
13181: break;
1.1.1.29 root 13182: #endif
13183: case 0x10:
1.1.1.42! root 13184: switch(REG16(BX)) {
! 13185: case 0x0000:
! 13186: case 0x0001:
! 13187: case 0x0002:
! 13188: case 0x0003:
! 13189: case 0x0004:
! 13190: case 0x0005:
! 13191: case 0x0006:
! 13192: case 0x0007:
! 13193: case 0x0008:
! 13194: case 0x000a:
! 13195: case 0x1234:
! 13196: // SMARTDRV v4.00+ is not installed
! 13197: break;
! 13198: default:
1.1.1.29 root 13199: 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));
13200: REG16(AX) = 0x01;
13201: m_CF = 1;
1.1.1.42! root 13202: break;
1.1.1.29 root 13203: }
13204: break;
13205: case 0x11:
1.1.1.42! root 13206: switch(REG16(BX)) {
! 13207: case 0x0000:
! 13208: case 0x0001:
! 13209: case 0x0002:
! 13210: case 0x0003:
! 13211: case 0x0004:
! 13212: case 0x0005:
! 13213: case 0x0006:
! 13214: case 0x0007:
! 13215: case 0x0008:
! 13216: case 0x0009:
! 13217: case 0x000a:
! 13218: case 0x000b:
! 13219: case 0xfffe:
! 13220: case 0xffff:
1.1.1.29 root 13221: // DBLSPACE.BIN is not installed
1.1.1.42! root 13222: break;
! 13223: default:
! 13224: 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));
! 13225: REG16(AX) = 0x01;
! 13226: m_CF = 1;
! 13227: break;
! 13228: }
! 13229: break;
! 13230: case 0x12:
! 13231: if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
! 13232: // Microsoft Realtime Compression Interface (MRCI) is not installed
! 13233: } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
! 13234: // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29 root 13235: } else {
13236: 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));
13237: REG16(AX) = 0x01;
13238: m_CF = 1;
13239: }
1.1.1.22 root 13240: break;
1.1.1.42! root 13241: case 0x13:
! 13242: // DBLSPACE.BIN is not installed
! 13243: break;
1.1.1.22 root 13244: default:
13245: 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));
13246: REG16(AX) = 0x01;
13247: m_CF = 1;
13248: break;
13249: }
13250: }
13251:
13252: inline void msdos_int_2fh_4bh()
13253: {
13254: switch(REG8(AL)) {
1.1.1.24 root 13255: case 0x01:
1.1.1.22 root 13256: case 0x02:
1.1.1.29 root 13257: // Task Switcher is not installed
1.1.1.24 root 13258: break;
13259: case 0x03:
13260: // this call is available from within DOSSHELL even if the task switcher is not installed
13261: REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22 root 13262: break;
1.1.1.30 root 13263: case 0x04:
13264: REG16(BX) = 0x0000; // free switcher id successfully
13265: break;
1.1 root 13266: default:
1.1.1.22 root 13267: 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 13268: REG16(AX) = 0x01;
1.1.1.3 root 13269: m_CF = 1;
1.1 root 13270: break;
13271: }
13272: }
13273:
13274: inline void msdos_int_2fh_4fh()
13275: {
13276: switch(REG8(AL)) {
13277: case 0x00:
1.1.1.29 root 13278: // BILING is installed
1.1.1.27 root 13279: REG16(AX) = 0x0000;
13280: REG8(DL) = 0x01; // major version
13281: REG8(DH) = 0x00; // minor version
1.1 root 13282: break;
13283: case 0x01:
1.1.1.27 root 13284: REG16(AX) = 0x0000;
1.1 root 13285: REG16(BX) = active_code_page;
13286: break;
13287: default:
1.1.1.22 root 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:
13295: inline void msdos_int_2fh_55h()
13296: {
13297: switch(REG8(AL)) {
13298: case 0x00:
13299: case 0x01:
13300: // 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));
13301: break;
13302: default:
13303: 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 13304: REG16(AX) = 0x01;
1.1.1.3 root 13305: m_CF = 1;
1.1 root 13306: break;
13307: }
13308: }
13309:
1.1.1.24 root 13310: inline void msdos_int_2fh_adh()
13311: {
13312: switch(REG8(AL)) {
13313: case 0x00:
1.1.1.29 root 13314: // DISPLAY.SYS is installed
1.1.1.24 root 13315: REG8(AL) = 0xff;
13316: REG16(BX) = 0x100; // ???
13317: break;
13318: case 0x01:
13319: active_code_page = REG16(BX);
13320: msdos_nls_tables_update();
13321: REG16(AX) = 0x01;
13322: break;
13323: case 0x02:
13324: REG16(BX) = active_code_page;
13325: break;
13326: case 0x03:
13327: // FIXME
13328: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
13329: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
13330: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
13331: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
13332: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
13333: break;
13334: case 0x80:
13335: break; // keyb.com is not installed
13336: default:
13337: 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));
13338: REG16(AX) = 0x01;
13339: m_CF = 1;
13340: break;
13341: }
13342: }
13343:
1.1 root 13344: inline void msdos_int_2fh_aeh()
13345: {
13346: switch(REG8(AL)) {
13347: case 0x00:
1.1.1.28 root 13348: // FIXME: we need to check the given command line
13349: REG8(AL) = 0x00; // the command should be executed as usual
13350: // REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1 root 13351: break;
13352: case 0x01:
13353: {
13354: char command[MAX_PATH];
13355: memset(command, 0, sizeof(command));
1.1.1.3 root 13356: memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1 root 13357:
13358: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
13359: param->env_seg = 0;
13360: param->cmd_line.w.l = 44;
13361: param->cmd_line.w.h = (WORK_TOP >> 4);
13362: param->fcb1.w.l = 24;
13363: param->fcb1.w.h = (WORK_TOP >> 4);
13364: param->fcb2.w.l = 24;
13365: param->fcb2.w.h = (WORK_TOP >> 4);
13366:
13367: memset(mem + WORK_TOP + 24, 0x20, 20);
13368:
13369: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3 root 13370: cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
13371: memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1 root 13372: cmd_line->cmd[cmd_line->len] = 0x0d;
13373:
1.1.1.28 root 13374: try {
13375: msdos_process_exec(command, param, 0);
13376: } catch(...) {
13377: fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1 root 13378: }
13379: }
13380: break;
13381: default:
1.1.1.22 root 13382: 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 13383: REG16(AX) = 0x01;
1.1.1.3 root 13384: m_CF = 1;
1.1 root 13385: break;
13386: }
13387: }
13388:
1.1.1.34 root 13389: inline void msdos_int_2fh_b7h()
13390: {
13391: switch(REG8(AL)) {
13392: case 0x00:
13393: // APPEND is not installed
13394: // REG8(AL) = 0x00;
13395: break;
13396: case 0x07:
13397: // COMMAND.COM calls this service without checking APPEND is installed
13398: break;
13399: default:
13400: 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));
13401: REG16(AX) = 0x01;
13402: m_CF = 1;
13403: break;
13404: }
13405: }
13406:
1.1.1.24 root 13407: inline void msdos_int_33h_0000h()
13408: {
13409: REG16(AX) = 0xffff; // hardware/driver installed
13410: REG16(BX) = MAX_MOUSE_BUTTONS;
13411: }
13412:
13413: inline void msdos_int_33h_0001h()
13414: {
1.1.1.34 root 13415: if(mouse.hidden > 0) {
13416: mouse.hidden--;
13417: }
13418: if(mouse.hidden == 0) {
1.1.1.24 root 13419: if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
13420: SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
13421: }
13422: pic[1].imr &= ~0x10; // enable irq12
13423: }
13424: }
13425:
13426: inline void msdos_int_33h_0002h()
13427: {
1.1.1.34 root 13428: mouse.hidden++;
13429: // SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
13430: pic[1].imr |= 0x10; // enable irq12
1.1.1.24 root 13431: }
13432:
13433: inline void msdos_int_33h_0003h()
13434: {
1.1.1.34 root 13435: // if(mouse.hidden > 0) {
13436: update_console_input();
13437: // }
1.1.1.24 root 13438: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 13439: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
13440: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
13441: }
13442:
13443: inline void msdos_int_33h_0004h()
13444: {
13445: mouse.position.x = REG16(CX);
13446: mouse.position.x = REG16(DX);
1.1.1.24 root 13447: }
13448:
13449: inline void msdos_int_33h_0005h()
13450: {
1.1.1.34 root 13451: // if(mouse.hidden > 0) {
13452: update_console_input();
13453: // }
1.1.1.24 root 13454: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
13455: int idx = REG16(BX);
1.1.1.34 root 13456: REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
13457: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
13458: 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 13459: mouse.buttons[idx].pressed_times = 0;
13460: } else {
13461: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
13462: }
13463: REG16(AX) = mouse.get_buttons();
13464: }
13465:
13466: inline void msdos_int_33h_0006h()
13467: {
1.1.1.34 root 13468: // if(mouse.hidden > 0) {
13469: update_console_input();
13470: // }
1.1.1.24 root 13471: if(REG16(BX) < MAX_MOUSE_BUTTONS) {
13472: int idx = REG16(BX);
1.1.1.34 root 13473: REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
13474: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
13475: 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 13476: mouse.buttons[idx].released_times = 0;
13477: } else {
13478: REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
13479: }
13480: REG16(AX) = mouse.get_buttons();
13481: }
13482:
13483: inline void msdos_int_33h_0007h()
13484: {
13485: mouse.min_position.x = min(REG16(CX), REG16(DX));
13486: mouse.max_position.x = max(REG16(CX), REG16(DX));
13487: }
13488:
13489: inline void msdos_int_33h_0008h()
13490: {
13491: mouse.min_position.y = min(REG16(CX), REG16(DX));
13492: mouse.max_position.y = max(REG16(CX), REG16(DX));
13493: }
13494:
13495: inline void msdos_int_33h_0009h()
13496: {
13497: mouse.hot_spot[0] = REG16(BX);
13498: mouse.hot_spot[1] = REG16(CX);
13499: }
13500:
13501: inline void msdos_int_33h_000bh()
13502: {
1.1.1.34 root 13503: // if(mouse.hidden > 0) {
13504: update_console_input();
13505: // }
1.1.1.24 root 13506: int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
13507: int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
13508: mouse.prev_position.x = mouse.position.x;
13509: mouse.prev_position.y = mouse.position.y;
13510: REG16(CX) = dx;
13511: REG16(DX) = dy;
13512: }
13513:
13514: inline void msdos_int_33h_000ch()
13515: {
13516: mouse.call_mask = REG16(CX);
13517: mouse.call_addr.w.l = REG16(DX);
13518: mouse.call_addr.w.h = SREG(ES);
13519: }
13520:
13521: inline void msdos_int_33h_000fh()
13522: {
13523: mouse.mickey.x = REG16(CX);
13524: mouse.mickey.y = REG16(DX);
13525: }
13526:
13527: inline void msdos_int_33h_0011h()
13528: {
13529: REG16(AX) = 0xffff;
13530: REG16(BX) = MAX_MOUSE_BUTTONS;
13531: }
13532:
13533: inline void msdos_int_33h_0014h()
13534: {
13535: UINT16 old_mask = mouse.call_mask;
13536: UINT16 old_ofs = mouse.call_addr.w.l;
13537: UINT16 old_seg = mouse.call_addr.w.h;
13538:
13539: mouse.call_mask = REG16(CX);
13540: mouse.call_addr.w.l = REG16(DX);
13541: mouse.call_addr.w.h = SREG(ES);
13542:
13543: REG16(CX) = old_mask;
13544: REG16(DX) = old_ofs;
13545: SREG(ES) = old_seg;
13546: i386_load_segment_descriptor(ES);
13547: }
13548:
13549: inline void msdos_int_33h_0015h()
13550: {
13551: REG16(BX) = sizeof(mouse);
13552: }
13553:
13554: inline void msdos_int_33h_0016h()
13555: {
13556: memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
13557: }
13558:
13559: inline void msdos_int_33h_0017h()
13560: {
13561: memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
13562: }
13563:
13564: inline void msdos_int_33h_001ah()
13565: {
13566: mouse.sensitivity[0] = REG16(BX);
13567: mouse.sensitivity[1] = REG16(CX);
13568: mouse.sensitivity[2] = REG16(DX);
13569: }
13570:
13571: inline void msdos_int_33h_001bh()
13572: {
13573: REG16(BX) = mouse.sensitivity[0];
13574: REG16(CX) = mouse.sensitivity[1];
13575: REG16(DX) = mouse.sensitivity[2];
13576: }
13577:
13578: inline void msdos_int_33h_001dh()
13579: {
13580: mouse.display_page = REG16(BX);
13581: }
13582:
13583: inline void msdos_int_33h_001eh()
13584: {
13585: REG16(BX) = mouse.display_page;
13586: }
13587:
1.1.1.34 root 13588: inline void msdos_int_33h_001fh()
13589: {
13590: // from DOSBox
13591: REG16(BX) = 0x0000;
13592: SREG(ES) = 0x0000;
13593: i386_load_segment_descriptor(ES);
13594: mouse.enabled = false;
13595: mouse.old_hidden = mouse.hidden;
13596: mouse.hidden = 1;
13597: }
13598:
13599: inline void msdos_int_33h_0020h()
13600: {
13601: // from DOSBox
13602: mouse.enabled = true;
13603: mouse.hidden = mouse.old_hidden;
13604: }
13605:
1.1.1.24 root 13606: inline void msdos_int_33h_0021h()
13607: {
13608: REG16(AX) = 0xffff;
13609: REG16(BX) = MAX_MOUSE_BUTTONS;
13610: }
13611:
13612: inline void msdos_int_33h_0022h()
13613: {
13614: mouse.language = REG16(BX);
13615: }
13616:
13617: inline void msdos_int_33h_0023h()
13618: {
13619: REG16(BX) = mouse.language;
13620: }
13621:
13622: inline void msdos_int_33h_0024h()
13623: {
13624: REG16(BX) = 0x0805; // V8.05
13625: REG16(CX) = 0x0400; // PS/2
13626: }
13627:
13628: inline void msdos_int_33h_0026h()
13629: {
13630: REG16(BX) = 0x0000;
13631: REG16(CX) = mouse.max_position.x;
13632: REG16(DX) = mouse.max_position.y;
13633: }
13634:
13635: inline void msdos_int_33h_002ah()
13636: {
1.1.1.34 root 13637: REG16(AX) = -mouse.hidden;
1.1.1.24 root 13638: REG16(BX) = mouse.hot_spot[0];
13639: REG16(CX) = mouse.hot_spot[1];
13640: REG16(DX) = 4; // PS/2
13641: }
13642:
13643: inline void msdos_int_33h_0031h()
13644: {
13645: REG16(AX) = mouse.min_position.x;
13646: REG16(BX) = mouse.min_position.y;
13647: REG16(CX) = mouse.max_position.x;
13648: REG16(DX) = mouse.max_position.y;
13649: }
13650:
13651: inline void msdos_int_33h_0032h()
13652: {
13653: REG16(AX) = 0;
13654: // REG16(AX) |= 0x8000; // 0025h
13655: REG16(AX) |= 0x4000; // 0026h
13656: // REG16(AX) |= 0x2000; // 0027h
13657: // REG16(AX) |= 0x1000; // 0028h
13658: // REG16(AX) |= 0x0800; // 0029h
13659: REG16(AX) |= 0x0400; // 002ah
13660: // REG16(AX) |= 0x0200; // 002bh
13661: // REG16(AX) |= 0x0100; // 002ch
13662: // REG16(AX) |= 0x0080; // 002dh
13663: // REG16(AX) |= 0x0040; // 002eh
13664: REG16(AX) |= 0x0020; // 002fh
13665: // REG16(AX) |= 0x0010; // 0030h
13666: REG16(AX) |= 0x0008; // 0031h
13667: REG16(AX) |= 0x0004; // 0032h
13668: // REG16(AX) |= 0x0002; // 0033h
13669: // REG16(AX) |= 0x0001; // 0034h
13670: }
13671:
1.1.1.19 root 13672: inline void msdos_int_67h_40h()
13673: {
13674: if(!support_ems) {
13675: REG8(AH) = 0x84;
13676: } else {
13677: REG8(AH) = 0x00;
13678: }
13679: }
13680:
13681: inline void msdos_int_67h_41h()
13682: {
13683: if(!support_ems) {
13684: REG8(AH) = 0x84;
13685: } else {
13686: REG8(AH) = 0x00;
13687: REG16(BX) = EMS_TOP >> 4;
13688: }
13689: }
13690:
13691: inline void msdos_int_67h_42h()
13692: {
13693: if(!support_ems) {
13694: REG8(AH) = 0x84;
13695: } else {
13696: REG8(AH) = 0x00;
13697: REG16(BX) = free_ems_pages;
13698: REG16(DX) = MAX_EMS_PAGES;
13699: }
13700: }
13701:
13702: inline void msdos_int_67h_43h()
13703: {
13704: if(!support_ems) {
13705: REG8(AH) = 0x84;
13706: } else if(REG16(BX) > MAX_EMS_PAGES) {
13707: REG8(AH) = 0x87;
13708: } else if(REG16(BX) > free_ems_pages) {
13709: REG8(AH) = 0x88;
13710: } else if(REG16(BX) == 0) {
13711: REG8(AH) = 0x89;
13712: } else {
1.1.1.31 root 13713: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 13714: if(!ems_handles[i].allocated) {
13715: ems_allocate_pages(i, REG16(BX));
13716: REG8(AH) = 0x00;
13717: REG16(DX) = i;
13718: return;
13719: }
13720: }
13721: REG8(AH) = 0x85;
13722: }
13723: }
13724:
13725: inline void msdos_int_67h_44h()
13726: {
13727: if(!support_ems) {
13728: REG8(AH) = 0x84;
1.1.1.31 root 13729: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13730: REG8(AH) = 0x83;
13731: } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
13732: REG8(AH) = 0x8a;
13733: // } else if(!(REG8(AL) < 4)) {
13734: // REG8(AH) = 0x8b;
13735: } else if(REG16(BX) == 0xffff) {
13736: ems_unmap_page(REG8(AL) & 3);
13737: REG8(AH) = 0x00;
13738: } else {
13739: ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
13740: REG8(AH) = 0x00;
13741: }
13742: }
13743:
13744: inline void msdos_int_67h_45h()
13745: {
13746: if(!support_ems) {
13747: REG8(AH) = 0x84;
1.1.1.31 root 13748: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13749: REG8(AH) = 0x83;
13750: } else {
13751: ems_release_pages(REG16(DX));
13752: REG8(AH) = 0x00;
13753: }
13754: }
13755:
13756: inline void msdos_int_67h_46h()
13757: {
13758: if(!support_ems) {
13759: REG8(AH) = 0x84;
13760: } else {
1.1.1.29 root 13761: // REG16(AX) = 0x0032; // EMS 3.2
13762: REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19 root 13763: }
13764: }
13765:
13766: inline void msdos_int_67h_47h()
13767: {
13768: // NOTE: the map data should be stored in the specified ems page, not process data
13769: process_t *process = msdos_process_info_get(current_psp);
13770:
13771: if(!support_ems) {
13772: REG8(AH) = 0x84;
1.1.1.31 root 13773: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13774: // REG8(AH) = 0x83;
13775: } else if(process->ems_pages_stored) {
13776: REG8(AH) = 0x8d;
13777: } else {
13778: for(int i = 0; i < 4; i++) {
13779: process->ems_pages[i].handle = ems_pages[i].handle;
13780: process->ems_pages[i].page = ems_pages[i].page;
13781: process->ems_pages[i].mapped = ems_pages[i].mapped;
13782: }
13783: process->ems_pages_stored = true;
13784: REG8(AH) = 0x00;
13785: }
13786: }
13787:
13788: inline void msdos_int_67h_48h()
13789: {
13790: // NOTE: the map data should be restored from the specified ems page, not process data
13791: process_t *process = msdos_process_info_get(current_psp);
13792:
13793: if(!support_ems) {
13794: REG8(AH) = 0x84;
1.1.1.31 root 13795: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13796: // REG8(AH) = 0x83;
13797: } else if(!process->ems_pages_stored) {
13798: REG8(AH) = 0x8e;
13799: } else {
13800: for(int i = 0; i < 4; i++) {
13801: if(process->ems_pages[i].mapped) {
13802: ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
13803: } else {
13804: ems_unmap_page(i);
13805: }
13806: }
13807: process->ems_pages_stored = false;
13808: REG8(AH) = 0x00;
13809: }
13810: }
13811:
13812: inline void msdos_int_67h_4bh()
13813: {
13814: if(!support_ems) {
13815: REG8(AH) = 0x84;
13816: } else {
13817: REG8(AH) = 0x00;
13818: REG16(BX) = 0;
1.1.1.31 root 13819: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 13820: if(ems_handles[i].allocated) {
13821: REG16(BX)++;
13822: }
13823: }
13824: }
13825: }
13826:
13827: inline void msdos_int_67h_4ch()
13828: {
13829: if(!support_ems) {
13830: REG8(AH) = 0x84;
1.1.1.31 root 13831: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13832: REG8(AH) = 0x83;
13833: } else {
13834: REG8(AH) = 0x00;
13835: REG16(BX) = ems_handles[REG16(DX)].pages;
13836: }
13837: }
13838:
13839: inline void msdos_int_67h_4dh()
13840: {
13841: if(!support_ems) {
13842: REG8(AH) = 0x84;
13843: } else {
13844: REG8(AH) = 0x00;
13845: REG16(BX) = 0;
1.1.1.31 root 13846: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 13847: if(ems_handles[i].allocated) {
13848: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
13849: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
13850: REG16(BX)++;
13851: }
13852: }
13853: }
13854: }
13855:
1.1.1.20 root 13856: inline void msdos_int_67h_4eh()
13857: {
13858: if(!support_ems) {
13859: REG8(AH) = 0x84;
13860: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
13861: if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
13862: // save page map
13863: for(int i = 0; i < 4; i++) {
13864: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
13865: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page : 0xffff;
13866: }
13867: }
13868: if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
13869: // restore page map
13870: for(int i = 0; i < 4; i++) {
13871: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
13872: UINT16 page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
13873:
1.1.1.31 root 13874: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20 root 13875: ems_map_page(i, handle, page);
13876: } else {
13877: ems_unmap_page(i);
13878: }
13879: }
13880: }
13881: REG8(AH) = 0x00;
13882: } else if(REG8(AL) == 0x03) {
13883: REG8(AH) = 0x00;
1.1.1.21 root 13884: REG8(AL) = 4 * 4;
13885: } else {
1.1.1.22 root 13886: 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 13887: REG8(AH) = 0x8f;
13888: }
13889: }
13890:
13891: inline void msdos_int_67h_4fh()
13892: {
13893: if(!support_ems) {
13894: REG8(AH) = 0x84;
13895: } else if(REG8(AL) == 0x00) {
13896: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
13897:
13898: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
13899: for(int i = 0; i < count; i++) {
13900: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
13901: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
13902:
13903: // if(!(physical < 4)) {
13904: // REG8(AH) = 0x8b;
13905: // return;
13906: // }
13907: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41 root 13908: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
13909: *(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 13910: }
13911: REG8(AH) = 0x00;
13912: } else if(REG8(AL) == 0x01) {
13913: int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
13914:
13915: for(int i = 0; i < count; i++) {
13916: UINT16 segment = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
13917: UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
13918: UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
13919: UINT16 logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
13920:
13921: // if(!(physical < 4)) {
13922: // REG8(AH) = 0x8b;
13923: // return;
13924: // } else
1.1.1.41 root 13925: if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21 root 13926: ems_map_page(physical & 3, handle, logical);
13927: } else {
1.1.1.41 root 13928: ems_unmap_page(physical & 3);
1.1.1.21 root 13929: }
13930: }
13931: REG8(AH) = 0x00;
13932: } else if(REG8(AL) == 0x02) {
13933: REG8(AH) = 0x00;
13934: REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20 root 13935: } else {
1.1.1.22 root 13936: 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 13937: REG8(AH) = 0x8f;
13938: }
13939: }
13940:
13941: inline void msdos_int_67h_50h()
13942: {
13943: if(!support_ems) {
13944: REG8(AH) = 0x84;
1.1.1.31 root 13945: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20 root 13946: REG8(AH) = 0x83;
13947: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
13948: for(int i = 0; i < REG16(CX); i++) {
13949: int logical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
13950: int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
13951:
13952: if(REG8(AL) == 0x01) {
13953: physical = ((physical << 4) - EMS_TOP) / 0x4000;
13954: }
13955: // if(!(physical < 4)) {
13956: // REG8(AH) = 0x8b;
13957: // return;
13958: // } else
13959: if(logical == 0xffff) {
13960: ems_unmap_page(physical & 3);
13961: } else if(logical < ems_handles[REG16(DX)].pages) {
13962: ems_map_page(physical & 3, REG16(DX), logical);
13963: } else {
13964: REG8(AH) = 0x8a;
13965: return;
13966: }
13967: }
13968: REG8(AH) = 0x00;
13969: } else {
1.1.1.22 root 13970: 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 13971: REG8(AH) = 0x8f;
13972: }
13973: }
13974:
1.1.1.19 root 13975: inline void msdos_int_67h_51h()
13976: {
13977: if(!support_ems) {
13978: REG8(AH) = 0x84;
1.1.1.31 root 13979: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 13980: REG8(AH) = 0x83;
13981: } else if(REG16(BX) > MAX_EMS_PAGES) {
13982: REG8(AH) = 0x87;
13983: } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
13984: REG8(AH) = 0x88;
13985: } else {
13986: ems_reallocate_pages(REG16(DX), REG16(BX));
13987: REG8(AH) = 0x00;
13988: }
13989: }
13990:
1.1.1.20 root 13991: inline void msdos_int_67h_52h()
13992: {
13993: if(!support_ems) {
13994: REG8(AH) = 0x84;
1.1.1.31 root 13995: // } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
13996: // REG8(AH) = 0x83;
1.1.1.20 root 13997: } else if(REG8(AL) == 0x00) {
13998: REG8(AL) = 0x00; // handle is volatile
13999: REG8(AH) = 0x00;
14000: } else if(REG8(AL) == 0x01) {
14001: if(REG8(BL) == 0x00) {
14002: REG8(AH) = 0x00;
14003: } else {
14004: REG8(AH) = 0x90; // undefined attribute type
14005: }
14006: } else if(REG8(AL) == 0x02) {
14007: REG8(AL) = 0x00; // only volatile handles supported
14008: REG8(AH) = 0x00;
14009: } else {
1.1.1.22 root 14010: 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 14011: REG8(AH) = 0x8f;
14012: }
14013: }
14014:
1.1.1.19 root 14015: inline void msdos_int_67h_53h()
14016: {
14017: if(!support_ems) {
14018: REG8(AH) = 0x84;
1.1.1.31 root 14019: } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19 root 14020: REG8(AH) = 0x83;
14021: } else if(REG8(AL) == 0x00) {
14022: memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
14023: REG8(AH) = 0x00;
14024: } else if(REG8(AL) == 0x01) {
1.1.1.31 root 14025: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14026: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
14027: REG8(AH) = 0xa1;
14028: return;
14029: }
14030: }
14031: REG8(AH) = 0x00;
14032: memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
14033: } else {
1.1.1.22 root 14034: 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 14035: REG8(AH) = 0x8f;
1.1.1.19 root 14036: }
14037: }
14038:
14039: inline void msdos_int_67h_54h()
14040: {
14041: if(!support_ems) {
14042: REG8(AH) = 0x84;
14043: } else if(REG8(AL) == 0x00) {
1.1.1.31 root 14044: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14045: if(ems_handles[i].allocated) {
14046: memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
14047: } else {
14048: memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
14049: }
14050: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
14051: }
14052: REG8(AH) = 0x00;
14053: REG8(AL) = MAX_EMS_HANDLES;
14054: } else if(REG8(AL) == 0x01) {
14055: REG8(AH) = 0xa0; // not found
1.1.1.31 root 14056: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19 root 14057: if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
14058: REG8(AH) = 0x00;
14059: REG16(DX) = i;
14060: break;
14061: }
14062: }
14063: } else if(REG8(AL) == 0x02) {
14064: REG8(AH) = 0x00;
14065: REG16(BX) = MAX_EMS_HANDLES;
14066: } else {
1.1.1.22 root 14067: 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 14068: REG8(AH) = 0x8f;
14069: }
14070: }
14071:
14072: inline void msdos_int_67h_57h_tmp()
14073: {
14074: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
14075: UINT8 src_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
14076: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
14077: UINT16 src_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
14078: UINT16 src_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
14079: UINT8 dest_type = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
14080: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
14081: UINT16 dest_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
14082: UINT16 dest_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
14083:
1.1.1.32 root 14084: UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20 root 14085: UINT32 src_addr, dest_addr;
14086: UINT32 src_addr_max, dest_addr_max;
14087:
14088: if(src_type == 0) {
14089: src_buffer = mem;
14090: src_addr = (src_seg << 4) + src_ofs;
14091: src_addr_max = MAX_MEM;
14092: } else {
1.1.1.31 root 14093: if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20 root 14094: REG8(AH) = 0x83;
14095: return;
14096: } else if(!(src_seg < ems_handles[src_handle].pages)) {
14097: REG8(AH) = 0x8a;
14098: return;
14099: }
1.1.1.32 root 14100: if(ems_handles[src_handle].buffer != NULL) {
14101: src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
14102: }
1.1.1.20 root 14103: src_addr = src_ofs;
1.1.1.32 root 14104: src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20 root 14105: }
14106: if(dest_type == 0) {
14107: dest_buffer = mem;
14108: dest_addr = (dest_seg << 4) + dest_ofs;
14109: dest_addr_max = MAX_MEM;
14110: } else {
1.1.1.31 root 14111: if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20 root 14112: REG8(AH) = 0x83;
14113: return;
14114: } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
14115: REG8(AH) = 0x8a;
14116: return;
14117: }
1.1.1.32 root 14118: if(ems_handles[dest_handle].buffer != NULL) {
14119: dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
14120: }
1.1.1.20 root 14121: dest_addr = dest_ofs;
1.1.1.32 root 14122: dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20 root 14123: }
1.1.1.32 root 14124: if(src_buffer != NULL && dest_buffer != NULL) {
14125: for(int i = 0; i < copy_length; i++) {
14126: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
14127: if(REG8(AL) == 0x00) {
14128: dest_buffer[dest_addr++] = src_buffer[src_addr++];
14129: } else if(REG8(AL) == 0x01) {
14130: UINT8 tmp = dest_buffer[dest_addr];
14131: dest_buffer[dest_addr++] = src_buffer[src_addr];
14132: src_buffer[src_addr++] = tmp;
14133: }
14134: } else {
14135: REG8(AH) = 0x93;
14136: return;
1.1.1.20 root 14137: }
14138: }
1.1.1.32 root 14139: REG8(AH) = 0x00;
14140: } else {
14141: REG8(AH) = 0x80;
1.1.1.20 root 14142: }
14143: }
14144:
14145: inline void msdos_int_67h_57h()
14146: {
14147: if(!support_ems) {
14148: REG8(AH) = 0x84;
14149: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
14150: struct {
14151: UINT16 handle;
14152: UINT16 page;
14153: bool mapped;
14154: } tmp_pages[4];
14155:
14156: // unmap pages to copy memory data to ems buffer
14157: for(int i = 0; i < 4; i++) {
14158: tmp_pages[i].handle = ems_pages[i].handle;
14159: tmp_pages[i].page = ems_pages[i].page;
14160: tmp_pages[i].mapped = ems_pages[i].mapped;
14161: ems_unmap_page(i);
14162: }
14163:
14164: // run move/exchange operation
14165: msdos_int_67h_57h_tmp();
14166:
14167: // restore unmapped pages
14168: for(int i = 0; i < 4; i++) {
14169: if(tmp_pages[i].mapped) {
14170: ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
14171: }
14172: }
14173: } else {
1.1.1.22 root 14174: 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 14175: REG8(AH) = 0x8f;
14176: }
14177: }
14178:
14179: inline void msdos_int_67h_58h()
14180: {
14181: if(!support_ems) {
14182: REG8(AH) = 0x84;
14183: } else if(REG8(AL) == 0x00) {
14184: for(int i = 0; i < 4; i++) {
1.1.1.30 root 14185: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
14186: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20 root 14187: }
14188: REG8(AH) = 0x00;
14189: REG16(CX) = 4;
14190: } else if(REG8(AL) == 0x01) {
14191: REG8(AH) = 0x00;
14192: REG16(CX) = 4;
14193: } else {
1.1.1.22 root 14194: 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 14195: REG8(AH) = 0x8f;
14196: }
14197: }
14198:
1.1.1.42! root 14199: inline void msdos_int_67h_59h()
! 14200: {
! 14201: if(!support_ems) {
! 14202: REG8(AH) = 0x84;
! 14203: } else if(REG8(AL) == 0x00) {
! 14204: REG8(AH) = 0xa4; // access denied by operating system
! 14205: } else if(REG8(AL) == 0x01) {
! 14206: REG8(AH) = 0x00;
! 14207: REG16(BX) = free_ems_pages;
! 14208: REG16(DX) = MAX_EMS_PAGES;
! 14209: } else {
! 14210: 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));
! 14211: REG8(AH) = 0x8f;
! 14212: }
! 14213: }
! 14214:
1.1.1.20 root 14215: inline void msdos_int_67h_5ah()
14216: {
14217: if(!support_ems) {
1.1.1.19 root 14218: REG8(AH) = 0x84;
1.1.1.20 root 14219: } else if(REG16(BX) > MAX_EMS_PAGES) {
14220: REG8(AH) = 0x87;
14221: } else if(REG16(BX) > free_ems_pages) {
14222: REG8(AH) = 0x88;
14223: // } else if(REG16(BX) == 0) {
14224: // REG8(AH) = 0x89;
14225: } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31 root 14226: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20 root 14227: if(!ems_handles[i].allocated) {
14228: ems_allocate_pages(i, REG16(BX));
14229: REG8(AH) = 0x00;
14230: REG16(DX) = i;
14231: return;
14232: }
14233: }
14234: REG8(AH) = 0x85;
14235: } else {
1.1.1.22 root 14236: 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 14237: REG8(AH) = 0x8f;
1.1.1.19 root 14238: }
14239: }
14240:
1.1.1.30 root 14241: inline void msdos_int_67h_deh()
14242: {
14243: REG8(AH) = 0x84;
14244: }
14245:
1.1.1.19 root 14246: #ifdef SUPPORT_XMS
14247:
1.1.1.32 root 14248: void msdos_xms_init()
1.1.1.26 root 14249: {
1.1.1.30 root 14250: emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
14251: emb_handle_top->address = EMB_TOP;
14252: emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26 root 14253: xms_a20_local_enb_count = 0;
14254: }
14255:
1.1.1.32 root 14256: void msdos_xms_finish()
14257: {
14258: msdos_xms_release();
14259: }
14260:
14261: void msdos_xms_release()
1.1.1.30 root 14262: {
14263: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
14264: emb_handle_t *next_handle = emb_handle->next;
14265: free(emb_handle);
14266: emb_handle = next_handle;
14267: }
14268: }
14269:
14270: emb_handle_t *msdos_xms_get_emb_handle(int handle)
14271: {
14272: if(handle != 0) {
14273: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
14274: if(emb_handle->handle == handle) {
14275: return(emb_handle);
14276: }
14277: }
14278: }
14279: return(NULL);
14280: }
14281:
14282: int msdos_xms_get_unused_emb_handle_id()
14283: {
14284: for(int handle = 1;; handle++) {
14285: if(msdos_xms_get_emb_handle(handle) == NULL) {
14286: return(handle);
14287: }
14288: }
14289: return(0);
14290: }
14291:
14292: int msdos_xms_get_unused_emb_handle_count()
14293: {
14294: int count = 64; //255;
14295:
14296: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
14297: if(emb_handle->handle != 0) {
14298: if(--count == 1) {
14299: break;
14300: }
14301: }
14302: }
14303: return(count);
14304: }
14305:
14306: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
14307: {
14308: if(emb_handle->size_kb > size_kb) {
14309: emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
14310:
14311: new_handle->address = emb_handle->address + size_kb * 1024;
14312: new_handle->size_kb = emb_handle->size_kb - size_kb;
14313: emb_handle->size_kb = size_kb;
14314:
14315: new_handle->prev = emb_handle;
14316: new_handle->next = emb_handle->next;
14317: if(emb_handle->next != NULL) {
14318: emb_handle->next->prev = new_handle;
14319: }
14320: emb_handle->next = new_handle;
14321: }
14322: }
14323:
14324: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
14325: {
14326: emb_handle_t *next_handle = emb_handle->next;
14327:
14328: if(next_handle != NULL) {
14329: emb_handle->size_kb += next_handle->size_kb;
14330:
14331: if(next_handle->next != NULL) {
14332: next_handle->next->prev = emb_handle;
14333: }
14334: emb_handle->next = next_handle->next;
14335: free(next_handle);
14336: }
14337: }
14338:
14339: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
14340: {
14341: emb_handle_t *target_handle = NULL;
14342:
14343: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
14344: if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
14345: if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
14346: target_handle = emb_handle;
14347: }
14348: }
14349: }
14350: if(target_handle != NULL) {
14351: if(target_handle->size_kb > size_kb) {
14352: msdos_xms_split_emb_handle(target_handle, size_kb);
14353: }
14354: // target_handle->handle = msdos_xms_get_unused_emb_handle_id();
14355: return(target_handle);
14356: }
14357: return(NULL);
14358: }
14359:
14360: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
14361: {
14362: emb_handle_t *prev_handle = emb_handle->prev;
14363: emb_handle_t *next_handle = emb_handle->next;
14364:
14365: if(prev_handle != NULL && prev_handle->handle == 0) {
14366: msdos_xms_combine_emb_handles(prev_handle);
14367: emb_handle = prev_handle;
14368: }
14369: if(next_handle != NULL && next_handle->handle == 0) {
14370: msdos_xms_combine_emb_handles(emb_handle);
14371: }
14372: emb_handle->handle = 0;
14373: }
14374:
1.1.1.19 root 14375: inline void msdos_call_xms_00h()
14376: {
1.1.1.29 root 14377: #if defined(HAS_I386)
14378: REG16(AX) = 0x0300; // V3.00 (XMS Version)
14379: REG16(BX) = 0x035f; // V3.95 (Driver Revision)
14380: #else
14381: REG16(AX) = 0x0200; // V2.00 (XMS Version)
14382: REG16(BX) = 0x0270; // V2.70 (Driver Revision)
14383: #endif
14384: // REG16(DX) = 0x0000; // HMA does not exist
14385: REG16(DX) = 0x0001; // HMA does exist
1.1.1.19 root 14386: }
14387:
14388: inline void msdos_call_xms_01h()
14389: {
1.1.1.29 root 14390: if(REG8(AL) == 0x40) {
14391: // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
14392: // DX=KB free extended memory returned by last call of function 08h
14393: REG16(AX) = 0x0000;
14394: REG8(BL) = 0x91;
14395: REG16(DX) = xms_dx_after_call_08h;
14396: } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
14397: REG16(AX) = 0x0000;
14398: REG8(BL) = 0x81; // Vdisk was detected
14399: #ifdef SUPPORT_HMA
14400: } else if(is_hma_used_by_int_2fh) {
14401: REG16(AX) = 0x0000;
14402: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
14403: } else if(is_hma_used_by_xms) {
14404: REG16(AX) = 0x0000;
14405: REG8(BL) = 0x91; // HMA is already in use
14406: } else {
14407: REG16(AX) = 0x0001;
14408: is_hma_used_by_xms = true;
14409: #else
14410: } else {
14411: REG16(AX) = 0x0000;
14412: REG8(BL) = 0x91; // HMA is already in use
14413: #endif
14414: }
1.1.1.19 root 14415: }
14416:
14417: inline void msdos_call_xms_02h()
14418: {
1.1.1.29 root 14419: if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
14420: REG16(AX) = 0x0000;
14421: REG8(BL) = 0x81; // Vdisk was detected
14422: #ifdef SUPPORT_HMA
14423: } else if(is_hma_used_by_int_2fh) {
14424: REG16(AX) = 0x0000;
14425: REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
14426: } else if(!is_hma_used_by_xms) {
14427: REG16(AX) = 0x0000;
14428: REG8(BL) = 0x93; // HMA is not allocated
14429: } else {
14430: REG16(AX) = 0x0001;
14431: is_hma_used_by_xms = false;
14432: // restore first free mcb in high memory area
14433: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
14434: #else
14435: } else {
14436: REG16(AX) = 0x0000;
14437: REG8(BL) = 0x91; // HMA is already in use
14438: #endif
14439: }
1.1.1.19 root 14440: }
14441:
14442: inline void msdos_call_xms_03h()
14443: {
14444: i386_set_a20_line(1);
14445: REG16(AX) = 0x0001;
14446: REG8(BL) = 0x00;
14447: }
14448:
14449: inline void msdos_call_xms_04h()
14450: {
1.1.1.21 root 14451: i386_set_a20_line(0);
14452: REG16(AX) = 0x0001;
14453: REG8(BL) = 0x00;
1.1.1.19 root 14454: }
14455:
14456: inline void msdos_call_xms_05h()
14457: {
14458: i386_set_a20_line(1);
14459: REG16(AX) = 0x0001;
14460: REG8(BL) = 0x00;
1.1.1.21 root 14461: xms_a20_local_enb_count++;
1.1.1.19 root 14462: }
14463:
14464: void msdos_call_xms_06h()
14465: {
1.1.1.21 root 14466: if(xms_a20_local_enb_count > 0) {
14467: xms_a20_local_enb_count--;
14468: }
14469: if(xms_a20_local_enb_count == 0) {
1.1.1.19 root 14470: i386_set_a20_line(0);
1.1.1.21 root 14471: }
14472: if((m_a20_mask >> 20) & 1) {
1.1.1.19 root 14473: REG16(AX) = 0x0000;
14474: REG8(BL) = 0x94;
1.1.1.21 root 14475: } else {
14476: REG16(AX) = 0x0001;
14477: REG8(BL) = 0x00;
1.1.1.19 root 14478: }
14479: }
14480:
14481: inline void msdos_call_xms_07h()
14482: {
14483: REG16(AX) = (m_a20_mask >> 20) & 1;
14484: REG8(BL) = 0x00;
14485: }
14486:
14487: inline void msdos_call_xms_08h()
14488: {
14489: REG16(AX) = REG16(DX) = 0x0000;
14490:
1.1.1.30 root 14491: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
14492: if(emb_handle->handle == 0) {
14493: if(REG16(AX) < emb_handle->size_kb) {
14494: REG16(AX) = emb_handle->size_kb;
1.1.1.19 root 14495: }
1.1.1.30 root 14496: REG16(DX) += emb_handle->size_kb;
1.1.1.19 root 14497: }
14498: }
14499:
14500: if(REG16(AX) == 0 && REG16(DX) == 0) {
14501: REG8(BL) = 0xa0;
14502: } else {
14503: REG8(BL) = 0x00;
14504: }
1.1.1.29 root 14505: xms_dx_after_call_08h = REG16(DX);
1.1.1.19 root 14506: }
14507:
1.1.1.30 root 14508: void msdos_call_xms_09h(int size_kb)
1.1.1.19 root 14509: {
1.1.1.30 root 14510: emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
14511:
14512: if(emb_handle != NULL) {
14513: emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
14514:
14515: REG16(AX) = 0x0001;
14516: REG16(DX) = emb_handle->handle;
14517: REG8(BL) = 0x00;
14518: } else {
14519: REG16(AX) = REG16(DX) = 0x0000;
14520: REG8(BL) = 0xa0;
1.1.1.19 root 14521: }
1.1.1.30 root 14522: }
14523:
14524: inline void msdos_call_xms_09h()
14525: {
14526: msdos_call_xms_09h(REG16(DX));
1.1.1.19 root 14527: }
14528:
14529: inline void msdos_call_xms_0ah()
14530: {
1.1.1.30 root 14531: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14532:
14533: if(emb_handle == NULL) {
1.1.1.19 root 14534: REG16(AX) = 0x0000;
14535: REG8(BL) = 0xa2;
1.1.1.30 root 14536: } else if(emb_handle->lock > 0) {
1.1.1.19 root 14537: REG16(AX) = 0x0000;
14538: REG8(BL) = 0xab;
14539: } else {
1.1.1.30 root 14540: msdos_xms_free_emb_handle(emb_handle);
1.1.1.19 root 14541:
14542: REG16(AX) = 0x0001;
14543: REG8(BL) = 0x00;
14544: }
14545: }
14546:
14547: inline void msdos_call_xms_0bh()
14548: {
14549: UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
14550: UINT16 src_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
14551: UINT32 src_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
14552: UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
14553: UINT32 dest_addr = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
14554:
14555: UINT8 *src_buffer, *dest_buffer;
14556: UINT32 src_addr_max, dest_addr_max;
1.1.1.30 root 14557: emb_handle_t *emb_handle;
1.1.1.19 root 14558:
14559: if(src_handle == 0) {
14560: src_buffer = mem;
14561: src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
14562: src_addr_max = MAX_MEM;
1.1.1.30 root 14563:
1.1.1.19 root 14564: } else {
1.1.1.30 root 14565: if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19 root 14566: REG16(AX) = 0x0000;
14567: REG8(BL) = 0xa3;
14568: return;
14569: }
1.1.1.30 root 14570: src_buffer = mem + emb_handle->address;
14571: src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 14572: }
14573: if(dest_handle == 0) {
14574: dest_buffer = mem;
14575: dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
14576: dest_addr_max = MAX_MEM;
14577: } else {
1.1.1.30 root 14578: if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19 root 14579: REG16(AX) = 0x0000;
14580: REG8(BL) = 0xa5;
14581: return;
14582: }
1.1.1.30 root 14583: dest_buffer = mem + emb_handle->address;
14584: dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19 root 14585: }
14586: for(int i = 0; i < copy_length; i++) {
14587: if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
14588: dest_buffer[dest_addr++] = src_buffer[src_addr++];
14589: } else {
14590: break;
14591: }
14592: }
14593: REG16(AX) = 0x0001;
14594: REG8(BL) = 0x00;
14595: }
14596:
14597: inline void msdos_call_xms_0ch()
14598: {
1.1.1.30 root 14599: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14600:
14601: if(emb_handle == NULL) {
1.1.1.19 root 14602: REG16(AX) = 0x0000;
14603: REG8(BL) = 0xa2;
14604: } else {
1.1.1.30 root 14605: emb_handle->lock++;
1.1.1.19 root 14606: REG16(AX) = 0x0001;
14607: REG8(BL) = 0x00;
1.1.1.30 root 14608: REG16(DX) = (emb_handle->address >> 16) & 0xffff;
14609: REG16(BX) = (emb_handle->address ) & 0xffff;
1.1.1.19 root 14610: }
14611: }
14612:
14613: inline void msdos_call_xms_0dh()
14614: {
1.1.1.30 root 14615: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14616:
14617: if(emb_handle == NULL) {
1.1.1.19 root 14618: REG16(AX) = 0x0000;
14619: REG8(BL) = 0xa2;
1.1.1.30 root 14620: } else if(!(emb_handle->lock > 0)) {
1.1.1.19 root 14621: REG16(AX) = 0x0000;
14622: REG8(BL) = 0xaa;
14623: } else {
1.1.1.30 root 14624: emb_handle->lock--;
1.1.1.19 root 14625: REG16(AX) = 0x0001;
14626: REG8(BL) = 0x00;
14627: }
14628: }
14629:
14630: inline void msdos_call_xms_0eh()
14631: {
1.1.1.30 root 14632: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14633:
14634: if(emb_handle == NULL) {
1.1.1.19 root 14635: REG16(AX) = 0x0000;
14636: REG8(BL) = 0xa2;
14637: } else {
14638: REG16(AX) = 0x0001;
1.1.1.30 root 14639: REG8(BH) = emb_handle->lock;
14640: REG8(BL) = msdos_xms_get_unused_emb_handle_count();
14641: REG16(DX) = emb_handle->size_kb;
1.1.1.19 root 14642: }
14643: }
14644:
1.1.1.30 root 14645: void msdos_call_xms_0fh(int size_kb)
1.1.1.19 root 14646: {
1.1.1.30 root 14647: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14648:
14649: if(emb_handle == NULL) {
1.1.1.19 root 14650: REG16(AX) = 0x0000;
14651: REG8(BL) = 0xa2;
1.1.1.30 root 14652: } else if(emb_handle->lock > 0) {
1.1.1.19 root 14653: REG16(AX) = 0x0000;
14654: REG8(BL) = 0xab;
14655: } else {
1.1.1.30 root 14656: if(emb_handle->size_kb < size_kb) {
14657: if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
14658: msdos_xms_combine_emb_handles(emb_handle);
14659: if(emb_handle->size_kb > size_kb) {
14660: msdos_xms_split_emb_handle(emb_handle, size_kb);
14661: }
14662: } else {
14663: int old_handle = emb_handle->handle;
14664: int old_size_kb = emb_handle->size_kb;
14665: UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
14666:
14667: memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
14668: msdos_xms_free_emb_handle(emb_handle);
14669:
14670: if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
14671: emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
14672: }
14673: emb_handle->handle = old_handle;
14674: memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
14675: free(buffer);
14676: }
14677: } else if(emb_handle->size_kb > size_kb) {
14678: msdos_xms_split_emb_handle(emb_handle, size_kb);
14679: }
14680: if(emb_handle->size_kb != size_kb) {
14681: REG16(AX) = 0x0000;
14682: REG8(BL) = 0xa0;
14683: } else {
14684: REG16(AX) = 0x0001;
14685: REG8(BL) = 0x00;
14686: }
1.1.1.19 root 14687: }
14688: }
14689:
1.1.1.30 root 14690: inline void msdos_call_xms_0fh()
14691: {
14692: msdos_call_xms_0fh(REG16(BX));
14693: }
14694:
1.1.1.19 root 14695: inline void msdos_call_xms_10h()
14696: {
14697: int seg;
14698:
14699: if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
14700: REG16(AX) = 0x0001;
14701: REG16(BX) = seg;
14702: } else {
14703: REG16(AX) = 0x0000;
14704: REG8(BL) = 0xb0;
14705: REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
14706: }
14707: }
14708:
14709: inline void msdos_call_xms_11h()
14710: {
14711: int mcb_seg = REG16(DX) - 1;
14712: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
14713:
14714: if(mcb->mz == 'M' || mcb->mz == 'Z') {
14715: msdos_mem_free(REG16(DX));
14716: REG16(AX) = 0x0001;
14717: REG8(BL) = 0x00;
14718: } else {
14719: REG16(AX) = 0x0000;
14720: REG8(BL) = 0xb2;
14721: }
14722: }
14723:
14724: inline void msdos_call_xms_12h()
14725: {
14726: int mcb_seg = REG16(DX) - 1;
14727: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
14728: int max_paragraphs;
14729:
14730: if(mcb->mz == 'M' || mcb->mz == 'Z') {
14731: if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
14732: REG16(AX) = 0x0001;
14733: REG8(BL) = 0x00;
14734: } else {
14735: REG16(AX) = 0x0000;
14736: REG8(BL) = 0xb0;
14737: REG16(DX) = max_paragraphs;
14738: }
14739: } else {
14740: REG16(AX) = 0x0000;
14741: REG8(BL) = 0xb2;
14742: }
14743: }
14744:
1.1.1.29 root 14745: #if defined(HAS_I386)
14746:
14747: inline void msdos_call_xms_88h()
14748: {
14749: REG32(EAX) = REG32(EDX) = 0x0000;
14750:
1.1.1.30 root 14751: for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
14752: if(emb_handle->handle == 0) {
14753: if(REG32(EAX) < emb_handle->size_kb) {
14754: REG32(EAX) = emb_handle->size_kb;
1.1.1.29 root 14755: }
1.1.1.30 root 14756: REG32(EDX) += emb_handle->size_kb;
1.1.1.29 root 14757: }
14758: }
14759:
14760: if(REG32(EAX) == 0 && REG32(EDX) == 0) {
14761: REG8(BL) = 0xa0;
14762: } else {
14763: REG8(BL) = 0x00;
14764: }
14765: REG32(ECX) = EMB_END - 1;
14766: }
14767:
14768: inline void msdos_call_xms_89h()
14769: {
1.1.1.30 root 14770: msdos_call_xms_09h(REG32(EDX));
1.1.1.29 root 14771: }
14772:
14773: inline void msdos_call_xms_8eh()
14774: {
1.1.1.30 root 14775: emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
14776:
14777: if(emb_handle == NULL) {
1.1.1.29 root 14778: REG16(AX) = 0x0000;
14779: REG8(BL) = 0xa2;
14780: } else {
14781: REG16(AX) = 0x0001;
1.1.1.30 root 14782: REG8(BH) = emb_handle->lock;
14783: REG16(CX) = msdos_xms_get_unused_emb_handle_count();
14784: REG32(EDX) = emb_handle->size_kb;
1.1.1.29 root 14785: }
14786: }
14787:
14788: inline void msdos_call_xms_8fh()
14789: {
1.1.1.30 root 14790: msdos_call_xms_0fh(REG32(EBX));
1.1.1.29 root 14791: }
14792:
14793: #endif
1.1.1.19 root 14794: #endif
14795:
1.1.1.26 root 14796: UINT16 msdos_get_equipment()
14797: {
14798: static UINT16 equip = 0;
14799:
14800: if(equip == 0) {
14801: #ifdef SUPPORT_FPU
14802: equip |= (1 << 1); // 80x87 coprocessor installed
14803: #endif
14804: equip |= (1 << 2); // pointing device installed (PS/2)
14805: equip |= (2 << 4); // initial video mode (80x25 color)
14806: // equip |= (1 << 8); // 0 if DMA installed
14807: equip |= (2 << 9); // number of serial ports
14808: 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 14809:
14810: // check only A: and B: if it is floppy drive
14811: DWORD dwDrives = GetLogicalDrives();
14812: int n = 0;
14813: for(int i = 0; i < 2; i++) {
14814: if(dwDrives & (1 << i)) {
14815: char volume[] = "A:\\";
14816: volume[0] = 'A' + i;
14817: if(GetDriveType(volume) == DRIVE_REMOVABLE) {
14818: n++;
14819: }
14820: }
14821: }
14822: if(n != 0) {
14823: equip |= (1 << 0); // floppy disk(s) installed
14824: n--;
14825: equip |= (n << 6); // number of floppies installed less 1
14826: }
14827: // if(joyGetNumDevs() != 0) {
14828: // equip |= (1 << 12); // game port installed
14829: // }
1.1.1.26 root 14830: }
14831: return(equip);
14832: }
14833:
1.1 root 14834: void msdos_syscall(unsigned num)
14835: {
1.1.1.22 root 14836: #ifdef ENABLE_DEBUG_SYSCALL
14837: if(num == 0x68) {
14838: // dummy interrupt for EMS (int 67h)
1.1.1.33 root 14839: 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 14840: } else if(num == 0x69) {
14841: // dummy interrupt for XMS (call far)
1.1.1.33 root 14842: 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.22 root 14843: } else if(num == 0x6a) {
14844: // dummy interrupt for case map routine pointed in the country info
14845: } else {
1.1.1.33 root 14846: 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 14847: }
14848: #endif
1.1.1.36 root 14849: // update cursor position
14850: if(cursor_moved) {
14851: pcbios_update_cursor_position();
14852: cursor_moved = false;
14853: }
1.1.1.33 root 14854: ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22 root 14855:
1.1 root 14856: switch(num) {
14857: case 0x00:
1.1.1.28 root 14858: try {
14859: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
14860: error("division by zero\n");
14861: } catch(...) {
14862: fatalerror("division by zero detected, and failed to terminate current process\n");
14863: }
1.1 root 14864: break;
14865: case 0x04:
1.1.1.28 root 14866: try {
14867: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
14868: error("overflow\n");
14869: } catch(...) {
14870: fatalerror("overflow detected, and failed to terminate current process\n");
14871: }
1.1 root 14872: break;
14873: case 0x06:
14874: // NOTE: ish.com has illegal instruction...
1.1.1.14 root 14875: if(!ignore_illegal_insn) {
1.1.1.28 root 14876: try {
14877: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
14878: error("illegal instruction\n");
14879: } catch(...) {
14880: fatalerror("illegal instruction detected, and failed to terminate current process\n");
14881: }
1.1.1.14 root 14882: } else {
14883: #if defined(HAS_I386)
1.1.1.39 root 14884: m_eip = m_int6h_skip_eip;
14885: #elif defined(HAS_I286)
14886: m_pc = m_int6h_skip_pc;
1.1.1.14 root 14887: #else
1.1.1.39 root 14888: // 8086/80186 ignore an invalid opcode
1.1.1.14 root 14889: #endif
14890: }
1.1 root 14891: break;
1.1.1.33 root 14892: case 0x09:
14893: // ctrl-break is pressed
14894: if(raise_int_1bh) {
14895: #if defined(HAS_I386)
14896: m_ext = 0; // not an external interrupt
14897: i386_trap(0x1b, 1, 0);
14898: m_ext = 1;
14899: #else
14900: PREFIX86(_interrupt)(0x1b);
14901: #endif
14902: raise_int_1bh = false;
14903: }
1.1.1.8 root 14904: case 0x08:
1.1.1.14 root 14905: // pcbios_irq0(); // this causes too slow emulation...
1.1.1.8 root 14906: case 0x0b:
14907: case 0x0c:
14908: case 0x0d:
14909: case 0x0e:
14910: case 0x0f:
14911: // EOI
14912: pic[0].isr &= ~(1 << (num - 0x08));
14913: pic_update();
14914: break;
1.1 root 14915: case 0x10:
14916: // PC BIOS - Video
1.1.1.14 root 14917: if(!restore_console_on_exit) {
1.1.1.15 root 14918: change_console_size(scr_width, scr_height);
1.1 root 14919: }
1.1.1.3 root 14920: m_CF = 0;
1.1 root 14921: switch(REG8(AH)) {
1.1.1.16 root 14922: case 0x00: pcbios_int_10h_00h(); break;
1.1 root 14923: case 0x01: pcbios_int_10h_01h(); break;
14924: case 0x02: pcbios_int_10h_02h(); break;
14925: case 0x03: pcbios_int_10h_03h(); break;
14926: case 0x05: pcbios_int_10h_05h(); break;
14927: case 0x06: pcbios_int_10h_06h(); break;
14928: case 0x07: pcbios_int_10h_07h(); break;
14929: case 0x08: pcbios_int_10h_08h(); break;
14930: case 0x09: pcbios_int_10h_09h(); break;
14931: case 0x0a: pcbios_int_10h_0ah(); break;
14932: case 0x0b: break;
1.1.1.40 root 14933: case 0x0c: pcbios_int_10h_0ch(); break;
14934: case 0x0d: pcbios_int_10h_0dh(); break;
1.1 root 14935: case 0x0e: pcbios_int_10h_0eh(); break;
14936: case 0x0f: pcbios_int_10h_0fh(); break;
14937: case 0x10: break;
1.1.1.14 root 14938: case 0x11: pcbios_int_10h_11h(); break;
14939: case 0x12: pcbios_int_10h_12h(); break;
1.1 root 14940: case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30 root 14941: case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14 root 14942: case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24 root 14943: case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
14944: case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1 root 14945: case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24 root 14946: case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
14947: case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22 root 14948: case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30 root 14949: case 0x6f: break;
1.1.1.22 root 14950: case 0x80: m_CF = 1; break; // unknown
14951: case 0x81: m_CF = 1; break; // unknown
1.1 root 14952: case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22 root 14953: case 0x83: pcbios_int_10h_83h(); break;
14954: case 0x8b: break;
14955: case 0x8c: m_CF = 1; break; // unknown
14956: case 0x8d: m_CF = 1; break; // unknown
14957: case 0x8e: m_CF = 1; break; // unknown
14958: case 0x90: pcbios_int_10h_90h(); break;
14959: case 0x91: pcbios_int_10h_91h(); break;
14960: case 0x92: break;
14961: case 0x93: break;
14962: case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24 root 14963: case 0xfa: break; // ega register interface library is not installed
1.1 root 14964: case 0xfe: pcbios_int_10h_feh(); break;
14965: case 0xff: pcbios_int_10h_ffh(); break;
14966: default:
1.1.1.22 root 14967: 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));
14968: m_CF = 1;
1.1 root 14969: break;
14970: }
14971: break;
14972: case 0x11:
14973: // PC BIOS - Get Equipment List
1.1.1.26 root 14974: REG16(AX) = msdos_get_equipment();
1.1 root 14975: break;
14976: case 0x12:
14977: // PC BIOS - Get Memory Size
1.1.1.33 root 14978: REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1 root 14979: break;
14980: case 0x13:
1.1.1.42! root 14981: // PC BIOS - Disk I/O
! 14982: {
! 14983: static UINT8 last = 0x00;
! 14984: switch(REG8(AH)) {
! 14985: case 0x00: pcbios_int_13h_00h(); break;
! 14986: case 0x01: // get last status
! 14987: REG8(AH) = last;
! 14988: break;
! 14989: case 0x02: pcbios_int_13h_02h(); break;
! 14990: case 0x03: pcbios_int_13h_03h(); break;
! 14991: case 0x04: pcbios_int_13h_04h(); break;
! 14992: case 0x08: pcbios_int_13h_08h(); break;
! 14993: case 0x0a: pcbios_int_13h_02h(); break;
! 14994: case 0x0b: pcbios_int_13h_03h(); break;
! 14995: case 0x0d: pcbios_int_13h_00h(); break;
! 14996: case 0x10: pcbios_int_13h_10h(); break;
! 14997: case 0x15: pcbios_int_13h_15h(); break;
! 14998: case 0x05: // format
! 14999: case 0x06:
! 15000: case 0x07:
! 15001: REG8(AH) = 0x0c; // unsupported track or invalid media
! 15002: m_CF = 1;
! 15003: break;
! 15004: case 0x09:
! 15005: case 0x0c: // seek
! 15006: case 0x11: // recalib
! 15007: case 0x14:
! 15008: case 0x17:
! 15009: REG8(AH) = 0x00; // successful completion
! 15010: break;
! 15011: default:
! 15012: 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));
! 15013: REG8(AH) = 0x01; // invalid function
! 15014: m_CF = 1;
! 15015: break;
! 15016: }
! 15017: last = REG8(AH);
! 15018: }
1.1 root 15019: break;
15020: case 0x14:
15021: // PC BIOS - Serial I/O
1.1.1.25 root 15022: switch(REG8(AH)) {
15023: case 0x00: pcbios_int_14h_00h(); break;
15024: case 0x01: pcbios_int_14h_01h(); break;
15025: case 0x02: pcbios_int_14h_02h(); break;
15026: case 0x03: pcbios_int_14h_03h(); break;
15027: case 0x04: pcbios_int_14h_04h(); break;
15028: case 0x05: pcbios_int_14h_05h(); break;
15029: default:
15030: 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));
15031: break;
15032: }
1.1 root 15033: break;
15034: case 0x15:
15035: // PC BIOS
1.1.1.3 root 15036: m_CF = 0;
1.1 root 15037: switch(REG8(AH)) {
1.1.1.14 root 15038: case 0x10: pcbios_int_15h_10h(); break;
1.1 root 15039: case 0x23: pcbios_int_15h_23h(); break;
15040: case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24 root 15041: case 0x41: break;
1.1 root 15042: case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22 root 15043: case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30 root 15044: case 0x53: pcbios_int_15h_53h(); break;
1.1 root 15045: case 0x86: pcbios_int_15h_86h(); break;
15046: case 0x87: pcbios_int_15h_87h(); break;
15047: case 0x88: pcbios_int_15h_88h(); break;
15048: case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21 root 15049: case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22 root 15050: case 0xc0: // PS/2 ???
15051: case 0xc1:
15052: case 0xc2:
1.1.1.30 root 15053: case 0xc3: // PS50+ ???
15054: case 0xc4:
1.1.1.22 root 15055: REG8(AH) = 0x86;
15056: m_CF = 1;
15057: break;
1.1.1.3 root 15058: #if defined(HAS_I386)
1.1 root 15059: case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3 root 15060: #endif
1.1 root 15061: case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22 root 15062: case 0xe8: pcbios_int_15h_e8h(); break;
1.1 root 15063: default:
1.1.1.22 root 15064: 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));
15065: REG8(AH) = 0x86;
1.1.1.3 root 15066: m_CF = 1;
1.1 root 15067: break;
15068: }
15069: break;
15070: case 0x16:
15071: // PC BIOS - Keyboard
1.1.1.3 root 15072: m_CF = 0;
1.1 root 15073: switch(REG8(AH)) {
15074: case 0x00: pcbios_int_16h_00h(); break;
15075: case 0x01: pcbios_int_16h_01h(); break;
15076: case 0x02: pcbios_int_16h_02h(); break;
15077: case 0x03: pcbios_int_16h_03h(); break;
15078: case 0x05: pcbios_int_16h_05h(); break;
15079: case 0x10: pcbios_int_16h_00h(); break;
15080: case 0x11: pcbios_int_16h_01h(); break;
15081: case 0x12: pcbios_int_16h_12h(); break;
15082: case 0x13: pcbios_int_16h_13h(); break;
15083: case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24 root 15084: case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30 root 15085: case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22 root 15086: case 0xda: break; // unknown
15087: case 0xff: break; // unknown
1.1 root 15088: default:
1.1.1.22 root 15089: 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 15090: break;
15091: }
15092: break;
15093: case 0x17:
15094: // PC BIOS - Printer
1.1.1.37 root 15095: m_CF = 0;
15096: switch(REG8(AH)) {
15097: case 0x00: pcbios_int_17h_00h(); break;
15098: case 0x01: pcbios_int_17h_01h(); break;
15099: case 0x02: pcbios_int_17h_02h(); break;
15100: case 0x03: pcbios_int_17h_03h(); break;
15101: case 0x50: pcbios_int_17h_50h(); break;
15102: case 0x51: pcbios_int_17h_51h(); break;
15103: case 0x52: pcbios_int_17h_52h(); break;
15104: case 0x84: pcbios_int_17h_84h(); break;
15105: case 0x85: pcbios_int_17h_85h(); break;
15106: default:
15107: 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));
15108: break;
15109: }
1.1 root 15110: break;
15111: case 0x1a:
15112: // PC BIOS - Timer
1.1.1.3 root 15113: m_CF = 0;
1.1 root 15114: switch(REG8(AH)) {
15115: case 0x00: pcbios_int_1ah_00h(); break;
15116: case 0x01: break;
15117: case 0x02: pcbios_int_1ah_02h(); break;
15118: case 0x03: break;
15119: case 0x04: pcbios_int_1ah_04h(); break;
15120: case 0x05: break;
15121: case 0x0a: pcbios_int_1ah_0ah(); break;
15122: case 0x0b: break;
1.1.1.14 root 15123: case 0x35: break; // Word Perfect Third Party Interface?
15124: case 0x36: break; // Word Perfect Third Party Interface
15125: case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1 root 15126: default:
1.1.1.22 root 15127: 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 15128: break;
15129: }
15130: break;
1.1.1.33 root 15131: case 0x1b:
15132: mem[0x471] = 0x00;
15133: break;
1.1 root 15134: case 0x20:
1.1.1.28 root 15135: try {
15136: msdos_process_terminate(SREG(CS), retval, 1);
15137: } catch(...) {
15138: fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
15139: }
1.1 root 15140: break;
15141: case 0x21:
15142: // MS-DOS System Call
1.1.1.3 root 15143: m_CF = 0;
1.1.1.28 root 15144: try {
15145: switch(REG8(AH)) {
15146: case 0x00: msdos_int_21h_00h(); break;
15147: case 0x01: msdos_int_21h_01h(); break;
15148: case 0x02: msdos_int_21h_02h(); break;
15149: case 0x03: msdos_int_21h_03h(); break;
15150: case 0x04: msdos_int_21h_04h(); break;
15151: case 0x05: msdos_int_21h_05h(); break;
15152: case 0x06: msdos_int_21h_06h(); break;
15153: case 0x07: msdos_int_21h_07h(); break;
15154: case 0x08: msdos_int_21h_08h(); break;
15155: case 0x09: msdos_int_21h_09h(); break;
15156: case 0x0a: msdos_int_21h_0ah(); break;
15157: case 0x0b: msdos_int_21h_0bh(); break;
15158: case 0x0c: msdos_int_21h_0ch(); break;
15159: case 0x0d: msdos_int_21h_0dh(); break;
15160: case 0x0e: msdos_int_21h_0eh(); break;
15161: case 0x0f: msdos_int_21h_0fh(); break;
15162: case 0x10: msdos_int_21h_10h(); break;
15163: case 0x11: msdos_int_21h_11h(); break;
15164: case 0x12: msdos_int_21h_12h(); break;
15165: case 0x13: msdos_int_21h_13h(); break;
15166: case 0x14: msdos_int_21h_14h(); break;
15167: case 0x15: msdos_int_21h_15h(); break;
15168: case 0x16: msdos_int_21h_16h(); break;
15169: case 0x17: msdos_int_21h_17h(); break;
15170: case 0x18: msdos_int_21h_18h(); break;
15171: case 0x19: msdos_int_21h_19h(); break;
15172: case 0x1a: msdos_int_21h_1ah(); break;
15173: case 0x1b: msdos_int_21h_1bh(); break;
15174: case 0x1c: msdos_int_21h_1ch(); break;
15175: case 0x1d: msdos_int_21h_1dh(); break;
15176: case 0x1e: msdos_int_21h_1eh(); break;
15177: case 0x1f: msdos_int_21h_1fh(); break;
15178: case 0x20: msdos_int_21h_20h(); break;
15179: case 0x21: msdos_int_21h_21h(); break;
15180: case 0x22: msdos_int_21h_22h(); break;
15181: case 0x23: msdos_int_21h_23h(); break;
15182: case 0x24: msdos_int_21h_24h(); break;
15183: case 0x25: msdos_int_21h_25h(); break;
15184: case 0x26: msdos_int_21h_26h(); break;
15185: case 0x27: msdos_int_21h_27h(); break;
15186: case 0x28: msdos_int_21h_28h(); break;
15187: case 0x29: msdos_int_21h_29h(); break;
15188: case 0x2a: msdos_int_21h_2ah(); break;
15189: case 0x2b: msdos_int_21h_2bh(); break;
15190: case 0x2c: msdos_int_21h_2ch(); break;
15191: case 0x2d: msdos_int_21h_2dh(); break;
15192: case 0x2e: msdos_int_21h_2eh(); break;
15193: case 0x2f: msdos_int_21h_2fh(); break;
15194: case 0x30: msdos_int_21h_30h(); break;
15195: case 0x31: msdos_int_21h_31h(); break;
15196: case 0x32: msdos_int_21h_32h(); break;
15197: case 0x33: msdos_int_21h_33h(); break;
15198: case 0x34: msdos_int_21h_34h(); break;
15199: case 0x35: msdos_int_21h_35h(); break;
15200: case 0x36: msdos_int_21h_36h(); break;
15201: case 0x37: msdos_int_21h_37h(); break;
15202: case 0x38: msdos_int_21h_38h(); break;
15203: case 0x39: msdos_int_21h_39h(0); break;
15204: case 0x3a: msdos_int_21h_3ah(0); break;
15205: case 0x3b: msdos_int_21h_3bh(0); break;
15206: case 0x3c: msdos_int_21h_3ch(); break;
15207: case 0x3d: msdos_int_21h_3dh(); break;
15208: case 0x3e: msdos_int_21h_3eh(); break;
15209: case 0x3f: msdos_int_21h_3fh(); break;
15210: case 0x40: msdos_int_21h_40h(); break;
15211: case 0x41: msdos_int_21h_41h(0); break;
15212: case 0x42: msdos_int_21h_42h(); break;
15213: case 0x43: msdos_int_21h_43h(0); break;
15214: case 0x44: msdos_int_21h_44h(); break;
15215: case 0x45: msdos_int_21h_45h(); break;
15216: case 0x46: msdos_int_21h_46h(); break;
15217: case 0x47: msdos_int_21h_47h(0); break;
15218: case 0x48: msdos_int_21h_48h(); break;
15219: case 0x49: msdos_int_21h_49h(); break;
15220: case 0x4a: msdos_int_21h_4ah(); break;
15221: case 0x4b: msdos_int_21h_4bh(); break;
15222: case 0x4c: msdos_int_21h_4ch(); break;
15223: case 0x4d: msdos_int_21h_4dh(); break;
15224: case 0x4e: msdos_int_21h_4eh(); break;
15225: case 0x4f: msdos_int_21h_4fh(); break;
15226: case 0x50: msdos_int_21h_50h(); break;
15227: case 0x51: msdos_int_21h_51h(); break;
15228: case 0x52: msdos_int_21h_52h(); break;
1.1.1.33 root 15229: // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28 root 15230: case 0x54: msdos_int_21h_54h(); break;
15231: case 0x55: msdos_int_21h_55h(); break;
15232: case 0x56: msdos_int_21h_56h(0); break;
15233: case 0x57: msdos_int_21h_57h(); break;
15234: case 0x58: msdos_int_21h_58h(); break;
15235: case 0x59: msdos_int_21h_59h(); break;
15236: case 0x5a: msdos_int_21h_5ah(); break;
15237: case 0x5b: msdos_int_21h_5bh(); break;
15238: case 0x5c: msdos_int_21h_5ch(); break;
15239: case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42! root 15240: case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30 root 15241: case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28 root 15242: case 0x60: msdos_int_21h_60h(0); break;
15243: case 0x61: msdos_int_21h_61h(); break;
15244: case 0x62: msdos_int_21h_62h(); break;
15245: case 0x63: msdos_int_21h_63h(); break;
1.1.1.33 root 15246: // 0x64: Set Device Driver Lockahead Flag
1.1.1.28 root 15247: case 0x65: msdos_int_21h_65h(); break;
15248: case 0x66: msdos_int_21h_66h(); break;
15249: case 0x67: msdos_int_21h_67h(); break;
15250: case 0x68: msdos_int_21h_68h(); break;
15251: case 0x69: msdos_int_21h_69h(); break;
15252: case 0x6a: msdos_int_21h_6ah(); break;
15253: case 0x6b: msdos_int_21h_6bh(); break;
15254: case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33 root 15255: // 0x6d: Find First ROM Program
15256: // 0x6e: Find Next ROM Program
15257: // 0x6f: Get/Set ROM Scan Start Address
15258: // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28 root 15259: case 0x71:
1.1.1.33 root 15260: // Windows95 - Long Filename Functions
1.1.1.28 root 15261: switch(REG8(AL)) {
15262: case 0x0d: msdos_int_21h_710dh(); break;
15263: case 0x39: msdos_int_21h_39h(1); break;
15264: case 0x3a: msdos_int_21h_3ah(1); break;
15265: case 0x3b: msdos_int_21h_3bh(1); break;
15266: case 0x41: msdos_int_21h_7141h(1); break;
15267: case 0x43: msdos_int_21h_43h(1); break;
15268: case 0x47: msdos_int_21h_47h(1); break;
15269: case 0x4e: msdos_int_21h_714eh(); break;
15270: case 0x4f: msdos_int_21h_714fh(); break;
15271: case 0x56: msdos_int_21h_56h(1); break;
15272: case 0x60: msdos_int_21h_60h(1); break;
15273: case 0x6c: msdos_int_21h_6ch(1); break;
15274: case 0xa0: msdos_int_21h_71a0h(); break;
15275: case 0xa1: msdos_int_21h_71a1h(); break;
15276: case 0xa6: msdos_int_21h_71a6h(); break;
15277: case 0xa7: msdos_int_21h_71a7h(); break;
15278: case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33 root 15279: // 0xa9: Server Create/Open File
1.1.1.28 root 15280: case 0xaa: msdos_int_21h_71aah(); break;
15281: default:
15282: 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));
15283: REG16(AX) = 0x7100;
15284: m_CF = 1;
15285: break;
15286: }
15287: break;
15288: // 0x72: Windows95 beta - LFN FindClose
15289: case 0x73:
1.1.1.33 root 15290: // Windows95 - FAT32 Functions
1.1.1.28 root 15291: switch(REG8(AL)) {
15292: case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33 root 15293: // 0x01: Set Drive Locking ???
1.1.1.28 root 15294: case 0x02: msdos_int_21h_7302h(); break;
15295: case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33 root 15296: // 0x04: Set DPB to Use for Formatting
15297: // 0x05: Extended Absolute Disk Read/Write
1.1.1.28 root 15298: default:
15299: 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));
15300: REG16(AX) = 0x7300;
15301: m_CF = 1;
15302: break;
15303: }
1.1 root 15304: break;
1.1.1.30 root 15305: case 0xdb: msdos_int_21h_dbh(); break;
15306: case 0xdc: msdos_int_21h_dch(); break;
1.1 root 15307: default:
1.1.1.22 root 15308: 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 15309: REG16(AX) = 0x01;
1.1.1.3 root 15310: m_CF = 1;
1.1 root 15311: break;
15312: }
1.1.1.28 root 15313: } catch(int error) {
15314: REG16(AX) = error;
15315: m_CF = 1;
15316: } catch(...) {
15317: REG16(AX) = 0x1f; // general failure
1.1.1.3 root 15318: m_CF = 1;
1.1 root 15319: }
1.1.1.3 root 15320: if(m_CF) {
1.1.1.23 root 15321: sda_t *sda = (sda_t *)(mem + SDA_TOP);
15322: sda->extended_error_code = REG16(AX);
15323: switch(sda->extended_error_code) {
15324: case 4: // Too many open files
15325: case 8: // Insufficient memory
15326: sda->error_class = 1; // Out of resource
15327: break;
15328: case 5: // Access denied
15329: sda->error_class = 3; // Authorization
15330: break;
15331: case 7: // Memory control block destroyed
15332: sda->error_class = 4; // Internal
15333: break;
15334: case 2: // File not found
15335: case 3: // Path not found
15336: case 15: // Invaid drive specified
15337: case 18: // No more files
15338: sda->error_class = 8; // Not found
15339: break;
15340: case 32: // Sharing violation
15341: case 33: // Lock violation
15342: sda->error_class = 10; // Locked
15343: break;
15344: // case 16: // Removal of current directory attempted
15345: case 19: // Attempted write on protected disk
15346: case 21: // Drive not ready
15347: // case 29: // Write failure
15348: // case 30: // Read failure
15349: // case 82: // Cannot create subdirectory
15350: sda->error_class = 11; // Media
15351: break;
15352: case 80: // File already exists
15353: sda->error_class = 12; // Already exist
15354: break;
15355: default:
15356: sda->error_class = 13; // Unknown
15357: break;
15358: }
15359: sda->suggested_action = 1; // Retry
15360: sda->locus_of_last_error = 1; // Unknown
1.1 root 15361: }
1.1.1.33 root 15362: if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26 root 15363: // raise int 23h
15364: #if defined(HAS_I386)
15365: m_ext = 0; // not an external interrupt
15366: i386_trap(0x23, 1, 0);
15367: m_ext = 1;
15368: #else
15369: PREFIX86(_interrupt)(0x23);
15370: #endif
15371: }
1.1 root 15372: break;
15373: case 0x22:
15374: fatalerror("int 22h (terminate address)\n");
15375: case 0x23:
1.1.1.28 root 15376: try {
15377: msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
15378: } catch(...) {
15379: fatalerror("failed to terminate the current process by int 23h\n");
15380: }
1.1 root 15381: break;
15382: case 0x24:
1.1.1.32 root 15383: /*
1.1.1.28 root 15384: try {
15385: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
15386: } catch(...) {
15387: fatalerror("failed to terminate the current process by int 24h\n");
15388: }
1.1.1.32 root 15389: */
15390: msdos_int_24h();
1.1 root 15391: break;
15392: case 0x25:
15393: msdos_int_25h();
15394: break;
15395: case 0x26:
15396: msdos_int_26h();
15397: break;
15398: case 0x27:
1.1.1.28 root 15399: try {
15400: msdos_int_27h();
15401: } catch(...) {
15402: fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
15403: }
1.1 root 15404: break;
15405: case 0x28:
15406: Sleep(10);
1.1.1.35 root 15407: REQUEST_HARDWRE_UPDATE();
1.1 root 15408: break;
15409: case 0x29:
15410: msdos_int_29h();
15411: break;
15412: case 0x2e:
15413: msdos_int_2eh();
15414: break;
15415: case 0x2f:
15416: // multiplex interrupt
15417: switch(REG8(AH)) {
1.1.1.22 root 15418: case 0x05: msdos_int_2fh_05h(); break;
15419: case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21 root 15420: case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30 root 15421: case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22 root 15422: case 0x14: msdos_int_2fh_14h(); break;
15423: case 0x15: msdos_int_2fh_15h(); break;
1.1 root 15424: case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22 root 15425: case 0x19: msdos_int_2fh_19h(); break;
1.1 root 15426: case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30 root 15427: case 0x40: msdos_int_2fh_40h(); break;
1.1 root 15428: case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22 root 15429: case 0x46: msdos_int_2fh_46h(); break;
15430: case 0x48: msdos_int_2fh_48h(); break;
1.1 root 15431: case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22 root 15432: case 0x4b: msdos_int_2fh_4bh(); break;
1.1 root 15433: case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22 root 15434: case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24 root 15435: case 0xad: msdos_int_2fh_adh(); break;
1.1 root 15436: case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34 root 15437: case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30 root 15438: // Installation Check
1.1.1.42! root 15439: case 0x01: // DOS 3.0+ PRINT
! 15440: case 0x02: // PC LAN PROGRAM REDIR/REDIRIFS internal
! 15441: case 0x06: // DOS 3.0+ ASSIGN
! 15442: case 0x08: // DRIVER.SYS support
1.1.1.30 root 15443: case 0x10: // SHARE
1.1.1.42! root 15444: case 0x17: // MS Windows "WINOLDAP"
! 15445: case 0x1b: // DOS 4+ XMA2EMS.SYS extension internal
1.1.1.30 root 15446: case 0x23: // DR DOS 5.0 GRAFTABL
15447: case 0x27: // DR-DOR 6.0 TaskMAX
15448: case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33 root 15449: case 0x39: // Kingswood TSR INTERFACE
15450: case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.42! root 15451: case 0x45: // Microsoft Profiler (PROF.COM/VPROD.386)
1.1.1.30 root 15452: case 0x51: // ODIHELP.EXE
1.1.1.33 root 15453: case 0x52: // JAM.SYS v1.10+
1.1.1.30 root 15454: case 0x54: // POWER.EXE
15455: case 0x56: // INTERLNK
1.1.1.33 root 15456: case 0x57: // IOMEGA DRIVERS
1.1.1.30 root 15457: case 0x70: // License Service API
1.1.1.33 root 15458: case 0x72: // SRDISK v1.30+
1.1.1.30 root 15459: case 0x7a: // Novell NetWare
1.1.1.33 root 15460: case 0x7f: // PRINDIR v9.0
1.1.1.42! root 15461: case 0x80: // FaxBIOS interface
1.1.1.33 root 15462: case 0x81: // Nanosoft, Inc. TurboNET redirector
15463: case 0x82: // Nanosoft, Inc. CAPDOS
15464: case 0x89: // WHOA!.COM
15465: case 0x90: // Resident AID
1.1.1.30 root 15466: case 0x94: // MICRO.EXE
1.1.1.33 root 15467: case 0x97: // Micro Focus COBOL v3.1.31
15468: case 0x98: // Micro Focus COBOL v3.1.31
15469: case 0x99: // DOS Navigator II
15470: case 0x9e: // INTMON v2.1
15471: case 0x9f: // INTCFG v2.1
15472: case 0xa9: // METZTSR.COM
1.1.1.42! root 15473: case 0xaa: // VIDCLOCK.COM
1.1.1.33 root 15474: case 0xab: // SRSoft MODAL PC v2
1.1.1.42! root 15475: case 0xac: // DOS 4.01+ GRAPHICS.COM
1.1.1.33 root 15476: case 0xaf: // WinDOS v2.11
1.1.1.42! root 15477: case 0xb0: // DOS 3.3+ GRAFTABL.COM
1.1.1.33 root 15478: case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30 root 15479: case 0xb8: // NETWORK
1.1.1.42! root 15480: case 0xb9: // PC Network RECEIVER.COM
! 15481: case 0xbc: // Windows 3.0, DOS 5+ EGA.SYS
1.1.1.33 root 15482: case 0xbe: // REDVIEW
1.1.1.42! root 15483: case 0xbf: // PC LAN PROGRAM REDIRIFS.EXE internal
! 15484: case 0xc0: // Novell ODI Link Support Layer (LSL.COM)
1.1.1.33 root 15485: case 0xc1: // Personal NetWare - STPIPX v1.00
15486: case 0xc3: // SETWPR.COM
15487: case 0xc5: // PC-DOS Econet v1.05
15488: case 0xc7: // COLAP.COM
15489: case 0xc9: // ThunderByte???
15490: case 0xca: // TBSCANX
15491: case 0xcb: // Communicating Applications Specification
15492: case 0xcc: // Tsoft NFSDRVR
15493: case 0xcd: // SWELL.EXE
15494: case 0xcf: // TEMPLEXX 1.0
15495: case 0xd0: // Lotus CD/Networker
1.1.1.30 root 15496: case 0xd2: // PCL-838.EXE
1.1.1.33 root 15497: case 0xd3: // TeleReplica
15498: case 0xd6: // VEDIT VSWAP
15499: case 0xd7: // Banyan VINES v4+
1.1.1.30 root 15500: case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33 root 15501: case 0xda: // ZyXEL ZFAX v1.x
15502: case 0xdb: // ZyXEL ZFAX v2+
15503: case 0xdc: // GOLD.COM
15504: case 0xdd: // MIXFIX.EXE
1.1.1.42! root 15505: case 0xde: // Quarterdeck QDPMI.SYS v1.0
1.1.1.33 root 15506: case 0xdf: // HyperWare programs
15507: case 0xe0: // SETDRVER.COM v2.10+
15508: case 0xe1: // Phantom2 v1.1+
15509: case 0xe3: // ANARKEY.COM
15510: case 0xed: // Phar Lap DOS EXTENDERS
15511: case 0xee: // XVIEW
15512: case 0xf0: // 4MAP
15513: case 0xf1: // DOS EXTENDER
15514: case 0xf2: // WINX
15515: case 0xf4: // FINDIRQ.COM
15516: case 0xf7: // AUTOPARK.COM
15517: case 0xf8: // SuperStor PRO 2XON.COM
1.1.1.42! root 15518: case 0xfa: // Watcom Debugger
1.1.1.33 root 15519: case 0xfb: // AutoBraille v1.1A
15520: case 0xfe: // PC-NFS ???
15521: case 0xff: // Topware Network Operating System
1.1.1.30 root 15522: switch(REG8(AL)) {
15523: case 0x00:
15524: // This is not installed
15525: // REG8(AL) = 0x00;
15526: break;
1.1.1.33 root 15527: case 0x01:
1.1.1.42! root 15528: // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
! 15529: if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
! 15530: break;
! 15531: }
1.1.1.33 root 15532: // Banyan VINES v4+ is not installed
15533: if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
15534: break;
15535: }
1.1.1.42! root 15536: // Quarterdeck QDPMI.SYS v1.0 is not installed
! 15537: if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
! 15538: break;
! 15539: }
1.1.1.30 root 15540: default:
1.1.1.42! root 15541: // NORTON UTILITIES 5.0+
! 15542: if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
! 15543: break;
! 15544: }
1.1.1.30 root 15545: 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));
15546: REG16(AX) = 0x01;
15547: m_CF = 1;
15548: break;
15549: }
15550: break;
1.1.1.22 root 15551: default:
15552: unimplemented_2fh("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));
15553: break;
1.1 root 15554: }
15555: break;
1.1.1.24 root 15556: case 0x33:
15557: switch(REG8(AH)) {
15558: case 0x00:
15559: // Mouse
15560: switch(REG8(AL)) {
15561: case 0x00: msdos_int_33h_0000h(); break;
15562: case 0x01: msdos_int_33h_0001h(); break;
15563: case 0x02: msdos_int_33h_0002h(); break;
15564: case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34 root 15565: case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24 root 15566: case 0x05: msdos_int_33h_0005h(); break;
15567: case 0x06: msdos_int_33h_0006h(); break;
15568: case 0x07: msdos_int_33h_0007h(); break;
15569: case 0x08: msdos_int_33h_0008h(); break;
15570: case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34 root 15571: case 0x0a: break; // Define Text Cursor
1.1.1.24 root 15572: case 0x0b: msdos_int_33h_000bh(); break;
15573: case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34 root 15574: case 0x0d: break; // Light Pen Emulation On
15575: case 0x0e: break; // Light Pen Emulation Off
1.1.1.24 root 15576: case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34 root 15577: case 0x10: break; // Define Screen Region for Updating
1.1.1.24 root 15578: case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34 root 15579: case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
15580: case 0x13: break; // Define Double-Speed Threshold
1.1.1.24 root 15581: case 0x14: msdos_int_33h_0014h(); break;
15582: case 0x15: msdos_int_33h_0015h(); break;
15583: case 0x16: msdos_int_33h_0016h(); break;
15584: case 0x17: msdos_int_33h_0017h(); break;
15585: case 0x1a: msdos_int_33h_001ah(); break;
15586: case 0x1b: msdos_int_33h_001bh(); break;
15587: case 0x1d: msdos_int_33h_001dh(); break;
15588: case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34 root 15589: case 0x1f: msdos_int_33h_001fh(); break;
15590: case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24 root 15591: case 0x21: msdos_int_33h_0021h(); break;
15592: case 0x22: msdos_int_33h_0022h(); break;
15593: case 0x23: msdos_int_33h_0023h(); break;
15594: case 0x24: msdos_int_33h_0024h(); break;
15595: case 0x26: msdos_int_33h_0026h(); break;
15596: case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34 root 15597: case 0x2f: break; // Mouse Hardware Reset
1.1.1.24 root 15598: case 0x31: msdos_int_33h_0031h(); break;
15599: case 0x32: msdos_int_33h_0032h(); break;
15600: default:
15601: 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));
15602: break;
15603: }
15604: break;
15605: default:
15606: 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));
15607: break;
15608: }
15609: break;
1.1.1.19 root 15610: case 0x68:
15611: // dummy interrupt for EMS (int 67h)
15612: switch(REG8(AH)) {
15613: case 0x40: msdos_int_67h_40h(); break;
15614: case 0x41: msdos_int_67h_41h(); break;
15615: case 0x42: msdos_int_67h_42h(); break;
15616: case 0x43: msdos_int_67h_43h(); break;
15617: case 0x44: msdos_int_67h_44h(); break;
15618: case 0x45: msdos_int_67h_45h(); break;
15619: case 0x46: msdos_int_67h_46h(); break;
15620: case 0x47: msdos_int_67h_47h(); break;
15621: case 0x48: msdos_int_67h_48h(); break;
1.1.1.20 root 15622: // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
15623: // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19 root 15624: case 0x4b: msdos_int_67h_4bh(); break;
15625: case 0x4c: msdos_int_67h_4ch(); break;
15626: case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20 root 15627: case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21 root 15628: case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20 root 15629: case 0x50: msdos_int_67h_50h(); break;
1.1.1.19 root 15630: case 0x51: msdos_int_67h_51h(); break;
1.1.1.20 root 15631: case 0x52: msdos_int_67h_52h(); break;
1.1.1.19 root 15632: case 0x53: msdos_int_67h_53h(); break;
15633: case 0x54: msdos_int_67h_54h(); break;
1.1.1.20 root 15634: // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
15635: // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
15636: case 0x57: msdos_int_67h_57h(); break;
15637: case 0x58: msdos_int_67h_58h(); break;
1.1.1.42! root 15638: case 0x59: msdos_int_67h_59h(); break;
1.1.1.20 root 15639: case 0x5a: msdos_int_67h_5ah(); break;
15640: // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
15641: // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
15642: // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31 root 15643: // 0x60: EEMS - Get Physical Window Array
15644: // 0x61: EEMS - Generic Accelerator Card Support
15645: // 0x68: EEMS - Get Address of All Pge Frames om System
15646: // 0x69: EEMS - Map Page into Frame
15647: // 0x6a: EEMS - Page Mapping
15648: // 0xde: VCPI
1.1.1.30 root 15649: case 0xde: msdos_int_67h_deh(); break;
1.1.1.19 root 15650: default:
1.1.1.22 root 15651: 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 15652: REG8(AH) = 0x84;
15653: break;
15654: }
15655: break;
15656: #ifdef SUPPORT_XMS
15657: case 0x69:
15658: // dummy interrupt for XMS (call far)
1.1.1.28 root 15659: try {
15660: switch(REG8(AH)) {
15661: case 0x00: msdos_call_xms_00h(); break;
15662: case 0x01: msdos_call_xms_01h(); break;
15663: case 0x02: msdos_call_xms_02h(); break;
15664: case 0x03: msdos_call_xms_03h(); break;
15665: case 0x04: msdos_call_xms_04h(); break;
15666: case 0x05: msdos_call_xms_05h(); break;
15667: case 0x06: msdos_call_xms_06h(); break;
15668: case 0x07: msdos_call_xms_07h(); break;
15669: case 0x08: msdos_call_xms_08h(); break;
15670: case 0x09: msdos_call_xms_09h(); break;
15671: case 0x0a: msdos_call_xms_0ah(); break;
15672: case 0x0b: msdos_call_xms_0bh(); break;
15673: case 0x0c: msdos_call_xms_0ch(); break;
15674: case 0x0d: msdos_call_xms_0dh(); break;
15675: case 0x0e: msdos_call_xms_0eh(); break;
15676: case 0x0f: msdos_call_xms_0fh(); break;
15677: case 0x10: msdos_call_xms_10h(); break;
15678: case 0x11: msdos_call_xms_11h(); break;
15679: case 0x12: msdos_call_xms_12h(); break;
1.1.1.29 root 15680: #if defined(HAS_I386)
15681: case 0x88: msdos_call_xms_88h(); break;
15682: case 0x89: msdos_call_xms_89h(); break;
15683: case 0x8e: msdos_call_xms_8eh(); break;
15684: case 0x8f: msdos_call_xms_8fh(); break;
15685: #endif
1.1.1.28 root 15686: default:
15687: 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));
15688: REG16(AX) = 0x0000;
15689: REG8(BL) = 0x80; // function not implemented
15690: break;
15691: }
15692: } catch(...) {
1.1.1.19 root 15693: REG16(AX) = 0x0000;
1.1.1.28 root 15694: REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19 root 15695: }
15696: break;
15697: #endif
15698: case 0x6a:
1.1.1.24 root 15699: // irq12 (mouse)
15700: mouse_push_ax = REG16(AX);
15701: mouse_push_bx = REG16(BX);
15702: mouse_push_cx = REG16(CX);
15703: mouse_push_dx = REG16(DX);
15704: mouse_push_si = REG16(SI);
15705: mouse_push_di = REG16(DI);
15706:
1.1.1.34 root 15707: if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24 root 15708: REG16(AX) = mouse.status_irq;
15709: REG16(BX) = mouse.get_buttons();
1.1.1.34 root 15710: REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
15711: REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24 root 15712: REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
15713: REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
15714:
15715: mem[0xfffd0 + 0x02] = 0x9a; // call far
15716: mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
15717: mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
15718: mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
15719: mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
15720: } else {
15721: mem[0xfffd0 + 0x02] = 0x90; // nop
15722: mem[0xfffd0 + 0x03] = 0x90; // nop
15723: mem[0xfffd0 + 0x04] = 0x90; // nop
15724: mem[0xfffd0 + 0x05] = 0x90; // nop
15725: mem[0xfffd0 + 0x06] = 0x90; // nop
15726: }
15727: break;
15728: case 0x6b:
15729: // end of irq12 (mouse)
15730: REG16(AX) = mouse_push_ax;
15731: REG16(BX) = mouse_push_bx;
15732: REG16(CX) = mouse_push_cx;
15733: REG16(DX) = mouse_push_dx;
15734: REG16(SI) = mouse_push_si;
15735: REG16(DI) = mouse_push_di;
15736:
15737: // EOI
15738: if((pic[1].isr &= ~(1 << 4)) == 0) {
15739: pic[0].isr &= ~(1 << 2); // master
15740: }
15741: pic_update();
15742: break;
15743: case 0x6c:
1.1.1.19 root 15744: // dummy interrupt for case map routine pointed in the country info
15745: if(REG8(AL) >= 0x80) {
15746: char tmp[2] = {0};
15747: tmp[0] = REG8(AL);
15748: my_strupr(tmp);
15749: REG8(AL) = tmp[0];
15750: }
15751: break;
1.1.1.27 root 15752: case 0x6d:
15753: // dummy interrupt for font read routine pointed by int 15h, ax=5000h
15754: REG8(AL) = 0x86; // not supported
15755: m_CF = 1;
15756: break;
1.1.1.32 root 15757: case 0x6e:
15758: // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
15759: {
15760: UINT16 code = REG16(AX);
15761: if(code & 0xf0) {
15762: code = (code & 7) | ((code & 0x10) >> 1);
15763: }
15764: for(int i = 0; i < array_length(param_error_table); i++) {
15765: if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
15766: const char *message = NULL;
15767: if(active_code_page == 932) {
15768: message = param_error_table[i].message_japanese;
15769: }
15770: if(message == NULL) {
15771: message = param_error_table[i].message_english;
15772: }
15773: *(UINT8 *)(mem + WORK_TOP) = strlen(message);
15774: strcpy((char *)(mem + WORK_TOP + 1), message);
15775:
15776: SREG(ES) = WORK_TOP >> 4;
15777: i386_load_segment_descriptor(ES);
15778: REG16(DI) = 0x0000;
15779: break;
15780: }
15781: }
15782: }
15783: break;
1.1.1.8 root 15784: case 0x70:
15785: case 0x71:
15786: case 0x72:
15787: case 0x73:
15788: case 0x74:
15789: case 0x75:
15790: case 0x76:
15791: case 0x77:
15792: // EOI
15793: if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
15794: pic[0].isr &= ~(1 << 2); // master
15795: }
15796: pic_update();
15797: break;
1.1 root 15798: default:
1.1.1.22 root 15799: // 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 15800: break;
15801: }
15802:
15803: // update cursor position
15804: if(cursor_moved) {
1.1.1.36 root 15805: pcbios_update_cursor_position();
1.1 root 15806: cursor_moved = false;
15807: }
15808: }
15809:
15810: // init
15811:
15812: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
15813: {
15814: // init file handler
15815: memset(file_handler, 0, sizeof(file_handler));
15816: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
15817: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
15818: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21 root 15819: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 15820: if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21 root 15821: #else
15822: if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
15823: #endif
15824: msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1 root 15825: }
1.1.1.21 root 15826: if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37 root 15827: msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21 root 15828: }
1.1 root 15829: _dup2(0, DUP_STDIN);
15830: _dup2(1, DUP_STDOUT);
15831: _dup2(2, DUP_STDERR);
1.1.1.21 root 15832: _dup2(3, DUP_STDAUX);
15833: _dup2(4, DUP_STDPRN);
1.1 root 15834:
1.1.1.24 root 15835: // init mouse
15836: memset(&mouse, 0, sizeof(mouse));
1.1.1.34 root 15837: mouse.enabled = true; // from DOSBox
15838: mouse.hidden = 1; // hidden in default ???
15839: mouse.old_hidden = 1; // from DOSBox
15840: mouse.max_position.x = 8 * (scr_width - 1);
15841: mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24 root 15842: mouse.mickey.x = 8;
15843: mouse.mickey.y = 16;
15844:
1.1.1.26 root 15845: #ifdef SUPPORT_XMS
15846: // init xms
15847: msdos_xms_init();
15848: #endif
15849:
1.1 root 15850: // init process
15851: memset(process, 0, sizeof(process));
15852:
1.1.1.13 root 15853: // init dtainfo
15854: msdos_dta_info_init();
15855:
1.1 root 15856: // init memory
15857: memset(mem, 0, sizeof(mem));
15858:
15859: // bios data area
1.1.1.23 root 15860: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1 root 15861: CONSOLE_SCREEN_BUFFER_INFO csbi;
15862: GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14 root 15863: CONSOLE_FONT_INFO cfi;
15864: GetCurrentConsoleFont(hStdout, FALSE, &cfi);
15865:
15866: int regen = min(scr_width * scr_height * 2, 0x8000);
15867: text_vram_top_address = TEXT_VRAM_TOP;
15868: text_vram_end_address = text_vram_top_address + regen;
15869: shadow_buffer_top_address = SHADOW_BUF_TOP;
15870: shadow_buffer_end_address = shadow_buffer_top_address + regen;
15871:
15872: if(regen > 0x4000) {
15873: regen = 0x8000;
15874: vram_pages = 1;
15875: } else if(regen > 0x2000) {
15876: regen = 0x4000;
15877: vram_pages = 2;
15878: } else if(regen > 0x1000) {
15879: regen = 0x2000;
15880: vram_pages = 4;
15881: } else {
15882: regen = 0x1000;
15883: vram_pages = 8;
15884: }
1.1 root 15885:
1.1.1.25 root 15886: *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
15887: *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29 root 15888: *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
15889: *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25 root 15890: *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37 root 15891: *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
15892: *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32 root 15893: #ifdef EXT_BIOS_TOP
1.1.1.25 root 15894: *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32 root 15895: #endif
1.1.1.26 root 15896: *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33 root 15897: *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41 root 15898: *(UINT16 *)(mem + 0x41a) = 0x1e;
15899: *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1 root 15900: *(UINT8 *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14 root 15901: *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
15902: *(UINT16 *)(mem + 0x44c) = regen;
1.1 root 15903: *(UINT16 *)(mem + 0x44e) = 0;
15904: *(UINT8 *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14 root 15905: *(UINT8 *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1 root 15906: *(UINT8 *)(mem + 0x460) = 7;
15907: *(UINT8 *)(mem + 0x461) = 7;
15908: *(UINT8 *)(mem + 0x462) = 0;
15909: *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19 root 15910: *(UINT8 *)(mem + 0x465) = 0x09;
15911: *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40 root 15912: *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41 root 15913: *(UINT16 *)(mem + 0x480) = 0x1e;
15914: *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14 root 15915: *(UINT8 *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
15916: *(UINT8 *)(mem + 0x485) = cfi.dwFontSize.Y;
15917: *(UINT8 *)(mem + 0x487) = 0x60;
15918: *(UINT8 *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32 root 15919: #ifdef EXT_BIOS_TOP
1.1.1.25 root 15920: *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32 root 15921: #endif
1.1.1.14 root 15922:
15923: // initial screen
15924: SMALL_RECT rect;
15925: SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
15926: ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
15927: for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
15928: for(int x = 0; x < scr_width; x++) {
15929: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
15930: mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
15931: }
15932: }
1.1 root 15933:
1.1.1.19 root 15934: // init mcb
1.1 root 15935: int seg = MEMORY_TOP >> 4;
1.1.1.19 root 15936:
15937: // iret table
15938: // note: int 2eh vector should address the routine in command.com,
15939: // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
15940: // so move iret table into allocated memory block
15941: // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33 root 15942: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19 root 15943: IRET_TOP = seg << 4;
15944: seg += IRET_SIZE >> 4;
1.1.1.25 root 15945: memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19 root 15946:
15947: // dummy xms/ems device
1.1.1.33 root 15948: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19 root 15949: XMS_TOP = seg << 4;
15950: seg += XMS_SIZE >> 4;
15951:
15952: // environment
1.1.1.33 root 15953: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1 root 15954: int env_seg = seg;
15955: int ofs = 0;
1.1.1.32 root 15956: char env_append[ENV_SIZE] = {0}, append_added = 0;
15957: char comspec_added = 0;
1.1.1.33 root 15958: char lastdrive_added = 0;
1.1.1.32 root 15959: char env_msdos_path[ENV_SIZE] = {0};
15960: char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33 root 15961: char prompt_added = 0;
1.1.1.32 root 15962: char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33 root 15963: char tz_added = 0;
1.1.1.32 root 15964: char *path, *short_path;
15965:
15966: if((path = getenv("MSDOS_APPEND")) != NULL) {
15967: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15968: strcpy(env_append, short_path);
15969: }
15970: }
15971: if((path = getenv("APPEND")) != NULL) {
15972: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15973: if(env_append[0] != '\0') {
15974: strcat(env_append, ";");
15975: }
15976: strcat(env_append, short_path);
15977: }
15978: }
15979:
15980: if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
15981: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15982: strcpy(comspec_path, short_path);
15983: }
15984: }
15985: if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
15986: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15987: strcpy(comspec_path, short_path);
15988: }
15989: }
1.1 root 15990:
1.1.1.28 root 15991: if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32 root 15992: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15993: strcpy(env_msdos_path, short_path);
15994: strcpy(env_path, short_path);
1.1.1.14 root 15995: }
15996: }
1.1.1.28 root 15997: if((path = getenv("PATH")) != NULL) {
1.1.1.32 root 15998: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
15999: if(env_path[0] != '\0') {
16000: strcat(env_path, ";");
16001: }
16002: strcat(env_path, short_path);
1.1.1.9 root 16003: }
16004: }
1.1.1.32 root 16005:
16006: if(GetTempPath(ENV_SIZE, env_temp) != 0) {
16007: strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15 root 16008: }
1.1.1.32 root 16009: for(int i = 0; i < 4; i++) {
16010: static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
16011: if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
16012: if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
16013: strcpy(env_temp, short_path);
16014: break;
16015: }
16016: }
1.1.1.24 root 16017: }
1.1.1.32 root 16018:
1.1.1.9 root 16019: for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1 root 16020: // lower to upper
1.1.1.28 root 16021: char tmp[ENV_SIZE], name[ENV_SIZE];
1.1 root 16022: strcpy(tmp, *p);
16023: for(int i = 0;; i++) {
16024: if(tmp[i] == '=') {
16025: tmp[i] = '\0';
16026: sprintf(name, ";%s;", tmp);
1.1.1.25 root 16027: my_strupr(name);
1.1 root 16028: tmp[i] = '=';
16029: break;
16030: } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28 root 16031: tmp[i] = (tmp[i] - 'a') + 'A';
1.1 root 16032: }
16033: }
1.1.1.33 root 16034: if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
16035: // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
16036: } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18 root 16037: // ignore non standard environments
16038: } else {
1.1.1.33 root 16039: if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32 root 16040: if(env_append[0] != '\0') {
16041: sprintf(tmp, "APPEND=%s", env_append);
16042: } else {
16043: sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
16044: }
16045: append_added = 1;
16046: } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14 root 16047: strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32 root 16048: comspec_added = 1;
1.1.1.33 root 16049: } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
16050: char *env = getenv("MSDOS_LASTDRIVE");
16051: if(env != NULL) {
16052: sprintf(tmp, "LASTDRIVE=%s", env);
16053: }
16054: lastdrive_added = 1;
16055: } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28 root 16056: if(env_msdos_path[0] != '\0') {
16057: sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
16058: } else {
16059: sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
16060: }
1.1.1.33 root 16061: } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28 root 16062: if(env_path[0] != '\0') {
16063: sprintf(tmp, "PATH=%s", env_path);
16064: } else {
16065: sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
16066: }
1.1.1.32 root 16067: path_added = 1;
1.1.1.33 root 16068: } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
16069: prompt_added = 1;
1.1.1.28 root 16070: } else if(strncmp(tmp, "TEMP=", 5) == 0) {
16071: if(env_temp[0] != '\0') {
16072: sprintf(tmp, "TEMP=%s", env_temp);
16073: } else {
16074: sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
16075: }
1.1.1.32 root 16076: temp_added = 1;
1.1.1.33 root 16077: } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28 root 16078: if(env_temp[0] != '\0') {
16079: sprintf(tmp, "TMP=%s", env_temp);
16080: } else {
16081: sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1 root 16082: }
1.1.1.32 root 16083: tmp_added = 1;
1.1.1.33 root 16084: } else if(strncmp(tmp, "TZ=", 3) == 0) {
16085: char *env = getenv("MSDOS_TZ");
16086: if(env != NULL) {
16087: sprintf(tmp, "TZ=%s", env);
16088: }
16089: tz_added = 1;
1.1 root 16090: }
16091: int len = strlen(tmp);
1.1.1.14 root 16092: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1 root 16093: fatalerror("too many environments\n");
16094: }
16095: memcpy(mem + (seg << 4) + ofs, tmp, len);
16096: ofs += len + 1;
16097: }
16098: }
1.1.1.32 root 16099: if(!append_added && env_append[0] != '\0') {
16100: #define SET_ENV(name, value) { \
16101: char tmp[ENV_SIZE]; \
16102: sprintf(tmp, "%s=%s", name, value); \
16103: int len = strlen(tmp); \
16104: if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
16105: fatalerror("too many environments\n"); \
16106: } \
16107: memcpy(mem + (seg << 4) + ofs, tmp, len); \
16108: ofs += len + 1; \
16109: }
16110: SET_ENV("APPEND", env_append);
16111: }
16112: if(!comspec_added) {
16113: SET_ENV("COMSPEC", "C:\\COMMAND.COM");
16114: }
1.1.1.33 root 16115: if(!lastdrive_added) {
16116: SET_ENV("LASTDRIVE", "Z");
16117: }
1.1.1.32 root 16118: if(!path_added) {
16119: SET_ENV("PATH", env_path);
16120: }
1.1.1.33 root 16121: if(!prompt_added) {
16122: SET_ENV("PROMPT", "$P$G");
16123: }
1.1.1.32 root 16124: if(!temp_added) {
16125: SET_ENV("TEMP", env_temp);
16126: }
16127: if(!tmp_added) {
16128: SET_ENV("TMP", env_temp);
16129: }
1.1.1.33 root 16130: if(!tz_added) {
16131: TIME_ZONE_INFORMATION tzi;
16132: HKEY hKey, hSubKey;
16133: char tzi_std_name[64];
16134: char tz_std[8] = "GMT";
16135: char tz_dlt[8] = "GST";
16136: char tz_value[32];
16137:
16138: // timezone name from GetTimeZoneInformation may not be english
16139: bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
16140: setlocale(LC_CTYPE, "");
16141: wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
16142:
16143: // get english timezone name from registry
16144: if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
16145: for(DWORD i = 0; !tz_added; i++) {
16146: char reg_name[256], sub_key[1024], std_name[256];
16147: DWORD size;
16148: FILETIME ftTime;
16149: LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
16150:
16151: if(result == ERROR_SUCCESS) {
16152: sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
16153: if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
16154: if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
16155: // search english timezone name from table
1.1.1.37 root 16156: if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33 root 16157: for(int j = 0; j < array_length(tz_table); j++) {
16158: if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
16159: if(tz_table[j].std != NULL) {
16160: strcpy(tz_std, tz_table[j].std);
16161: }
16162: if(tz_table[j].dlt != NULL) {
16163: strcpy(tz_dlt, tz_table[j].dlt);
16164: }
16165: tz_added = 1;
16166: break;
16167: }
16168: }
16169: }
16170: }
16171: RegCloseKey(hSubKey);
16172: }
16173: } else if(result == ERROR_NO_MORE_ITEMS) {
16174: break;
16175: }
16176: }
16177: RegCloseKey(hKey);
16178: }
16179: if((tzi.Bias % 60) != 0) {
16180: sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
16181: } else {
16182: sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
16183: }
16184: if(daylight) {
16185: strcat(tz_value, tz_dlt);
16186: }
16187: SET_ENV("TZ", tz_value);
16188: }
1.1 root 16189: seg += (ENV_SIZE >> 4);
16190:
16191: // psp
1.1.1.33 root 16192: msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1 root 16193: current_psp = seg;
1.1.1.35 root 16194: psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
16195: psp->parent_psp = current_psp;
1.1 root 16196: seg += (PSP_SIZE >> 4);
16197:
1.1.1.19 root 16198: // first free mcb in conventional memory
1.1.1.33 root 16199: msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8 root 16200: first_mcb = seg;
16201:
1.1.1.19 root 16202: // dummy mcb to link to umb
1.1.1.33 root 16203: #if 0
1.1.1.39 root 16204: msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33 root 16205: #else
1.1.1.39 root 16206: msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33 root 16207: #endif
1.1.1.19 root 16208:
16209: // first free mcb in upper memory block
1.1.1.8 root 16210: msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1 root 16211:
1.1.1.29 root 16212: #ifdef SUPPORT_HMA
16213: // first free mcb in high memory area
16214: msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
16215: #endif
16216:
1.1.1.26 root 16217: // interrupt vector
16218: for(int i = 0; i < 0x80; i++) {
16219: *(UINT16 *)(mem + 4 * i + 0) = i;
16220: *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
16221: }
1.1.1.35 root 16222: *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018; // fffd:0018 irq0 (system timer)
1.1.1.26 root 16223: *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
16224: *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000; // ffff:0000 boot
16225: *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
16226: *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012; // xxxx:0012 ems
16227: *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
16228: *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000; // fffd:0000 irq12 (mouse)
16229: *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
16230:
1.1.1.29 root 16231: // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26 root 16232: static const struct {
16233: UINT16 attributes;
16234: char *dev_name;
16235: } dummy_devices[] = {
16236: {0x8013, "CON "},
16237: {0x8000, "AUX "},
16238: {0xa0c0, "PRN "},
16239: {0x8008, "CLOCK$ "},
16240: {0x8000, "COM1 "},
16241: {0xa0c0, "LPT1 "},
16242: {0xa0c0, "LPT2 "},
16243: {0xa0c0, "LPT3 "},
16244: {0x8000, "COM2 "},
16245: {0x8000, "COM3 "},
16246: {0x8000, "COM4 "},
1.1.1.30 root 16247: // {0xc000, "CONFIG$ "},
16248: {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26 root 16249: };
16250: static const UINT8 dummy_device_routine[] = {
16251: // from NUL device of Windows 98 SE
16252: // or word ptr ES:[BX+03],0100
16253: 0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
16254: // retf
16255: 0xcb,
16256: };
1.1.1.29 root 16257: device_t *last = NULL;
1.1.1.32 root 16258: for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26 root 16259: device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29 root 16260: device->next_driver.w.l = 22 + 18 * (i + 1);
16261: device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 16262: device->attributes = dummy_devices[i].attributes;
1.1.1.29 root 16263: device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
16264: device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26 root 16265: memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29 root 16266: last = device;
16267: }
16268: if(last != NULL) {
16269: last->next_driver.w.l = 0;
16270: last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26 root 16271: }
1.1.1.29 root 16272: memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26 root 16273:
1.1.1.25 root 16274: // dos info
16275: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
16276: dos_info->magic_word = 1;
16277: dos_info->first_mcb = MEMORY_TOP >> 4;
16278: dos_info->first_dpb.w.l = 0;
16279: dos_info->first_dpb.w.h = DPB_TOP >> 4;
16280: dos_info->first_sft.w.l = 0;
16281: dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41 root 16282: dos_info->clock_device.w.l = 22 + 18 * 3; // CLOCK$ is the 4th device in IO.SYS
1.1.1.25 root 16283: dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26 root 16284: dos_info->con_device.w.l = 22 + 18 * 0; // CON is the 1st device in IO.SYS
1.1.1.25 root 16285: dos_info->con_device.w.h = DEVICE_TOP >> 4;
16286: dos_info->max_sector_len = 512;
16287: dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
16288: dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
16289: dos_info->cds.w.l = 0;
16290: dos_info->cds.w.h = CDS_TOP >> 4;
16291: dos_info->fcb_table.w.l = 0;
16292: dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
16293: dos_info->last_drive = 'Z' - 'A' + 1;
16294: dos_info->buffers_x = 20;
16295: dos_info->buffers_y = 0;
16296: dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29 root 16297: dos_info->nul_device.next_driver.w.l = 22;
16298: dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25 root 16299: dos_info->nul_device.attributes = 0x8004;
1.1.1.29 root 16300: dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
16301: dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 16302: memcpy(dos_info->nul_device.dev_name, "NUL ", 8);
16303: dos_info->disk_buf_heads.w.l = 0;
16304: dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39 root 16305: dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25 root 16306: dos_info->first_umb_fcb = UMB_TOP >> 4;
16307: dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29 root 16308: memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25 root 16309:
16310: char *env;
16311: if((env = getenv("LASTDRIVE")) != NULL) {
16312: if(env[0] >= 'A' && env[0] <= 'Z') {
16313: dos_info->last_drive = env[0] - 'A' + 1;
16314: } else if(env[0] >= 'a' && env[0] <= 'z') {
16315: dos_info->last_drive = env[0] - 'a' + 1;
16316: }
16317: }
16318: if((env = getenv("windir")) != NULL) {
16319: if(env[0] >= 'A' && env[0] <= 'Z') {
16320: dos_info->boot_drive = env[0] - 'A' + 1;
16321: } else if(env[0] >= 'a' && env[0] <= 'z') {
16322: dos_info->boot_drive = env[0] - 'a' + 1;
16323: }
16324: }
16325: #if defined(HAS_I386)
16326: dos_info->i386_or_later = 1;
16327: #else
16328: dos_info->i386_or_later = 0;
16329: #endif
16330: dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
16331:
1.1.1.27 root 16332: // ems (int 67h) and xms
1.1.1.25 root 16333: device_t *xms_device = (device_t *)(mem + XMS_TOP);
16334: xms_device->next_driver.w.l = 0xffff;
16335: xms_device->next_driver.w.h = 0xffff;
16336: xms_device->attributes = 0xc000;
1.1.1.29 root 16337: xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
16338: xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25 root 16339: memcpy(xms_device->dev_name, "EMMXXXX0", 8);
16340:
1.1.1.26 root 16341: mem[XMS_TOP + 0x12] = 0xcd; // int 68h (dummy)
16342: mem[XMS_TOP + 0x13] = 0x68;
16343: mem[XMS_TOP + 0x14] = 0xcf; // iret
1.1.1.19 root 16344: #ifdef SUPPORT_XMS
16345: if(support_xms) {
1.1.1.26 root 16346: mem[XMS_TOP + 0x15] = 0xcd; // int 69h (dummy)
16347: mem[XMS_TOP + 0x16] = 0x69;
16348: mem[XMS_TOP + 0x17] = 0xcb; // retf
1.1.1.19 root 16349: } else
16350: #endif
1.1.1.26 root 16351: mem[XMS_TOP + 0x15] = 0xcb; // retf
1.1.1.29 root 16352: memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19 root 16353:
1.1.1.26 root 16354: // irq12 routine (mouse)
1.1.1.24 root 16355: mem[0xfffd0 + 0x00] = 0xcd; // int 6ah (dummy)
16356: mem[0xfffd0 + 0x01] = 0x6a;
16357: mem[0xfffd0 + 0x02] = 0x9a; // call far mouse
16358: mem[0xfffd0 + 0x03] = 0xff;
16359: mem[0xfffd0 + 0x04] = 0xff;
16360: mem[0xfffd0 + 0x05] = 0xff;
16361: mem[0xfffd0 + 0x06] = 0xff;
16362: mem[0xfffd0 + 0x07] = 0xcd; // int 6bh (dummy)
16363: mem[0xfffd0 + 0x08] = 0x6b;
16364: mem[0xfffd0 + 0x09] = 0xcf; // iret
16365:
1.1.1.27 root 16366: // case map routine
16367: mem[0xfffd0 + 0x0a] = 0xcd; // int 6ch (dummy)
16368: mem[0xfffd0 + 0x0b] = 0x6c;
16369: mem[0xfffd0 + 0x0c] = 0xcb; // retf
16370:
16371: // font read routine
16372: mem[0xfffd0 + 0x0d] = 0xcd; // int 6dh (dummy)
16373: mem[0xfffd0 + 0x0e] = 0x6d;
16374: mem[0xfffd0 + 0x0f] = 0xcb; // retf
1.1.1.19 root 16375:
1.1.1.32 root 16376: // error message read routine
16377: mem[0xfffd0 + 0x10] = 0xcd; // int 6eh (dummy)
16378: mem[0xfffd0 + 0x11] = 0x6e;
16379: mem[0xfffd0 + 0x12] = 0xcb; // retf
16380:
1.1.1.35 root 16381: // dummy loop to wait BIOS/DOS service is done
16382: mem[0xfffd0 + 0x13] = 0xe6; // out f7h, al
16383: mem[0xfffd0 + 0x14] = 0xf7;
16384: mem[0xfffd0 + 0x15] = 0x78; // js/jns -4
16385: mem[0xfffd0 + 0x16] = 0xfc;
16386: mem[0xfffd0 + 0x17] = 0xcb; // retf
16387:
1.1.1.26 root 16388: // irq0 routine (system time)
1.1.1.35 root 16389: mem[0xfffd0 + 0x18] = 0xcd; // int 1ch
16390: mem[0xfffd0 + 0x19] = 0x1c;
16391: mem[0xfffd0 + 0x1a] = 0xea; // jmp far (IRET_TOP >> 4):0008
16392: mem[0xfffd0 + 0x1b] = 0x08;
16393: mem[0xfffd0 + 0x1c] = 0x00;
16394: mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4) ) & 0xff;
16395: mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14 root 16396:
1.1.1.26 root 16397: // boot routine
1.1 root 16398: mem[0xffff0] = 0xf4; // halt
16399: mem[0xffff1] = 0xcd; // int 21h
16400: mem[0xffff2] = 0x21;
16401: mem[0xffff3] = 0xcb; // retf
16402:
1.1.1.24 root 16403: mem[0xffff5] = '0'; // rom date
16404: mem[0xffff6] = '2';
16405: mem[0xffff7] = '/';
16406: mem[0xffff8] = '2';
16407: mem[0xffff9] = '2';
16408: mem[0xffffa] = '/';
16409: mem[0xffffb] = '0';
16410: mem[0xffffc] = '6';
16411: mem[0xffffe] = 0xfc; // machine id
16412: mem[0xfffff] = 0x00;
16413:
1.1 root 16414: // param block
16415: // + 0: param block (22bytes)
16416: // +24: fcb1/2 (20bytes)
16417: // +44: command tail (128bytes)
16418: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
16419: param->env_seg = 0;
16420: param->cmd_line.w.l = 44;
16421: param->cmd_line.w.h = (WORK_TOP >> 4);
16422: param->fcb1.w.l = 24;
16423: param->fcb1.w.h = (WORK_TOP >> 4);
16424: param->fcb2.w.l = 24;
16425: param->fcb2.w.h = (WORK_TOP >> 4);
16426:
16427: memset(mem + WORK_TOP + 24, 0x20, 20);
16428:
16429: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
16430: if(argc > 1) {
16431: sprintf(cmd_line->cmd, " %s", argv[1]);
16432: for(int i = 2; i < argc; i++) {
16433: char tmp[128];
16434: sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
16435: strcpy(cmd_line->cmd, tmp);
16436: }
16437: cmd_line->len = (UINT8)strlen(cmd_line->cmd);
16438: } else {
16439: cmd_line->len = 0;
16440: }
16441: cmd_line->cmd[cmd_line->len] = 0x0d;
16442:
16443: // system file table
1.1.1.21 root 16444: *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
16445: *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1 root 16446:
1.1.1.19 root 16447: // disk buffer header (from DOSBox)
16448: *(UINT16 *)(mem + DISK_BUF_TOP + 0) = 0xffff; // forward ptr
16449: *(UINT16 *)(mem + DISK_BUF_TOP + 2) = 0xffff; // backward ptr
16450: *(UINT8 *)(mem + DISK_BUF_TOP + 4) = 0xff; // not in use
16451: *(UINT8 *)(mem + DISK_BUF_TOP + 10) = 0x01; // number of FATs
16452: *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff; // pointer to DPB
16453:
1.1 root 16454: // current directory structure
16455: msdos_cds_update(_getdrive() - 1);
16456:
16457: // fcb table
16458: *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14 root 16459: *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1 root 16460:
1.1.1.41 root 16461: // drive parameter block
1.1.1.42! root 16462: for(int i = 0; i < 2; i++) {
1.1.1.41 root 16463: dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
16464: dpb->drive_num = i;
16465: dpb->unit_num = i;
16466: dpb->next_dpb_ofs = (i == 25) ? 0xffff : sizeof(dpb_t) * (i + 1);
16467: dpb->next_dpb_seg = (i == 25) ? 0xffff : DPB_TOP >> 4;
1.1.1.42! root 16468: }
! 16469: for(int i = 2; i < 26; i++) {
! 16470: UINT16 seg, ofs;
! 16471: msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41 root 16472: }
16473:
1.1.1.17 root 16474: // nls stuff
16475: msdos_nls_tables_init();
1.1 root 16476:
16477: // execute command
1.1.1.28 root 16478: try {
16479: if(msdos_process_exec(argv[0], param, 0)) {
16480: fatalerror("'%s' not found\n", argv[0]);
16481: }
16482: } catch(...) {
16483: // we should not reach here :-(
16484: fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1 root 16485: }
16486: retval = 0;
16487: return(0);
16488: }
16489:
16490: #define remove_std_file(path) { \
16491: int fd = _open(path, _O_RDONLY | _O_BINARY); \
16492: if(fd != -1) { \
16493: _lseek(fd, 0, SEEK_END); \
16494: int size = _tell(fd); \
16495: _close(fd); \
16496: if(size == 0) { \
16497: remove(path); \
16498: } \
16499: } \
16500: }
16501:
16502: void msdos_finish()
16503: {
16504: for(int i = 0; i < MAX_FILES; i++) {
16505: if(file_handler[i].valid) {
16506: _close(i);
16507: }
16508: }
1.1.1.21 root 16509: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1 root 16510: remove_std_file("stdaux.txt");
1.1.1.21 root 16511: #endif
1.1.1.30 root 16512: #ifdef SUPPORT_XMS
16513: msdos_xms_finish();
16514: #endif
1.1 root 16515: msdos_dbcs_table_finish();
16516: }
16517:
16518: /* ----------------------------------------------------------------------------
16519: PC/AT hardware emulation
16520: ---------------------------------------------------------------------------- */
16521:
16522: void hardware_init()
16523: {
1.1.1.3 root 16524: CPU_INIT_CALL(CPU_MODEL);
1.1 root 16525: CPU_RESET_CALL(CPU_MODEL);
1.1.1.14 root 16526: m_IF = 1;
1.1.1.3 root 16527: #if defined(HAS_I386)
1.1 root 16528: cpu_type = (REG32(EDX) >> 8) & 0x0f;
16529: cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3 root 16530: #endif
16531: i386_set_a20_line(0);
1.1.1.14 root 16532:
1.1.1.19 root 16533: ems_init();
1.1.1.25 root 16534: dma_init();
1.1 root 16535: pic_init();
1.1.1.25 root 16536: pio_init();
1.1.1.8 root 16537: #ifdef PIT_ALWAYS_RUNNING
16538: pit_init();
16539: #else
1.1 root 16540: pit_active = 0;
16541: #endif
1.1.1.25 root 16542: sio_init();
1.1.1.8 root 16543: cmos_init();
16544: kbd_init();
1.1 root 16545: }
16546:
1.1.1.10 root 16547: void hardware_finish()
16548: {
16549: #if defined(HAS_I386)
16550: vtlb_free(m_vtlb);
16551: #endif
1.1.1.19 root 16552: ems_finish();
1.1.1.37 root 16553: pio_finish();
1.1.1.25 root 16554: sio_finish();
1.1.1.10 root 16555: }
16556:
1.1.1.28 root 16557: void hardware_release()
16558: {
16559: // release hardware resources when this program will be terminated abnormally
16560: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 16561: if(fp_debug_log != NULL) {
16562: fclose(fp_debug_log);
16563: fp_debug_log = NULL;
1.1.1.28 root 16564: }
16565: #endif
16566: #if defined(HAS_I386)
16567: vtlb_free(m_vtlb);
16568: #endif
16569: ems_release();
1.1.1.37 root 16570: pio_release();
1.1.1.28 root 16571: sio_release();
16572: }
16573:
1.1 root 16574: void hardware_run()
16575: {
1.1.1.22 root 16576: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28 root 16577: // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33 root 16578: fp_debug_log = fopen("debug.log", "w");
1.1.1.22 root 16579: #endif
1.1.1.3 root 16580: while(!m_halted) {
16581: #if defined(HAS_I386)
1.1 root 16582: CPU_EXECUTE_CALL(i386);
1.1.1.14 root 16583: if(m_eip != m_prev_eip) {
1.1.1.35 root 16584: idle_ops++;
16585: }
1.1.1.14 root 16586: #else
1.1.1.35 root 16587: CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14 root 16588: if(m_pc != m_prevpc) {
1.1.1.35 root 16589: idle_ops++;
1.1.1.14 root 16590: }
1.1.1.35 root 16591: #endif
16592: if(++update_ops == UPDATE_OPS) {
1.1 root 16593: hardware_update();
1.1.1.35 root 16594: update_ops = 0;
1.1 root 16595: }
16596: }
1.1.1.22 root 16597: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33 root 16598: if(fp_debug_log != NULL) {
16599: fclose(fp_debug_log);
16600: fp_debug_log = NULL;
1.1.1.28 root 16601: }
1.1.1.22 root 16602: #endif
1.1 root 16603: }
16604:
16605: void hardware_update()
16606: {
1.1.1.8 root 16607: static UINT32 prev_time = 0;
16608: UINT32 cur_time = timeGetTime();
16609:
16610: if(prev_time != cur_time) {
16611: // update pit and raise irq0
16612: #ifndef PIT_ALWAYS_RUNNING
16613: if(pit_active)
16614: #endif
16615: {
16616: if(pit_run(0, cur_time)) {
16617: pic_req(0, 0, 1);
16618: }
16619: pit_run(1, cur_time);
16620: pit_run(2, cur_time);
16621: }
1.1.1.24 root 16622:
1.1.1.25 root 16623: // update sio and raise irq4/3
1.1.1.29 root 16624: for(int c = 0; c < 4; c++) {
1.1.1.25 root 16625: sio_update(c);
16626: }
16627:
1.1.1.24 root 16628: // update keyboard and mouse
1.1.1.14 root 16629: static UINT32 prev_tick = 0;
16630: UINT32 cur_tick = cur_time / 32;
1.1.1.25 root 16631:
1.1.1.14 root 16632: if(prev_tick != cur_tick) {
16633: // update keyboard flags
16634: UINT8 state;
1.1.1.24 root 16635: state = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
16636: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
16637: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
16638: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
16639: state |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
16640: state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
16641: state |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
16642: state |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
1.1.1.14 root 16643: mem[0x417] = state;
16644: state = (GetAsyncKeyState(VK_INSERT ) & 0x8000) ? 0x80 : 0;
16645: state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
16646: state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
16647: state |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
1.1.1.24 root 16648: // state |= (GetAsyncKeyState(VK_PAUSE ) & 0x0001) ? 0x08 : 0;
16649: // state |= (GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x04 : 0;
1.1.1.14 root 16650: state |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
16651: state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
16652: mem[0x418] = state;
16653:
1.1.1.24 root 16654: // update console input if needed
1.1.1.34 root 16655: if(!key_changed || mouse.hidden == 0) {
1.1.1.24 root 16656: update_console_input();
16657: }
16658:
16659: // raise irq1 if key is pressed/released
16660: if(key_changed) {
1.1.1.8 root 16661: pic_req(0, 1, 1);
1.1.1.24 root 16662: key_changed = false;
16663: }
16664:
16665: // raise irq12 if mouse status is changed
16666: if(mouse.status & mouse.call_mask) {
1.1.1.34 root 16667: // if(mouse.hidden == 0) {
1.1.1.24 root 16668: pic_req(1, 4, 1);
16669: mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34 root 16670: // }
1.1.1.24 root 16671: mouse.status &= ~mouse.call_mask;
1.1.1.8 root 16672: }
1.1.1.24 root 16673:
1.1.1.14 root 16674: prev_tick = cur_tick;
1.1.1.8 root 16675: }
1.1.1.24 root 16676:
1.1.1.19 root 16677: // update daily timer counter
16678: pcbios_update_daily_timer_counter(cur_time);
1.1.1.25 root 16679:
1.1.1.8 root 16680: prev_time = cur_time;
1.1 root 16681: }
16682: }
16683:
1.1.1.19 root 16684: // ems
16685:
16686: void ems_init()
16687: {
16688: memset(ems_handles, 0, sizeof(ems_handles));
16689: memset(ems_pages, 0, sizeof(ems_pages));
16690: free_ems_pages = MAX_EMS_PAGES;
16691: }
16692:
16693: void ems_finish()
16694: {
1.1.1.28 root 16695: ems_release();
16696: }
16697:
16698: void ems_release()
16699: {
1.1.1.31 root 16700: for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28 root 16701: if(ems_handles[i].buffer != NULL) {
1.1.1.19 root 16702: free(ems_handles[i].buffer);
16703: ems_handles[i].buffer = NULL;
16704: }
16705: }
16706: }
16707:
16708: void ems_allocate_pages(int handle, int pages)
16709: {
16710: if(pages > 0) {
16711: ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
16712: } else {
16713: ems_handles[handle].buffer = NULL;
16714: }
16715: ems_handles[handle].pages = pages;
16716: ems_handles[handle].allocated = true;
16717: free_ems_pages -= pages;
16718: }
16719:
16720: void ems_reallocate_pages(int handle, int pages)
16721: {
16722: if(ems_handles[handle].allocated) {
16723: if(ems_handles[handle].pages != pages) {
16724: UINT8 *new_buffer = NULL;
16725:
16726: if(pages > 0) {
16727: new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
16728: }
1.1.1.32 root 16729: if(ems_handles[handle].buffer != NULL) {
16730: if(new_buffer != NULL) {
1.1.1.19 root 16731: if(pages > ems_handles[handle].pages) {
16732: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
16733: } else {
16734: memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
16735: }
16736: }
16737: free(ems_handles[handle].buffer);
16738: ems_handles[handle].buffer = NULL;
16739: }
16740: free_ems_pages += ems_handles[handle].pages;
16741:
16742: ems_handles[handle].buffer = new_buffer;
16743: ems_handles[handle].pages = pages;
16744: free_ems_pages -= pages;
16745: }
16746: } else {
16747: ems_allocate_pages(handle, pages);
16748: }
16749: }
16750:
16751: void ems_release_pages(int handle)
16752: {
16753: if(ems_handles[handle].allocated) {
1.1.1.32 root 16754: if(ems_handles[handle].buffer != NULL) {
1.1.1.19 root 16755: free(ems_handles[handle].buffer);
16756: ems_handles[handle].buffer = NULL;
16757: }
16758: free_ems_pages += ems_handles[handle].pages;
16759: ems_handles[handle].allocated = false;
16760: }
16761: }
16762:
16763: void ems_map_page(int physical, int handle, int logical)
16764: {
16765: if(ems_pages[physical].mapped) {
16766: if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
16767: return;
16768: }
16769: ems_unmap_page(physical);
16770: }
1.1.1.32 root 16771: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 16772: memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
16773: }
16774: ems_pages[physical].handle = handle;
16775: ems_pages[physical].page = logical;
16776: ems_pages[physical].mapped = true;
16777: }
16778:
16779: void ems_unmap_page(int physical)
16780: {
16781: if(ems_pages[physical].mapped) {
16782: int handle = ems_pages[physical].handle;
16783: int logical = ems_pages[physical].page;
16784:
1.1.1.32 root 16785: if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19 root 16786: memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
16787: }
16788: ems_pages[physical].mapped = false;
16789: }
16790: }
16791:
1.1.1.25 root 16792: // dma
1.1 root 16793:
1.1.1.25 root 16794: void dma_init()
1.1 root 16795: {
1.1.1.26 root 16796: memset(dma, 0, sizeof(dma));
1.1.1.25 root 16797: for(int c = 0; c < 2; c++) {
1.1.1.26 root 16798: // for(int ch = 0; ch < 4; ch++) {
16799: // dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
16800: // }
1.1.1.25 root 16801: dma_reset(c);
16802: }
1.1 root 16803: }
16804:
1.1.1.25 root 16805: void dma_reset(int c)
1.1 root 16806: {
1.1.1.25 root 16807: dma[c].low_high = false;
16808: dma[c].cmd = dma[c].req = dma[c].tc = 0;
16809: dma[c].mask = 0xff;
16810: }
16811:
16812: void dma_write(int c, UINT32 addr, UINT8 data)
16813: {
16814: int ch = (addr >> 1) & 3;
16815: UINT8 bit = 1 << (data & 3);
16816:
16817: switch(addr & 0x0f) {
16818: case 0x00: case 0x02: case 0x04: case 0x06:
16819: if(dma[c].low_high) {
16820: dma[c].ch[ch].bareg.b.h = data;
1.1 root 16821: } else {
1.1.1.25 root 16822: dma[c].ch[ch].bareg.b.l = data;
16823: }
16824: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
16825: dma[c].low_high = !dma[c].low_high;
16826: break;
16827: case 0x01: case 0x03: case 0x05: case 0x07:
16828: if(dma[c].low_high) {
16829: dma[c].ch[ch].bcreg.b.h = data;
16830: } else {
16831: dma[c].ch[ch].bcreg.b.l = data;
16832: }
16833: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
16834: dma[c].low_high = !dma[c].low_high;
16835: break;
16836: case 0x08:
16837: // command register
16838: dma[c].cmd = data;
16839: break;
16840: case 0x09:
16841: // dma[c].request register
16842: if(data & 4) {
16843: if(!(dma[c].req & bit)) {
16844: dma[c].req |= bit;
16845: // dma_run(c, ch);
16846: }
16847: } else {
16848: dma[c].req &= ~bit;
16849: }
16850: break;
16851: case 0x0a:
16852: // single mask register
16853: if(data & 4) {
16854: dma[c].mask |= bit;
16855: } else {
16856: dma[c].mask &= ~bit;
16857: }
16858: break;
16859: case 0x0b:
16860: // mode register
16861: dma[c].ch[data & 3].mode = data;
16862: break;
16863: case 0x0c:
16864: dma[c].low_high = false;
16865: break;
16866: case 0x0d:
16867: // clear master
16868: dma_reset(c);
16869: break;
16870: case 0x0e:
16871: // clear mask register
16872: dma[c].mask = 0;
16873: break;
16874: case 0x0f:
16875: // all mask register
16876: dma[c].mask = data & 0x0f;
16877: break;
16878: }
16879: }
16880:
16881: UINT8 dma_read(int c, UINT32 addr)
16882: {
16883: int ch = (addr >> 1) & 3;
16884: UINT8 val = 0xff;
16885:
16886: switch(addr & 0x0f) {
16887: case 0x00: case 0x02: case 0x04: case 0x06:
16888: if(dma[c].low_high) {
16889: val = dma[c].ch[ch].areg.b.h;
16890: } else {
16891: val = dma[c].ch[ch].areg.b.l;
16892: }
16893: dma[c].low_high = !dma[c].low_high;
16894: return(val);
16895: case 0x01: case 0x03: case 0x05: case 0x07:
16896: if(dma[c].low_high) {
16897: val = dma[c].ch[ch].creg.b.h;
16898: } else {
16899: val = dma[c].ch[ch].creg.b.l;
16900: }
16901: dma[c].low_high = !dma[c].low_high;
16902: return(val);
16903: case 0x08:
16904: // status register
16905: val = (dma[c].req << 4) | dma[c].tc;
16906: dma[c].tc = 0;
16907: return(val);
16908: case 0x0d:
1.1.1.26 root 16909: // temporary register (intel 82374 does not support)
1.1.1.25 root 16910: return(dma[c].tmp & 0xff);
1.1.1.26 root 16911: case 0x0f:
16912: // mask register (intel 82374 does support)
16913: return(dma[c].mask);
1.1.1.25 root 16914: }
16915: return(0xff);
16916: }
16917:
16918: void dma_page_write(int c, int ch, UINT8 data)
16919: {
16920: dma[c].ch[ch].pagereg = data;
16921: }
16922:
16923: UINT8 dma_page_read(int c, int ch)
16924: {
16925: return(dma[c].ch[ch].pagereg);
16926: }
16927:
16928: void dma_run(int c, int ch)
16929: {
16930: UINT8 bit = 1 << ch;
16931:
16932: if((dma[c].req & bit) && !(dma[c].mask & bit)) {
16933: // execute dma
16934: while(dma[c].req & bit) {
16935: if(ch == 0 && (dma[c].cmd & 0x01)) {
16936: // memory -> memory
16937: UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
16938: UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
16939:
16940: if(c == 0) {
16941: dma[c].tmp = read_byte(saddr);
16942: write_byte(daddr, dma[c].tmp);
16943: } else {
16944: dma[c].tmp = read_word(saddr << 1);
16945: write_word(daddr << 1, dma[c].tmp);
16946: }
16947: if(!(dma[c].cmd & 0x02)) {
16948: if(dma[c].ch[0].mode & 0x20) {
16949: dma[c].ch[0].areg.w--;
16950: if(dma[c].ch[0].areg.w == 0xffff) {
16951: dma[c].ch[0].pagereg--;
16952: }
16953: } else {
16954: dma[c].ch[0].areg.w++;
16955: if(dma[c].ch[0].areg.w == 0) {
16956: dma[c].ch[0].pagereg++;
16957: }
16958: }
16959: }
16960: if(dma[c].ch[1].mode & 0x20) {
16961: dma[c].ch[1].areg.w--;
16962: if(dma[c].ch[1].areg.w == 0xffff) {
16963: dma[c].ch[1].pagereg--;
16964: }
16965: } else {
16966: dma[c].ch[1].areg.w++;
16967: if(dma[c].ch[1].areg.w == 0) {
16968: dma[c].ch[1].pagereg++;
16969: }
16970: }
16971:
16972: // check dma condition
16973: if(dma[c].ch[0].creg.w-- == 0) {
16974: if(dma[c].ch[0].mode & 0x10) {
16975: // self initialize
16976: dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
16977: dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
16978: } else {
16979: // dma[c].mask |= bit;
16980: }
16981: }
16982: if(dma[c].ch[1].creg.w-- == 0) {
16983: // terminal count
16984: if(dma[c].ch[1].mode & 0x10) {
16985: // self initialize
16986: dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
16987: dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
16988: } else {
16989: dma[c].mask |= bit;
16990: }
16991: dma[c].req &= ~bit;
16992: dma[c].tc |= bit;
16993: }
16994: } else {
16995: UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
16996:
16997: if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
16998: // verify
16999: } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
17000: // io -> memory
17001: if(c == 0) {
17002: dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
17003: write_byte(addr, dma[c].tmp);
17004: } else {
17005: dma[c].tmp = read_io_word(dma[c].ch[ch].port);
17006: write_word(addr << 1, dma[c].tmp);
17007: }
17008: } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
17009: // memory -> io
17010: if(c == 0) {
17011: dma[c].tmp = read_byte(addr);
17012: write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
17013: } else {
17014: dma[c].tmp = read_word(addr << 1);
17015: write_io_word(dma[c].ch[ch].port, dma[c].tmp);
17016: }
17017: }
17018: if(dma[c].ch[ch].mode & 0x20) {
17019: dma[c].ch[ch].areg.w--;
17020: if(dma[c].ch[ch].areg.w == 0xffff) {
17021: dma[c].ch[ch].pagereg--;
17022: }
17023: } else {
17024: dma[c].ch[ch].areg.w++;
17025: if(dma[c].ch[ch].areg.w == 0) {
17026: dma[c].ch[ch].pagereg++;
17027: }
17028: }
17029:
17030: // check dma condition
17031: if(dma[c].ch[ch].creg.w-- == 0) {
17032: // terminal count
17033: if(dma[c].ch[ch].mode & 0x10) {
17034: // self initialize
17035: dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
17036: dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
17037: } else {
17038: dma[c].mask |= bit;
17039: }
17040: dma[c].req &= ~bit;
17041: dma[c].tc |= bit;
17042: } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
17043: // single mode
17044: break;
17045: }
17046: }
17047: }
17048: }
17049: }
17050:
17051: // pic
17052:
17053: void pic_init()
17054: {
17055: memset(pic, 0, sizeof(pic));
17056: pic[0].imr = pic[1].imr = 0xff;
17057:
17058: // from bochs bios
17059: pic_write(0, 0, 0x11); // icw1 = 11h
17060: pic_write(0, 1, 0x08); // icw2 = 08h
17061: pic_write(0, 1, 0x04); // icw3 = 04h
17062: pic_write(0, 1, 0x01); // icw4 = 01h
17063: pic_write(0, 1, 0xb8); // ocw1 = b8h
17064: pic_write(1, 0, 0x11); // icw1 = 11h
17065: pic_write(1, 1, 0x70); // icw2 = 70h
17066: pic_write(1, 1, 0x02); // icw3 = 02h
17067: pic_write(1, 1, 0x01); // icw4 = 01h
17068: }
17069:
17070: void pic_write(int c, UINT32 addr, UINT8 data)
17071: {
17072: if(addr & 1) {
17073: if(pic[c].icw2_r) {
17074: // icw2
17075: pic[c].icw2 = data;
17076: pic[c].icw2_r = 0;
17077: } else if(pic[c].icw3_r) {
17078: // icw3
17079: pic[c].icw3 = data;
17080: pic[c].icw3_r = 0;
17081: } else if(pic[c].icw4_r) {
17082: // icw4
17083: pic[c].icw4 = data;
17084: pic[c].icw4_r = 0;
17085: } else {
17086: // ocw1
1.1 root 17087: pic[c].imr = data;
17088: }
17089: } else {
17090: if(data & 0x10) {
17091: // icw1
17092: pic[c].icw1 = data;
17093: pic[c].icw2_r = 1;
17094: pic[c].icw3_r = (data & 2) ? 0 : 1;
17095: pic[c].icw4_r = data & 1;
17096: pic[c].irr = 0;
17097: pic[c].isr = 0;
17098: pic[c].imr = 0;
17099: pic[c].prio = 0;
17100: if(!(pic[c].icw1 & 1)) {
17101: pic[c].icw4 = 0;
17102: }
17103: pic[c].ocw3 = 0;
17104: } else if(data & 8) {
17105: // ocw3
17106: if(!(data & 2)) {
17107: data = (data & ~1) | (pic[c].ocw3 & 1);
17108: }
17109: if(!(data & 0x40)) {
17110: data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
17111: }
17112: pic[c].ocw3 = data;
17113: } else {
17114: // ocw2
17115: int level = 0;
17116: if(data & 0x40) {
17117: level = data & 7;
17118: } else {
17119: if(!pic[c].isr) {
17120: return;
17121: }
17122: level = pic[c].prio;
17123: while(!(pic[c].isr & (1 << level))) {
17124: level = (level + 1) & 7;
17125: }
17126: }
17127: if(data & 0x80) {
17128: pic[c].prio = (level + 1) & 7;
17129: }
17130: if(data & 0x20) {
17131: pic[c].isr &= ~(1 << level);
17132: }
17133: }
17134: }
17135: pic_update();
17136: }
17137:
17138: UINT8 pic_read(int c, UINT32 addr)
17139: {
17140: if(addr & 1) {
17141: return(pic[c].imr);
17142: } else {
17143: // polling mode is not supported...
17144: //if(pic[c].ocw3 & 4) {
17145: // return ???;
17146: //}
17147: if(pic[c].ocw3 & 1) {
17148: return(pic[c].isr);
17149: } else {
17150: return(pic[c].irr);
17151: }
17152: }
17153: }
17154:
17155: void pic_req(int c, int level, int signal)
17156: {
17157: if(signal) {
17158: pic[c].irr |= (1 << level);
17159: } else {
17160: pic[c].irr &= ~(1 << level);
17161: }
17162: pic_update();
17163: }
17164:
17165: int pic_ack()
17166: {
17167: // ack (INTA=L)
17168: pic[pic_req_chip].isr |= pic_req_bit;
17169: pic[pic_req_chip].irr &= ~pic_req_bit;
17170: if(pic_req_chip > 0) {
17171: // update isr and irr of master
17172: UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
17173: pic[pic_req_chip - 1].isr |= slave;
17174: pic[pic_req_chip - 1].irr &= ~slave;
17175: }
17176: //if(pic[pic_req_chip].icw4 & 1) {
17177: // 8086 mode
17178: int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
17179: //} else {
17180: // // 8080 mode
17181: // UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
17182: // if(pic[pic_req_chip].icw1 & 4) {
17183: // addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
17184: // } else {
17185: // addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
17186: // }
17187: // vector = 0xcd | (addr << 8);
17188: //}
17189: if(pic[pic_req_chip].icw4 & 2) {
17190: // auto eoi
17191: pic[pic_req_chip].isr &= ~pic_req_bit;
17192: }
17193: return(vector);
17194: }
17195:
17196: void pic_update()
17197: {
17198: for(int c = 0; c < 2; c++) {
17199: UINT8 irr = pic[c].irr;
17200: if(c + 1 < 2) {
17201: // this is master
17202: if(pic[c + 1].irr & (~pic[c + 1].imr)) {
17203: // request from slave
17204: irr |= 1 << (pic[c + 1].icw3 & 7);
17205: }
17206: }
17207: irr &= (~pic[c].imr);
17208: if(!irr) {
17209: break;
17210: }
17211: if(!(pic[c].ocw3 & 0x20)) {
17212: irr |= pic[c].isr;
17213: }
17214: int level = pic[c].prio;
17215: UINT8 bit = 1 << level;
17216: while(!(irr & bit)) {
17217: level = (level + 1) & 7;
17218: bit = 1 << level;
17219: }
17220: if((c + 1 < 2) && (pic[c].icw3 & bit)) {
17221: // check slave
17222: continue;
17223: }
17224: if(pic[c].isr & bit) {
17225: break;
17226: }
17227: // interrupt request
17228: pic_req_chip = c;
17229: pic_req_level = level;
17230: pic_req_bit = bit;
1.1.1.3 root 17231: i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1 root 17232: return;
17233: }
1.1.1.3 root 17234: i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2 root 17235: }
1.1 root 17236:
1.1.1.25 root 17237: // pio
17238:
17239: void pio_init()
17240: {
1.1.1.38 root 17241: // bool conv_mode = (GetConsoleCP() == 932);
17242:
1.1.1.26 root 17243: memset(pio, 0, sizeof(pio));
1.1.1.37 root 17244:
1.1.1.25 root 17245: for(int c = 0; c < 2; c++) {
1.1.1.37 root 17246: pio[c].stat = 0xdf;
1.1.1.25 root 17247: pio[c].ctrl = 0x0c;
1.1.1.38 root 17248: // pio[c].conv_mode = conv_mode;
1.1.1.25 root 17249: }
17250: }
17251:
1.1.1.37 root 17252: void pio_finish()
17253: {
17254: pio_release();
17255: }
17256:
17257: void pio_release()
17258: {
17259: for(int c = 0; c < 2; c++) {
17260: if(pio[c].fp != NULL) {
1.1.1.38 root 17261: if(pio[c].jis_mode) {
17262: fputc(0x1c, pio[c].fp);
17263: fputc(0x2e, pio[c].fp);
17264: }
1.1.1.37 root 17265: fclose(pio[c].fp);
17266: pio[c].fp = NULL;
17267: }
17268: }
17269: }
17270:
1.1.1.25 root 17271: void pio_write(int c, UINT32 addr, UINT8 data)
17272: {
17273: switch(addr & 3) {
17274: case 0:
17275: pio[c].data = data;
17276: break;
17277: case 2:
1.1.1.37 root 17278: if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
17279: // strobe H -> L
17280: if(pio[c].data == 0x0d && (data & 0x02)) {
17281: // auto feed
17282: printer_out(c, 0x0d);
17283: printer_out(c, 0x0a);
17284: } else {
17285: printer_out(c, pio[c].data);
17286: }
17287: pio[c].stat &= ~0x40; // set ack
17288: }
1.1.1.25 root 17289: pio[c].ctrl = data;
17290: break;
17291: }
17292: }
17293:
17294: UINT8 pio_read(int c, UINT32 addr)
17295: {
17296: switch(addr & 3) {
17297: case 0:
1.1.1.37 root 17298: if(pio[c].ctrl & 0x20) {
17299: // input mode
17300: return(0xff);
17301: }
1.1.1.25 root 17302: return(pio[c].data);
17303: case 1:
1.1.1.37 root 17304: {
17305: UINT8 stat = pio[c].stat;
17306: pio[c].stat |= 0x40; // clear ack
17307: return(stat);
17308: }
1.1.1.25 root 17309: case 2:
17310: return(pio[c].ctrl);
17311: }
17312: return(0xff);
17313: }
17314:
1.1.1.37 root 17315: void printer_out(int c, UINT8 data)
17316: {
17317: SYSTEMTIME time;
1.1.1.38 root 17318: bool jis_mode = false;
1.1.1.37 root 17319:
17320: GetLocalTime(&time);
17321:
17322: if(pio[c].fp != NULL) {
17323: // if at least 1000ms passed from last written, close the current file
17324: FILETIME ftime1;
17325: FILETIME ftime2;
17326: SystemTimeToFileTime(&pio[c].time, &ftime1);
17327: SystemTimeToFileTime(&time, &ftime2);
17328: INT64 *time1 = (INT64 *)&ftime1;
17329: INT64 *time2 = (INT64 *)&ftime2;
17330: INT64 msec = (*time2 - *time1) / 10000;
17331:
17332: if(msec >= 1000) {
1.1.1.38 root 17333: if(pio[c].jis_mode) {
17334: fputc(0x1c, pio[c].fp);
17335: fputc(0x2e, pio[c].fp);
17336: jis_mode = true;
17337: }
1.1.1.37 root 17338: fclose(pio[c].fp);
17339: pio[c].fp = NULL;
17340: }
17341: }
17342: if(pio[c].fp == NULL) {
17343: // create a new file in the temp folder
17344: char file_name[MAX_PATH];
17345:
17346: 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);
17347: if(GetTempPath(MAX_PATH, pio[c].path)) {
17348: strcat(pio[c].path, file_name);
17349: } else {
17350: strcpy(pio[c].path, file_name);
17351: }
1.1.1.38 root 17352: pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37 root 17353: }
17354: if(pio[c].fp != NULL) {
1.1.1.38 root 17355: if(jis_mode) {
17356: fputc(0x1c, pio[c].fp);
17357: fputc(0x26, pio[c].fp);
17358: }
1.1.1.37 root 17359: fputc(data, pio[c].fp);
1.1.1.38 root 17360:
17361: // reopen file if 1ch 26h 1ch 2eh (kanji-on kanji-off) are written at the top
17362: if(data == 0x2e && ftell(pio[c].fp) == 4) {
17363: UINT8 buffer[4];
17364: fseek(pio[c].fp, 0, SEEK_SET);
17365: fread(buffer, 4, 1, pio[c].fp);
17366: if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
17367: fclose(pio[c].fp);
17368: pio[c].fp = fopen(pio[c].path, "w+b");
17369: }
17370: }
1.1.1.37 root 17371: pio[c].time = time;
17372: }
17373: }
17374:
1.1 root 17375: // pit
17376:
1.1.1.22 root 17377: #define PIT_FREQ 1193182ULL
1.1 root 17378: #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)
17379:
17380: void pit_init()
17381: {
1.1.1.8 root 17382: memset(pit, 0, sizeof(pit));
1.1 root 17383: for(int ch = 0; ch < 3; ch++) {
17384: pit[ch].count = 0x10000;
17385: pit[ch].ctrl_reg = 0x34;
17386: pit[ch].mode = 3;
17387: }
17388:
17389: // from bochs bios
17390: pit_write(3, 0x34);
17391: pit_write(0, 0x00);
17392: pit_write(0, 0x00);
17393: }
17394:
17395: void pit_write(int ch, UINT8 val)
17396: {
1.1.1.8 root 17397: #ifndef PIT_ALWAYS_RUNNING
1.1 root 17398: if(!pit_active) {
17399: pit_active = 1;
17400: pit_init();
17401: }
1.1.1.8 root 17402: #endif
1.1 root 17403: switch(ch) {
17404: case 0:
17405: case 1:
17406: case 2:
17407: // write count register
17408: if(!pit[ch].low_write && !pit[ch].high_write) {
17409: if(pit[ch].ctrl_reg & 0x10) {
17410: pit[ch].low_write = 1;
17411: }
17412: if(pit[ch].ctrl_reg & 0x20) {
17413: pit[ch].high_write = 1;
17414: }
17415: }
17416: if(pit[ch].low_write) {
17417: pit[ch].count_reg = val;
17418: pit[ch].low_write = 0;
17419: } else if(pit[ch].high_write) {
17420: if((pit[ch].ctrl_reg & 0x30) == 0x20) {
17421: pit[ch].count_reg = val << 8;
17422: } else {
17423: pit[ch].count_reg |= val << 8;
17424: }
17425: pit[ch].high_write = 0;
17426: }
17427: // start count
1.1.1.8 root 17428: if(!pit[ch].low_write && !pit[ch].high_write) {
17429: if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
17430: pit[ch].count = PIT_COUNT_VALUE(ch);
17431: pit[ch].prev_time = timeGetTime();
17432: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 17433: }
17434: }
17435: break;
17436: case 3: // ctrl reg
17437: if((val & 0xc0) == 0xc0) {
17438: // i8254 read-back command
17439: for(ch = 0; ch < 3; ch++) {
17440: if(!(val & 0x10) && !pit[ch].status_latched) {
17441: pit[ch].status = pit[ch].ctrl_reg & 0x3f;
17442: pit[ch].status_latched = 1;
17443: }
17444: if(!(val & 0x20) && !pit[ch].count_latched) {
17445: pit_latch_count(ch);
17446: }
17447: }
17448: break;
17449: }
17450: ch = (val >> 6) & 3;
17451: if(val & 0x30) {
1.1.1.35 root 17452: static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1 root 17453: pit[ch].mode = modes[(val >> 1) & 7];
17454: pit[ch].count_latched = 0;
17455: pit[ch].low_read = pit[ch].high_read = 0;
17456: pit[ch].low_write = pit[ch].high_write = 0;
17457: pit[ch].ctrl_reg = val;
17458: // stop count
1.1.1.8 root 17459: pit[ch].prev_time = pit[ch].expired_time = 0;
1.1 root 17460: pit[ch].count_reg = 0;
17461: } else if(!pit[ch].count_latched) {
17462: pit_latch_count(ch);
17463: }
17464: break;
17465: }
17466: }
17467:
17468: UINT8 pit_read(int ch)
17469: {
1.1.1.8 root 17470: #ifndef PIT_ALWAYS_RUNNING
1.1 root 17471: if(!pit_active) {
17472: pit_active = 1;
17473: pit_init();
17474: }
1.1.1.8 root 17475: #endif
1.1 root 17476: switch(ch) {
17477: case 0:
17478: case 1:
17479: case 2:
17480: if(pit[ch].status_latched) {
17481: pit[ch].status_latched = 0;
17482: return(pit[ch].status);
17483: }
17484: // if not latched, through current count
17485: if(!pit[ch].count_latched) {
17486: if(!pit[ch].low_read && !pit[ch].high_read) {
17487: pit_latch_count(ch);
17488: }
17489: }
17490: // return latched count
17491: if(pit[ch].low_read) {
17492: pit[ch].low_read = 0;
17493: if(!pit[ch].high_read) {
17494: pit[ch].count_latched = 0;
17495: }
17496: return(pit[ch].latch & 0xff);
17497: } else if(pit[ch].high_read) {
17498: pit[ch].high_read = 0;
17499: pit[ch].count_latched = 0;
17500: return((pit[ch].latch >> 8) & 0xff);
17501: }
17502: }
17503: return(0xff);
17504: }
17505:
1.1.1.8 root 17506: int pit_run(int ch, UINT32 cur_time)
1.1 root 17507: {
1.1.1.8 root 17508: if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1 root 17509: pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8 root 17510: pit[ch].prev_time = pit[ch].expired_time;
17511: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
17512: if(cur_time >= pit[ch].expired_time) {
17513: pit[ch].prev_time = cur_time;
17514: pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1 root 17515: }
1.1.1.8 root 17516: return(1);
1.1 root 17517: }
1.1.1.8 root 17518: return(0);
1.1 root 17519: }
17520:
17521: void pit_latch_count(int ch)
17522: {
1.1.1.8 root 17523: if(pit[ch].expired_time != 0) {
1.1.1.26 root 17524: UINT32 cur_time = timeGetTime();
1.1.1.8 root 17525: pit_run(ch, cur_time);
17526: UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26 root 17527: UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
17528:
17529: if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
17530: // decrement counter in 1msec period
17531: if(pit[ch].next_latch == 0) {
17532: tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
17533: pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
17534: }
17535: if(pit[ch].latch > pit[ch].next_latch) {
17536: pit[ch].latch--;
17537: }
17538: } else {
17539: pit[ch].prev_latch = pit[ch].latch = latch;
17540: pit[ch].next_latch = 0;
17541: }
1.1.1.8 root 17542: } else {
17543: pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26 root 17544: pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1 root 17545: }
17546: pit[ch].count_latched = 1;
17547: if((pit[ch].ctrl_reg & 0x30) == 0x10) {
17548: // lower byte
17549: pit[ch].low_read = 1;
17550: pit[ch].high_read = 0;
17551: } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
17552: // upper byte
17553: pit[ch].low_read = 0;
17554: pit[ch].high_read = 1;
17555: } else {
17556: // lower -> upper
1.1.1.14 root 17557: pit[ch].low_read = pit[ch].high_read = 1;
1.1 root 17558: }
17559: }
17560:
1.1.1.8 root 17561: int pit_get_expired_time(int ch)
1.1 root 17562: {
1.1.1.22 root 17563: pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
17564: UINT64 val = pit[ch].accum >> 10;
17565: pit[ch].accum -= val << 10;
17566: return((val != 0) ? val : 1);
1.1.1.8 root 17567: }
17568:
1.1.1.25 root 17569: // sio
17570:
17571: void sio_init()
17572: {
1.1.1.26 root 17573: memset(sio, 0, sizeof(sio));
17574: memset(sio_mt, 0, sizeof(sio_mt));
17575:
1.1.1.29 root 17576: for(int c = 0; c < 4; c++) {
1.1.1.25 root 17577: sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
17578: sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
17579:
17580: sio[c].divisor.w = 12; // 115200Hz / 9600Baud
17581: sio[c].line_ctrl = 0x03; // 8bit, stop 1bit, non parity
1.1.1.26 root 17582: sio[c].modem_ctrl = 0x03; // rts=on, dtr=on
17583: sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25 root 17584: sio[c].line_stat_buf = 0x60; // send/recv buffers are empty
17585: sio[c].irq_identify = 0x01; // no pending irq
17586:
17587: InitializeCriticalSection(&sio_mt[c].csSendData);
17588: InitializeCriticalSection(&sio_mt[c].csRecvData);
17589: InitializeCriticalSection(&sio_mt[c].csLineCtrl);
17590: InitializeCriticalSection(&sio_mt[c].csLineStat);
17591: InitializeCriticalSection(&sio_mt[c].csModemCtrl);
17592: InitializeCriticalSection(&sio_mt[c].csModemStat);
17593:
1.1.1.26 root 17594: if(sio_port_number[c] != 0) {
1.1.1.25 root 17595: sio[c].channel = c;
17596: sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
17597: }
17598: }
17599: }
17600:
17601: void sio_finish()
17602: {
1.1.1.29 root 17603: for(int c = 0; c < 4; c++) {
1.1.1.25 root 17604: if(sio_mt[c].hThread != NULL) {
17605: WaitForSingleObject(sio_mt[c].hThread, INFINITE);
17606: CloseHandle(sio_mt[c].hThread);
1.1.1.28 root 17607: sio_mt[c].hThread = NULL;
1.1.1.25 root 17608: }
17609: DeleteCriticalSection(&sio_mt[c].csSendData);
17610: DeleteCriticalSection(&sio_mt[c].csRecvData);
17611: DeleteCriticalSection(&sio_mt[c].csLineCtrl);
17612: DeleteCriticalSection(&sio_mt[c].csLineStat);
17613: DeleteCriticalSection(&sio_mt[c].csModemCtrl);
17614: DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28 root 17615: }
17616: sio_release();
17617: }
17618:
17619: void sio_release()
17620: {
1.1.1.29 root 17621: for(int c = 0; c < 4; c++) {
1.1.1.28 root 17622: // sio_thread() may access the resources :-(
1.1.1.32 root 17623: bool running = (sio_mt[c].hThread != NULL);
17624:
17625: if(running) {
17626: EnterCriticalSection(&sio_mt[c].csSendData);
17627: }
17628: if(sio[c].send_buffer != NULL) {
17629: sio[c].send_buffer->release();
17630: delete sio[c].send_buffer;
17631: sio[c].send_buffer = NULL;
17632: }
17633: if(running) {
17634: LeaveCriticalSection(&sio_mt[c].csSendData);
17635: EnterCriticalSection(&sio_mt[c].csRecvData);
17636: }
17637: if(sio[c].recv_buffer != NULL) {
17638: sio[c].recv_buffer->release();
17639: delete sio[c].recv_buffer;
17640: sio[c].recv_buffer = NULL;
17641: }
17642: if(running) {
17643: LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28 root 17644: }
1.1.1.25 root 17645: }
17646: }
17647:
17648: void sio_write(int c, UINT32 addr, UINT8 data)
17649: {
17650: switch(addr & 7) {
17651: case 0:
17652: if(sio[c].selector & 0x80) {
17653: if(sio[c].divisor.b.l != data) {
17654: EnterCriticalSection(&sio_mt[c].csLineCtrl);
17655: sio[c].divisor.b.l = data;
17656: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
17657: }
17658: } else {
17659: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 17660: if(sio[c].send_buffer != NULL) {
17661: sio[c].send_buffer->write(data);
17662: }
1.1.1.25 root 17663: // transmitter holding/shift registers are not empty
17664: sio[c].line_stat_buf &= ~0x60;
17665: LeaveCriticalSection(&sio_mt[c].csSendData);
17666:
17667: if(sio[c].irq_enable & 0x02) {
17668: sio_update_irq(c);
17669: }
17670: }
17671: break;
17672: case 1:
17673: if(sio[c].selector & 0x80) {
17674: if(sio[c].divisor.b.h != data) {
17675: EnterCriticalSection(&sio_mt[c].csLineCtrl);
17676: sio[c].divisor.b.h = data;
17677: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
17678: }
17679: } else {
17680: if(sio[c].irq_enable != data) {
17681: sio[c].irq_enable = data;
17682: sio_update_irq(c);
17683: }
17684: }
17685: break;
17686: case 3:
17687: {
17688: UINT8 line_ctrl = data & 0x3f;
17689: bool set_brk = ((data & 0x40) != 0);
17690:
17691: if(sio[c].line_ctrl != line_ctrl) {
17692: EnterCriticalSection(&sio_mt[c].csLineCtrl);
17693: sio[c].line_ctrl = line_ctrl;
17694: LeaveCriticalSection(&sio_mt[c].csLineCtrl);
17695: }
17696: if(sio[c].set_brk != set_brk) {
17697: EnterCriticalSection(&sio_mt[c].csModemCtrl);
17698: sio[c].set_brk = set_brk;
17699: LeaveCriticalSection(&sio_mt[c].csModemCtrl);
17700: }
17701: }
17702: sio[c].selector = data;
17703: break;
17704: case 4:
17705: {
17706: bool set_dtr = ((data & 0x01) != 0);
17707: bool set_rts = ((data & 0x02) != 0);
17708:
17709: if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26 root 17710: // EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25 root 17711: sio[c].set_dtr = set_dtr;
17712: sio[c].set_rts = set_rts;
1.1.1.26 root 17713: // LeaveCriticalSection(&sio_mt[c].csModemCtrl);
17714:
17715: bool state_changed = false;
17716:
17717: EnterCriticalSection(&sio_mt[c].csModemStat);
17718: if(set_dtr) {
17719: sio[c].modem_stat |= 0x20; // dsr on
17720: } else {
17721: sio[c].modem_stat &= ~0x20; // dsr off
17722: }
17723: if(set_rts) {
17724: sio[c].modem_stat |= 0x10; // cts on
17725: } else {
17726: sio[c].modem_stat &= ~0x10; // cts off
17727: }
17728: if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
17729: if(!(sio[c].modem_stat & 0x02)) {
17730: if(sio[c].irq_enable & 0x08) {
17731: state_changed = true;
17732: }
17733: sio[c].modem_stat |= 0x02;
17734: }
17735: }
17736: if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
17737: if(!(sio[c].modem_stat & 0x01)) {
17738: if(sio[c].irq_enable & 0x08) {
17739: state_changed = true;
17740: }
17741: sio[c].modem_stat |= 0x01;
17742: }
17743: }
17744: LeaveCriticalSection(&sio_mt[c].csModemStat);
17745:
17746: if(state_changed) {
17747: sio_update_irq(c);
17748: }
1.1.1.25 root 17749: }
17750: }
17751: sio[c].modem_ctrl = data;
17752: break;
17753: case 7:
17754: sio[c].scratch = data;
17755: break;
17756: }
17757: }
17758:
17759: UINT8 sio_read(int c, UINT32 addr)
17760: {
17761: switch(addr & 7) {
17762: case 0:
17763: if(sio[c].selector & 0x80) {
17764: return(sio[c].divisor.b.l);
17765: } else {
17766: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 17767: UINT8 data = 0;
17768: if(sio[c].recv_buffer != NULL) {
17769: data = sio[c].recv_buffer->read();
17770: }
1.1.1.25 root 17771: // data is not ready
17772: sio[c].line_stat_buf &= ~0x01;
17773: LeaveCriticalSection(&sio_mt[c].csRecvData);
17774:
17775: if(sio[c].irq_enable & 0x01) {
17776: sio_update_irq(c);
17777: }
17778: return(data);
17779: }
17780: case 1:
17781: if(sio[c].selector & 0x80) {
17782: return(sio[c].divisor.b.h);
17783: } else {
17784: return(sio[c].irq_enable);
17785: }
17786: case 2:
17787: return(sio[c].irq_identify);
17788: case 3:
17789: return(sio[c].selector);
17790: case 4:
17791: return(sio[c].modem_ctrl);
17792: case 5:
17793: {
17794: EnterCriticalSection(&sio_mt[c].csLineStat);
17795: UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
17796: sio[c].line_stat_err = 0x00;
17797: LeaveCriticalSection(&sio_mt[c].csLineStat);
17798:
17799: bool state_changed = false;
17800:
17801: if((sio[c].line_stat_buf & 0x60) == 0x00) {
17802: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 17803: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 17804: // transmitter holding register will be empty first
17805: if(sio[c].irq_enable & 0x02) {
17806: state_changed = true;
17807: }
17808: sio[c].line_stat_buf |= 0x20;
17809: }
17810: LeaveCriticalSection(&sio_mt[c].csSendData);
17811: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
17812: // transmitter shift register will be empty later
17813: sio[c].line_stat_buf |= 0x40;
17814: }
17815: if(!(sio[c].line_stat_buf & 0x01)) {
17816: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 17817: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 17818: // data is ready
17819: if(sio[c].irq_enable & 0x01) {
17820: state_changed = true;
17821: }
17822: sio[c].line_stat_buf |= 0x01;
17823: }
17824: LeaveCriticalSection(&sio_mt[c].csRecvData);
17825: }
17826: if(state_changed) {
17827: sio_update_irq(c);
17828: }
17829: return(val);
17830: }
17831: case 6:
17832: {
17833: EnterCriticalSection(&sio_mt[c].csModemStat);
17834: UINT8 val = sio[c].modem_stat;
17835: sio[c].modem_stat &= 0xf0;
17836: sio[c].prev_modem_stat = sio[c].modem_stat;
17837: LeaveCriticalSection(&sio_mt[c].csModemStat);
17838:
17839: if(sio[c].modem_ctrl & 0x10) {
17840: // loop-back
17841: val &= 0x0f;
17842: val |= (sio[c].modem_ctrl & 0x0c) << 4;
17843: val |= (sio[c].modem_ctrl & 0x01) << 5;
17844: val |= (sio[c].modem_ctrl & 0x02) << 3;
17845: }
17846: return(val);
17847: }
17848: case 7:
17849: return(sio[c].scratch);
17850: }
17851: return(0xff);
17852: }
17853:
17854: void sio_update(int c)
17855: {
17856: if((sio[c].line_stat_buf & 0x60) == 0x00) {
17857: EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32 root 17858: if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25 root 17859: // transmitter holding/shift registers will be empty
17860: sio[c].line_stat_buf |= 0x60;
17861: }
17862: LeaveCriticalSection(&sio_mt[c].csSendData);
17863: } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
17864: // transmitter shift register will be empty
17865: sio[c].line_stat_buf |= 0x40;
17866: }
17867: if(!(sio[c].line_stat_buf & 0x01)) {
17868: EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32 root 17869: if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25 root 17870: // data is ready
17871: sio[c].line_stat_buf |= 0x01;
17872: }
17873: LeaveCriticalSection(&sio_mt[c].csRecvData);
17874: }
17875: sio_update_irq(c);
17876: }
17877:
17878: void sio_update_irq(int c)
17879: {
17880: int level = -1;
17881:
17882: if(sio[c].irq_enable & 0x08) {
17883: EnterCriticalSection(&sio_mt[c].csModemStat);
17884: if((sio[c].modem_stat & 0x0f) != 0) {
17885: level = 0;
17886: }
17887: EnterCriticalSection(&sio_mt[c].csModemStat);
17888: }
17889: if(sio[c].irq_enable & 0x02) {
17890: if(sio[c].line_stat_buf & 0x20) {
17891: level = 1;
17892: }
17893: }
17894: if(sio[c].irq_enable & 0x01) {
17895: if(sio[c].line_stat_buf & 0x01) {
17896: level = 2;
17897: }
17898: }
17899: if(sio[c].irq_enable & 0x04) {
17900: EnterCriticalSection(&sio_mt[c].csLineStat);
17901: if(sio[c].line_stat_err != 0) {
17902: level = 3;
17903: }
17904: LeaveCriticalSection(&sio_mt[c].csLineStat);
17905: }
1.1.1.29 root 17906:
17907: // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25 root 17908: if(level != -1) {
17909: sio[c].irq_identify = level << 1;
1.1.1.29 root 17910: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25 root 17911: } else {
17912: sio[c].irq_identify = 1;
1.1.1.29 root 17913: pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25 root 17914: }
17915: }
17916:
17917: DWORD WINAPI sio_thread(void *lpx)
17918: {
17919: volatile sio_t *p = (sio_t *)lpx;
17920: sio_mt_t *q = &sio_mt[p->channel];
17921:
17922: char name[] = "COM1";
1.1.1.26 root 17923: name[3] = '0' + sio_port_number[p->channel];
17924: HANDLE hComm = NULL;
17925: COMMPROP commProp;
17926: DCB dcb;
17927: DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
17928: BYTE bytBuffer[SIO_BUFFER_SIZE];
17929:
17930: if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
17931: if(GetCommProperties(hComm, &commProp)) {
17932: dwSettableBaud = commProp.dwSettableBaud;
17933: }
1.1.1.25 root 17934: EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26 root 17935: // EscapeCommFunction(hComm, SETRTS);
17936: // EscapeCommFunction(hComm, SETDTR);
1.1.1.25 root 17937:
17938: while(!m_halted) {
17939: // setup comm port
17940: bool comm_state_changed = false;
17941:
17942: EnterCriticalSection(&q->csLineCtrl);
17943: if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
17944: p->prev_divisor = p->divisor.w;
17945: p->prev_line_ctrl = p->line_ctrl;
17946: comm_state_changed = true;
17947: }
17948: LeaveCriticalSection(&q->csLineCtrl);
17949:
17950: if(comm_state_changed) {
1.1.1.26 root 17951: if(GetCommState(hComm, &dcb)) {
17952: // dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
17953: DWORD baud = 115200 / p->prev_divisor;
17954: dcb.BaudRate = 9600; // default
17955:
17956: if((dwSettableBaud & BAUD_075 ) && baud >= 75 ) dcb.BaudRate = 75;
17957: if((dwSettableBaud & BAUD_110 ) && baud >= 110 ) dcb.BaudRate = 110;
17958: // 134.5bps is not supported ???
17959: // if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
17960: if((dwSettableBaud & BAUD_150 ) && baud >= 150 ) dcb.BaudRate = 150;
17961: if((dwSettableBaud & BAUD_300 ) && baud >= 300 ) dcb.BaudRate = 300;
17962: if((dwSettableBaud & BAUD_600 ) && baud >= 600 ) dcb.BaudRate = 600;
17963: if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
17964: if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
17965: if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
17966: if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
17967: if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
17968: if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
17969: // if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
17970: // if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
17971: // if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
17972:
17973: switch(p->prev_line_ctrl & 0x03) {
17974: case 0x00: dcb.ByteSize = 5; break;
17975: case 0x01: dcb.ByteSize = 6; break;
17976: case 0x02: dcb.ByteSize = 7; break;
17977: case 0x03: dcb.ByteSize = 8; break;
17978: }
17979: switch(p->prev_line_ctrl & 0x04) {
17980: case 0x00: dcb.StopBits = ONESTOPBIT; break;
17981: case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
17982: }
17983: switch(p->prev_line_ctrl & 0x38) {
17984: case 0x08: dcb.Parity = ODDPARITY; break;
17985: case 0x18: dcb.Parity = EVENPARITY; break;
17986: case 0x28: dcb.Parity = MARKPARITY; break;
17987: case 0x38: dcb.Parity = SPACEPARITY; break;
17988: default: dcb.Parity = NOPARITY; break;
17989: }
17990: dcb.fBinary = TRUE;
17991: dcb.fParity = (dcb.Parity != NOPARITY);
17992: dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
17993: dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
17994: dcb.fDsrSensitivity = FALSE;//TRUE;
17995: dcb.fTXContinueOnXoff = TRUE;
17996: dcb.fOutX = dcb.fInX = FALSE;
17997: dcb.fErrorChar = FALSE;
17998: dcb.fNull = FALSE;
17999: dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
18000: dcb.fAbortOnError = FALSE;
18001:
18002: SetCommState(hComm, &dcb);
1.1.1.25 root 18003: }
18004:
18005: // check again to apply all comm state changes
18006: Sleep(10);
18007: continue;
18008: }
18009:
18010: // set comm pins
18011: bool change_brk = false;
1.1.1.26 root 18012: // bool change_rts = false;
18013: // bool change_dtr = false;
1.1.1.25 root 18014:
18015: EnterCriticalSection(&q->csModemCtrl);
18016: if(p->prev_set_brk != p->set_brk) {
18017: p->prev_set_brk = p->set_brk;
18018: change_brk = true;
18019: }
1.1.1.26 root 18020: // if(p->prev_set_rts != p->set_rts) {
18021: // p->prev_set_rts = p->set_rts;
18022: // change_rts = true;
18023: // }
18024: // if(p->prev_set_dtr != p->set_dtr) {
18025: // p->prev_set_dtr = p->set_dtr;
18026: // change_dtr = true;
18027: // }
1.1.1.25 root 18028: LeaveCriticalSection(&q->csModemCtrl);
18029:
18030: if(change_brk) {
1.1.1.26 root 18031: static UINT32 clear_time = 0;
18032: if(p->prev_set_brk) {
18033: EscapeCommFunction(hComm, SETBREAK);
18034: clear_time = timeGetTime() + 200;
18035: } else {
18036: // keep break for at least 200msec
18037: UINT32 cur_time = timeGetTime();
18038: if(clear_time > cur_time) {
18039: Sleep(clear_time - cur_time);
18040: }
18041: EscapeCommFunction(hComm, CLRBREAK);
18042: }
1.1.1.25 root 18043: }
1.1.1.26 root 18044: // if(change_rts) {
18045: // if(p->prev_set_rts) {
18046: // EscapeCommFunction(hComm, SETRTS);
18047: // } else {
18048: // EscapeCommFunction(hComm, CLRRTS);
18049: // }
18050: // }
18051: // if(change_dtr) {
18052: // if(p->prev_set_dtr) {
18053: // EscapeCommFunction(hComm, SETDTR);
18054: // } else {
18055: // EscapeCommFunction(hComm, CLRDTR);
18056: // }
18057: // }
1.1.1.25 root 18058:
18059: // get comm pins
18060: DWORD dwModemStat = 0;
18061:
18062: if(GetCommModemStatus(hComm, &dwModemStat)) {
18063: EnterCriticalSection(&q->csModemStat);
18064: if(dwModemStat & MS_RLSD_ON) {
18065: p->modem_stat |= 0x80;
18066: } else {
18067: p->modem_stat &= ~0x80;
18068: }
18069: if(dwModemStat & MS_RING_ON) {
18070: p->modem_stat |= 0x40;
18071: } else {
18072: p->modem_stat &= ~0x40;
18073: }
1.1.1.26 root 18074: // if(dwModemStat & MS_DSR_ON) {
18075: // p->modem_stat |= 0x20;
18076: // } else {
18077: // p->modem_stat &= ~0x20;
18078: // }
18079: // if(dwModemStat & MS_CTS_ON) {
18080: // p->modem_stat |= 0x10;
18081: // } else {
18082: // p->modem_stat &= ~0x10;
18083: // }
1.1.1.25 root 18084: if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
18085: p->modem_stat |= 0x08;
18086: }
18087: if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
18088: p->modem_stat |= 0x04;
18089: }
1.1.1.26 root 18090: // if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
18091: // p->modem_stat |= 0x02;
18092: // }
18093: // if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
18094: // p->modem_stat |= 0x01;
18095: // }
1.1.1.25 root 18096: LeaveCriticalSection(&q->csModemStat);
18097: }
18098:
18099: // send data
18100: DWORD dwSend = 0;
18101:
18102: EnterCriticalSection(&q->csSendData);
1.1.1.32 root 18103: while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25 root 18104: bytBuffer[dwSend++] = p->send_buffer->read();
18105: }
18106: LeaveCriticalSection(&q->csSendData);
18107:
18108: if(dwSend != 0) {
18109: DWORD dwWritten = 0;
18110: WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
18111: }
18112:
18113: // get line status and recv data
18114: DWORD dwLineStat = 0;
18115: COMSTAT comStat;
18116:
18117: if(ClearCommError(hComm, &dwLineStat, &comStat)) {
18118: EnterCriticalSection(&q->csLineStat);
18119: if(dwLineStat & CE_BREAK) {
18120: p->line_stat_err |= 0x10;
18121: }
18122: if(dwLineStat & CE_FRAME) {
18123: p->line_stat_err |= 0x08;
18124: }
18125: if(dwLineStat & CE_RXPARITY) {
18126: p->line_stat_err |= 0x04;
18127: }
18128: if(dwLineStat & CE_OVERRUN) {
18129: p->line_stat_err |= 0x02;
18130: }
18131: LeaveCriticalSection(&q->csLineStat);
18132:
18133: if(comStat.cbInQue != 0) {
18134: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 18135: DWORD dwRecv = 0;
18136: if(p->recv_buffer != NULL) {
18137: dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
18138: }
1.1.1.25 root 18139: LeaveCriticalSection(&q->csRecvData);
18140:
18141: if(dwRecv != 0) {
18142: DWORD dwRead = 0;
18143: if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
18144: EnterCriticalSection(&q->csRecvData);
1.1.1.32 root 18145: if(p->recv_buffer != NULL) {
18146: for(int i = 0; i < dwRead; i++) {
18147: p->recv_buffer->write(bytBuffer[i]);
18148: }
1.1.1.25 root 18149: }
18150: LeaveCriticalSection(&q->csRecvData);
18151: }
18152: }
18153: }
18154: }
18155: Sleep(10);
18156: }
18157: CloseHandle(hComm);
18158: }
18159: return 0;
18160: }
18161:
1.1.1.8 root 18162: // cmos
18163:
18164: void cmos_init()
18165: {
18166: memset(cmos, 0, sizeof(cmos));
18167: cmos_addr = 0;
1.1 root 18168:
1.1.1.8 root 18169: // from DOSBox
18170: cmos_write(0x0a, 0x26);
18171: cmos_write(0x0b, 0x02);
18172: cmos_write(0x0d, 0x80);
1.1 root 18173: }
18174:
1.1.1.8 root 18175: void cmos_write(int addr, UINT8 val)
1.1 root 18176: {
1.1.1.8 root 18177: cmos[addr & 0x7f] = val;
18178: }
18179:
18180: #define CMOS_GET_TIME() { \
18181: UINT32 cur_sec = timeGetTime() / 1000 ; \
18182: if(prev_sec != cur_sec) { \
18183: GetLocalTime(&time); \
18184: prev_sec = cur_sec; \
18185: } \
1.1 root 18186: }
1.1.1.8 root 18187: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1 root 18188:
1.1.1.8 root 18189: UINT8 cmos_read(int addr)
1.1 root 18190: {
1.1.1.8 root 18191: static SYSTEMTIME time;
18192: static UINT32 prev_sec = 0;
1.1 root 18193:
1.1.1.8 root 18194: switch(addr & 0x7f) {
18195: case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
18196: case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
18197: case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
18198: case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
18199: case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
18200: case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
18201: case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
18202: // case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0)); // 2msec
18203: case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0)); // precision of timeGetTime() may not be 1msec
18204: case 0x15: return((MEMORY_END >> 10) & 0xff);
18205: case 0x16: return((MEMORY_END >> 18) & 0xff);
18206: case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
18207: case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
18208: case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
18209: case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
18210: case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1 root 18211: }
1.1.1.8 root 18212: return(cmos[addr & 0x7f]);
1.1 root 18213: }
18214:
1.1.1.7 root 18215: // kbd (a20)
18216:
18217: void kbd_init()
18218: {
1.1.1.8 root 18219: kbd_data = kbd_command = 0;
1.1.1.7 root 18220: kbd_status = 0x18;
18221: }
18222:
18223: UINT8 kbd_read_data()
18224: {
1.1.1.8 root 18225: kbd_status &= ~1;
1.1.1.7 root 18226: return(kbd_data);
18227: }
18228:
18229: void kbd_write_data(UINT8 val)
18230: {
18231: switch(kbd_command) {
18232: case 0xd1:
18233: i386_set_a20_line((val >> 1) & 1);
18234: break;
18235: }
18236: kbd_command = 0;
1.1.1.8 root 18237: kbd_status &= ~8;
1.1.1.7 root 18238: }
18239:
18240: UINT8 kbd_read_status()
18241: {
18242: return(kbd_status);
18243: }
18244:
18245: void kbd_write_command(UINT8 val)
18246: {
18247: switch(val) {
18248: case 0xd0:
18249: kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8 root 18250: kbd_status |= 1;
1.1.1.7 root 18251: break;
18252: case 0xdd:
18253: i386_set_a20_line(0);
18254: break;
18255: case 0xdf:
18256: i386_set_a20_line(1);
18257: break;
1.1.1.26 root 18258: case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
18259: case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7 root 18260: if(!(val & 1)) {
1.1.1.8 root 18261: if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7 root 18262: // reset pic
18263: pic_init();
18264: pic[0].irr = pic[1].irr = 0x00;
18265: pic[0].imr = pic[1].imr = 0xff;
18266: }
18267: CPU_RESET_CALL(CPU_MODEL);
1.1.1.40 root 18268: UINT16 address = *(UINT16 *)(mem + 0x467);
18269: UINT16 selector = *(UINT16 *)(mem + 0x469);
18270: i386_jmp_far(selector, address);
1.1.1.7 root 18271: }
18272: i386_set_a20_line((val >> 1) & 1);
18273: break;
18274: }
18275: kbd_command = val;
1.1.1.8 root 18276: kbd_status |= 8;
1.1.1.7 root 18277: }
18278:
1.1.1.9 root 18279: // vga
18280:
18281: UINT8 vga_read_status()
18282: {
18283: // 60hz
18284: static const int period[3] = {16, 17, 17};
18285: static int index = 0;
18286: UINT32 time = timeGetTime() % period[index];
18287:
18288: index = (index + 1) % 3;
1.1.1.14 root 18289: return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9 root 18290: }
18291:
1.1 root 18292: // i/o bus
18293:
1.1.1.29 root 18294: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
18295: //#define SW1US_PATCH
18296:
1.1.1.25 root 18297: UINT8 read_io_byte(offs_t addr)
1.1.1.33 root 18298: #ifdef USE_DEBUGGER
1.1.1.25 root 18299: {
1.1.1.33 root 18300: if(now_debugging) {
18301: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
18302: if(in_break_point.table[i].status == 1) {
18303: if(addr == in_break_point.table[i].addr) {
18304: in_break_point.hit = i + 1;
18305: now_suspended = true;
18306: break;
18307: }
18308: }
18309: }
1.1.1.25 root 18310: }
1.1.1.33 root 18311: return(debugger_read_io_byte(addr));
1.1.1.25 root 18312: }
1.1.1.33 root 18313: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25 root 18314: #endif
1.1 root 18315: {
1.1.1.33 root 18316: UINT8 val = 0xff;
18317:
1.1 root 18318: switch(addr) {
1.1.1.29 root 18319: #ifdef SW1US_PATCH
18320: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
18321: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33 root 18322: val = sio_read(0, addr - 1);
18323: break;
1.1.1.29 root 18324: #else
1.1.1.25 root 18325: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
18326: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33 root 18327: val = dma_read(0, addr);
18328: break;
1.1.1.29 root 18329: #endif
1.1.1.25 root 18330: case 0x20: case 0x21:
1.1.1.33 root 18331: val = pic_read(0, addr);
18332: break;
1.1.1.25 root 18333: case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33 root 18334: val = pit_read(addr & 0x03);
18335: break;
1.1.1.7 root 18336: case 0x60:
1.1.1.33 root 18337: val = kbd_read_data();
18338: break;
1.1.1.9 root 18339: case 0x61:
1.1.1.33 root 18340: val = system_port;
18341: break;
1.1.1.7 root 18342: case 0x64:
1.1.1.33 root 18343: val = kbd_read_status();
18344: break;
1.1 root 18345: case 0x71:
1.1.1.33 root 18346: val = cmos_read(cmos_addr);
18347: break;
1.1.1.25 root 18348: case 0x81:
1.1.1.33 root 18349: val = dma_page_read(0, 2);
18350: break;
1.1.1.25 root 18351: case 0x82:
1.1.1.33 root 18352: val = dma_page_read(0, 3);
18353: break;
1.1.1.25 root 18354: case 0x83:
1.1.1.33 root 18355: val = dma_page_read(0, 1);
18356: break;
1.1.1.25 root 18357: case 0x87:
1.1.1.33 root 18358: val = dma_page_read(0, 0);
18359: break;
1.1.1.25 root 18360: case 0x89:
1.1.1.33 root 18361: val = dma_page_read(1, 2);
18362: break;
1.1.1.25 root 18363: case 0x8a:
1.1.1.33 root 18364: val = dma_page_read(1, 3);
18365: break;
1.1.1.25 root 18366: case 0x8b:
1.1.1.33 root 18367: val = dma_page_read(1, 1);
18368: break;
1.1.1.25 root 18369: case 0x8f:
1.1.1.33 root 18370: val = dma_page_read(1, 0);
18371: break;
1.1 root 18372: case 0x92:
1.1.1.33 root 18373: val = (m_a20_mask >> 19) & 2;
18374: break;
1.1.1.25 root 18375: case 0xa0: case 0xa1:
1.1.1.33 root 18376: val = pic_read(1, addr);
18377: break;
1.1.1.25 root 18378: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
18379: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33 root 18380: val = dma_read(1, (addr - 0xc0) >> 1);
18381: break;
1.1.1.37 root 18382: case 0x278: case 0x279: case 0x27a:
18383: val = pio_read(1, addr);
18384: break;
1.1.1.29 root 18385: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33 root 18386: val = sio_read(3, addr);
18387: break;
1.1.1.25 root 18388: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33 root 18389: val = sio_read(1, addr);
18390: break;
1.1.1.25 root 18391: case 0x378: case 0x379: case 0x37a:
1.1.1.33 root 18392: val = pio_read(0, addr);
18393: break;
1.1.1.25 root 18394: case 0x3ba: case 0x3da:
1.1.1.33 root 18395: val = vga_read_status();
18396: break;
1.1.1.37 root 18397: case 0x3bc: case 0x3bd: case 0x3be:
18398: val = pio_read(2, addr);
18399: break;
1.1.1.29 root 18400: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33 root 18401: val = sio_read(2, addr);
18402: break;
1.1.1.25 root 18403: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33 root 18404: val = sio_read(0, addr);
18405: break;
1.1 root 18406: default:
1.1.1.33 root 18407: // fatalerror("unknown inb %4x\n", addr);
1.1 root 18408: break;
18409: }
1.1.1.33 root 18410: #ifdef ENABLE_DEBUG_IOPORT
18411: if(fp_debug_log != NULL) {
18412: fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
18413: }
18414: #endif
18415: return(val);
1.1 root 18416: }
18417:
18418: UINT16 read_io_word(offs_t addr)
18419: {
18420: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
18421: }
18422:
1.1.1.33 root 18423: #ifdef USE_DEBUGGER
18424: UINT16 debugger_read_io_word(offs_t addr)
18425: {
18426: return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
18427: }
18428: #endif
18429:
1.1 root 18430: UINT32 read_io_dword(offs_t addr)
18431: {
18432: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
18433: }
18434:
1.1.1.33 root 18435: #ifdef USE_DEBUGGER
18436: UINT32 debugger_read_io_dword(offs_t addr)
18437: {
18438: 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));
18439: }
18440: #endif
18441:
1.1 root 18442: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33 root 18443: #ifdef USE_DEBUGGER
18444: {
18445: if(now_debugging) {
18446: for(int i = 0; i < MAX_BREAK_POINTS; i++) {
18447: if(out_break_point.table[i].status == 1) {
18448: if(addr == out_break_point.table[i].addr) {
18449: out_break_point.hit = i + 1;
18450: now_suspended = true;
18451: break;
18452: }
18453: }
18454: }
18455: }
18456: debugger_write_io_byte(addr, val);
18457: }
18458: void debugger_write_io_byte(offs_t addr, UINT8 val)
18459: #endif
1.1 root 18460: {
1.1.1.25 root 18461: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33 root 18462: if(fp_debug_log != NULL) {
18463: fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25 root 18464: }
18465: #endif
1.1 root 18466: switch(addr) {
1.1.1.29 root 18467: #ifdef SW1US_PATCH
18468: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
18469: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
18470: sio_write(0, addr - 1, val);
18471: break;
18472: #else
1.1.1.25 root 18473: case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
18474: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
18475: dma_write(0, addr, val);
18476: break;
1.1.1.29 root 18477: #endif
1.1.1.25 root 18478: case 0x20: case 0x21:
1.1 root 18479: pic_write(0, addr, val);
18480: break;
1.1.1.25 root 18481: case 0x40: case 0x41: case 0x42: case 0x43:
1.1 root 18482: pit_write(addr & 0x03, val);
18483: break;
1.1.1.7 root 18484: case 0x60:
18485: kbd_write_data(val);
18486: break;
1.1.1.9 root 18487: case 0x61:
18488: if((system_port & 3) != 3 && (val & 3) == 3) {
18489: // beep on
18490: // MessageBeep(-1);
18491: } else if((system_port & 3) == 3 && (val & 3) != 3) {
18492: // beep off
18493: }
18494: system_port = val;
18495: break;
1.1 root 18496: case 0x64:
1.1.1.7 root 18497: kbd_write_command(val);
1.1 root 18498: break;
18499: case 0x70:
18500: cmos_addr = val;
18501: break;
18502: case 0x71:
1.1.1.8 root 18503: cmos_write(cmos_addr, val);
1.1 root 18504: break;
1.1.1.25 root 18505: case 0x81:
18506: dma_page_write(0, 2, val);
18507: case 0x82:
18508: dma_page_write(0, 3, val);
18509: case 0x83:
18510: dma_page_write(0, 1, val);
18511: case 0x87:
18512: dma_page_write(0, 0, val);
18513: case 0x89:
18514: dma_page_write(1, 2, val);
18515: case 0x8a:
18516: dma_page_write(1, 3, val);
18517: case 0x8b:
18518: dma_page_write(1, 1, val);
18519: case 0x8f:
18520: dma_page_write(1, 0, val);
1.1 root 18521: case 0x92:
1.1.1.7 root 18522: i386_set_a20_line((val >> 1) & 1);
1.1 root 18523: break;
1.1.1.25 root 18524: case 0xa0: case 0xa1:
1.1 root 18525: pic_write(1, addr, val);
18526: break;
1.1.1.25 root 18527: case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
18528: case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26 root 18529: dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25 root 18530: break;
1.1.1.35 root 18531: #ifdef USE_SERVICE_THREAD
18532: case 0xf7:
18533: // dummy i/o for BIOS/DOS service
1.1.1.36 root 18534: if(in_service && cursor_moved) {
18535: // update cursor position before service is done
18536: pcbios_update_cursor_position();
18537: cursor_moved = false;
18538: }
1.1.1.35 root 18539: finish_service_loop();
18540: break;
18541: #endif
1.1.1.37 root 18542: case 0x278: case 0x279: case 0x27a:
18543: pio_write(1, addr, val);
18544: break;
1.1.1.29 root 18545: case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
18546: sio_write(3, addr, val);
18547: break;
1.1.1.25 root 18548: case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
18549: sio_write(1, addr, val);
18550: break;
18551: case 0x378: case 0x379: case 0x37a:
18552: pio_write(0, addr, val);
18553: break;
1.1.1.37 root 18554: case 0x3bc: case 0x3bd: case 0x3be:
18555: pio_write(2, addr, val);
18556: break;
1.1.1.29 root 18557: case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
18558: sio_write(2, addr, val);
18559: break;
1.1.1.25 root 18560: case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
18561: sio_write(0, addr, val);
18562: break;
1.1 root 18563: default:
1.1.1.33 root 18564: // fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1 root 18565: break;
18566: }
18567: }
18568:
18569: void write_io_word(offs_t addr, UINT16 val)
18570: {
18571: write_io_byte(addr + 0, (val >> 0) & 0xff);
18572: write_io_byte(addr + 1, (val >> 8) & 0xff);
18573: }
18574:
1.1.1.33 root 18575: #ifdef USE_DEBUGGER
18576: void debugger_write_io_word(offs_t addr, UINT16 val)
18577: {
18578: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
18579: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
18580: }
18581: #endif
18582:
1.1 root 18583: void write_io_dword(offs_t addr, UINT32 val)
18584: {
18585: write_io_byte(addr + 0, (val >> 0) & 0xff);
18586: write_io_byte(addr + 1, (val >> 8) & 0xff);
18587: write_io_byte(addr + 2, (val >> 16) & 0xff);
18588: write_io_byte(addr + 3, (val >> 24) & 0xff);
18589: }
1.1.1.33 root 18590:
18591: #ifdef USE_DEBUGGER
18592: void debugger_write_io_dword(offs_t addr, UINT32 val)
18593: {
18594: debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
18595: debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
18596: debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
18597: debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
18598: }
18599: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.