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