If (n)curses wasn't available at compile time:
[wine/multimedia.git] / msdos / ioports.c
blobf02e6aae7cb663ddb652ce73ff2a4973f9b97c7a
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 <stdio.h>
34 #include <string.h>
35 #include <time.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include "windef.h"
40 #include "winnls.h"
41 #include "callback.h"
42 #include "file.h"
43 #include "miscemu.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(int);
48 static struct {
49 WORD countmax;
50 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
51 WORD latch;
52 BOOL16 latched;
53 BYTE ctrlbyte_ch;
54 WORD oldval;
55 } tmr_8253[3] = {
56 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
57 {0x0012, FALSE, 0, FALSE, 0x74, 0},
58 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
61 static int dummy_ctr = 0;
63 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
65 static BYTE cmosaddress;
67 static BYTE cmosimage[64] =
69 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
70 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
71 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
72 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
73 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
75 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
76 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
79 #if defined(linux) && defined(__i386__)
80 # define DIRECT_IO_ACCESS
81 #else
82 # undef DIRECT_IO_ACCESS
83 # undef PP_IO_ACCESS
84 #endif /* linux && __i386__ */
86 #ifdef HAVE_PPDEV
87 static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available
88 0: available);*/
89 #endif
91 #ifdef DIRECT_IO_ACCESS
93 extern int iopl(int level);
94 static char do_direct_port_access = -1;
95 static char port_permissions[0x10000];
97 #define IO_READ 1
98 #define IO_WRITE 2
100 static void IO_FixCMOSCheckSum(void)
102 WORD sum = 0;
103 int i;
105 for (i=0x10; i < 0x2d; i++)
106 sum += cmosimage[i];
107 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
108 cmosimage[0x2f] = sum & 0xff;
109 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
112 #endif /* DIRECT_IO_ACCESS */
114 static void set_timer_maxval(unsigned timer, unsigned maxval)
116 switch (timer) {
117 case 0: /* System timer counter divisor */
118 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
119 break;
120 case 1: /* RAM refresh */
121 FIXME("RAM refresh counter handling not implemented !\n");
122 break;
123 case 2: /* cassette & speaker */
124 /* speaker on ? */
125 if (((BYTE)parport_8255[1] & 3) == 3)
127 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
128 Beep(1193180 / maxval, 20);
130 break;
134 /**********************************************************************
135 * IO_port_init
138 /* set_IO_permissions(int val1, int val)
139 * Helper function for IO_port_init
141 #ifdef DIRECT_IO_ACCESS
142 static void set_IO_permissions(int val1, int val, char rw)
144 int j;
145 if (val1 != -1) {
146 if (val == -1) val = 0x3ff;
147 for (j = val1; j <= val; j++)
148 port_permissions[j] |= rw;
150 do_direct_port_access = 1;
152 val1 = -1;
153 } else if (val != -1) {
154 do_direct_port_access = 1;
156 port_permissions[val] |= rw;
161 /* do_IO_port_init_read_or_write(char* temp, char rw)
162 * Helper function for IO_port_init
165 static void do_IO_port_init_read_or_write(char* temp, char rw)
167 int val, val1, i, len;
168 if (!strcasecmp(temp, "all")) {
169 MESSAGE("Warning!!! Granting FULL IO port access to"
170 " windoze programs!\nWarning!!! "
171 "*** THIS IS NOT AT ALL "
172 "RECOMMENDED!!! ***\n");
173 for (i=0; i < sizeof(port_permissions); i++)
174 port_permissions[i] |= rw;
176 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
177 len = strlen(temp);
178 val = -1;
179 val1 = -1;
180 for (i = 0; i < len; i++) {
181 switch (temp[i]) {
182 case '0':
183 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
184 sscanf(temp+i, "%x", &val);
185 i += 2;
186 } else {
187 sscanf(temp+i, "%d", &val);
189 while (isxdigit(temp[i]))
190 i++;
191 i--;
192 break;
193 case ',':
194 case ' ':
195 case '\t':
196 set_IO_permissions(val1, val, rw);
197 val1 = -1; val = -1;
198 break;
199 case '-':
200 val1 = val;
201 if (val1 == -1) val1 = 0;
202 break;
203 default:
204 if (temp[i] >= '0' && temp[i] <= '9') {
205 sscanf(temp+i, "%d", &val);
206 while (isdigit(temp[i]))
207 i++;
211 set_IO_permissions(val1, val, rw);
215 static inline BYTE inb( WORD port )
217 BYTE b;
218 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
219 return b;
222 static inline WORD inw( WORD port )
224 WORD w;
225 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
226 return w;
229 static inline DWORD inl( WORD port )
231 DWORD dw;
232 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
233 return dw;
236 static inline void outb( BYTE value, WORD port )
238 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
241 static inline void outw( WORD value, WORD port )
243 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
246 static inline void outl( DWORD value, WORD port )
248 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
251 static void IO_port_init(void)
253 char temp[1024];
254 WCHAR tempW[1024];
255 static const WCHAR portsW[] = {'p','o','r','t','s',0};
256 static const WCHAR readW[] = {'r','e','a','d',0};
257 static const WCHAR writeW[] = {'w','r','i','t','e',0};
258 static const WCHAR asteriskW[] = {'*',0};
260 do_direct_port_access = 0;
261 /* Can we do that? */
262 if (!iopl(3)) {
263 iopl(0);
265 PROFILE_GetWineIniString( portsW, readW, asteriskW, tempW, 1024 );
266 WideCharToMultiByte(CP_ACP, 0, tempW, -1, temp, 1024, NULL, NULL);
267 do_IO_port_init_read_or_write(temp, IO_READ);
268 PROFILE_GetWineIniString( portsW, writeW, asteriskW, tempW, 1024 );
269 WideCharToMultiByte(CP_ACP, 0, tempW, -1, temp, 1024, NULL, NULL);
270 do_IO_port_init_read_or_write(temp, IO_WRITE);
272 IO_FixCMOSCheckSum();
275 #endif /* DIRECT_IO_ACCESS */
277 /**********************************************************************
278 * IO_inport
280 * Note: The size argument has to be handled correctly _externally_
281 * (as we always return a DWORD)
283 DWORD IO_inport( int port, int size )
285 DWORD res = 0;
287 TRACE("%d-byte value from port 0x%02x\n", size, port );
289 #ifdef HAVE_PPDEV
290 if (do_pp_port_access == -1)
291 do_pp_port_access =IO_pp_init();
292 if ((do_pp_port_access == 0 ) && (size == 1))
293 if (!IO_pp_inp(port,&res))
294 return res;
295 #endif
296 #ifdef DIRECT_IO_ACCESS
297 if (do_direct_port_access == -1) IO_port_init();
298 if ((do_direct_port_access)
299 /* Make sure we have access to the port */
300 && (port_permissions[port] & IO_READ))
302 iopl(3);
303 switch(size)
305 case 1: res = inb( port ); break;
306 case 2: res = inw( port ); break;
307 case 4: res = inl( port ); break;
308 default:
309 ERR("invalid data size %d\n", size);
311 iopl(0);
312 return res;
314 #endif
316 /* first give the DOS VM a chance to handle it */
317 if (Dosvm.inport || DPMI_LoadDosSystem())
318 if (Dosvm.inport( port, size, &res ))
319 return res;
321 switch (port)
323 case 0x40:
324 case 0x41:
325 case 0x42:
327 BYTE chan = port & 3;
328 WORD tempval = 0;
329 if (tmr_8253[chan].latched)
330 tempval = tmr_8253[chan].latch;
331 else
333 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
334 if (chan == 0) /* System timer counter divisor */
336 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
337 if (Dosvm.GetTimer)
338 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
339 else
340 tempval = dummy_ctr;
342 else
344 /* FIXME: intelligent hardware timer emulation needed */
345 tempval = dummy_ctr;
349 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
351 case 0:
352 res = 0; /* shouldn't happen? */
353 break;
354 case 1: /* read lo byte */
355 res = (BYTE)tempval;
356 tmr_8253[chan].latched = FALSE;
357 break;
358 case 3: /* read lo byte, then hi byte */
359 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
360 if (tmr_8253[chan].byte_toggle)
362 res = (BYTE)tempval;
363 break;
365 /* else [fall through if read hi byte !] */
366 case 2: /* read hi byte */
367 res = (BYTE)(tempval >> 8);
368 tmr_8253[chan].latched = FALSE;
369 break;
372 break;
373 case 0x60:
374 #if 0 /* what's this port got to do with parport ? */
375 res = (DWORD)parport_8255[0];
376 #endif
377 break;
378 case 0x61:
379 res = (DWORD)parport_8255[1];
380 break;
381 case 0x62:
382 res = (DWORD)parport_8255[2];
383 break;
384 case 0x70:
385 res = (DWORD)cmosaddress;
386 break;
387 case 0x71:
388 res = (DWORD)cmosimage[cmosaddress & 0x3f];
389 break;
390 case 0x200:
391 case 0x201:
392 res = 0xffffffff; /* no joystick */
393 break;
394 default:
395 WARN("Direct I/O read attempted from port %x\n", port);
396 res = 0xffffffff;
397 break;
399 TRACE(" returning ( 0x%lx )\n", res );
400 return res;
404 /**********************************************************************
405 * IO_outport
407 void IO_outport( int port, int size, DWORD value )
409 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
410 value, size, port );
412 #ifdef HAVE_PPDEV
413 if (do_pp_port_access == -1)
414 do_pp_port_access = IO_pp_init();
415 if ((do_pp_port_access == 0) && (size == 1))
416 if (!IO_pp_outp(port,&value))
417 return;
418 #endif
419 #ifdef DIRECT_IO_ACCESS
421 if (do_direct_port_access == -1) IO_port_init();
422 if ((do_direct_port_access)
423 /* Make sure we have access to the port */
424 && (port_permissions[port] & IO_WRITE))
426 iopl(3);
427 switch(size)
429 case 1: outb( LOBYTE(value), port ); break;
430 case 2: outw( LOWORD(value), port ); break;
431 case 4: outl( value, port ); break;
432 default:
433 WARN("Invalid data size %d\n", size);
435 iopl(0);
436 return;
438 #endif
440 /* first give the DOS VM a chance to handle it */
441 if (Dosvm.outport || DPMI_LoadDosSystem())
442 if (Dosvm.outport( port, size, value ))
443 return;
445 switch (port)
447 case 0x40:
448 case 0x41:
449 case 0x42:
451 BYTE chan = port & 3;
453 /* we need to get the oldval before any lo/hi byte change has been made */
454 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
455 !tmr_8253[chan].byte_toggle)
456 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
457 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
459 case 0:
460 break; /* shouldn't happen? */
461 case 1: /* write lo byte */
462 tmr_8253[chan].countmax =
463 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
464 break;
465 case 3: /* write lo byte, then hi byte */
466 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
467 if (tmr_8253[chan].byte_toggle)
469 tmr_8253[chan].countmax =
470 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
471 break;
473 /* else [fall through if write hi byte !] */
474 case 2: /* write hi byte */
475 tmr_8253[chan].countmax =
476 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
477 break;
479 /* if programming is finished and value has changed
480 then update to new value */
481 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
482 !tmr_8253[chan].byte_toggle) &&
483 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
484 set_timer_maxval(chan, tmr_8253[chan].countmax);
486 break;
487 case 0x43:
489 BYTE chan = ((BYTE)value & 0xc0) >> 6;
490 /* ctrl byte for specific timer channel */
491 if (chan == 3)
493 FIXME("8254 timer readback not implemented yet\n");
494 break;
496 switch (((BYTE)value & 0x30) >> 4)
498 case 0: /* latch timer */
499 tmr_8253[chan].latched = TRUE;
500 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
501 if (chan == 0) /* System timer divisor */
502 if (Dosvm.GetTimer)
503 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
504 else
505 tmr_8253[chan].latch = dummy_ctr;
506 else
508 /* FIXME: intelligent hardware timer emulation needed */
509 tmr_8253[chan].latch = dummy_ctr;
511 break;
512 case 3: /* write lo byte, then hi byte */
513 tmr_8253[chan].byte_toggle = FALSE; /* init */
514 /* fall through */
515 case 1: /* write lo byte only */
516 case 2: /* write hi byte only */
517 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
518 break;
521 break;
522 case 0x61:
523 parport_8255[1] = (BYTE)value;
524 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
526 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
527 Beep(1193180 / tmr_8253[2].countmax, 20);
529 break;
530 case 0x70:
531 cmosaddress = (BYTE)value & 0x7f;
532 break;
533 case 0x71:
534 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
535 break;
536 default:
537 WARN("Direct I/O write attempted to port %x\n", port );
538 break;