Revert some word-splits to their normal form, e.g. InstallShield.
[wine/multimedia.git] / msdos / ioports.c
blob52f9dea37f40ff55c1aa08b6968a0f683c72f611
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"
29 #include "wine/port.h"
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winreg.h"
44 #include "winternl.h"
45 #include "callback.h"
46 #include "miscemu.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(int);
52 static struct {
53 WORD countmax;
54 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
55 WORD latch;
56 BOOL16 latched;
57 BYTE ctrlbyte_ch;
58 WORD oldval;
59 } tmr_8253[3] = {
60 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
61 {0x0012, FALSE, 0, FALSE, 0x74, 0},
62 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
65 static int dummy_ctr = 0;
67 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
69 static BYTE cmosaddress;
71 static BYTE cmosimage[64] =
73 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
74 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
75 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
76 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
77 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
79 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
80 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
83 #if defined(linux) && defined(__i386__)
84 # define DIRECT_IO_ACCESS
85 #else
86 # undef DIRECT_IO_ACCESS
87 # undef PP_IO_ACCESS
88 #endif /* linux && __i386__ */
90 #ifdef HAVE_PPDEV
91 static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available
92 0: available);*/
93 #endif
95 #ifdef DIRECT_IO_ACCESS
97 extern int iopl(int level);
98 static char do_direct_port_access = -1;
99 static char port_permissions[0x10000];
101 #define IO_READ 1
102 #define IO_WRITE 2
104 static void IO_FixCMOSCheckSum(void)
106 WORD sum = 0;
107 int i;
109 for (i=0x10; i < 0x2d; i++)
110 sum += cmosimage[i];
111 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
112 cmosimage[0x2f] = sum & 0xff;
113 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
116 #endif /* DIRECT_IO_ACCESS */
118 static void set_timer_maxval(unsigned timer, unsigned maxval)
120 switch (timer) {
121 case 0: /* System timer counter divisor */
122 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
123 break;
124 case 1: /* RAM refresh */
125 FIXME("RAM refresh counter handling not implemented !\n");
126 break;
127 case 2: /* cassette & speaker */
128 /* speaker on ? */
129 if (((BYTE)parport_8255[1] & 3) == 3)
131 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
132 Beep(1193180 / maxval, 20);
134 break;
138 /**********************************************************************
139 * IO_port_init
142 /* set_IO_permissions(int val1, int val)
143 * Helper function for IO_port_init
145 #ifdef DIRECT_IO_ACCESS
146 static void set_IO_permissions(int val1, int val, char rw)
148 int j;
149 if (val1 != -1) {
150 if (val == -1) val = 0x3ff;
151 for (j = val1; j <= val; j++)
152 port_permissions[j] |= rw;
154 do_direct_port_access = 1;
156 val1 = -1;
157 } else if (val != -1) {
158 do_direct_port_access = 1;
160 port_permissions[val] |= rw;
165 /* do_IO_port_init_read_or_write(char* temp, char rw)
166 * Helper function for IO_port_init
169 static void do_IO_port_init_read_or_write(const WCHAR *str, char rw)
171 int val, val1, i;
172 WCHAR *end;
173 static const WCHAR allW[] = {'a','l','l',0};
175 if (!strcmpiW(str, allW))
177 for (i=0; i < sizeof(port_permissions); i++)
178 port_permissions[i] |= rw;
180 else
182 val = -1;
183 val1 = -1;
184 while (*str)
186 switch(*str)
188 case ',':
189 case ' ':
190 case '\t':
191 set_IO_permissions(val1, val, rw);
192 val1 = -1;
193 val = -1;
194 str++;
195 break;
196 case '-':
197 val1 = val;
198 if (val1 == -1) val1 = 0;
199 str++;
200 break;
201 default:
202 if (isdigitW(*str))
204 val = strtoulW( str, &end, 0 );
205 if (end == str)
207 val = -1;
208 str++;
210 else str = end;
212 break;
215 set_IO_permissions(val1, val, rw);
219 static inline BYTE inb( WORD port )
221 BYTE b;
222 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
223 return b;
226 static inline WORD inw( WORD port )
228 WORD w;
229 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
230 return w;
233 static inline DWORD inl( WORD port )
235 DWORD dw;
236 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
237 return dw;
240 static inline void outb( BYTE value, WORD port )
242 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
245 static inline void outw( WORD value, WORD port )
247 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
250 static inline void outl( DWORD value, WORD port )
252 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
255 static void IO_port_init(void)
257 char tmp[1024];
258 HKEY hkey;
259 DWORD dummy;
260 OBJECT_ATTRIBUTES attr;
261 UNICODE_STRING nameW;
263 static const WCHAR portsW[] = {'M','a','c','h','i','n','e','\\',
264 'S','o','f','t','w','a','r','e','\\',
265 'W','i','n','e','\\','W','i','n','e','\\',
266 'C','o','n','f','i','g','\\','P','o','r','t','s',0};
267 static const WCHAR readW[] = {'r','e','a','d',0};
268 static const WCHAR writeW[] = {'w','r','i','t','e',0};
270 do_direct_port_access = 0;
271 /* Can we do that? */
272 if (!iopl(3))
274 iopl(0);
276 attr.Length = sizeof(attr);
277 attr.RootDirectory = 0;
278 attr.ObjectName = &nameW;
279 attr.Attributes = 0;
280 attr.SecurityDescriptor = NULL;
281 attr.SecurityQualityOfService = NULL;
282 RtlInitUnicodeString( &nameW, portsW );
284 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
286 RtlInitUnicodeString( &nameW, readW );
287 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
289 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
290 do_IO_port_init_read_or_write(str, IO_READ);
292 RtlInitUnicodeString( &nameW, writeW );
293 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
295 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
296 do_IO_port_init_read_or_write(str, IO_WRITE);
298 NtClose( hkey );
301 IO_FixCMOSCheckSum();
304 #endif /* DIRECT_IO_ACCESS */
306 /**********************************************************************
307 * IO_inport
309 * Note: The size argument has to be handled correctly _externally_
310 * (as we always return a DWORD)
312 DWORD IO_inport( int port, int size )
314 DWORD res = 0;
316 TRACE("%d-byte value from port 0x%02x\n", size, port );
318 #ifdef HAVE_PPDEV
319 if (do_pp_port_access == -1)
320 do_pp_port_access =IO_pp_init();
321 if ((do_pp_port_access == 0 ) && (size == 1))
322 if (!IO_pp_inp(port,&res))
323 return res;
324 #endif
325 #ifdef DIRECT_IO_ACCESS
326 if (do_direct_port_access == -1) IO_port_init();
327 if ((do_direct_port_access)
328 /* Make sure we have access to the port */
329 && (port_permissions[port] & IO_READ))
331 iopl(3);
332 switch(size)
334 case 1: res = inb( port ); break;
335 case 2: res = inw( port ); break;
336 case 4: res = inl( port ); break;
337 default:
338 ERR("invalid data size %d\n", size);
340 iopl(0);
341 return res;
343 #endif
345 /* first give the DOS VM a chance to handle it */
346 if (Dosvm.inport || DPMI_LoadDosSystem())
347 if (Dosvm.inport( port, size, &res ))
348 return res;
350 switch (port)
352 case 0x40:
353 case 0x41:
354 case 0x42:
356 BYTE chan = port & 3;
357 WORD tempval = 0;
358 if (tmr_8253[chan].latched)
359 tempval = tmr_8253[chan].latch;
360 else
362 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
363 if (chan == 0) /* System timer counter divisor */
365 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
366 if (Dosvm.GetTimer)
367 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
368 else
369 tempval = dummy_ctr;
371 else
373 /* FIXME: intelligent hardware timer emulation needed */
374 tempval = dummy_ctr;
378 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
380 case 0:
381 res = 0; /* shouldn't happen? */
382 break;
383 case 1: /* read lo byte */
384 res = (BYTE)tempval;
385 tmr_8253[chan].latched = FALSE;
386 break;
387 case 3: /* read lo byte, then hi byte */
388 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
389 if (tmr_8253[chan].byte_toggle)
391 res = (BYTE)tempval;
392 break;
394 /* else [fall through if read hi byte !] */
395 case 2: /* read hi byte */
396 res = (BYTE)(tempval >> 8);
397 tmr_8253[chan].latched = FALSE;
398 break;
401 break;
402 case 0x60:
403 #if 0 /* what's this port got to do with parport ? */
404 res = (DWORD)parport_8255[0];
405 #endif
406 break;
407 case 0x61:
408 res = (DWORD)parport_8255[1];
409 break;
410 case 0x62:
411 res = (DWORD)parport_8255[2];
412 break;
413 case 0x70:
414 res = (DWORD)cmosaddress;
415 break;
416 case 0x71:
417 res = (DWORD)cmosimage[cmosaddress & 0x3f];
418 break;
419 case 0x200:
420 case 0x201:
421 res = 0xffffffff; /* no joystick */
422 break;
423 default:
424 WARN("Direct I/O read attempted from port %x\n", port);
425 res = 0xffffffff;
426 break;
428 TRACE(" returning ( 0x%lx )\n", res );
429 return res;
433 /**********************************************************************
434 * IO_outport
436 void IO_outport( int port, int size, DWORD value )
438 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
439 value, size, port );
441 #ifdef HAVE_PPDEV
442 if (do_pp_port_access == -1)
443 do_pp_port_access = IO_pp_init();
444 if ((do_pp_port_access == 0) && (size == 1))
445 if (!IO_pp_outp(port,&value))
446 return;
447 #endif
448 #ifdef DIRECT_IO_ACCESS
450 if (do_direct_port_access == -1) IO_port_init();
451 if ((do_direct_port_access)
452 /* Make sure we have access to the port */
453 && (port_permissions[port] & IO_WRITE))
455 iopl(3);
456 switch(size)
458 case 1: outb( LOBYTE(value), port ); break;
459 case 2: outw( LOWORD(value), port ); break;
460 case 4: outl( value, port ); break;
461 default:
462 WARN("Invalid data size %d\n", size);
464 iopl(0);
465 return;
467 #endif
469 /* first give the DOS VM a chance to handle it */
470 if (Dosvm.outport || DPMI_LoadDosSystem())
471 if (Dosvm.outport( port, size, value ))
472 return;
474 switch (port)
476 case 0x40:
477 case 0x41:
478 case 0x42:
480 BYTE chan = port & 3;
482 /* we need to get the oldval before any lo/hi byte change has been made */
483 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
484 !tmr_8253[chan].byte_toggle)
485 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
486 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
488 case 0:
489 break; /* shouldn't happen? */
490 case 1: /* write lo byte */
491 tmr_8253[chan].countmax =
492 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
493 break;
494 case 3: /* write lo byte, then hi byte */
495 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
496 if (tmr_8253[chan].byte_toggle)
498 tmr_8253[chan].countmax =
499 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
500 break;
502 /* else [fall through if write hi byte !] */
503 case 2: /* write hi byte */
504 tmr_8253[chan].countmax =
505 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
506 break;
508 /* if programming is finished and value has changed
509 then update to new value */
510 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
511 !tmr_8253[chan].byte_toggle) &&
512 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
513 set_timer_maxval(chan, tmr_8253[chan].countmax);
515 break;
516 case 0x43:
518 BYTE chan = ((BYTE)value & 0xc0) >> 6;
519 /* ctrl byte for specific timer channel */
520 if (chan == 3)
522 FIXME("8254 timer readback not implemented yet\n");
523 break;
525 switch (((BYTE)value & 0x30) >> 4)
527 case 0: /* latch timer */
528 tmr_8253[chan].latched = TRUE;
529 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
530 if (chan == 0) /* System timer divisor */
531 if (Dosvm.GetTimer)
532 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
533 else
534 tmr_8253[chan].latch = dummy_ctr;
535 else
537 /* FIXME: intelligent hardware timer emulation needed */
538 tmr_8253[chan].latch = dummy_ctr;
540 break;
541 case 3: /* write lo byte, then hi byte */
542 tmr_8253[chan].byte_toggle = FALSE; /* init */
543 /* fall through */
544 case 1: /* write lo byte only */
545 case 2: /* write hi byte only */
546 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
547 break;
550 break;
551 case 0x61:
552 parport_8255[1] = (BYTE)value;
553 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
555 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
556 Beep(1193180 / tmr_8253[2].countmax, 20);
558 break;
559 case 0x70:
560 cmosaddress = (BYTE)value & 0x7f;
561 break;
562 case 0x71:
563 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
564 break;
565 default:
566 WARN("Direct I/O write attempted to port %x\n", port );
567 break;