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, 0x19,
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 static void IO_FixCMOSCheckSum()
60 for (i
=0x10; i
< 0x2d; i
++)
62 cmosimage
[0x2e] = sum
>> 8; /* yes, this IS hi byte !! */
63 cmosimage
[0x2f] = sum
& 0xff;
64 TRACE(int, "calculated hi %02x, lo %02x\n", cmosimage
[0x2e], cmosimage
[0x2f]);
67 /**********************************************************************
71 /* set_IO_permissions(int val1, int val)
72 * Helper function for IO_port_init
74 #ifdef DIRECT_IO_ACCESS
75 static void set_IO_permissions(int val1
, int val
, char rw
)
79 if (val
== -1) val
= 0x3ff;
80 for (j
= val1
; j
<= val
; j
++)
81 port_permissions
[j
] |= rw
;
83 do_direct_port_access
= 1;
86 } else if (val
!= -1) {
87 do_direct_port_access
= 1;
89 port_permissions
[val
] |= rw
;
94 /* do_IO_port_init_read_or_write(char* temp, char rw)
95 * Helper function for IO_port_init
98 static void do_IO_port_init_read_or_write(char* temp
, char rw
)
100 int val
, val1
, i
, len
;
101 if (!strcasecmp(temp
, "all")) {
102 MSG("Warning!!! Granting FULL IO port access to"
103 " windoze programs!\nWarning!!! "
104 "*** THIS IS NOT AT ALL "
105 "RECOMMENDED!!! ***\n");
106 for (i
=0; i
< sizeof(port_permissions
); i
++)
107 port_permissions
[i
] |= rw
;
109 } else if (!(!strcmp(temp
, "*") || *temp
== '\0')) {
113 for (i
= 0; i
< len
; i
++) {
116 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
117 sscanf(temp
+i
, "%x", &val
);
120 sscanf(temp
+i
, "%d", &val
);
122 while (isxdigit(temp
[i
]))
129 set_IO_permissions(val1
, val
, rw
);
134 if (val1
== -1) val1
= 0;
137 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
138 sscanf(temp
+i
, "%d", &val
);
139 while (isdigit(temp
[i
]))
144 set_IO_permissions(val1
, val
, rw
);
148 static __inline__ BYTE
inb( WORD port
)
151 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
155 static __inline__ WORD
inw( WORD port
)
158 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
162 static __inline__ DWORD
inl( WORD port
)
165 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
169 static __inline__
void outb( BYTE value
, WORD port
)
171 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
174 static __inline__
void outw( WORD value
, WORD port
)
176 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
179 static __inline__
void outl( DWORD value
, WORD port
)
181 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
184 #endif /* DIRECT_IO_ACCESS */
188 #ifdef DIRECT_IO_ACCESS
191 /* Can we do that? */
195 PROFILE_GetWineIniString( "ports", "read", "*",
196 temp
, sizeof(temp
) );
197 do_IO_port_init_read_or_write(temp
, IO_READ
);
198 PROFILE_GetWineIniString( "ports", "write", "*",
199 temp
, sizeof(temp
) );
200 do_IO_port_init_read_or_write(temp
, IO_WRITE
);
202 #endif /* DIRECT_IO_ACCESS */
203 IO_FixCMOSCheckSum();
207 /**********************************************************************
210 DWORD
IO_inport( int port
, int count
)
215 #ifdef DIRECT_IO_ACCESS
216 if (do_direct_port_access
)
218 /* Make sure we have access to the whole range */
220 for (i
= 0; i
< count
; i
++)
221 if (!(port_permissions
[port
+i
] & IO_READ
)) break;
227 case 1: res
= inb( port
); break;
228 case 2: res
= inw( port
); break;
229 case 4: res
= inl( port
); break;
231 ERR(int, "invalid count %d\n", count
);
239 TRACE(int, "%d bytes from port 0x%02x\n", count
, port
);
249 b
= cmosimage
[cmosaddress
& 0x3f];
253 b
= 0xff; /* no joystick */
256 b
= VGA_ioport_in( port
);
259 WARN( int, "Direct I/O read attempted from port %x\n", port
);
264 res
= (res
<< 8) | b
;
266 TRACE(int, " returning ( 0x%lx )\n", res
);
271 /**********************************************************************
274 void IO_outport( int port
, int count
, DWORD value
)
278 TRACE(int, "IO: 0x%lx (%d bytes) to port 0x%02x\n",
279 value
, count
, port
);
281 #ifdef DIRECT_IO_ACCESS
282 if (do_direct_port_access
)
284 /* Make sure we have access to the whole range */
286 for (i
= 0; i
< count
; i
++)
287 if (!(port_permissions
[port
+i
] & IO_WRITE
)) break;
293 case 1: outb( LOBYTE(value
), port
); break;
294 case 2: outw( LOWORD(value
), port
); break;
295 case 4: outl( value
, port
); break;
297 WARN(int, "Invalid count %d\n", count
);
312 cmosaddress
= b
& 0x7f;
315 cmosimage
[cmosaddress
& 0x3f] = b
;
319 VGA_ioport_out( port
, b
);
322 WARN(int, "Direct I/O write attempted to port %x\n", port
);