Home | History | Annotate | Line # | Download | only in fpe
fpu_emulate.c revision 1.5
      1 /*	$NetBSD: fpu_emulate.c,v 1.5 1996/04/30 11:52:13 briggs 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;		/* XXX - FP regs order? */
    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 	regmask = 1 << regnum;
    491 	if (regmask & reglist) {
    492 	    if (fpu_to_mem) {
    493 		sig = fpu_store_ea(frame, insn, &insn->is_ea0,
    494 				   (char*)&fpregs[regnum * 3]);
    495 		if (fpu_debug_level & DL_RESULT)
    496 		    printf("  fpu_emul_fmovm: FP%d (%08x,%08x,%08x) saved\n",
    497 			   regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
    498 			   fpregs[regnum * 3 + 2]);
    499 	    } else {		/* mem to fpu */
    500 		sig = fpu_load_ea(frame, insn, &insn->is_ea0,
    501 				  (char*)&fpregs[regnum * 3]);
    502 		if (fpu_debug_level & DL_RESULT)
    503 		    printf("  fpu_emul_fmovm: FP%d (%08x,%08x,%08x) loaded\n",
    504 			   regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
    505 			   fpregs[regnum * 3 + 2]);
    506 	    }
    507 	    if (sig) { break; }
    508 	}
    509 	regnum += order;
    510     }
    511 
    512     return sig;
    513 }
    514 
    515 static struct fpn *
    516 fpu_cmp(fe)
    517      struct fpemu *fe;
    518 {
    519     struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
    520 
    521     /* take care of special cases */
    522     if (x->fp_class < 0 || y->fp_class < 0) {
    523 	/* if either of two is a SNAN, result is SNAN */
    524 	x->fp_class = (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
    525     } else if (x->fp_class == FPC_INF) {
    526 	if (y->fp_class == FPC_INF) {
    527 	    /* both infinities */
    528 	    if (x->fp_sign == y->fp_sign) {
    529 		x->fp_class = FPC_ZERO;	/* return a signed zero */
    530 	    } else {
    531 		x->fp_class = FPC_NUM; /* return a faked number w/x's sign */
    532 		x->fp_exp = 16383;
    533 		x->fp_mant[0] = FP_1;
    534 	    }
    535 	} else {
    536 	    /* y is a number */
    537 	    x->fp_class = FPC_NUM; /* return a forged number w/x's sign */
    538 	    x->fp_exp = 16383;
    539 	    x->fp_mant[0] = FP_1;
    540 	}
    541     } else if (y->fp_class == FPC_INF) {
    542 	/* x is a Num but y is an Inf */
    543 	/* return a forged number w/y's sign inverted */
    544 	x->fp_class = FPC_NUM;
    545 	x->fp_sign = !y->fp_sign;
    546 	x->fp_exp = 16383;
    547 	x->fp_mant[0] = FP_1;
    548     } else {
    549 	/* x and y are both numbers or zeros, or pair of a number and a zero */
    550 	y->fp_sign = !y->fp_sign;
    551 	x = fpu_add(fe);	/* (x - y) */
    552 	/*
    553 	 * FCMP does not set Inf bit in CC, so return a forged number
    554 	 * (value doesn't matter) if Inf is the result of fsub.
    555 	 */
    556 	if (x->fp_class == FPC_INF) {
    557 	    x->fp_class = FPC_NUM;
    558 	    x->fp_exp = 16383;
    559 	    x->fp_mant[0] = FP_1;
    560 	}
    561     }
    562     return x;
    563 }
    564 
    565 /*
    566  * arithmetic oprations
    567  */
    568 static int
    569 fpu_emul_arith(fe, insn)
    570      struct fpemu *fe;
    571      struct instruction *insn;
    572 {
    573     struct frame *frame = fe->fe_frame;
    574     u_int *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
    575     struct fpn *res;
    576     int word1, sig = 0;
    577     int regnum, format;
    578     int discard_result = 0;
    579     u_int buf[3];
    580     int flags;
    581     char regname;
    582 
    583     DUMP_INSN(insn);
    584 
    585     if (fpu_debug_level & DL_ARITH) {
    586 	printf("  fpu_emul_arith: FPSR = %08x, FPCR = %08x\n",
    587 	       fe->fe_fpsr, fe->fe_fpcr);
    588     }
    589 
    590     word1 = insn->is_word1;
    591     format = (word1 >> 10) & 7;
    592     regnum = (word1 >> 7) & 7;
    593 
    594     /* fetch a source operand : may not be used */
    595     if (fpu_debug_level & DL_ARITH) {
    596 	printf("  fpu_emul_arith: dst/src FP%d=%08x,%08x,%08x\n",
    597 	       regnum, fpregs[regnum*3], fpregs[regnum*3+1],
    598 	       fpregs[regnum*3+2]);
    599     }
    600     fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
    601 
    602     DUMP_INSN(insn);
    603 
    604     /* get the other operand which is always the source */
    605     if ((word1 & 0x4000) == 0) {
    606 	if (fpu_debug_level & DL_ARITH) {
    607 	    printf("  fpu_emul_arith: FP%d op FP%d => FP%d\n",
    608 		   format, regnum, regnum);
    609 	    printf("  fpu_emul_arith: src opr FP%d=%08x,%08x,%08x\n",
    610 		   format, fpregs[format*3], fpregs[format*3+1],
    611 		   fpregs[format*3+2]);
    612 	}
    613 	fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
    614     } else {
    615 	/* the operand is in memory */
    616 	if (format == FTYPE_DBL) {
    617 	    insn->is_datasize = 8;
    618 	} else if (format == FTYPE_SNG || format == FTYPE_LNG) {
    619 	    insn->is_datasize = 4;
    620 	} else if (format == FTYPE_WRD) {
    621 	    insn->is_datasize = 2;
    622 	} else if (format == FTYPE_BYT) {
    623 	    insn->is_datasize = 1;
    624 	} else if (format == FTYPE_EXT) {
    625 	    insn->is_datasize = 12;
    626 	} else {
    627 	    /* invalid or unsupported operand format */
    628 	    sig = SIGFPE;
    629 	    return sig;
    630 	}
    631 
    632 	/* Get effective address. (modreg=opcode&077) */
    633 	sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
    634 	if (sig) {
    635 	    if (fpu_debug_level & DL_ARITH) {
    636 		printf("  fpu_emul_arith: error in fpu_decode_ea\n");
    637 	    }
    638 	    return sig;
    639 	}
    640 
    641 	DUMP_INSN(insn);
    642 
    643 	if (fpu_debug_level & DL_ARITH) {
    644 	    printf("  fpu_emul_arith: addr mode = ");
    645 	    flags = insn->is_ea0.ea_flags;
    646 	    regname = (insn->is_ea0.ea_regnum & 8) ? 'a' : 'd';
    647 
    648 	    if (flags & EA_DIRECT) {
    649 		printf("%c%d\n",
    650 		       regname, insn->is_ea0.ea_regnum & 7);
    651 	    } else if (flags & EA_PC_REL) {
    652 		if (flags & EA_OFFSET) {
    653 		    printf("pc@(%d)\n", insn->is_ea0.ea_offset);
    654 		} else if (flags & EA_INDEXED) {
    655 		    printf("pc@(...)\n");
    656 		}
    657 	    } else if (flags & EA_PREDECR) {
    658 		printf("%c%d@-\n",
    659 		       regname, insn->is_ea0.ea_regnum & 7);
    660 	    } else if (flags & EA_POSTINCR) {
    661 		printf("%c%d@+\n", regname, insn->is_ea0.ea_regnum & 7);
    662 	    } else if (flags & EA_OFFSET) {
    663 		printf("%c%d@(%d)\n", regname, insn->is_ea0.ea_regnum & 7,
    664 		       insn->is_ea0.ea_offset);
    665 	    } else if (flags & EA_INDEXED) {
    666 		printf("%c%d@(...)\n", regname, insn->is_ea0.ea_regnum & 7);
    667 	    } else if (flags & EA_ABS) {
    668 		printf("0x%08x\n", insn->is_ea0.ea_absaddr);
    669 	    } else if (flags & EA_IMMED) {
    670 
    671 		printf("#0x%08x,%08x,%08x\n", insn->is_ea0.ea_immed[0],
    672 		       insn->is_ea0.ea_immed[1], insn->is_ea0.ea_immed[2]);
    673 	    } else {
    674 		printf("%c%d@\n", regname, insn->is_ea0.ea_regnum & 7);
    675 	    }
    676 	} /* if (fpu_debug_level & DL_ARITH) */
    677 
    678 	fpu_load_ea(frame, insn, &insn->is_ea0, (char*)buf);
    679 	if (format == FTYPE_WRD) {
    680 	    /* sign-extend */
    681 	    buf[0] &= 0xffff;
    682 	    if (buf[0] & 0x8000) {
    683 		buf[0] |= 0xffff0000;
    684 	    }
    685 	    format = FTYPE_LNG;
    686 	} else if (format == FTYPE_BYT) {
    687 	    /* sign-extend */
    688 	    buf[0] &= 0xff;
    689 	    if (buf[0] & 0x80) {
    690 		buf[0] |= 0xffffff00;
    691 	    }
    692 	    format = FTYPE_LNG;
    693 	}
    694 	if (fpu_debug_level & DL_ARITH) {
    695 	    printf("  fpu_emul_arith: src = %08x %08x %08x, siz = %d\n",
    696 		   buf[0], buf[1], buf[2], insn->is_datasize);
    697 	}
    698 	fpu_explode(fe, &fe->fe_f2, format, buf);
    699     }
    700 
    701     DUMP_INSN(insn);
    702 
    703     /* An arithmetic instruction emulate function has a prototype of
    704      * struct fpn *fpu_op(struct fpemu *);
    705 
    706      * 1) If the instruction is monadic, then fpu_op() must use
    707      * fe->fe_f2 as its operand, and return a pointer to the
    708      * result.
    709 
    710      * 2) If the instruction is diadic, then fpu_op() must use
    711      * fe->fe_f1 and fe->fe_f2 as its two operands, and return a
    712      * pointer to the result.
    713 
    714      */
    715     switch (word1 & 0x3f) {
    716     case 0x00:			/* fmove */
    717 	res = &fe->fe_f2;
    718 	break;
    719 
    720     case 0x01:			/* fint */
    721 	res = fpu_int(fe);
    722 	break;
    723 
    724     case 0x02:			/* fsinh */
    725 	res = fpu_sinh(fe);
    726 	break;
    727 
    728     case 0x03:			/* fintrz */
    729 	res = fpu_intrz(fe);
    730 	break;
    731 
    732     case 0x04:			/* fsqrt */
    733 	res = fpu_sqrt(fe);
    734 	break;
    735 
    736     case 0x06:			/* flognp1 */
    737 	res = fpu_lognp1(fe);
    738 	break;
    739 
    740     case 0x08:			/* fetoxm1 */
    741 	res = fpu_etoxm1(fe);
    742 	break;
    743 
    744     case 0x09:			/* ftanh */
    745 	res = fpu_tanh(fe);
    746 	break;
    747 
    748     case 0x0A:			/* fatan */
    749 	res = fpu_atan(fe);
    750 	break;
    751 
    752     case 0x0C:			/* fasin */
    753 	res = fpu_asin(fe);
    754 	break;
    755 
    756     case 0x0D:			/* fatanh */
    757 	res = fpu_atanh(fe);
    758 	break;
    759 
    760     case 0x0E:			/* fsin */
    761 	res = fpu_sin(fe);
    762 	break;
    763 
    764     case 0x0F:			/* ftan */
    765 	res = fpu_tan(fe);
    766 	break;
    767 
    768     case 0x10:			/* fetox */
    769 	res = fpu_etox(fe);
    770 	break;
    771 
    772     case 0x11:			/* ftwotox */
    773 	res = fpu_twotox(fe);
    774 	break;
    775 
    776     case 0x12:			/* ftentox */
    777 	res = fpu_tentox(fe);
    778 	break;
    779 
    780     case 0x14:			/* flogn */
    781 	res = fpu_logn(fe);
    782 	break;
    783 
    784     case 0x15:			/* flog10 */
    785 	res = fpu_log10(fe);
    786 	break;
    787 
    788     case 0x16:			/* flog2 */
    789 	res = fpu_log2(fe);
    790 	break;
    791 
    792     case 0x18:			/* fabs */
    793 	fe->fe_f2.fp_sign = 0;
    794 	res = &fe->fe_f2;
    795 	break;
    796 
    797     case 0x19:			/* fcosh */
    798 	res = fpu_cosh(fe);
    799 	break;
    800 
    801     case 0x1A:			/* fneg */
    802 	fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
    803 	res = &fe->fe_f2;
    804 	break;
    805 
    806     case 0x1C:			/* facos */
    807 	res = fpu_acos(fe);
    808 	break;
    809 
    810     case 0x1D:			/* fcos */
    811 	res = fpu_cos(fe);
    812 	break;
    813 
    814     case 0x1E:			/* fgetexp */
    815 	res = fpu_getexp(fe);
    816 	break;
    817 
    818     case 0x1F:			/* fgetman */
    819 	res = fpu_getman(fe);
    820 	break;
    821 
    822     case 0x20:			/* fdiv */
    823     case 0x24:			/* fsgldiv: cheating - better than nothing */
    824 	res = fpu_div(fe);
    825 	break;
    826 
    827     case 0x21:			/* fmod */
    828 	res = fpu_mod(fe);
    829 	break;
    830 
    831     case 0x28:			/* fsub */
    832 	fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
    833     case 0x22:			/* fadd */
    834 	res = fpu_add(fe);
    835 	break;
    836 
    837     case 0x23:			/* fmul */
    838     case 0x27:			/* fsglmul: cheating - better than nothing */
    839 	res = fpu_mul(fe);
    840 	break;
    841 
    842     case 0x25:			/* frem */
    843 	res = fpu_rem(fe);
    844 	break;
    845 
    846     case 0x26:
    847 	/* fscale is handled by a separate function */
    848 	break;
    849 
    850     case 0x30:
    851     case 0x32:
    852     case 0x33:
    853     case 0x34:
    854     case 0x35:
    855     case 0x36:
    856     case 0x37:			/* fsincos */
    857 	res = fpu_sincos(fe, word1 & 7);
    858 	break;
    859 
    860     case 0x38:			/* fcmp */
    861 	res = fpu_cmp(fe);
    862 	discard_result = 1;
    863 	break;
    864 
    865     case 0x3A:			/* ftst */
    866 	res = &fe->fe_f2;
    867 	discard_result = 1;
    868 	break;
    869 
    870     default:
    871 #ifdef DEBUG
    872 	printf("  fpu_emul_arith: bad opcode=0x%x, word1=0x%x\n",
    873 	       insn->is_opcode, insn->is_word1);
    874 #endif
    875 	sig = SIGILL;
    876     } /* switch (word1 & 0x3f) */
    877 
    878     if (!discard_result && sig == 0) {
    879 	fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
    880 	if (fpu_debug_level & DL_ARITH) {
    881 	    printf("  fpu_emul_arith: %08x,%08x,%08x stored in FP%d\n",
    882 		   fpregs[regnum*3], fpregs[regnum*3+1],
    883 		   fpregs[regnum*3+2], regnum);
    884 	}
    885     } else if (sig == 0 && fpu_debug_level & DL_ARITH) {
    886 	static char *class_name[] = { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
    887 	printf("  fpu_emul_arith: result(%s,%c,%d,%08x,%08x,%08x,%08x) discarded\n",
    888 	       class_name[res->fp_class + 2],
    889 	       res->fp_sign ? '-' : '+', res->fp_exp,
    890 	       res->fp_mant[0], res->fp_mant[1],
    891 	       res->fp_mant[2], res->fp_mant[3]);
    892     } else if (fpu_debug_level & DL_ARITH) {
    893 	printf("  fpu_emul_arith: received signal %d\n", sig);
    894     }
    895 
    896     /* update fpsr according to the result of operation */
    897     fpu_upd_fpsr(fe, res);
    898 
    899     if (fpu_debug_level & DL_ARITH) {
    900 	printf("  fpu_emul_arith: FPSR = %08x, FPCR = %08x\n",
    901 	       fe->fe_fpsr, fe->fe_fpcr);
    902     }
    903 
    904     DUMP_INSN(insn);
    905 
    906     return sig;
    907 }
    908 
    909 /* test condition code according to the predicate in the opcode.
    910  * returns -1 when the predicate evaluates to true, 0 when false.
    911  * signal numbers are returned when an error is detected.
    912  */
    913 static int
    914 test_cc(fe, pred)
    915      struct fpemu *fe;
    916      int pred;
    917 {
    918     int result, sig_bsun, invert;
    919     int fpsr;
    920 
    921     fpsr = fe->fe_fpsr;
    922     invert = 0;
    923     fpsr &= ~FPSR_EXCP;		/* clear all exceptions */
    924     if (fpu_debug_level & DL_TESTCC) {
    925 	printf("  test_cc: fpsr=0x%08x\n", fpsr);
    926     }
    927     pred &= 0x3f;		/* lowest 6 bits */
    928 
    929     if (fpu_debug_level & DL_TESTCC) {
    930 	printf("  test_cc: ");
    931     }
    932 
    933     if (pred >= 040) {
    934 	return SIGILL;
    935     } else if (pred & 0x10) {
    936 	/* IEEE nonaware tests */
    937 	sig_bsun = 1;
    938 	pred &= 017;		/* lower 4 bits */
    939     } else {
    940 	/* IEEE aware tests */
    941 	if (fpu_debug_level & DL_TESTCC) {
    942 	    printf("IEEE ");
    943 	}
    944 	sig_bsun = 0;
    945     }
    946 
    947     if (pred >= 010) {
    948 	if (fpu_debug_level & DL_TESTCC) {
    949 	    printf("Not ");
    950 	}
    951 	/* predicate is "NOT ..." */
    952 	pred ^= 0xf;		/* invert */
    953 	invert = -1;
    954     }
    955     switch (pred) {
    956     case 0:			/* (Signaling) False */
    957 	if (fpu_debug_level & DL_TESTCC) {
    958 	    printf("False");
    959 	}
    960 	result = 0;
    961 	break;
    962     case 1:			/* (Signaling) Equal */
    963 	if (fpu_debug_level & DL_TESTCC) {
    964 	    printf("Equal");
    965 	}
    966 	result = -((fpsr & FPSR_ZERO) == FPSR_ZERO);
    967 	break;
    968     case 2:			/* Greater Than */
    969 	if (fpu_debug_level & DL_TESTCC) {
    970 	    printf("GT");
    971 	}
    972 	result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == 0);
    973 	break;
    974     case 3:			/* Greater or Equal */
    975 	if (fpu_debug_level & DL_TESTCC) {
    976 	    printf("GE");
    977 	}
    978 	result = -((fpsr & FPSR_ZERO) ||
    979 		   (fpsr & (FPSR_NAN|FPSR_NEG)) == 0);
    980 	break;
    981     case 4:			/* Less Than */
    982 	if (fpu_debug_level & DL_TESTCC) {
    983 	    printf("LT");
    984 	}
    985 	result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == FPSR_NEG);
    986 	break;
    987     case 5:			/* Less or Equal */
    988 	if (fpu_debug_level & DL_TESTCC) {
    989 	    printf("LE");
    990 	}
    991 	result = -((fpsr & FPSR_ZERO) ||
    992 		   ((fpsr & (FPSR_NAN|FPSR_NEG)) == FPSR_NEG));
    993 	break;
    994     case 6:			/* Greater or Less than */
    995 	if (fpu_debug_level & DL_TESTCC) {
    996 	    printf("GLT");
    997 	}
    998 	result = -((fpsr & (FPSR_NAN|FPSR_ZERO)) == 0);
    999 	break;
   1000     case 7:			/* Greater, Less or Equal */
   1001 	if (fpu_debug_level & DL_TESTCC) {
   1002 	    printf("GLE");
   1003 	}
   1004 	result = -((fpsr & FPSR_NAN) == 0);
   1005 	break;
   1006     default:
   1007 	/* invalid predicate */
   1008 	return SIGILL;
   1009     }
   1010     result ^= invert;		/* if the predicate is "NOT ...", then
   1011 				   invert the result */
   1012     if (fpu_debug_level & DL_TESTCC) {
   1013 	printf(" => %s (%d)\n", result ? "true" : "false", result);
   1014     }
   1015     /* if it's an IEEE unaware test and NAN is set, BSUN is set */
   1016     if (sig_bsun && (fpsr & FPSR_NAN)) {
   1017 	fpsr |= FPSR_BSUN;
   1018     }
   1019 
   1020     /* put fpsr back */
   1021     fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
   1022 
   1023     return result;
   1024 }
   1025 
   1026 /*
   1027  * type 1: fdbcc, fscc, ftrapcc
   1028  * In this function, we know:
   1029  *   (opcode & 0x01C0) == 0x0040
   1030  */
   1031 static int
   1032 fpu_emul_type1(fe, insn)
   1033      struct fpemu *fe;
   1034      struct instruction *insn;
   1035 {
   1036     struct frame *frame = fe->fe_frame;
   1037     int advance, sig, branch, displ;
   1038 
   1039     branch = test_cc(fe, insn->is_word1);
   1040     fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1041 
   1042     insn->is_advance = 4;
   1043     sig = 0;
   1044 
   1045     switch (insn->is_opcode & 070) {
   1046     case 010:			/* fdbcc */
   1047 	if (branch == -1) {
   1048 	    /* advance */
   1049 	    insn->is_advance = 6;
   1050 	} else if (!branch) {
   1051 	    /* decrement Dn and if (Dn != -1) branch */
   1052 	    u_int16_t count = frame->f_regs[insn->is_opcode & 7];
   1053 
   1054 	    if (count-- != 0) {
   1055 		displ = fusword((void *) (frame->f_pc + insn->is_advance));
   1056 		if (displ < 0) {
   1057 #ifdef DEBUG
   1058 		    printf("  fpu_emul_type1: fault reading displacement\n");
   1059 #endif
   1060 		    return SIGSEGV;
   1061 		}
   1062 		/* sign-extend the displacement */
   1063 		displ &= 0xffff;
   1064 		if (displ & 0x8000) {
   1065 		    displ |= 0xffff0000;
   1066 		}
   1067 		insn->is_advance += displ;
   1068 	    } else {
   1069 		insn->is_advance = 6;
   1070 	    }
   1071 	    /* write it back */
   1072 	    frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
   1073 	    frame->f_regs[insn->is_opcode & 7] |= (u_int32_t)count;
   1074 	} else {		/* got a signal */
   1075 	    sig = SIGFPE;
   1076 	}
   1077 	break;
   1078 
   1079     case 070:			/* ftrapcc or fscc */
   1080 	advance = 4;
   1081 	if ((insn->is_opcode & 07) >= 2) {
   1082 	    switch (insn->is_opcode & 07) {
   1083 	    case 3:		/* long opr */
   1084 		advance += 2;
   1085 	    case 2:		/* word opr */
   1086 		advance += 2;
   1087 	    case 4:		/* no opr */
   1088 		break;
   1089 	    default:
   1090 		return SIGILL;
   1091 		break;
   1092 	    }
   1093 
   1094 	    if (branch == 0) {
   1095 		/* no trap */
   1096 		insn->is_advance = advance;
   1097 		sig = 0;
   1098 	    } else {
   1099 		/* trap */
   1100 		sig = SIGFPE;
   1101 	    }
   1102 	    break;
   1103 	} /* if ((insn->is_opcode & 7) < 2), fall through to FScc */
   1104 
   1105     default:			/* fscc */
   1106 	insn->is_advance = 4;
   1107 	insn->is_datasize = 1;	/* always byte */
   1108 	sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
   1109 	if (sig) {
   1110 	    break;
   1111 	}
   1112 	if (branch == -1 || branch == 0) {
   1113 	    /* set result */
   1114 	    sig = fpu_store_ea(frame, insn, &insn->is_ea0, (char *)&branch);
   1115 	} else {
   1116 	    /* got an exception */
   1117 	    sig = branch;
   1118 	}
   1119 	break;
   1120     }
   1121     return sig;
   1122 }
   1123 
   1124 /*
   1125  * Type 2 or 3: fbcc (also fnop)
   1126  * In this function, we know:
   1127  *   (opcode & 0x0180) == 0x0080
   1128  */
   1129 static int
   1130 fpu_emul_brcc(fe, insn)
   1131      struct fpemu *fe;
   1132      struct instruction *insn;
   1133 {
   1134     struct frame *frame = fe->fe_frame;
   1135     int displ, word2;
   1136     int sig;
   1137 
   1138     /*
   1139      * Get branch displacement.
   1140      */
   1141     insn->is_advance = 4;
   1142     displ = insn->is_word1;
   1143 
   1144     if (insn->is_opcode & 0x40) {
   1145 	word2 = fusword((void *) (frame->f_pc + insn->is_advance));
   1146 	if (word2 < 0) {
   1147 #ifdef DEBUG
   1148 	    printf("  fpu_emul_brcc: fault reading word2\n");
   1149 #endif
   1150 	    return SIGSEGV;
   1151 	}
   1152 	displ <<= 16;
   1153 	displ |= word2;
   1154 	insn->is_advance += 2;
   1155     } else /* displacement is word sized */
   1156         if (displ & 0x8000)
   1157 	    displ |= 0xFFFF0000;
   1158 
   1159     /* XXX: If CC, frame->f_pc += displ */
   1160     sig = test_cc(fe, insn->is_opcode);
   1161     fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1162 
   1163     if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
   1164 	return SIGFPE;		/* caught an exception */
   1165     }
   1166     if (sig == -1) {
   1167 	/* branch does take place; 2 is the offset to the 1st disp word */
   1168 	insn->is_advance = displ + 2;
   1169     } else if (sig) {
   1170 	return SIGILL;		/* got a signal */
   1171     }
   1172     if (fpu_debug_level & DL_BRANCH) {
   1173 	printf("  fpu_emul_brcc: %s insn @ %x (%x+%x) (disp=%x)\n",
   1174 	       (sig == -1) ? "BRANCH to" : "NEXT",
   1175 	       frame->f_pc + insn->is_advance, frame->f_pc, insn->is_advance,
   1176 	       displ);
   1177     }
   1178     return 0;
   1179 }
   1180