Added, update information about AppDefault.
[wine/multimedia.git] / msdos / ioports.c
blob235b1e1f5e3499c3ec7a2bb8d916dab93d57c750
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 "config.h"
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include "windef.h"
23 #include "callback.h"
24 #include "options.h"
25 #include "miscemu.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(int);
30 static struct {
31 WORD countmax;
32 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
33 WORD latch;
34 BOOL16 latched;
35 BYTE ctrlbyte_ch;
36 WORD oldval;
37 } tmr_8253[3] = {
38 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
39 {0x0012, FALSE, 0, FALSE, 0x74, 0},
40 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
43 static int dummy_ctr = 0;
45 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
47 static BYTE cmosaddress;
49 static BYTE cmosimage[64] =
51 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
52 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
53 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
54 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
55 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
57 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
58 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
61 #if defined(linux) && defined(__i386__)
62 # define DIRECT_IO_ACCESS
63 #else
64 # undef DIRECT_IO_ACCESS
65 # undef PP_IO_ACCESS
66 #endif /* linux && __i386__ */
68 #ifdef DIRECT_IO_ACCESS
70 extern int iopl(int level);
71 static char do_direct_port_access = -1;
72 #ifdef HAVE_PPDEV
73 static char do_pp_port_access = -1; /* -1: uninitialized, 1: not available
74 0: available);*/
75 #endif
76 static char port_permissions[0x10000];
78 #define IO_READ 1
79 #define IO_WRITE 2
81 static void IO_FixCMOSCheckSum(void)
83 WORD sum = 0;
84 int i;
86 for (i=0x10; i < 0x2d; i++)
87 sum += cmosimage[i];
88 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
89 cmosimage[0x2f] = sum & 0xff;
90 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
93 #endif /* DIRECT_IO_ACCESS */
95 static void set_timer_maxval(unsigned timer, unsigned maxval)
97 switch (timer) {
98 case 0: /* System timer counter divisor */
99 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
100 break;
101 case 1: /* RAM refresh */
102 FIXME("RAM refresh counter handling not implemented !\n");
103 break;
104 case 2: /* cassette & speaker */
105 /* speaker on ? */
106 if (((BYTE)parport_8255[1] & 3) == 3)
108 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
109 Beep(1193180 / maxval, 20);
111 break;
115 /**********************************************************************
116 * IO_port_init
119 /* set_IO_permissions(int val1, int val)
120 * Helper function for IO_port_init
122 #ifdef DIRECT_IO_ACCESS
123 static void set_IO_permissions(int val1, int val, char rw)
125 int j;
126 if (val1 != -1) {
127 if (val == -1) val = 0x3ff;
128 for (j = val1; j <= val; j++)
129 port_permissions[j] |= rw;
131 do_direct_port_access = 1;
133 val1 = -1;
134 } else if (val != -1) {
135 do_direct_port_access = 1;
137 port_permissions[val] |= rw;
142 /* do_IO_port_init_read_or_write(char* temp, char rw)
143 * Helper function for IO_port_init
146 static void do_IO_port_init_read_or_write(char* temp, char rw)
148 int val, val1, i, len;
149 if (!strcasecmp(temp, "all")) {
150 MESSAGE("Warning!!! Granting FULL IO port access to"
151 " windoze programs!\nWarning!!! "
152 "*** THIS IS NOT AT ALL "
153 "RECOMMENDED!!! ***\n");
154 for (i=0; i < sizeof(port_permissions); i++)
155 port_permissions[i] |= rw;
157 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
158 len = strlen(temp);
159 val = -1;
160 val1 = -1;
161 for (i = 0; i < len; i++) {
162 switch (temp[i]) {
163 case '0':
164 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
165 sscanf(temp+i, "%x", &val);
166 i += 2;
167 } else {
168 sscanf(temp+i, "%d", &val);
170 while (isxdigit(temp[i]))
171 i++;
172 i--;
173 break;
174 case ',':
175 case ' ':
176 case '\t':
177 set_IO_permissions(val1, val, rw);
178 val1 = -1; val = -1;
179 break;
180 case '-':
181 val1 = val;
182 if (val1 == -1) val1 = 0;
183 break;
184 default:
185 if (temp[i] >= '0' && temp[i] <= '9') {
186 sscanf(temp+i, "%d", &val);
187 while (isdigit(temp[i]))
188 i++;
192 set_IO_permissions(val1, val, rw);
196 static inline BYTE inb( WORD port )
198 BYTE b;
199 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
200 return b;
203 static inline WORD inw( WORD port )
205 WORD w;
206 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
207 return w;
210 static inline DWORD inl( WORD port )
212 DWORD dw;
213 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
214 return dw;
217 static inline void outb( BYTE value, WORD port )
219 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
222 static inline void outw( WORD value, WORD port )
224 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
227 static inline void outl( DWORD value, WORD port )
229 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
232 static void IO_port_init(void)
234 char temp[1024];
236 do_direct_port_access = 0;
237 /* Can we do that? */
238 if (!iopl(3)) {
239 iopl(0);
241 PROFILE_GetWineIniString( "ports", "read", "*",
242 temp, sizeof(temp) );
243 do_IO_port_init_read_or_write(temp, IO_READ);
244 PROFILE_GetWineIniString( "ports", "write", "*",
245 temp, sizeof(temp) );
246 do_IO_port_init_read_or_write(temp, IO_WRITE);
248 IO_FixCMOSCheckSum();
251 #endif /* DIRECT_IO_ACCESS */
253 /**********************************************************************
254 * IO_inport
256 * Note: The size argument has to be handled correctly _externally_
257 * (as we always return a DWORD)
259 DWORD IO_inport( int port, int size )
261 DWORD res = 0;
263 TRACE("%d-byte value from port 0x%02x\n", size, port );
265 #ifdef HAVE_PPDEV
266 if (do_pp_port_access == -1)
267 do_pp_port_access =IO_pp_init();
268 if ((do_pp_port_access == 0 ) && (size == 1))
269 if (!IO_pp_inp(port,&res))
270 return res;
271 #endif
272 #ifdef DIRECT_IO_ACCESS
273 if (do_direct_port_access == -1) IO_port_init();
274 if ((do_direct_port_access)
275 /* Make sure we have access to the port */
276 && (port_permissions[port] & IO_READ))
278 iopl(3);
279 switch(size)
281 case 1: res = inb( port ); break;
282 case 2: res = inw( port ); break;
283 case 4: res = inl( port ); break;
284 default:
285 ERR("invalid data size %d\n", size);
287 iopl(0);
288 return res;
290 #endif
292 /* first give the DOS VM a chance to handle it */
293 if (Dosvm.inport && Dosvm.inport( port, size, &res )) return res;
295 switch (port)
297 case 0x40:
298 case 0x41:
299 case 0x42:
301 BYTE chan = port & 3;
302 WORD tempval = 0;
303 if (tmr_8253[chan].latched)
304 tempval = tmr_8253[chan].latch;
305 else
307 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
308 if (chan == 0) /* System timer counter divisor */
310 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
311 if (Dosvm.GetTimer)
312 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
313 else
314 tempval = dummy_ctr;
316 else
318 /* FIXME: intelligent hardware timer emulation needed */
319 tempval = dummy_ctr;
323 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
325 case 0:
326 res = 0; /* shouldn't happen? */
327 break;
328 case 1: /* read lo byte */
329 res = (BYTE)tempval;
330 tmr_8253[chan].latched = FALSE;
331 break;
332 case 3: /* read lo byte, then hi byte */
333 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
334 if (tmr_8253[chan].byte_toggle)
336 res = (BYTE)tempval;
337 break;
339 /* else [fall through if read hi byte !] */
340 case 2: /* read hi byte */
341 res = (BYTE)(tempval >> 8);
342 tmr_8253[chan].latched = FALSE;
343 break;
346 break;
347 case 0x60:
348 #if 0 /* what's this port got to do with parport ? */
349 res = (DWORD)parport_8255[0];
350 #endif
351 break;
352 case 0x61:
353 res = (DWORD)parport_8255[1];
354 break;
355 case 0x62:
356 res = (DWORD)parport_8255[2];
357 break;
358 case 0x70:
359 res = (DWORD)cmosaddress;
360 break;
361 case 0x71:
362 res = (DWORD)cmosimage[cmosaddress & 0x3f];
363 break;
364 case 0x200:
365 case 0x201:
366 res = 0xffffffff; /* no joystick */
367 break;
368 default:
369 WARN("Direct I/O read attempted from port %x\n", port);
370 res = 0xffffffff;
371 break;
373 TRACE(" returning ( 0x%lx )\n", res );
374 return res;
378 /**********************************************************************
379 * IO_outport
381 void IO_outport( int port, int size, DWORD value )
383 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
384 value, size, port );
386 #ifdef HAVE_PPDEV
387 if (do_pp_port_access == -1)
388 do_pp_port_access = IO_pp_init();
389 if ((do_pp_port_access == 0) && (size == 1))
390 if (!IO_pp_outp(port,&value))
391 return;
392 #endif
393 #ifdef DIRECT_IO_ACCESS
395 if (do_direct_port_access == -1) IO_port_init();
396 if ((do_direct_port_access)
397 /* Make sure we have access to the port */
398 && (port_permissions[port] & IO_WRITE))
400 iopl(3);
401 switch(size)
403 case 1: outb( LOBYTE(value), port ); break;
404 case 2: outw( LOWORD(value), port ); break;
405 case 4: outl( value, port ); break;
406 default:
407 WARN("Invalid data size %d\n", size);
409 iopl(0);
410 return;
412 #endif
414 /* first give the DOS VM a chance to handle it */
415 if (Dosvm.outport && Dosvm.outport( port, size, value )) return;
417 switch (port)
419 case 0x40:
420 case 0x41:
421 case 0x42:
423 BYTE chan = port & 3;
425 /* we need to get the oldval before any lo/hi byte change has been made */
426 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
427 !tmr_8253[chan].byte_toggle)
428 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
429 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
431 case 0:
432 break; /* shouldn't happen? */
433 case 1: /* write lo byte */
434 tmr_8253[chan].countmax =
435 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
436 break;
437 case 3: /* write lo byte, then hi byte */
438 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
439 if (tmr_8253[chan].byte_toggle)
441 tmr_8253[chan].countmax =
442 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
443 break;
445 /* else [fall through if write hi byte !] */
446 case 2: /* write hi byte */
447 tmr_8253[chan].countmax =
448 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
449 break;
451 /* if programming is finished and value has changed
452 then update to new value */
453 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
454 !tmr_8253[chan].byte_toggle) &&
455 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
456 set_timer_maxval(chan, tmr_8253[chan].countmax);
458 break;
459 case 0x43:
461 BYTE chan = ((BYTE)value & 0xc0) >> 6;
462 /* ctrl byte for specific timer channel */
463 if (chan == 3)
465 FIXME("8254 timer readback not implemented yet\n");
466 break;
468 switch (((BYTE)value & 0x30) >> 4)
470 case 0: /* latch timer */
471 tmr_8253[chan].latched = TRUE;
472 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
473 if (chan == 0) /* System timer divisor */
474 if (Dosvm.GetTimer)
475 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
476 else
477 tmr_8253[chan].latch = dummy_ctr;
478 else
480 /* FIXME: intelligent hardware timer emulation needed */
481 tmr_8253[chan].latch = dummy_ctr;
483 break;
484 case 3: /* write lo byte, then hi byte */
485 tmr_8253[chan].byte_toggle = FALSE; /* init */
486 /* fall through */
487 case 1: /* write lo byte only */
488 case 2: /* write hi byte only */
489 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
490 break;
493 break;
494 case 0x61:
495 parport_8255[1] = (BYTE)value;
496 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
498 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
499 Beep(1193180 / tmr_8253[2].countmax, 20);
501 break;
502 case 0x70:
503 cmosaddress = (BYTE)value & 0x7f;
504 break;
505 case 0x71:
506 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
507 break;
508 default:
509 WARN("Direct I/O write attempted to port %x\n", port );
510 break;