2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
9 - only a few ports are emulated.
10 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
26 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
27 /* if byte_toggle is TRUE, then hi byte has already been written */
28 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
29 static WORD tmr_8253_latch[3] = {0, 0, 0};
31 /* 4th contents are dummy */
32 static BOOL16 tmr_8253_latched[4] = {FALSE, FALSE, FALSE, FALSE};
33 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
35 static int dummy_ctr = 0;
37 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
39 static BYTE cmosaddress;
41 static BYTE cmosimage[64] =
43 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
44 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
45 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
46 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
47 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
49 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
50 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
53 #if defined(linux) && defined(__i386__)
54 # define DIRECT_IO_ACCESS
56 # undef DIRECT_IO_ACCESS
57 #endif /* linux && __i386__ */
59 #ifdef DIRECT_IO_ACCESS
61 extern int iopl(int level);
63 static char do_direct_port_access = 0;
64 static char port_permissions[0x10000];
69 #endif /* DIRECT_IO_ACCESS */
71 static void IO_FixCMOSCheckSum(void)
76 for (i=0x10; i < 0x2d; i++)
78 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
79 cmosimage[0x2f] = sum & 0xff;
80 TRACE(int, "calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
83 static void set_timer_maxval(unsigned timer, unsigned maxval)
86 case 0: /* System timer counter divisor */
87 DOSVM_SetTimer(maxval);
89 case 1: /* RAM refresh */
90 FIXME(int, "RAM refresh counter handling not implemented !");
92 case 2: /* cassette & speaker */
94 if (((BYTE)parport_8255[1] & 3) == 3)
96 TRACE(int, "Beep (freq: %d) !\n", 1193180 / maxval );
97 Beep(1193180 / maxval, 20);
103 /**********************************************************************
107 /* set_IO_permissions(int val1, int val)
108 * Helper function for IO_port_init
110 #ifdef DIRECT_IO_ACCESS
111 static void set_IO_permissions(int val1, int val, char rw)
115 if (val == -1) val = 0x3ff;
116 for (j = val1; j <= val; j++)
117 port_permissions[j] |= rw;
119 do_direct_port_access = 1;
122 } else if (val != -1) {
123 do_direct_port_access = 1;
125 port_permissions[val] |= rw;
130 /* do_IO_port_init_read_or_write(char* temp, char rw)
131 * Helper function for IO_port_init
134 static void do_IO_port_init_read_or_write(char* temp, char rw)
136 int val, val1, i, len;
137 if (!strcasecmp(temp, "all")) {
138 MSG("Warning!!! Granting FULL IO port access to"
139 " windoze programs!\nWarning!!! "
140 "*** THIS IS NOT AT ALL "
141 "RECOMMENDED!!! ***\n");
142 for (i=0; i < sizeof(port_permissions); i++)
143 port_permissions[i] |= rw;
145 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
149 for (i = 0; i < len; i++) {
152 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
153 sscanf(temp+i, "%x", &val);
156 sscanf(temp+i, "%d", &val);
158 while (isxdigit(temp[i]))
165 set_IO_permissions(val1, val, rw);
170 if (val1 == -1) val1 = 0;
173 if (temp[i] >= '0' && temp[i] <= '9') {
174 sscanf(temp+i, "%d", &val);
175 while (isdigit(temp[i]))
180 set_IO_permissions(val1, val, rw);
184 static __inline__ BYTE inb( WORD port )
187 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
191 static __inline__ WORD inw( WORD port )
194 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
198 static __inline__ DWORD inl( WORD port )
201 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
205 static __inline__ void outb( BYTE value, WORD port )
207 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
210 static __inline__ void outw( WORD value, WORD port )
212 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
215 static __inline__ void outl( DWORD value, WORD port )
217 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
220 #endif /* DIRECT_IO_ACCESS */
224 #ifdef DIRECT_IO_ACCESS
227 /* Can we do that? */
231 PROFILE_GetWineIniString( "ports", "read", "*",
232 temp, sizeof(temp) );
233 do_IO_port_init_read_or_write(temp, IO_READ);
234 PROFILE_GetWineIniString( "ports", "write", "*",
235 temp, sizeof(temp) );
236 do_IO_port_init_read_or_write(temp, IO_WRITE);
238 #endif /* DIRECT_IO_ACCESS */
239 IO_FixCMOSCheckSum();
243 /**********************************************************************
246 * Note: The size argument has to be handled correctly _externally_
247 * (as we always return a DWORD)
249 DWORD IO_inport( int port, int size )
253 TRACE(int, "%d-byte value from port 0x%02x\n", size, port );
255 #ifdef DIRECT_IO_ACCESS
256 if ((do_direct_port_access)
257 /* Make sure we have access to the port */
258 && (port_permissions[port] & IO_READ))
263 case 1: res = inb( port ); break;
264 case 2: res = inw( port ); break;
265 case 4: res = inl( port ); break;
267 ERR(int, "invalid data size %d\n", size);
278 res = (DWORD)VGA_ioport_in( port );
284 BYTE chan = port & 3;
287 if (tmr_8253_latched[chan] == TRUE)
289 tempval = tmr_8253_latch[chan];
290 tmr_8253_latched[chan] = FALSE;
294 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
295 if (chan == 0) /* System timer counter divisor */
296 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
297 tempval = dummy_ctr+(WORD)DOSVM_GetTimer();
299 { /* FIXME: intelligent hardware timer emulation needed */
303 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
306 break; /* correct ? */
307 case 0x10: /* read lo byte */
310 case 0x30: /* read lo byte, then hi byte */
311 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
312 if (tmr_8253_byte_toggle[chan] == TRUE)
317 /* else [fall through if read hi byte !] */
318 case 0x20: /* read hi byte */
319 res = (BYTE)tempval>>8;
325 res = INT_Int09ReadScan();
326 #if 0 /* what's this port got to do with parport ? */
327 res = (DWORD)parport_8255[0];
331 res = (DWORD)parport_8255[1];
334 res = (DWORD)parport_8255[2];
337 res = (DWORD)cmosaddress;
340 res = (DWORD)cmosimage[cmosaddress & 0x3f];
344 res = 0xffffffff; /* no joystick */
347 WARN( int, "Direct I/O read attempted from port %x\n", port);
351 TRACE(int, " returning ( 0x%lx )\n", res );
356 /**********************************************************************
359 void IO_outport( int port, int size, DWORD value )
361 TRACE(int, "IO: 0x%lx (%d-byte value) to port 0x%02x\n",
364 #ifdef DIRECT_IO_ACCESS
365 if ((do_direct_port_access)
366 /* Make sure we have access to the port */
367 && (port_permissions[port] & IO_WRITE))
372 case 1: outb( LOBYTE(value), port ); break;
373 case 2: outw( LOWORD(value), port ); break;
374 case 4: outl( value, port ); break;
376 WARN(int, "Invalid data size %d\n", size);
387 VGA_ioport_out( port, (BYTE)value );
390 DOSVM_PIC_ioport_out( port, (BYTE)value );
396 BYTE chan = port & 3;
399 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
400 /* we need to get the oldval before any lo/hi byte change has been made */
401 (tmr_8253_byte_toggle[chan] == FALSE) )
402 oldval = tmr_8253_countmax[chan];
403 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
406 break; /* correct ? */
407 case 0x10: /* write lo byte */
408 tmr_8253_countmax[chan] =
409 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
411 case 0x30: /* write lo byte, then hi byte */
412 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
413 if (tmr_8253_byte_toggle[chan] == TRUE)
415 tmr_8253_countmax[chan] =
416 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
419 /* else [fall through if write hi byte !] */
420 case 0x20: /* write hi byte */
421 tmr_8253_countmax[chan] =
422 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
426 /* programming finished ? */
427 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
428 (tmr_8253_byte_toggle[chan] == FALSE) )
429 /* update to new value ? */
430 && (tmr_8253_countmax[chan] != oldval)
432 set_timer_maxval(chan, tmr_8253_countmax[chan]);
437 BYTE chan = ((BYTE)value & 0xc0) >> 6;
439 /* ctrl byte for specific timer channel */
440 tmr_8253_ctrlbyte_ch[chan] = (BYTE)value;
442 FIXME(int,"8254 timer readback not implemented yet\n");
445 if (((BYTE)value&0x30)==0) { /* latch timer */
446 tmr_8253_latched[chan] = TRUE;
447 dummy_ctr -= 1+(int) (10.0*rand()/(RAND_MAX+1.0));
448 if (chan == 0) /* System timer divisor */
449 tmr_8253_latch[0] = dummy_ctr+(WORD)DOSVM_GetTimer();
451 { /* FIXME: intelligent hardware timer emulation needed */
452 tmr_8253_latch[chan] = dummy_ctr;
455 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
456 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
460 parport_8255[1] = (BYTE)value;
461 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
463 TRACE(int, "Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
464 Beep(1193180 / tmr_8253_countmax[2], 20);
468 cmosaddress = (BYTE)value & 0x7f;
471 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
474 WARN(int, "Direct I/O write attempted to port %x\n", port );