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