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