- Begun implementation of a C statements parser.
[wine/multimedia.git] / msdos / ioports.c
blob8047b8a29e40b96b871ccf48f6d6dd02b13ea010
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 "vga.h"
24 #include "callback.h"
25 #include "dosexe.h"
26 #include "options.h"
27 #include "miscemu.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(int);
32 static struct {
33 WORD countmax;
34 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
35 WORD latch;
36 BOOL16 latched;
37 BYTE ctrlbyte_ch;
38 WORD oldval;
39 } tmr_8253[3] = {
40 {0xFFFF, FALSE, 0, FALSE, 0x06, 0},
41 {0x0012, FALSE, 0, FALSE, 0x44, 0},
42 {0x0001, FALSE, 0, FALSE, 0x86, 0},
45 static int dummy_ctr = 0;
47 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
49 static BYTE cmosaddress;
51 static BYTE cmosimage[64] =
53 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
54 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
55 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
56 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
57 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
59 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
60 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
63 #if defined(linux) && defined(__i386__)
64 # define DIRECT_IO_ACCESS
65 #else
66 # undef DIRECT_IO_ACCESS
67 # undef PP_IO_ACCESS
68 #endif /* linux && __i386__ */
70 #ifdef DIRECT_IO_ACCESS
72 extern int iopl(int level);
73 static char do_direct_port_access = -1;
74 #ifdef HAVE_PPDEV
75 static char do_pp_port_access = -1; /* -1: uninitialized, 1: not available
76 0: available);*/
77 #endif
78 static char port_permissions[0x10000];
80 #define IO_READ 1
81 #define IO_WRITE 2
83 static void IO_FixCMOSCheckSum(void)
85 WORD sum = 0;
86 int i;
88 for (i=0x10; i < 0x2d; i++)
89 sum += cmosimage[i];
90 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
91 cmosimage[0x2f] = sum & 0xff;
92 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
95 #endif /* DIRECT_IO_ACCESS */
97 static void set_timer_maxval(unsigned timer, unsigned maxval)
99 switch (timer) {
100 case 0: /* System timer counter divisor */
101 Dosvm.SetTimer(maxval);
102 break;
103 case 1: /* RAM refresh */
104 FIXME("RAM refresh counter handling not implemented !\n");
105 break;
106 case 2: /* cassette & speaker */
107 /* speaker on ? */
108 if (((BYTE)parport_8255[1] & 3) == 3)
110 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
111 Beep(1193180 / maxval, 20);
113 break;
117 /**********************************************************************
118 * IO_port_init
121 /* set_IO_permissions(int val1, int val)
122 * Helper function for IO_port_init
124 #ifdef DIRECT_IO_ACCESS
125 static void set_IO_permissions(int val1, int val, char rw)
127 int j;
128 if (val1 != -1) {
129 if (val == -1) val = 0x3ff;
130 for (j = val1; j <= val; j++)
131 port_permissions[j] |= rw;
133 do_direct_port_access = 1;
135 val1 = -1;
136 } else if (val != -1) {
137 do_direct_port_access = 1;
139 port_permissions[val] |= rw;
144 /* do_IO_port_init_read_or_write(char* temp, char rw)
145 * Helper function for IO_port_init
148 static void do_IO_port_init_read_or_write(char* temp, char rw)
150 int val, val1, i, len;
151 if (!strcasecmp(temp, "all")) {
152 MESSAGE("Warning!!! Granting FULL IO port access to"
153 " windoze programs!\nWarning!!! "
154 "*** THIS IS NOT AT ALL "
155 "RECOMMENDED!!! ***\n");
156 for (i=0; i < sizeof(port_permissions); i++)
157 port_permissions[i] |= rw;
159 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
160 len = strlen(temp);
161 val = -1;
162 val1 = -1;
163 for (i = 0; i < len; i++) {
164 switch (temp[i]) {
165 case '0':
166 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
167 sscanf(temp+i, "%x", &val);
168 i += 2;
169 } else {
170 sscanf(temp+i, "%d", &val);
172 while (isxdigit(temp[i]))
173 i++;
174 i--;
175 break;
176 case ',':
177 case ' ':
178 case '\t':
179 set_IO_permissions(val1, val, rw);
180 val1 = -1; val = -1;
181 break;
182 case '-':
183 val1 = val;
184 if (val1 == -1) val1 = 0;
185 break;
186 default:
187 if (temp[i] >= '0' && temp[i] <= '9') {
188 sscanf(temp+i, "%d", &val);
189 while (isdigit(temp[i]))
190 i++;
194 set_IO_permissions(val1, val, rw);
198 static inline BYTE inb( WORD port )
200 BYTE b;
201 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
202 return b;
205 static inline WORD inw( WORD port )
207 WORD w;
208 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
209 return w;
212 static inline DWORD inl( WORD port )
214 DWORD dw;
215 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
216 return dw;
219 static inline void outb( BYTE value, WORD port )
221 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
224 static inline void outw( WORD value, WORD port )
226 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
229 static inline void outl( DWORD value, WORD port )
231 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
234 static void IO_port_init(void)
236 char temp[1024];
238 do_direct_port_access = 0;
239 /* Can we do that? */
240 if (!iopl(3)) {
241 iopl(0);
243 PROFILE_GetWineIniString( "ports", "read", "*",
244 temp, sizeof(temp) );
245 do_IO_port_init_read_or_write(temp, IO_READ);
246 PROFILE_GetWineIniString( "ports", "write", "*",
247 temp, sizeof(temp) );
248 do_IO_port_init_read_or_write(temp, IO_WRITE);
250 IO_FixCMOSCheckSum();
253 #endif /* DIRECT_IO_ACCESS */
255 /**********************************************************************
256 * IO_inport
258 * Note: The size argument has to be handled correctly _externally_
259 * (as we always return a DWORD)
261 DWORD IO_inport( int port, int size )
263 DWORD res = 0;
265 TRACE("%d-byte value from port 0x%02x\n", size, port );
267 #ifdef HAVE_PPDEV
268 if (do_pp_port_access == -1)
269 do_pp_port_access =IO_pp_init();
270 if ((do_pp_port_access == 0 ) && (size == 1))
271 if (!IO_pp_inp(port,&res))
272 return res;
273 #endif
274 #ifdef DIRECT_IO_ACCESS
275 if (do_direct_port_access == -1) IO_port_init();
276 if ((do_direct_port_access)
277 /* Make sure we have access to the port */
278 && (port_permissions[port] & IO_READ))
280 iopl(3);
281 switch(size)
283 case 1: res = inb( port ); break;
284 case 2: res = inw( port ); break;
285 case 4: res = inl( port ); break;
286 default:
287 ERR("invalid data size %d\n", size);
289 iopl(0);
290 return res;
292 #endif
294 switch (port)
296 case 0x40:
297 case 0x41:
298 case 0x42:
300 BYTE chan = port & 3;
301 WORD tempval = 0;
302 if (tmr_8253[chan].latched)
303 tempval = tmr_8253[chan].latch;
304 else
306 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
307 if (chan == 0) /* System timer counter divisor */
309 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
310 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
312 else
314 /* FIXME: intelligent hardware timer emulation needed */
315 tempval = dummy_ctr;
319 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
321 case 0:
322 res = 0; /* shouldn't happen? */
323 break;
324 case 1: /* read lo byte */
325 res = (BYTE)tempval;
326 tmr_8253[chan].latched = FALSE;
327 break;
328 case 3: /* read lo byte, then hi byte */
329 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
330 if (tmr_8253[chan].byte_toggle)
332 res = (BYTE)tempval;
333 break;
335 /* else [fall through if read hi byte !] */
336 case 2: /* read hi byte */
337 res = (BYTE)(tempval >> 8);
338 tmr_8253[chan].latched = FALSE;
339 break;
342 break;
343 case 0x60:
344 res = INT_Int09ReadScan(NULL);
345 #if 0 /* what's this port got to do with parport ? */
346 res = (DWORD)parport_8255[0];
347 #endif
348 break;
349 case 0x61:
350 res = (DWORD)parport_8255[1];
351 break;
352 case 0x62:
353 res = (DWORD)parport_8255[2];
354 break;
355 case 0x70:
356 res = (DWORD)cmosaddress;
357 break;
358 case 0x71:
359 res = (DWORD)cmosimage[cmosaddress & 0x3f];
360 break;
361 case 0x200:
362 case 0x201:
363 res = 0xffffffff; /* no joystick */
364 break;
365 case 0x3ba:
366 case 0x3da:
367 res = (DWORD)VGA_ioport_in( port );
368 break;
369 default:
370 WARN("Direct I/O read attempted from port %x\n", port);
371 res = 0xffffffff;
372 break;
374 TRACE(" returning ( 0x%lx )\n", res );
375 return res;
379 /**********************************************************************
380 * IO_outport
382 void IO_outport( int port, int size, DWORD value )
384 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
385 value, size, port );
387 #ifdef HAVE_PPDEV
388 if (do_pp_port_access == -1)
389 do_pp_port_access = IO_pp_init();
390 if ((do_pp_port_access == 0) && (size == 1))
391 if (!IO_pp_outp(port,&value))
392 return;
393 #endif
394 #ifdef DIRECT_IO_ACCESS
396 if (do_direct_port_access == -1) IO_port_init();
397 if ((do_direct_port_access)
398 /* Make sure we have access to the port */
399 && (port_permissions[port] & IO_WRITE))
401 iopl(3);
402 switch(size)
404 case 1: outb( LOBYTE(value), port ); break;
405 case 2: outw( LOWORD(value), port ); break;
406 case 4: outl( value, port ); break;
407 default:
408 WARN("Invalid data size %d\n", size);
410 iopl(0);
411 return;
413 #endif
415 switch (port)
417 case 0x20:
418 Dosvm.OutPIC( port, (BYTE)value );
419 break;
420 case 0x40:
421 case 0x41:
422 case 0x42:
424 BYTE chan = port & 3;
426 /* we need to get the oldval before any lo/hi byte change has been made */
427 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
428 !tmr_8253[chan].byte_toggle)
429 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
430 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
432 case 0:
433 break; /* shouldn't happen? */
434 case 1: /* write lo byte */
435 tmr_8253[chan].countmax =
436 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
437 break;
438 case 3: /* write lo byte, then hi byte */
439 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
440 if (tmr_8253[chan].byte_toggle)
442 tmr_8253[chan].countmax =
443 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
444 break;
446 /* else [fall through if write hi byte !] */
447 case 2: /* write hi byte */
448 tmr_8253[chan].countmax =
449 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
450 break;
452 /* if programming is finished and value has changed
453 then update to new value */
454 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
455 !tmr_8253[chan].byte_toggle) &&
456 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
457 set_timer_maxval(chan, tmr_8253[chan].countmax);
459 break;
460 case 0x43:
462 BYTE chan = ((BYTE)value & 0xc0) >> 6;
463 /* ctrl byte for specific timer channel */
464 if (chan == 3)
466 FIXME("8254 timer readback not implemented yet\n");
467 break;
469 switch (((BYTE)value & 0x30) >> 4)
471 case 0: /* latch timer */
472 tmr_8253[chan].latched = TRUE;
473 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
474 if (chan == 0) /* System timer divisor */
475 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
476 else
478 /* FIXME: intelligent hardware timer emulation needed */
479 tmr_8253[chan].latch = dummy_ctr;
481 break;
482 case 3: /* write lo byte, then hi byte */
483 tmr_8253[chan].byte_toggle = FALSE; /* init */
484 /* fall through */
485 case 1: /* write lo byte only */
486 case 2: /* write hi byte only */
487 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
488 break;
491 break;
492 case 0x61:
493 parport_8255[1] = (BYTE)value;
494 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
496 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
497 Beep(1193180 / tmr_8253[2].countmax, 20);
499 break;
500 case 0x70:
501 cmosaddress = (BYTE)value & 0x7f;
502 break;
503 case 0x71:
504 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
505 break;
506 case 0x3c8:
507 case 0x3c9:
508 VGA_ioport_out( port, (BYTE)value );
509 break;
510 default:
511 WARN("Direct I/O write attempted to port %x\n", port );
512 break;