2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 - only a few ports are emulated.
24 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(int);
45 #if defined(linux) && defined(__i386__)
46 # define DIRECT_IO_ACCESS
48 # undef DIRECT_IO_ACCESS
49 #endif /* linux && __i386__ */
53 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
59 {0xFFFF, FALSE, 0, FALSE, 0x36, 0},
60 {0x0012, FALSE, 0, FALSE, 0x74, 0},
61 {0x0001, FALSE, 0, FALSE, 0xB6, 0},
64 static int dummy_ctr = 0;
66 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
68 static BYTE cmosaddress;
70 /* if you change anything here, use IO_FixCMOSCheckSum below to compute
71 * the checksum and put the right values in.
73 static BYTE cmosimage[64] =
75 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
76 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
77 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
78 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
79 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19, /* last 2 bytes are checksum */
81 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
82 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
86 static void IO_FixCMOSCheckSum(void)
91 for (i=0x10; i < 0x2d; i++)
93 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
94 cmosimage[0x2f] = sum & 0xff;
95 MESSAGE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
99 #ifdef DIRECT_IO_ACCESS
101 extern int iopl(int level);
102 static char do_direct_port_access = -1;
103 static char port_permissions[0x10000];
108 #endif /* DIRECT_IO_ACCESS */
111 static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available
115 static void set_timer_maxval(unsigned timer, unsigned maxval)
118 case 0: /* System timer counter divisor */
119 DOSVM_SetTimer(maxval);
121 case 1: /* RAM refresh */
122 FIXME("RAM refresh counter handling not implemented !\n");
124 case 2: /* cassette & speaker */
126 if ((parport_8255[1] & 3) == 3)
128 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
129 Beep(1193180 / maxval, 20);
136 /**********************************************************************
140 /* set_IO_permissions(int val1, int val)
141 * Helper function for IO_port_init
143 #ifdef DIRECT_IO_ACCESS
144 static void set_IO_permissions(int val1, int val, char rw)
148 if (val == -1) val = 0x3ff;
149 for (j = val1; j <= val; j++)
150 port_permissions[j] |= rw;
152 do_direct_port_access = 1;
155 } else if (val != -1) {
156 do_direct_port_access = 1;
158 port_permissions[val] |= rw;
163 /* do_IO_port_init_read_or_write(char* temp, char rw)
164 * Helper function for IO_port_init
167 static void do_IO_port_init_read_or_write(const WCHAR *str, char rw)
172 static const WCHAR allW[] = {'a','l','l',0};
174 if (!strcmpiW(str, allW))
176 for (i=0; i < sizeof(port_permissions); i++)
177 port_permissions[i] |= rw;
190 set_IO_permissions(val1, val, rw);
197 if (val1 == -1) val1 = 0;
203 val = strtoulW( str, &end, 0 );
214 set_IO_permissions(val1, val, rw);
218 static inline BYTE inb( WORD port )
221 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
225 static inline WORD inw( WORD port )
228 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
232 static inline DWORD inl( WORD port )
235 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
239 static inline void outb( BYTE value, WORD port )
241 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
244 static inline void outw( WORD value, WORD port )
246 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
249 static inline void outl( DWORD value, WORD port )
251 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
254 static void IO_port_init(void)
259 OBJECT_ATTRIBUTES attr;
260 UNICODE_STRING nameW;
262 static const WCHAR portsW[] = {'S','o','f','t','w','a','r','e','\\',
263 'W','i','n','e','\\','V','D','M','\\','P','o','r','t','s',0};
264 static const WCHAR readW[] = {'r','e','a','d',0};
265 static const WCHAR writeW[] = {'w','r','i','t','e',0};
267 do_direct_port_access = 0;
268 /* Can we do that? */
273 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
274 attr.Length = sizeof(attr);
275 attr.RootDirectory = root;
276 attr.ObjectName = &nameW;
278 attr.SecurityDescriptor = NULL;
279 attr.SecurityQualityOfService = NULL;
280 RtlInitUnicodeString( &nameW, portsW );
282 /* @@ Wine registry key: HKCU\Software\Wine\VDM\Ports */
283 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
285 RtlInitUnicodeString( &nameW, readW );
286 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
288 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
289 do_IO_port_init_read_or_write(str, IO_READ);
291 RtlInitUnicodeString( &nameW, writeW );
292 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
294 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
295 do_IO_port_init_read_or_write(str, IO_WRITE);
303 #endif /* DIRECT_IO_ACCESS */
306 /**********************************************************************
309 * Note: The size argument has to be handled correctly _externally_
310 * (as we always return a DWORD)
312 DWORD WINAPI DOSVM_inport( int port, int size )
316 TRACE("%d-byte value from port 0x%04x\n", size, port );
319 if (do_pp_port_access == -1) do_pp_port_access =IO_pp_init();
320 if ((do_pp_port_access == 0 ) && (size == 1))
322 if (!IO_pp_inp(port,&res)) return res;
326 #ifdef DIRECT_IO_ACCESS
327 if (do_direct_port_access == -1) IO_port_init();
328 if ((do_direct_port_access)
329 /* Make sure we have access to the port */
330 && (port_permissions[port] & IO_READ))
335 case 1: res = inb( port ); break;
336 case 2: res = inw( port ); break;
337 case 4: res = inl( port ); break;
350 BYTE chan = port & 3;
352 if (tmr_8253[chan].latched)
353 tempval = tmr_8253[chan].latch;
356 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
357 if (chan == 0) /* System timer counter divisor */
359 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
360 tempval = dummy_ctr + (WORD)DOSVM_GetTimer();
364 /* FIXME: intelligent hardware timer emulation needed */
369 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
372 res = 0; /* shouldn't happen? */
374 case 1: /* read lo byte */
376 tmr_8253[chan].latched = FALSE;
378 case 3: /* read lo byte, then hi byte */
379 tmr_8253[chan].byte_toggle ^= 1; /* toggle */
380 if (tmr_8253[chan].byte_toggle)
385 /* else [fall through if read hi byte !] */
386 case 2: /* read hi byte */
387 res = (BYTE)(tempval >> 8);
388 tmr_8253[chan].latched = FALSE;
394 res = DOSVM_Int09ReadScan(NULL);
397 res = (DWORD)parport_8255[1];
400 res = (DWORD)parport_8255[2];
403 res = (DWORD)cmosaddress;
406 res = (DWORD)cmosimage[cmosaddress & 0x3f];
410 res = ~0U; /* no joystick */
415 res = (DWORD)SB_ioport_in( port );
451 FIXME("Trying to read more than one byte from VGA!\n");
452 res = (DWORD)VGA_ioport_in( port );
488 res = (DWORD)DMA_ioport_in( port );
491 WARN("Direct I/O read attempted from port %x\n", port);
498 /**********************************************************************
499 * outport (WINEDOS.@)
501 void WINAPI DOSVM_outport( int port, int size, DWORD value )
503 TRACE("IO: 0x%lx (%d-byte value) to port 0x%04x\n", value, size, port );
506 if (do_pp_port_access == -1) do_pp_port_access = IO_pp_init();
507 if ((do_pp_port_access == 0) && (size == 1))
509 if (!IO_pp_outp(port,&value)) return;
513 #ifdef DIRECT_IO_ACCESS
515 if (do_direct_port_access == -1) IO_port_init();
516 if ((do_direct_port_access)
517 /* Make sure we have access to the port */
518 && (port_permissions[port] & IO_WRITE))
523 case 1: outb( LOBYTE(value), port ); break;
524 case 2: outw( LOWORD(value), port ); break;
525 case 4: outl( value, port ); break;
535 DOSVM_PIC_ioport_out( port, (BYTE)value );
541 BYTE chan = port & 3;
543 /* we need to get the oldval before any lo/hi byte change has been made */
544 if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
545 !tmr_8253[chan].byte_toggle)
546 tmr_8253[chan].oldval = tmr_8253[chan].countmax;
547 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
550 break; /* shouldn't happen? */
551 case 1: /* write lo byte */
552 tmr_8253[chan].countmax =
553 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
555 case 3: /* write lo byte, then hi byte */
556 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
557 if (tmr_8253[chan].byte_toggle)
559 tmr_8253[chan].countmax =
560 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
563 /* else [fall through if write hi byte !] */
564 case 2: /* write hi byte */
565 tmr_8253[chan].countmax =
566 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
569 /* if programming is finished and value has changed
570 then update to new value */
571 if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
572 !tmr_8253[chan].byte_toggle) &&
573 (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
574 set_timer_maxval(chan, tmr_8253[chan].countmax);
579 BYTE chan = ((BYTE)value & 0xc0) >> 6;
580 /* ctrl byte for specific timer channel */
583 FIXME("8254 timer readback not implemented yet\n");
586 switch (((BYTE)value & 0x30) >> 4)
588 case 0: /* latch timer */
589 tmr_8253[chan].latched = TRUE;
590 dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
591 if (chan == 0) /* System timer divisor */
592 tmr_8253[chan].latch = dummy_ctr + (WORD)DOSVM_GetTimer();
595 /* FIXME: intelligent hardware timer emulation needed */
596 tmr_8253[chan].latch = dummy_ctr;
599 case 3: /* write lo byte, then hi byte */
600 tmr_8253[chan].byte_toggle = FALSE; /* init */
602 case 1: /* write lo byte only */
603 case 2: /* write hi byte only */
604 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
610 parport_8255[1] = (BYTE)value;
611 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
613 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
614 Beep(1193180 / tmr_8253[2].countmax, 20);
618 cmosaddress = (BYTE)value & 0x7f;
621 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
625 SB_ioport_out( port, (BYTE)value );
659 VGA_ioport_out( port, LOBYTE(value) );
661 VGA_ioport_out( port+1, HIBYTE(value) );
663 VGA_ioport_out( port+2, LOBYTE(HIWORD(value)) );
664 VGA_ioport_out( port+3, HIBYTE(HIWORD(value)) );
714 DMA_ioport_out( port, (BYTE)value );
717 WARN("Direct I/O write attempted to port %x\n", port );