Added stub for DCIOpenProvider.
[wine.git] / msdos / ioports.c
blobd72651d5088311e9715bff1fb182cef1caed1b8d
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 <ctype.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "windef.h"
21 #include "vga.h"
22 #include "dosexe.h"
23 #include "options.h"
24 #include "miscemu.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(int)
29 static struct {
30 WORD countmax;
31 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
32 WORD latch;
33 BOOL16 latched;
34 BYTE ctrlbyte_ch;
35 WORD oldval;
36 } tmr_8253[3] = {
37 {0xFFFF, FALSE, 0, FALSE, 0x06, 0},
38 {0x0012, FALSE, 0, FALSE, 0x44, 0},
39 {0x0001, FALSE, 0, FALSE, 0x86, 0},
42 static int dummy_ctr = 0;
44 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
46 static BYTE cmosaddress;
48 static BYTE cmosimage[64] =
50 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
51 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
52 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
53 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
54 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
56 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
57 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
60 #if defined(linux) && defined(__i386__)
61 # define DIRECT_IO_ACCESS
62 #else
63 # undef DIRECT_IO_ACCESS
64 #endif /* linux && __i386__ */
66 #ifdef DIRECT_IO_ACCESS
68 extern int iopl(int level);
70 static char do_direct_port_access = -1;
71 static char port_permissions[0x10000];
73 #define IO_READ 1
74 #define IO_WRITE 2
76 static void IO_FixCMOSCheckSum(void)
78 WORD sum = 0;
79 int i;
81 for (i=0x10; i < 0x2d; i++)
82 sum += cmosimage[i];
83 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
84 cmosimage[0x2f] = sum & 0xff;
85 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
88 #endif /* DIRECT_IO_ACCESS */
90 static void set_timer_maxval(unsigned timer, unsigned maxval)
92 switch (timer) {
93 case 0: /* System timer counter divisor */
94 DOSVM_SetTimer(maxval);
95 break;
96 case 1: /* RAM refresh */
97 FIXME("RAM refresh counter handling not implemented !");
98 break;
99 case 2: /* cassette & speaker */
100 /* speaker on ? */
101 if (((BYTE)parport_8255[1] & 3) == 3)
103 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
104 Beep(1193180 / maxval, 20);
106 break;
110 /**********************************************************************
111 * IO_port_init
114 /* set_IO_permissions(int val1, int val)
115 * Helper function for IO_port_init
117 #ifdef DIRECT_IO_ACCESS
118 static void set_IO_permissions(int val1, int val, char rw)
120 int j;
121 if (val1 != -1) {
122 if (val == -1) val = 0x3ff;
123 for (j = val1; j <= val; j++)
124 port_permissions[j] |= rw;
126 do_direct_port_access = 1;
128 val1 = -1;
129 } else if (val != -1) {
130 do_direct_port_access = 1;
132 port_permissions[val] |= rw;
137 /* do_IO_port_init_read_or_write(char* temp, char rw)
138 * Helper function for IO_port_init
141 static void do_IO_port_init_read_or_write(char* temp, char rw)
143 int val, val1, i, len;
144 if (!strcasecmp(temp, "all")) {
145 MESSAGE("Warning!!! Granting FULL IO port access to"
146 " windoze programs!\nWarning!!! "
147 "*** THIS IS NOT AT ALL "
148 "RECOMMENDED!!! ***\n");
149 for (i=0; i < sizeof(port_permissions); i++)
150 port_permissions[i] |= rw;
152 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
153 len = strlen(temp);
154 val = -1;
155 val1 = -1;
156 for (i = 0; i < len; i++) {
157 switch (temp[i]) {
158 case '0':
159 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
160 sscanf(temp+i, "%x", &val);
161 i += 2;
162 } else {
163 sscanf(temp+i, "%d", &val);
165 while (isxdigit(temp[i]))
166 i++;
167 i--;
168 break;
169 case ',':
170 case ' ':
171 case '\t':
172 set_IO_permissions(val1, val, rw);
173 val1 = -1; val = -1;
174 break;
175 case '-':
176 val1 = val;
177 if (val1 == -1) val1 = 0;
178 break;
179 default:
180 if (temp[i] >= '0' && temp[i] <= '9') {
181 sscanf(temp+i, "%d", &val);
182 while (isdigit(temp[i]))
183 i++;
187 set_IO_permissions(val1, val, rw);
191 static inline BYTE inb( WORD port )
193 BYTE b;
194 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
195 return b;
198 static inline WORD inw( WORD port )
200 WORD w;
201 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
202 return w;
205 static inline DWORD inl( WORD port )
207 DWORD dw;
208 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
209 return dw;
212 static inline void outb( BYTE value, WORD port )
214 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
217 static inline void outw( WORD value, WORD port )
219 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
222 static inline void outl( DWORD value, WORD port )
224 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
227 static void IO_port_init(void)
229 char temp[1024];
231 do_direct_port_access = 0;
232 /* Can we do that? */
233 if (!iopl(3)) {
234 iopl(0);
236 PROFILE_GetWineIniString( "ports", "read", "*",
237 temp, sizeof(temp) );
238 do_IO_port_init_read_or_write(temp, IO_READ);
239 PROFILE_GetWineIniString( "ports", "write", "*",
240 temp, sizeof(temp) );
241 do_IO_port_init_read_or_write(temp, IO_WRITE);
243 IO_FixCMOSCheckSum();
246 #endif /* DIRECT_IO_ACCESS */
248 /**********************************************************************
249 * IO_inport
251 * Note: The size argument has to be handled correctly _externally_
252 * (as we always return a DWORD)
254 DWORD IO_inport( int port, int size )
256 DWORD res = 0;
258 TRACE("%d-byte value from port 0x%02x\n", size, port );
260 #ifdef DIRECT_IO_ACCESS
261 if (do_direct_port_access == -1) IO_port_init();
262 if ((do_direct_port_access)
263 /* Make sure we have access to the port */
264 && (port_permissions[port] & IO_READ))
266 iopl(3);
267 switch(size)
269 case 1: res = inb( port ); break;
270 case 2: res = inw( port ); break;
271 case 4: res = inl( port ); break;
272 default:
273 ERR("invalid data size %d\n", size);
275 iopl(0);
276 return res;
278 #endif
280 switch (port)
282 case 0x40:
283 case 0x41:
284 case 0x42:
286 BYTE chan = port & 3;
287 WORD tempval = 0;
288 if (tmr_8253[chan].latched)
289 tempval = tmr_8253[chan].latch;
290 else
292 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
293 if (chan == 0) /* System timer counter divisor */
295 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
296 tempval = dummy_ctr + (WORD)DOSVM_GetTimer();
298 else
300 /* FIXME: intelligent hardware timer emulation needed */
301 tempval = dummy_ctr;
305 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
307 case 0:
308 res = 0; /* shouldn't happen? */
309 break;
310 case 1: /* read lo byte */
311 res = (BYTE)tempval;
312 tmr_8253[chan].latched = FALSE;
313 break;
314 case 3: /* read lo byte, then hi byte */
315 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
316 if (tmr_8253[chan].byte_toggle)
318 res = (BYTE)tempval;
319 break;
321 /* else [fall through if read hi byte !] */
322 case 2: /* read hi byte */
323 res = (BYTE)(tempval >> 8);
324 tmr_8253[chan].latched = FALSE;
325 break;
328 break;
329 case 0x60:
330 res = INT_Int09ReadScan(NULL);
331 #if 0 /* what's this port got to do with parport ? */
332 res = (DWORD)parport_8255[0];
333 #endif
334 break;
335 case 0x61:
336 res = (DWORD)parport_8255[1];
337 break;
338 case 0x62:
339 res = (DWORD)parport_8255[2];
340 break;
341 case 0x70:
342 res = (DWORD)cmosaddress;
343 break;
344 case 0x71:
345 res = (DWORD)cmosimage[cmosaddress & 0x3f];
346 break;
347 case 0x200:
348 case 0x201:
349 res = 0xffffffff; /* no joystick */
350 break;
351 case 0x3ba:
352 case 0x3da:
353 res = (DWORD)VGA_ioport_in( port );
354 break;
355 default:
356 WARN("Direct I/O read attempted from port %x\n", port);
357 res = 0xffffffff;
358 break;
360 TRACE(" returning ( 0x%lx )\n", res );
361 return res;
365 /**********************************************************************
366 * IO_outport
368 void IO_outport( int port, int size, DWORD value )
370 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
371 value, size, port );
373 #ifdef DIRECT_IO_ACCESS
374 if (do_direct_port_access == -1) IO_port_init();
375 if ((do_direct_port_access)
376 /* Make sure we have access to the port */
377 && (port_permissions[port] & IO_WRITE))
379 iopl(3);
380 switch(size)
382 case 1: outb( LOBYTE(value), port ); break;
383 case 2: outw( LOWORD(value), port ); break;
384 case 4: outl( value, port ); break;
385 default:
386 WARN("Invalid data size %d\n", size);
388 iopl(0);
389 return;
391 #endif
393 switch (port)
395 case 0x20:
396 DOSVM_PIC_ioport_out( port, (BYTE)value );
397 break;
398 case 0x40:
399 case 0x41:
400 case 0x42:
402 BYTE chan = port & 3;
404 /* we need to get the oldval before any lo/hi byte change has been made */
405 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
406 !tmr_8253[chan].byte_toggle)
407 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
408 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
410 case 0:
411 break; /* shouldn't happen? */
412 case 1: /* write lo byte */
413 tmr_8253[chan].countmax =
414 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
415 break;
416 case 3: /* write lo byte, then hi byte */
417 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
418 if (tmr_8253[chan].byte_toggle)
420 tmr_8253[chan].countmax =
421 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
422 break;
424 /* else [fall through if write hi byte !] */
425 case 2: /* write hi byte */
426 tmr_8253[chan].countmax =
427 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
428 break;
430 /* if programming is finished and value has changed
431 then update to new value */
432 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
433 !tmr_8253[chan].byte_toggle) &&
434 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
435 set_timer_maxval(chan, tmr_8253[chan].countmax);
437 break;
438 case 0x43:
440 BYTE chan = ((BYTE)value & 0xc0) >> 6;
441 /* ctrl byte for specific timer channel */
442 if (chan == 3)
444 FIXME("8254 timer readback not implemented yet\n");
445 break;
447 switch (((BYTE)value & 0x30) >> 4)
449 case 0: /* latch timer */
450 tmr_8253[chan].latched = TRUE;
451 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
452 if (chan == 0) /* System timer divisor */
453 tmr_8253[chan].latch = dummy_ctr + (WORD)DOSVM_GetTimer();
454 else
456 /* FIXME: intelligent hardware timer emulation needed */
457 tmr_8253[chan].latch = dummy_ctr;
459 break;
460 case 3: /* write lo byte, then hi byte */
461 tmr_8253[chan].byte_toggle = FALSE; /* init */
462 /* fall through */
463 case 1: /* write lo byte only */
464 case 2: /* write hi byte only */
465 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
466 break;
469 break;
470 case 0x61:
471 parport_8255[1] = (BYTE)value;
472 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
474 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
475 Beep(1193180 / tmr_8253[2].countmax, 20);
477 break;
478 case 0x70:
479 cmosaddress = (BYTE)value & 0x7f;
480 break;
481 case 0x71:
482 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
483 break;
484 case 0x3c8:
485 case 0x3c9:
486 VGA_ioport_out( port, (BYTE)value );
487 break;
488 default:
489 WARN("Direct I/O write attempted to port %x\n", port );
490 break;