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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 - only a few ports are emulated.
24 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(int);
45 #if defined(linux) && defined(__i386__)
46 # define DIRECT_IO_ACCESS
48 # undef DIRECT_IO_ACCESS
49 #endif /* linux && __i386__ */
53 BOOL16 byte_toggle
; /* if TRUE, then hi byte has already been written */
59 {0xFFFF, FALSE
, 0, FALSE
, 0x36, 0},
60 {0x0012, FALSE
, 0, FALSE
, 0x74, 0},
61 {0x0001, FALSE
, 0, FALSE
, 0xB6, 0},
64 static int dummy_ctr
= 0;
66 static BYTE parport_8255
[4] = {0x4f, 0x20, 0xff, 0xff};
68 static BYTE cmosaddress
;
70 static int cmos_image_initialized
= 0;
72 static BYTE cmosimage
[64] =
74 0x27, /* 0x00: seconds */
75 0x34, /* 0X01: seconds alarm */
76 0x31, /* 0x02: minutes */
77 0x47, /* 0x03: minutes alarm */
78 0x16, /* 0x04: hour */
79 0x15, /* 0x05: hour alarm */
80 0x00, /* 0x06: week day */
81 0x01, /* 0x07: month day */
82 0x04, /* 0x08: month */
83 0x94, /* 0x09: year */
84 0x26, /* 0x0a: state A */
85 0x02, /* 0x0b: state B */
86 0x50, /* 0x0c: state C */
87 0x80, /* 0x0d: state D */
88 0x00, /* 0x0e: state diagnostic */
89 0x00, /* 0x0f: state state shutdown */
90 0x40, /* 0x10: floppy type */
91 0xb1, /* 0x11: reserved */
92 0x00, /* 0x12: HD type */
93 0x9c, /* 0x13: reserved */
94 0x01, /* 0x14: equipment */
95 0x80, /* 0x15: low base memory */
96 0x02, /* 0x16: high base memory (0x280 => 640KB) */
97 0x00, /* 0x17: low extended memory */
98 0x3b, /* 0x18: high extended memory (0x3b00 => 15MB) */
99 0x00, /* 0x19: HD 1 extended type byte */
100 0x00, /* 0x1a: HD 2 extended type byte */
101 0xad, /* 0x1b: reserved */
102 0x02, /* 0x1c: reserved */
103 0x10, /* 0x1d: reserved */
104 0x00, /* 0x1e: reserved */
105 0x00, /* 0x1f: installed features */
106 0x08, /* 0x20: HD 1 low cylinder number */
107 0x00, /* 0x21: HD 1 high cylinder number */
108 0x00, /* 0x22: HD 1 heads */
109 0x26, /* 0x23: HD 1 low pre-compensation start */
110 0x00, /* 0x24: HD 1 high pre-compensation start */
111 0x00, /* 0x25: HD 1 low landing zone */
112 0x00, /* 0x26: HD 1 high landing zone */
113 0x00, /* 0x27: HD 1 sectors */
114 0x00, /* 0x28: options 1 */
115 0x00, /* 0x29: reserved */
116 0x00, /* 0x2a: reserved */
117 0x00, /* 0x2b: options 2 */
118 0x00, /* 0x2c: options 3 */
119 0x3f, /* 0x2d: reserved */
120 0xcc, /* 0x2e: low CMOS ram checksum (computed automatically) */
121 0xcc, /* 0x2f: high CMOS ram checksum (computed automatically) */
122 0x00, /* 0x30: low extended memory byte */
123 0x1c, /* 0x31: high extended memory byte */
124 0x19, /* 0x32: century byte */
125 0x81, /* 0x33: setup information */
126 0x00, /* 0x34: CPU speed */
127 0x0e, /* 0x35: HD 2 low cylinder number */
128 0x00, /* 0x36: HD 2 high cylinder number */
129 0x80, /* 0x37: HD 2 heads */
130 0x1b, /* 0x38: HD 2 low pre-compensation start */
131 0x7b, /* 0x39: HD 2 high pre-compensation start */
132 0x21, /* 0x3a: HD 2 low landing zone */
133 0x00, /* 0x3b: HD 2 high landing zone */
134 0x00, /* 0x3c: HD 2 sectors */
135 0x00, /* 0x3d: reserved */
136 0x05, /* 0x3e: reserved */
137 0x5f /* 0x3f: reserved */
140 static void IO_FixCMOSCheckSum(void)
145 for (i
=0x10; i
< 0x2d; i
++)
147 cmosimage
[0x2e] = sum
>> 8; /* yes, this IS hi byte !! */
148 cmosimage
[0x2f] = sum
& 0xff;
149 TRACE("calculated hi %02x, lo %02x\n", cmosimage
[0x2e], cmosimage
[0x2f]);
152 #ifdef DIRECT_IO_ACCESS
154 extern int iopl(int level
);
155 static char do_direct_port_access
= -1;
156 static char port_permissions
[0x10000];
161 #endif /* DIRECT_IO_ACCESS */
164 static int do_pp_port_access
= -1; /* -1: uninitialized, 1: not available
168 static void set_timer_maxval(unsigned timer
, unsigned maxval
)
171 case 0: /* System timer counter divisor */
172 DOSVM_SetTimer(maxval
);
174 case 1: /* RAM refresh */
175 FIXME("RAM refresh counter handling not implemented !\n");
177 case 2: /* cassette & speaker */
179 if ((parport_8255
[1] & 3) == 3)
181 TRACE("Beep (freq: %d) !\n", 1193180 / maxval
);
182 Beep(1193180 / maxval
, 20);
189 /**********************************************************************
193 /* set_IO_permissions(int val1, int val)
194 * Helper function for IO_port_init
196 #ifdef DIRECT_IO_ACCESS
197 static void set_IO_permissions(int val1
, int val
, char rw
)
201 if (val
== -1) val
= 0x3ff;
202 for (j
= val1
; j
<= val
; j
++)
203 port_permissions
[j
] |= rw
;
205 do_direct_port_access
= 1;
208 } else if (val
!= -1) {
209 do_direct_port_access
= 1;
211 port_permissions
[val
] |= rw
;
216 /* do_IO_port_init_read_or_write(char* temp, char rw)
217 * Helper function for IO_port_init
220 static void do_IO_port_init_read_or_write(const WCHAR
*str
, char rw
)
225 static const WCHAR allW
[] = {'a','l','l',0};
227 if (!strcmpiW(str
, allW
))
229 for (i
=0; i
< sizeof(port_permissions
); i
++)
230 port_permissions
[i
] |= rw
;
243 set_IO_permissions(val1
, val
, rw
);
250 if (val1
== -1) val1
= 0;
256 val
= strtoulW( str
, &end
, 0 );
267 set_IO_permissions(val1
, val
, rw
);
271 static inline BYTE
inb( WORD port
)
274 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
278 static inline WORD
inw( WORD port
)
281 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
285 static inline DWORD
inl( WORD port
)
288 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
292 static inline void outb( BYTE value
, WORD port
)
294 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
297 static inline void outw( WORD value
, WORD port
)
299 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
302 static inline void outl( DWORD value
, WORD port
)
304 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
307 static void IO_port_init(void)
312 OBJECT_ATTRIBUTES attr
;
313 UNICODE_STRING nameW
;
315 static const WCHAR portsW
[] = {'S','o','f','t','w','a','r','e','\\',
316 'W','i','n','e','\\','V','D','M','\\','P','o','r','t','s',0};
317 static const WCHAR readW
[] = {'r','e','a','d',0};
318 static const WCHAR writeW
[] = {'w','r','i','t','e',0};
320 do_direct_port_access
= 0;
321 /* Can we do that? */
326 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
327 attr
.Length
= sizeof(attr
);
328 attr
.RootDirectory
= root
;
329 attr
.ObjectName
= &nameW
;
331 attr
.SecurityDescriptor
= NULL
;
332 attr
.SecurityQualityOfService
= NULL
;
333 RtlInitUnicodeString( &nameW
, portsW
);
335 /* @@ Wine registry key: HKCU\Software\Wine\VDM\Ports */
336 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
338 RtlInitUnicodeString( &nameW
, readW
);
339 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
341 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
342 do_IO_port_init_read_or_write(str
, IO_READ
);
344 RtlInitUnicodeString( &nameW
, writeW
);
345 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
347 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
348 do_IO_port_init_read_or_write(str
, IO_WRITE
);
356 #endif /* DIRECT_IO_ACCESS */
359 /**********************************************************************
362 * Note: The size argument has to be handled correctly _externally_
363 * (as we always return a DWORD)
365 DWORD WINAPI
DOSVM_inport( int port
, int size
)
369 TRACE("%d-byte value from port 0x%04x\n", size
, port
);
372 if (do_pp_port_access
== -1) do_pp_port_access
=IO_pp_init();
373 if ((do_pp_port_access
== 0 ) && (size
== 1))
375 if (!IO_pp_inp(port
,&res
)) return res
;
379 #ifdef DIRECT_IO_ACCESS
380 if (do_direct_port_access
== -1) IO_port_init();
381 if ((do_direct_port_access
)
382 /* Make sure we have access to the port */
383 && (port_permissions
[port
] & IO_READ
))
388 case 1: res
= inb( port
); break;
389 case 2: res
= inw( port
); break;
390 case 4: res
= inl( port
); break;
403 BYTE chan
= port
& 3;
405 if (tmr_8253
[chan
].latched
)
406 tempval
= tmr_8253
[chan
].latch
;
409 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
410 if (chan
== 0) /* System timer counter divisor */
412 /* FIXME: DOSVM_GetTimer() returns quite rigid values */
413 tempval
= dummy_ctr
+ (WORD
)DOSVM_GetTimer();
417 /* FIXME: intelligent hardware timer emulation needed */
422 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
425 res
= 0; /* shouldn't happen? */
427 case 1: /* read lo byte */
429 tmr_8253
[chan
].latched
= FALSE
;
431 case 3: /* read lo byte, then hi byte */
432 tmr_8253
[chan
].byte_toggle
^= 1; /* toggle */
433 if (tmr_8253
[chan
].byte_toggle
)
438 /* else [fall through if read hi byte !] */
439 case 2: /* read hi byte */
440 res
= (BYTE
)(tempval
>> 8);
441 tmr_8253
[chan
].latched
= FALSE
;
447 res
= DOSVM_Int09ReadScan(NULL
);
450 res
= (DWORD
)parport_8255
[1];
453 res
= (DWORD
)parport_8255
[2];
456 res
= (DWORD
)cmosaddress
;
459 if (!cmos_image_initialized
)
461 IO_FixCMOSCheckSum();
462 cmos_image_initialized
= 1;
464 res
= (DWORD
)cmosimage
[cmosaddress
& 0x3f];
468 res
= ~0U; /* no joystick */
473 res
= (DWORD
)SB_ioport_in( port
);
475 /* VGA read registers */
476 case 0x3b4: /* CRT Controller Register - Index (MDA) */
477 case 0x3b5: /* CRT Controller Register - Other (MDA) */
478 case 0x3ba: /* General Register - Input status 1 (MDA) */
479 case 0x3c0: /* Attribute Controller - Address */
480 case 0x3c1: /* Attribute Controller - Other */
481 case 0x3c2: /* General Register - Input status 0 */
482 case 0x3c3: /* General Register - Video subsystem enable */
483 case 0x3c4: /* Sequencer Register - Address */
484 case 0x3c5: /* Sequencer Register - Other */
486 case 0x3c7: /* General Register - DAC State */
489 case 0x3ca: /* General Register - Feature control */
491 case 0x3cc: /* General Register - Misc output */
493 case 0x3ce: /* Graphics Controller Register - Address */
494 case 0x3cf: /* Graphics Controller Register - Other */
499 case 0x3d4: /* CRT Controller Register - Index (CGA) */
500 case 0x3d5: /* CRT Controller Register - Other (CGA) */
512 FIXME("Trying to read more than one byte from VGA!\n");
513 res
= (DWORD
)VGA_ioport_in( port
);
549 res
= (DWORD
)DMA_ioport_in( port
);
552 WARN("Direct I/O read attempted from port %x\n", port
);
559 /**********************************************************************
560 * outport (WINEDOS.@)
562 void WINAPI
DOSVM_outport( int port
, int size
, DWORD value
)
564 TRACE("IO: 0x%x (%d-byte value) to port 0x%04x\n", value
, size
, port
);
567 if (do_pp_port_access
== -1) do_pp_port_access
= IO_pp_init();
568 if ((do_pp_port_access
== 0) && (size
== 1))
570 if (!IO_pp_outp(port
,&value
)) return;
574 #ifdef DIRECT_IO_ACCESS
576 if (do_direct_port_access
== -1) IO_port_init();
577 if ((do_direct_port_access
)
578 /* Make sure we have access to the port */
579 && (port_permissions
[port
] & IO_WRITE
))
584 case 1: outb( LOBYTE(value
), port
); break;
585 case 2: outw( LOWORD(value
), port
); break;
586 case 4: outl( value
, port
); break;
596 DOSVM_PIC_ioport_out( port
, (BYTE
)value
);
602 BYTE chan
= port
& 3;
604 /* we need to get the oldval before any lo/hi byte change has been made */
605 if (((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
606 !tmr_8253
[chan
].byte_toggle
)
607 tmr_8253
[chan
].oldval
= tmr_8253
[chan
].countmax
;
608 switch ((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) >> 4)
611 break; /* shouldn't happen? */
612 case 1: /* write lo byte */
613 tmr_8253
[chan
].countmax
=
614 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
616 case 3: /* write lo byte, then hi byte */
617 tmr_8253
[chan
].byte_toggle
^= TRUE
; /* toggle */
618 if (tmr_8253
[chan
].byte_toggle
)
620 tmr_8253
[chan
].countmax
=
621 (tmr_8253
[chan
].countmax
& 0xff00) | (BYTE
)value
;
624 /* else [fall through if write hi byte !] */
625 case 2: /* write hi byte */
626 tmr_8253
[chan
].countmax
=
627 (tmr_8253
[chan
].countmax
& 0x00ff) | ((BYTE
)value
<< 8);
630 /* if programming is finished and value has changed
631 then update to new value */
632 if ((((tmr_8253
[chan
].ctrlbyte_ch
& 0x30) != 0x30) ||
633 !tmr_8253
[chan
].byte_toggle
) &&
634 (tmr_8253
[chan
].countmax
!= tmr_8253
[chan
].oldval
))
635 set_timer_maxval(chan
, tmr_8253
[chan
].countmax
);
640 BYTE chan
= ((BYTE
)value
& 0xc0) >> 6;
641 /* ctrl byte for specific timer channel */
644 FIXME("8254 timer readback not implemented yet\n");
647 switch (((BYTE
)value
& 0x30) >> 4)
649 case 0: /* latch timer */
650 tmr_8253
[chan
].latched
= TRUE
;
651 dummy_ctr
-= 1 + (int)(10.0 * rand() / (RAND_MAX
+ 1.0));
652 if (chan
== 0) /* System timer divisor */
653 tmr_8253
[chan
].latch
= dummy_ctr
+ (WORD
)DOSVM_GetTimer();
656 /* FIXME: intelligent hardware timer emulation needed */
657 tmr_8253
[chan
].latch
= dummy_ctr
;
660 case 3: /* write lo byte, then hi byte */
661 tmr_8253
[chan
].byte_toggle
= FALSE
; /* init */
663 case 1: /* write lo byte only */
664 case 2: /* write hi byte only */
665 tmr_8253
[chan
].ctrlbyte_ch
= (BYTE
)value
;
671 parport_8255
[1] = (BYTE
)value
;
672 if (((parport_8255
[1] & 3) == 3) && (tmr_8253
[2].countmax
!= 1))
674 TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253
[2].countmax
);
675 Beep(1193180 / tmr_8253
[2].countmax
, 20);
679 cmosaddress
= (BYTE
)value
& 0x7f;
682 if (!cmos_image_initialized
)
684 IO_FixCMOSCheckSum();
685 cmos_image_initialized
= 1;
687 cmosimage
[cmosaddress
& 0x3f] = (BYTE
)value
;
691 SB_ioport_out( port
, (BYTE
)value
);
693 /* VGA Write registers */
694 case 0x3b4: /* CRT Controller Register - Index (MDA) */
695 case 0x3b5: /* CRT Controller Register - Other (MDA) */
696 case 0x3ba: /* General Register - Feature Control */
697 case 0x3c0: /* Attribute Controller - Address/Other */
699 case 0x3c2: /* General Register - Misc output */
700 case 0x3c3: /* General Register - Video subsystem enable */
701 case 0x3c4: /* Sequencer Register - Address */
702 case 0x3c5: /* Sequencer Register - Other */
711 case 0x3ce: /* Graphics Controller Register - Address */
712 case 0x3cf: /* Graphics Controller Register - Other */
717 case 0x3d4: /* CRT Controller Register - Index (CGA) */
718 case 0x3d5: /* CRT Controller Register - Other (CGA) */
729 VGA_ioport_out( port
, LOBYTE(value
) );
731 VGA_ioport_out( port
+1, HIBYTE(value
) );
733 VGA_ioport_out( port
+2, LOBYTE(HIWORD(value
)) );
734 VGA_ioport_out( port
+3, HIBYTE(HIWORD(value
)) );
784 DMA_ioport_out( port
, (BYTE
)value
);
787 WARN("Direct I/O write attempted to port %x\n", port
);