Changed many WARN()s related to internal Wine memory failures to
[wine/multimedia.git] / msdos / ioports.c
blob05e1a9313711d63703a07b1fb78712e2d5f9942a
1 /*
2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 */
7 /* Known problems:
8 - only a few ports are emulated.
9 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
10 fix that, I guess.
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include "windows.h"
19 #include "vga.h"
20 #include "options.h"
21 #include "debug.h"
23 static BYTE cmosaddress;
25 static BYTE cmosimage[64] =
27 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
28 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
29 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
30 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
31 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x58,
33 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
34 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
37 #if defined(linux) && defined(__i386__)
38 # define DIRECT_IO_ACCESS
39 #else
40 # undef DIRECT_IO_ACCESS
41 #endif /* linux && __i386__ */
43 #ifdef DIRECT_IO_ACCESS
45 extern int iopl(int level);
47 static char do_direct_port_access = 0;
48 static char port_permissions[0x10000];
50 #define IO_READ 1
51 #define IO_WRITE 2
53 #endif /* DIRECT_IO_ACCESS */
55 /**********************************************************************
56 * IO_port_init
59 /* set_IO_permissions(int val1, int val)
60 * Helper function for IO_port_init
62 #ifdef DIRECT_IO_ACCESS
63 static void set_IO_permissions(int val1, int val, char rw)
65 int j;
66 if (val1 != -1) {
67 if (val == -1) val = 0x3ff;
68 for (j = val1; j <= val; j++)
69 port_permissions[j] |= rw;
71 do_direct_port_access = 1;
73 val1 = -1;
74 } else if (val != -1) {
75 do_direct_port_access = 1;
77 port_permissions[val] |= rw;
82 /* do_IO_port_init_read_or_write(char* temp, char rw)
83 * Helper function for IO_port_init
86 static void do_IO_port_init_read_or_write(char* temp, char rw)
88 int val, val1, i, len;
89 if (!strcasecmp(temp, "all")) {
90 MSG("Warning!!! Granting FULL IO port access to"
91 " windoze programs!\nWarning!!! "
92 "*** THIS IS NOT AT ALL "
93 "RECOMMENDED!!! ***\n");
94 for (i=0; i < sizeof(port_permissions); i++)
95 port_permissions[i] |= rw;
97 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
98 len = strlen(temp);
99 val = -1;
100 val1 = -1;
101 for (i = 0; i < len; i++) {
102 switch (temp[i]) {
103 case '0':
104 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
105 sscanf(temp+i, "%x", &val);
106 i += 2;
107 } else {
108 sscanf(temp+i, "%d", &val);
110 while (isxdigit(temp[i]))
111 i++;
112 i--;
113 break;
114 case ',':
115 case ' ':
116 case '\t':
117 set_IO_permissions(val1, val, rw);
118 val1 = -1; val = -1;
119 break;
120 case '-':
121 val1 = val;
122 if (val1 == -1) val1 = 0;
123 break;
124 default:
125 if (temp[i] >= '0' && temp[i] <= '9') {
126 sscanf(temp+i, "%d", &val);
127 while (isdigit(temp[i]))
128 i++;
132 set_IO_permissions(val1, val, rw);
136 static __inline__ BYTE inb( WORD port )
138 BYTE b;
139 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
140 return b;
143 static __inline__ WORD inw( WORD port )
145 WORD w;
146 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
147 return w;
150 static __inline__ DWORD inl( WORD port )
152 DWORD dw;
153 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
154 return dw;
157 static __inline__ void outb( BYTE value, WORD port )
159 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
162 static __inline__ void outw( WORD value, WORD port )
164 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
167 static __inline__ void outl( DWORD value, WORD port )
169 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
172 #endif /* DIRECT_IO_ACCESS */
174 void IO_port_init()
176 #ifdef DIRECT_IO_ACCESS
177 char temp[1024];
179 /* Can we do that? */
180 if (!iopl(3)) {
181 iopl(0);
183 PROFILE_GetWineIniString( "ports", "read", "*",
184 temp, sizeof(temp) );
185 do_IO_port_init_read_or_write(temp, IO_READ);
186 PROFILE_GetWineIniString( "ports", "write", "*",
187 temp, sizeof(temp) );
188 do_IO_port_init_read_or_write(temp, IO_WRITE);
190 #endif /* DIRECT_IO_ACCESS */
194 /**********************************************************************
195 * IO_inport
197 DWORD IO_inport( int port, int count )
199 DWORD res = 0;
200 BYTE b;
202 #ifdef DIRECT_IO_ACCESS
203 if (do_direct_port_access)
205 /* Make sure we have access to the whole range */
206 int i;
207 for (i = 0; i < count; i++)
208 if (!(port_permissions[port+i] & IO_READ)) break;
209 if (i == count)
211 iopl(3);
212 switch(count)
214 case 1: res = inb( port ); break;
215 case 2: res = inw( port ); break;
216 case 4: res = inl( port ); break;
217 default:
218 ERR(int, "invalid count %d\n", count);
220 iopl(0);
221 return res;
224 #endif
226 TRACE(int, "%d bytes from port 0x%02x\n", count, port );
228 while (count-- > 0)
230 switch (port)
232 case 0x70:
233 b = cmosaddress;
234 break;
235 case 0x71:
236 b = cmosimage[cmosaddress & 0x3f];
237 break;
238 case 0x200:
239 case 0x201:
240 b = 0xff; /* no joystick */
241 break;
242 default:
243 WARN( int, "Direct I/O read attempted from port %x\n", port);
244 b = 0xff;
245 break;
247 port++;
248 res = (res << 8) | b;
250 TRACE(int, " returning ( 0x%lx )\n", res );
251 return res;
255 /**********************************************************************
256 * IO_outport
258 void IO_outport( int port, int count, DWORD value )
260 BYTE b;
262 TRACE(int, "IO: 0x%lx (%d bytes) to port 0x%02x\n",
263 value, count, port );
265 #ifdef DIRECT_IO_ACCESS
266 if (do_direct_port_access)
268 /* Make sure we have access to the whole range */
269 int i;
270 for (i = 0; i < count; i++)
271 if (!(port_permissions[port+i] & IO_WRITE)) break;
272 if (i == count)
274 iopl(3);
275 switch(count)
277 case 1: outb( LOBYTE(value), port ); break;
278 case 2: outw( LOWORD(value), port ); break;
279 case 4: outl( value, port ); break;
280 default:
281 WARN(int, "Invalid count %d\n", count);
283 iopl(0);
284 return;
287 #endif
289 while (count-- > 0)
291 b = value & 0xff;
292 value >>= 8;
293 switch (port)
295 case 0x70:
296 cmosaddress = b & 0x7f;
297 break;
298 case 0x71:
299 cmosimage[cmosaddress & 0x3f] = b;
300 break;
301 case 0x3c8:
302 case 0x3c9:
303 VGA_ioport_out( port, b );
304 break;
305 default:
306 WARN(int, "Direct I/O write attempted to port %x\n", port );
307 break;
309 port++;