Added ctrl-c support.
[wine.git] / msdos / ioports.c
blob69d275c1d38fc21cfa21b8e707570d069c529648
1 /*
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
22 /* Known problems:
23 - only a few ports are emulated.
24 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
25 fix that, I guess.
28 #include "config.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include "windef.h"
37 #include "callback.h"
38 #include "file.h"
39 #include "miscemu.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(int);
44 static struct {
45 WORD countmax;
46 BOOL16 byte_toggle; /* if TRUE, then hi byte has already been written */
47 WORD latch;
48 BOOL16 latched;
49 BYTE ctrlbyte_ch;
50 WORD oldval;
51 } tmr_8253[3] = {
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
77 #else
78 # undef DIRECT_IO_ACCESS
79 # undef PP_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;
86 #ifdef HAVE_PPDEV
87 static char do_pp_port_access = -1; /* -1: uninitialized, 1: not available
88 0: available);*/
89 #endif
90 static char port_permissions[0x10000];
92 #define IO_READ 1
93 #define IO_WRITE 2
95 static void IO_FixCMOSCheckSum(void)
97 WORD sum = 0;
98 int i;
100 for (i=0x10; i < 0x2d; i++)
101 sum += cmosimage[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)
111 switch (timer) {
112 case 0: /* System timer counter divisor */
113 if (Dosvm.SetTimer) Dosvm.SetTimer(maxval);
114 break;
115 case 1: /* RAM refresh */
116 FIXME("RAM refresh counter handling not implemented !\n");
117 break;
118 case 2: /* cassette & speaker */
119 /* speaker on ? */
120 if (((BYTE)parport_8255[1] & 3) == 3)
122 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
123 Beep(1193180 / maxval, 20);
125 break;
129 /**********************************************************************
130 * IO_port_init
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)
139 int j;
140 if (val1 != -1) {
141 if (val == -1) val = 0x3ff;
142 for (j = val1; j <= val; j++)
143 port_permissions[j] |= rw;
145 do_direct_port_access = 1;
147 val1 = -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')) {
172 len = strlen(temp);
173 val = -1;
174 val1 = -1;
175 for (i = 0; i < len; i++) {
176 switch (temp[i]) {
177 case '0':
178 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
179 sscanf(temp+i, "%x", &val);
180 i += 2;
181 } else {
182 sscanf(temp+i, "%d", &val);
184 while (isxdigit(temp[i]))
185 i++;
186 i--;
187 break;
188 case ',':
189 case ' ':
190 case '\t':
191 set_IO_permissions(val1, val, rw);
192 val1 = -1; val = -1;
193 break;
194 case '-':
195 val1 = val;
196 if (val1 == -1) val1 = 0;
197 break;
198 default:
199 if (temp[i] >= '0' && temp[i] <= '9') {
200 sscanf(temp+i, "%d", &val);
201 while (isdigit(temp[i]))
202 i++;
206 set_IO_permissions(val1, val, rw);
210 static inline BYTE inb( WORD port )
212 BYTE b;
213 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
214 return b;
217 static inline WORD inw( WORD port )
219 WORD w;
220 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
221 return w;
224 static inline DWORD inl( WORD port )
226 DWORD dw;
227 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
228 return dw;
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)
248 char temp[1024];
250 do_direct_port_access = 0;
251 /* Can we do that? */
252 if (!iopl(3)) {
253 iopl(0);
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 /**********************************************************************
268 * IO_inport
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 )
275 DWORD res = 0;
277 TRACE("%d-byte value from port 0x%02x\n", size, port );
279 #ifdef HAVE_PPDEV
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))
284 return res;
285 #endif
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))
292 iopl(3);
293 switch(size)
295 case 1: res = inb( port ); break;
296 case 2: res = inw( port ); break;
297 case 4: res = inl( port ); break;
298 default:
299 ERR("invalid data size %d\n", size);
301 iopl(0);
302 return res;
304 #endif
306 /* first give the DOS VM a chance to handle it */
307 if (Dosvm.inport && Dosvm.inport( port, size, &res )) return res;
309 switch (port)
311 case 0x40:
312 case 0x41:
313 case 0x42:
315 BYTE chan = port & 3;
316 WORD tempval = 0;
317 if (tmr_8253[chan].latched)
318 tempval = tmr_8253[chan].latch;
319 else
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 */
325 if (Dosvm.GetTimer)
326 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
327 else
328 tempval = dummy_ctr;
330 else
332 /* FIXME: intelligent hardware timer emulation needed */
333 tempval = dummy_ctr;
337 switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
339 case 0:
340 res = 0; /* shouldn't happen? */
341 break;
342 case 1: /* read lo byte */
343 res = (BYTE)tempval;
344 tmr_8253[chan].latched = FALSE;
345 break;
346 case 3: /* read lo byte, then hi byte */
347 tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
348 if (tmr_8253[chan].byte_toggle)
350 res = (BYTE)tempval;
351 break;
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;
357 break;
360 break;
361 case 0x60:
362 #if 0 /* what's this port got to do with parport ? */
363 res = (DWORD)parport_8255[0];
364 #endif
365 break;
366 case 0x61:
367 res = (DWORD)parport_8255[1];
368 break;
369 case 0x62:
370 res = (DWORD)parport_8255[2];
371 break;
372 case 0x70:
373 res = (DWORD)cmosaddress;
374 break;
375 case 0x71:
376 res = (DWORD)cmosimage[cmosaddress & 0x3f];
377 break;
378 case 0x200:
379 case 0x201:
380 res = 0xffffffff; /* no joystick */
381 break;
382 default:
383 WARN("Direct I/O read attempted from port %x\n", port);
384 res = 0xffffffff;
385 break;
387 TRACE(" returning ( 0x%lx )\n", res );
388 return res;
392 /**********************************************************************
393 * IO_outport
395 void IO_outport( int port, int size, DWORD value )
397 TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
398 value, size, port );
400 #ifdef HAVE_PPDEV
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))
405 return;
406 #endif
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))
414 iopl(3);
415 switch(size)
417 case 1: outb( LOBYTE(value), port ); break;
418 case 2: outw( LOWORD(value), port ); break;
419 case 4: outl( value, port ); break;
420 default:
421 WARN("Invalid data size %d\n", size);
423 iopl(0);
424 return;
426 #endif
428 /* first give the DOS VM a chance to handle it */
429 if (Dosvm.outport && Dosvm.outport( port, size, value )) return;
431 switch (port)
433 case 0x40:
434 case 0x41:
435 case 0x42:
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)
445 case 0:
446 break; /* shouldn't happen? */
447 case 1: /* write lo byte */
448 tmr_8253[chan].countmax =
449 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
450 break;
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;
457 break;
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);
463 break;
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);
472 break;
473 case 0x43:
475 BYTE chan = ((BYTE)value & 0xc0) >> 6;
476 /* ctrl byte for specific timer channel */
477 if (chan == 3)
479 FIXME("8254 timer readback not implemented yet\n");
480 break;
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 */
488 if (Dosvm.GetTimer)
489 tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
490 else
491 tmr_8253[chan].latch = dummy_ctr;
492 else
494 /* FIXME: intelligent hardware timer emulation needed */
495 tmr_8253[chan].latch = dummy_ctr;
497 break;
498 case 3: /* write lo byte, then hi byte */
499 tmr_8253[chan].byte_toggle = FALSE; /* init */
500 /* fall through */
501 case 1: /* write lo byte only */
502 case 2: /* write hi byte only */
503 tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
504 break;
507 break;
508 case 0x61:
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);
515 break;
516 case 0x70:
517 cmosaddress = (BYTE)value & 0x7f;
518 break;
519 case 0x71:
520 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
521 break;
522 default:
523 WARN("Direct I/O write attempted to port %x\n", port );
524 break;