- Add a max wait time for selection notifies.
[wine.git] / msdos / ioports.c
blobf8e3b7c2c8ee1479b3e764dd15cfc58ce9983443
1 /*
2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* Known problems:
23 - only a few ports are emulated.
24 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
25 fix that, I guess.
28 #include "config.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include "windef.h"
39 #include "winnls.h"
40 #include "callback.h"
41 #include "file.h"
42 #include "miscemu.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(int);
47 static struct {
48 WORD countmax;
49 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
50 WORD latch;
51 BOOL16 latched;
52 BYTE ctrlbyte_ch;
53 WORD oldval;
54 } tmr_8253[3] = {
55 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
56 {0x0012, FALSE, 0, FALSE, 0x74, 0},
57 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
60 static int dummy_ctr = 0;
62 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
64 static BYTE cmosaddress;
66 static BYTE cmosimage[64] =
68 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
69 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
70 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
71 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
72 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
74 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
75 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
78 #if defined(linux) && defined(__i386__)
79 # define DIRECT_IO_ACCESS
80 #else
81 # undef DIRECT_IO_ACCESS
82 # undef PP_IO_ACCESS
83 #endif /* linux && __i386__ */
85 #ifdef HAVE_PPDEV
86 static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available
87 0: available);*/
88 #endif
90 #ifdef DIRECT_IO_ACCESS
92 extern int iopl(int level);
93 static char do_direct_port_access = -1;
94 static char port_permissions[0x10000];
96 #define IO_READ 1
97 #define IO_WRITE 2
99 static void IO_FixCMOSCheckSum(void)
101 WORD sum = 0;
102 int i;
104 for (i=0x10; i < 0x2d; i++)
105 sum += cmosimage[i];
106 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
107 cmosimage[0x2f] = sum & 0xff;
108 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
111 #endif /* DIRECT_IO_ACCESS */
113 static void set_timer_maxval(unsigned timer, unsigned maxval)
115 switch (timer) {
116 case 0: /* System timer counter divisor */
117 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
118 break;
119 case 1: /* RAM refresh */
120 FIXME("RAM refresh counter handling not implemented !\n");
121 break;
122 case 2: /* cassette & speaker */
123 /* speaker on ? */
124 if (((BYTE)parport_8255[1] & 3) == 3)
126 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
127 Beep(1193180 / maxval, 20);
129 break;
133 /**********************************************************************
134 * IO_port_init
137 /* set_IO_permissions(int val1, int val)
138 * Helper function for IO_port_init
140 #ifdef DIRECT_IO_ACCESS
141 static void set_IO_permissions(int val1, int val, char rw)
143 int j;
144 if (val1 != -1) {
145 if (val == -1) val = 0x3ff;
146 for (j = val1; j <= val; j++)
147 port_permissions[j] |= rw;
149 do_direct_port_access = 1;
151 val1 = -1;
152 } else if (val != -1) {
153 do_direct_port_access = 1;
155 port_permissions[val] |= rw;
160 /* do_IO_port_init_read_or_write(char* temp, char rw)
161 * Helper function for IO_port_init
164 static void do_IO_port_init_read_or_write(char* temp, char rw)
166 int val, val1, i, len;
167 if (!strcasecmp(temp, "all")) {
168 MESSAGE("Warning!!! Granting FULL IO port access to"
169 " windoze programs!\nWarning!!! "
170 "*** THIS IS NOT AT ALL "
171 "RECOMMENDED!!! ***\n");
172 for (i=0; i < sizeof(port_permissions); i++)
173 port_permissions[i] |= rw;
175 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
176 len = strlen(temp);
177 val = -1;
178 val1 = -1;
179 for (i = 0; i < len; i++) {
180 switch (temp[i]) {
181 case '0':
182 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
183 sscanf(temp+i, "%x", &val);
184 i += 2;
185 } else {
186 sscanf(temp+i, "%d", &val);
188 while (isxdigit(temp[i]))
189 i++;
190 i--;
191 break;
192 case ',':
193 case ' ':
194 case '\t':
195 set_IO_permissions(val1, val, rw);
196 val1 = -1; val = -1;
197 break;
198 case '-':
199 val1 = val;
200 if (val1 == -1) val1 = 0;
201 break;
202 default:
203 if (temp[i] >= '0' && temp[i] <= '9') {
204 sscanf(temp+i, "%d", &val);
205 while (isdigit(temp[i]))
206 i++;
210 set_IO_permissions(val1, val, rw);
214 static inline BYTE inb( WORD port )
216 BYTE b;
217 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
218 return b;
221 static inline WORD inw( WORD port )
223 WORD w;
224 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
225 return w;
228 static inline DWORD inl( WORD port )
230 DWORD dw;
231 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
232 return dw;
235 static inline void outb( BYTE value, WORD port )
237 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
240 static inline void outw( WORD value, WORD port )
242 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
245 static inline void outl( DWORD value, WORD port )
247 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
250 static void IO_port_init(void)
252 char temp[1024];
253 WCHAR tempW[1024];
254 static const WCHAR portsW[] = {'p','o','r','t','s',0};
255 static const WCHAR readW[] = {'r','e','a','d',0};
256 static const WCHAR writeW[] = {'w','r','i','t','e',0};
257 static const WCHAR asteriskW[] = {'*',0};
259 do_direct_port_access = 0;
260 /* Can we do that? */
261 if (!iopl(3)) {
262 iopl(0);
264 PROFILE_GetWineIniString( portsW, readW, asteriskW, tempW, 1024 );
265 WideCharToMultiByte(CP_ACP, 0, tempW, -1, temp, 1024, NULL, NULL);
266 do_IO_port_init_read_or_write(temp, IO_READ);
267 PROFILE_GetWineIniString( portsW, writeW, asteriskW, tempW, 1024 );
268 WideCharToMultiByte(CP_ACP, 0, tempW, -1, temp, 1024, NULL, NULL);
269 do_IO_port_init_read_or_write(temp, IO_WRITE);
271 IO_FixCMOSCheckSum();
274 #endif /* DIRECT_IO_ACCESS */
276 /**********************************************************************
277 * IO_inport
279 * Note: The size argument has to be handled correctly _externally_
280 * (as we always return a DWORD)
282 DWORD IO_inport( int port, int size )
284 DWORD res = 0;
286 TRACE("%d-byte value from port 0x%02x\n", size, port );
288 #ifdef HAVE_PPDEV
289 if (do_pp_port_access == -1)
290 do_pp_port_access =IO_pp_init();
291 if ((do_pp_port_access == 0 ) && (size == 1))
292 if (!IO_pp_inp(port,&res))
293 return res;
294 #endif
295 #ifdef DIRECT_IO_ACCESS
296 if (do_direct_port_access == -1) IO_port_init();
297 if ((do_direct_port_access)
298 /* Make sure we have access to the port */
299 && (port_permissions[port] & IO_READ))
301 iopl(3);
302 switch(size)
304 case 1: res = inb( port ); break;
305 case 2: res = inw( port ); break;
306 case 4: res = inl( port ); break;
307 default:
308 ERR("invalid data size %d\n", size);
310 iopl(0);
311 return res;
313 #endif
315 /* first give the DOS VM a chance to handle it */
316 if (Dosvm.inport && Dosvm.inport( port, size, &res )) return res;
318 switch (port)
320 case 0x40:
321 case 0x41:
322 case 0x42:
324 BYTE chan = port & 3;
325 WORD tempval = 0;
326 if (tmr_8253[chan].latched)
327 tempval = tmr_8253[chan].latch;
328 else
330 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
331 if (chan == 0) /* System timer counter divisor */
333 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
334 if (Dosvm.GetTimer)
335 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
336 else
337 tempval = dummy_ctr;
339 else
341 /* FIXME: intelligent hardware timer emulation needed */
342 tempval = dummy_ctr;
346 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
348 case 0:
349 res = 0; /* shouldn't happen? */
350 break;
351 case 1: /* read lo byte */
352 res = (BYTE)tempval;
353 tmr_8253[chan].latched = FALSE;
354 break;
355 case 3: /* read lo byte, then hi byte */
356 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
357 if (tmr_8253[chan].byte_toggle)
359 res = (BYTE)tempval;
360 break;
362 /* else [fall through if read hi byte !] */
363 case 2: /* read hi byte */
364 res = (BYTE)(tempval >> 8);
365 tmr_8253[chan].latched = FALSE;
366 break;
369 break;
370 case 0x60:
371 #if 0 /* what's this port got to do with parport ? */
372 res = (DWORD)parport_8255[0];
373 #endif
374 break;
375 case 0x61:
376 res = (DWORD)parport_8255[1];
377 break;
378 case 0x62:
379 res = (DWORD)parport_8255[2];
380 break;
381 case 0x70:
382 res = (DWORD)cmosaddress;
383 break;
384 case 0x71:
385 res = (DWORD)cmosimage[cmosaddress & 0x3f];
386 break;
387 case 0x200:
388 case 0x201:
389 res = 0xffffffff; /* no joystick */
390 break;
391 default:
392 WARN("Direct I/O read attempted from port %x\n", port);
393 res = 0xffffffff;
394 break;
396 TRACE(" returning ( 0x%lx )\n", res );
397 return res;
401 /**********************************************************************
402 * IO_outport
404 void IO_outport( int port, int size, DWORD value )
406 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
407 value, size, port );
409 #ifdef HAVE_PPDEV
410 if (do_pp_port_access == -1)
411 do_pp_port_access = IO_pp_init();
412 if ((do_pp_port_access == 0) && (size == 1))
413 if (!IO_pp_outp(port,&value))
414 return;
415 #endif
416 #ifdef DIRECT_IO_ACCESS
418 if (do_direct_port_access == -1) IO_port_init();
419 if ((do_direct_port_access)
420 /* Make sure we have access to the port */
421 && (port_permissions[port] & IO_WRITE))
423 iopl(3);
424 switch(size)
426 case 1: outb( LOBYTE(value), port ); break;
427 case 2: outw( LOWORD(value), port ); break;
428 case 4: outl( value, port ); break;
429 default:
430 WARN("Invalid data size %d\n", size);
432 iopl(0);
433 return;
435 #endif
437 /* first give the DOS VM a chance to handle it */
438 if (Dosvm.outport && Dosvm.outport( port, size, value )) return;
440 switch (port)
442 case 0x40:
443 case 0x41:
444 case 0x42:
446 BYTE chan = port & 3;
448 /* we need to get the oldval before any lo/hi byte change has been made */
449 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
450 !tmr_8253[chan].byte_toggle)
451 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
452 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
454 case 0:
455 break; /* shouldn't happen? */
456 case 1: /* write lo byte */
457 tmr_8253[chan].countmax =
458 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
459 break;
460 case 3: /* write lo byte, then hi byte */
461 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
462 if (tmr_8253[chan].byte_toggle)
464 tmr_8253[chan].countmax =
465 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
466 break;
468 /* else [fall through if write hi byte !] */
469 case 2: /* write hi byte */
470 tmr_8253[chan].countmax =
471 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
472 break;
474 /* if programming is finished and value has changed
475 then update to new value */
476 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
477 !tmr_8253[chan].byte_toggle) &&
478 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
479 set_timer_maxval(chan, tmr_8253[chan].countmax);
481 break;
482 case 0x43:
484 BYTE chan = ((BYTE)value & 0xc0) >> 6;
485 /* ctrl byte for specific timer channel */
486 if (chan == 3)
488 FIXME("8254 timer readback not implemented yet\n");
489 break;
491 switch (((BYTE)value & 0x30) >> 4)
493 case 0: /* latch timer */
494 tmr_8253[chan].latched = TRUE;
495 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
496 if (chan == 0) /* System timer divisor */
497 if (Dosvm.GetTimer)
498 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
499 else
500 tmr_8253[chan].latch = dummy_ctr;
501 else
503 /* FIXME: intelligent hardware timer emulation needed */
504 tmr_8253[chan].latch = dummy_ctr;
506 break;
507 case 3: /* write lo byte, then hi byte */
508 tmr_8253[chan].byte_toggle = FALSE; /* init */
509 /* fall through */
510 case 1: /* write lo byte only */
511 case 2: /* write hi byte only */
512 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
513 break;
516 break;
517 case 0x61:
518 parport_8255[1] = (BYTE)value;
519 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
521 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
522 Beep(1193180 / tmr_8253[2].countmax, 20);
524 break;
525 case 0x70:
526 cmosaddress = (BYTE)value & 0x7f;
527 break;
528 case 0x71:
529 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
530 break;
531 default:
532 WARN("Direct I/O write attempted to port %x\n", port );
533 break;