Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Emulation of processor ioports. |
| 3 | * |
| 4 | * Copyright 1995 Morten Welinder |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 5 | * Copyright 1998 Andreas Mohr, Ove Kaaven |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* Known problems: |
| 23 | - only a few ports are emulated. |
| 24 | - real-time clock in "cmos" is bogus. A nifty alarm() setup could |
| 25 | fix that, I guess. |
| 26 | */ |
| 27 | |
Patrik Stridvall | 17fd4e3 | 2001-06-28 18:04:41 +0000 | [diff] [blame] | 28 | #include "config.h" |
Patrik Stridvall | 9aab47e | 2002-08-28 23:42:34 +0000 | [diff] [blame] | 29 | #include "wine/port.h" |
Patrik Stridvall | 17fd4e3 | 2001-06-28 18:04:41 +0000 | [diff] [blame] | 30 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 31 | #include <ctype.h> |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Alexandre Julliard | 383da68 | 2000-02-10 22:15:21 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 34 | #include <string.h> |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 35 | #include <time.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 36 | #ifdef HAVE_UNISTD_H |
| 37 | # include <unistd.h> |
| 38 | #endif |
Jim Aston | 2e1cafa | 1999-03-14 16:35:05 +0000 | [diff] [blame] | 39 | #include "windef.h" |
Alexandre Julliard | 864ca06 | 2003-08-20 18:22:31 +0000 | [diff] [blame] | 40 | #include "winbase.h" |
Dmitry Timoshkov | d75aed2 | 2002-08-27 01:13:58 +0000 | [diff] [blame] | 41 | #include "winnls.h" |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 42 | #include "winternl.h" |
Ove Kaaven | e5557b3 | 2000-12-26 00:22:45 +0000 | [diff] [blame] | 43 | #include "callback.h" |
Michael Veksler | 3496b43 | 1999-04-01 10:01:20 +0000 | [diff] [blame] | 44 | #include "miscemu.h" |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 45 | #include "wine/unicode.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 46 | #include "wine/debug.h" |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 47 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 48 | WINE_DEFAULT_DEBUG_CHANNEL(int); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 49 | |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 50 | static struct { |
| 51 | WORD countmax; |
| 52 | BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */ |
| 53 | WORD latch; |
| 54 | BOOL16 latched; |
| 55 | BYTE ctrlbyte_ch; |
| 56 | WORD oldval; |
| 57 | } tmr_8253[3] = { |
Ove Kaaven | e2c477b | 2001-11-08 17:06:40 +0000 | [diff] [blame] | 58 | {0xFFFF, FALSE, 0, FALSE, 0x36, 0}, |
| 59 | {0x0012, FALSE, 0, FALSE, 0x74, 0}, |
| 60 | {0x0001, FALSE, 0, FALSE, 0xB6, 0}, |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 61 | }; |
Andreas Mohr | 935ccab | 1999-01-30 13:37:54 +0000 | [diff] [blame] | 62 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 63 | static int dummy_ctr = 0; |
| 64 | |
| 65 | static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff}; |
| 66 | |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 67 | static BYTE cmosaddress; |
| 68 | |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 69 | static BYTE cmosimage[64] = |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 70 | { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 71 | 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01, |
| 72 | 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00, |
| 73 | 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00, |
| 74 | 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00, |
| 75 | 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, |
Andreas Mohr | eeaafcc | 1999-01-03 12:30:43 +0000 | [diff] [blame] | 76 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19, |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 77 | 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80, |
| 78 | 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f |
| 79 | }; |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 80 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 81 | #if defined(linux) && defined(__i386__) |
| 82 | # define DIRECT_IO_ACCESS |
| 83 | #else |
| 84 | # undef DIRECT_IO_ACCESS |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 85 | # undef PP_IO_ACCESS |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 86 | #endif /* linux && __i386__ */ |
| 87 | |
Marcus Meissner | 03c1f4a | 2002-08-13 03:18:40 +0000 | [diff] [blame] | 88 | #ifdef HAVE_PPDEV |
| 89 | static int do_pp_port_access = -1; /* -1: uninitialized, 1: not available |
| 90 | 0: available);*/ |
| 91 | #endif |
| 92 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 93 | #ifdef DIRECT_IO_ACCESS |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 94 | |
| 95 | extern int iopl(int level); |
Alexandre Julliard | de30490 | 2000-06-03 04:50:59 +0000 | [diff] [blame] | 96 | static char do_direct_port_access = -1; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 97 | static char port_permissions[0x10000]; |
| 98 | |
| 99 | #define IO_READ 1 |
| 100 | #define IO_WRITE 2 |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 101 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 102 | static void IO_FixCMOSCheckSum(void) |
Andreas Mohr | eeaafcc | 1999-01-03 12:30:43 +0000 | [diff] [blame] | 103 | { |
| 104 | WORD sum = 0; |
| 105 | int i; |
| 106 | |
| 107 | for (i=0x10; i < 0x2d; i++) |
| 108 | sum += cmosimage[i]; |
| 109 | cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */ |
| 110 | cmosimage[0x2f] = sum & 0xff; |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 111 | TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]); |
Andreas Mohr | eeaafcc | 1999-01-03 12:30:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Patrik Stridvall | c94e086 | 2000-06-07 02:16:47 +0000 | [diff] [blame] | 114 | #endif /* DIRECT_IO_ACCESS */ |
| 115 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 116 | static void set_timer_maxval(unsigned timer, unsigned maxval) |
| 117 | { |
| 118 | switch (timer) { |
| 119 | case 0: /* System timer counter divisor */ |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 120 | if (Dosvm.SetTimer) Dosvm.SetTimer(maxval); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 121 | break; |
| 122 | case 1: /* RAM refresh */ |
Francois Gouget | e76218d | 2001-05-09 17:31:31 +0000 | [diff] [blame] | 123 | FIXME("RAM refresh counter handling not implemented !\n"); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 124 | break; |
| 125 | case 2: /* cassette & speaker */ |
| 126 | /* speaker on ? */ |
| 127 | if (((BYTE)parport_8255[1] & 3) == 3) |
| 128 | { |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 129 | TRACE("Beep (freq: %d) !\n", 1193180 / maxval ); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 130 | Beep(1193180 / maxval, 20); |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 136 | /********************************************************************** |
| 137 | * IO_port_init |
| 138 | */ |
| 139 | |
| 140 | /* set_IO_permissions(int val1, int val) |
| 141 | * Helper function for IO_port_init |
| 142 | */ |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 143 | #ifdef DIRECT_IO_ACCESS |
| 144 | static void set_IO_permissions(int val1, int val, char rw) |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 145 | { |
| 146 | int j; |
| 147 | if (val1 != -1) { |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 148 | if (val == -1) val = 0x3ff; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 149 | for (j = val1; j <= val; j++) |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 150 | port_permissions[j] |= rw; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 151 | |
| 152 | do_direct_port_access = 1; |
| 153 | |
| 154 | val1 = -1; |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 155 | } else if (val != -1) { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 156 | do_direct_port_access = 1; |
| 157 | |
| 158 | port_permissions[val] |= rw; |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /* do_IO_port_init_read_or_write(char* temp, char rw) |
| 164 | * Helper function for IO_port_init |
| 165 | */ |
| 166 | |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 167 | static void do_IO_port_init_read_or_write(const WCHAR *str, char rw) |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 168 | { |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 169 | int val, val1, i; |
| 170 | WCHAR *end; |
| 171 | static const WCHAR allW[] = {'a','l','l',0}; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 172 | |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 173 | if (!strcmpiW(str, allW)) |
| 174 | { |
| 175 | for (i=0; i < sizeof(port_permissions); i++) |
| 176 | port_permissions[i] |= rw; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | val = -1; |
| 181 | val1 = -1; |
| 182 | while (*str) |
| 183 | { |
| 184 | switch(*str) |
| 185 | { |
| 186 | case ',': |
| 187 | case ' ': |
| 188 | case '\t': |
| 189 | set_IO_permissions(val1, val, rw); |
| 190 | val1 = -1; |
| 191 | val = -1; |
| 192 | str++; |
| 193 | break; |
| 194 | case '-': |
| 195 | val1 = val; |
| 196 | if (val1 == -1) val1 = 0; |
| 197 | str++; |
| 198 | break; |
| 199 | default: |
| 200 | if (isdigitW(*str)) |
| 201 | { |
| 202 | val = strtoulW( str, &end, 0 ); |
| 203 | if (end == str) |
| 204 | { |
| 205 | val = -1; |
| 206 | str++; |
| 207 | } |
| 208 | else str = end; |
| 209 | } |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | set_IO_permissions(val1, val, rw); |
| 214 | } |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 217 | static inline BYTE inb( WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 218 | { |
| 219 | BYTE b; |
| 220 | __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) ); |
| 221 | return b; |
| 222 | } |
| 223 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 224 | static inline WORD inw( WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 225 | { |
| 226 | WORD w; |
| 227 | __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) ); |
| 228 | return w; |
| 229 | } |
| 230 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 231 | static inline DWORD inl( WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 232 | { |
| 233 | DWORD dw; |
| 234 | __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) ); |
| 235 | return dw; |
| 236 | } |
| 237 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 238 | static inline void outb( BYTE value, WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 239 | { |
| 240 | __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) ); |
| 241 | } |
| 242 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 243 | static inline void outw( WORD value, WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 244 | { |
| 245 | __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) ); |
| 246 | } |
| 247 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 248 | static inline void outl( DWORD value, WORD port ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 249 | { |
| 250 | __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) ); |
| 251 | } |
| 252 | |
Alexandre Julliard | de30490 | 2000-06-03 04:50:59 +0000 | [diff] [blame] | 253 | static void IO_port_init(void) |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 254 | { |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 255 | char tmp[1024]; |
| 256 | HKEY hkey; |
| 257 | DWORD dummy; |
| 258 | OBJECT_ATTRIBUTES attr; |
| 259 | UNICODE_STRING nameW; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 260 | |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 261 | static const WCHAR portsW[] = {'M','a','c','h','i','n','e','\\', |
| 262 | 'S','o','f','t','w','a','r','e','\\', |
| 263 | 'W','i','n','e','\\','W','i','n','e','\\', |
| 264 | 'C','o','n','f','i','g','\\','P','o','r','t','s',0}; |
| 265 | static const WCHAR readW[] = {'r','e','a','d',0}; |
| 266 | static const WCHAR writeW[] = {'w','r','i','t','e',0}; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 267 | |
Alexandre Julliard | e0deb0c | 2003-08-19 03:21:04 +0000 | [diff] [blame] | 268 | do_direct_port_access = 0; |
| 269 | /* Can we do that? */ |
| 270 | if (!iopl(3)) |
| 271 | { |
| 272 | iopl(0); |
| 273 | |
| 274 | attr.Length = sizeof(attr); |
| 275 | attr.RootDirectory = 0; |
| 276 | attr.ObjectName = &nameW; |
| 277 | attr.Attributes = 0; |
| 278 | attr.SecurityDescriptor = NULL; |
| 279 | attr.SecurityQualityOfService = NULL; |
| 280 | RtlInitUnicodeString( &nameW, portsW ); |
| 281 | |
| 282 | if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) |
| 283 | { |
| 284 | RtlInitUnicodeString( &nameW, readW ); |
| 285 | if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy )) |
| 286 | { |
| 287 | WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data; |
| 288 | do_IO_port_init_read_or_write(str, IO_READ); |
| 289 | } |
| 290 | RtlInitUnicodeString( &nameW, writeW ); |
| 291 | if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy )) |
| 292 | { |
| 293 | WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data; |
| 294 | do_IO_port_init_read_or_write(str, IO_WRITE); |
| 295 | } |
| 296 | NtClose( hkey ); |
| 297 | } |
| 298 | } |
Andreas Mohr | eeaafcc | 1999-01-03 12:30:43 +0000 | [diff] [blame] | 299 | IO_FixCMOSCheckSum(); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 300 | } |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 301 | |
Patrik Stridvall | c94e086 | 2000-06-07 02:16:47 +0000 | [diff] [blame] | 302 | #endif /* DIRECT_IO_ACCESS */ |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 303 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 304 | /********************************************************************** |
| 305 | * IO_inport |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 306 | * |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 307 | * Note: The size argument has to be handled correctly _externally_ |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 308 | * (as we always return a DWORD) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 309 | */ |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 310 | DWORD IO_inport( int port, int size ) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 311 | { |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 312 | DWORD res = 0; |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 313 | |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 314 | TRACE("%d-byte value from port 0x%02x\n", size, port ); |
Andreas Mohr | 935ccab | 1999-01-30 13:37:54 +0000 | [diff] [blame] | 315 | |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 316 | #ifdef HAVE_PPDEV |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 317 | if (do_pp_port_access == -1) |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 318 | do_pp_port_access =IO_pp_init(); |
| 319 | if ((do_pp_port_access == 0 ) && (size == 1)) |
| 320 | if (!IO_pp_inp(port,&res)) |
| 321 | return res; |
| 322 | #endif |
Alexandre Julliard | de30490 | 2000-06-03 04:50:59 +0000 | [diff] [blame] | 323 | #ifdef DIRECT_IO_ACCESS |
| 324 | if (do_direct_port_access == -1) IO_port_init(); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 325 | if ((do_direct_port_access) |
| 326 | /* Make sure we have access to the port */ |
| 327 | && (port_permissions[port] & IO_READ)) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 328 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 329 | iopl(3); |
| 330 | switch(size) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 331 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 332 | case 1: res = inb( port ); break; |
| 333 | case 2: res = inw( port ); break; |
| 334 | case 4: res = inl( port ); break; |
| 335 | default: |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 336 | ERR("invalid data size %d\n", size); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 337 | } |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 338 | iopl(0); |
| 339 | return res; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 340 | } |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 341 | #endif |
| 342 | |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 343 | /* first give the DOS VM a chance to handle it */ |
Jukka Heinonen | 47592a4 | 2003-05-11 03:30:02 +0000 | [diff] [blame] | 344 | if (Dosvm.inport || DPMI_LoadDosSystem()) |
| 345 | if (Dosvm.inport( port, size, &res )) |
| 346 | return res; |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 347 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 348 | switch (port) |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 349 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 350 | case 0x40: |
| 351 | case 0x41: |
| 352 | case 0x42: |
| 353 | { |
| 354 | BYTE chan = port & 3; |
| 355 | WORD tempval = 0; |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 356 | if (tmr_8253[chan].latched) |
| 357 | tempval = tmr_8253[chan].latch; |
| 358 | else |
| 359 | { |
| 360 | dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0)); |
| 361 | if (chan == 0) /* System timer counter divisor */ |
| 362 | { |
Ove Kaaven | e5557b3 | 2000-12-26 00:22:45 +0000 | [diff] [blame] | 363 | /* FIXME: Dosvm.GetTimer() returns quite rigid values */ |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 364 | if (Dosvm.GetTimer) |
Uwe Bonnes | 23fbf4a | 2001-08-15 23:19:45 +0000 | [diff] [blame] | 365 | tempval = dummy_ctr + (WORD)Dosvm.GetTimer(); |
| 366 | else |
| 367 | tempval = dummy_ctr; |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
| 371 | /* FIXME: intelligent hardware timer emulation needed */ |
| 372 | tempval = dummy_ctr; |
| 373 | } |
| 374 | } |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 375 | |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 376 | switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4) |
Andreas Mohr | 935ccab | 1999-01-30 13:37:54 +0000 | [diff] [blame] | 377 | { |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 378 | case 0: |
| 379 | res = 0; /* shouldn't happen? */ |
| 380 | break; |
| 381 | case 1: /* read lo byte */ |
| 382 | res = (BYTE)tempval; |
| 383 | tmr_8253[chan].latched = FALSE; |
| 384 | break; |
| 385 | case 3: /* read lo byte, then hi byte */ |
| 386 | tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */ |
| 387 | if (tmr_8253[chan].byte_toggle) |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 388 | { |
| 389 | res = (BYTE)tempval; |
| 390 | break; |
| 391 | } |
| 392 | /* else [fall through if read hi byte !] */ |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 393 | case 2: /* read hi byte */ |
| 394 | res = (BYTE)(tempval >> 8); |
| 395 | tmr_8253[chan].latched = FALSE; |
| 396 | break; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 397 | } |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 398 | } |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 399 | break; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 400 | case 0x60: |
Ove Kaaven | 866b337 | 1999-03-25 10:51:38 +0000 | [diff] [blame] | 401 | #if 0 /* what's this port got to do with parport ? */ |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 402 | res = (DWORD)parport_8255[0]; |
Ove Kaaven | 866b337 | 1999-03-25 10:51:38 +0000 | [diff] [blame] | 403 | #endif |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 404 | break; |
| 405 | case 0x61: |
| 406 | res = (DWORD)parport_8255[1]; |
| 407 | break; |
| 408 | case 0x62: |
| 409 | res = (DWORD)parport_8255[2]; |
| 410 | break; |
| 411 | case 0x70: |
| 412 | res = (DWORD)cmosaddress; |
| 413 | break; |
| 414 | case 0x71: |
| 415 | res = (DWORD)cmosimage[cmosaddress & 0x3f]; |
| 416 | break; |
| 417 | case 0x200: |
| 418 | case 0x201: |
| 419 | res = 0xffffffff; /* no joystick */ |
| 420 | break; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 421 | default: |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 422 | WARN("Direct I/O read attempted from port %x\n", port); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 423 | res = 0xffffffff; |
| 424 | break; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 425 | } |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 426 | TRACE(" returning ( 0x%lx )\n", res ); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 427 | return res; |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 430 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 431 | /********************************************************************** |
| 432 | * IO_outport |
| 433 | */ |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 434 | void IO_outport( int port, int size, DWORD value ) |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 435 | { |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 436 | TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n", |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 437 | value, size, port ); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 438 | |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 439 | #ifdef HAVE_PPDEV |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 440 | if (do_pp_port_access == -1) |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 441 | do_pp_port_access = IO_pp_init(); |
| 442 | if ((do_pp_port_access == 0) && (size == 1)) |
| 443 | if (!IO_pp_outp(port,&value)) |
| 444 | return; |
| 445 | #endif |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 446 | #ifdef DIRECT_IO_ACCESS |
Uwe Bonnes | 6509fa9 | 2001-06-26 21:06:07 +0000 | [diff] [blame] | 447 | |
Alexandre Julliard | de30490 | 2000-06-03 04:50:59 +0000 | [diff] [blame] | 448 | if (do_direct_port_access == -1) IO_port_init(); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 449 | if ((do_direct_port_access) |
| 450 | /* Make sure we have access to the port */ |
| 451 | && (port_permissions[port] & IO_WRITE)) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 452 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 453 | iopl(3); |
| 454 | switch(size) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 455 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 456 | case 1: outb( LOBYTE(value), port ); break; |
| 457 | case 2: outw( LOWORD(value), port ); break; |
| 458 | case 4: outl( value, port ); break; |
| 459 | default: |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 460 | WARN("Invalid data size %d\n", size); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 461 | } |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 462 | iopl(0); |
| 463 | return; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 464 | } |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 465 | #endif |
| 466 | |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 467 | /* first give the DOS VM a chance to handle it */ |
Jukka Heinonen | 47592a4 | 2003-05-11 03:30:02 +0000 | [diff] [blame] | 468 | if (Dosvm.outport || DPMI_LoadDosSystem()) |
| 469 | if (Dosvm.outport( port, size, value )) |
| 470 | return; |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 471 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 472 | switch (port) |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 473 | { |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 474 | case 0x40: |
| 475 | case 0x41: |
| 476 | case 0x42: |
| 477 | { |
| 478 | BYTE chan = port & 3; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 479 | |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 480 | /* we need to get the oldval before any lo/hi byte change has been made */ |
| 481 | if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) || |
| 482 | !tmr_8253[chan].byte_toggle) |
| 483 | tmr_8253[chan].oldval = tmr_8253[chan].countmax; |
| 484 | switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 485 | { |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 486 | case 0: |
| 487 | break; /* shouldn't happen? */ |
| 488 | case 1: /* write lo byte */ |
| 489 | tmr_8253[chan].countmax = |
| 490 | (tmr_8253[chan].countmax & 0xff00) | (BYTE)value; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 491 | break; |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 492 | case 3: /* write lo byte, then hi byte */ |
| 493 | tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */ |
| 494 | if (tmr_8253[chan].byte_toggle) |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 495 | { |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 496 | tmr_8253[chan].countmax = |
| 497 | (tmr_8253[chan].countmax & 0xff00) | (BYTE)value; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | /* else [fall through if write hi byte !] */ |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 501 | case 2: /* write hi byte */ |
| 502 | tmr_8253[chan].countmax = |
| 503 | (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 506 | /* if programming is finished and value has changed |
| 507 | then update to new value */ |
| 508 | if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) || |
| 509 | !tmr_8253[chan].byte_toggle) && |
| 510 | (tmr_8253[chan].countmax != tmr_8253[chan].oldval)) |
| 511 | set_timer_maxval(chan, tmr_8253[chan].countmax); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 512 | } |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 513 | break; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 514 | case 0x43: |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 515 | { |
| 516 | BYTE chan = ((BYTE)value & 0xc0) >> 6; |
| 517 | /* ctrl byte for specific timer channel */ |
| 518 | if (chan == 3) |
| 519 | { |
| 520 | FIXME("8254 timer readback not implemented yet\n"); |
| 521 | break; |
| 522 | } |
| 523 | switch (((BYTE)value & 0x30) >> 4) |
| 524 | { |
| 525 | case 0: /* latch timer */ |
| 526 | tmr_8253[chan].latched = TRUE; |
| 527 | dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0)); |
| 528 | if (chan == 0) /* System timer divisor */ |
Alexandre Julliard | 8cd55d0 | 2001-12-04 19:54:44 +0000 | [diff] [blame] | 529 | if (Dosvm.GetTimer) |
Uwe Bonnes | 23fbf4a | 2001-08-15 23:19:45 +0000 | [diff] [blame] | 530 | tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer(); |
| 531 | else |
| 532 | tmr_8253[chan].latch = dummy_ctr; |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 533 | else |
| 534 | { |
| 535 | /* FIXME: intelligent hardware timer emulation needed */ |
| 536 | tmr_8253[chan].latch = dummy_ctr; |
| 537 | } |
| 538 | break; |
| 539 | case 3: /* write lo byte, then hi byte */ |
| 540 | tmr_8253[chan].byte_toggle = FALSE; /* init */ |
| 541 | /* fall through */ |
| 542 | case 1: /* write lo byte only */ |
| 543 | case 2: /* write hi byte only */ |
| 544 | tmr_8253[chan].ctrlbyte_ch = (BYTE)value; |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | break; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 549 | case 0x61: |
| 550 | parport_8255[1] = (BYTE)value; |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 551 | if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1)) |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 552 | { |
James Abbatiello | 9ad9e65 | 2000-08-22 20:38:47 +0000 | [diff] [blame] | 553 | TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax); |
| 554 | Beep(1193180 / tmr_8253[2].countmax, 20); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 555 | } |
| 556 | break; |
| 557 | case 0x70: |
| 558 | cmosaddress = (BYTE)value & 0x7f; |
| 559 | break; |
| 560 | case 0x71: |
| 561 | cmosimage[cmosaddress & 0x3f] = (BYTE)value; |
| 562 | break; |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 563 | default: |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 564 | WARN("Direct I/O write attempted to port %x\n", port ); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 565 | break; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 566 | } |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 567 | } |