Throw exception only for Wine internal locks.
[wine/gsoc_dplay.git] / msdos / ioports.c
blobbb6b798dddff2326938da2c8c0990793b1205b89
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 #include <unistd.h>
36 #include "windef.h"
37 #include "callback.h"
38 #include "file.h"
39 #include "miscemu.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(int);
44 static struct {
45 WORD countmax;
46 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
47 WORD latch;
48 BOOL16 latched;
49 BYTE ctrlbyte_ch;
50 WORD oldval;
51 } tmr_8253[3] = {
52 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
53 {0x0012, FALSE, 0, FALSE, 0x74, 0},
54 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
57 static int dummy_ctr = 0;
59 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
61 static BYTE cmosaddress;
63 static BYTE cmosimage[64] =
65 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
66 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
67 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
68 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
69 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
71 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
72 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
75 #if defined(linux) && defined(__i386__)
76 # define DIRECT_IO_ACCESS
77 #else
78 # undef DIRECT_IO_ACCESS
79 # undef PP_IO_ACCESS
80 #endif /* linux && __i386__ */
82 #ifdef HAVE_PPDEV
83 static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available
84 0: available);*/
85 #endif
87 #ifdef DIRECT_IO_ACCESS
89 extern int iopl(int level);
90 static char do_direct_port_access = -1;
91 static char port_permissions[0x10000];
93 #define IO_READ 1
94 #define IO_WRITE 2
96 static void IO_FixCMOSCheckSum(void)
98 WORD sum = 0;
99 int i;
101 for (i=0x10; i < 0x2d; i++)
102 sum += cmosimage[i];
103 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
104 cmosimage[0x2f] = sum & 0xff;
105 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
108 #endif /* DIRECT_IO_ACCESS */
110 static void set_timer_maxval(unsigned timer, unsigned maxval)
112 switch (timer) {
113 case 0: /* System timer counter divisor */
114 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
115 break;
116 case 1: /* RAM refresh */
117 FIXME("RAM refresh counter handling not implemented !\n");
118 break;
119 case 2: /* cassette & speaker */
120 /* speaker on ? */
121 if (((BYTE)parport_8255[1] & 3) == 3)
123 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
124 Beep(1193180 / maxval, 20);
126 break;
130 /**********************************************************************
131 * IO_port_init
134 /* set_IO_permissions(int val1, int val)
135 * Helper function for IO_port_init
137 #ifdef DIRECT_IO_ACCESS
138 static void set_IO_permissions(int val1, int val, char rw)
140 int j;
141 if (val1 != -1) {
142 if (val == -1) val = 0x3ff;
143 for (j = val1; j <= val; j++)
144 port_permissions[j] |= rw;
146 do_direct_port_access = 1;
148 val1 = -1;
149 } else if (val != -1) {
150 do_direct_port_access = 1;
152 port_permissions[val] |= rw;
157 /* do_IO_port_init_read_or_write(char* temp, char rw)
158 * Helper function for IO_port_init
161 static void do_IO_port_init_read_or_write(char* temp, char rw)
163 int val, val1, i, len;
164 if (!strcasecmp(temp, "all")) {
165 MESSAGE("Warning!!! Granting FULL IO port access to"
166 " windoze programs!\nWarning!!! "
167 "*** THIS IS NOT AT ALL "
168 "RECOMMENDED!!! ***\n");
169 for (i=0; i < sizeof(port_permissions); i++)
170 port_permissions[i] |= rw;
172 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
173 len = strlen(temp);
174 val = -1;
175 val1 = -1;
176 for (i = 0; i < len; i++) {
177 switch (temp[i]) {
178 case '0':
179 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
180 sscanf(temp+i, "%x", &val);
181 i += 2;
182 } else {
183 sscanf(temp+i, "%d", &val);
185 while (isxdigit(temp[i]))
186 i++;
187 i--;
188 break;
189 case ',':
190 case ' ':
191 case '\t':
192 set_IO_permissions(val1, val, rw);
193 val1 = -1; val = -1;
194 break;
195 case '-':
196 val1 = val;
197 if (val1 == -1) val1 = 0;
198 break;
199 default:
200 if (temp[i] >= '0' && temp[i] <= '9') {
201 sscanf(temp+i, "%d", &val);
202 while (isdigit(temp[i]))
203 i++;
207 set_IO_permissions(val1, val, rw);
211 static inline BYTE inb( WORD port )
213 BYTE b;
214 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
215 return b;
218 static inline WORD inw( WORD port )
220 WORD w;
221 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
222 return w;
225 static inline DWORD inl( WORD port )
227 DWORD dw;
228 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
229 return dw;
232 static inline void outb( BYTE value, WORD port )
234 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
237 static inline void outw( WORD value, WORD port )
239 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
242 static inline void outl( DWORD value, WORD port )
244 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
247 static void IO_port_init(void)
249 char temp[1024];
251 do_direct_port_access = 0;
252 /* Can we do that? */
253 if (!iopl(3)) {
254 iopl(0);
256 PROFILE_GetWineIniString( "ports", "read", "*",
257 temp, sizeof(temp) );
258 do_IO_port_init_read_or_write(temp, IO_READ);
259 PROFILE_GetWineIniString( "ports", "write", "*",
260 temp, sizeof(temp) );
261 do_IO_port_init_read_or_write(temp, IO_WRITE);
263 IO_FixCMOSCheckSum();
266 #endif /* DIRECT_IO_ACCESS */
268 /**********************************************************************
269 * IO_inport
271 * Note: The size argument has to be handled correctly _externally_
272 * (as we always return a DWORD)
274 DWORD IO_inport( int port, int size )
276 DWORD res = 0;
278 TRACE("%d-byte value from port 0x%02x\n", size, port );
280 #ifdef HAVE_PPDEV
281 if (do_pp_port_access == -1)
282 do_pp_port_access =IO_pp_init();
283 if ((do_pp_port_access == 0 ) && (size == 1))
284 if (!IO_pp_inp(port,&res))
285 return res;
286 #endif
287 #ifdef DIRECT_IO_ACCESS
288 if (do_direct_port_access == -1) IO_port_init();
289 if ((do_direct_port_access)
290 /* Make sure we have access to the port */
291 && (port_permissions[port] & IO_READ))
293 iopl(3);
294 switch(size)
296 case 1: res = inb( port ); break;
297 case 2: res = inw( port ); break;
298 case 4: res = inl( port ); break;
299 default:
300 ERR("invalid data size %d\n", size);
302 iopl(0);
303 return res;
305 #endif
307 /* first give the DOS VM a chance to handle it */
308 if (Dosvm.inport && Dosvm.inport( port, size, &res )) return res;
310 switch (port)
312 case 0x40:
313 case 0x41:
314 case 0x42:
316 BYTE chan = port & 3;
317 WORD tempval = 0;
318 if (tmr_8253[chan].latched)
319 tempval = tmr_8253[chan].latch;
320 else
322 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
323 if (chan == 0) /* System timer counter divisor */
325 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
326 if (Dosvm.GetTimer)
327 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
328 else
329 tempval = dummy_ctr;
331 else
333 /* FIXME: intelligent hardware timer emulation needed */
334 tempval = dummy_ctr;
338 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
340 case 0:
341 res = 0; /* shouldn't happen? */
342 break;
343 case 1: /* read lo byte */
344 res = (BYTE)tempval;
345 tmr_8253[chan].latched = FALSE;
346 break;
347 case 3: /* read lo byte, then hi byte */
348 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
349 if (tmr_8253[chan].byte_toggle)
351 res = (BYTE)tempval;
352 break;
354 /* else [fall through if read hi byte !] */
355 case 2: /* read hi byte */
356 res = (BYTE)(tempval >> 8);
357 tmr_8253[chan].latched = FALSE;
358 break;
361 break;
362 case 0x60:
363 #if 0 /* what's this port got to do with parport ? */
364 res = (DWORD)parport_8255[0];
365 #endif
366 break;
367 case 0x61:
368 res = (DWORD)parport_8255[1];
369 break;
370 case 0x62:
371 res = (DWORD)parport_8255[2];
372 break;
373 case 0x70:
374 res = (DWORD)cmosaddress;
375 break;
376 case 0x71:
377 res = (DWORD)cmosimage[cmosaddress & 0x3f];
378 break;
379 case 0x200:
380 case 0x201:
381 res = 0xffffffff; /* no joystick */
382 break;
383 default:
384 WARN("Direct I/O read attempted from port %x\n", port);
385 res = 0xffffffff;
386 break;
388 TRACE(" returning ( 0x%lx )\n", res );
389 return res;
393 /**********************************************************************
394 * IO_outport
396 void IO_outport( int port, int size, DWORD value )
398 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
399 value, size, port );
401 #ifdef HAVE_PPDEV
402 if (do_pp_port_access == -1)
403 do_pp_port_access = IO_pp_init();
404 if ((do_pp_port_access == 0) && (size == 1))
405 if (!IO_pp_outp(port,&value))
406 return;
407 #endif
408 #ifdef DIRECT_IO_ACCESS
410 if (do_direct_port_access == -1) IO_port_init();
411 if ((do_direct_port_access)
412 /* Make sure we have access to the port */
413 && (port_permissions[port] & IO_WRITE))
415 iopl(3);
416 switch(size)
418 case 1: outb( LOBYTE(value), port ); break;
419 case 2: outw( LOWORD(value), port ); break;
420 case 4: outl( value, port ); break;
421 default:
422 WARN("Invalid data size %d\n", size);
424 iopl(0);
425 return;
427 #endif
429 /* first give the DOS VM a chance to handle it */
430 if (Dosvm.outport && Dosvm.outport( port, size, value )) return;
432 switch (port)
434 case 0x40:
435 case 0x41:
436 case 0x42:
438 BYTE chan = port & 3;
440 /* we need to get the oldval before any lo/hi byte change has been made */
441 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
442 !tmr_8253[chan].byte_toggle)
443 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
444 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
446 case 0:
447 break; /* shouldn't happen? */
448 case 1: /* write lo byte */
449 tmr_8253[chan].countmax =
450 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
451 break;
452 case 3: /* write lo byte, then hi byte */
453 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
454 if (tmr_8253[chan].byte_toggle)
456 tmr_8253[chan].countmax =
457 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
458 break;
460 /* else [fall through if write hi byte !] */
461 case 2: /* write hi byte */
462 tmr_8253[chan].countmax =
463 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
464 break;
466 /* if programming is finished and value has changed
467 then update to new value */
468 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
469 !tmr_8253[chan].byte_toggle) &&
470 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
471 set_timer_maxval(chan, tmr_8253[chan].countmax);
473 break;
474 case 0x43:
476 BYTE chan = ((BYTE)value & 0xc0) >> 6;
477 /* ctrl byte for specific timer channel */
478 if (chan == 3)
480 FIXME("8254 timer readback not implemented yet\n");
481 break;
483 switch (((BYTE)value & 0x30) >> 4)
485 case 0: /* latch timer */
486 tmr_8253[chan].latched = TRUE;
487 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
488 if (chan == 0) /* System timer divisor */
489 if (Dosvm.GetTimer)
490 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
491 else
492 tmr_8253[chan].latch = dummy_ctr;
493 else
495 /* FIXME: intelligent hardware timer emulation needed */
496 tmr_8253[chan].latch = dummy_ctr;
498 break;
499 case 3: /* write lo byte, then hi byte */
500 tmr_8253[chan].byte_toggle = FALSE; /* init */
501 /* fall through */
502 case 1: /* write lo byte only */
503 case 2: /* write hi byte only */
504 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
505 break;
508 break;
509 case 0x61:
510 parport_8255[1] = (BYTE)value;
511 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
513 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
514 Beep(1193180 / tmr_8253[2].countmax, 20);
516 break;
517 case 0x70:
518 cmosaddress = (BYTE)value & 0x7f;
519 break;
520 case 0x71:
521 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
522 break;
523 default:
524 WARN("Direct I/O write attempted to port %x\n", port );
525 break;