2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
8 - only a few ports are emulated.
9 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
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
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];
53 #endif /* DIRECT_IO_ACCESS */
55 /**********************************************************************
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
)
67 if (val
== -1) val
= 0x3ff;
68 for (j
= val1
; j
<= val
; j
++)
69 port_permissions
[j
] |= rw
;
71 do_direct_port_access
= 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')) {
101 for (i
= 0; i
< len
; i
++) {
104 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
105 sscanf(temp
+i
, "%x", &val
);
108 sscanf(temp
+i
, "%d", &val
);
110 while (isxdigit(temp
[i
]))
117 set_IO_permissions(val1
, val
, rw
);
122 if (val1
== -1) val1
= 0;
125 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
126 sscanf(temp
+i
, "%d", &val
);
127 while (isdigit(temp
[i
]))
132 set_IO_permissions(val1
, val
, rw
);
136 static __inline__ BYTE
inb( WORD port
)
139 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
143 static __inline__ WORD
inw( WORD port
)
146 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
150 static __inline__ DWORD
inl( WORD port
)
153 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
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 */
176 #ifdef DIRECT_IO_ACCESS
179 /* Can we do that? */
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 /**********************************************************************
197 DWORD
IO_inport( int port
, int count
)
202 #ifdef DIRECT_IO_ACCESS
203 if (do_direct_port_access
)
205 /* Make sure we have access to the whole range */
207 for (i
= 0; i
< count
; i
++)
208 if (!(port_permissions
[port
+i
] & IO_READ
)) break;
214 case 1: res
= inb( port
); break;
215 case 2: res
= inw( port
); break;
216 case 4: res
= inl( port
); break;
218 ERR(int, "invalid count %d\n", count
);
226 TRACE(int, "%d bytes from port 0x%02x\n", count
, port
);
236 b
= cmosimage
[cmosaddress
& 0x3f];
240 b
= 0xff; /* no joystick */
243 b
= VGA_ioport_in( port
);
246 WARN( int, "Direct I/O read attempted from port %x\n", port
);
251 res
= (res
<< 8) | b
;
253 TRACE(int, " returning ( 0x%lx )\n", res
);
258 /**********************************************************************
261 void IO_outport( int port
, int count
, DWORD value
)
265 TRACE(int, "IO: 0x%lx (%d bytes) to port 0x%02x\n",
266 value
, count
, port
);
268 #ifdef DIRECT_IO_ACCESS
269 if (do_direct_port_access
)
271 /* Make sure we have access to the whole range */
273 for (i
= 0; i
< count
; i
++)
274 if (!(port_permissions
[port
+i
] & IO_WRITE
)) break;
280 case 1: outb( LOBYTE(value
), port
); break;
281 case 2: outw( LOWORD(value
), port
); break;
282 case 4: outl( value
, port
); break;
284 WARN(int, "Invalid count %d\n", count
);
299 cmosaddress
= b
& 0x7f;
302 cmosimage
[cmosaddress
& 0x3f] = b
;
306 VGA_ioport_out( port
, b
);
309 WARN(int, "Direct I/O write attempted to port %x\n", port
);