Release 20000326.
[wine/gsoc-2012-control.git] / dlls / win87em / emulate.c
blob2ceba0916fed8883471332eec386c099945f773e
1 #include <stdlib.h>
2 #include "miscemu.h"
3 #include "debugtools.h"
5 DEFAULT_DEBUG_CHANNEL(int)
7 struct Win87EmInfoStruct
9 unsigned short Version;
10 unsigned short SizeSaveArea;
11 unsigned short WinDataSeg;
12 unsigned short WinCodeSeg;
13 unsigned short Have80x87;
14 unsigned short Unused;
17 /* Implementing this is easy cause Linux and *BSD* ALWAYS have a numerical
18 * coprocessor. (either real or emulated on kernellevel)
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)
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
28 /* FIXME: Still rather skeletal implementation only */
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;
42 void WIN87_ClearCtrlWord( CONTEXT86 *context )
44 AX_reg(context) = 0;
45 if (Installed)
46 #ifdef __i386__
47 __asm__("fclex");
48 #else
50 #endif
51 StatusWord_3 = StatusWord_2 = 0;
54 void WIN87_SetCtrlWord( CONTEXT86 *context )
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
64 CtrlWord_2 = AX_reg(context);
67 void WIN87_Init( CONTEXT86 *context )
69 if (Installed) {
70 #ifdef __i386__
71 __asm__("fninit");
72 __asm__("fninit");
73 #endif
75 StackBottom = StackTop;
76 AX_reg(context) = 0x1332;
77 WIN87_SetCtrlWord(context);
78 WIN87_ClearCtrlWord(context);
81 void WINAPI WIN87_fpmath( CONTEXT86 *context )
83 TRACE("(cs:eip=%x:%lx es=%x bx=%04x ax=%04x dx==%04x)\n",
84 (WORD)CS_reg(context), EIP_reg(context),
85 (WORD)ES_reg(context), BX_reg(context),
86 AX_reg(context), DX_reg(context) );
88 switch(BX_reg(context))
90 case 0: /* install (increase instanceref) emulator, install NMI vector */
91 RefCount++;
92 if (Installed)
93 /* InstallIntVecs02hAnd75h(); */ ;
94 WIN87_Init(context);
95 AX_reg(context) = 0;
96 break;
98 case 1: /* Init Emulator */
99 WIN87_Init(context);
100 break;
102 case 2: /* deinstall emulator (decrease instanceref), deinstall NMI vector
103 * if zero. Every '0' call should have a matching '2' call.
105 WIN87_Init(context);
106 if (!(--RefCount) && (Installed))
107 /* RestoreInt02h() */ ;
109 break;
111 case 3:
112 /*INT_SetHandler(0x3E,MAKELONG(AX,DX));*/
113 break;
115 case 4: /* set control word (& ~(CW_Denormal|CW_Invalid)) */
116 /* OUT: newset control word in AX */
117 WIN87_SetCtrlWord(context);
118 break;
120 case 5: /* return internal control word in AX */
121 AX_reg(context) = CtrlWord_1;
122 break;
124 case 6: /* round top of stack to integer using method AX & 0x0C00 */
125 /* returns current controlword */
127 DWORD dw=0;
128 WORD save,mask;
129 /* I don't know much about asm() programming. This could be
130 * wrong.
132 #ifdef __i386__
133 __asm__ __volatile__("fstcw %0;wait" : "=m" (save) : : "memory");
134 __asm__ __volatile__("fstcw %0;wait" : "=m" (mask) : : "memory");
135 __asm__ __volatile__("orw $0xC00,%0" : "=m" (mask) : : "memory");
136 __asm__ __volatile__("fldcw %0;wait" : : "m" (mask));
137 __asm__ __volatile__("frndint");
138 __asm__ __volatile__("fist %0;wait" : "=m" (dw) : : "memory");
139 __asm__ __volatile__("fldcw %0" : : "m" (save));
140 #endif
141 TRACE("On top of stack is %ld\n",dw);
143 break;
145 case 7: /* POP top of stack as integer into DX:AX */
146 /* IN: AX&0x0C00 rounding protocol */
147 /* OUT: DX:AX variable popped */
149 DWORD dw=0;
150 /* I don't know much about asm() programming. This could be
151 * wrong.
153 /* FIXME: could someone who really understands asm() fix this please? --AJ */
154 /* __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
155 TRACE("On top of stack was %ld\n",dw);
156 AX_reg(context) = LOWORD(dw);
157 DX_reg(context) = HIWORD(dw);
159 break;
161 case 8: /* restore internal status words from emulator status word */
162 AX_reg(context) = 0;
163 if (Installed) {
164 #ifdef __i386__
165 __asm__("fstsw %0;wait" : "=m" (StatusWord_1));
166 #endif
167 AL_reg(context) = (BYTE)StatusWord_1 & 0x3f;
169 AX_reg(context) |= StatusWord_2;
170 AX_reg(context) &= 0x1fff;
171 StatusWord_2 = AX_reg(context);
172 break;
174 case 9: /* clear emu control word and some other things */
175 WIN87_ClearCtrlWord(context);
176 break;
178 case 10: /* dunno. but looks like returning nr. of things on stack in AX */
179 AX_reg(context) = 0;
180 break;
182 case 11: /* just returns the installed flag in DX:AX */
183 DX_reg(context) = 0;
184 AX_reg(context) = Installed;
185 break;
187 case 12: /* save AX in some internal state var */
188 Inthandler02hVar = AX_reg(context);
189 break;
191 default: /* error. Say that loud and clear */
192 FIXME("unhandled switch %d\n",BX_reg(context));
193 AX_reg(context) = DX_reg(context) = 0xFFFF;
194 break;
199 void WINAPI WIN87_WinEm87Info(struct Win87EmInfoStruct *pWIS,
200 int cbWin87EmInfoStruct)
202 FIXME("(%p,%d), stub !\n",pWIS,cbWin87EmInfoStruct);
205 void WINAPI WIN87_WinEm87Restore(void *pWin87EmSaveArea,
206 int cbWin87EmSaveArea)
208 FIXME("(%p,%d), stub !\n",
209 pWin87EmSaveArea,cbWin87EmSaveArea);
212 void WINAPI WIN87_WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
214 FIXME("(%p,%d), stub !\n",
215 pWin87EmSaveArea,cbWin87EmSaveArea);