File: fpu.cc

package info (click to toggle)
bochs 2.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,116 kB
  • ctags: 16,927
  • sloc: cpp: 130,524; ansic: 18,822; sh: 7,922; makefile: 3,836; yacc: 1,056; asm: 463; perl: 381; lex: 280; csh: 3
file content (541 lines) | stat: -rw-r--r-- 19,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/////////////////////////////////////////////////////////////////////////
// $Id: fpu.cc,v 1.20 2006/03/06 22:03:04 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
//   Copyright (c) 2003 Stanislav Shwartsman
//          Written by Stanislav Shwartsman <stl at fidonet.org.il>
//
//  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 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
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
//
/////////////////////////////////////////////////////////////////////////


#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "cpu/cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR

#include "iodev/iodev.h"

#define UPDATE_LAST_OPCODE       1
#define CHECK_PENDING_EXCEPTIONS 1


#if BX_SUPPORT_FPU
void BX_CPU_C::prepareFPU(bxInstruction_c *i, 
	bx_bool check_pending_exceptions, bx_bool update_last_instruction)
{
  if (BX_CPU_THIS_PTR cr0.em || BX_CPU_THIS_PTR cr0.ts)
    exception(BX_NM_EXCEPTION, 0, 0);

  if (check_pending_exceptions)
    BX_CPU_THIS_PTR FPU_check_pending_exceptions();

  if (update_last_instruction)
  {
    BX_CPU_THIS_PTR the_i387.foo = ((Bit32u)(i->b1()) << 8) | (Bit32u)(i->modrm());
    BX_CPU_THIS_PTR the_i387.fcs = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
    BX_CPU_THIS_PTR the_i387.fip = BX_CPU_THIS_PTR prev_eip;

    if (! i->modC0()) {
         BX_CPU_THIS_PTR the_i387.fds = BX_CPU_THIS_PTR sregs[i->seg()].selector.value;
         BX_CPU_THIS_PTR the_i387.fdp = RMAddr(i);
    } else {
         BX_CPU_THIS_PTR the_i387.fds = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
         BX_CPU_THIS_PTR the_i387.fdp = 0;
    }
  }
}

void BX_CPU_C::FPU_check_pending_exceptions(void)
{
  if(BX_CPU_THIS_PTR the_i387.get_partial_status() & FPU_SW_Summary)
  {
    // NE=1 selects the native or internal mode, which generates #MF, 
    // which is the same as the native version of exception handling 
    // for the 80286 and 80287 and the i386 processors and i387 math 
    // coprocessor.
#if BX_CPU_LEVEL >= 4
    if (BX_CPU_THIS_PTR cr0.ne == 0)
    {
      // MSDOS compatibility external interrupt (IRQ13)
      BX_INFO (("math_abort: MSDOS compatibility FPU exception"));
      DEV_pic_raise_irq(13);
    }
    else
#endif
      exception(BX_MF_EXCEPTION, 0, 0);
  }
}

int BX_CPU_C::fpu_save_environment(bxInstruction_c *i)
{
    if (protected_mode())  /* Protected Mode */
    {
        if (i->os32L() || i->os64L())
        {
            Bit32u tmp;

            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_control_word();
            write_virtual_dword(i->seg(), RMAddr(i), &tmp);
            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_status_word();
            write_virtual_dword(i->seg(), RMAddr(i) + 0x04, &tmp);
            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_tag_word();
            write_virtual_dword(i->seg(), RMAddr(i) + 0x08, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fip) & 0xffffffff;
            write_virtual_dword(i->seg(), RMAddr(i) + 0x0c, &tmp);
            tmp  = (BX_CPU_THIS_PTR the_i387.fcs & 0xffff) |
                          ((Bit32u)(BX_CPU_THIS_PTR the_i387.foo)) << 16;
            write_virtual_dword(i->seg(), RMAddr(i) + 0x10, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fdp) & 0xffffffff;
            write_virtual_dword(i->seg(), RMAddr(i) + 0x14, &tmp);
            tmp = 0xffff0000 | (BX_CPU_THIS_PTR the_i387.fds);
            write_virtual_dword(i->seg(), RMAddr(i) + 0x18, &tmp);

            return 0x1c;
        }
        else /* Protected Mode - 16 bit */
        {
            Bit16u tmp;

            tmp = BX_CPU_THIS_PTR the_i387.get_control_word();
            write_virtual_word(i->seg(), RMAddr(i), &tmp);
            tmp = BX_CPU_THIS_PTR the_i387.get_status_word();
            write_virtual_word(i->seg(), RMAddr(i) + 0x02, &tmp);
            tmp = BX_CPU_THIS_PTR the_i387.get_tag_word();
            write_virtual_word(i->seg(), RMAddr(i) + 0x04, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fip) & 0xffff;
            write_virtual_word(i->seg(), RMAddr(i) + 0x06, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fcs);
            write_virtual_word(i->seg(), RMAddr(i) + 0x08, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fdp) & 0xffff;
            write_virtual_word(i->seg(), RMAddr(i) + 0x0a, &tmp);
            tmp = (BX_CPU_THIS_PTR the_i387.fds);
            write_virtual_word(i->seg(), RMAddr(i) + 0x0c, &tmp);

            return 0x0e;
        }
    }
    else   /* Real or V86 Mode */
    {
        Bit32u fp_ip = ((Bit32u)(BX_CPU_THIS_PTR the_i387.fcs) << 4) +
              (BX_CPU_THIS_PTR the_i387.fip);
        Bit32u fp_dp = ((Bit32u)(BX_CPU_THIS_PTR the_i387.fds) << 4) +
              (BX_CPU_THIS_PTR the_i387.fdp);

        if (i->os32L() || i->os64L())
        {
            Bit32u tmp;
        
            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_control_word();
            write_virtual_dword(i->seg(), RMAddr(i), &tmp);
            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_status_word();
            write_virtual_dword(i->seg(), RMAddr(i) + 0x04, &tmp);
            tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_tag_word();
            write_virtual_dword(i->seg(), RMAddr(i) + 0x08, &tmp);
            tmp = 0xffff0000 | (fp_ip & 0xffff);
            write_virtual_dword(i->seg(), RMAddr(i) + 0x0c, &tmp);
            tmp = ((fp_ip & 0xffff0000) >> 4) |
                          (BX_CPU_THIS_PTR the_i387.foo & 0x7ff);
            write_virtual_dword(i->seg(), RMAddr(i) + 0x10, &tmp);
            tmp = 0xffff0000 | (fp_dp & 0xffff);
            write_virtual_dword(i->seg(), RMAddr(i) + 0x14, &tmp);
            tmp = (fp_dp & 0xffff0000) >> 4;
            write_virtual_dword(i->seg(), RMAddr(i) + 0x18, &tmp);

            return 0x1c;
        }
        else  /* Real or V86 Mode - 16 bit */
        {
            Bit16u tmp;

            tmp = BX_CPU_THIS_PTR the_i387.get_control_word();
            write_virtual_word(i->seg(), RMAddr(i), &tmp);
            tmp = BX_CPU_THIS_PTR the_i387.get_status_word();
            write_virtual_word(i->seg(), RMAddr(i) + 0x02, &tmp);
            tmp = BX_CPU_THIS_PTR the_i387.get_tag_word();
            write_virtual_word(i->seg(), RMAddr(i) + 0x04, &tmp);
            tmp = fp_ip & 0xffff;
            write_virtual_word(i->seg(), RMAddr(i) + 0x06, &tmp);
            tmp = (Bit16u)(((fp_ip & 0xf0000) >> 4) |
                          (BX_CPU_THIS_PTR the_i387.foo & 0x7ff));
            write_virtual_word(i->seg(), RMAddr(i) + 0x08, &tmp);
            tmp = fp_dp & 0xffff;
            write_virtual_word(i->seg(), RMAddr(i) + 0x0a, &tmp);
            tmp = (Bit16u)((fp_dp & 0xf0000) >> 4);
            write_virtual_word(i->seg(), RMAddr(i) + 0x0c, &tmp);

            return 0x0e;
        }       
    }   
}

int BX_CPU_C::fpu_load_environment(bxInstruction_c *i)
{
    int offset;

    if (protected_mode())  /* Protected Mode */
    {
        if (i->os32L() || i->os64L())
        {
            Bit32u tmp;

            read_virtual_dword(i->seg(), RMAddr(i), &tmp);
            BX_CPU_THIS_PTR the_i387.cwd = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x04, &tmp);
            BX_CPU_THIS_PTR the_i387.swd = tmp & 0xffff;
            BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x08, &tmp);
            BX_CPU_THIS_PTR the_i387.twd = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x0c, &tmp);
            BX_CPU_THIS_PTR the_i387.fip = tmp;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x10, &tmp);
            BX_CPU_THIS_PTR the_i387.fcs = tmp & 0xffff;
            BX_CPU_THIS_PTR the_i387.foo = (tmp >> 16) & 0x07ff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x14, &tmp);
            BX_CPU_THIS_PTR the_i387.fdp = tmp;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x18, &tmp);
            BX_CPU_THIS_PTR the_i387.fds = tmp & 0xffff;
            offset = 0x1c;
        }
        else /* Protected Mode - 16 bit */
        {
            Bit16u tmp;

            read_virtual_word(i->seg(), RMAddr(i), &tmp);
            BX_CPU_THIS_PTR the_i387.cwd = tmp;
            read_virtual_word(i->seg(), RMAddr(i) + 0x2, &tmp);
            BX_CPU_THIS_PTR the_i387.swd = tmp;
            BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
            read_virtual_word(i->seg(), RMAddr(i) + 0x04, &tmp);
            BX_CPU_THIS_PTR the_i387.twd = tmp;
            read_virtual_word(i->seg(), RMAddr(i) + 0x06, &tmp);
            BX_CPU_THIS_PTR the_i387.fip = tmp & 0xffff;
            read_virtual_word(i->seg(), RMAddr(i) + 0x08, &tmp);
            BX_CPU_THIS_PTR the_i387.fcs = tmp;
            read_virtual_word(i->seg(), RMAddr(i) + 0x0a, &tmp);
            BX_CPU_THIS_PTR the_i387.fdp = tmp & 0xffff;
            read_virtual_word(i->seg(), RMAddr(i) + 0x0c, &tmp);
            BX_CPU_THIS_PTR the_i387.fds = tmp;
            /* opcode is defined to be zero */
            BX_CPU_THIS_PTR the_i387.foo = 0;
            offset = 0x0e;
        }
    }
    else   /* Real or V86 Mode */
    {
        Bit32u fp_ip = 0, fp_dp = 0;

        if (i->os32L() || i->os64L())
        {
            Bit32u tmp;

            read_virtual_dword(i->seg(), RMAddr(i), &tmp);
            BX_CPU_THIS_PTR the_i387.cwd = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x04, &tmp);
            BX_CPU_THIS_PTR the_i387.swd = tmp & 0xffff;
            BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x08, &tmp);
            BX_CPU_THIS_PTR the_i387.twd = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x0c, &tmp);
            fp_ip = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x10, &tmp);
            fp_ip = fp_ip | ((tmp & 0x0ffff000) << 4);
            BX_CPU_THIS_PTR the_i387.fip = fp_ip;
            BX_CPU_THIS_PTR the_i387.foo = tmp & 0x07ff;
            BX_CPU_THIS_PTR the_i387.fcs = 0;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x14, &tmp);
            fp_dp = tmp & 0xffff;
            read_virtual_dword(i->seg(), RMAddr(i) + 0x18, &tmp);
            fp_dp = fp_dp | ((tmp & 0x0ffff000) << 4);
            BX_CPU_THIS_PTR the_i387.fdp = fp_dp;
            BX_CPU_THIS_PTR the_i387.fds = 0;
            offset = 0x1c;
        }
        else  /* Real or V86 Mode - 16 bit */
        {
            Bit16u tmp;

            read_virtual_word(i->seg(), RMAddr(i), &tmp);
            BX_CPU_THIS_PTR the_i387.cwd = tmp;
            read_virtual_word(i->seg(), RMAddr(i) + 0x2, &tmp);
            BX_CPU_THIS_PTR the_i387.swd = tmp;
            BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
            read_virtual_word(i->seg(), RMAddr(i) + 0x04, &tmp);
            BX_CPU_THIS_PTR the_i387.twd = tmp;
            read_virtual_word(i->seg(), RMAddr(i) + 0x06, &tmp);
            fp_ip = tmp & 0xffff;
            read_virtual_word(i->seg(), RMAddr(i) + 0x08, &tmp);
            fp_ip = fp_ip | ((tmp & 0xf000) << 4);
            BX_CPU_THIS_PTR the_i387.fip = fp_ip;
            BX_CPU_THIS_PTR the_i387.foo = tmp & 0x07ff;
            BX_CPU_THIS_PTR the_i387.fcs = 0;
            read_virtual_word(i->seg(), RMAddr(i) + 0x0a, &tmp);
            fp_dp = tmp & 0xffff;
            read_virtual_word(i->seg(), RMAddr(i) + 0x0c, &tmp);
            fp_dp = fp_dp | ((tmp & 0xf000) << 4);
            BX_CPU_THIS_PTR the_i387.fdp = fp_dp;
            BX_CPU_THIS_PTR the_i387.fds = 0;
            offset = 0x0e;
        }
    }

    /* check for unmasked exceptions */
    if (FPU_PARTIAL_STATUS & ~FPU_CONTROL_WORD & FPU_CW_Exceptions_Mask)
    {
        /* set the B and ES bits in the status-word */
        FPU_PARTIAL_STATUS |= FPU_SW_Summary | FPU_SW_Backward;
    }
    else
    {
        /* clear the B and ES bits in the status-word */
        FPU_PARTIAL_STATUS &= ~(FPU_SW_Summary | FPU_SW_Backward);
    }

    return offset;
}
#endif

/* D9 /5 */
void BX_CPU_C::FLDCW(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  Bit16u cwd;
  read_virtual_word(i->seg(), RMAddr(i), &cwd);
  FPU_CONTROL_WORD = cwd;

  /* check for unmasked exceptions */
  if (FPU_PARTIAL_STATUS & ~FPU_CONTROL_WORD & FPU_CW_Exceptions_Mask)
  {
      /* set the B and ES bits in the status-word */
      FPU_PARTIAL_STATUS |= FPU_SW_Summary | FPU_SW_Backward;
  }
  else
  {
      /* clear the B and ES bits in the status-word */
      FPU_PARTIAL_STATUS &= ~(FPU_SW_Summary | FPU_SW_Backward);
  }
#else
  BX_INFO(("FLDCW: required FPU, configure --enable-fpu"));
#endif
}

/* D9 /7 */
void BX_CPU_C::FNSTCW(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  Bit16u cwd = BX_CPU_THIS_PTR the_i387.get_control_word();
  write_virtual_word(i->seg(), RMAddr(i), &cwd);
#else
  BX_INFO(("FNSTCW: required FPU, configure --enable-fpu"));
#endif
}

/* DD /7 */
void BX_CPU_C::FNSTSW(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  Bit16u swd = BX_CPU_THIS_PTR the_i387.get_status_word();
  write_virtual_word(i->seg(), RMAddr(i), &swd);
#else
  BX_INFO(("FNSTSW: required FPU, configure --enable-fpu"));
#endif
}

/* DF E0 */
void BX_CPU_C::FNSTSW_AX(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  AX = BX_CPU_THIS_PTR the_i387.get_status_word();
#else
  BX_INFO(("FNSTSW_AX: required FPU, configure --enable-fpu"));
#endif
}

/* DD /4 */
void BX_CPU_C::FRSTOR(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  int offset = fpu_load_environment(i);

  /* read all registers in stack order. */
  for(int n=0;n<8;n++)
  {
     floatx80 tmp;

     // read register only if its tag is not empty
     if (! IS_TAG_EMPTY(n))
     {
         read_virtual_tword(i->seg(), RMAddr(i) + offset + n*10, &tmp);
         BX_WRITE_FPU_REG(tmp, n);
     }
  }
#else
  BX_INFO(("FRSTOR: required FPU, configure --enable-fpu"));
#endif
}

/* DD /6 */
void BX_CPU_C::FNSAVE(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);

  int offset = fpu_save_environment(i);

  /* save all registers in stack order. */
  for(int n=0;n<8;n++)
  {
     floatx80 stn = BX_READ_FPU_REG(n);
     write_virtual_tword(i->seg(), RMAddr(i) + offset + n*10, &stn);
  }

  BX_CPU_THIS_PTR the_i387.init();
#else
  BX_INFO(("FNSAVE: required FPU, configure --enable-fpu"));
#endif
}

/* 9B E2 */
void BX_CPU_C::FNCLEX(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);

  FPU_PARTIAL_STATUS &= ~(FPU_SW_Backward|FPU_SW_Summary|FPU_SW_Stack_Fault|FPU_SW_Precision|
		   FPU_SW_Underflow|FPU_SW_Overflow|FPU_SW_Zero_Div|FPU_SW_Denormal_Op|
		   FPU_SW_Invalid);

  // do not update last fpu instruction pointer
#else
  BX_INFO(("FNCLEX: required FPU, configure --enable-fpu"));
#endif
}

/* DB E3 */
void BX_CPU_C::FNINIT(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  BX_CPU_THIS_PTR the_i387.init();
#else
  BX_INFO(("FNINIT: required FPU, configure --enable-fpu"));
#endif
}

/* D9 /4 */
void BX_CPU_C::FLDENV(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  fpu_load_environment(i);
#else
  BX_INFO(("FLDENV: required FPU, configure --enable-fpu"));
#endif
}

/* D9 /6 */
void BX_CPU_C::FNSTENV(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
  fpu_save_environment(i);
  /* mask all floating point exceptions */
  FPU_CONTROL_WORD |= FPU_CW_Exceptions_Mask;
  /* clear the B and ES bits in the status word */
  FPU_PARTIAL_STATUS &= ~(FPU_SW_Backward|FPU_SW_Summary);
#else
  BX_INFO(("FNSTENV: required FPU, configure --enable-fpu"));
#endif
}

/* D9 D0 */
void BX_CPU_C::FNOP(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);

  // Perform no FPU operation. This instruction takes up space in the
  // instruction stream but does not affect the FPU or machine
  // context, except the EIP register.
#else
  BX_INFO(("FNOP: required FPU, configure --enable-fpu"));
#endif
}

void BX_CPU_C::FPLEGACY(bxInstruction_c *i)
{
#if BX_SUPPORT_FPU
  BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);

  // FPU performs no specific operation and no internal x87 states
  // are affected
#else
  BX_INFO(("legacy FPU opcodes: required FPU, configure --enable-fpu"));
#endif
}


#if BX_SUPPORT_FPU

#include <math.h>

void BX_CPU_C::print_state_FPU()
{
  static double scale_factor = pow(2.0, -63.0);

  Bit32u reg;
  reg = BX_CPU_THIS_PTR the_i387.get_control_word();
  fprintf(stderr, "control word: 0x%04x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.get_status_word();
  fprintf(stderr, "status word:  0x%04x\n", reg);
  fprintf(stderr, "        TOP:  %d\n", FPU_TOS&7);
  reg = BX_CPU_THIS_PTR the_i387.get_tag_word();
  fprintf(stderr, "tag word:     0x%04x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.foo;
  fprintf(stderr, "operand:      0x%04x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.fip & 0xffffffff;
  fprintf(stderr, "fip:          0x%08x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.fcs;
  fprintf(stderr, "fcs:          0x%04x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.fdp & 0xffffffff;
  fprintf(stderr, "fdp:          0x%08x\n", reg);
  reg = BX_CPU_THIS_PTR the_i387.fds;
  fprintf(stderr, "fds:          0x%04x\n", reg);

  // print stack too
  int tos = FPU_TOS & 7;
  for (int i=0; i<8; i++) {
    const floatx80 &fp = BX_FPU_REG(i);
    double f = pow(2.0, ((0x7fff & fp.exp) - 0x3fff));
    if (fp.exp & 0x8000) f = -f;
#ifdef _MSC_VER
    f *= (double)(signed __int64)(fp.fraction>>1) * scale_factor * 2;
#else
    f *= fp.fraction*scale_factor;
#endif
    fprintf(stderr, "%sFPR%d(%c):        %.10f (raw 0x%04x:%08x%08x)\n",
          i==tos?"=>":"  ",
          i, 
          "v0s?"[BX_CPU_THIS_PTR the_i387.FPU_gettagi((i-tos)&7)],
          f, fp.exp & 0xffff, fp.fraction >> 32, fp.fraction & 0xffffffff);
  }
}
#endif