wininet/tests: Improve error reporting in a couple of tests.
[wine] / dlls / win87em.dll16 / win87em.c
1 /*
2  * Copyright 1993 Bob Amstadt
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdlib.h>
20 #include "windef.h"
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(int);
24
25 struct Win87EmInfoStruct
26 {
27     unsigned short Version;
28     unsigned short SizeSaveArea;
29     unsigned short WinDataSeg;
30     unsigned short WinCodeSeg;
31     unsigned short Have80x87;
32     unsigned short Unused;
33 };
34
35 /* Implementing this is easy cause Linux and *BSD* ALWAYS have a numerical
36  * coprocessor. (either real or emulated on kernellevel)
37  */
38 /* win87em.dll also sets interrupt vectors: 2 (NMI), 0x34 - 0x3f (emulator
39  * calls of standard libraries, see Ralph Browns interrupt list), 0x75
40  * (int13 error reporting of coprocessor)
41  */
42
43 /* have a look at /usr/src/linux/arch/i386/math-emu/ *.[ch] for more info
44  * especially control_w.h and status_w.h
45  */
46 /* FIXME: Still rather skeletal implementation only */
47
48 static BOOL Installed = 1; /* 8087 is installed */
49 static WORD RefCount = 0;
50 static WORD CtrlWord_1 = 0;
51 static WORD CtrlWord_2 = 0;
52 static WORD CtrlWord_Internal = 0;
53 static WORD StatusWord_1 = 0x000b;
54 static WORD StatusWord_2 = 0;
55 static WORD StatusWord_3 = 0;
56 static WORD StackTop = 175;
57 static WORD StackBottom = 0;
58 static WORD Inthandler02hVar = 1;
59
60 static void WIN87_ClearCtrlWord( CONTEXT *context )
61 {
62     context->Eax &= ~0xffff;  /* set AX to 0 */
63     if (Installed)
64 #ifdef __i386__
65         __asm__("fclex");
66 #else
67         ;
68 #endif
69     StatusWord_3 = StatusWord_2 = 0;
70 }
71
72 static void WIN87_SetCtrlWord( CONTEXT *context )
73 {
74     CtrlWord_1 = LOWORD(context->Eax);
75     context->Eax &= ~0x00c3;
76     if (Installed) {
77         CtrlWord_Internal = LOWORD(context->Eax);
78 #ifdef __i386__
79         __asm__("wait;fldcw %0" : : "m" (CtrlWord_Internal));
80 #endif
81     }
82     CtrlWord_2 = LOWORD(context->Eax);
83 }
84
85 static void WIN87_Init( CONTEXT *context )
86 {
87     if (Installed) {
88 #ifdef __i386__
89         __asm__("fninit");
90         __asm__("fninit");
91 #endif
92     }
93     StackBottom = StackTop;
94     context->Eax = (context->Eax & ~0xffff) | 0x1332;
95     WIN87_SetCtrlWord(context);
96     WIN87_ClearCtrlWord(context);
97 }
98
99 /***********************************************************************
100  *              _fpMath (WIN87EM.1)
101  */
102 void WINAPI _fpMath( CONTEXT *context )
103 {
104     TRACE("(cs:eip=%04x:%04x es=%04x bx=%04x ax=%04x dx=%04x)\n",
105           context->SegCs, context->Eip, context->SegEs, context->Ebx,
106           context->Eax, context->Edx );
107
108     switch(LOWORD(context->Ebx))
109     {
110     case 0: /* install (increase instanceref) emulator, install NMI vector */
111         RefCount++;
112 #if 0
113         if (Installed)
114             InstallIntVecs02hAnd75h();
115 #endif
116         WIN87_Init(context);
117         context->Eax &= ~0xffff;  /* set AX to 0 */
118         break;
119
120     case 1: /* Init Emulator */
121         WIN87_Init(context);
122         break;
123
124     case 2: /* deinstall emulator (decrease instanceref), deinstall NMI vector
125              * if zero. Every '0' call should have a matching '2' call.
126              */
127         WIN87_Init(context);
128         RefCount--;
129 #if 0
130         if (!RefCount && Installed)
131             RestoreInt02h();
132 #endif
133
134         break;
135
136     case 3:
137         /*INT_SetHandler(0x3E,MAKELONG(AX,DX));*/
138         break;
139
140     case 4: /* set control word (& ~(CW_Denormal|CW_Invalid)) */
141         /* OUT: newset control word in AX */
142         WIN87_SetCtrlWord(context);
143         break;
144
145     case 5: /* return internal control word in AX */
146         context->Eax = (context->Eax & ~0xffff) | CtrlWord_1;
147         break;
148
149     case 6: /* round top of stack to integer using method AX & 0x0C00 */
150         /* returns current controlword */
151         {
152             WORD save,mask;
153             /* I don't know much about asm() programming. This could be
154              * wrong.
155              */
156 #ifdef __i386__
157            __asm__ __volatile__("fstcw %0;wait" : "=m" (save) : : "memory");
158            __asm__ __volatile__("fstcw %0;wait" : "=m" (mask) : : "memory");
159            __asm__ __volatile__("orw $0xC00,%0" : "=m" (mask) : : "memory");
160            __asm__ __volatile__("fldcw %0;wait" : : "m" (mask));
161            __asm__ __volatile__("frndint");
162            __asm__ __volatile__("fldcw %0" : : "m" (save));
163 #endif
164         }
165         break;
166
167     case 7: /* POP top of stack as integer into DX:AX */
168         /* IN: AX&0x0C00 rounding protocol */
169         /* OUT: DX:AX variable popped */
170         {
171             DWORD dw=0;
172             /* I don't know much about asm() programming. This could be
173              * wrong.
174              */
175 /* FIXME: could someone who really understands asm() fix this please? --AJ */
176 /*            __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
177             TRACE("On top of stack was %d\n",dw);
178             context->Eax = (context->Eax & ~0xffff) | LOWORD(dw);
179             context->Edx = (context->Edx & ~0xffff) | HIWORD(dw);
180         }
181         break;
182
183     case 8: /* restore internal status words from emulator status word */
184         context->Eax &= ~0xffff;  /* set AX to 0 */
185         if (Installed) {
186 #ifdef __i386__
187             __asm__("fstsw %0;wait" : "=m" (StatusWord_1));
188 #endif
189             context->Eax |= StatusWord_1 & 0x3f;
190         }
191         context->Eax = (context->Eax | StatusWord_2) & ~0xe000;
192         StatusWord_2 = LOWORD(context->Eax);
193         break;
194
195     case 9: /* clear emu control word and some other things */
196         WIN87_ClearCtrlWord(context);
197         break;
198
199     case 10: /* dunno. but looks like returning nr. of things on stack in AX */
200         context->Eax &= ~0xffff;  /* set AX to 0 */
201         break;
202
203     case 11: /* just returns the installed flag in DX:AX */
204         context->Edx &= ~0xffff;  /* set DX to 0 */
205         context->Eax = (context->Eax & ~0xffff) | Installed;
206         break;
207
208     case 12: /* save AX in some internal state var */
209         Inthandler02hVar = LOWORD(context->Eax);
210         break;
211
212     default: /* error. Say that loud and clear */
213         FIXME("unhandled switch %d\n",LOWORD(context->Ebx));
214         context->Eax |= 0xffff;
215         context->Edx |= 0xffff;
216         break;
217     }
218 }
219
220 /***********************************************************************
221  *              __WinEm87Info (WIN87EM.3)
222  */
223 void WINAPI __WinEm87Info(struct Win87EmInfoStruct *pWIS, int cbWin87EmInfoStruct)
224 {
225   FIXME("(%p,%d), stub !\n",pWIS,cbWin87EmInfoStruct);
226 }
227
228 /***********************************************************************
229  *              __WinEm87Restore (WIN87EM.4)
230  */
231 void WINAPI __WinEm87Restore(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
232 {
233   FIXME("(%p,%d), stub !\n",
234         pWin87EmSaveArea,cbWin87EmSaveArea);
235 }
236
237 /***********************************************************************
238  *              __WinEm87Save (WIN87EM.5)
239  */
240 void WINAPI __WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
241 {
242   FIXME("(%p,%d), stub !\n",
243         pWin87EmSaveArea,cbWin87EmSaveArea);
244 }