Work around problems on Solaris if config.h is not included.
[wine] / msdos / ioports.c
1 /*
2  * Emulation of processor ioports.
3  *
4  * Copyright 1995 Morten Welinder
5  * Copyright 1998 Andreas Mohr, Ove Kaaven
6  */
7
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.
12 */
13
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "windef.h"
21 #include "vga.h"
22 #include "callback.h"
23 #include "dosexe.h"
24 #include "options.h"
25 #include "miscemu.h"
26 #include "debugtools.h"
27
28 DEFAULT_DEBUG_CHANNEL(int);
29
30 static struct {
31     WORD        countmax;
32     BOOL16      byte_toggle; /* if TRUE, then hi byte has already been written */
33     WORD        latch;
34     BOOL16      latched;
35     BYTE        ctrlbyte_ch;
36     WORD        oldval;
37 } tmr_8253[3] = {
38     {0xFFFF,    FALSE,  0,      FALSE,  0x06,   0},
39     {0x0012,    FALSE,  0,      FALSE,  0x44,   0},
40     {0x0001,    FALSE,  0,      FALSE,  0x86,   0},
41 };
42
43 static int dummy_ctr = 0;
44
45 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
46
47 static BYTE cmosaddress;
48
49 static BYTE cmosimage[64] =
50 {
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
59 };
60
61 #if defined(linux) && defined(__i386__)
62 # define DIRECT_IO_ACCESS
63 #else
64 # undef DIRECT_IO_ACCESS
65 #endif  /* linux && __i386__ */
66
67 #ifdef DIRECT_IO_ACCESS
68
69 extern int iopl(int level);
70
71 static char do_direct_port_access = -1;
72 static char port_permissions[0x10000];
73
74 #define IO_READ  1
75 #define IO_WRITE 2
76
77 static void IO_FixCMOSCheckSum(void)
78 {
79         WORD sum = 0;
80         int i;
81
82         for (i=0x10; i < 0x2d; i++)
83                 sum += cmosimage[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]);
87 }
88
89 #endif  /* DIRECT_IO_ACCESS */
90
91 static void set_timer_maxval(unsigned timer, unsigned maxval)
92 {
93     switch (timer) {
94         case 0: /* System timer counter divisor */
95             Dosvm.SetTimer(maxval);
96             break;
97         case 1: /* RAM refresh */
98             FIXME("RAM refresh counter handling not implemented !");
99             break;
100         case 2: /* cassette & speaker */
101             /* speaker on ? */
102             if (((BYTE)parport_8255[1] & 3) == 3)
103             {
104                 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
105                 Beep(1193180 / maxval, 20);
106             }
107             break;
108     }
109 }
110
111 /**********************************************************************
112  *          IO_port_init
113  */
114
115 /* set_IO_permissions(int val1, int val)
116  * Helper function for IO_port_init
117  */
118 #ifdef DIRECT_IO_ACCESS
119 static void set_IO_permissions(int val1, int val, char rw)
120 {
121         int j;
122         if (val1 != -1) {
123                 if (val == -1) val = 0x3ff;             
124                 for (j = val1; j <= val; j++)
125                         port_permissions[j] |= rw;              
126
127                 do_direct_port_access = 1;
128
129                 val1 = -1;
130         } else if (val != -1) {         
131                 do_direct_port_access = 1;
132
133                 port_permissions[val] |= rw;
134         }
135
136 }
137
138 /* do_IO_port_init_read_or_write(char* temp, char rw)
139  * Helper function for IO_port_init
140  */
141
142 static void do_IO_port_init_read_or_write(char* temp, char rw)
143 {
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;
152
153         } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
154                 len = strlen(temp);
155                 val = -1;
156                 val1 = -1;              
157                 for (i = 0; i < len; i++) {
158                         switch (temp[i]) {
159                         case '0':
160                                 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
161                                         sscanf(temp+i, "%x", &val);
162                                         i += 2;
163                                 } else {
164                                         sscanf(temp+i, "%d", &val);
165                                 }
166                                 while (isxdigit(temp[i]))
167                                         i++;
168                                 i--;
169                                 break;
170                         case ',':
171                         case ' ':
172                         case '\t':
173                                 set_IO_permissions(val1, val, rw);
174                                 val1 = -1; val = -1;
175                                 break;
176                         case '-':
177                                 val1 = val;
178                                 if (val1 == -1) val1 = 0;
179                                 break;
180                         default:
181                                 if (temp[i] >= '0' && temp[i] <= '9') {
182                                         sscanf(temp+i, "%d", &val);
183                                         while (isdigit(temp[i]))
184                                                 i++;
185                                 }
186                         }
187                 }
188                 set_IO_permissions(val1, val, rw);              
189         }
190 }
191
192 static inline BYTE inb( WORD port )
193 {
194     BYTE b;
195     __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
196     return b;
197 }
198
199 static inline WORD inw( WORD port )
200 {
201     WORD w;
202     __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
203     return w;
204 }
205
206 static inline DWORD inl( WORD port )
207 {
208     DWORD dw;
209     __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
210     return dw;
211 }
212
213 static inline void outb( BYTE value, WORD port )
214 {
215     __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
216 }
217
218 static inline void outw( WORD value, WORD port )
219 {
220     __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
221 }
222
223 static inline void outl( DWORD value, WORD port )
224 {
225     __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
226 }
227
228 static void IO_port_init(void)
229 {
230         char temp[1024];
231
232         do_direct_port_access = 0;
233         /* Can we do that? */
234         if (!iopl(3)) {
235                 iopl(0);
236
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);
243         }
244     IO_FixCMOSCheckSum();
245 }
246
247 #endif  /* DIRECT_IO_ACCESS */
248
249 /**********************************************************************
250  *          IO_inport
251  *
252  * Note: The size argument has to be handled correctly _externally_ 
253  * (as we always return a DWORD)
254  */
255 DWORD IO_inport( int port, int size )
256 {
257     DWORD res = 0;
258
259     TRACE("%d-byte value from port 0x%02x\n", size, port );
260
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))
266     {
267         iopl(3);
268         switch(size)
269         {
270         case 1: res = inb( port ); break;
271         case 2: res = inw( port ); break;
272         case 4: res = inl( port ); break;
273         default:
274             ERR("invalid data size %d\n", size);
275         }
276         iopl(0);
277         return res;
278     }
279 #endif
280
281     switch (port)
282     {
283     case 0x40:
284     case 0x41:
285     case 0x42:
286     {
287         BYTE chan = port & 3;
288         WORD tempval = 0;
289         if (tmr_8253[chan].latched)
290             tempval = tmr_8253[chan].latch;
291         else
292         {
293             dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
294             if (chan == 0) /* System timer counter divisor */
295             {
296                 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
297                 tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
298             }
299             else
300             {
301                 /* FIXME: intelligent hardware timer emulation needed */
302                 tempval = dummy_ctr;
303             }
304         }
305
306         switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
307         {
308         case 0:
309             res = 0; /* shouldn't happen? */
310             break;
311         case 1: /* read lo byte */
312             res = (BYTE)tempval;
313             tmr_8253[chan].latched = FALSE;
314             break;
315         case 3: /* read lo byte, then hi byte */
316             tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
317             if (tmr_8253[chan].byte_toggle)
318             {
319                 res = (BYTE)tempval;
320                 break;
321             }
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;
326             break;
327         }
328     }
329     break;
330     case 0x60:
331         res = INT_Int09ReadScan(NULL);
332 #if 0 /* what's this port got to do with parport ? */
333         res = (DWORD)parport_8255[0];
334 #endif
335         break;
336     case 0x61:
337         res = (DWORD)parport_8255[1];
338         break;
339     case 0x62:
340         res = (DWORD)parport_8255[2];
341         break;
342     case 0x70:
343         res = (DWORD)cmosaddress;
344         break;
345     case 0x71:
346         res = (DWORD)cmosimage[cmosaddress & 0x3f];
347         break;
348     case 0x200:
349     case 0x201:
350         res = 0xffffffff; /* no joystick */
351         break;
352     case 0x3ba:
353     case 0x3da:
354         res = (DWORD)VGA_ioport_in( port );
355         break;
356     default:
357         WARN("Direct I/O read attempted from port %x\n", port);
358         res = 0xffffffff;
359         break;
360     }
361     TRACE("  returning ( 0x%lx )\n", res );
362     return res;
363 }
364
365
366 /**********************************************************************
367  *          IO_outport
368  */
369 void IO_outport( int port, int size, DWORD value )
370 {
371     TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
372                  value, size, port );
373
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))
379     {
380         iopl(3);
381         switch(size)
382         {
383         case 1: outb( LOBYTE(value), port ); break;
384         case 2: outw( LOWORD(value), port ); break;
385         case 4: outl( value, port ); break;
386         default:
387             WARN("Invalid data size %d\n", size);
388         }
389         iopl(0);
390         return;
391     }
392 #endif
393
394     switch (port)
395     {
396     case 0x20:
397         Dosvm.OutPIC( port, (BYTE)value );
398         break;
399     case 0x40:
400     case 0x41:
401     case 0x42:
402     {
403         BYTE chan = port & 3;
404
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)
410         {
411         case 0:
412             break; /* shouldn't happen? */
413         case 1: /* write lo byte */
414             tmr_8253[chan].countmax =
415                 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
416             break;
417         case 3: /* write lo byte, then hi byte */
418             tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
419             if (tmr_8253[chan].byte_toggle)
420             {
421                 tmr_8253[chan].countmax =
422                     (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
423                 break;
424             }
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);
429             break;
430         }
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);
437     }
438     break;          
439     case 0x43:
440     {
441         BYTE chan = ((BYTE)value & 0xc0) >> 6;
442         /* ctrl byte for specific timer channel */
443         if (chan == 3)
444         {
445             FIXME("8254 timer readback not implemented yet\n");
446             break;
447         }
448         switch (((BYTE)value & 0x30) >> 4)
449         {
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();
455             else
456             {
457                 /* FIXME: intelligent hardware timer emulation needed */
458                 tmr_8253[chan].latch = dummy_ctr;
459             }
460             break;
461         case 3: /* write lo byte, then hi byte */
462             tmr_8253[chan].byte_toggle = FALSE; /* init */
463             /* fall through */
464         case 1: /* write lo byte only */
465         case 2: /* write hi byte only */
466             tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
467             break;
468         }
469     }
470     break;
471     case 0x61:
472         parport_8255[1] = (BYTE)value;
473         if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
474         {
475             TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
476             Beep(1193180 / tmr_8253[2].countmax, 20);
477         }
478         break;
479     case 0x70:
480         cmosaddress = (BYTE)value & 0x7f;
481         break;
482     case 0x71:
483         cmosimage[cmosaddress & 0x3f] = (BYTE)value;
484         break;
485     case 0x3c8:
486     case 0x3c9:
487         VGA_ioport_out( port, (BYTE)value );
488         break;
489     default:
490         WARN("Direct I/O write attempted to port %x\n", port );
491         break;
492     }
493 }