2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 - only a few ports are emulated.
24 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(int);
46 BOOL16 byte_toggle
; /* if TRUE, then hi byte has already been written */
52 {0xFFFF, FALSE
, 0, FALSE
, 0x36, 0},
53 {0x0012, FALSE
, 0, FALSE
, 0x74, 0},
54 {0x0001, FALSE
, 0, FALSE
, 0xB6, 0},
57 static int dummy_ctr
= 0;
59 static BYTE parport_8255
[4] = {0x4f, 0x20, 0xff, 0xff};
61 static BYTE cmosaddress
;
63 static BYTE cmosimage
[64] =
65 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
66 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
67 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
68 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
69 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
71 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
72 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
75 #if defined(linux) && defined(__i386__)
76 # define DIRECT_IO_ACCESS
78 # undef DIRECT_IO_ACCESS
80 #endif /* linux && __i386__ */
82 #ifdef DIRECT_IO_ACCESS
84 extern int iopl(int level
);
85 static char do_direct_port_access
= -1;
87 static char do_pp_port_access
= -1; /* -1: uninitialized, 1: not available
90 static char port_permissions
[0x10000];
95 static void IO_FixCMOSCheckSum(void)
100 for (i
=0x10; i
< 0x2d; i
++)
102 cmosimage
[0x2e] = sum
>> 8; /* yes, this IS hi byte !! */
103 cmosimage
[0x2f] = sum
& 0xff;
104 TRACE("calculated hi %02x, lo %02x\n", cmosimage
[0x2e], cmosimage
[0x2f]);
107 #endif /* DIRECT_IO_ACCESS */
109 static void set_timer_maxval(unsigned timer
, unsigned maxval
)
112 case 0: /* System timer counter divisor */
113 if (Dosvm
.SetTimer
) Dosvm
.SetTimer(maxval
);
115 case 1: /* RAM refresh */
116 FIXME("RAM refresh counter handling not implemented !\n");
118 case 2: /* cassette & speaker */
120 if (((BYTE
)parport_8255
[1] & 3) == 3)
122 TRACE("Beep (freq: %d) !\n", 1193180 / maxval
);
123 Beep(1193180 / maxval
, 20);
129 /**********************************************************************
133 /* set_IO_permissions(int val1, int val)
134 * Helper function for IO_port_init
136 #ifdef DIRECT_IO_ACCESS
137 static void set_IO_permissions(int val1
, int val
, char rw
)
141 if (val
== -1) val
= 0x3ff;
142 for (j
= val1
; j
<= val
; j
++)
143 port_permissions
[j
] |= rw
;
145 do_direct_port_access
= 1;
148 } else if (val
!= -1) {
149 do_direct_port_access
= 1;
151 port_permissions
[val
] |= rw
;
156 /* do_IO_port_init_read_or_write(char* temp, char rw)
157 * Helper function for IO_port_init
160 static void do_IO_port_init_read_or_write(char* temp
, char rw
)
162 int val
, val1
, i
, len
;
163 if (!strcasecmp(temp
, "all")) {
164 MESSAGE("Warning!!! Granting FULL IO port access to"
165 " windoze programs!\nWarning!!! "
166 "*** THIS IS NOT AT ALL "
167 "RECOMMENDED!!! ***\n");
168 for (i
=0; i
< sizeof(port_permissions
); i
++)
169 port_permissions
[i
] |= rw
;
171 } else if (!(!strcmp(temp
, "*") || *temp
== '\0')) {
175 for (i
= 0; i
< len
; i
++) {
178 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
179 sscanf(temp
+i
, "%x", &val
);
182 sscanf(temp
+i
, "%d", &val
);
184 while (isxdigit(temp
[i
]))
191 set_IO_permissions(val1
, val
, rw
);
196 if (val1
== -1) val1
= 0;
199 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
200 sscanf(temp
+i
, "%d", &val
);
201 while (isdigit(temp
[i
]))
206 set_IO_permissions(val1
, val
, rw
);
210 static inline BYTE
inb( WORD port
)
213 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
217 static inline WORD
inw( WORD port
)
220 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
224 static inline DWORD
inl( WORD port
)
227 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
231 static inline void outb( BYTE value
, WORD port
)
233 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
236 static inline void outw( WORD value
, WORD port
)
238 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
241 static inline void outl( DWORD value
, WORD port
)
243 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
246 static void IO_port_init(void)
250 do_direct_port_access
= 0;
251 /* Can we do that? */
255 PROFILE_GetWineIniString( "ports", "read", "*",
256 temp
, sizeof(temp
) );
257 do_IO_port_init_read_or_write(temp
, IO_READ
);
258 PROFILE_GetWineIniString( "ports", "write", "*",
259 temp
, sizeof(temp
) );
260 do_IO_port_init_read_or_write(temp
, IO_WRITE
);
262 IO_FixCMOSCheckSum();
265 #endif /* DIRECT_IO_ACCESS */
267 /**********************************************************************
270 * Note: The size argument has to be handled correctly _externally_
271 * (as we always return a DWORD)
273 DWORD
IO_inport( int port
, int size
)
277 TRACE("%d-byte value from port 0x%02x\n", size
, port
);
280 if (do_pp_port_access
== -1)
281 do_pp_port_access
=IO_pp_init();
282 if ((do_pp_port_access
== 0 ) && (size
== 1))
283 if (!IO_pp_inp(port
,&res
))
286 #ifdef DIRECT_IO_ACCESS
287 if (do_direct_port_access
== -1) IO_port_init();
288 if ((do_direct_port_access
)
289 /* Make sure we have access to the port */
290 && (port_permissions
[port
] & IO_READ
))
295 case 1: res
= inb( port
); break;
296 case 2: res
= inw( port
); break;
297 case 4: res
= inl( port
); break;
299 ERR("invalid data size %d\n", size
);
306 /* first give the DOS VM a chance to handle it */
307 if (Dosvm
.inport
&& Dosvm
.inport( port
, size
, &res
)) return res
;
315 BYTE chan
= port
& 3;
317 if (tmr_8253
[chan
].latched
)
318 tempval
= tmr_8253
[chan
].latch
;
321 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
322 if (chan
== 0) /* System timer counter divisor */
324 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
326 tempval
= dummy_ctr
+ (WORD
)Dosvm
.GetTimer();
332 /* FIXME: intelligent hardware timer emulation needed */
337 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
340 res
= 0; /* shouldn't happen? */
342 case 1: /* read lo byte */
344 tmr_8253
[chan
].latched
= FALSE
;
346 case 3: /* read lo byte, then hi byte */
347 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
348 if (tmr_8253
[chan
].byte_toggle
)
353 /* else [fall through if read hi byte !] */
354 case 2: /* read hi byte */
355 res
= (BYTE
)(tempval
>> 8);
356 tmr_8253
[chan
].latched
= FALSE
;
362 #if 0 /* what's this port got to do with parport ? */
363 res
= (DWORD
)parport_8255
[0];
367 res
= (DWORD
)parport_8255
[1];
370 res
= (DWORD
)parport_8255
[2];
373 res
= (DWORD
)cmosaddress
;
376 res
= (DWORD
)cmosimage
[cmosaddress
& 0x3f];
380 res
= 0xffffffff; /* no joystick */
383 WARN("Direct I/O read attempted from port %x\n", port
);
387 TRACE(" returning ( 0x%lx )\n", res
);
392 /**********************************************************************
395 void IO_outport( int port
, int size
, DWORD value
)
397 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
401 if (do_pp_port_access
== -1)
402 do_pp_port_access
= IO_pp_init();
403 if ((do_pp_port_access
== 0) && (size
== 1))
404 if (!IO_pp_outp(port
,&value
))
407 #ifdef DIRECT_IO_ACCESS
409 if (do_direct_port_access
== -1) IO_port_init();
410 if ((do_direct_port_access
)
411 /* Make sure we have access to the port */
412 && (port_permissions
[port
] & IO_WRITE
))
417 case 1: outb( LOBYTE(value
), port
); break;
418 case 2: outw( LOWORD(value
), port
); break;
419 case 4: outl( value
, port
); break;
421 WARN("Invalid data size %d\n", size
);
428 /* first give the DOS VM a chance to handle it */
429 if (Dosvm
.outport
&& Dosvm
.outport( port
, size
, value
)) return;
437 BYTE chan
= port
& 3;
439 /* we need to get the oldval before any lo/hi byte change has been made */
440 if (((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
441 !tmr_8253
[chan
].byte_toggle
)
442 tmr_8253
[chan
].oldval
= tmr_8253
[chan
].countmax
;
443 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
446 break; /* shouldn't happen? */
447 case 1: /* write lo byte */
448 tmr_8253
[chan
].countmax
=
449 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
451 case 3: /* write lo byte, then hi byte */
452 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
453 if (tmr_8253
[chan
].byte_toggle
)
455 tmr_8253
[chan
].countmax
=
456 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
459 /* else [fall through if write hi byte !] */
460 case 2: /* write hi byte */
461 tmr_8253
[chan
].countmax
=
462 (tmr_8253
[chan
].countmax
& 0x00ff) | ((BYTE
)value
<< 8);
465 /* if programming is finished and value has changed
466 then update to new value */
467 if ((((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
468 !tmr_8253
[chan
].byte_toggle
) &&
469 (tmr_8253
[chan
].countmax
!= tmr_8253
[chan
].oldval
))
470 set_timer_maxval(chan
, tmr_8253
[chan
].countmax
);
475 BYTE chan
= ((BYTE
)value
& 0xc0) >> 6;
476 /* ctrl byte for specific timer channel */
479 FIXME("8254 timer readback not implemented yet\n");
482 switch (((BYTE
)value
& 0x30) >> 4)
484 case 0: /* latch timer */
485 tmr_8253
[chan
].latched
= TRUE
;
486 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
487 if (chan
== 0) /* System timer divisor */
489 tmr_8253
[chan
].latch
= dummy_ctr
+ (WORD
)Dosvm
.GetTimer();
491 tmr_8253
[chan
].latch
= dummy_ctr
;
494 /* FIXME: intelligent hardware timer emulation needed */
495 tmr_8253
[chan
].latch
= dummy_ctr
;
498 case 3: /* write lo byte, then hi byte */
499 tmr_8253
[chan
].byte_toggle
= FALSE
; /* init */
501 case 1: /* write lo byte only */
502 case 2: /* write hi byte only */
503 tmr_8253
[chan
].ctrlbyte_ch
= (BYTE
)value
;
509 parport_8255
[1] = (BYTE
)value
;
510 if ((((BYTE
)parport_8255
[1] & 3) == 3) && (tmr_8253
[2].countmax
!= 1))
512 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253
[2].countmax
);
513 Beep(1193180 / tmr_8253
[2].countmax
, 20);
517 cmosaddress
= (BYTE
)value
& 0x7f;
520 cmosimage
[cmosaddress
& 0x3f] = (BYTE
)value
;
523 WARN("Direct I/O write attempted to port %x\n", port
);