Moved stack switch handling (large stack) to sysdeps.c
[wine.git] / msdos / ioports.c
blob49fb7a0d3149f4e44207978708e2a17904178a31
1 /*
2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
6 */
8 /* Known problems:
9 - only a few ports are emulated.
10 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
11 fix that, I guess.
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "windef.h"
21 #include "vga.h"
22 #include "dosexe.h"
23 #include "options.h"
24 #include "miscemu.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(int)
29 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
30 /* if byte_toggle is TRUE, then hi byte has already been written */
31 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
32 static WORD tmr_8253_latch[3] = {0, 0, 0};
34 /* 4th contents are dummy */
35 static BOOL16 tmr_8253_latched[4] = {FALSE, FALSE, FALSE, FALSE};
36 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
38 static int dummy_ctr = 0;
40 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
42 static BYTE cmosaddress;
44 static BYTE cmosimage[64] =
46 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
47 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
48 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
49 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
50 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
52 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
53 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
56 #if defined(linux) && defined(__i386__)
57 # define DIRECT_IO_ACCESS
58 #else
59 # undef DIRECT_IO_ACCESS
60 #endif /* linux && __i386__ */
62 #ifdef DIRECT_IO_ACCESS
64 extern int iopl(int level);
66 static char do_direct_port_access = -1;
67 static char port_permissions[0x10000];
69 #define IO_READ 1
70 #define IO_WRITE 2
72 #endif /* DIRECT_IO_ACCESS */
74 static void IO_FixCMOSCheckSum(void)
76 WORD sum = 0;
77 int i;
79 for (i=0x10; i < 0x2d; i++)
80 sum += cmosimage[i];
81 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
82 cmosimage[0x2f] = sum & 0xff;
83 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
86 static void set_timer_maxval(unsigned timer, unsigned maxval)
88 switch (timer) {
89 case 0: /* System timer counter divisor */
90 DOSVM_SetTimer(maxval);
91 break;
92 case 1: /* RAM refresh */
93 FIXME("RAM refresh counter handling not implemented !");
94 break;
95 case 2: /* cassette & speaker */
96 /* speaker on ? */
97 if (((BYTE)parport_8255[1] & 3) == 3)
99 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
100 Beep(1193180 / maxval, 20);
102 break;
106 /**********************************************************************
107 * IO_port_init
110 /* set_IO_permissions(int val1, int val)
111 * Helper function for IO_port_init
113 #ifdef DIRECT_IO_ACCESS
114 static void set_IO_permissions(int val1, int val, char rw)
116 int j;
117 if (val1 != -1) {
118 if (val == -1) val = 0x3ff;
119 for (j = val1; j <= val; j++)
120 port_permissions[j] |= rw;
122 do_direct_port_access = 1;
124 val1 = -1;
125 } else if (val != -1) {
126 do_direct_port_access = 1;
128 port_permissions[val] |= rw;
133 /* do_IO_port_init_read_or_write(char* temp, char rw)
134 * Helper function for IO_port_init
137 static void do_IO_port_init_read_or_write(char* temp, char rw)
139 int val, val1, i, len;
140 if (!strcasecmp(temp, "all")) {
141 MESSAGE("Warning!!! Granting FULL IO port access to"
142 " windoze programs!\nWarning!!! "
143 "*** THIS IS NOT AT ALL "
144 "RECOMMENDED!!! ***\n");
145 for (i=0; i < sizeof(port_permissions); i++)
146 port_permissions[i] |= rw;
148 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
149 len = strlen(temp);
150 val = -1;
151 val1 = -1;
152 for (i = 0; i < len; i++) {
153 switch (temp[i]) {
154 case '0':
155 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
156 sscanf(temp+i, "%x", &val);
157 i += 2;
158 } else {
159 sscanf(temp+i, "%d", &val);
161 while (isxdigit(temp[i]))
162 i++;
163 i--;
164 break;
165 case ',':
166 case ' ':
167 case '\t':
168 set_IO_permissions(val1, val, rw);
169 val1 = -1; val = -1;
170 break;
171 case '-':
172 val1 = val;
173 if (val1 == -1) val1 = 0;
174 break;
175 default:
176 if (temp[i] >= '0' && temp[i] <= '9') {
177 sscanf(temp+i, "%d", &val);
178 while (isdigit(temp[i]))
179 i++;
183 set_IO_permissions(val1, val, rw);
187 static inline BYTE inb( WORD port )
189 BYTE b;
190 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
191 return b;
194 static inline WORD inw( WORD port )
196 WORD w;
197 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
198 return w;
201 static inline DWORD inl( WORD port )
203 DWORD dw;
204 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
205 return dw;
208 static inline void outb( BYTE value, WORD port )
210 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
213 static inline void outw( WORD value, WORD port )
215 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
218 static inline void outl( DWORD value, WORD port )
220 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
223 #endif /* DIRECT_IO_ACCESS */
225 static void IO_port_init(void)
227 #ifdef DIRECT_IO_ACCESS
228 char temp[1024];
230 do_direct_port_access = 0;
231 /* Can we do that? */
232 if (!iopl(3)) {
233 iopl(0);
235 PROFILE_GetWineIniString( "ports", "read", "*",
236 temp, sizeof(temp) );
237 do_IO_port_init_read_or_write(temp, IO_READ);
238 PROFILE_GetWineIniString( "ports", "write", "*",
239 temp, sizeof(temp) );
240 do_IO_port_init_read_or_write(temp, IO_WRITE);
242 #endif /* DIRECT_IO_ACCESS */
243 IO_FixCMOSCheckSum();
247 /**********************************************************************
248 * IO_inport
250 * Note: The size argument has to be handled correctly _externally_
251 * (as we always return a DWORD)
253 DWORD IO_inport( int port, int size )
255 DWORD res = 0;
257 TRACE("%d-byte value from port 0x%02x\n", size, port );
259 #ifdef DIRECT_IO_ACCESS
260 if (do_direct_port_access == -1) IO_port_init();
261 if ((do_direct_port_access)
262 /* Make sure we have access to the port */
263 && (port_permissions[port] & IO_READ))
265 iopl(3);
266 switch(size)
268 case 1: res = inb( port ); break;
269 case 2: res = inw( port ); break;
270 case 4: res = inl( port ); break;
271 default:
272 ERR("invalid data size %d\n", size);
274 iopl(0);
275 return res;
277 #endif
279 switch (port)
281 case 0x3ba:
282 case 0x3da:
283 res = (DWORD)VGA_ioport_in( port );
284 break;
285 case 0x40:
286 case 0x41:
287 case 0x42:
289 BYTE chan = port & 3;
290 WORD tempval = 0;
292 if (tmr_8253_latched[chan] == TRUE)
294 tempval = tmr_8253_latch[chan];
295 tmr_8253_latched[chan] = FALSE;
297 else
299 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
300 if (chan == 0) /* System timer counter divisor */
301 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
302 tempval = dummy_ctr+(WORD)DOSVM_GetTimer();
303 else
304 { /* FIXME: intelligent hardware timer emulation needed */
305 tempval = dummy_ctr;
308 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
310 case 0x00:
311 break; /* correct ? */
312 case 0x10: /* read lo byte */
313 res = (BYTE)tempval;
314 break;
315 case 0x30: /* read lo byte, then hi byte */
316 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
317 if (tmr_8253_byte_toggle[chan] == TRUE)
319 res = (BYTE)tempval;
320 break;
322 /* else [fall through if read hi byte !] */
323 case 0x20: /* read hi byte */
324 res = (BYTE)tempval>>8;
325 break;
327 break;
329 case 0x60:
330 res = INT_Int09ReadScan();
331 #if 0 /* what's this port got to do with parport ? */
332 res = (DWORD)parport_8255[0];
333 #endif
334 break;
335 case 0x61:
336 res = (DWORD)parport_8255[1];
337 break;
338 case 0x62:
339 res = (DWORD)parport_8255[2];
340 break;
341 case 0x70:
342 res = (DWORD)cmosaddress;
343 break;
344 case 0x71:
345 res = (DWORD)cmosimage[cmosaddress & 0x3f];
346 break;
347 case 0x200:
348 case 0x201:
349 res = 0xffffffff; /* no joystick */
350 break;
351 default:
352 WARN("Direct I/O read attempted from port %x\n", port);
353 res = 0xffffffff;
354 break;
356 TRACE(" returning ( 0x%lx )\n", res );
357 return res;
361 /**********************************************************************
362 * IO_outport
364 void IO_outport( int port, int size, DWORD value )
366 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
367 value, size, port );
369 #ifdef DIRECT_IO_ACCESS
370 if (do_direct_port_access == -1) IO_port_init();
371 if ((do_direct_port_access)
372 /* Make sure we have access to the port */
373 && (port_permissions[port] & IO_WRITE))
375 iopl(3);
376 switch(size)
378 case 1: outb( LOBYTE(value), port ); break;
379 case 2: outw( LOWORD(value), port ); break;
380 case 4: outl( value, port ); break;
381 default:
382 WARN("Invalid data size %d\n", size);
384 iopl(0);
385 return;
387 #endif
389 switch (port)
391 case 0x3c8:
392 case 0x3c9:
393 VGA_ioport_out( port, (BYTE)value );
394 break;
395 case 0x20:
396 DOSVM_PIC_ioport_out( port, (BYTE)value );
397 break;
398 case 0x40:
399 case 0x41:
400 case 0x42:
402 BYTE chan = port & 3;
403 WORD oldval = 0;
405 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
406 /* we need to get the oldval before any lo/hi byte change has been made */
407 (tmr_8253_byte_toggle[chan] == FALSE) )
408 oldval = tmr_8253_countmax[chan];
409 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
411 case 0x00:
412 break; /* correct ? */
413 case 0x10: /* write lo byte */
414 tmr_8253_countmax[chan] =
415 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
416 break;
417 case 0x30: /* write lo byte, then hi byte */
418 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
419 if (tmr_8253_byte_toggle[chan] == TRUE)
421 tmr_8253_countmax[chan] =
422 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
423 break;
425 /* else [fall through if write hi byte !] */
426 case 0x20: /* write hi byte */
427 tmr_8253_countmax[chan] =
428 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
429 break;
431 if (
432 /* programming finished ? */
433 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
434 (tmr_8253_byte_toggle[chan] == FALSE) )
435 /* update to new value ? */
436 && (tmr_8253_countmax[chan] != oldval)
438 set_timer_maxval(chan, tmr_8253_countmax[chan]);
440 break;
441 case 0x43:
443 BYTE chan = ((BYTE)value & 0xc0) >> 6;
445 /* ctrl byte for specific timer channel */
446 tmr_8253_ctrlbyte_ch[chan] = (BYTE)value;
447 if (chan == 3) {
448 FIXME("8254 timer readback not implemented yet\n");
450 else
451 if (((BYTE)value&0x30)==0) { /* latch timer */
452 tmr_8253_latched[chan] = TRUE;
453 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
454 if (chan == 0) /* System timer divisor */
455 tmr_8253_latch[0] = dummy_ctr+(WORD)DOSVM_GetTimer();
456 else
457 { /* FIXME: intelligent hardware timer emulation needed */
458 tmr_8253_latch[chan] = dummy_ctr;
461 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
462 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
464 break;
465 case 0x61:
466 parport_8255[1] = (BYTE)value;
467 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
469 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
470 Beep(1193180 / tmr_8253_countmax[2], 20);
472 break;
473 case 0x70:
474 cmosaddress = (BYTE)value & 0x7f;
475 break;
476 case 0x71:
477 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
478 break;
479 default:
480 WARN("Direct I/O write attempted to port %x\n", port );
481 break;