win87em.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 1993 Bob Amstadt
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
19
#include <stdlib.h>
20
#include "windef.h"
21
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
22

23
WINE_DEFAULT_DEBUG_CHANNEL(int);
24

Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27 28 29 30 31 32
struct Win87EmInfoStruct
{
    unsigned short Version;
    unsigned short SizeSaveArea;
    unsigned short WinDataSeg;
    unsigned short WinCodeSeg;
    unsigned short Have80x87;
    unsigned short Unused;
Alexandre Julliard's avatar
Alexandre Julliard committed
33 34
};

Alexandre Julliard's avatar
Alexandre Julliard committed
35 36 37 38 39 40 41 42
/* Implementing this is easy cause Linux and *BSD* ALWAYS have a numerical
 * coprocessor. (either real or emulated on kernellevel)
 */
/* win87em.dll also sets interrupt vectors: 2 (NMI), 0x34 - 0x3f (emulator
 * calls of standard libraries, see Ralph Browns interrupt list), 0x75
 * (int13 error reporting of coprocessor)
 */

43
/* have a look at /usr/src/linux/arch/i386/math-emu/ *.[ch] for more info
Alexandre Julliard's avatar
Alexandre Julliard committed
44 45
 * especially control_w.h and status_w.h
 */
Andreas Mohr's avatar
Andreas Mohr committed
46 47
/* FIXME: Still rather skeletal implementation only */

48
static BOOL Installed = 1; /* 8087 is installed */
Andreas Mohr's avatar
Andreas Mohr committed
49 50 51 52 53 54 55 56 57 58 59
static WORD RefCount = 0;
static WORD CtrlWord_1 = 0;
static WORD CtrlWord_2 = 0;
static WORD CtrlWord_Internal = 0;
static WORD StatusWord_1 = 0x000b;
static WORD StatusWord_2 = 0;
static WORD StatusWord_3 = 0;
static WORD StackTop = 175;
static WORD StackBottom = 0;
static WORD Inthandler02hVar = 1;

60
static void WIN87_ClearCtrlWord( CONTEXT *context )
Andreas Mohr's avatar
Andreas Mohr committed
61
{
62
    context->Eax &= ~0xffff;  /* set AX to 0 */
Andreas Mohr's avatar
Andreas Mohr committed
63 64 65 66 67 68 69 70 71
    if (Installed)
#ifdef __i386__
        __asm__("fclex");
#else
        ;
#endif
    StatusWord_3 = StatusWord_2 = 0;
}

72
static void WIN87_SetCtrlWord( CONTEXT *context )
Andreas Mohr's avatar
Andreas Mohr committed
73
{
74
    CtrlWord_1 = LOWORD(context->Eax);
75
    context->Eax &= ~0x00c3;
Andreas Mohr's avatar
Andreas Mohr committed
76
    if (Installed) {
77
        CtrlWord_Internal = LOWORD(context->Eax);
Andreas Mohr's avatar
Andreas Mohr committed
78 79 80 81
#ifdef __i386__
        __asm__("wait;fldcw %0" : : "m" (CtrlWord_Internal));
#endif
    }
82
    CtrlWord_2 = LOWORD(context->Eax);
Andreas Mohr's avatar
Andreas Mohr committed
83 84
}

85
static void WIN87_Init( CONTEXT *context )
Andreas Mohr's avatar
Andreas Mohr committed
86 87 88 89 90 91 92 93
{
    if (Installed) {
#ifdef __i386__
        __asm__("fninit");
        __asm__("fninit");
#endif
    }
    StackBottom = StackTop;
94
    context->Eax = (context->Eax & ~0xffff) | 0x1332;
Andreas Mohr's avatar
Andreas Mohr committed
95 96 97
    WIN87_SetCtrlWord(context);
    WIN87_ClearCtrlWord(context);
}
Alexandre Julliard's avatar
Alexandre Julliard committed
98

99
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
100
 *		_fpMath (WIN87EM.1)
101
 */
102
void WINAPI _fpMath( CONTEXT *context )
Alexandre Julliard's avatar
Alexandre Julliard committed
103
{
104 105 106
    TRACE("(cs:eip=%04x:%04x es=%04x bx=%04x ax=%04x dx=%04x)\n",
          context->SegCs, context->Eip, context->SegEs, context->Ebx,
          context->Eax, context->Edx );
Alexandre Julliard's avatar
Alexandre Julliard committed
107

108
    switch(LOWORD(context->Ebx))
Alexandre Julliard's avatar
Alexandre Julliard committed
109
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
110
    case 0: /* install (increase instanceref) emulator, install NMI vector */
Andreas Mohr's avatar
Andreas Mohr committed
111
        RefCount++;
112
#if 0
Andreas Mohr's avatar
Andreas Mohr committed
113
        if (Installed)
114 115
            InstallIntVecs02hAnd75h();
#endif
Andreas Mohr's avatar
Andreas Mohr committed
116
        WIN87_Init(context);
117
        context->Eax &= ~0xffff;  /* set AX to 0 */
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119 120
        break;

    case 1: /* Init Emulator */
Andreas Mohr's avatar
Andreas Mohr committed
121
        WIN87_Init(context);
Alexandre Julliard's avatar
Alexandre Julliard committed
122 123
        break;

124
    case 2: /* deinstall emulator (decrease instanceref), deinstall NMI vector
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126
             * if zero. Every '0' call should have a matching '2' call.
             */
Andreas Mohr's avatar
Andreas Mohr committed
127
        WIN87_Init(context);
128 129 130 131 132
	RefCount--;
#if 0
        if (!RefCount && Installed)
            RestoreInt02h();
#endif
133

Alexandre Julliard's avatar
Alexandre Julliard committed
134 135 136 137 138 139 140 141
        break;

    case 3:
        /*INT_SetHandler(0x3E,MAKELONG(AX,DX));*/
        break;

    case 4: /* set control word (& ~(CW_Denormal|CW_Invalid)) */
        /* OUT: newset control word in AX */
Andreas Mohr's avatar
Andreas Mohr committed
142
        WIN87_SetCtrlWord(context);
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144 145
        break;

    case 5: /* return internal control word in AX */
146
        context->Eax = (context->Eax & ~0xffff) | CtrlWord_1;
Alexandre Julliard's avatar
Alexandre Julliard committed
147
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
148 149 150

    case 6: /* round top of stack to integer using method AX & 0x0C00 */
        /* returns current controlword */
151
        {
152
            WORD save,mask;
153 154 155
            /* I don't know much about asm() programming. This could be
             * wrong.
             */
156
#ifdef __i386__
157 158 159 160
           __asm__ __volatile__("fstcw %0;wait" : "=m" (save) : : "memory");
           __asm__ __volatile__("fstcw %0;wait" : "=m" (mask) : : "memory");
           __asm__ __volatile__("orw $0xC00,%0" : "=m" (mask) : : "memory");
           __asm__ __volatile__("fldcw %0;wait" : : "m" (mask));
161
           __asm__ __volatile__("frndint");
162
           __asm__ __volatile__("fldcw %0" : : "m" (save));
163
#endif
164
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166 167 168 169 170
        break;

    case 7: /* POP top of stack as integer into DX:AX */
        /* IN: AX&0x0C00 rounding protocol */
        /* OUT: DX:AX variable popped */
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
171
            DWORD dw=0;
172 173
            /* I don't know much about asm() programming. This could be
             * wrong.
Alexandre Julliard's avatar
Alexandre Julliard committed
174
             */
Alexandre Julliard's avatar
Alexandre Julliard committed
175 176
/* FIXME: could someone who really understands asm() fix this please? --AJ */
/*            __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
177
            TRACE("On top of stack was %d\n",dw);
178 179
            context->Eax = (context->Eax & ~0xffff) | LOWORD(dw);
            context->Edx = (context->Edx & ~0xffff) | HIWORD(dw);
Alexandre Julliard's avatar
Alexandre Julliard committed
180 181 182
        }
        break;

Andreas Mohr's avatar
Andreas Mohr committed
183
    case 8: /* restore internal status words from emulator status word */
184
        context->Eax &= ~0xffff;  /* set AX to 0 */
Andreas Mohr's avatar
Andreas Mohr committed
185 186 187 188
        if (Installed) {
#ifdef __i386__
            __asm__("fstsw %0;wait" : "=m" (StatusWord_1));
#endif
189
            context->Eax |= StatusWord_1 & 0x3f;
Andreas Mohr's avatar
Andreas Mohr committed
190
        }
191
        context->Eax = (context->Eax | StatusWord_2) & ~0xe000;
192
        StatusWord_2 = LOWORD(context->Eax);
Alexandre Julliard's avatar
Alexandre Julliard committed
193 194 195
        break;

    case 9: /* clear emu control word and some other things */
Andreas Mohr's avatar
Andreas Mohr committed
196
        WIN87_ClearCtrlWord(context);
Alexandre Julliard's avatar
Alexandre Julliard committed
197 198 199
        break;

    case 10: /* dunno. but looks like returning nr. of things on stack in AX */
200
        context->Eax &= ~0xffff;  /* set AX to 0 */
Alexandre Julliard's avatar
Alexandre Julliard committed
201 202 203
        break;

    case 11: /* just returns the installed flag in DX:AX */
204 205
        context->Edx &= ~0xffff;  /* set DX to 0 */
        context->Eax = (context->Eax & ~0xffff) | Installed;
Alexandre Julliard's avatar
Alexandre Julliard committed
206 207 208
        break;

    case 12: /* save AX in some internal state var */
209
        Inthandler02hVar = LOWORD(context->Eax);
Alexandre Julliard's avatar
Alexandre Julliard committed
210 211 212
        break;

    default: /* error. Say that loud and clear */
213 214 215
        FIXME("unhandled switch %d\n",LOWORD(context->Ebx));
        context->Eax |= 0xffff;
        context->Edx |= 0xffff;
Alexandre Julliard's avatar
Alexandre Julliard committed
216
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
217 218 219
    }
}

220
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
221
 *		__WinEm87Info (WIN87EM.3)
222
 */
223
void WINAPI __WinEm87Info(struct Win87EmInfoStruct *pWIS, int cbWin87EmInfoStruct)
Alexandre Julliard's avatar
Alexandre Julliard committed
224
{
Andreas Mohr's avatar
Andreas Mohr committed
225
  FIXME("(%p,%d), stub !\n",pWIS,cbWin87EmInfoStruct);
Alexandre Julliard's avatar
Alexandre Julliard committed
226 227
}

228
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
229
 *		__WinEm87Restore (WIN87EM.4)
230
 */
231
void WINAPI __WinEm87Restore(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
Alexandre Julliard's avatar
Alexandre Julliard committed
232
{
Andreas Mohr's avatar
Andreas Mohr committed
233
  FIXME("(%p,%d), stub !\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
234
	pWin87EmSaveArea,cbWin87EmSaveArea);
Alexandre Julliard's avatar
Alexandre Julliard committed
235 236
}

237
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
238
 *		__WinEm87Save (WIN87EM.5)
239
 */
240
void WINAPI __WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
Alexandre Julliard's avatar
Alexandre Julliard committed
241
{
Andreas Mohr's avatar
Andreas Mohr committed
242
  FIXME("(%p,%d), stub !\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
243
	pWin87EmSaveArea,cbWin87EmSaveArea);
Alexandre Julliard's avatar
Alexandre Julliard committed
244
}