2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
9 - only a few ports are emulated.
10 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(int);
32 BOOL16 byte_toggle
; /* if TRUE, then hi byte has already been written */
38 {0xFFFF, FALSE
, 0, FALSE
, 0x06, 0},
39 {0x0012, FALSE
, 0, FALSE
, 0x44, 0},
40 {0x0001, FALSE
, 0, FALSE
, 0x86, 0},
43 static int dummy_ctr
= 0;
45 static BYTE parport_8255
[4] = {0x4f, 0x20, 0xff, 0xff};
47 static BYTE cmosaddress
;
49 static BYTE cmosimage
[64] =
51 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
52 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
53 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
54 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
55 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
57 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
58 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
61 #if defined(linux) && defined(__i386__)
62 # define DIRECT_IO_ACCESS
64 # undef DIRECT_IO_ACCESS
65 #endif /* linux && __i386__ */
67 #ifdef DIRECT_IO_ACCESS
69 extern int iopl(int level
);
71 static char do_direct_port_access
= -1;
72 static char port_permissions
[0x10000];
77 static void IO_FixCMOSCheckSum(void)
82 for (i
=0x10; i
< 0x2d; i
++)
84 cmosimage
[0x2e] = sum
>> 8; /* yes, this IS hi byte !! */
85 cmosimage
[0x2f] = sum
& 0xff;
86 TRACE("calculated hi %02x, lo %02x\n", cmosimage
[0x2e], cmosimage
[0x2f]);
89 #endif /* DIRECT_IO_ACCESS */
91 static void set_timer_maxval(unsigned timer
, unsigned maxval
)
94 case 0: /* System timer counter divisor */
95 Dosvm
.SetTimer(maxval
);
97 case 1: /* RAM refresh */
98 FIXME("RAM refresh counter handling not implemented !");
100 case 2: /* cassette & speaker */
102 if (((BYTE
)parport_8255
[1] & 3) == 3)
104 TRACE("Beep (freq: %d) !\n", 1193180 / maxval
);
105 Beep(1193180 / maxval
, 20);
111 /**********************************************************************
115 /* set_IO_permissions(int val1, int val)
116 * Helper function for IO_port_init
118 #ifdef DIRECT_IO_ACCESS
119 static void set_IO_permissions(int val1
, int val
, char rw
)
123 if (val
== -1) val
= 0x3ff;
124 for (j
= val1
; j
<= val
; j
++)
125 port_permissions
[j
] |= rw
;
127 do_direct_port_access
= 1;
130 } else if (val
!= -1) {
131 do_direct_port_access
= 1;
133 port_permissions
[val
] |= rw
;
138 /* do_IO_port_init_read_or_write(char* temp, char rw)
139 * Helper function for IO_port_init
142 static void do_IO_port_init_read_or_write(char* temp
, char rw
)
144 int val
, val1
, i
, len
;
145 if (!strcasecmp(temp
, "all")) {
146 MESSAGE("Warning!!! Granting FULL IO port access to"
147 " windoze programs!\nWarning!!! "
148 "*** THIS IS NOT AT ALL "
149 "RECOMMENDED!!! ***\n");
150 for (i
=0; i
< sizeof(port_permissions
); i
++)
151 port_permissions
[i
] |= rw
;
153 } else if (!(!strcmp(temp
, "*") || *temp
== '\0')) {
157 for (i
= 0; i
< len
; i
++) {
160 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
161 sscanf(temp
+i
, "%x", &val
);
164 sscanf(temp
+i
, "%d", &val
);
166 while (isxdigit(temp
[i
]))
173 set_IO_permissions(val1
, val
, rw
);
178 if (val1
== -1) val1
= 0;
181 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
182 sscanf(temp
+i
, "%d", &val
);
183 while (isdigit(temp
[i
]))
188 set_IO_permissions(val1
, val
, rw
);
192 static inline BYTE
inb( WORD port
)
195 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
199 static inline WORD
inw( WORD port
)
202 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
206 static inline DWORD
inl( WORD port
)
209 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
213 static inline void outb( BYTE value
, WORD port
)
215 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
218 static inline void outw( WORD value
, WORD port
)
220 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
223 static inline void outl( DWORD value
, WORD port
)
225 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
228 static void IO_port_init(void)
232 do_direct_port_access
= 0;
233 /* Can we do that? */
237 PROFILE_GetWineIniString( "ports", "read", "*",
238 temp
, sizeof(temp
) );
239 do_IO_port_init_read_or_write(temp
, IO_READ
);
240 PROFILE_GetWineIniString( "ports", "write", "*",
241 temp
, sizeof(temp
) );
242 do_IO_port_init_read_or_write(temp
, IO_WRITE
);
244 IO_FixCMOSCheckSum();
247 #endif /* DIRECT_IO_ACCESS */
249 /**********************************************************************
252 * Note: The size argument has to be handled correctly _externally_
253 * (as we always return a DWORD)
255 DWORD
IO_inport( int port
, int size
)
259 TRACE("%d-byte value from port 0x%02x\n", size
, port
);
261 #ifdef DIRECT_IO_ACCESS
262 if (do_direct_port_access
== -1) IO_port_init();
263 if ((do_direct_port_access
)
264 /* Make sure we have access to the port */
265 && (port_permissions
[port
] & IO_READ
))
270 case 1: res
= inb( port
); break;
271 case 2: res
= inw( port
); break;
272 case 4: res
= inl( port
); break;
274 ERR("invalid data size %d\n", size
);
287 BYTE chan
= port
& 3;
289 if (tmr_8253
[chan
].latched
)
290 tempval
= tmr_8253
[chan
].latch
;
293 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
294 if (chan
== 0) /* System timer counter divisor */
296 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
297 tempval
= dummy_ctr
+ (WORD
)Dosvm
.GetTimer();
301 /* FIXME: intelligent hardware timer emulation needed */
306 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
309 res
= 0; /* shouldn't happen? */
311 case 1: /* read lo byte */
313 tmr_8253
[chan
].latched
= FALSE
;
315 case 3: /* read lo byte, then hi byte */
316 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
317 if (tmr_8253
[chan
].byte_toggle
)
322 /* else [fall through if read hi byte !] */
323 case 2: /* read hi byte */
324 res
= (BYTE
)(tempval
>> 8);
325 tmr_8253
[chan
].latched
= FALSE
;
331 res
= INT_Int09ReadScan(NULL
);
332 #if 0 /* what's this port got to do with parport ? */
333 res
= (DWORD
)parport_8255
[0];
337 res
= (DWORD
)parport_8255
[1];
340 res
= (DWORD
)parport_8255
[2];
343 res
= (DWORD
)cmosaddress
;
346 res
= (DWORD
)cmosimage
[cmosaddress
& 0x3f];
350 res
= 0xffffffff; /* no joystick */
354 res
= (DWORD
)VGA_ioport_in( port
);
357 WARN("Direct I/O read attempted from port %x\n", port
);
361 TRACE(" returning ( 0x%lx )\n", res
);
366 /**********************************************************************
369 void IO_outport( int port
, int size
, DWORD value
)
371 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
374 #ifdef DIRECT_IO_ACCESS
375 if (do_direct_port_access
== -1) IO_port_init();
376 if ((do_direct_port_access
)
377 /* Make sure we have access to the port */
378 && (port_permissions
[port
] & IO_WRITE
))
383 case 1: outb( LOBYTE(value
), port
); break;
384 case 2: outw( LOWORD(value
), port
); break;
385 case 4: outl( value
, port
); break;
387 WARN("Invalid data size %d\n", size
);
397 Dosvm
.OutPIC( port
, (BYTE
)value
);
403 BYTE chan
= port
& 3;
405 /* we need to get the oldval before any lo/hi byte change has been made */
406 if (((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
407 !tmr_8253
[chan
].byte_toggle
)
408 tmr_8253
[chan
].oldval
= tmr_8253
[chan
].countmax
;
409 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
412 break; /* shouldn't happen? */
413 case 1: /* write lo byte */
414 tmr_8253
[chan
].countmax
=
415 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
417 case 3: /* write lo byte, then hi byte */
418 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
419 if (tmr_8253
[chan
].byte_toggle
)
421 tmr_8253
[chan
].countmax
=
422 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
425 /* else [fall through if write hi byte !] */
426 case 2: /* write hi byte */
427 tmr_8253
[chan
].countmax
=
428 (tmr_8253
[chan
].countmax
& 0x00ff) | ((BYTE
)value
<< 8);
431 /* if programming is finished and value has changed
432 then update to new value */
433 if ((((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
434 !tmr_8253
[chan
].byte_toggle
) &&
435 (tmr_8253
[chan
].countmax
!= tmr_8253
[chan
].oldval
))
436 set_timer_maxval(chan
, tmr_8253
[chan
].countmax
);
441 BYTE chan
= ((BYTE
)value
& 0xc0) >> 6;
442 /* ctrl byte for specific timer channel */
445 FIXME("8254 timer readback not implemented yet\n");
448 switch (((BYTE
)value
& 0x30) >> 4)
450 case 0: /* latch timer */
451 tmr_8253
[chan
].latched
= TRUE
;
452 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
453 if (chan
== 0) /* System timer divisor */
454 tmr_8253
[chan
].latch
= dummy_ctr
+ (WORD
)Dosvm
.GetTimer();
457 /* FIXME: intelligent hardware timer emulation needed */
458 tmr_8253
[chan
].latch
= dummy_ctr
;
461 case 3: /* write lo byte, then hi byte */
462 tmr_8253
[chan
].byte_toggle
= FALSE
; /* init */
464 case 1: /* write lo byte only */
465 case 2: /* write hi byte only */
466 tmr_8253
[chan
].ctrlbyte_ch
= (BYTE
)value
;
472 parport_8255
[1] = (BYTE
)value
;
473 if ((((BYTE
)parport_8255
[1] & 3) == 3) && (tmr_8253
[2].countmax
!= 1))
475 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253
[2].countmax
);
476 Beep(1193180 / tmr_8253
[2].countmax
, 20);
480 cmosaddress
= (BYTE
)value
& 0x7f;
483 cmosimage
[cmosaddress
& 0x3f] = (BYTE
)value
;
487 VGA_ioport_out( port
, (BYTE
)value
);
490 WARN("Direct I/O write attempted to port %x\n", port
);