fpu_emulate.c revision 1.30.4.1 1 1.30.4.1 rmind /* $NetBSD: fpu_emulate.c,v 1.30.4.1 2011/05/31 03:04:07 rmind Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1995 Gordon W. Ross
5 1.3 briggs * some portion Copyright (c) 1995 Ken Nakata
6 1.1 gwr * All rights reserved.
7 1.1 gwr *
8 1.1 gwr * Redistribution and use in source and binary forms, with or without
9 1.1 gwr * modification, are permitted provided that the following conditions
10 1.1 gwr * are met:
11 1.1 gwr * 1. Redistributions of source code must retain the above copyright
12 1.1 gwr * notice, this list of conditions and the following disclaimer.
13 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 gwr * notice, this list of conditions and the following disclaimer in the
15 1.1 gwr * documentation and/or other materials provided with the distribution.
16 1.1 gwr * 3. The name of the author may not be used to endorse or promote products
17 1.1 gwr * derived from this software without specific prior written permission.
18 1.1 gwr * 4. All advertising materials mentioning features or use of this software
19 1.1 gwr * must display the following acknowledgement:
20 1.1 gwr * This product includes software developed by Gordon Ross
21 1.1 gwr *
22 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 gwr */
33 1.1 gwr
34 1.1 gwr /*
35 1.1 gwr * mc68881 emulator
36 1.1 gwr * XXX - Just a start at it for now...
37 1.1 gwr */
38 1.24 lukem
39 1.24 lukem #include <sys/cdefs.h>
40 1.30.4.1 rmind __KERNEL_RCSID(0, "$NetBSD: fpu_emulate.c,v 1.30.4.1 2011/05/31 03:04:07 rmind Exp $");
41 1.20 jonathan
42 1.27 tsutsui #include <sys/param.h>
43 1.1 gwr #include <sys/types.h>
44 1.1 gwr #include <sys/signal.h>
45 1.5 briggs #include <sys/systm.h>
46 1.1 gwr #include <machine/frame.h>
47 1.1 gwr
48 1.21 briggs #if defined(DDB) && defined(DEBUG_FPE)
49 1.15 veego # include <m68k/db_machdep.h>
50 1.15 veego #endif
51 1.15 veego
52 1.3 briggs #include "fpu_emulate.h"
53 1.1 gwr
54 1.30.4.1 rmind #define fpe_abort(tfp, ksi, signo, code) \
55 1.30.4.1 rmind do { \
56 1.30.4.1 rmind (ksi)->ksi_signo = (signo); \
57 1.30.4.1 rmind (ksi)->ksi_code = (code); \
58 1.30.4.1 rmind (ksi)->ksi_addr = (void *)(frame)->f_pc; \
59 1.30.4.1 rmind return -1; \
60 1.30.4.1 rmind } while (/* CONSTCOND */ 0)
61 1.30.4.1 rmind
62 1.30.4.1 rmind static int fpu_emul_fmovmcr(struct fpemu *, struct instruction *);
63 1.30.4.1 rmind static int fpu_emul_fmovm(struct fpemu *, struct instruction *);
64 1.30.4.1 rmind static int fpu_emul_arith(struct fpemu *, struct instruction *);
65 1.30.4.1 rmind static int fpu_emul_type1(struct fpemu *, struct instruction *);
66 1.30.4.1 rmind static int fpu_emul_brcc(struct fpemu *, struct instruction *);
67 1.30.4.1 rmind static int test_cc(struct fpemu *, int);
68 1.30.4.1 rmind static struct fpn *fpu_cmp(struct fpemu *);
69 1.30.4.1 rmind
70 1.30.4.1 rmind #ifdef DEBUG_FPE
71 1.30.4.1 rmind #define DUMP_INSN(insn) \
72 1.30.4.1 rmind printf("%s: insn={adv=%d,siz=%d,op=%04x,w1=%04x}\n", \
73 1.30.4.1 rmind __func__, \
74 1.30.4.1 rmind (insn)->is_advance, (insn)->is_datasize, \
75 1.30.4.1 rmind (insn)->is_opcode, (insn)->is_word1)
76 1.30.4.1 rmind #define DPRINTF(x) printf x
77 1.21 briggs #else
78 1.30.4.1 rmind #define DUMP_INSN(insn) do {} while (/* CONSTCOND */ 0)
79 1.30.4.1 rmind #define DPRINTF(x) do {} while (/* CONSTCOND */ 0)
80 1.3 briggs #endif
81 1.1 gwr
82 1.1 gwr /*
83 1.1 gwr * Emulate a floating-point instruction.
84 1.1 gwr * Return zero for success, else signal number.
85 1.1 gwr * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
86 1.1 gwr */
87 1.3 briggs int
88 1.30 dsl fpu_emulate(struct frame *frame, struct fpframe *fpf, ksiginfo_t *ksi)
89 1.1 gwr {
90 1.30.4.1 rmind static struct instruction insn;
91 1.30.4.1 rmind static struct fpemu fe;
92 1.30.4.1 rmind int word, optype, sig;
93 1.30.4.1 rmind
94 1.30.4.1 rmind
95 1.30.4.1 rmind /* initialize insn.is_datasize to tell it is *not* initialized */
96 1.30.4.1 rmind insn.is_datasize = -1;
97 1.30.4.1 rmind
98 1.30.4.1 rmind fe.fe_frame = frame;
99 1.30.4.1 rmind fe.fe_fpframe = fpf;
100 1.30.4.1 rmind fe.fe_fpsr = fpf->fpf_fpsr;
101 1.30.4.1 rmind fe.fe_fpcr = fpf->fpf_fpcr;
102 1.30.4.1 rmind
103 1.30.4.1 rmind DPRINTF(("%s: ENTERING: FPSR=%08x, FPCR=%08x\n",
104 1.30.4.1 rmind __func__, fe.fe_fpsr, fe.fe_fpcr));
105 1.30.4.1 rmind
106 1.30.4.1 rmind /* always set this (to avoid a warning) */
107 1.30.4.1 rmind insn.is_pc = frame->f_pc;
108 1.30.4.1 rmind insn.is_nextpc = 0;
109 1.30.4.1 rmind if (frame->f_format == 4) {
110 1.30.4.1 rmind /*
111 1.30.4.1 rmind * A format 4 is generated by the 68{EC,LC}040. The PC is
112 1.30.4.1 rmind * already set to the instruction following the faulting
113 1.30.4.1 rmind * instruction. We need to calculate that, anyway. The
114 1.30.4.1 rmind * fslw is the PC of the faulted instruction, which is what
115 1.30.4.1 rmind * we expect to be in f_pc.
116 1.30.4.1 rmind *
117 1.30.4.1 rmind * XXX - This is a hack; it assumes we at least know the
118 1.30.4.1 rmind * sizes of all instructions we run across.
119 1.30.4.1 rmind * XXX TODO: This may not be true, so we might want to save
120 1.30.4.1 rmind * the PC in order to restore it later.
121 1.30.4.1 rmind */
122 1.30.4.1 rmind #if 0
123 1.30.4.1 rmind insn.is_nextpc = frame->f_pc;
124 1.1 gwr #endif
125 1.30.4.1 rmind insn.is_pc = frame->f_fmt4.f_fslw;
126 1.30.4.1 rmind frame->f_pc = insn.is_pc;
127 1.30.4.1 rmind }
128 1.1 gwr
129 1.30.4.1 rmind word = fusword((void *)(insn.is_pc));
130 1.30.4.1 rmind if (word < 0) {
131 1.30.4.1 rmind DPRINTF(("%s: fault reading opcode\n", __func__));
132 1.30.4.1 rmind fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
133 1.30.4.1 rmind }
134 1.3 briggs
135 1.30.4.1 rmind if ((word & 0xf000) != 0xf000) {
136 1.30.4.1 rmind DPRINTF(("%s: not coproc. insn.: opcode=0x%x\n",
137 1.30.4.1 rmind __func__, word));
138 1.30.4.1 rmind fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
139 1.30.4.1 rmind }
140 1.1 gwr
141 1.30.4.1 rmind if ((word & 0x0E00) != 0x0200) {
142 1.30.4.1 rmind DPRINTF(("%s: bad coproc. id: opcode=0x%x\n", __func__, word));
143 1.30.4.1 rmind fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
144 1.30.4.1 rmind }
145 1.1 gwr
146 1.30.4.1 rmind insn.is_opcode = word;
147 1.30.4.1 rmind optype = (word & 0x01C0);
148 1.1 gwr
149 1.30.4.1 rmind word = fusword((void *)(insn.is_pc + 2));
150 1.30.4.1 rmind if (word < 0) {
151 1.30.4.1 rmind DPRINTF(("%s: fault reading word1\n", __func__));
152 1.30.4.1 rmind fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
153 1.1 gwr }
154 1.30.4.1 rmind insn.is_word1 = word;
155 1.30.4.1 rmind /* all FPU instructions are at least 4-byte long */
156 1.30.4.1 rmind insn.is_advance = 4;
157 1.30.4.1 rmind
158 1.30.4.1 rmind DUMP_INSN(&insn);
159 1.30.4.1 rmind
160 1.30.4.1 rmind /*
161 1.30.4.1 rmind * Which family (or type) of opcode is it?
162 1.30.4.1 rmind * Tests ordered by likelihood (hopefully).
163 1.30.4.1 rmind * Certainly, type 0 is the most common.
164 1.30.4.1 rmind */
165 1.30.4.1 rmind if (optype == 0x0000) {
166 1.30.4.1 rmind /* type=0: generic */
167 1.30.4.1 rmind if ((word & 0xc000) == 0xc000) {
168 1.30.4.1 rmind DPRINTF(("%s: fmovm FPr\n", __func__));
169 1.30.4.1 rmind sig = fpu_emul_fmovm(&fe, &insn);
170 1.30.4.1 rmind } else if ((word & 0xc000) == 0x8000) {
171 1.30.4.1 rmind DPRINTF(("%s: fmovm FPcr\n", __func__));
172 1.30.4.1 rmind sig = fpu_emul_fmovmcr(&fe, &insn);
173 1.30.4.1 rmind } else if ((word & 0xe000) == 0x6000) {
174 1.30.4.1 rmind /* fstore = fmove FPn,mem */
175 1.30.4.1 rmind DPRINTF(("%s: fmove to mem\n", __func__));
176 1.30.4.1 rmind sig = fpu_emul_fstore(&fe, &insn);
177 1.30.4.1 rmind } else if ((word & 0xfc00) == 0x5c00) {
178 1.30.4.1 rmind /* fmovecr */
179 1.30.4.1 rmind DPRINTF(("%s: fmovecr\n", __func__));
180 1.30.4.1 rmind sig = fpu_emul_fmovecr(&fe, &insn);
181 1.30.4.1 rmind } else if ((word & 0xa07f) == 0x26) {
182 1.30.4.1 rmind /* fscale */
183 1.30.4.1 rmind DPRINTF(("%s: fscale\n", __func__));
184 1.30.4.1 rmind sig = fpu_emul_fscale(&fe, &insn);
185 1.30.4.1 rmind } else {
186 1.30.4.1 rmind DPRINTF(("%s: other type0\n", __func__));
187 1.30.4.1 rmind /* all other type0 insns are arithmetic */
188 1.30.4.1 rmind sig = fpu_emul_arith(&fe, &insn);
189 1.30.4.1 rmind }
190 1.30.4.1 rmind if (sig == 0) {
191 1.30.4.1 rmind DPRINTF(("%s: type 0 returned 0\n", __func__));
192 1.30.4.1 rmind sig = fpu_upd_excp(&fe);
193 1.30.4.1 rmind }
194 1.30.4.1 rmind } else if (optype == 0x0080 || optype == 0x00C0) {
195 1.30.4.1 rmind /* type=2 or 3: fbcc, short or long disp. */
196 1.30.4.1 rmind DPRINTF(("%s: fbcc %s\n", __func__,
197 1.30.4.1 rmind (optype & 0x40) ? "long" : "short"));
198 1.30.4.1 rmind sig = fpu_emul_brcc(&fe, &insn);
199 1.30.4.1 rmind } else if (optype == 0x0040) {
200 1.30.4.1 rmind /* type=1: fdbcc, fscc, ftrapcc */
201 1.30.4.1 rmind DPRINTF(("%s: type1\n", __func__));
202 1.30.4.1 rmind sig = fpu_emul_type1(&fe, &insn);
203 1.30.4.1 rmind } else {
204 1.30.4.1 rmind /* type=4: fsave (privileged) */
205 1.30.4.1 rmind /* type=5: frestore (privileged) */
206 1.30.4.1 rmind /* type=6: reserved */
207 1.30.4.1 rmind /* type=7: reserved */
208 1.30.4.1 rmind DPRINTF(("%s: bad opcode type: opcode=0x%x\n", __func__,
209 1.30.4.1 rmind insn.is_opcode));
210 1.30.4.1 rmind sig = SIGILL;
211 1.1 gwr }
212 1.3 briggs
213 1.30.4.1 rmind DUMP_INSN(&insn);
214 1.1 gwr
215 1.30.4.1 rmind /*
216 1.30.4.1 rmind * XXX it is not clear to me, if we should progress the PC always,
217 1.30.4.1 rmind * for SIGFPE || 0, or only for 0; however, without SIGFPE, we
218 1.30.4.1 rmind * don't pass the signalling regression tests. -is
219 1.30.4.1 rmind */
220 1.30.4.1 rmind if ((sig == 0) || (sig == SIGFPE))
221 1.30.4.1 rmind frame->f_pc += insn.is_advance;
222 1.23 chs #if defined(DDB) && defined(DEBUG_FPE)
223 1.30.4.1 rmind else {
224 1.30.4.1 rmind printf("%s: sig=%d, opcode=%x, word1=%x\n", __func__,
225 1.30.4.1 rmind sig, insn.is_opcode, insn.is_word1);
226 1.30.4.1 rmind kdb_trap(-1, (db_regs_t *)&frame);
227 1.30.4.1 rmind }
228 1.1 gwr #endif
229 1.22 is #if 0 /* XXX something is wrong */
230 1.30.4.1 rmind if (frame->f_format == 4) {
231 1.30.4.1 rmind /* XXX Restore PC -- 68{EC,LC}040 only */
232 1.30.4.1 rmind if (insn.is_nextpc)
233 1.30.4.1 rmind frame->f_pc = insn.is_nextpc;
234 1.30.4.1 rmind }
235 1.22 is #endif
236 1.1 gwr
237 1.30.4.1 rmind DPRINTF(("%s: EXITING: w/FPSR=%08x, FPCR=%08x\n", __func__,
238 1.30.4.1 rmind fe.fe_fpsr, fe.fe_fpcr));
239 1.3 briggs
240 1.30.4.1 rmind if (sig)
241 1.30.4.1 rmind fpe_abort(frame, ksi, sig, 0);
242 1.30.4.1 rmind return sig;
243 1.1 gwr }
244 1.1 gwr
245 1.3 briggs /* update accrued exception bits and see if there's an FP exception */
246 1.3 briggs int
247 1.30 dsl fpu_upd_excp(struct fpemu *fe)
248 1.1 gwr {
249 1.30.4.1 rmind u_int fpsr;
250 1.30.4.1 rmind u_int fpcr;
251 1.3 briggs
252 1.30.4.1 rmind fpsr = fe->fe_fpsr;
253 1.30.4.1 rmind fpcr = fe->fe_fpcr;
254 1.30.4.1 rmind /*
255 1.30.4.1 rmind * update fpsr accrued exception bits; each insn doesn't have to
256 1.30.4.1 rmind * update this
257 1.30.4.1 rmind */
258 1.30.4.1 rmind if (fpsr & (FPSR_BSUN | FPSR_SNAN | FPSR_OPERR)) {
259 1.30.4.1 rmind fpsr |= FPSR_AIOP;
260 1.30.4.1 rmind }
261 1.30.4.1 rmind if (fpsr & FPSR_OVFL) {
262 1.30.4.1 rmind fpsr |= FPSR_AOVFL;
263 1.30.4.1 rmind }
264 1.30.4.1 rmind if ((fpsr & FPSR_UNFL) && (fpsr & FPSR_INEX2)) {
265 1.30.4.1 rmind fpsr |= FPSR_AUNFL;
266 1.30.4.1 rmind }
267 1.30.4.1 rmind if (fpsr & FPSR_DZ) {
268 1.30.4.1 rmind fpsr |= FPSR_ADZ;
269 1.30.4.1 rmind }
270 1.30.4.1 rmind if (fpsr & (FPSR_INEX1 | FPSR_INEX2 | FPSR_OVFL)) {
271 1.30.4.1 rmind fpsr |= FPSR_AINEX;
272 1.30.4.1 rmind }
273 1.1 gwr
274 1.30.4.1 rmind fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
275 1.1 gwr
276 1.30.4.1 rmind return (fpsr & fpcr & FPSR_EXCP) ? SIGFPE : 0;
277 1.3 briggs }
278 1.1 gwr
279 1.3 briggs /* update fpsr according to fp (= result of an fp op) */
280 1.3 briggs u_int
281 1.30 dsl fpu_upd_fpsr(struct fpemu *fe, struct fpn *fp)
282 1.3 briggs {
283 1.30.4.1 rmind u_int fpsr;
284 1.1 gwr
285 1.30.4.1 rmind DPRINTF(("%s: previous fpsr=%08x\n", __func__, fe->fe_fpsr));
286 1.30.4.1 rmind /* clear all condition code */
287 1.30.4.1 rmind fpsr = fe->fe_fpsr & ~FPSR_CCB;
288 1.30.4.1 rmind
289 1.30.4.1 rmind DPRINTF(("%s: result is a ", __func__));
290 1.30.4.1 rmind if (fp->fp_sign) {
291 1.30.4.1 rmind DPRINTF(("negative "));
292 1.30.4.1 rmind fpsr |= FPSR_NEG;
293 1.30.4.1 rmind } else {
294 1.30.4.1 rmind DPRINTF(("positive "));
295 1.30.4.1 rmind }
296 1.3 briggs
297 1.30.4.1 rmind switch (fp->fp_class) {
298 1.30.4.1 rmind case FPC_SNAN:
299 1.30.4.1 rmind DPRINTF(("signaling NAN\n"));
300 1.30.4.1 rmind fpsr |= (FPSR_NAN | FPSR_SNAN);
301 1.30.4.1 rmind break;
302 1.30.4.1 rmind case FPC_QNAN:
303 1.30.4.1 rmind DPRINTF(("quiet NAN\n"));
304 1.30.4.1 rmind fpsr |= FPSR_NAN;
305 1.30.4.1 rmind break;
306 1.30.4.1 rmind case FPC_ZERO:
307 1.30.4.1 rmind DPRINTF(("Zero\n"));
308 1.30.4.1 rmind fpsr |= FPSR_ZERO;
309 1.30.4.1 rmind break;
310 1.30.4.1 rmind case FPC_INF:
311 1.30.4.1 rmind DPRINTF(("Inf\n"));
312 1.30.4.1 rmind fpsr |= FPSR_INF;
313 1.30.4.1 rmind break;
314 1.30.4.1 rmind default:
315 1.30.4.1 rmind DPRINTF(("Number\n"));
316 1.30.4.1 rmind /* anything else is treated as if it is a number */
317 1.30.4.1 rmind break;
318 1.30.4.1 rmind }
319 1.1 gwr
320 1.30.4.1 rmind fe->fe_fpsr = fe->fe_fpframe->fpf_fpsr = fpsr;
321 1.1 gwr
322 1.30.4.1 rmind DPRINTF(("%s: new fpsr=%08x\n", __func__, fe->fe_fpframe->fpf_fpsr));
323 1.1 gwr
324 1.30.4.1 rmind return fpsr;
325 1.3 briggs }
326 1.1 gwr
327 1.3 briggs static int
328 1.30 dsl fpu_emul_fmovmcr(struct fpemu *fe, struct instruction *insn)
329 1.3 briggs {
330 1.30.4.1 rmind struct frame *frame = fe->fe_frame;
331 1.30.4.1 rmind struct fpframe *fpf = fe->fe_fpframe;
332 1.30.4.1 rmind int sig;
333 1.30.4.1 rmind int reglist;
334 1.30.4.1 rmind int fpu_to_mem;
335 1.30.4.1 rmind
336 1.30.4.1 rmind /* move to/from control registers */
337 1.30.4.1 rmind reglist = (insn->is_word1 & 0x1c00) >> 10;
338 1.30.4.1 rmind /* Bit 13 selects direction (FPU to/from Mem) */
339 1.30.4.1 rmind fpu_to_mem = insn->is_word1 & 0x2000;
340 1.1 gwr
341 1.30.4.1 rmind insn->is_datasize = 4;
342 1.30.4.1 rmind insn->is_advance = 4;
343 1.30.4.1 rmind sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
344 1.30.4.1 rmind if (sig)
345 1.30.4.1 rmind return sig;
346 1.30.4.1 rmind
347 1.30.4.1 rmind if (reglist != 1 && reglist != 2 && reglist != 4 &&
348 1.30.4.1 rmind (insn->is_ea.ea_flags & EA_DIRECT)) {
349 1.30.4.1 rmind /* attempted to copy more than one FPcr to CPU regs */
350 1.30.4.1 rmind DPRINTF(("%s: tried to copy too many FPcr\n", __func__));
351 1.30.4.1 rmind return SIGILL;
352 1.3 briggs }
353 1.1 gwr
354 1.30.4.1 rmind if (reglist & 4) {
355 1.30.4.1 rmind /* fpcr */
356 1.30.4.1 rmind if ((insn->is_ea.ea_flags & EA_DIRECT) &&
357 1.30.4.1 rmind insn->is_ea.ea_regnum >= 8 /* address reg */) {
358 1.30.4.1 rmind /* attempted to copy FPCR to An */
359 1.30.4.1 rmind DPRINTF(("%s: tried to copy FPCR from/to A%d\n",
360 1.30.4.1 rmind __func__, insn->is_ea.ea_regnum & 7));
361 1.30.4.1 rmind return SIGILL;
362 1.30.4.1 rmind }
363 1.30.4.1 rmind if (fpu_to_mem) {
364 1.30.4.1 rmind sig = fpu_store_ea(frame, insn, &insn->is_ea,
365 1.30.4.1 rmind (char *)&fpf->fpf_fpcr);
366 1.30.4.1 rmind } else {
367 1.30.4.1 rmind sig = fpu_load_ea(frame, insn, &insn->is_ea,
368 1.30.4.1 rmind (char *)&fpf->fpf_fpcr);
369 1.30.4.1 rmind }
370 1.3 briggs }
371 1.30.4.1 rmind if (sig)
372 1.30.4.1 rmind return sig;
373 1.30.4.1 rmind
374 1.30.4.1 rmind if (reglist & 2) {
375 1.30.4.1 rmind /* fpsr */
376 1.30.4.1 rmind if ((insn->is_ea.ea_flags & EA_DIRECT) &&
377 1.30.4.1 rmind insn->is_ea.ea_regnum >= 8 /* address reg */) {
378 1.30.4.1 rmind /* attempted to copy FPSR to An */
379 1.30.4.1 rmind DPRINTF(("%s: tried to copy FPSR from/to A%d\n",
380 1.30.4.1 rmind __func__, insn->is_ea.ea_regnum & 7));
381 1.30.4.1 rmind return SIGILL;
382 1.30.4.1 rmind }
383 1.30.4.1 rmind if (fpu_to_mem) {
384 1.30.4.1 rmind sig = fpu_store_ea(frame, insn, &insn->is_ea,
385 1.30.4.1 rmind (char *)&fpf->fpf_fpsr);
386 1.30.4.1 rmind } else {
387 1.30.4.1 rmind sig = fpu_load_ea(frame, insn, &insn->is_ea,
388 1.30.4.1 rmind (char *)&fpf->fpf_fpsr);
389 1.30.4.1 rmind }
390 1.3 briggs }
391 1.30.4.1 rmind if (sig)
392 1.30.4.1 rmind return sig;
393 1.30.4.1 rmind
394 1.30.4.1 rmind if (reglist & 1) {
395 1.30.4.1 rmind /* fpiar - can be moved to/from An */
396 1.30.4.1 rmind if (fpu_to_mem) {
397 1.30.4.1 rmind sig = fpu_store_ea(frame, insn, &insn->is_ea,
398 1.30.4.1 rmind (char *)&fpf->fpf_fpiar);
399 1.30.4.1 rmind } else {
400 1.30.4.1 rmind sig = fpu_load_ea(frame, insn, &insn->is_ea,
401 1.30.4.1 rmind (char *)&fpf->fpf_fpiar);
402 1.30.4.1 rmind }
403 1.3 briggs }
404 1.30.4.1 rmind return sig;
405 1.1 gwr }
406 1.1 gwr
407 1.1 gwr /*
408 1.3 briggs * type 0: fmovem
409 1.3 briggs * Separated out of fpu_emul_type0 for efficiency.
410 1.1 gwr * In this function, we know:
411 1.3 briggs * (opcode & 0x01C0) == 0
412 1.3 briggs * (word1 & 0x8000) == 0x8000
413 1.3 briggs *
414 1.3 briggs * No conversion or rounding is done by this instruction,
415 1.3 briggs * and the FPSR is not affected.
416 1.1 gwr */
417 1.3 briggs static int
418 1.30 dsl fpu_emul_fmovm(struct fpemu *fe, struct instruction *insn)
419 1.1 gwr {
420 1.30.4.1 rmind struct frame *frame = fe->fe_frame;
421 1.30.4.1 rmind struct fpframe *fpf = fe->fe_fpframe;
422 1.30.4.1 rmind int word1, sig;
423 1.30.4.1 rmind int reglist, regmask, regnum;
424 1.30.4.1 rmind int fpu_to_mem, order;
425 1.30.4.1 rmind int w1_post_incr;
426 1.30.4.1 rmind int *fpregs;
427 1.30.4.1 rmind
428 1.30.4.1 rmind insn->is_advance = 4;
429 1.30.4.1 rmind insn->is_datasize = 12;
430 1.30.4.1 rmind word1 = insn->is_word1;
431 1.30.4.1 rmind
432 1.30.4.1 rmind /* Bit 13 selects direction (FPU to/from Mem) */
433 1.30.4.1 rmind fpu_to_mem = word1 & 0x2000;
434 1.30.4.1 rmind
435 1.30.4.1 rmind /*
436 1.30.4.1 rmind * Bits 12,11 select register list mode:
437 1.30.4.1 rmind * 0,0: Static reg list, pre-decr.
438 1.30.4.1 rmind * 0,1: Dynamic reg list, pre-decr.
439 1.30.4.1 rmind * 1,0: Static reg list, post-incr.
440 1.30.4.1 rmind * 1,1: Dynamic reg list, post-incr
441 1.30.4.1 rmind */
442 1.30.4.1 rmind w1_post_incr = word1 & 0x1000;
443 1.30.4.1 rmind if (word1 & 0x0800) {
444 1.30.4.1 rmind /* dynamic reg list */
445 1.30.4.1 rmind reglist = frame->f_regs[(word1 & 0x70) >> 4];
446 1.30.4.1 rmind } else {
447 1.30.4.1 rmind reglist = word1;
448 1.30.4.1 rmind }
449 1.30.4.1 rmind reglist &= 0xFF;
450 1.30.4.1 rmind
451 1.30.4.1 rmind /* Get effective address. (modreg=opcode&077) */
452 1.30.4.1 rmind sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
453 1.30.4.1 rmind if (sig)
454 1.30.4.1 rmind return sig;
455 1.30.4.1 rmind
456 1.30.4.1 rmind /* Get address of soft coprocessor regs. */
457 1.30.4.1 rmind fpregs = &fpf->fpf_regs[0];
458 1.30.4.1 rmind
459 1.30.4.1 rmind if (insn->is_ea.ea_flags & EA_PREDECR) {
460 1.30.4.1 rmind regnum = 7;
461 1.30.4.1 rmind order = -1;
462 1.30.4.1 rmind } else {
463 1.30.4.1 rmind regnum = 0;
464 1.30.4.1 rmind order = 1;
465 1.30.4.1 rmind }
466 1.30.4.1 rmind
467 1.30.4.1 rmind regmask = 0x80;
468 1.30.4.1 rmind while ((0 <= regnum) && (regnum < 8)) {
469 1.30.4.1 rmind if (regmask & reglist) {
470 1.30.4.1 rmind if (fpu_to_mem) {
471 1.30.4.1 rmind sig = fpu_store_ea(frame, insn, &insn->is_ea,
472 1.30.4.1 rmind (char *)&fpregs[regnum * 3]);
473 1.30.4.1 rmind DPRINTF(("%s: FP%d (%08x,%08x,%08x) saved\n",
474 1.30.4.1 rmind __func__, regnum,
475 1.30.4.1 rmind fpregs[regnum * 3],
476 1.30.4.1 rmind fpregs[regnum * 3 + 1],
477 1.30.4.1 rmind fpregs[regnum * 3 + 2]));
478 1.30.4.1 rmind } else { /* mem to fpu */
479 1.30.4.1 rmind sig = fpu_load_ea(frame, insn, &insn->is_ea,
480 1.30.4.1 rmind (char *)&fpregs[regnum * 3]);
481 1.30.4.1 rmind DPRINTF(("%s: FP%d (%08x,%08x,%08x) loaded\n",
482 1.30.4.1 rmind __func__, regnum,
483 1.30.4.1 rmind fpregs[regnum * 3],
484 1.30.4.1 rmind fpregs[regnum * 3 + 1],
485 1.30.4.1 rmind fpregs[regnum * 3 + 2]));
486 1.30.4.1 rmind }
487 1.30.4.1 rmind if (sig)
488 1.30.4.1 rmind break;
489 1.30.4.1 rmind }
490 1.30.4.1 rmind regnum += order;
491 1.30.4.1 rmind regmask >>= 1;
492 1.3 briggs }
493 1.1 gwr
494 1.30.4.1 rmind return sig;
495 1.1 gwr }
496 1.1 gwr
497 1.3 briggs static struct fpn *
498 1.30 dsl fpu_cmp(struct fpemu *fe)
499 1.1 gwr {
500 1.30.4.1 rmind struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
501 1.1 gwr
502 1.30.4.1 rmind /* take care of special cases */
503 1.30.4.1 rmind if (x->fp_class < 0 || y->fp_class < 0) {
504 1.30.4.1 rmind /* if either of two is a SNAN, result is SNAN */
505 1.30.4.1 rmind x->fp_class =
506 1.30.4.1 rmind (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
507 1.30.4.1 rmind } else if (x->fp_class == FPC_INF) {
508 1.30.4.1 rmind if (y->fp_class == FPC_INF) {
509 1.30.4.1 rmind /* both infinities */
510 1.30.4.1 rmind if (x->fp_sign == y->fp_sign) {
511 1.30.4.1 rmind /* return a signed zero */
512 1.30.4.1 rmind x->fp_class = FPC_ZERO;
513 1.30.4.1 rmind } else {
514 1.30.4.1 rmind /* return a faked number w/x's sign */
515 1.30.4.1 rmind x->fp_class = FPC_NUM;
516 1.30.4.1 rmind x->fp_exp = 16383;
517 1.30.4.1 rmind x->fp_mant[0] = FP_1;
518 1.30.4.1 rmind }
519 1.30.4.1 rmind } else {
520 1.30.4.1 rmind /* y is a number */
521 1.30.4.1 rmind /* return a forged number w/x's sign */
522 1.30.4.1 rmind x->fp_class = FPC_NUM;
523 1.30.4.1 rmind x->fp_exp = 16383;
524 1.30.4.1 rmind x->fp_mant[0] = FP_1;
525 1.30.4.1 rmind }
526 1.30.4.1 rmind } else if (y->fp_class == FPC_INF) {
527 1.30.4.1 rmind /* x is a Num but y is an Inf */
528 1.30.4.1 rmind /* return a forged number w/y's sign inverted */
529 1.30.4.1 rmind x->fp_class = FPC_NUM;
530 1.30.4.1 rmind x->fp_sign = !y->fp_sign;
531 1.3 briggs x->fp_exp = 16383;
532 1.3 briggs x->fp_mant[0] = FP_1;
533 1.3 briggs } else {
534 1.30.4.1 rmind /*
535 1.30.4.1 rmind * x and y are both numbers or zeros,
536 1.30.4.1 rmind * or pair of a number and a zero
537 1.30.4.1 rmind */
538 1.30.4.1 rmind y->fp_sign = !y->fp_sign;
539 1.30.4.1 rmind x = fpu_add(fe); /* (x - y) */
540 1.30.4.1 rmind /*
541 1.30.4.1 rmind * FCMP does not set Inf bit in CC, so return a forged number
542 1.30.4.1 rmind * (value doesn't matter) if Inf is the result of fsub.
543 1.30.4.1 rmind */
544 1.30.4.1 rmind if (x->fp_class == FPC_INF) {
545 1.30.4.1 rmind x->fp_class = FPC_NUM;
546 1.30.4.1 rmind x->fp_exp = 16383;
547 1.30.4.1 rmind x->fp_mant[0] = FP_1;
548 1.30.4.1 rmind }
549 1.1 gwr }
550 1.30.4.1 rmind return x;
551 1.1 gwr }
552 1.1 gwr
553 1.1 gwr /*
554 1.3 briggs * arithmetic oprations
555 1.1 gwr */
556 1.3 briggs static int
557 1.30 dsl fpu_emul_arith(struct fpemu *fe, struct instruction *insn)
558 1.1 gwr {
559 1.30.4.1 rmind struct frame *frame = fe->fe_frame;
560 1.30.4.1 rmind u_int *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
561 1.30.4.1 rmind struct fpn *res;
562 1.30.4.1 rmind int word1, sig = 0;
563 1.30.4.1 rmind int regnum, format;
564 1.30.4.1 rmind int discard_result = 0;
565 1.30.4.1 rmind u_int buf[3];
566 1.30.4.1 rmind #ifdef DEBUG_FPE
567 1.30.4.1 rmind int flags;
568 1.30.4.1 rmind char regname;
569 1.21 briggs #endif
570 1.16 is
571 1.30.4.1 rmind fe->fe_fpsr &= ~FPSR_EXCP;
572 1.3 briggs
573 1.30.4.1 rmind DUMP_INSN(insn);
574 1.3 briggs
575 1.30.4.1 rmind DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
576 1.30.4.1 rmind fe->fe_fpsr, fe->fe_fpcr));
577 1.3 briggs
578 1.30.4.1 rmind word1 = insn->is_word1;
579 1.30.4.1 rmind format = (word1 >> 10) & 7;
580 1.30.4.1 rmind regnum = (word1 >> 7) & 7;
581 1.30.4.1 rmind
582 1.30.4.1 rmind /* fetch a source operand : may not be used */
583 1.30.4.1 rmind DPRINTF(("%s: dst/src FP%d=%08x,%08x,%08x\n", __func__,
584 1.30.4.1 rmind regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
585 1.30.4.1 rmind fpregs[regnum * 3 + 2]));
586 1.21 briggs
587 1.30.4.1 rmind fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
588 1.3 briggs
589 1.30.4.1 rmind DUMP_INSN(insn);
590 1.3 briggs
591 1.30.4.1 rmind /* get the other operand which is always the source */
592 1.30.4.1 rmind if ((word1 & 0x4000) == 0) {
593 1.30.4.1 rmind DPRINTF(("%s: FP%d op FP%d => FP%d\n", __func__,
594 1.30.4.1 rmind format, regnum, regnum));
595 1.30.4.1 rmind DPRINTF(("%s: src opr FP%d=%08x,%08x,%08x\n", __func__,
596 1.30.4.1 rmind format, fpregs[format * 3], fpregs[format * 3 + 1],
597 1.30.4.1 rmind fpregs[format * 3 + 2]));
598 1.30.4.1 rmind fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
599 1.3 briggs } else {
600 1.30.4.1 rmind /* the operand is in memory */
601 1.30.4.1 rmind if (format == FTYPE_DBL) {
602 1.30.4.1 rmind insn->is_datasize = 8;
603 1.30.4.1 rmind } else if (format == FTYPE_SNG || format == FTYPE_LNG) {
604 1.30.4.1 rmind insn->is_datasize = 4;
605 1.30.4.1 rmind } else if (format == FTYPE_WRD) {
606 1.30.4.1 rmind insn->is_datasize = 2;
607 1.30.4.1 rmind } else if (format == FTYPE_BYT) {
608 1.30.4.1 rmind insn->is_datasize = 1;
609 1.30.4.1 rmind } else if (format == FTYPE_EXT) {
610 1.30.4.1 rmind insn->is_datasize = 12;
611 1.30.4.1 rmind } else {
612 1.30.4.1 rmind /* invalid or unsupported operand format */
613 1.30.4.1 rmind sig = SIGFPE;
614 1.30.4.1 rmind return sig;
615 1.30.4.1 rmind }
616 1.1 gwr
617 1.30.4.1 rmind /* Get effective address. (modreg=opcode&077) */
618 1.30.4.1 rmind sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
619 1.30.4.1 rmind if (sig) {
620 1.30.4.1 rmind DPRINTF(("%s: error in fpu_decode_ea\n", __func__));
621 1.30.4.1 rmind return sig;
622 1.30.4.1 rmind }
623 1.30.4.1 rmind
624 1.30.4.1 rmind DUMP_INSN(insn);
625 1.30.4.1 rmind
626 1.30.4.1 rmind #ifdef DEBUG_FPE
627 1.30.4.1 rmind printf("%s: addr mode = ", __func__);
628 1.30.4.1 rmind flags = insn->is_ea.ea_flags;
629 1.30.4.1 rmind regname = (insn->is_ea.ea_regnum & 8) ? 'a' : 'd';
630 1.30.4.1 rmind
631 1.30.4.1 rmind if (flags & EA_DIRECT) {
632 1.30.4.1 rmind printf("%c%d\n",
633 1.30.4.1 rmind regname, insn->is_ea.ea_regnum & 7);
634 1.30.4.1 rmind } else if (flags & EA_PC_REL) {
635 1.30.4.1 rmind if (flags & EA_OFFSET) {
636 1.30.4.1 rmind printf("pc@(%d)\n", insn->is_ea.ea_offset);
637 1.30.4.1 rmind } else if (flags & EA_INDEXED) {
638 1.30.4.1 rmind printf("pc@(...)\n");
639 1.30.4.1 rmind }
640 1.30.4.1 rmind } else if (flags & EA_PREDECR) {
641 1.30.4.1 rmind printf("%c%d@-\n",
642 1.30.4.1 rmind regname, insn->is_ea.ea_regnum & 7);
643 1.30.4.1 rmind } else if (flags & EA_POSTINCR) {
644 1.30.4.1 rmind printf("%c%d@+\n", regname, insn->is_ea.ea_regnum & 7);
645 1.30.4.1 rmind } else if (flags & EA_OFFSET) {
646 1.30.4.1 rmind printf("%c%d@(%d)\n", regname,
647 1.30.4.1 rmind insn->is_ea.ea_regnum & 7,
648 1.30.4.1 rmind insn->is_ea.ea_offset);
649 1.30.4.1 rmind } else if (flags & EA_INDEXED) {
650 1.30.4.1 rmind printf("%c%d@(...)\n", regname,
651 1.30.4.1 rmind insn->is_ea.ea_regnum & 7);
652 1.30.4.1 rmind } else if (flags & EA_ABS) {
653 1.30.4.1 rmind printf("0x%08x\n", insn->is_ea.ea_absaddr);
654 1.30.4.1 rmind } else if (flags & EA_IMMED) {
655 1.30.4.1 rmind printf("#0x%08x,%08x,%08x\n", insn->is_ea.ea_immed[0],
656 1.30.4.1 rmind insn->is_ea.ea_immed[1], insn->is_ea.ea_immed[2]);
657 1.30.4.1 rmind } else {
658 1.30.4.1 rmind printf("%c%d@\n", regname, insn->is_ea.ea_regnum & 7);
659 1.30.4.1 rmind }
660 1.30.4.1 rmind #endif /* DEBUG_FPE */
661 1.30.4.1 rmind
662 1.30.4.1 rmind fpu_load_ea(frame, insn, &insn->is_ea, (char*)buf);
663 1.30.4.1 rmind if (format == FTYPE_WRD) {
664 1.30.4.1 rmind /* sign-extend */
665 1.30.4.1 rmind buf[0] &= 0xffff;
666 1.30.4.1 rmind if (buf[0] & 0x8000)
667 1.30.4.1 rmind buf[0] |= 0xffff0000;
668 1.30.4.1 rmind format = FTYPE_LNG;
669 1.30.4.1 rmind } else if (format == FTYPE_BYT) {
670 1.30.4.1 rmind /* sign-extend */
671 1.30.4.1 rmind buf[0] &= 0xff;
672 1.30.4.1 rmind if (buf[0] & 0x80)
673 1.30.4.1 rmind buf[0] |= 0xffffff00;
674 1.30.4.1 rmind format = FTYPE_LNG;
675 1.30.4.1 rmind }
676 1.30.4.1 rmind DPRINTF(("%s: src = %08x %08x %08x, siz = %d\n", __func__,
677 1.30.4.1 rmind buf[0], buf[1], buf[2], insn->is_datasize));
678 1.30.4.1 rmind fpu_explode(fe, &fe->fe_f2, format, buf);
679 1.3 briggs }
680 1.1 gwr
681 1.3 briggs DUMP_INSN(insn);
682 1.1 gwr
683 1.30.4.1 rmind /*
684 1.30.4.1 rmind * An arithmetic instruction emulate function has a prototype of
685 1.30.4.1 rmind * struct fpn *fpu_op(struct fpemu *);
686 1.30.4.1 rmind *
687 1.30.4.1 rmind * 1) If the instruction is monadic, then fpu_op() must use
688 1.30.4.1 rmind * fe->fe_f2 as its operand, and return a pointer to the
689 1.30.4.1 rmind * result.
690 1.30.4.1 rmind *
691 1.30.4.1 rmind * 2) If the instruction is diadic, then fpu_op() must use
692 1.30.4.1 rmind * fe->fe_f1 and fe->fe_f2 as its two operands, and return a
693 1.30.4.1 rmind * pointer to the result.
694 1.30.4.1 rmind *
695 1.30.4.1 rmind */
696 1.30.4.1 rmind res = NULL;
697 1.30.4.1 rmind switch (word1 & 0x7f) {
698 1.30.4.1 rmind case 0x00: /* fmove */
699 1.30.4.1 rmind res = &fe->fe_f2;
700 1.30.4.1 rmind break;
701 1.3 briggs
702 1.30.4.1 rmind case 0x01: /* fint */
703 1.30.4.1 rmind res = fpu_int(fe);
704 1.30.4.1 rmind break;
705 1.3 briggs
706 1.30.4.1 rmind case 0x02: /* fsinh */
707 1.30.4.1 rmind res = fpu_sinh(fe);
708 1.30.4.1 rmind break;
709 1.1 gwr
710 1.30.4.1 rmind case 0x03: /* fintrz */
711 1.30.4.1 rmind res = fpu_intrz(fe);
712 1.30.4.1 rmind break;
713 1.1 gwr
714 1.30.4.1 rmind case 0x04: /* fsqrt */
715 1.30.4.1 rmind res = fpu_sqrt(fe);
716 1.30.4.1 rmind break;
717 1.1 gwr
718 1.30.4.1 rmind case 0x06: /* flognp1 */
719 1.30.4.1 rmind res = fpu_lognp1(fe);
720 1.30.4.1 rmind break;
721 1.30.4.1 rmind
722 1.30.4.1 rmind case 0x08: /* fetoxm1 */
723 1.30.4.1 rmind res = fpu_etoxm1(fe);
724 1.30.4.1 rmind break;
725 1.30.4.1 rmind
726 1.30.4.1 rmind case 0x09: /* ftanh */
727 1.30.4.1 rmind res = fpu_tanh(fe);
728 1.30.4.1 rmind break;
729 1.30.4.1 rmind
730 1.30.4.1 rmind case 0x0A: /* fatan */
731 1.30.4.1 rmind res = fpu_atan(fe);
732 1.30.4.1 rmind break;
733 1.30.4.1 rmind
734 1.30.4.1 rmind case 0x0C: /* fasin */
735 1.30.4.1 rmind res = fpu_asin(fe);
736 1.30.4.1 rmind break;
737 1.30.4.1 rmind
738 1.30.4.1 rmind case 0x0D: /* fatanh */
739 1.30.4.1 rmind res = fpu_atanh(fe);
740 1.30.4.1 rmind break;
741 1.30.4.1 rmind
742 1.30.4.1 rmind case 0x0E: /* fsin */
743 1.30.4.1 rmind res = fpu_sin(fe);
744 1.30.4.1 rmind break;
745 1.30.4.1 rmind
746 1.30.4.1 rmind case 0x0F: /* ftan */
747 1.30.4.1 rmind res = fpu_tan(fe);
748 1.30.4.1 rmind break;
749 1.30.4.1 rmind
750 1.30.4.1 rmind case 0x10: /* fetox */
751 1.30.4.1 rmind res = fpu_etox(fe);
752 1.30.4.1 rmind break;
753 1.30.4.1 rmind
754 1.30.4.1 rmind case 0x11: /* ftwotox */
755 1.30.4.1 rmind res = fpu_twotox(fe);
756 1.30.4.1 rmind break;
757 1.30.4.1 rmind
758 1.30.4.1 rmind case 0x12: /* ftentox */
759 1.30.4.1 rmind res = fpu_tentox(fe);
760 1.30.4.1 rmind break;
761 1.30.4.1 rmind
762 1.30.4.1 rmind case 0x14: /* flogn */
763 1.30.4.1 rmind res = fpu_logn(fe);
764 1.30.4.1 rmind break;
765 1.30.4.1 rmind
766 1.30.4.1 rmind case 0x15: /* flog10 */
767 1.30.4.1 rmind res = fpu_log10(fe);
768 1.30.4.1 rmind break;
769 1.30.4.1 rmind
770 1.30.4.1 rmind case 0x16: /* flog2 */
771 1.30.4.1 rmind res = fpu_log2(fe);
772 1.30.4.1 rmind break;
773 1.30.4.1 rmind
774 1.30.4.1 rmind case 0x18: /* fabs */
775 1.30.4.1 rmind fe->fe_f2.fp_sign = 0;
776 1.30.4.1 rmind res = &fe->fe_f2;
777 1.30.4.1 rmind break;
778 1.30.4.1 rmind
779 1.30.4.1 rmind case 0x19: /* fcosh */
780 1.30.4.1 rmind res = fpu_cosh(fe);
781 1.30.4.1 rmind break;
782 1.30.4.1 rmind
783 1.30.4.1 rmind case 0x1A: /* fneg */
784 1.30.4.1 rmind fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
785 1.30.4.1 rmind res = &fe->fe_f2;
786 1.30.4.1 rmind break;
787 1.30.4.1 rmind
788 1.30.4.1 rmind case 0x1C: /* facos */
789 1.30.4.1 rmind res = fpu_acos(fe);
790 1.30.4.1 rmind break;
791 1.30.4.1 rmind
792 1.30.4.1 rmind case 0x1D: /* fcos */
793 1.30.4.1 rmind res = fpu_cos(fe);
794 1.30.4.1 rmind break;
795 1.30.4.1 rmind
796 1.30.4.1 rmind case 0x1E: /* fgetexp */
797 1.30.4.1 rmind res = fpu_getexp(fe);
798 1.30.4.1 rmind break;
799 1.30.4.1 rmind
800 1.30.4.1 rmind case 0x1F: /* fgetman */
801 1.30.4.1 rmind res = fpu_getman(fe);
802 1.30.4.1 rmind break;
803 1.30.4.1 rmind
804 1.30.4.1 rmind case 0x20: /* fdiv */
805 1.30.4.1 rmind case 0x24: /* fsgldiv: cheating - better than nothing */
806 1.30.4.1 rmind res = fpu_div(fe);
807 1.30.4.1 rmind break;
808 1.30.4.1 rmind
809 1.30.4.1 rmind case 0x21: /* fmod */
810 1.30.4.1 rmind res = fpu_mod(fe);
811 1.30.4.1 rmind break;
812 1.30.4.1 rmind
813 1.30.4.1 rmind case 0x28: /* fsub */
814 1.30.4.1 rmind fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
815 1.30.4.1 rmind /* FALLTHROUGH */
816 1.30.4.1 rmind case 0x22: /* fadd */
817 1.30.4.1 rmind res = fpu_add(fe);
818 1.30.4.1 rmind break;
819 1.30.4.1 rmind
820 1.30.4.1 rmind case 0x23: /* fmul */
821 1.30.4.1 rmind case 0x27: /* fsglmul: cheating - better than nothing */
822 1.30.4.1 rmind res = fpu_mul(fe);
823 1.30.4.1 rmind break;
824 1.30.4.1 rmind
825 1.30.4.1 rmind case 0x25: /* frem */
826 1.30.4.1 rmind res = fpu_rem(fe);
827 1.30.4.1 rmind break;
828 1.30.4.1 rmind
829 1.30.4.1 rmind case 0x26:
830 1.30.4.1 rmind /* fscale is handled by a separate function */
831 1.30.4.1 rmind break;
832 1.30.4.1 rmind
833 1.30.4.1 rmind case 0x30:
834 1.30.4.1 rmind case 0x31:
835 1.30.4.1 rmind case 0x32:
836 1.30.4.1 rmind case 0x33:
837 1.30.4.1 rmind case 0x34:
838 1.30.4.1 rmind case 0x35:
839 1.30.4.1 rmind case 0x36:
840 1.30.4.1 rmind case 0x37: /* fsincos */
841 1.30.4.1 rmind res = fpu_sincos(fe, word1 & 7);
842 1.30.4.1 rmind break;
843 1.30.4.1 rmind
844 1.30.4.1 rmind case 0x38: /* fcmp */
845 1.30.4.1 rmind res = fpu_cmp(fe);
846 1.30.4.1 rmind discard_result = 1;
847 1.30.4.1 rmind break;
848 1.30.4.1 rmind
849 1.30.4.1 rmind case 0x3A: /* ftst */
850 1.30.4.1 rmind res = &fe->fe_f2;
851 1.30.4.1 rmind discard_result = 1;
852 1.30.4.1 rmind break;
853 1.30.4.1 rmind
854 1.30.4.1 rmind default: /* possibly 040/060 instructions */
855 1.30.4.1 rmind DPRINTF(("%s: bad opcode=0x%x, word1=0x%x\n", __func__,
856 1.30.4.1 rmind insn->is_opcode, insn->is_word1));
857 1.30.4.1 rmind sig = SIGILL;
858 1.30.4.1 rmind }
859 1.30.4.1 rmind
860 1.30.4.1 rmind /* for sanity */
861 1.30.4.1 rmind if (res == NULL)
862 1.30.4.1 rmind sig = SIGILL;
863 1.3 briggs
864 1.30.4.1 rmind if (sig == 0) {
865 1.30.4.1 rmind if (!discard_result)
866 1.30.4.1 rmind fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
867 1.30.4.1 rmind
868 1.30.4.1 rmind /* update fpsr according to the result of operation */
869 1.30.4.1 rmind fpu_upd_fpsr(fe, res);
870 1.30.4.1 rmind #ifdef DEBUG_FPE
871 1.30.4.1 rmind if (!discard_result) {
872 1.30.4.1 rmind printf("%s: %08x,%08x,%08x stored in FP%d\n", __func__,
873 1.30.4.1 rmind fpregs[regnum * 3],
874 1.30.4.1 rmind fpregs[regnum * 3 + 1],
875 1.30.4.1 rmind fpregs[regnum * 3 + 2],
876 1.30.4.1 rmind regnum);
877 1.30.4.1 rmind } else {
878 1.30.4.1 rmind static const char *class_name[] =
879 1.30.4.1 rmind { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
880 1.30.4.1 rmind printf("%s: result(%s,%c,%d,%08x,%08x,%08x) "
881 1.30.4.1 rmind "discarded\n", __func__,
882 1.30.4.1 rmind class_name[res->fp_class + 2],
883 1.30.4.1 rmind res->fp_sign ? '-' : '+', res->fp_exp,
884 1.30.4.1 rmind res->fp_mant[0], res->fp_mant[1],
885 1.30.4.1 rmind res->fp_mant[2]);
886 1.30.4.1 rmind }
887 1.21 briggs #endif
888 1.30.4.1 rmind } else {
889 1.30.4.1 rmind DPRINTF(("%s: received signal %d\n", __func__, sig));
890 1.30.4.1 rmind }
891 1.30.4.1 rmind
892 1.30.4.1 rmind DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
893 1.30.4.1 rmind fe->fe_fpsr, fe->fe_fpcr));
894 1.1 gwr
895 1.30.4.1 rmind DUMP_INSN(insn);
896 1.1 gwr
897 1.30.4.1 rmind return sig;
898 1.1 gwr }
899 1.1 gwr
900 1.30.4.1 rmind /*
901 1.30.4.1 rmind * test condition code according to the predicate in the opcode.
902 1.3 briggs * returns -1 when the predicate evaluates to true, 0 when false.
903 1.3 briggs * signal numbers are returned when an error is detected.
904 1.1 gwr */
905 1.3 briggs static int
906 1.30 dsl test_cc(struct fpemu *fe, int pred)
907 1.1 gwr {
908 1.30.4.1 rmind int result, sig_bsun, invert;
909 1.30.4.1 rmind int fpsr;
910 1.1 gwr
911 1.30.4.1 rmind fpsr = fe->fe_fpsr;
912 1.30.4.1 rmind invert = 0;
913 1.30.4.1 rmind fpsr &= ~FPSR_EXCP; /* clear all exceptions */
914 1.30.4.1 rmind DPRINTF(("%s: fpsr=0x%08x\n", __func__, fpsr));
915 1.30.4.1 rmind pred &= 0x3f; /* lowest 6 bits */
916 1.3 briggs
917 1.30.4.1 rmind DPRINTF(("%s: ", __func__));
918 1.1 gwr
919 1.30.4.1 rmind if (pred >= 0x20) {
920 1.30.4.1 rmind DPRINTF(("Illegal condition code\n"));
921 1.30.4.1 rmind return SIGILL;
922 1.30.4.1 rmind } else if (pred & 0x10) {
923 1.30.4.1 rmind /* IEEE nonaware tests */
924 1.30.4.1 rmind sig_bsun = 1;
925 1.30.4.1 rmind pred &= 0x0f; /* lower 4 bits */
926 1.30.4.1 rmind } else {
927 1.30.4.1 rmind /* IEEE aware tests */
928 1.30.4.1 rmind DPRINTF(("IEEE "));
929 1.30.4.1 rmind sig_bsun = 0;
930 1.30.4.1 rmind }
931 1.1 gwr
932 1.30.4.1 rmind if (pred & 0x08) {
933 1.30.4.1 rmind DPRINTF(("Not "));
934 1.30.4.1 rmind /* predicate is "NOT ..." */
935 1.30.4.1 rmind pred ^= 0xf; /* invert */
936 1.30.4.1 rmind invert = -1;
937 1.30.4.1 rmind }
938 1.30.4.1 rmind switch (pred) {
939 1.30.4.1 rmind case 0: /* (Signaling) False */
940 1.30.4.1 rmind DPRINTF(("False"));
941 1.30.4.1 rmind result = 0;
942 1.30.4.1 rmind break;
943 1.30.4.1 rmind case 1: /* (Signaling) Equal */
944 1.30.4.1 rmind DPRINTF(("Equal"));
945 1.30.4.1 rmind result = -((fpsr & FPSR_ZERO) == FPSR_ZERO);
946 1.30.4.1 rmind break;
947 1.30.4.1 rmind case 2: /* Greater Than */
948 1.30.4.1 rmind DPRINTF(("GT"));
949 1.30.4.1 rmind result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == 0);
950 1.30.4.1 rmind break;
951 1.30.4.1 rmind case 3: /* Greater or Equal */
952 1.30.4.1 rmind DPRINTF(("GE"));
953 1.30.4.1 rmind result = -((fpsr & FPSR_ZERO) ||
954 1.30.4.1 rmind (fpsr & (FPSR_NAN|FPSR_NEG)) == 0);
955 1.30.4.1 rmind break;
956 1.30.4.1 rmind case 4: /* Less Than */
957 1.30.4.1 rmind DPRINTF(("LT"));
958 1.30.4.1 rmind result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == FPSR_NEG);
959 1.30.4.1 rmind break;
960 1.30.4.1 rmind case 5: /* Less or Equal */
961 1.30.4.1 rmind DPRINTF(("LE"));
962 1.30.4.1 rmind result = -((fpsr & FPSR_ZERO) ||
963 1.30.4.1 rmind ((fpsr & (FPSR_NAN|FPSR_NEG)) == FPSR_NEG));
964 1.30.4.1 rmind break;
965 1.30.4.1 rmind case 6: /* Greater or Less than */
966 1.30.4.1 rmind DPRINTF(("GLT"));
967 1.30.4.1 rmind result = -((fpsr & (FPSR_NAN|FPSR_ZERO)) == 0);
968 1.30.4.1 rmind break;
969 1.30.4.1 rmind case 7: /* Greater, Less or Equal */
970 1.30.4.1 rmind DPRINTF(("GLE"));
971 1.30.4.1 rmind result = -((fpsr & FPSR_NAN) == 0);
972 1.30.4.1 rmind break;
973 1.30.4.1 rmind default:
974 1.30.4.1 rmind /* invalid predicate */
975 1.30.4.1 rmind DPRINTF(("Invalid predicate\n"));
976 1.30.4.1 rmind return SIGILL;
977 1.30.4.1 rmind }
978 1.30.4.1 rmind /* if the predicate is "NOT ...", then invert the result */
979 1.30.4.1 rmind result ^= invert;
980 1.30.4.1 rmind DPRINTF(("=> %s (%d)\n", result ? "true" : "false", result));
981 1.30.4.1 rmind /* if it's an IEEE unaware test and NAN is set, BSUN is set */
982 1.30.4.1 rmind if (sig_bsun && (fpsr & FPSR_NAN)) {
983 1.30.4.1 rmind fpsr |= FPSR_BSUN;
984 1.30.4.1 rmind }
985 1.1 gwr
986 1.30.4.1 rmind /* put fpsr back */
987 1.30.4.1 rmind fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
988 1.1 gwr
989 1.30.4.1 rmind return result;
990 1.1 gwr }
991 1.1 gwr
992 1.1 gwr /*
993 1.3 briggs * type 1: fdbcc, fscc, ftrapcc
994 1.3 briggs * In this function, we know:
995 1.3 briggs * (opcode & 0x01C0) == 0x0040
996 1.1 gwr */
997 1.3 briggs static int
998 1.30 dsl fpu_emul_type1(struct fpemu *fe, struct instruction *insn)
999 1.1 gwr {
1000 1.30.4.1 rmind struct frame *frame = fe->fe_frame;
1001 1.30.4.1 rmind int advance, sig, branch, displ;
1002 1.3 briggs
1003 1.30.4.1 rmind branch = test_cc(fe, insn->is_word1);
1004 1.30.4.1 rmind fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1005 1.3 briggs
1006 1.30.4.1 rmind insn->is_advance = 4;
1007 1.30.4.1 rmind sig = 0;
1008 1.3 briggs
1009 1.30.4.1 rmind switch (insn->is_opcode & 070) {
1010 1.30.4.1 rmind case 010: /* fdbcc */
1011 1.30.4.1 rmind if (branch == -1) {
1012 1.30.4.1 rmind /* advance */
1013 1.30.4.1 rmind insn->is_advance = 6;
1014 1.30.4.1 rmind } else if (!branch) {
1015 1.30.4.1 rmind /* decrement Dn and if (Dn != -1) branch */
1016 1.30.4.1 rmind uint16_t count = frame->f_regs[insn->is_opcode & 7];
1017 1.30.4.1 rmind
1018 1.30.4.1 rmind if (count-- != 0) {
1019 1.30.4.1 rmind displ = fusword((void *)(insn->is_pc +
1020 1.30.4.1 rmind insn->is_advance));
1021 1.30.4.1 rmind if (displ < 0) {
1022 1.30.4.1 rmind DPRINTF(("%s: fault reading "
1023 1.30.4.1 rmind "displacement\n", __func__));
1024 1.30.4.1 rmind return SIGSEGV;
1025 1.30.4.1 rmind }
1026 1.30.4.1 rmind /* sign-extend the displacement */
1027 1.30.4.1 rmind displ &= 0xffff;
1028 1.30.4.1 rmind if (displ & 0x8000) {
1029 1.30.4.1 rmind displ |= 0xffff0000;
1030 1.30.4.1 rmind }
1031 1.30.4.1 rmind insn->is_advance += displ;
1032 1.30.4.1 rmind #if 0 /* XXX */
1033 1.30.4.1 rmind insn->is_nextpc = insn->is_pc +
1034 1.30.4.1 rmind insn->is_advance;
1035 1.30.4.1 rmind #endif
1036 1.30.4.1 rmind } else {
1037 1.30.4.1 rmind insn->is_advance = 6;
1038 1.30.4.1 rmind }
1039 1.30.4.1 rmind /* write it back */
1040 1.30.4.1 rmind frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
1041 1.30.4.1 rmind frame->f_regs[insn->is_opcode & 7] |= (uint32_t)count;
1042 1.30.4.1 rmind } else { /* got a signal */
1043 1.30.4.1 rmind sig = SIGFPE;
1044 1.3 briggs }
1045 1.3 briggs break;
1046 1.1 gwr
1047 1.30.4.1 rmind case 070: /* ftrapcc or fscc */
1048 1.30.4.1 rmind advance = 4;
1049 1.30.4.1 rmind if ((insn->is_opcode & 07) >= 2) {
1050 1.30.4.1 rmind switch (insn->is_opcode & 07) {
1051 1.30.4.1 rmind case 3: /* long opr */
1052 1.30.4.1 rmind advance += 2;
1053 1.30.4.1 rmind case 2: /* word opr */
1054 1.30.4.1 rmind advance += 2;
1055 1.30.4.1 rmind case 4: /* no opr */
1056 1.30.4.1 rmind break;
1057 1.30.4.1 rmind default:
1058 1.30.4.1 rmind return SIGILL;
1059 1.30.4.1 rmind break;
1060 1.30.4.1 rmind }
1061 1.30.4.1 rmind
1062 1.30.4.1 rmind if (branch == 0) {
1063 1.30.4.1 rmind /* no trap */
1064 1.30.4.1 rmind insn->is_advance = advance;
1065 1.30.4.1 rmind sig = 0;
1066 1.30.4.1 rmind } else {
1067 1.30.4.1 rmind /* trap */
1068 1.30.4.1 rmind sig = SIGFPE;
1069 1.30.4.1 rmind }
1070 1.30.4.1 rmind break;
1071 1.30.4.1 rmind }
1072 1.3 briggs
1073 1.30.4.1 rmind /* FALLTHROUGH */
1074 1.30.4.1 rmind default: /* fscc */
1075 1.30.4.1 rmind insn->is_advance = 4;
1076 1.30.4.1 rmind insn->is_datasize = 1; /* always byte */
1077 1.30.4.1 rmind sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
1078 1.30.4.1 rmind if (sig) {
1079 1.30.4.1 rmind break;
1080 1.30.4.1 rmind }
1081 1.30.4.1 rmind if (branch == -1 || branch == 0) {
1082 1.30.4.1 rmind /* set result */
1083 1.30.4.1 rmind sig = fpu_store_ea(frame, insn, &insn->is_ea,
1084 1.30.4.1 rmind (char *)&branch);
1085 1.30.4.1 rmind } else {
1086 1.30.4.1 rmind /* got an exception */
1087 1.30.4.1 rmind sig = branch;
1088 1.30.4.1 rmind }
1089 1.30.4.1 rmind break;
1090 1.3 briggs }
1091 1.30.4.1 rmind return sig;
1092 1.3 briggs }
1093 1.1 gwr
1094 1.3 briggs /*
1095 1.3 briggs * Type 2 or 3: fbcc (also fnop)
1096 1.3 briggs * In this function, we know:
1097 1.3 briggs * (opcode & 0x0180) == 0x0080
1098 1.3 briggs */
1099 1.3 briggs static int
1100 1.30 dsl fpu_emul_brcc(struct fpemu *fe, struct instruction *insn)
1101 1.3 briggs {
1102 1.30.4.1 rmind int displ, word2;
1103 1.30.4.1 rmind int sig;
1104 1.3 briggs
1105 1.30.4.1 rmind /*
1106 1.30.4.1 rmind * Get branch displacement.
1107 1.30.4.1 rmind */
1108 1.30.4.1 rmind insn->is_advance = 4;
1109 1.30.4.1 rmind displ = insn->is_word1;
1110 1.30.4.1 rmind
1111 1.30.4.1 rmind if (insn->is_opcode & 0x40) {
1112 1.30.4.1 rmind word2 = fusword((void *)(insn->is_pc + insn->is_advance));
1113 1.30.4.1 rmind if (word2 < 0) {
1114 1.30.4.1 rmind DPRINTF(("%s: fault reading word2\n", __func__));
1115 1.30.4.1 rmind return SIGSEGV;
1116 1.30.4.1 rmind }
1117 1.30.4.1 rmind displ <<= 16;
1118 1.30.4.1 rmind displ |= word2;
1119 1.30.4.1 rmind insn->is_advance += 2;
1120 1.30.4.1 rmind } else {
1121 1.30.4.1 rmind /* displacement is word sized */
1122 1.30.4.1 rmind if (displ & 0x8000)
1123 1.30.4.1 rmind displ |= 0xFFFF0000;
1124 1.1 gwr }
1125 1.30.4.1 rmind
1126 1.30.4.1 rmind /* XXX: If CC, insn->is_pc += displ */
1127 1.30.4.1 rmind sig = test_cc(fe, insn->is_opcode);
1128 1.30.4.1 rmind fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
1129 1.30.4.1 rmind
1130 1.30.4.1 rmind if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
1131 1.30.4.1 rmind return SIGFPE; /* caught an exception */
1132 1.30.4.1 rmind }
1133 1.30.4.1 rmind if (sig == -1) {
1134 1.30.4.1 rmind /*
1135 1.30.4.1 rmind * branch does take place; 2 is the offset to the 1st disp word
1136 1.30.4.1 rmind */
1137 1.30.4.1 rmind insn->is_advance = displ + 2;
1138 1.30.4.1 rmind #if 0 /* XXX */
1139 1.30.4.1 rmind insn->is_nextpc = insn->is_pc + insn->is_advance;
1140 1.30.4.1 rmind #endif
1141 1.30.4.1 rmind } else if (sig)
1142 1.30.4.1 rmind return SIGILL; /* got a signal */
1143 1.30.4.1 rmind DPRINTF(("%s: %s insn @ %x (%x+%x) (disp=%x)\n", __func__,
1144 1.30.4.1 rmind (sig == -1) ? "BRANCH to" : "NEXT",
1145 1.30.4.1 rmind insn->is_pc + insn->is_advance, insn->is_pc, insn->is_advance,
1146 1.30.4.1 rmind displ));
1147 1.30.4.1 rmind return 0;
1148 1.1 gwr }
1149