Fixed 32-bit module name handling to conform better to Win9x.
[wine.git] / msdos / ioports.c
blob29e2329c5502400901a7f9ca4167be013bef4c5c
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 <string.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include "windef.h"
20 #include "vga.h"
21 #include "dosexe.h"
22 #include "options.h"
23 #include "miscemu.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(int)
28 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
29 /* if byte_toggle is TRUE, then hi byte has already been written */
30 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
31 static WORD tmr_8253_latch[3] = {0, 0, 0};
33 /* 4th contents are dummy */
34 static BOOL16 tmr_8253_latched[4] = {FALSE, FALSE, FALSE, FALSE};
35 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
37 static int dummy_ctr = 0;
39 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
41 static BYTE cmosaddress;
43 static BYTE cmosimage[64] =
45 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
46 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
47 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
48 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
49 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
51 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
52 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
55 #if defined(linux) && defined(__i386__)
56 # define DIRECT_IO_ACCESS
57 #else
58 # undef DIRECT_IO_ACCESS
59 #endif /* linux && __i386__ */
61 #ifdef DIRECT_IO_ACCESS
63 extern int iopl(int level);
65 static char do_direct_port_access = 0;
66 static char port_permissions[0x10000];
68 #define IO_READ 1
69 #define IO_WRITE 2
71 #endif /* DIRECT_IO_ACCESS */
73 static void IO_FixCMOSCheckSum(void)
75 WORD sum = 0;
76 int i;
78 for (i=0x10; i < 0x2d; i++)
79 sum += cmosimage[i];
80 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
81 cmosimage[0x2f] = sum & 0xff;
82 TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
85 static void set_timer_maxval(unsigned timer, unsigned maxval)
87 switch (timer) {
88 case 0: /* System timer counter divisor */
89 DOSVM_SetTimer(maxval);
90 break;
91 case 1: /* RAM refresh */
92 FIXME("RAM refresh counter handling not implemented !");
93 break;
94 case 2: /* cassette & speaker */
95 /* speaker on ? */
96 if (((BYTE)parport_8255[1] & 3) == 3)
98 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
99 Beep(1193180 / maxval, 20);
101 break;
105 /**********************************************************************
106 * IO_port_init
109 /* set_IO_permissions(int val1, int val)
110 * Helper function for IO_port_init
112 #ifdef DIRECT_IO_ACCESS
113 static void set_IO_permissions(int val1, int val, char rw)
115 int j;
116 if (val1 != -1) {
117 if (val == -1) val = 0x3ff;
118 for (j = val1; j <= val; j++)
119 port_permissions[j] |= rw;
121 do_direct_port_access = 1;
123 val1 = -1;
124 } else if (val != -1) {
125 do_direct_port_access = 1;
127 port_permissions[val] |= rw;
132 /* do_IO_port_init_read_or_write(char* temp, char rw)
133 * Helper function for IO_port_init
136 static void do_IO_port_init_read_or_write(char* temp, char rw)
138 int val, val1, i, len;
139 if (!strcasecmp(temp, "all")) {
140 MESSAGE("Warning!!! Granting FULL IO port access to"
141 " windoze programs!\nWarning!!! "
142 "*** THIS IS NOT AT ALL "
143 "RECOMMENDED!!! ***\n");
144 for (i=0; i < sizeof(port_permissions); i++)
145 port_permissions[i] |= rw;
147 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
148 len = strlen(temp);
149 val = -1;
150 val1 = -1;
151 for (i = 0; i < len; i++) {
152 switch (temp[i]) {
153 case '0':
154 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
155 sscanf(temp+i, "%x", &val);
156 i += 2;
157 } else {
158 sscanf(temp+i, "%d", &val);
160 while (isxdigit(temp[i]))
161 i++;
162 i--;
163 break;
164 case ',':
165 case ' ':
166 case '\t':
167 set_IO_permissions(val1, val, rw);
168 val1 = -1; val = -1;
169 break;
170 case '-':
171 val1 = val;
172 if (val1 == -1) val1 = 0;
173 break;
174 default:
175 if (temp[i] >= '0' && temp[i] <= '9') {
176 sscanf(temp+i, "%d", &val);
177 while (isdigit(temp[i]))
178 i++;
182 set_IO_permissions(val1, val, rw);
186 static inline BYTE inb( WORD port )
188 BYTE b;
189 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
190 return b;
193 static inline WORD inw( WORD port )
195 WORD w;
196 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
197 return w;
200 static inline DWORD inl( WORD port )
202 DWORD dw;
203 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
204 return dw;
207 static inline void outb( BYTE value, WORD port )
209 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
212 static inline void outw( WORD value, WORD port )
214 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
217 static inline void outl( DWORD value, WORD port )
219 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
222 #endif /* DIRECT_IO_ACCESS */
224 void IO_port_init()
226 #ifdef DIRECT_IO_ACCESS
227 char temp[1024];
229 /* Can we do that? */
230 if (!iopl(3)) {
231 iopl(0);
233 PROFILE_GetWineIniString( "ports", "read", "*",
234 temp, sizeof(temp) );
235 do_IO_port_init_read_or_write(temp, IO_READ);
236 PROFILE_GetWineIniString( "ports", "write", "*",
237 temp, sizeof(temp) );
238 do_IO_port_init_read_or_write(temp, IO_WRITE);
240 #endif /* DIRECT_IO_ACCESS */
241 IO_FixCMOSCheckSum();
245 /**********************************************************************
246 * IO_inport
248 * Note: The size argument has to be handled correctly _externally_
249 * (as we always return a DWORD)
251 DWORD IO_inport( int port, int size )
253 DWORD res = 0;
255 TRACE("%d-byte value from port 0x%02x\n", size, port );
257 #ifdef DIRECT_IO_ACCESS
258 if ((do_direct_port_access)
259 /* Make sure we have access to the port */
260 && (port_permissions[port] & IO_READ))
262 iopl(3);
263 switch(size)
265 case 1: res = inb( port ); break;
266 case 2: res = inw( port ); break;
267 case 4: res = inl( port ); break;
268 default:
269 ERR("invalid data size %d\n", size);
271 iopl(0);
272 return res;
274 #endif
276 switch (port)
278 case 0x3ba:
279 case 0x3da:
280 res = (DWORD)VGA_ioport_in( port );
281 break;
282 case 0x40:
283 case 0x41:
284 case 0x42:
286 BYTE chan = port & 3;
287 WORD tempval = 0;
289 if (tmr_8253_latched[chan] == TRUE)
291 tempval = tmr_8253_latch[chan];
292 tmr_8253_latched[chan] = FALSE;
294 else
296 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
297 if (chan == 0) /* System timer counter divisor */
298 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
299 tempval = dummy_ctr+(WORD)DOSVM_GetTimer();
300 else
301 { /* FIXME: intelligent hardware timer emulation needed */
302 tempval = dummy_ctr;
305 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
307 case 0x00:
308 break; /* correct ? */
309 case 0x10: /* read lo byte */
310 res = (BYTE)tempval;
311 break;
312 case 0x30: /* read lo byte, then hi byte */
313 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
314 if (tmr_8253_byte_toggle[chan] == TRUE)
316 res = (BYTE)tempval;
317 break;
319 /* else [fall through if read hi byte !] */
320 case 0x20: /* read hi byte */
321 res = (BYTE)tempval>>8;
322 break;
324 break;
326 case 0x60:
327 res = INT_Int09ReadScan();
328 #if 0 /* what's this port got to do with parport ? */
329 res = (DWORD)parport_8255[0];
330 #endif
331 break;
332 case 0x61:
333 res = (DWORD)parport_8255[1];
334 break;
335 case 0x62:
336 res = (DWORD)parport_8255[2];
337 break;
338 case 0x70:
339 res = (DWORD)cmosaddress;
340 break;
341 case 0x71:
342 res = (DWORD)cmosimage[cmosaddress & 0x3f];
343 break;
344 case 0x200:
345 case 0x201:
346 res = 0xffffffff; /* no joystick */
347 break;
348 default:
349 WARN("Direct I/O read attempted from port %x\n", port);
350 res = 0xffffffff;
351 break;
353 TRACE(" returning ( 0x%lx )\n", res );
354 return res;
358 /**********************************************************************
359 * IO_outport
361 void IO_outport( int port, int size, DWORD value )
363 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
364 value, size, port );
366 #ifdef DIRECT_IO_ACCESS
367 if ((do_direct_port_access)
368 /* Make sure we have access to the port */
369 && (port_permissions[port] & IO_WRITE))
371 iopl(3);
372 switch(size)
374 case 1: outb( LOBYTE(value), port ); break;
375 case 2: outw( LOWORD(value), port ); break;
376 case 4: outl( value, port ); break;
377 default:
378 WARN("Invalid data size %d\n", size);
380 iopl(0);
381 return;
383 #endif
385 switch (port)
387 case 0x3c8:
388 case 0x3c9:
389 VGA_ioport_out( port, (BYTE)value );
390 break;
391 case 0x20:
392 DOSVM_PIC_ioport_out( port, (BYTE)value );
393 break;
394 case 0x40:
395 case 0x41:
396 case 0x42:
398 BYTE chan = port & 3;
399 WORD oldval = 0;
401 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
402 /* we need to get the oldval before any lo/hi byte change has been made */
403 (tmr_8253_byte_toggle[chan] == FALSE) )
404 oldval = tmr_8253_countmax[chan];
405 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
407 case 0x00:
408 break; /* correct ? */
409 case 0x10: /* write lo byte */
410 tmr_8253_countmax[chan] =
411 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
412 break;
413 case 0x30: /* write lo byte, then hi byte */
414 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
415 if (tmr_8253_byte_toggle[chan] == TRUE)
417 tmr_8253_countmax[chan] =
418 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
419 break;
421 /* else [fall through if write hi byte !] */
422 case 0x20: /* write hi byte */
423 tmr_8253_countmax[chan] =
424 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
425 break;
427 if (
428 /* programming finished ? */
429 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
430 (tmr_8253_byte_toggle[chan] == FALSE) )
431 /* update to new value ? */
432 && (tmr_8253_countmax[chan] != oldval)
434 set_timer_maxval(chan, tmr_8253_countmax[chan]);
436 break;
437 case 0x43:
439 BYTE chan = ((BYTE)value & 0xc0) >> 6;
441 /* ctrl byte for specific timer channel */
442 tmr_8253_ctrlbyte_ch[chan] = (BYTE)value;
443 if (chan == 3) {
444 FIXME("8254 timer readback not implemented yet\n");
446 else
447 if (((BYTE)value&0x30)==0) { /* latch timer */
448 tmr_8253_latched[chan] = TRUE;
449 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
450 if (chan == 0) /* System timer divisor */
451 tmr_8253_latch[0] = dummy_ctr+(WORD)DOSVM_GetTimer();
452 else
453 { /* FIXME: intelligent hardware timer emulation needed */
454 tmr_8253_latch[chan] = dummy_ctr;
457 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
458 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
460 break;
461 case 0x61:
462 parport_8255[1] = (BYTE)value;
463 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
465 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
466 Beep(1193180 / tmr_8253_countmax[2], 20);
468 break;
469 case 0x70:
470 cmosaddress = (BYTE)value & 0x7f;
471 break;
472 case 0x71:
473 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
474 break;
475 default:
476 WARN("Direct I/O write attempted to port %x\n", port );
477 break;