master
c 262 lines 6.83 KB
Raw
1 /*
2 NetWinder Floating Point Emulator
3 (c) Rebel.COM, 1998,1999
4
5 Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "fpa11.h"
23
24 #include "fpopcode.h"
25
26 //#include "fpmodule.h"
27 //#include "fpmodule.inl"
28
29 //#include <asm/system.h>
30
31
32 __thread FPA11* qemufpa = NULL;
33
34 /* Reset the FPA11 chip. Called to initialize and reset the emulator. */
35 void resetFPA11(void)
36 {
37 int i;
38 FPA11 *fpa11 = GET_FPA11();
39
40 /* initialize the register type array */
41 for (i=0;i<=7;i++)
42 {
43 fpa11->fType[i] = typeNone;
44 }
45
46 /* FPSR: set system id to FP_EMULATOR, set AC, clear all other bits */
47 fpa11->fpsr = FP_EMULATOR | BIT_AC;
48
49 /* FPCR: set SB, AB and DA bits, clear all others */
50 #ifdef MAINTAIN_FPCR
51 fpa11->fpcr = MASK_RESET;
52 #endif
53
54 /*
55 * Real FPA11 hardware does not handle NaNs, but always takes an
56 * exception for them to be software-emulated (ARM7500FE datasheet
57 * section 10.4). There is no documented architectural requirement
58 * for NaN propagation rules and it will depend on how the OS
59 * level software emulation opted to do it. We here use prop_s_ab
60 * which matches the later VFP hardware choice and how QEMU's
61 * fpa11 emulation has worked in the past. The real Linux kernel
62 * does something slightly different: arch/arm/nwfpe/softfloat-specialize
63 * propagateFloat64NaN() has the curious behaviour that it prefers
64 * the QNaN over the SNaN, but if both are QNaN it picks A and
65 * if both are SNaN it picks B. In theory we could add this as
66 * a NaN propagation rule, but in practice FPA11 emulation is so
67 * close to totally dead that it's not worth trying to match it at
68 * this late date.
69 */
70 set_float_2nan_prop_rule(float_2nan_prop_s_ab, &fpa11->fp_status);
71 /*
72 * Use the same default NaN value as Arm VFP. This doesn't match
73 * the Linux kernel's nwfpe emulation, which uses an all-1s value.
74 */
75 set_float_default_nan_pattern(0b01000000, &fpa11->fp_status);
76 }
77
78 void SetRoundingMode(const unsigned int opcode)
79 {
80 int rounding_mode;
81 FPA11 *fpa11 = GET_FPA11();
82
83 #ifdef MAINTAIN_FPCR
84 fpa11->fpcr &= ~MASK_ROUNDING_MODE;
85 #endif
86 switch (opcode & MASK_ROUNDING_MODE)
87 {
88 default:
89 case ROUND_TO_NEAREST:
90 rounding_mode = float_round_nearest_even;
91 #ifdef MAINTAIN_FPCR
92 fpa11->fpcr |= ROUND_TO_NEAREST;
93 #endif
94 break;
95
96 case ROUND_TO_PLUS_INFINITY:
97 rounding_mode = float_round_up;
98 #ifdef MAINTAIN_FPCR
99 fpa11->fpcr |= ROUND_TO_PLUS_INFINITY;
100 #endif
101 break;
102
103 case ROUND_TO_MINUS_INFINITY:
104 rounding_mode = float_round_down;
105 #ifdef MAINTAIN_FPCR
106 fpa11->fpcr |= ROUND_TO_MINUS_INFINITY;
107 #endif
108 break;
109
110 case ROUND_TO_ZERO:
111 rounding_mode = float_round_to_zero;
112 #ifdef MAINTAIN_FPCR
113 fpa11->fpcr |= ROUND_TO_ZERO;
114 #endif
115 break;
116 }
117 set_float_rounding_mode(rounding_mode, &fpa11->fp_status);
118 }
119
120 void SetRoundingPrecision(const unsigned int opcode)
121 {
122 FloatX80RoundPrec rounding_precision;
123 FPA11 *fpa11 = GET_FPA11();
124 #ifdef MAINTAIN_FPCR
125 fpa11->fpcr &= ~MASK_ROUNDING_PRECISION;
126 #endif
127 switch (opcode & MASK_ROUNDING_PRECISION) {
128 case ROUND_SINGLE:
129 rounding_precision = floatx80_precision_s;
130 #ifdef MAINTAIN_FPCR
131 fpa11->fpcr |= ROUND_SINGLE;
132 #endif
133 break;
134
135 case ROUND_DOUBLE:
136 rounding_precision = floatx80_precision_d;
137 #ifdef MAINTAIN_FPCR
138 fpa11->fpcr |= ROUND_DOUBLE;
139 #endif
140 break;
141
142 case ROUND_EXTENDED:
143 rounding_precision = floatx80_precision_x;
144 #ifdef MAINTAIN_FPCR
145 fpa11->fpcr |= ROUND_EXTENDED;
146 #endif
147 break;
148
149 default:
150 rounding_precision = floatx80_precision_x;
151 break;
152 }
153 set_floatx80_rounding_precision(rounding_precision, &fpa11->fp_status);
154 }
155
156 /* Emulate the instruction in the opcode. */
157 unsigned int EmulateAll(unsigned int opcode, FPA11* qfpa)
158 {
159 unsigned int nRc = 0;
160 // unsigned long flags;
161 FPA11 *fpa11;
162 unsigned int cp;
163 // save_flags(flags); sti();
164
165 /* Check that this is really an FPA11 instruction: the coprocessor
166 * field in bits [11:8] must be 1 or 2.
167 */
168 cp = (opcode >> 8) & 0xf;
169 if (cp != 1 && cp != 2) {
170 return 0;
171 }
172
173 qemufpa=qfpa;
174 fpa11 = GET_FPA11();
175
176 if (fpa11->initflag == 0) /* good place for __builtin_expect */
177 {
178 resetFPA11();
179 SetRoundingMode(ROUND_TO_NEAREST);
180 SetRoundingPrecision(ROUND_EXTENDED);
181 fpa11->initflag = 1;
182 }
183
184 set_float_exception_flags(0, &fpa11->fp_status);
185
186 if (TEST_OPCODE(opcode,MASK_CPRT))
187 {
188 //fprintf(stderr,"emulating CPRT\n");
189 /* Emulate conversion opcodes. */
190 /* Emulate register transfer opcodes. */
191 /* Emulate comparison opcodes. */
192 nRc = EmulateCPRT(opcode);
193 }
194 else if (TEST_OPCODE(opcode,MASK_CPDO))
195 {
196 //fprintf(stderr,"emulating CPDO\n");
197 /* Emulate monadic arithmetic opcodes. */
198 /* Emulate dyadic arithmetic opcodes. */
199 nRc = EmulateCPDO(opcode);
200 }
201 else if (TEST_OPCODE(opcode,MASK_CPDT))
202 {
203 //fprintf(stderr,"emulating CPDT\n");
204 /* Emulate load/store opcodes. */
205 /* Emulate load/store multiple opcodes. */
206 nRc = EmulateCPDT(opcode);
207 }
208 else
209 {
210 /* Invalid instruction detected. Return FALSE. */
211 nRc = 0;
212 }
213
214 // restore_flags(flags);
215 if(nRc == 1 && get_float_exception_flags(&fpa11->fp_status))
216 {
217 //printf("fef 0x%x\n",float_exception_flags);
218 nRc = -get_float_exception_flags(&fpa11->fp_status);
219 }
220
221 //printf("returning %d\n",nRc);
222 return(nRc);
223 }
224
225 #if 0
226 unsigned int EmulateAll1(unsigned int opcode)
227 {
228 switch ((opcode >> 24) & 0xf)
229 {
230 case 0xc:
231 case 0xd:
232 if ((opcode >> 20) & 0x1)
233 {
234 switch ((opcode >> 8) & 0xf)
235 {
236 case 0x1: return PerformLDF(opcode); break;
237 case 0x2: return PerformLFM(opcode); break;
238 default: return 0;
239 }
240 }
241 else
242 {
243 switch ((opcode >> 8) & 0xf)
244 {
245 case 0x1: return PerformSTF(opcode); break;
246 case 0x2: return PerformSFM(opcode); break;
247 default: return 0;
248 }
249 }
250 break;
251
252 case 0xe:
253 if (opcode & 0x10)
254 return EmulateCPDO(opcode);
255 else
256 return EmulateCPRT(opcode);
257 break;
258
259 default: return 0;
260 }
261 }
262 #endif