Recovery of release 990110 after disk crash.
[wine/multimedia.git] / msdos / ioports.c
blobd6adf753b2f1cc5f0025cb2b4a9b8d8ce6528635
1 /*
2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
5 * Copyright 1998 Andreas Mohr, Ove Kaaven
6 */
8 /* Known problems:
9 - only a few ports are emulated.
10 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
11 fix that, I guess.
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include "windows.h"
20 #include "vga.h"
21 #include "dosexe.h"
22 #include "options.h"
23 #include "debug.h"
25 static WORD tmr_8253_countmax[3] = {0xffff, 0x12, 1}; /* [2] needs to be 1 ! */
26 /* if byte_toggle is TRUE, then hi byte has already been written */
27 static BOOL16 tmr_8253_byte_toggle[3] = {FALSE, FALSE, FALSE};
28 static BYTE tmr_8253_ctrlbyte_ch[4] = {0x06, 0x44, 0x86, 0};
29 static int dummy_ctr = 0;
31 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
33 static BYTE cmosaddress;
35 static BYTE cmosimage[64] =
37 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
38 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
39 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
40 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
41 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
43 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
44 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
47 #if defined(linux) && defined(__i386__)
48 # define DIRECT_IO_ACCESS
49 #else
50 # undef DIRECT_IO_ACCESS
51 #endif /* linux && __i386__ */
53 #ifdef DIRECT_IO_ACCESS
55 extern int iopl(int level);
57 static char do_direct_port_access = 0;
58 static char port_permissions[0x10000];
60 #define IO_READ 1
61 #define IO_WRITE 2
63 #endif /* DIRECT_IO_ACCESS */
65 static void IO_FixCMOSCheckSum(void)
67 WORD sum = 0;
68 int i;
70 for (i=0x10; i < 0x2d; i++)
71 sum += cmosimage[i];
72 cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
73 cmosimage[0x2f] = sum & 0xff;
74 TRACE(int, "calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
77 static void set_timer_maxval(unsigned timer, unsigned maxval)
79 switch (timer) {
80 case 0: /* System timer counter divisor */
81 DOSVM_SetTimer(maxval);
82 break;
83 case 1: /* RAM refresh */
84 FIXME(int, "RAM refresh counter handling not implemented !");
85 break;
86 case 2: /* cassette & speaker */
87 /* speaker on ? */
88 if (((BYTE)parport_8255[1] & 3) == 3)
90 TRACE(int, "Beep (freq: %d) !\n", 1193180 / maxval );
91 Beep(1193180 / maxval, 20);
93 break;
97 /**********************************************************************
98 * IO_port_init
101 /* set_IO_permissions(int val1, int val)
102 * Helper function for IO_port_init
104 #ifdef DIRECT_IO_ACCESS
105 static void set_IO_permissions(int val1, int val, char rw)
107 int j;
108 if (val1 != -1) {
109 if (val == -1) val = 0x3ff;
110 for (j = val1; j <= val; j++)
111 port_permissions[j] |= rw;
113 do_direct_port_access = 1;
115 val1 = -1;
116 } else if (val != -1) {
117 do_direct_port_access = 1;
119 port_permissions[val] |= rw;
124 /* do_IO_port_init_read_or_write(char* temp, char rw)
125 * Helper function for IO_port_init
128 static void do_IO_port_init_read_or_write(char* temp, char rw)
130 int val, val1, i, len;
131 if (!strcasecmp(temp, "all")) {
132 MSG("Warning!!! Granting FULL IO port access to"
133 " windoze programs!\nWarning!!! "
134 "*** THIS IS NOT AT ALL "
135 "RECOMMENDED!!! ***\n");
136 for (i=0; i < sizeof(port_permissions); i++)
137 port_permissions[i] |= rw;
139 } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
140 len = strlen(temp);
141 val = -1;
142 val1 = -1;
143 for (i = 0; i < len; i++) {
144 switch (temp[i]) {
145 case '0':
146 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
147 sscanf(temp+i, "%x", &val);
148 i += 2;
149 } else {
150 sscanf(temp+i, "%d", &val);
152 while (isxdigit(temp[i]))
153 i++;
154 i--;
155 break;
156 case ',':
157 case ' ':
158 case '\t':
159 set_IO_permissions(val1, val, rw);
160 val1 = -1; val = -1;
161 break;
162 case '-':
163 val1 = val;
164 if (val1 == -1) val1 = 0;
165 break;
166 default:
167 if (temp[i] >= '0' && temp[i] <= '9') {
168 sscanf(temp+i, "%d", &val);
169 while (isdigit(temp[i]))
170 i++;
174 set_IO_permissions(val1, val, rw);
178 static __inline__ BYTE inb( WORD port )
180 BYTE b;
181 __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
182 return b;
185 static __inline__ WORD inw( WORD port )
187 WORD w;
188 __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
189 return w;
192 static __inline__ DWORD inl( WORD port )
194 DWORD dw;
195 __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
196 return dw;
199 static __inline__ void outb( BYTE value, WORD port )
201 __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
204 static __inline__ void outw( WORD value, WORD port )
206 __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
209 static __inline__ void outl( DWORD value, WORD port )
211 __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
214 #endif /* DIRECT_IO_ACCESS */
216 void IO_port_init()
218 #ifdef DIRECT_IO_ACCESS
219 char temp[1024];
221 /* Can we do that? */
222 if (!iopl(3)) {
223 iopl(0);
225 PROFILE_GetWineIniString( "ports", "read", "*",
226 temp, sizeof(temp) );
227 do_IO_port_init_read_or_write(temp, IO_READ);
228 PROFILE_GetWineIniString( "ports", "write", "*",
229 temp, sizeof(temp) );
230 do_IO_port_init_read_or_write(temp, IO_WRITE);
232 #endif /* DIRECT_IO_ACCESS */
233 IO_FixCMOSCheckSum();
237 /**********************************************************************
238 * IO_inport
240 * Note: The size argument has to be handled correctly _externally_
241 * (as we always return a DWORD)
243 DWORD IO_inport( int port, int size )
245 DWORD res = 0;
247 #ifdef DIRECT_IO_ACCESS
248 if ((do_direct_port_access)
249 /* Make sure we have access to the port */
250 && (port_permissions[port] & IO_READ))
252 iopl(3);
253 switch(size)
255 case 1: res = inb( port ); break;
256 case 2: res = inw( port ); break;
257 case 4: res = inl( port ); break;
258 default:
259 ERR(int, "invalid data size %d\n", size);
261 iopl(0);
262 return res;
264 #endif
266 TRACE(int, "%d-byte value from port 0x%02x\n", size, port );
268 switch (port)
270 case 0x40:
271 case 0x41:
272 case 0x42:
274 BYTE chan = port & 3;
275 WORD tempval = 0;
277 if (chan == 0) /* System timer counter divisor */
278 tempval = (WORD)DOSVM_GetTimer();
279 else
280 { /* FIXME: intelligent hardware timer emulation needed */
281 dummy_ctr -= 10;
282 tempval = dummy_ctr;
284 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
286 case 0x00:
287 break; /* correct ? */
288 case 0x10: /* read lo byte */
289 res = (BYTE)tempval;
290 break;
291 case 0x30: /* read lo byte, then hi byte */
292 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
293 if (tmr_8253_byte_toggle[chan] == TRUE)
295 res = (BYTE)tempval;
296 break;
298 /* else [fall through if read hi byte !] */
299 case 0x20: /* read hi byte */
300 res = (BYTE)tempval>>8;
301 break;
303 break;
305 case 0x60:
306 res = (DWORD)parport_8255[0];
307 break;
308 case 0x61:
309 res = (DWORD)parport_8255[1];
310 break;
311 case 0x62:
312 res = (DWORD)parport_8255[2];
313 break;
314 case 0x70:
315 res = (DWORD)cmosaddress;
316 break;
317 case 0x71:
318 res = (DWORD)cmosimage[cmosaddress & 0x3f];
319 break;
320 case 0x200:
321 case 0x201:
322 res = 0xffffffff; /* no joystick */
323 break;
324 case 0x3ba:
325 case 0x3da:
326 res = (DWORD)VGA_ioport_in( port );
327 break;
328 default:
329 WARN( int, "Direct I/O read attempted from port %x\n", port);
330 res = 0xffffffff;
331 break;
333 TRACE(int, " returning ( 0x%lx )\n", res );
334 return res;
338 /**********************************************************************
339 * IO_outport
341 void IO_outport( int port, int size, DWORD value )
343 TRACE(int, "IO: 0x%lx (%d-byte value) to port 0x%02x\n",
344 value, size, port );
346 #ifdef DIRECT_IO_ACCESS
347 if ((do_direct_port_access)
348 /* Make sure we have access to the port */
349 && (port_permissions[port] & IO_WRITE))
351 iopl(3);
352 switch(size)
354 case 1: outb( LOBYTE(value), port ); break;
355 case 2: outw( LOWORD(value), port ); break;
356 case 4: outl( value, port ); break;
357 default:
358 WARN(int, "Invalid data size %d\n", size);
360 iopl(0);
361 return;
363 #endif
365 switch (port)
367 case 0x40:
368 case 0x41:
369 case 0x42:
371 BYTE chan = port & 3;
372 WORD oldval = 0;
374 if ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
375 /* we need to get the oldval before any lo/hi byte change has been made */
376 (tmr_8253_byte_toggle[chan] == FALSE) )
377 oldval = tmr_8253_countmax[chan];
378 switch (tmr_8253_ctrlbyte_ch[chan] & 0x30)
380 case 0x00:
381 break; /* correct ? */
382 case 0x10: /* write lo byte */
383 tmr_8253_countmax[chan] =
384 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
385 break;
386 case 0x30: /* write lo byte, then hi byte */
387 tmr_8253_byte_toggle[chan] ^= TRUE; /* toggle */
388 if (tmr_8253_byte_toggle[chan] == TRUE)
390 tmr_8253_countmax[chan] =
391 (tmr_8253_countmax[chan] & 0xff00) | (BYTE)value;
392 break;
394 /* else [fall through if write hi byte !] */
395 case 0x20: /* write hi byte */
396 tmr_8253_countmax[chan] =
397 (tmr_8253_countmax[chan] & 0xff)|((BYTE)value << 8);
398 break;
400 if (
401 /* programming finished ? */
402 ( ((tmr_8253_ctrlbyte_ch[chan] & 0x30) != 0x30) ||
403 (tmr_8253_byte_toggle[chan] == FALSE) )
404 /* update to new value ? */
405 && (tmr_8253_countmax[chan] != oldval)
407 set_timer_maxval(chan, tmr_8253_countmax[chan]);
409 break;
410 case 0x43:
411 /* ctrl byte for specific timer channel */
412 tmr_8253_ctrlbyte_ch[((BYTE)value & 0xc0) >> 6] = (BYTE)value;
413 if (((BYTE)value&0xc0)==0xc0) {
414 FIXME(int,"8254 timer readback not implemented yet\n");
415 } else
416 if (((BYTE)value&3)==0) {
417 FIXME(int,"timer counter latch not implemented yet\n");
419 if ((value & 0x30) == 0x30) /* write lo byte, then hi byte */
420 tmr_8253_byte_toggle[((BYTE)value & 0xc0) >> 6] = FALSE; /* init */
421 break;
422 case 0x61:
423 parport_8255[1] = (BYTE)value;
424 if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253_countmax[2] != 1))
426 TRACE(int, "Beep (freq: %d) !\n", 1193180 / tmr_8253_countmax[2]);
427 Beep(1193180 / tmr_8253_countmax[2], 20);
429 break;
430 case 0x70:
431 cmosaddress = (BYTE)value & 0x7f;
432 break;
433 case 0x71:
434 cmosimage[cmosaddress & 0x3f] = (BYTE)value;
435 break;
436 case 0x3c8:
437 case 0x3c9:
438 VGA_ioport_out( port, (BYTE)value );
439 break;
440 default:
441 WARN(int, "Direct I/O write attempted to port %x\n", port );
442 break;