2004-04-09 19:34:59 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2007-12-23 20:21:28 +03:00
|
|
|
// $Id: fpu.cc,v 1.30 2007-12-23 17:21:27 sshwarts Exp $
|
2005-03-21 00:19:38 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-04-09 19:34:59 +04:00
|
|
|
//
|
2005-05-12 22:07:48 +04:00
|
|
|
// Copyright (c) 2003 Stanislav Shwartsman
|
2007-03-24 00:27:13 +03:00
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
2004-04-09 19:34:59 +04:00
|
|
|
//
|
|
|
|
// 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
|
2005-05-12 22:07:48 +04:00
|
|
|
//
|
2004-06-18 19:14:50 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-04-09 19:34:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
#define NEED_CPU_REG_SHORTCUTS 1
|
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu/cpu.h"
|
2004-04-09 19:34:59 +04:00
|
|
|
#define LOG_THIS BX_CPU_THIS_PTR
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "iodev/iodev.h"
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
#define UPDATE_LAST_OPCODE 1
|
|
|
|
#define CHECK_PENDING_EXCEPTIONS 1
|
|
|
|
|
|
|
|
|
2004-04-09 19:34:59 +04:00
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
void BX_CPU_C::prepareFPU(bxInstruction_c *i,
|
|
|
|
bx_bool check_pending_exceptions, bx_bool update_last_instruction)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
2007-07-09 19:16:14 +04:00
|
|
|
if (BX_CPU_THIS_PTR cr0.get_EM() || BX_CPU_THIS_PTR cr0.get_TS())
|
2004-04-09 19:34:59 +04:00
|
|
|
exception(BX_NM_EXCEPTION, 0, 0);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
if (check_pending_exceptions)
|
|
|
|
BX_CPU_THIS_PTR FPU_check_pending_exceptions();
|
|
|
|
|
|
|
|
if (update_last_instruction)
|
|
|
|
{
|
2007-10-30 21:52:25 +03:00
|
|
|
BX_CPU_THIS_PTR the_i387.foo = ((Bit32u)(i->b1()) << 8) | (Bit32u)(i->modrm()) & 0x7ff;
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fcs = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
2007-11-24 17:22:34 +03:00
|
|
|
BX_CPU_THIS_PTR the_i387.fip = BX_CPU_THIS_PTR prev_rip;
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BX_CPU_C::FPU_check_pending_exceptions(void)
|
|
|
|
{
|
2004-06-18 19:14:50 +04:00
|
|
|
if(BX_CPU_THIS_PTR the_i387.get_partial_status() & FPU_SW_Summary)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
2006-09-08 15:26:04 +04:00
|
|
|
// NE=1 selects the native or internal mode, which generates #MF,
|
|
|
|
// which is an extension introduced with 80486.
|
|
|
|
// NE=0 selects the original (backward compatible) FPU error
|
|
|
|
// handling, which generates an IRQ 13 via the PIC chip.
|
2005-08-13 21:40:41 +04:00
|
|
|
#if BX_CPU_LEVEL >= 4
|
2007-07-09 19:16:14 +04:00
|
|
|
if (BX_CPU_THIS_PTR cr0.get_NE() != 0) {
|
2006-09-08 15:26:04 +04:00
|
|
|
exception(BX_MF_EXCEPTION, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
2005-08-13 21:40:41 +04:00
|
|
|
#endif
|
2006-09-08 15:26:04 +04:00
|
|
|
{
|
|
|
|
// MSDOS compatibility external interrupt (IRQ13)
|
|
|
|
BX_INFO (("math_abort: MSDOS compatibility FPU exception"));
|
|
|
|
DEV_pic_raise_irq(13);
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
int BX_CPU_C::fpu_save_environment(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
2004-06-18 19:14:50 +04:00
|
|
|
if (protected_mode()) /* Protected Mode */
|
|
|
|
{
|
|
|
|
if (i->os32L() || i->os64L())
|
|
|
|
{
|
|
|
|
Bit32u tmp;
|
|
|
|
|
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_control_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i), tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x04, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_tag_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x08, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fip) & 0xffffffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x0c, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fcs & 0xffff) |
|
|
|
|
((Bit32u)(BX_CPU_THIS_PTR the_i387.foo)) << 16;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x10, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fdp) & 0xffffffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x14, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | (BX_CPU_THIS_PTR the_i387.fds);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x18, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
return 0x1c;
|
|
|
|
}
|
|
|
|
else /* Protected Mode - 16 bit */
|
|
|
|
{
|
|
|
|
Bit16u tmp;
|
|
|
|
|
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_control_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i), tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x02, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_tag_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x04, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fip) & 0xffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x06, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fcs);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x08, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fdp) & 0xffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x0a, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (BX_CPU_THIS_PTR the_i387.fds);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x0c, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
return 0x0e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* Real or V86 Mode */
|
|
|
|
{
|
|
|
|
Bit32u fp_ip = ((Bit32u)(BX_CPU_THIS_PTR the_i387.fcs) << 4) +
|
2007-12-23 20:21:28 +03:00
|
|
|
(Bit32u)(BX_CPU_THIS_PTR the_i387.fip);
|
2004-06-18 19:14:50 +04:00
|
|
|
Bit32u fp_dp = ((Bit32u)(BX_CPU_THIS_PTR the_i387.fds) << 4) +
|
2007-12-23 20:21:28 +03:00
|
|
|
(Bit32u)(BX_CPU_THIS_PTR the_i387.fdp);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
if (i->os32L() || i->os64L())
|
|
|
|
{
|
|
|
|
Bit32u tmp;
|
|
|
|
|
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_control_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i), tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x04, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | BX_CPU_THIS_PTR the_i387.get_tag_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x08, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | (fp_ip & 0xffff);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x0c, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = ((fp_ip & 0xffff0000) >> 4) |
|
|
|
|
(BX_CPU_THIS_PTR the_i387.foo & 0x7ff);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x10, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = 0xffff0000 | (fp_dp & 0xffff);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x14, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (fp_dp & 0xffff0000) >> 4;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_dword(i->seg(), RMAddr(i) + 0x18, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
return 0x1c;
|
|
|
|
}
|
|
|
|
else /* Real or V86 Mode - 16 bit */
|
|
|
|
{
|
|
|
|
Bit16u tmp;
|
|
|
|
|
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_control_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i), tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x02, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = BX_CPU_THIS_PTR the_i387.get_tag_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x04, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = fp_ip & 0xffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x06, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (Bit16u)(((fp_ip & 0xf0000) >> 4) |
|
|
|
|
(BX_CPU_THIS_PTR the_i387.foo & 0x7ff));
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x08, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = fp_dp & 0xffff;
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x0a, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
tmp = (Bit16u)((fp_dp & 0xf0000) >> 4);
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i) + 0x0c, tmp);
|
2004-06-18 19:14:50 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i));
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.cwd = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x04);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.swd = tmp & 0xffff;
|
|
|
|
BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x08);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.twd = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x0c);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fip = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x10);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fcs = tmp & 0xffff;
|
|
|
|
BX_CPU_THIS_PTR the_i387.foo = (tmp >> 16) & 0x07ff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x14);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fdp = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x18);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fds = tmp & 0xffff;
|
|
|
|
offset = 0x1c;
|
|
|
|
}
|
|
|
|
else /* Protected Mode - 16 bit */
|
|
|
|
{
|
|
|
|
Bit16u tmp;
|
|
|
|
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i));
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.cwd = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x2);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.swd = tmp;
|
|
|
|
BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x04);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.twd = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x06);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fip = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x08);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fcs = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x0a);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.fdp = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x0c);
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
|
|
|
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i));
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.cwd = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x04);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.swd = tmp & 0xffff;
|
|
|
|
BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x08);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.twd = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x0c);
|
2004-06-18 19:14:50 +04:00
|
|
|
fp_ip = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x10);
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x14);
|
2004-06-18 19:14:50 +04:00
|
|
|
fp_dp = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_dword(i->seg(), RMAddr(i) + 0x18);
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
|
|
|
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i));
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.cwd = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x2);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.swd = tmp;
|
|
|
|
BX_CPU_THIS_PTR the_i387.tos = (tmp >> 11) & 0x07;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x04);
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.twd = tmp;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x06);
|
2004-06-18 19:14:50 +04:00
|
|
|
fp_ip = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x08);
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x0a);
|
2004-06-18 19:14:50 +04:00
|
|
|
fp_dp = tmp & 0xffff;
|
2007-12-20 23:58:38 +03:00
|
|
|
tmp = read_virtual_word(i->seg(), RMAddr(i) + 0x0c);
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
return offset;
|
2004-04-09 19:34:59 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* D9 /5 */
|
2004-04-09 19:34:59 +04:00
|
|
|
void BX_CPU_C::FLDCW(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2007-12-20 23:58:38 +03:00
|
|
|
Bit16u cwd = read_virtual_word(i->seg(), RMAddr(i));
|
2004-06-18 19:14:50 +04:00
|
|
|
FPU_CONTROL_WORD = cwd;
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
|
|
|
BX_INFO(("FLDCW: required FPU, configure --enable-fpu"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* D9 /7 */
|
|
|
|
void BX_CPU_C::FNSTCW(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
Bit16u cwd = BX_CPU_THIS_PTR the_i387.get_control_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i), cwd);
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNSTCW: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* DD /7 */
|
|
|
|
void BX_CPU_C::FNSTSW(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
Bit16u swd = BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-12-20 21:29:42 +03:00
|
|
|
write_virtual_word(i->seg(), RMAddr(i), swd);
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNSTSW: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* DF E0 */
|
|
|
|
void BX_CPU_C::FNSTSW_AX(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
AX = BX_CPU_THIS_PTR the_i387.get_status_word();
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNSTSW_AX: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* DD /4 */
|
|
|
|
void BX_CPU_C::FRSTOR(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
int offset = fpu_load_environment(i);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FRSTOR: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* DD /6 */
|
|
|
|
void BX_CPU_C::FNSAVE(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
int offset = fpu_save_environment(i);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR the_i387.init();
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNSAVE: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* 9B E2 */
|
|
|
|
void BX_CPU_C::FNCLEX(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
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);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
// do not update last fpu instruction pointer
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNCLEX: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* DB E3 */
|
|
|
|
void BX_CPU_C::FNINIT(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
BX_CPU_THIS_PTR the_i387.init();
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNINIT: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* D9 /4 */
|
|
|
|
void BX_CPU_C::FLDENV(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2006-01-28 23:52:08 +03:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2006-01-29 00:31:20 +03:00
|
|
|
fpu_load_environment(i);
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FLDENV: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* D9 /6 */
|
2004-04-09 19:34:59 +04:00
|
|
|
void BX_CPU_C::FNSTENV(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
|
|
|
fpu_save_environment(i);
|
2006-01-29 20:37:23 +03:00
|
|
|
/* 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);
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
|
|
|
BX_INFO(("FNSTENV: required FPU, configure --enable-fpu"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
/* D9 D0 */
|
|
|
|
void BX_CPU_C::FNOP(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
// 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.
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("FNOP: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
void BX_CPU_C::FPLEGACY(bxInstruction_c *i)
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_FPU
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_CPU_THIS_PTR prepareFPU(i, !CHECK_PENDING_EXCEPTIONS, !UPDATE_LAST_OPCODE);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
// FPU performs no specific operation and no internal x87 states
|
|
|
|
// are affected
|
2004-04-09 19:34:59 +04:00
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
BX_INFO(("legacy FPU opcodes: required FPU, configure --enable-fpu"));
|
2004-04-09 19:34:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if BX_SUPPORT_FPU
|
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
#include <math.h>
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
void BX_CPU_C::print_state_FPU()
|
2004-04-09 19:34:59 +04:00
|
|
|
{
|
2004-06-18 19:14:50 +04:00
|
|
|
static double scale_factor = pow(2.0, -63.0);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
Bit32u reg;
|
2005-06-19 00:46:08 +04:00
|
|
|
reg = BX_CPU_THIS_PTR the_i387.get_control_word();
|
2004-06-18 19:14:50 +04:00
|
|
|
fprintf(stderr, "control word: 0x%04x\n", reg);
|
2005-06-19 00:46:08 +04:00
|
|
|
reg = BX_CPU_THIS_PTR the_i387.get_status_word();
|
2007-09-11 17:11:03 +04:00
|
|
|
fprintf(stderr, "status word: 0x%04x\n", reg);
|
|
|
|
fprintf(stderr, " TOS : %d\n", FPU_TOS&7);
|
2005-06-19 00:46:08 +04:00
|
|
|
reg = BX_CPU_THIS_PTR the_i387.get_tag_word();
|
2004-06-18 19:14:50 +04:00
|
|
|
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);
|
2004-04-09 19:34:59 +04:00
|
|
|
|
2004-06-18 19:14:50 +04:00
|
|
|
// print stack too
|
2006-01-21 03:05:30 +03:00
|
|
|
int tos = FPU_TOS & 7;
|
2004-06-18 19:14:50 +04:00
|
|
|
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;
|
2005-01-19 21:21:40 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
f *= (double)(signed __int64)(fp.fraction>>1) * scale_factor * 2;
|
|
|
|
#else
|
2004-06-18 19:14:50 +04:00
|
|
|
f *= fp.fraction*scale_factor;
|
2005-01-19 21:21:40 +03:00
|
|
|
#endif
|
2007-10-25 03:29:40 +04:00
|
|
|
fprintf(stderr, "%sFPR%d(%c): %.10f (raw 0x%04x:%08lx%08lx)\n",
|
2006-01-21 03:05:30 +03:00
|
|
|
i==tos?"=>":" ",
|
|
|
|
i,
|
|
|
|
"v0s?"[BX_CPU_THIS_PTR the_i387.FPU_gettagi((i-tos)&7)],
|
2004-06-18 19:14:50 +04:00
|
|
|
f, fp.exp & 0xffff, fp.fraction >> 32, fp.fraction & 0xffffffff);
|
|
|
|
}
|
2004-04-09 19:34:59 +04:00
|
|
|
}
|
|
|
|
#endif
|