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