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