1 /////////////////////////////////////////////////////////////////////////
2 // $Id: fpu_misc.cc 13466 2018-02-16 07:57:32Z sshwarts $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 //   Copyright (c) 2003-2018 Stanislav Shwartsman
6 //          Written by Stanislav Shwartsman [sshwarts at sourceforge net]
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 //  You should have received a copy of the GNU Lesser General Public
19 //  License along with this library; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 //
22 /////////////////////////////////////////////////////////////////////////
23 
24 #define NEED_CPU_REG_SHORTCUTS 1
25 #include "bochs.h"
26 #include "cpu/cpu.h"
27 #define LOG_THIS BX_CPU_THIS_PTR
28 
29 #if BX_SUPPORT_FPU
30 
31 #include "softfloatx80.h"
32 
33 /* D9 C8 */
FXCH_STi(bxInstruction_c * i)34 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FXCH_STi(bxInstruction_c *i)
35 {
36   BX_CPU_THIS_PTR prepareFPU(i);
37   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
38 
39   int st0_tag = BX_CPU_THIS_PTR the_i387.FPU_gettagi(0);
40   int sti_tag = BX_CPU_THIS_PTR the_i387.FPU_gettagi(i->src());
41 
42   floatx80 st0_reg = BX_READ_FPU_REG(0);
43   floatx80 sti_reg = BX_READ_FPU_REG(i->src());
44 
45   clear_C1();
46 
47   if (st0_tag == FPU_Tag_Empty || sti_tag == FPU_Tag_Empty)
48   {
49      FPU_exception(i, FPU_EX_Stack_Underflow);
50 
51      if(BX_CPU_THIS_PTR the_i387.is_IA_masked())
52      {
53          /* Masked response */
54          if (st0_tag == FPU_Tag_Empty)
55              st0_reg = floatx80_default_nan;
56 
57          if (sti_tag == FPU_Tag_Empty)
58              sti_reg = floatx80_default_nan;
59      }
60      else {
61          BX_NEXT_INSTR(i);
62      }
63   }
64 
65   BX_WRITE_FPU_REG(st0_reg, i->src());
66   BX_WRITE_FPU_REG(sti_reg, 0);
67 
68   BX_NEXT_INSTR(i);
69 }
70 
71 /* D9 E0 */
FCHS(bxInstruction_c * i)72 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FCHS(bxInstruction_c *i)
73 {
74   BX_CPU_THIS_PTR prepareFPU(i);
75   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
76 
77   if (IS_TAG_EMPTY(0)) {
78      FPU_stack_underflow(i, 0);
79   }
80   else {
81      clear_C1();
82      floatx80 st0_reg = BX_READ_FPU_REG(0);
83      BX_WRITE_FPU_REG(floatx80_chs(st0_reg), 0);
84   }
85 
86   BX_NEXT_INSTR(i);
87 }
88 
89 /* D9 E1 */
FABS(bxInstruction_c * i)90 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FABS(bxInstruction_c *i)
91 {
92   BX_CPU_THIS_PTR prepareFPU(i);
93   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
94 
95   if (IS_TAG_EMPTY(0)) {
96      FPU_stack_underflow(i, 0);
97   }
98   else {
99      clear_C1();
100      floatx80 st0_reg = BX_READ_FPU_REG(0);
101      BX_WRITE_FPU_REG(floatx80_abs(st0_reg), 0);
102   }
103 
104   BX_NEXT_INSTR(i);
105 }
106 
107 /* D9 F6 */
FDECSTP(bxInstruction_c * i)108 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FDECSTP(bxInstruction_c *i)
109 {
110   BX_CPU_THIS_PTR prepareFPU(i);
111   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
112 
113   clear_C1();
114 
115   BX_CPU_THIS_PTR the_i387.tos = (BX_CPU_THIS_PTR the_i387.tos-1) & 7;
116 
117   BX_NEXT_INSTR(i);
118 }
119 
120 /* D9 F7 */
FINCSTP(bxInstruction_c * i)121 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FINCSTP(bxInstruction_c *i)
122 {
123   BX_CPU_THIS_PTR prepareFPU(i);
124   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
125 
126   clear_C1();
127 
128   BX_CPU_THIS_PTR the_i387.tos = (BX_CPU_THIS_PTR the_i387.tos+1) & 7;
129 
130   BX_NEXT_INSTR(i);
131 }
132 
133 /* DD C0 */
FFREE_STi(bxInstruction_c * i)134 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FFREE_STi(bxInstruction_c *i)
135 {
136   BX_CPU_THIS_PTR prepareFPU(i);
137   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
138 
139   clear_C1();
140 
141   BX_CPU_THIS_PTR the_i387.FPU_settagi(FPU_Tag_Empty, i->dst());
142 
143   BX_NEXT_INSTR(i);
144 }
145 
146 /*
147  * Free the st(0) register and pop it from the FPU stack.
148  * "Undocumented" by Intel & AMD but mentioned in AMDs Athlon Docs.
149  */
150 
151 /* DF C0 */
FFREEP_STi(bxInstruction_c * i)152 void BX_CPP_AttrRegparmN(1) BX_CPU_C::FFREEP_STi(bxInstruction_c *i)
153 {
154   BX_CPU_THIS_PTR prepareFPU(i);
155   BX_CPU_THIS_PTR FPU_update_last_instruction(i);
156 
157   clear_C1();
158 
159   BX_CPU_THIS_PTR the_i387.FPU_settagi(FPU_Tag_Empty, i->dst());
160   BX_CPU_THIS_PTR the_i387.FPU_pop();
161 
162   BX_NEXT_INSTR(i);
163 }
164 
165 #endif
166