Release 980315
[wine/multimedia.git] / msdos / ioports.c
blobb4924bd7cc915289eaebd9b57d91abd765566041
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 <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include "windows.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 fprintf(stderr, "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 default:
239 fprintf( stderr, "Direct I/O read attempted from port %x\n", port);
240 b = 0xff;
241 break;
243 port++;
244 res = (res << 8) | b;
246 TRACE(int, " returning ( 0x%lx )\n", res );
247 return res;
251 /**********************************************************************
252 * IO_outport
254 void IO_outport( int port, int count, DWORD value )
256 BYTE b;
258 TRACE(int, "IO: 0x%lx (%d bytes) to port 0x%02x\n",
259 value, count, port );
261 #ifdef DIRECT_IO_ACCESS
262 if (do_direct_port_access)
264 /* Make sure we have access to the whole range */
265 int i;
266 for (i = 0; i < count; i++)
267 if (!(port_permissions[port+i] & IO_WRITE)) break;
268 if (i == count)
270 iopl(3);
271 switch(count)
273 case 1: outb( LOBYTE(value), port ); break;
274 case 2: outw( LOWORD(value), port ); break;
275 case 4: outl( value, port ); break;
276 default:
277 fprintf( stderr, "IO_outport: invalid count %d\n", count);
279 iopl(0);
280 return;
283 #endif
285 while (count-- > 0)
287 b = value & 0xff;
288 value >>= 8;
289 switch (port)
291 case 0x70:
292 cmosaddress = b & 0x7f;
293 break;
294 case 0x71:
295 cmosimage[cmosaddress & 0x3f] = b;
296 break;
297 default:
298 fprintf( stderr, "Direct I/O write attempted "
299 "to port %x\n", port );
300 break;
302 port++;