Authors: David Hammerton <david@transgaming.com>, Peter Hunnisett <peter@transgaming...
[wine.git] / dlls / kernel / win87em.c
blob573607abe5acafd0feb1c5131337543f8990aa32
1 /*
2 * Copyright 1993 Bob Amstadt
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdlib.h>
20 #include "miscemu.h"
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(int);
25 struct Win87EmInfoStruct
27 unsigned short Version;
28 unsigned short SizeSaveArea;
29 unsigned short WinDataSeg;
30 unsigned short WinCodeSeg;
31 unsigned short Have80x87;
32 unsigned short Unused;
35 /* Implementing this is easy cause Linux and *BSD* ALWAYS have a numerical
36 * coprocessor. (either real or emulated on kernellevel)
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)
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
46 /* FIXME: Still rather skeletal implementation only */
48 static BOOL Installed = 0;
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;
60 static void WIN87_ClearCtrlWord( CONTEXT86 *context )
62 AX_reg(context) = 0;
63 if (Installed)
64 #ifdef __i386__
65 __asm__("fclex");
66 #else
68 #endif
69 StatusWord_3 = StatusWord_2 = 0;
72 static void WIN87_SetCtrlWord( CONTEXT86 *context )
74 CtrlWord_1 = AX_reg(context);
75 AX_reg(context) &= 0xff3c;
76 if (Installed) {
77 CtrlWord_Internal = AX_reg(context);
78 #ifdef __i386__
79 __asm__("wait;fldcw %0" : : "m" (CtrlWord_Internal));
80 #endif
82 CtrlWord_2 = AX_reg(context);
85 void WIN87_Init( CONTEXT86 *context )
87 if (Installed) {
88 #ifdef __i386__
89 __asm__("fninit");
90 __asm__("fninit");
91 #endif
93 StackBottom = StackTop;
94 AX_reg(context) = 0x1332;
95 WIN87_SetCtrlWord(context);
96 WIN87_ClearCtrlWord(context);
99 /***********************************************************************
100 * _fpMath (WIN87EM.1)
102 void WINAPI WIN87_fpmath( CONTEXT86 *context )
104 TRACE("(cs:eip=%x:%lx es=%x bx=%04x ax=%04x dx==%04x)\n",
105 (WORD)context->SegCs, context->Eip,
106 (WORD)context->SegEs, BX_reg(context),
107 AX_reg(context), DX_reg(context) );
109 switch(BX_reg(context))
111 case 0: /* install (increase instanceref) emulator, install NMI vector */
112 RefCount++;
113 #if 0
114 if (Installed)
115 InstallIntVecs02hAnd75h();
116 #endif
117 WIN87_Init(context);
118 AX_reg(context) = 0;
119 break;
121 case 1: /* Init Emulator */
122 WIN87_Init(context);
123 break;
125 case 2: /* deinstall emulator (decrease instanceref), deinstall NMI vector
126 * if zero. Every '0' call should have a matching '2' call.
128 WIN87_Init(context);
129 RefCount--;
130 #if 0
131 if (!RefCount && Installed)
132 RestoreInt02h();
133 #endif
135 break;
137 case 3:
138 /*INT_SetHandler(0x3E,MAKELONG(AX,DX));*/
139 break;
141 case 4: /* set control word (& ~(CW_Denormal|CW_Invalid)) */
142 /* OUT: newset control word in AX */
143 WIN87_SetCtrlWord(context);
144 break;
146 case 5: /* return internal control word in AX */
147 AX_reg(context) = CtrlWord_1;
148 break;
150 case 6: /* round top of stack to integer using method AX & 0x0C00 */
151 /* returns current controlword */
153 DWORD dw=0;
154 WORD save,mask;
155 /* I don't know much about asm() programming. This could be
156 * wrong.
158 #ifdef __i386__
159 __asm__ __volatile__("fstcw %0;wait" : "=m" (save) : : "memory");
160 __asm__ __volatile__("fstcw %0;wait" : "=m" (mask) : : "memory");
161 __asm__ __volatile__("orw $0xC00,%0" : "=m" (mask) : : "memory");
162 __asm__ __volatile__("fldcw %0;wait" : : "m" (mask));
163 __asm__ __volatile__("frndint");
164 __asm__ __volatile__("fist %0;wait" : "=m" (dw) : : "memory");
165 __asm__ __volatile__("fldcw %0" : : "m" (save));
166 #endif
167 TRACE("On top of stack is %ld\n",dw);
169 break;
171 case 7: /* POP top of stack as integer into DX:AX */
172 /* IN: AX&0x0C00 rounding protocol */
173 /* OUT: DX:AX variable popped */
175 DWORD dw=0;
176 /* I don't know much about asm() programming. This could be
177 * wrong.
179 /* FIXME: could someone who really understands asm() fix this please? --AJ */
180 /* __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
181 TRACE("On top of stack was %ld\n",dw);
182 AX_reg(context) = LOWORD(dw);
183 DX_reg(context) = HIWORD(dw);
185 break;
187 case 8: /* restore internal status words from emulator status word */
188 AX_reg(context) = 0;
189 if (Installed) {
190 #ifdef __i386__
191 __asm__("fstsw %0;wait" : "=m" (StatusWord_1));
192 #endif
193 AL_reg(context) = (BYTE)StatusWord_1 & 0x3f;
195 AX_reg(context) |= StatusWord_2;
196 AX_reg(context) &= 0x1fff;
197 StatusWord_2 = AX_reg(context);
198 break;
200 case 9: /* clear emu control word and some other things */
201 WIN87_ClearCtrlWord(context);
202 break;
204 case 10: /* dunno. but looks like returning nr. of things on stack in AX */
205 AX_reg(context) = 0;
206 break;
208 case 11: /* just returns the installed flag in DX:AX */
209 DX_reg(context) = 0;
210 AX_reg(context) = Installed;
211 break;
213 case 12: /* save AX in some internal state var */
214 Inthandler02hVar = AX_reg(context);
215 break;
217 default: /* error. Say that loud and clear */
218 FIXME("unhandled switch %d\n",BX_reg(context));
219 AX_reg(context) = DX_reg(context) = 0xFFFF;
220 break;
224 /***********************************************************************
225 * __WinEm87Info (WIN87EM.3)
227 void WINAPI WIN87_WinEm87Info(struct Win87EmInfoStruct *pWIS,
228 int cbWin87EmInfoStruct)
230 FIXME("(%p,%d), stub !\n",pWIS,cbWin87EmInfoStruct);
233 /***********************************************************************
234 * __WinEm87Restore (WIN87EM.4)
236 void WINAPI WIN87_WinEm87Restore(void *pWin87EmSaveArea,
237 int cbWin87EmSaveArea)
239 FIXME("(%p,%d), stub !\n",
240 pWin87EmSaveArea,cbWin87EmSaveArea);
243 /***********************************************************************
244 * __WinEm87Save (WIN87EM.5)
246 void WINAPI WIN87_WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
248 FIXME("(%p,%d), stub !\n",
249 pWin87EmSaveArea,cbWin87EmSaveArea);