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
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(int)
31 BOOL16 byte_toggle
; /* if TRUE, then hi byte has already been written */
37 {0xFFFF, FALSE
, 0, FALSE
, 0x06, 0},
38 {0x0012, FALSE
, 0, FALSE
, 0x44, 0},
39 {0x0001, FALSE
, 0, FALSE
, 0x86, 0},
42 static int dummy_ctr
= 0;
44 static BYTE parport_8255
[4] = {0x4f, 0x20, 0xff, 0xff};
46 static BYTE cmosaddress
;
48 static BYTE cmosimage
[64] =
50 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
51 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
52 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
53 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
54 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
56 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
57 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
60 #if defined(linux) && defined(__i386__)
61 # define DIRECT_IO_ACCESS
63 # undef DIRECT_IO_ACCESS
64 #endif /* linux && __i386__ */
66 #ifdef DIRECT_IO_ACCESS
68 extern int iopl(int level
);
70 static char do_direct_port_access
= -1;
71 static char port_permissions
[0x10000];
76 static void IO_FixCMOSCheckSum(void)
81 for (i
=0x10; i
< 0x2d; i
++)
83 cmosimage
[0x2e] = sum
>> 8; /* yes, this IS hi byte !! */
84 cmosimage
[0x2f] = sum
& 0xff;
85 TRACE("calculated hi %02x, lo %02x\n", cmosimage
[0x2e], cmosimage
[0x2f]);
88 #endif /* DIRECT_IO_ACCESS */
90 static void set_timer_maxval(unsigned timer
, unsigned maxval
)
93 case 0: /* System timer counter divisor */
94 DOSVM_SetTimer(maxval
);
96 case 1: /* RAM refresh */
97 FIXME("RAM refresh counter handling not implemented !");
99 case 2: /* cassette & speaker */
101 if (((BYTE
)parport_8255
[1] & 3) == 3)
103 TRACE("Beep (freq: %d) !\n", 1193180 / maxval
);
104 Beep(1193180 / maxval
, 20);
110 /**********************************************************************
114 /* set_IO_permissions(int val1, int val)
115 * Helper function for IO_port_init
117 #ifdef DIRECT_IO_ACCESS
118 static void set_IO_permissions(int val1
, int val
, char rw
)
122 if (val
== -1) val
= 0x3ff;
123 for (j
= val1
; j
<= val
; j
++)
124 port_permissions
[j
] |= rw
;
126 do_direct_port_access
= 1;
129 } else if (val
!= -1) {
130 do_direct_port_access
= 1;
132 port_permissions
[val
] |= rw
;
137 /* do_IO_port_init_read_or_write(char* temp, char rw)
138 * Helper function for IO_port_init
141 static void do_IO_port_init_read_or_write(char* temp
, char rw
)
143 int val
, val1
, i
, len
;
144 if (!strcasecmp(temp
, "all")) {
145 MESSAGE("Warning!!! Granting FULL IO port access to"
146 " windoze programs!\nWarning!!! "
147 "*** THIS IS NOT AT ALL "
148 "RECOMMENDED!!! ***\n");
149 for (i
=0; i
< sizeof(port_permissions
); i
++)
150 port_permissions
[i
] |= rw
;
152 } else if (!(!strcmp(temp
, "*") || *temp
== '\0')) {
156 for (i
= 0; i
< len
; i
++) {
159 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
160 sscanf(temp
+i
, "%x", &val
);
163 sscanf(temp
+i
, "%d", &val
);
165 while (isxdigit(temp
[i
]))
172 set_IO_permissions(val1
, val
, rw
);
177 if (val1
== -1) val1
= 0;
180 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
181 sscanf(temp
+i
, "%d", &val
);
182 while (isdigit(temp
[i
]))
187 set_IO_permissions(val1
, val
, rw
);
191 static inline BYTE
inb( WORD port
)
194 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
198 static inline WORD
inw( WORD port
)
201 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
205 static inline DWORD
inl( WORD port
)
208 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
212 static inline void outb( BYTE value
, WORD port
)
214 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
217 static inline void outw( WORD value
, WORD port
)
219 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
222 static inline void outl( DWORD value
, WORD port
)
224 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
227 static void IO_port_init(void)
231 do_direct_port_access
= 0;
232 /* Can we do that? */
236 PROFILE_GetWineIniString( "ports", "read", "*",
237 temp
, sizeof(temp
) );
238 do_IO_port_init_read_or_write(temp
, IO_READ
);
239 PROFILE_GetWineIniString( "ports", "write", "*",
240 temp
, sizeof(temp
) );
241 do_IO_port_init_read_or_write(temp
, IO_WRITE
);
243 IO_FixCMOSCheckSum();
246 #endif /* DIRECT_IO_ACCESS */
248 /**********************************************************************
251 * Note: The size argument has to be handled correctly _externally_
252 * (as we always return a DWORD)
254 DWORD
IO_inport( int port
, int size
)
258 TRACE("%d-byte value from port 0x%02x\n", size
, port
);
260 #ifdef DIRECT_IO_ACCESS
261 if (do_direct_port_access
== -1) IO_port_init();
262 if ((do_direct_port_access
)
263 /* Make sure we have access to the port */
264 && (port_permissions
[port
] & IO_READ
))
269 case 1: res
= inb( port
); break;
270 case 2: res
= inw( port
); break;
271 case 4: res
= inl( port
); break;
273 ERR("invalid data size %d\n", size
);
286 BYTE chan
= port
& 3;
288 if (tmr_8253
[chan
].latched
)
289 tempval
= tmr_8253
[chan
].latch
;
292 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
293 if (chan
== 0) /* System timer counter divisor */
295 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
296 tempval
= dummy_ctr
+ (WORD
)DOSVM_GetTimer();
300 /* FIXME: intelligent hardware timer emulation needed */
305 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
308 res
= 0; /* shouldn't happen? */
310 case 1: /* read lo byte */
312 tmr_8253
[chan
].latched
= FALSE
;
314 case 3: /* read lo byte, then hi byte */
315 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
316 if (tmr_8253
[chan
].byte_toggle
)
321 /* else [fall through if read hi byte !] */
322 case 2: /* read hi byte */
323 res
= (BYTE
)(tempval
>> 8);
324 tmr_8253
[chan
].latched
= FALSE
;
330 res
= INT_Int09ReadScan(NULL
);
331 #if 0 /* what's this port got to do with parport ? */
332 res
= (DWORD
)parport_8255
[0];
336 res
= (DWORD
)parport_8255
[1];
339 res
= (DWORD
)parport_8255
[2];
342 res
= (DWORD
)cmosaddress
;
345 res
= (DWORD
)cmosimage
[cmosaddress
& 0x3f];
349 res
= 0xffffffff; /* no joystick */
353 res
= (DWORD
)VGA_ioport_in( port
);
356 WARN("Direct I/O read attempted from port %x\n", port
);
360 TRACE(" returning ( 0x%lx )\n", res
);
365 /**********************************************************************
368 void IO_outport( int port
, int size
, DWORD value
)
370 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
373 #ifdef DIRECT_IO_ACCESS
374 if (do_direct_port_access
== -1) IO_port_init();
375 if ((do_direct_port_access
)
376 /* Make sure we have access to the port */
377 && (port_permissions
[port
] & IO_WRITE
))
382 case 1: outb( LOBYTE(value
), port
); break;
383 case 2: outw( LOWORD(value
), port
); break;
384 case 4: outl( value
, port
); break;
386 WARN("Invalid data size %d\n", size
);
396 DOSVM_PIC_ioport_out( port
, (BYTE
)value
);
402 BYTE chan
= port
& 3;
404 /* we need to get the oldval before any lo/hi byte change has been made */
405 if (((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
406 !tmr_8253
[chan
].byte_toggle
)
407 tmr_8253
[chan
].oldval
= tmr_8253
[chan
].countmax
;
408 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
411 break; /* shouldn't happen? */
412 case 1: /* write lo byte */
413 tmr_8253
[chan
].countmax
=
414 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
416 case 3: /* write lo byte, then hi byte */
417 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
418 if (tmr_8253
[chan
].byte_toggle
)
420 tmr_8253
[chan
].countmax
=
421 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
424 /* else [fall through if write hi byte !] */
425 case 2: /* write hi byte */
426 tmr_8253
[chan
].countmax
=
427 (tmr_8253
[chan
].countmax
& 0x00ff) | ((BYTE
)value
<< 8);
430 /* if programming is finished and value has changed
431 then update to new value */
432 if ((((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
433 !tmr_8253
[chan
].byte_toggle
) &&
434 (tmr_8253
[chan
].countmax
!= tmr_8253
[chan
].oldval
))
435 set_timer_maxval(chan
, tmr_8253
[chan
].countmax
);
440 BYTE chan
= ((BYTE
)value
& 0xc0) >> 6;
441 /* ctrl byte for specific timer channel */
444 FIXME("8254 timer readback not implemented yet\n");
447 switch (((BYTE
)value
& 0x30) >> 4)
449 case 0: /* latch timer */
450 tmr_8253
[chan
].latched
= TRUE
;
451 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
452 if (chan
== 0) /* System timer divisor */
453 tmr_8253
[chan
].latch
= dummy_ctr
+ (WORD
)DOSVM_GetTimer();
456 /* FIXME: intelligent hardware timer emulation needed */
457 tmr_8253
[chan
].latch
= dummy_ctr
;
460 case 3: /* write lo byte, then hi byte */
461 tmr_8253
[chan
].byte_toggle
= FALSE
; /* init */
463 case 1: /* write lo byte only */
464 case 2: /* write hi byte only */
465 tmr_8253
[chan
].ctrlbyte_ch
= (BYTE
)value
;
471 parport_8255
[1] = (BYTE
)value
;
472 if ((((BYTE
)parport_8255
[1] & 3) == 3) && (tmr_8253
[2].countmax
!= 1))
474 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253
[2].countmax
);
475 Beep(1193180 / tmr_8253
[2].countmax
, 20);
479 cmosaddress
= (BYTE
)value
& 0x7f;
482 cmosimage
[cmosaddress
& 0x3f] = (BYTE
)value
;
486 VGA_ioport_out( port
, (BYTE
)value
);
489 WARN("Direct I/O write attempted to port %x\n", port
);