Implemented the Win95 look and feel.
[wine.git] / msdos / ioports.c
blob49a9f1046920f42a1098f9d3e0d0b299e1484b5c
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 "debug.h"
25 #include "miscemu.h"
27 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
28 /* if byte_toggle is TRUE, then hi byte has already been written */
29 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
30 static WORD tmr_8253_latch[3] = {0, 0, 0};
32 /* 4th contents are dummy */
33 static BOOL16 tmr_8253_latched[4] = {FALSE, FALSE, FALSE, FALSE};
34 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
36 static int dummy_ctr = 0;
38 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
40 static BYTE cmosaddress;
42 static BYTE cmosimage[64] =
44 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
45 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
46 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
47 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
48 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
50 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
51 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
54 #if defined(linux) && defined(__i386__)
55 # define DIRECT_IO_ACCESS
56 #else
57 # undef DIRECT_IO_ACCESS
58 #endif /* linux && __i386__ */
60 #ifdef DIRECT_IO_ACCESS
62 extern int iopl(int level);
64 static char do_direct_port_access = 0;
65 static char port_permissions[0x10000];
67 #define IO_READ 1
68 #define IO_WRITE 2
70 #endif /* DIRECT_IO_ACCESS */
72 static void IO_FixCMOSCheckSum(void)
74 WORD sum = 0;
75 int i;
77 for (i=0x10; i < 0x2d; i++)
78 sum += cmosimage[i];
79 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
80 cmosimage[0x2f] = sum & 0xff;
81 TRACE(int, "calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
84 static void set_timer_maxval(unsigned timer, unsigned maxval)
86 switch (timer) {
87 case 0: /* System timer counter divisor */
88 DOSVM_SetTimer(maxval);
89 break;
90 case 1: /* RAM refresh */
91 FIXME(int, "RAM refresh counter handling not implemented !");
92 break;
93 case 2: /* cassette & speaker */
94 /* speaker on ? */
95 if (((BYTE)parport_8255[1] & 3) == 3)
97 TRACE(int, "Beep (freq: %d) !\n", 1193180 / maxval );
98 Beep(1193180 / maxval, 20);
100 break;
104 /**********************************************************************
105 * IO_port_init
108 /* set_IO_permissions(int val1, int val)
109 * Helper function for IO_port_init
111 #ifdef DIRECT_IO_ACCESS
112 static void set_IO_permissions(int val1, int val, char rw)
114 int j;
115 if (val1 != -1) {
116 if (val == -1) val = 0x3ff;
117 for (j = val1; j <= val; j++)
118 port_permissions[j] |= rw;
120 do_direct_port_access = 1;
122 val1 = -1;
123 } else if (val != -1) {
124 do_direct_port_access = 1;
126 port_permissions[val] |= rw;
131 /* do_IO_port_init_read_or_write(char* temp, char rw)
132 * Helper function for IO_port_init
135 static void do_IO_port_init_read_or_write(char* temp, char rw)
137 int val, val1, i, len;
138 if (!strcasecmp(temp, "all")) {
139 MSG("Warning!!! Granting FULL IO port access to"
140 " windoze programs!\nWarning!!! "
141 "*** THIS IS NOT AT ALL "
142 "RECOMMENDED!!! ***\n");
143 for (i=0; i < sizeof(port_permissions); i++)
144 port_permissions[i] |= rw;
146 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
147 len = strlen(temp);
148 val = -1;
149 val1 = -1;
150 for (i = 0; i < len; i++) {
151 switch (temp[i]) {
152 case '0':
153 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
154 sscanf(temp+i, "%x", &val);
155 i += 2;
156 } else {
157 sscanf(temp+i, "%d", &val);
159 while (isxdigit(temp[i]))
160 i++;
161 i--;
162 break;
163 case ',':
164 case ' ':
165 case '\t':
166 set_IO_permissions(val1, val, rw);
167 val1 = -1; val = -1;
168 break;
169 case '-':
170 val1 = val;
171 if (val1 == -1) val1 = 0;
172 break;
173 default:
174 if (temp[i] >= '0' && temp[i] <= '9') {
175 sscanf(temp+i, "%d", &val);
176 while (isdigit(temp[i]))
177 i++;
181 set_IO_permissions(val1, val, rw);
185 static __inline__ BYTE inb( WORD port )
187 BYTE b;
188 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
189 return b;
192 static __inline__ WORD inw( WORD port )
194 WORD w;
195 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
196 return w;
199 static __inline__ DWORD inl( WORD port )
201 DWORD dw;
202 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
203 return dw;
206 static __inline__ void outb( BYTE value, WORD port )
208 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
211 static __inline__ void outw( WORD value, WORD port )
213 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
216 static __inline__ void outl( DWORD value, WORD port )
218 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
221 #endif /* DIRECT_IO_ACCESS */
223 void IO_port_init()
225 #ifdef DIRECT_IO_ACCESS
226 char temp[1024];
228 /* Can we do that? */
229 if (!iopl(3)) {
230 iopl(0);
232 PROFILE_GetWineIniString( "ports", "read", "*",
233 temp, sizeof(temp) );
234 do_IO_port_init_read_or_write(temp, IO_READ);
235 PROFILE_GetWineIniString( "ports", "write", "*",
236 temp, sizeof(temp) );
237 do_IO_port_init_read_or_write(temp, IO_WRITE);
239 #endif /* DIRECT_IO_ACCESS */
240 IO_FixCMOSCheckSum();
244 /**********************************************************************
245 * IO_inport
247 * Note: The size argument has to be handled correctly _externally_
248 * (as we always return a DWORD)
250 DWORD IO_inport( int port, int size )
252 DWORD res = 0;
254 TRACE(int, "%d-byte value from port 0x%02x\n", size, port );
256 #ifdef DIRECT_IO_ACCESS
257 if ((do_direct_port_access)
258 /* Make sure we have access to the port */
259 && (port_permissions[port] & IO_READ))
261 iopl(3);
262 switch(size)
264 case 1: res = inb( port ); break;
265 case 2: res = inw( port ); break;
266 case 4: res = inl( port ); break;
267 default:
268 ERR(int, "invalid data size %d\n", size);
270 iopl(0);
271 return res;
273 #endif
275 switch (port)
277 case 0x3ba:
278 case 0x3da:
279 res = (DWORD)VGA_ioport_in( port );
280 break;
281 case 0x40:
282 case 0x41:
283 case 0x42:
285 BYTE chan = port & 3;
286 WORD tempval = 0;
288 if (tmr_8253_latched[chan] == TRUE)
290 tempval = tmr_8253_latch[chan];
291 tmr_8253_latched[chan] = FALSE;
293 else
295 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
296 if (chan == 0) /* System timer counter divisor */
297 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
298 tempval = dummy_ctr+(WORD)DOSVM_GetTimer();
299 else
300 { /* FIXME: intelligent hardware timer emulation needed */
301 tempval = dummy_ctr;
304 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
306 case 0x00:
307 break; /* correct ? */
308 case 0x10: /* read lo byte */
309 res = (BYTE)tempval;
310 break;
311 case 0x30: /* read lo byte, then hi byte */
312 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
313 if (tmr_8253_byte_toggle[chan] == TRUE)
315 res = (BYTE)tempval;
316 break;
318 /* else [fall through if read hi byte !] */
319 case 0x20: /* read hi byte */
320 res = (BYTE)tempval>>8;
321 break;
323 break;
325 case 0x60:
326 res = INT_Int09ReadScan();
327 #if 0 /* what's this port got to do with parport ? */
328 res = (DWORD)parport_8255[0];
329 #endif
330 break;
331 case 0x61:
332 res = (DWORD)parport_8255[1];
333 break;
334 case 0x62:
335 res = (DWORD)parport_8255[2];
336 break;
337 case 0x70:
338 res = (DWORD)cmosaddress;
339 break;
340 case 0x71:
341 res = (DWORD)cmosimage[cmosaddress & 0x3f];
342 break;
343 case 0x200:
344 case 0x201:
345 res = 0xffffffff; /* no joystick */
346 break;
347 default:
348 WARN( int, "Direct I/O read attempted from port %x\n", port);
349 res = 0xffffffff;
350 break;
352 TRACE(int, " returning ( 0x%lx )\n", res );
353 return res;
357 /**********************************************************************
358 * IO_outport
360 void IO_outport( int port, int size, DWORD value )
362 TRACE(int, "IO: 0x%lx (%d-byte value) to port 0x%02x\n",
363 value, size, port );
365 #ifdef DIRECT_IO_ACCESS
366 if ((do_direct_port_access)
367 /* Make sure we have access to the port */
368 && (port_permissions[port] & IO_WRITE))
370 iopl(3);
371 switch(size)
373 case 1: outb( LOBYTE(value), port ); break;
374 case 2: outw( LOWORD(value), port ); break;
375 case 4: outl( value, port ); break;
376 default:
377 WARN(int, "Invalid data size %d\n", size);
379 iopl(0);
380 return;
382 #endif
384 switch (port)
386 case 0x3c8:
387 case 0x3c9:
388 VGA_ioport_out( port, (BYTE)value );
389 break;
390 case 0x20:
391 DOSVM_PIC_ioport_out( port, (BYTE)value );
392 break;
393 case 0x40:
394 case 0x41:
395 case 0x42:
397 BYTE chan = port & 3;
398 WORD oldval = 0;
400 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
401 /* we need to get the oldval before any lo/hi byte change has been made */
402 (tmr_8253_byte_toggle[chan] == FALSE) )
403 oldval = tmr_8253_countmax[chan];
404 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
406 case 0x00:
407 break; /* correct ? */
408 case 0x10: /* write lo byte */
409 tmr_8253_countmax[chan] =
410 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
411 break;
412 case 0x30: /* write lo byte, then hi byte */
413 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
414 if (tmr_8253_byte_toggle[chan] == TRUE)
416 tmr_8253_countmax[chan] =
417 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
418 break;
420 /* else [fall through if write hi byte !] */
421 case 0x20: /* write hi byte */
422 tmr_8253_countmax[chan] =
423 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
424 break;
426 if (
427 /* programming finished ? */
428 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
429 (tmr_8253_byte_toggle[chan] == FALSE) )
430 /* update to new value ? */
431 && (tmr_8253_countmax[chan] != oldval)
433 set_timer_maxval(chan, tmr_8253_countmax[chan]);
435 break;
436 case 0x43:
438 BYTE chan = ((BYTE)value & 0xc0) >> 6;
440 /* ctrl byte for specific timer channel */
441 tmr_8253_ctrlbyte_ch[chan] = (BYTE)value;
442 if (chan == 3) {
443 FIXME(int,"8254 timer readback not implemented yet\n");
445 else
446 if (((BYTE)value&0x30)==0) { /* latch timer */
447 tmr_8253_latched[chan] = TRUE;
448 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
449 if (chan == 0) /* System timer divisor */
450 tmr_8253_latch[0] = dummy_ctr+(WORD)DOSVM_GetTimer();
451 else
452 { /* FIXME: intelligent hardware timer emulation needed */
453 tmr_8253_latch[chan] = dummy_ctr;
456 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
457 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
459 break;
460 case 0x61:
461 parport_8255[1] = (BYTE)value;
462 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
464 TRACE(int, "Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
465 Beep(1193180 / tmr_8253_countmax[2], 20);
467 break;
468 case 0x70:
469 cmosaddress = (BYTE)value & 0x7f;
470 break;
471 case 0x71:
472 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
473 break;
474 default:
475 WARN(int, "Direct I/O write attempted to port %x\n", port );
476 break;