win87em.c 7.56 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 48 49 50 51 52 53 54 55 56 57 58 59
/* FIXME: Still rather skeletal implementation only */

static BOOL Installed = 0;
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( CONTEXT86 *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( CONTEXT86 *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( CONTEXT86 *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 WIN87_fpmath( CONTEXT86 *context )
Alexandre Julliard's avatar
Alexandre Julliard committed
103
{
104
    TRACE("(cs:eip=%x:%x es=%x bx=%04x ax=%04x dx=%04x)\n",
105
                 (WORD)context->SegCs, context->Eip,
106 107
                 (WORD)context->SegEs, (WORD)context->Ebx,
                 (WORD)context->Eax, (WORD)context->Edx );
Alexandre Julliard's avatar
Alexandre Julliard committed
108

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

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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
135 136 137 138 139 140 141 142
        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
143
        WIN87_SetCtrlWord(context);
Alexandre Julliard's avatar
Alexandre Julliard committed
144 145 146
        break;

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

    case 6: /* round top of stack to integer using method AX & 0x0C00 */
        /* returns current controlword */
152 153
        {
            DWORD dw=0;
154
            WORD save,mask;
155 156 157
            /* I don't know much about asm() programming. This could be
             * wrong.
             */
158
#ifdef __i386__
159 160 161 162
           __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));
163 164
           __asm__ __volatile__("frndint");
           __asm__ __volatile__("fist %0;wait" : "=m" (dw) : : "memory");
165
           __asm__ __volatile__("fldcw %0" : : "m" (save));
166
#endif
167
            TRACE("On top of stack is %d\n",dw);
168
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
169 170 171 172 173 174
        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
175
            DWORD dw=0;
176 177
            /* I don't know much about asm() programming. This could be
             * wrong.
Alexandre Julliard's avatar
Alexandre Julliard committed
178
             */
Alexandre Julliard's avatar
Alexandre Julliard committed
179 180
/* FIXME: could someone who really understands asm() fix this please? --AJ */
/*            __asm__("fistp %0;wait" : "=m" (dw) : : "memory"); */
181
            TRACE("On top of stack was %d\n",dw);
182 183
            context->Eax = (context->Eax & ~0xffff) | LOWORD(dw);
            context->Edx = (context->Edx & ~0xffff) | HIWORD(dw);
Alexandre Julliard's avatar
Alexandre Julliard committed
184 185 186
        }
        break;

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

    case 9: /* clear emu control word and some other things */
Andreas Mohr's avatar
Andreas Mohr committed
200
        WIN87_ClearCtrlWord(context);
Alexandre Julliard's avatar
Alexandre Julliard committed
201 202 203
        break;

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

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

    case 12: /* save AX in some internal state var */
213
        Inthandler02hVar = LOWORD(context->Eax);
Alexandre Julliard's avatar
Alexandre Julliard committed
214 215 216
        break;

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

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

233
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
234
 *		__WinEm87Restore (WIN87EM.4)
235
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
236 237
void WINAPI WIN87_WinEm87Restore(void *pWin87EmSaveArea,
                                 int cbWin87EmSaveArea)
Alexandre Julliard's avatar
Alexandre Julliard committed
238
{
Andreas Mohr's avatar
Andreas Mohr committed
239
  FIXME("(%p,%d), stub !\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
240
	pWin87EmSaveArea,cbWin87EmSaveArea);
Alexandre Julliard's avatar
Alexandre Julliard committed
241 242
}

243
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
244
 *		__WinEm87Save (WIN87EM.5)
245
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
246
void WINAPI WIN87_WinEm87Save(void *pWin87EmSaveArea, int cbWin87EmSaveArea)
Alexandre Julliard's avatar
Alexandre Julliard committed
247
{
Andreas Mohr's avatar
Andreas Mohr committed
248
  FIXME("(%p,%d), stub !\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
249
	pWin87EmSaveArea,cbWin87EmSaveArea);
Alexandre Julliard's avatar
Alexandre Julliard committed
250
}