Moved scrollbar tracking code to scroll.c.
[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 "config.h"
15
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include "windef.h"
23 #include "vga.h"
24 #include "callback.h"
25 #include "dosexe.h"
26 #include "options.h"
27 #include "miscemu.h"
28 #include "debugtools.h"
29
30 DEFAULT_DEBUG_CHANNEL(int);
31
32 static struct {
33     WORD        countmax;
34     BOOL16      byte_toggle; /* if TRUE, then hi byte has already been written */
35     WORD        latch;
36     BOOL16      latched;
37     BYTE        ctrlbyte_ch;
38     WORD        oldval;
39 } tmr_8253[3] = {
40     {0xFFFF,    FALSE,  0,      FALSE,  0x06,   0},
41     {0x0012,    FALSE,  0,      FALSE,  0x44,   0},
42     {0x0001,    FALSE,  0,      FALSE,  0x86,   0},
43 };
44
45 static int dummy_ctr = 0;
46
47 static BYTE parport_8255[4] = {0x4f, 0x20, 0xff, 0xff};
48
49 static BYTE cmosaddress;
50
51 static BYTE cmosimage[64] =
52 {
53   0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
54   0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
55   0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
56   0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
57   0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
58   0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x19,
59   0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
60   0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
61 };
62
63 #if defined(linux) && defined(__i386__)
64 # define DIRECT_IO_ACCESS
65 #else
66 # undef DIRECT_IO_ACCESS
67 # undef PP_IO_ACCESS
68 #endif  /* linux && __i386__ */
69
70 #ifdef DIRECT_IO_ACCESS
71
72 extern int iopl(int level);
73 static char do_direct_port_access = -1;
74 #ifdef HAVE_PPDEV
75 static char do_pp_port_access = -1; /* -1: uninitialized, 1: not available
76                                        0: available);*/
77 #endif
78 static char port_permissions[0x10000];
79
80 #define IO_READ  1
81 #define IO_WRITE 2
82
83 static void IO_FixCMOSCheckSum(void)
84 {
85         WORD sum = 0;
86         int i;
87
88         for (i=0x10; i < 0x2d; i++)
89                 sum += cmosimage[i];
90         cmosimage[0x2e] = sum >> 8; /* yes, this IS hi byte !! */
91         cmosimage[0x2f] = sum & 0xff;
92         TRACE("calculated hi %02x, lo %02x\n", cmosimage[0x2e], cmosimage[0x2f]);
93 }
94
95 #endif  /* DIRECT_IO_ACCESS */
96
97 static void set_timer_maxval(unsigned timer, unsigned maxval)
98 {
99     switch (timer) {
100         case 0: /* System timer counter divisor */
101             if (Dosvm.Current())
102               Dosvm.SetTimer(maxval);
103             break;
104         case 1: /* RAM refresh */
105             FIXME("RAM refresh counter handling not implemented !\n");
106             break;
107         case 2: /* cassette & speaker */
108             /* speaker on ? */
109             if (((BYTE)parport_8255[1] & 3) == 3)
110             {
111                 TRACE("Beep (freq: %d) !\n", 1193180 / maxval );
112                 Beep(1193180 / maxval, 20);
113             }
114             break;
115     }
116 }
117
118 /**********************************************************************
119  *          IO_port_init
120  */
121
122 /* set_IO_permissions(int val1, int val)
123  * Helper function for IO_port_init
124  */
125 #ifdef DIRECT_IO_ACCESS
126 static void set_IO_permissions(int val1, int val, char rw)
127 {
128         int j;
129         if (val1 != -1) {
130                 if (val == -1) val = 0x3ff;             
131                 for (j = val1; j <= val; j++)
132                         port_permissions[j] |= rw;              
133
134                 do_direct_port_access = 1;
135
136                 val1 = -1;
137         } else if (val != -1) {         
138                 do_direct_port_access = 1;
139
140                 port_permissions[val] |= rw;
141         }
142
143 }
144
145 /* do_IO_port_init_read_or_write(char* temp, char rw)
146  * Helper function for IO_port_init
147  */
148
149 static void do_IO_port_init_read_or_write(char* temp, char rw)
150 {
151         int val, val1, i, len;
152         if (!strcasecmp(temp, "all")) {
153                 MESSAGE("Warning!!! Granting FULL IO port access to"
154                         " windoze programs!\nWarning!!! "
155                         "*** THIS IS NOT AT ALL "
156                         "RECOMMENDED!!! ***\n");
157                 for (i=0; i < sizeof(port_permissions); i++)
158                         port_permissions[i] |= rw;
159
160         } else if (!(!strcmp(temp, "*") || *temp == '\0')) {
161                 len = strlen(temp);
162                 val = -1;
163                 val1 = -1;              
164                 for (i = 0; i < len; i++) {
165                         switch (temp[i]) {
166                         case '0':
167                                 if (temp[i+1] == 'x' || temp[i+1] == 'X') {
168                                         sscanf(temp+i, "%x", &val);
169                                         i += 2;
170                                 } else {
171                                         sscanf(temp+i, "%d", &val);
172                                 }
173                                 while (isxdigit(temp[i]))
174                                         i++;
175                                 i--;
176                                 break;
177                         case ',':
178                         case ' ':
179                         case '\t':
180                                 set_IO_permissions(val1, val, rw);
181                                 val1 = -1; val = -1;
182                                 break;
183                         case '-':
184                                 val1 = val;
185                                 if (val1 == -1) val1 = 0;
186                                 break;
187                         default:
188                                 if (temp[i] >= '0' && temp[i] <= '9') {
189                                         sscanf(temp+i, "%d", &val);
190                                         while (isdigit(temp[i]))
191                                                 i++;
192                                 }
193                         }
194                 }
195                 set_IO_permissions(val1, val, rw);              
196         }
197 }
198
199 static inline BYTE inb( WORD port )
200 {
201     BYTE b;
202     __asm__ __volatile__( "inb %w1,%0" : "=a" (b) : "d" (port) );
203     return b;
204 }
205
206 static inline WORD inw( WORD port )
207 {
208     WORD w;
209     __asm__ __volatile__( "inw %w1,%0" : "=a" (w) : "d" (port) );
210     return w;
211 }
212
213 static inline DWORD inl( WORD port )
214 {
215     DWORD dw;
216     __asm__ __volatile__( "inl %w1,%0" : "=a" (dw) : "d" (port) );
217     return dw;
218 }
219
220 static inline void outb( BYTE value, WORD port )
221 {
222     __asm__ __volatile__( "outb %b0,%w1" : : "a" (value), "d" (port) );
223 }
224
225 static inline void outw( WORD value, WORD port )
226 {
227     __asm__ __volatile__( "outw %w0,%w1" : : "a" (value), "d" (port) );
228 }
229
230 static inline void outl( DWORD value, WORD port )
231 {
232     __asm__ __volatile__( "outl %0,%w1" : : "a" (value), "d" (port) );
233 }
234
235 static void IO_port_init(void)
236 {
237         char temp[1024];
238
239         do_direct_port_access = 0;
240         /* Can we do that? */
241         if (!iopl(3)) {
242                 iopl(0);
243
244                 PROFILE_GetWineIniString( "ports", "read", "*",
245                                          temp, sizeof(temp) );
246                 do_IO_port_init_read_or_write(temp, IO_READ);
247                 PROFILE_GetWineIniString( "ports", "write", "*",
248                                          temp, sizeof(temp) );
249                 do_IO_port_init_read_or_write(temp, IO_WRITE);
250         }
251     IO_FixCMOSCheckSum();
252 }
253
254 #endif  /* DIRECT_IO_ACCESS */
255
256 /**********************************************************************
257  *          IO_inport
258  *
259  * Note: The size argument has to be handled correctly _externally_ 
260  * (as we always return a DWORD)
261  */
262 DWORD IO_inport( int port, int size )
263 {
264     DWORD res = 0;
265
266     TRACE("%d-byte value from port 0x%02x\n", size, port );
267
268 #ifdef HAVE_PPDEV
269     if (do_pp_port_access == -1) 
270       do_pp_port_access =IO_pp_init();
271     if ((do_pp_port_access == 0 ) && (size == 1))
272       if (!IO_pp_inp(port,&res))
273          return res;
274 #endif
275 #ifdef DIRECT_IO_ACCESS
276     if (do_direct_port_access == -1) IO_port_init();
277     if ((do_direct_port_access)
278         /* Make sure we have access to the port */
279         && (port_permissions[port] & IO_READ))
280     {
281         iopl(3);
282         switch(size)
283         {
284         case 1: res = inb( port ); break;
285         case 2: res = inw( port ); break;
286         case 4: res = inl( port ); break;
287         default:
288             ERR("invalid data size %d\n", size);
289         }
290         iopl(0);
291         return res;
292     }
293 #endif
294
295     switch (port)
296     {
297     case 0x40:
298     case 0x41:
299     case 0x42:
300     {
301         BYTE chan = port & 3;
302         WORD tempval = 0;
303         if (tmr_8253[chan].latched)
304             tempval = tmr_8253[chan].latch;
305         else
306         {
307             dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
308             if (chan == 0) /* System timer counter divisor */
309             {
310                 /* FIXME: Dosvm.GetTimer() returns quite rigid values */
311                 if (Dosvm.Current())
312                   tempval = dummy_ctr + (WORD)Dosvm.GetTimer();
313                 else
314                   tempval = dummy_ctr;
315             }
316             else
317             {
318                 /* FIXME: intelligent hardware timer emulation needed */
319                 tempval = dummy_ctr;
320             }
321         }
322
323         switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
324         {
325         case 0:
326             res = 0; /* shouldn't happen? */
327             break;
328         case 1: /* read lo byte */
329             res = (BYTE)tempval;
330             tmr_8253[chan].latched = FALSE;
331             break;
332         case 3: /* read lo byte, then hi byte */
333             tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
334             if (tmr_8253[chan].byte_toggle)
335             {
336                 res = (BYTE)tempval;
337                 break;
338             }
339             /* else [fall through if read hi byte !] */
340         case 2: /* read hi byte */
341             res = (BYTE)(tempval >> 8);
342             tmr_8253[chan].latched = FALSE;
343             break;
344         }
345     }
346     break;
347     case 0x60:
348         res = INT_Int09ReadScan(NULL);
349 #if 0 /* what's this port got to do with parport ? */
350         res = (DWORD)parport_8255[0];
351 #endif
352         break;
353     case 0x61:
354         res = (DWORD)parport_8255[1];
355         break;
356     case 0x62:
357         res = (DWORD)parport_8255[2];
358         break;
359     case 0x70:
360         res = (DWORD)cmosaddress;
361         break;
362     case 0x71:
363         res = (DWORD)cmosimage[cmosaddress & 0x3f];
364         break;
365     case 0x200:
366     case 0x201:
367         res = 0xffffffff; /* no joystick */
368         break;
369     case 0x3ba:
370     case 0x3da:
371         res = (DWORD)VGA_ioport_in( port );
372         break;
373     default:
374         WARN("Direct I/O read attempted from port %x\n", port);
375         res = 0xffffffff;
376         break;
377     }
378     TRACE("  returning ( 0x%lx )\n", res );
379     return res;
380 }
381
382
383 /**********************************************************************
384  *          IO_outport
385  */
386 void IO_outport( int port, int size, DWORD value )
387 {
388     TRACE("IO: 0x%lx (%d-byte value) to port 0x%02x\n",
389                  value, size, port );
390
391 #ifdef HAVE_PPDEV
392     if (do_pp_port_access == -1) 
393       do_pp_port_access = IO_pp_init();
394     if ((do_pp_port_access == 0) && (size == 1))
395       if (!IO_pp_outp(port,&value))
396          return;
397 #endif
398 #ifdef DIRECT_IO_ACCESS
399
400     if (do_direct_port_access == -1) IO_port_init();
401     if ((do_direct_port_access)
402         /* Make sure we have access to the port */
403         && (port_permissions[port] & IO_WRITE))
404     {
405         iopl(3);
406         switch(size)
407         {
408         case 1: outb( LOBYTE(value), port ); break;
409         case 2: outw( LOWORD(value), port ); break;
410         case 4: outl( value, port ); break;
411         default:
412             WARN("Invalid data size %d\n", size);
413         }
414         iopl(0);
415         return;
416     }
417 #endif
418
419     switch (port)
420     {
421     case 0x20:
422         if (Dosvm.Current())
423           Dosvm.OutPIC( port, (BYTE)value );
424         break;
425     case 0x40:
426     case 0x41:
427     case 0x42:
428     {
429         BYTE chan = port & 3;
430
431         /* we need to get the oldval before any lo/hi byte change has been made */
432         if (((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
433             !tmr_8253[chan].byte_toggle)
434             tmr_8253[chan].oldval = tmr_8253[chan].countmax;
435         switch ((tmr_8253[chan].ctrlbyte_ch & 0x30) >> 4)
436         {
437         case 0:
438             break; /* shouldn't happen? */
439         case 1: /* write lo byte */
440             tmr_8253[chan].countmax =
441                 (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
442             break;
443         case 3: /* write lo byte, then hi byte */
444             tmr_8253[chan].byte_toggle ^= TRUE; /* toggle */
445             if (tmr_8253[chan].byte_toggle)
446             {
447                 tmr_8253[chan].countmax =
448                     (tmr_8253[chan].countmax & 0xff00) | (BYTE)value;
449                 break;
450             }
451             /* else [fall through if write hi byte !] */
452         case 2: /* write hi byte */
453             tmr_8253[chan].countmax =
454                 (tmr_8253[chan].countmax & 0x00ff) | ((BYTE)value << 8);
455             break;
456         }
457         /* if programming is finished and value has changed
458            then update to new value */
459         if ((((tmr_8253[chan].ctrlbyte_ch & 0x30) != 0x30) ||
460              !tmr_8253[chan].byte_toggle) &&
461             (tmr_8253[chan].countmax != tmr_8253[chan].oldval))
462             set_timer_maxval(chan, tmr_8253[chan].countmax);
463     }
464     break;          
465     case 0x43:
466     {
467         BYTE chan = ((BYTE)value & 0xc0) >> 6;
468         /* ctrl byte for specific timer channel */
469         if (chan == 3)
470         {
471             FIXME("8254 timer readback not implemented yet\n");
472             break;
473         }
474         switch (((BYTE)value & 0x30) >> 4)
475         {
476         case 0: /* latch timer */
477             tmr_8253[chan].latched = TRUE;
478             dummy_ctr -= 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
479             if (chan == 0) /* System timer divisor */
480                 if (Dosvm.Current())
481                   tmr_8253[chan].latch = dummy_ctr + (WORD)Dosvm.GetTimer();
482                 else
483                   tmr_8253[chan].latch = dummy_ctr;
484             else
485             {
486                 /* FIXME: intelligent hardware timer emulation needed */
487                 tmr_8253[chan].latch = dummy_ctr;
488             }
489             break;
490         case 3: /* write lo byte, then hi byte */
491             tmr_8253[chan].byte_toggle = FALSE; /* init */
492             /* fall through */
493         case 1: /* write lo byte only */
494         case 2: /* write hi byte only */
495             tmr_8253[chan].ctrlbyte_ch = (BYTE)value;
496             break;
497         }
498     }
499     break;
500     case 0x61:
501         parport_8255[1] = (BYTE)value;
502         if ((((BYTE)parport_8255[1] & 3) == 3) && (tmr_8253[2].countmax != 1))
503         {
504             TRACE("Beep (freq: %d) !\n", 1193180 / tmr_8253[2].countmax);
505             Beep(1193180 / tmr_8253[2].countmax, 20);
506         }
507         break;
508     case 0x70:
509         cmosaddress = (BYTE)value & 0x7f;
510         break;
511     case 0x71:
512         cmosimage[cmosaddress & 0x3f] = (BYTE)value;
513         break;
514     case 0x3c8:
515     case 0x3c9:
516         VGA_ioport_out( port, (BYTE)value );
517         break;
518     default:
519         WARN("Direct I/O write attempted to port %x\n", port );
520         break;
521     }
522 }