LCMapString32A: Implemented flags NORM_IGNORENONSPACE and
[wine.git] / msdos / ioports.c
blob641320e57886ecc8aaf7ae5b9277d5d70be53d67
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 "windows.h"
20 #include "vga.h"
21 #include "dosexe.h"
22 #include "options.h"
23 #include "debug.h"
25 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
26 /* if byte_toggle is TRUE, then hi byte has already been written */
27 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
28 static WORD tmr_8253_latch[3] = {0, 0, 0};
30 /* 4th contents are dummy */
31 static BOOL16 tmr_8253_latched[4] = {FALSE, FALSE, FALSE, FALSE};
32 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
34 static int dummy_ctr = 0;
36 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
38 static BYTE cmosaddress;
40 static BYTE cmosimage[64] =
42 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
43 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
44 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
45 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
46 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
48 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
49 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
52 #if defined(linux) && defined(__i386__)
53 # define DIRECT_IO_ACCESS
54 #else
55 # undef DIRECT_IO_ACCESS
56 #endif /* linux && __i386__ */
58 #ifdef DIRECT_IO_ACCESS
60 extern int iopl(int level);
62 static char do_direct_port_access = 0;
63 static char port_permissions[0x10000];
65 #define IO_READ 1
66 #define IO_WRITE 2
68 #endif /* DIRECT_IO_ACCESS */
70 static void IO_FixCMOSCheckSum(void)
72 WORD sum = 0;
73 int i;
75 for (i=0x10; i < 0x2d; i++)
76 sum += cmosimage[i];
77 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
78 cmosimage[0x2f] = sum & 0xff;
79 TRACE(int, "calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
82 static void set_timer_maxval(unsigned timer, unsigned maxval)
84 switch (timer) {
85 case 0: /* System timer counter divisor */
86 DOSVM_SetTimer(maxval);
87 break;
88 case 1: /* RAM refresh */
89 FIXME(int, "RAM refresh counter handling not implemented !");
90 break;
91 case 2: /* cassette & speaker */
92 /* speaker on ? */
93 if (((BYTE)parport_8255[1] & 3) == 3)
95 TRACE(int, "Beep (freq: %d) !\n", 1193180 / maxval );
96 Beep(1193180 / maxval, 20);
98 break;
102 /**********************************************************************
103 * IO_port_init
106 /* set_IO_permissions(int val1, int val)
107 * Helper function for IO_port_init
109 #ifdef DIRECT_IO_ACCESS
110 static void set_IO_permissions(int val1, int val, char rw)
112 int j;
113 if (val1 != -1) {
114 if (val == -1) val = 0x3ff;
115 for (j = val1; j <= val; j++)
116 port_permissions[j] |= rw;
118 do_direct_port_access = 1;
120 val1 = -1;
121 } else if (val != -1) {
122 do_direct_port_access = 1;
124 port_permissions[val] |= rw;
129 /* do_IO_port_init_read_or_write(char* temp, char rw)
130 * Helper function for IO_port_init
133 static void do_IO_port_init_read_or_write(char* temp, char rw)
135 int val, val1, i, len;
136 if (!strcasecmp(temp, "all")) {
137 MSG("Warning!!! Granting FULL IO port access to"
138 " windoze programs!\nWarning!!! "
139 "*** THIS IS NOT AT ALL "
140 "RECOMMENDED!!! ***\n");
141 for (i=0; i < sizeof(port_permissions); i++)
142 port_permissions[i] |= rw;
144 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
145 len = strlen(temp);
146 val = -1;
147 val1 = -1;
148 for (i = 0; i < len; i++) {
149 switch (temp[i]) {
150 case '0':
151 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
152 sscanf(temp+i, "%x", &val);
153 i += 2;
154 } else {
155 sscanf(temp+i, "%d", &val);
157 while (isxdigit(temp[i]))
158 i++;
159 i--;
160 break;
161 case ',':
162 case ' ':
163 case '\t':
164 set_IO_permissions(val1, val, rw);
165 val1 = -1; val = -1;
166 break;
167 case '-':
168 val1 = val;
169 if (val1 == -1) val1 = 0;
170 break;
171 default:
172 if (temp[i] >= '0' && temp[i] <= '9') {
173 sscanf(temp+i, "%d", &val);
174 while (isdigit(temp[i]))
175 i++;
179 set_IO_permissions(val1, val, rw);
183 static __inline__ BYTE inb( WORD port )
185 BYTE b;
186 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
187 return b;
190 static __inline__ WORD inw( WORD port )
192 WORD w;
193 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
194 return w;
197 static __inline__ DWORD inl( WORD port )
199 DWORD dw;
200 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
201 return dw;
204 static __inline__ void outb( BYTE value, WORD port )
206 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
209 static __inline__ void outw( WORD value, WORD port )
211 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
214 static __inline__ void outl( DWORD value, WORD port )
216 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
219 #endif /* DIRECT_IO_ACCESS */
221 void IO_port_init()
223 #ifdef DIRECT_IO_ACCESS
224 char temp[1024];
226 /* Can we do that? */
227 if (!iopl(3)) {
228 iopl(0);
230 PROFILE_GetWineIniString( "ports", "read", "*",
231 temp, sizeof(temp) );
232 do_IO_port_init_read_or_write(temp, IO_READ);
233 PROFILE_GetWineIniString( "ports", "write", "*",
234 temp, sizeof(temp) );
235 do_IO_port_init_read_or_write(temp, IO_WRITE);
237 #endif /* DIRECT_IO_ACCESS */
238 IO_FixCMOSCheckSum();
242 /**********************************************************************
243 * IO_inport
245 * Note: The size argument has to be handled correctly _externally_
246 * (as we always return a DWORD)
248 DWORD IO_inport( int port, int size )
250 DWORD res = 0;
252 TRACE(int, "%d-byte value from port 0x%02x\n", size, port );
254 #ifdef DIRECT_IO_ACCESS
255 if ((do_direct_port_access)
256 /* Make sure we have access to the port */
257 && (port_permissions[port] & IO_READ))
259 iopl(3);
260 switch(size)
262 case 1: res = inb( port ); break;
263 case 2: res = inw( port ); break;
264 case 4: res = inl( port ); break;
265 default:
266 ERR(int, "invalid data size %d\n", size);
268 iopl(0);
269 return res;
271 #endif
273 switch (port)
275 case 0x40:
276 case 0x41:
277 case 0x42:
279 BYTE chan = port & 3;
280 WORD tempval = 0;
282 if (tmr_8253_latched[chan] == TRUE)
284 tempval = tmr_8253_latch[chan];
285 tmr_8253_latched[chan] = FALSE;
287 else
289 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
290 if (chan == 0) /* System timer counter divisor */
291 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
292 tempval = dummy_ctr+(WORD)DOSVM_GetTimer();
293 else
294 { /* FIXME: intelligent hardware timer emulation needed */
295 tempval = dummy_ctr;
298 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
300 case 0x00:
301 break; /* correct ? */
302 case 0x10: /* read lo byte */
303 res = (BYTE)tempval;
304 break;
305 case 0x30: /* read lo byte, then hi byte */
306 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
307 if (tmr_8253_byte_toggle[chan] == TRUE)
309 res = (BYTE)tempval;
310 break;
312 /* else [fall through if read hi byte !] */
313 case 0x20: /* read hi byte */
314 res = (BYTE)tempval>>8;
315 break;
317 break;
319 case 0x60:
320 res = (DWORD)parport_8255[0];
321 break;
322 case 0x61:
323 res = (DWORD)parport_8255[1];
324 break;
325 case 0x62:
326 res = (DWORD)parport_8255[2];
327 break;
328 case 0x70:
329 res = (DWORD)cmosaddress;
330 break;
331 case 0x71:
332 res = (DWORD)cmosimage[cmosaddress & 0x3f];
333 break;
334 case 0x200:
335 case 0x201:
336 res = 0xffffffff; /* no joystick */
337 break;
338 case 0x3ba:
339 case 0x3da:
340 res = (DWORD)VGA_ioport_in( port );
341 break;
342 default:
343 WARN( int, "Direct I/O read attempted from port %x\n", port);
344 res = 0xffffffff;
345 break;
347 TRACE(int, " returning ( 0x%lx )\n", res );
348 return res;
352 /**********************************************************************
353 * IO_outport
355 void IO_outport( int port, int size, DWORD value )
357 TRACE(int, "IO: 0x%lx (%d-byte value) to port 0x%02x\n",
358 value, size, port );
360 #ifdef DIRECT_IO_ACCESS
361 if ((do_direct_port_access)
362 /* Make sure we have access to the port */
363 && (port_permissions[port] & IO_WRITE))
365 iopl(3);
366 switch(size)
368 case 1: outb( LOBYTE(value), port ); break;
369 case 2: outw( LOWORD(value), port ); break;
370 case 4: outl( value, port ); break;
371 default:
372 WARN(int, "Invalid data size %d\n", size);
374 iopl(0);
375 return;
377 #endif
379 switch (port)
381 case 0x40:
382 case 0x41:
383 case 0x42:
385 BYTE chan = port & 3;
386 WORD oldval = 0;
388 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
389 /* we need to get the oldval before any lo/hi byte change has been made */
390 (tmr_8253_byte_toggle[chan] == FALSE) )
391 oldval = tmr_8253_countmax[chan];
392 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
394 case 0x00:
395 break; /* correct ? */
396 case 0x10: /* write lo byte */
397 tmr_8253_countmax[chan] =
398 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
399 break;
400 case 0x30: /* write lo byte, then hi byte */
401 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
402 if (tmr_8253_byte_toggle[chan] == TRUE)
404 tmr_8253_countmax[chan] =
405 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
406 break;
408 /* else [fall through if write hi byte !] */
409 case 0x20: /* write hi byte */
410 tmr_8253_countmax[chan] =
411 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
412 break;
414 if (
415 /* programming finished ? */
416 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
417 (tmr_8253_byte_toggle[chan] == FALSE) )
418 /* update to new value ? */
419 && (tmr_8253_countmax[chan] != oldval)
421 set_timer_maxval(chan, tmr_8253_countmax[chan]);
423 break;
424 case 0x43:
426 BYTE chan = ((BYTE)value & 0xc0) >> 6;
428 /* ctrl byte for specific timer channel */
429 tmr_8253_ctrlbyte_ch[chan] = (BYTE)value;
430 if (chan == 3) {
431 FIXME(int,"8254 timer readback not implemented yet\n");
433 else
434 if (((BYTE)value&0x30)==0) { /* latch timer */
435 tmr_8253_latched[chan] = TRUE;
436 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
437 if (chan == 0) /* System timer divisor */
438 tmr_8253_latch[0] = dummy_ctr+(WORD)DOSVM_GetTimer();
439 else
440 { /* FIXME: intelligent hardware timer emulation needed */
441 tmr_8253_latch[chan] = dummy_ctr;
444 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
445 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
447 break;
448 case 0x61:
449 parport_8255[1] = (BYTE)value;
450 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
452 TRACE(int, "Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
453 Beep(1193180 / tmr_8253_countmax[2], 20);
455 break;
456 case 0x70:
457 cmosaddress = (BYTE)value & 0x7f;
458 break;
459 case 0x71:
460 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
461 break;
462 case 0x3c8:
463 case 0x3c9:
464 VGA_ioport_out( port, (BYTE)value );
465 break;
466 default:
467 WARN(int, "Direct I/O write attempted to port %x\n", port );
468 break;