Set ansi/oem/mac code pages from current locale.
[wine] / dlls / win87em / emulate.c
1 #include <stdlib.h>
2 #include "miscemu.h"
3 #include "debugtools.h"
4
5 DEFAULT_DEBUG_CHANNEL(int)
6
7 struct Win87EmInfoStruct
8 {
9     unsigned short Version;
10     unsigned short SizeSaveArea;
11     unsigned short WinDataSeg;
12     unsigned short WinCodeSeg;
13     unsigned short Have80x87;
14     unsigned short Unused;
15 };
16
17 /* Implementing this is easy cause Linux and *BSD* ALWAYS have a numerical
18  * coprocessor. (either real or emulated on kernellevel)
19  */
20 /* win87em.dll also sets interrupt vectors: 2 (NMI), 0x34 - 0x3f (emulator
21  * calls of standard libraries, see Ralph Browns interrupt list), 0x75
22  * (int13 error reporting of coprocessor)
23  */
24
25 /* have a look at /usr/src/linux/arch/i386/math-emu/ *.[ch] for more info 
26  * especially control_w.h and status_w.h
27  */
28 /* FIXME: Still rather skeletal implementation only */
29
30 static BOOL Installed = 0;
31 static WORD RefCount = 0;
32 static WORD CtrlWord_1 = 0;
33 static WORD CtrlWord_2 = 0;
34 static WORD CtrlWord_Internal = 0;
35 static WORD StatusWord_1 = 0x000b;
36 static WORD StatusWord_2 = 0;
37 static WORD StatusWord_3 = 0;
38 static WORD StackTop = 175;
39 static WORD StackBottom = 0;
40 static WORD Inthandler02hVar = 1;
41
42 static void WIN87_ClearCtrlWord( CONTEXT86 *context )
43 {
44     AX_reg(context) = 0;
45     if (Installed)
46 #ifdef __i386__
47         __asm__("fclex");
48 #else
49         ;
50 #endif
51     StatusWord_3 = StatusWord_2 = 0;
52 }
53
54 static void WIN87_SetCtrlWord( CONTEXT86 *context )
55 {
56     CtrlWord_1 = AX_reg(context);
57     AX_reg(context) &= 0xff3c;
58     if (Installed) {
59         CtrlWord_Internal = AX_reg(context);
60 #ifdef __i386__
61         __asm__("wait;fldcw %0" : : "m" (CtrlWord_Internal));
62 #endif
63     }
64     CtrlWord_2 = AX_reg(context);
65 }
66
67 void WIN87_Init( CONTEXT86 *context )
68 {
69     if (Installed) {
70 #ifdef __i386__
71         __asm__("fninit");
72         __asm__("fninit");
73 #endif
74     }
75     StackBottom = StackTop;
76     AX_reg(context) = 0x1332;
77     WIN87_SetCtrlWord(context);
78     WIN87_ClearCtrlWord(context);
79 }
80
81 /***********************************************************************
82  *              WIN87_fpmath
83  */
84 void WINAPI WIN87_fpmath( CONTEXT86 *context )
85 {
86     TRACE("(cs:eip=%x:%lx es=%x bx=%04x ax=%04x dx==%04x)\n",
87                  (WORD)CS_reg(context), EIP_reg(context),
88                  (WORD)ES_reg(context), BX_reg(context),
89                  AX_reg(context), DX_reg(context) );
90
91     switch(BX_reg(context))
92     {
93     case 0: /* install (increase instanceref) emulator, install NMI vector */
94         RefCount++;
95         if (Installed)
96             /* InstallIntVecs02hAnd75h(); */    ;
97         WIN87_Init(context);
98         AX_reg(context) = 0;
99         break;
100
101     case 1: /* Init Emulator */
102         WIN87_Init(context);
103         break;
104
105     case 2: /* deinstall emulator (decrease instanceref), deinstall NMI vector  
106              * if zero. Every '0' call should have a matching '2' call.
107              */
108         WIN87_Init(context);
109         if (!(--RefCount) && (Installed))
110             /* RestoreInt02h() */   ;
111         
112         break;
113
114     case 3:
115         /*INT_SetHandler(0x3E,MAKELONG(AX,DX));*/
116         break;
117
118     case 4: /* set control word (& ~(CW_Denormal|CW_Invalid)) */
119         /* OUT: newset control word in AX */
120         WIN87_SetCtrlWord(context);
121         break;
122
123     case 5: /* return internal control word in AX */
124         AX_reg(context) = CtrlWord_1;
125         break;
126
127     case 6: /* round top of stack to integer using method AX & 0x0C00 */
128         /* returns current controlword */
129         {
130             DWORD dw=0;
131             WORD save,mask;
132             /* I don't know much about asm() programming. This could be
133              * wrong.
134              */
135 #ifdef __i386__
136            __asm__ __volatile__("fstcw %0;wait" : "=m" (save) : : "memory");
137            __asm__ __volatile__("fstcw %0;wait" : "=m" (mask) : : "memory");
138            __asm__ __volatile__("orw $0xC00,%0" : "=m" (mask) : : "memory");
139            __asm__ __volatile__("fldcw %0;wait" : : "m" (mask));
140            __asm__ __volatile__("frndint");
141            __asm__ __volatile__("fist %0;wait" : "=m" (dw) : : "memory");
142            __asm__ __volatile__("fldcw %0" : : "m" (save));
143 #endif
144             TRACE("On top of stack is %ld\n",dw);
145         }
146         break;
147
148     case 7: /* POP top of stack as integer into DX:AX */
149         /* IN: AX&0x0C00 rounding protocol */
150         /* OUT: DX:AX variable popped */
151         {
152             DWORD dw=0;
153             /* I don't know much about asm() programming. This could be 
154              * wrong. 
155              */
156 /* FIXME: could someone who really understands asm() fix this please? --AJ */
157 /*            __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
158             TRACE("On top of stack was %ld\n",dw);
159             AX_reg(context) = LOWORD(dw);
160             DX_reg(context) = HIWORD(dw);
161         }
162         break;
163
164     case 8: /* restore internal status words from emulator status word */
165         AX_reg(context) = 0;
166         if (Installed) {
167 #ifdef __i386__
168             __asm__("fstsw %0;wait" : "=m" (StatusWord_1));
169 #endif
170             AL_reg(context) = (BYTE)StatusWord_1 & 0x3f;
171         }
172         AX_reg(context) |= StatusWord_2;
173         AX_reg(context) &= 0x1fff;
174         StatusWord_2 = AX_reg(context);
175         break;
176
177     case 9: /* clear emu control word and some other things */
178         WIN87_ClearCtrlWord(context);
179         break;
180
181     case 10: /* dunno. but looks like returning nr. of things on stack in AX */
182         AX_reg(context) = 0;
183         break;
184
185     case 11: /* just returns the installed flag in DX:AX */
186         DX_reg(context) = 0;
187         AX_reg(context) = Installed;
188         break;
189
190     case 12: /* save AX in some internal state var */
191         Inthandler02hVar = AX_reg(context);
192         break;
193
194     default: /* error. Say that loud and clear */
195         FIXME("unhandled switch %d\n",BX_reg(context));
196         AX_reg(context) = DX_reg(context) = 0xFFFF;
197         break;
198     }
199 }
200
201 /***********************************************************************
202  *              WIN87_WinEm87Info
203  */
204 void WINAPI WIN87_WinEm87Info(struct Win87EmInfoStruct *pWIS,
205                               int cbWin87EmInfoStruct)
206 {
207   FIXME("(%p,%d), stub !\n",pWIS,cbWin87EmInfoStruct);
208 }
209
210 /***********************************************************************
211  *              WIN87_WinEm87Restore
212  */
213 void WINAPI WIN87_WinEm87Restore(void *pWin87EmSaveArea,
214                                  int cbWin87EmSaveArea)
215 {
216   FIXME("(%p,%d), stub !\n",
217         pWin87EmSaveArea,cbWin87EmSaveArea);
218 }
219
220 /***********************************************************************
221  *              WIN87_WinEm87Save
222  */
223 void WINAPI WIN87_WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
224 {
225   FIXME("(%p,%d), stub !\n",
226         pWin87EmSaveArea,cbWin87EmSaveArea);
227 }