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