Home | History | Annotate | Line # | Download | only in fpe
fpu_calcea.c revision 1.18
      1 /*	$NetBSD: fpu_calcea.c,v 1.18 2007/03/09 16:33:27 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * 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 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: fpu_calcea.c,v 1.18 2007/03/09 16:33:27 tsutsui Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/signal.h>
     39 #include <sys/systm.h>
     40 #include <machine/frame.h>
     41 #include <m68k/m68k.h>
     42 
     43 #include "fpu_emulate.h"
     44 
     45 /*
     46  * Prototypes of static functions
     47  */
     48 static int decode_ea6 __P((struct frame *frame, struct instruction *insn,
     49 			   struct insn_ea *ea, int modreg));
     50 static int fetch_immed __P((struct frame *frame, struct instruction *insn,
     51 			    int *dst));
     52 static int fetch_disp __P((struct frame *frame, struct instruction *insn,
     53 			   int size, int *res));
     54 static int calc_ea __P((struct insn_ea *ea, char *ptr, char **eaddr));
     55 
     56 /*
     57  * Helper routines for dealing with "effective address" values.
     58  */
     59 
     60 /*
     61  * Decode an effective address into internal form.
     62  * Returns zero on success, else signal number.
     63  */
     64 int
     65 fpu_decode_ea(frame, insn, ea, modreg)
     66      struct frame *frame;
     67      struct instruction *insn;
     68      struct insn_ea *ea;
     69      int modreg;
     70 {
     71     int sig;
     72 
     73 #ifdef DEBUG
     74     if (insn->is_datasize < 0) {
     75 	panic("decode_ea: called with uninitialized datasize");
     76     }
     77 #endif
     78 
     79     sig = 0;
     80 
     81     /* Set the most common value here. */
     82     ea->ea_regnum = 8 + (modreg & 7);
     83 
     84     if ((modreg & 060) == 0) {
     85 	/* register direct */
     86 	ea->ea_regnum = modreg & 0xf;
     87 	ea->ea_flags = EA_DIRECT;
     88 #ifdef DEBUG_FPE
     89 	printf("decode_ea: register direct reg=%d\n", ea->ea_regnum);
     90 #endif
     91     } else if ((modreg & 077) == 074) {
     92 	/* immediate */
     93 	ea->ea_flags = EA_IMMED;
     94 	sig = fetch_immed(frame, insn, &ea->ea_immed[0]);
     95 #ifdef DEBUG_FPE
     96 	printf("decode_ea: immediate size=%d\n", insn->is_datasize);
     97 #endif
     98     }
     99     /*
    100      * rest of the address modes need to be separately
    101      * handled for the LC040 and the others.
    102      */
    103 #if 0 /* XXX */
    104     else if (frame->f_format == 4 && frame->f_fmt4.f_fa) {
    105 	/* LC040 */
    106 	ea->ea_flags = EA_FRAME_EA;
    107 	ea->ea_fea = frame->f_fmt4.f_fa;
    108 #ifdef DEBUG_FPE
    109 	printf("decode_ea: 68LC040 - in-frame EA (%p) size %d\n",
    110 		(void *)ea->ea_fea, insn->is_datasize);
    111 #endif
    112 	if ((modreg & 070) == 030) {
    113 	    /* postincrement mode */
    114 	    ea->ea_flags |= EA_POSTINCR;
    115 	} else if ((modreg & 070) == 040) {
    116 	    /* predecrement mode */
    117 	    ea->ea_flags |= EA_PREDECR;
    118 #ifdef M68060
    119 #if defined(M68020) || defined(M68030) || defined(M68040)
    120 	    if (cputype == CPU_68060)
    121 #endif
    122 		if (insn->is_datasize == 12)
    123 			ea->ea_fea -= 8;
    124 #endif
    125 	}
    126     }
    127 #endif /* XXX */
    128     else {
    129 	/* 020/030 */
    130 	switch (modreg & 070) {
    131 
    132 	case 020:			/* (An) */
    133 	    ea->ea_flags = 0;
    134 #ifdef DEBUG_FPE
    135 	    printf("decode_ea: register indirect reg=%d\n", ea->ea_regnum);
    136 #endif
    137 	    break;
    138 
    139 	case 030:			/* (An)+ */
    140 	    ea->ea_flags = EA_POSTINCR;
    141 #ifdef DEBUG_FPE
    142 	    printf("decode_ea: reg indirect postincrement reg=%d\n",
    143 		   ea->ea_regnum);
    144 #endif
    145 	    break;
    146 
    147 	case 040:			/* -(An) */
    148 	    ea->ea_flags = EA_PREDECR;
    149 #ifdef DEBUG_FPE
    150 	    printf("decode_ea: reg indirect predecrement reg=%d\n",
    151 		   ea->ea_regnum);
    152 #endif
    153 	    break;
    154 
    155 	case 050:			/* (d16,An) */
    156 	    ea->ea_flags = EA_OFFSET;
    157 	    sig = fetch_disp(frame, insn, 1, &ea->ea_offset);
    158 #ifdef DEBUG_FPE
    159 	    printf("decode_ea: reg indirect with displacement reg=%d\n",
    160 		   ea->ea_regnum);
    161 #endif
    162 	    break;
    163 
    164 	case 060:			/* (d8,An,Xn) */
    165 	    ea->ea_flags = EA_INDEXED;
    166 	    sig = decode_ea6(frame, insn, ea, modreg);
    167 	    break;
    168 
    169 	case 070:			/* misc. */
    170 	    ea->ea_regnum = (modreg & 7);
    171 	    switch (modreg & 7) {
    172 
    173 	    case 0:			/* (xxxx).W */
    174 		ea->ea_flags = EA_ABS;
    175 		sig = fetch_disp(frame, insn, 1, &ea->ea_absaddr);
    176 #ifdef DEBUG_FPE
    177 		printf("decode_ea: absolute address (word)\n");
    178 #endif
    179 		break;
    180 
    181 	    case 1:			/* (xxxxxxxx).L */
    182 		ea->ea_flags = EA_ABS;
    183 		sig = fetch_disp(frame, insn, 2, &ea->ea_absaddr);
    184 #ifdef DEBUG_FPE
    185 		printf("decode_ea: absolute address (long)\n");
    186 #endif
    187 		break;
    188 
    189 	    case 2:			/* (d16,PC) */
    190 		ea->ea_flags = EA_PC_REL | EA_OFFSET;
    191 		sig = fetch_disp(frame, insn, 1, &ea->ea_absaddr);
    192 #ifdef DEBUG_FPE
    193 		printf("decode_ea: pc relative word displacement\n");
    194 #endif
    195 		break;
    196 
    197 	    case 3:			/* (d8,PC,Xn) */
    198 		ea->ea_flags = EA_PC_REL | EA_INDEXED;
    199 		sig = decode_ea6(frame, insn, ea, modreg);
    200 		break;
    201 
    202 	    case 4:			/* #data */
    203 		/* it should have been taken care of earlier */
    204 	    default:
    205 #ifdef DEBUG_FPE
    206 		printf("decode_ea: invalid addr mode (7,%d)\n", modreg & 7);
    207 #endif
    208 		return SIGILL;
    209 	    } /* switch for mode 7 */
    210 	    break;
    211 	} /* switch mode */
    212     }
    213     ea->ea_moffs = 0;
    214 
    215     return sig;
    216 }
    217 
    218 /*
    219  * Decode Mode=6 address modes
    220  */
    221 static int
    222 decode_ea6(frame, insn, ea, modreg)
    223      struct frame *frame;
    224      struct instruction *insn;
    225      struct insn_ea *ea;
    226      int modreg;
    227 {
    228     int extword, idx;
    229     int basedisp, outerdisp;
    230     int bd_size, od_size;
    231     int sig;
    232 
    233     extword = fusword((void *) (insn->is_pc + insn->is_advance));
    234     if (extword < 0) {
    235 	return SIGSEGV;
    236     }
    237     insn->is_advance += 2;
    238 
    239     /* get register index */
    240     ea->ea_idxreg = (extword >> 12) & 0xf;
    241     idx = frame->f_regs[ea->ea_idxreg];
    242     if ((extword & 0x0800) == 0) {
    243 	/* if word sized index, sign-extend */
    244 	idx &= 0xffff;
    245 	if (idx & 0x8000) {
    246 	    idx |= 0xffff0000;
    247 	}
    248     }
    249     /* scale register index */
    250     idx <<= ((extword >>9) & 3);
    251 
    252     if ((extword & 0x100) == 0) {
    253 	/* brief extension word - sign-extend the displacement */
    254 	basedisp = (extword & 0xff);
    255 	if (basedisp & 0x80) {
    256 	    basedisp |= 0xffffff00;
    257 	}
    258 
    259 	ea->ea_basedisp = idx + basedisp;
    260 	ea->ea_outerdisp = 0;
    261 #if DEBUG_FPE
    262 	printf("decode_ea6: brief ext word idxreg=%d, basedisp=%08x\n",
    263 	       ea->ea_idxreg, ea->ea_basedisp);
    264 #endif
    265     } else {
    266 	/* full extension word */
    267 	if (extword & 0x80) {
    268 	    ea->ea_flags |= EA_BASE_SUPPRSS;
    269 	}
    270 	bd_size = ((extword >> 4) & 3) - 1;
    271 	od_size = (extword & 3) - 1;
    272 	sig = fetch_disp(frame, insn, bd_size, &basedisp);
    273 	if (sig) {
    274 	    return sig;
    275 	}
    276 	if (od_size >= 0) {
    277 	    ea->ea_flags |= EA_MEM_INDIR;
    278 	}
    279 	sig = fetch_disp(frame, insn, od_size, &outerdisp);
    280 	if (sig) {
    281 	    return sig;
    282 	}
    283 
    284 	switch (extword & 0x44) {
    285 	case 0:			/* preindexed */
    286 	    ea->ea_basedisp = basedisp + idx;
    287 	    ea->ea_outerdisp = outerdisp;
    288 	    break;
    289 	case 4:			/* postindexed */
    290 	    ea->ea_basedisp = basedisp;
    291 	    ea->ea_outerdisp = outerdisp + idx;
    292 	    break;
    293 	case 0x40:		/* no index */
    294 	    ea->ea_basedisp = basedisp;
    295 	    ea->ea_outerdisp = outerdisp;
    296 	    break;
    297 	default:
    298 #ifdef DEBUG
    299 	    printf("decode_ea6: invalid indirect mode: ext word %04x\n",
    300 		   extword);
    301 #endif
    302 	    return SIGILL;
    303 	    break;
    304 	}
    305 #if DEBUG_FPE
    306 	printf("decode_ea6: full ext idxreg=%d, basedisp=%x, outerdisp=%x\n",
    307 	       ea->ea_idxreg, ea->ea_basedisp, ea->ea_outerdisp);
    308 #endif
    309     }
    310 #if DEBUG_FPE
    311     printf("decode_ea6: regnum=%d, flags=%x\n",
    312 	   ea->ea_regnum, ea->ea_flags);
    313 #endif
    314     return 0;
    315 }
    316 
    317 /*
    318  * Load a value from an effective address.
    319  * Returns zero on success, else signal number.
    320  */
    321 int
    322 fpu_load_ea(frame, insn, ea, dst)
    323      struct frame *frame;
    324      struct instruction *insn;
    325      struct insn_ea *ea;
    326      char *dst;
    327 {
    328     int *reg;
    329     char *src;
    330     int len, step;
    331     int sig;
    332 
    333 #ifdef DIAGNOSTIC
    334     if (ea->ea_regnum & ~0xF) {
    335 	panic("load_ea: bad regnum");
    336     }
    337 #endif
    338 
    339 #ifdef DEBUG_FPE
    340     printf("load_ea: frame at %p\n", frame);
    341 #endif
    342     /* dst is always int or larger. */
    343     len = insn->is_datasize;
    344     if (len < 4) {
    345 	dst += (4 - len);
    346     }
    347     step = (len == 1 && ea->ea_regnum == 15 /* sp */) ? 2 : len;
    348 
    349 #if 0
    350     if (ea->ea_flags & EA_FRAME_EA) {
    351 	/* Using LC040 frame EA */
    352 #ifdef DEBUG_FPE
    353 	if (ea->ea_flags & (EA_PREDECR|EA_POSTINCR)) {
    354 	    printf("load_ea: frame ea %08x w/r%d\n",
    355 		   ea->ea_fea, ea->ea_regnum);
    356 	} else {
    357 	    printf("load_ea: frame ea %08x\n", ea->ea_fea);
    358 	}
    359 #endif
    360 	src = (char *)ea->ea_fea;
    361 	copyin(src + ea->ea_moffs, dst, len);
    362 	if (ea->ea_flags & EA_PREDECR) {
    363 	    frame->f_regs[ea->ea_regnum] = ea->ea_fea;
    364 	    ea->ea_fea -= step;
    365 	    ea->ea_moffs = 0;
    366 	} else if (ea->ea_flags & EA_POSTINCR) {
    367 	    ea->ea_fea += step;
    368 	    frame->f_regs[ea->ea_regnum] = ea->ea_fea;
    369 	    ea->ea_moffs = 0;
    370 	} else {
    371 	    ea->ea_moffs += step;
    372 	}
    373 	/* That's it, folks */
    374     } else
    375 #endif
    376     if (ea->ea_flags & EA_DIRECT) {
    377 	if (len > 4) {
    378 #ifdef DEBUG
    379 	    printf("load_ea: operand doesn't fit CPU reg\n");
    380 #endif
    381 	    return SIGILL;
    382 	}
    383 	if (ea->ea_moffs > 0) {
    384 #ifdef DEBUG
    385 	    printf("load_ea: more than one move from CPU reg\n");
    386 #endif
    387 	    return SIGILL;
    388 	}
    389 	src = (char *)&frame->f_regs[ea->ea_regnum];
    390 	/* The source is an int. */
    391 	if (len < 4) {
    392 	    src += (4 - len);
    393 #ifdef DEBUG_FPE
    394 	    printf("load_ea: short/byte opr - addr adjusted\n");
    395 #endif
    396 	}
    397 #ifdef DEBUG_FPE
    398 	printf("load_ea: src %p\n", src);
    399 #endif
    400 	memcpy(dst, src, len);
    401     } else if (ea->ea_flags & EA_IMMED) {
    402 #ifdef DEBUG_FPE
    403 	printf("load_ea: immed %08x%08x%08x size %d\n",
    404 	       ea->ea_immed[0], ea->ea_immed[1], ea->ea_immed[2], len);
    405 #endif
    406 	src = (char *)&ea->ea_immed[0];
    407 	if (len < 4) {
    408 	    src += (4 - len);
    409 #ifdef DEBUG_FPE
    410 	    printf("load_ea: short/byte immed opr - addr adjusted\n");
    411 #endif
    412 	}
    413 	memcpy(dst, src, len);
    414     } else if (ea->ea_flags & EA_ABS) {
    415 #ifdef DEBUG_FPE
    416 	printf("load_ea: abs addr %08x\n", ea->ea_absaddr);
    417 #endif
    418 	src = (char *)ea->ea_absaddr;
    419 	copyin(src, dst, len);
    420     } else /* register indirect */ {
    421 	if (ea->ea_flags & EA_PC_REL) {
    422 #ifdef DEBUG_FPE
    423 	    printf("load_ea: using PC\n");
    424 #endif
    425 	    reg = NULL;
    426 	    /* Grab the register contents. 4 is offset to the first
    427 	       extension word from the opcode */
    428 	    src = (char *)insn->is_pc + 4;
    429 #ifdef DEBUG_FPE
    430 	    printf("load_ea: pc relative pc+4 = %p\n", src);
    431 #endif
    432 	} else /* not PC relative */ {
    433 #ifdef DEBUG_FPE
    434 	    printf("load_ea: using register %c%d\n",
    435 		   (ea->ea_regnum >= 8) ? 'a' : 'd', ea->ea_regnum & 7);
    436 #endif
    437 	    /* point to the register */
    438 	    reg = &frame->f_regs[ea->ea_regnum];
    439 
    440 	    if (ea->ea_flags & EA_PREDECR) {
    441 #ifdef DEBUG_FPE
    442 		printf("load_ea: predecr mode - reg decremented\n");
    443 #endif
    444 		*reg -= step;
    445 		ea->ea_moffs = 0;
    446 	    }
    447 
    448 	    /* Grab the register contents. */
    449 	    src = (char *)*reg;
    450 #ifdef DEBUG_FPE
    451 	    printf("load_ea: reg indirect reg = %p\n", src);
    452 #endif
    453 	}
    454 
    455 	sig = calc_ea(ea, src, &src);
    456 	if (sig)
    457 	    return sig;
    458 
    459 	copyin(src + ea->ea_moffs, dst, len);
    460 
    461 	/* do post-increment */
    462 	if (ea->ea_flags & EA_POSTINCR) {
    463 	    if (ea->ea_flags & EA_PC_REL) {
    464 #ifdef DEBUG
    465 		printf("load_ea: tried to postincrement PC\n");
    466 #endif
    467 		return SIGILL;
    468 	    }
    469 	    *reg += step;
    470 	    ea->ea_moffs = 0;
    471 #ifdef DEBUG_FPE
    472 	    printf("load_ea: postinc mode - reg incremented\n");
    473 #endif
    474 	} else {
    475 	    ea->ea_moffs += len;
    476 	}
    477     }
    478 
    479     return 0;
    480 }
    481 
    482 /*
    483  * Store a value at the effective address.
    484  * Returns zero on success, else signal number.
    485  */
    486 int
    487 fpu_store_ea(frame, insn, ea, src)
    488      struct frame *frame;
    489      struct instruction *insn;
    490      struct insn_ea *ea;
    491      char *src;
    492 {
    493     int *reg;
    494     char *dst;
    495     int len, step;
    496     int sig;
    497 
    498 #ifdef	DIAGNOSTIC
    499     if (ea->ea_regnum & ~0xf) {
    500 	panic("store_ea: bad regnum");
    501     }
    502 #endif
    503 
    504     if (ea->ea_flags & (EA_IMMED|EA_PC_REL)) {
    505 	/* not alterable address mode */
    506 #ifdef DEBUG
    507 	printf("store_ea: not alterable address mode\n");
    508 #endif
    509 	return SIGILL;
    510     }
    511 
    512     /* src is always int or larger. */
    513     len = insn->is_datasize;
    514     if (len < 4) {
    515 	src += (4 - len);
    516     }
    517     step = (len == 1 && ea->ea_regnum == 15 /* sp */) ? 2 : len;
    518 
    519     if (ea->ea_flags & EA_FRAME_EA) {
    520 	/* Using LC040 frame EA */
    521 #ifdef DEBUG_FPE
    522 	if (ea->ea_flags & (EA_PREDECR|EA_POSTINCR)) {
    523 	    printf("store_ea: frame ea %08x w/r%d\n",
    524 		   ea->ea_fea, ea->ea_regnum);
    525 	} else {
    526 	    printf("store_ea: frame ea %08x\n", ea->ea_fea);
    527 	}
    528 #endif
    529 	dst = (char *)ea->ea_fea;
    530 	copyout(src, dst + ea->ea_moffs, len);
    531 	if (ea->ea_flags & EA_PREDECR) {
    532 	    frame->f_regs[ea->ea_regnum] = ea->ea_fea;
    533 	    ea->ea_fea -= step;
    534 	    ea->ea_moffs = 0;
    535 	} else if (ea->ea_flags & EA_POSTINCR) {
    536 	    ea->ea_fea += step;
    537 	    frame->f_regs[ea->ea_regnum] = ea->ea_fea;
    538 	    ea->ea_moffs = 0;
    539 	} else {
    540 	    ea->ea_moffs += step;
    541 	}
    542 	/* That's it, folks */
    543     } else if (ea->ea_flags & EA_ABS) {
    544 #ifdef DEBUG_FPE
    545 	printf("store_ea: abs addr %08x\n", ea->ea_absaddr);
    546 #endif
    547 	dst = (char *)ea->ea_absaddr;
    548 	copyout(src, dst + ea->ea_moffs, len);
    549 	ea->ea_moffs += len;
    550     } else if (ea->ea_flags & EA_DIRECT) {
    551 	if (len > 4) {
    552 #ifdef DEBUG
    553 	    printf("store_ea: operand doesn't fit CPU reg\n");
    554 #endif
    555 	    return SIGILL;
    556 	}
    557 	if (ea->ea_moffs > 0) {
    558 #ifdef DEBUG
    559 	    printf("store_ea: more than one move to CPU reg\n");
    560 #endif
    561 	    return SIGILL;
    562 	}
    563 	dst = (char*)&frame->f_regs[ea->ea_regnum];
    564 	/* The destination is an int. */
    565 	if (len < 4) {
    566 	    dst += (4 - len);
    567 #ifdef DEBUG_FPE
    568 	    printf("store_ea: short/byte opr - dst addr adjusted\n");
    569 #endif
    570 	}
    571 #ifdef DEBUG_FPE
    572 	printf("store_ea: dst %p\n", dst);
    573 #endif
    574 	memcpy(dst, src, len);
    575     } else /* One of MANY indirect forms... */ {
    576 #ifdef DEBUG_FPE
    577 	printf("store_ea: using register %c%d\n",
    578 	       (ea->ea_regnum >= 8) ? 'a' : 'd', ea->ea_regnum & 7);
    579 #endif
    580 	/* point to the register */
    581 	reg = &(frame->f_regs[ea->ea_regnum]);
    582 
    583 	/* do pre-decrement */
    584 	if (ea->ea_flags & EA_PREDECR) {
    585 #ifdef DEBUG_FPE
    586 	    printf("store_ea: predecr mode - reg decremented\n");
    587 #endif
    588 	    *reg -= step;
    589 	    ea->ea_moffs = 0;
    590 	}
    591 
    592 	/* calculate the effective address */
    593 	sig = calc_ea(ea, (char *)*reg, &dst);
    594 	if (sig)
    595 	    return sig;
    596 
    597 #ifdef DEBUG_FPE
    598 	printf("store_ea: dst addr=%p+%d\n", dst, ea->ea_moffs);
    599 #endif
    600 	copyout(src, dst + ea->ea_moffs, len);
    601 
    602 	/* do post-increment */
    603 	if (ea->ea_flags & EA_POSTINCR) {
    604 	    *reg += step;
    605 	    ea->ea_moffs = 0;
    606 #ifdef DEBUG_FPE
    607 	    printf("store_ea: postinc mode - reg incremented\n");
    608 #endif
    609 	} else {
    610 	    ea->ea_moffs += len;
    611 	}
    612     }
    613 
    614     return 0;
    615 }
    616 
    617 /*
    618  * fetch_immed: fetch immediate operand
    619  */
    620 static int
    621 fetch_immed(frame, insn, dst)
    622      struct frame *frame;
    623      struct instruction *insn;
    624      int *dst;
    625 {
    626     int data, ext_bytes;
    627 
    628     ext_bytes = insn->is_datasize;
    629 
    630     if (0 < ext_bytes) {
    631 	data = fusword((void *) (insn->is_pc + insn->is_advance));
    632 	if (data < 0) {
    633 	    return SIGSEGV;
    634 	}
    635 	if (ext_bytes == 1) {
    636 	    /* sign-extend byte to long */
    637 	    data &= 0xff;
    638 	    if (data & 0x80) {
    639 		data |= 0xffffff00;
    640 	    }
    641 	} else if (ext_bytes == 2) {
    642 	    /* sign-extend word to long */
    643 	    data &= 0xffff;
    644 	    if (data & 0x8000) {
    645 		data |= 0xffff0000;
    646 	    }
    647 	}
    648 	insn->is_advance += 2;
    649 	dst[0] = data;
    650     }
    651     if (2 < ext_bytes) {
    652 	data = fusword((void *) (insn->is_pc + insn->is_advance));
    653 	if (data < 0) {
    654 	    return SIGSEGV;
    655 	}
    656 	insn->is_advance += 2;
    657 	dst[0] <<= 16;
    658 	dst[0] |= data;
    659     }
    660     if (4 < ext_bytes) {
    661 	data = fusword((void *) (insn->is_pc + insn->is_advance));
    662 	if (data < 0) {
    663 	    return SIGSEGV;
    664 	}
    665 	dst[1] = data << 16;
    666 	data = fusword((void *) (insn->is_pc + insn->is_advance + 2));
    667 	if (data < 0) {
    668 	    return SIGSEGV;
    669 	}
    670 	insn->is_advance += 4;
    671 	dst[1] |= data;
    672     }
    673     if (8 < ext_bytes) {
    674 	data = fusword((void *) (insn->is_pc + insn->is_advance));
    675 	if (data < 0) {
    676 	    return SIGSEGV;
    677 	}
    678 	dst[2] = data << 16;
    679 	data = fusword((void *) (insn->is_pc + insn->is_advance + 2));
    680 	if (data < 0) {
    681 	    return SIGSEGV;
    682 	}
    683 	insn->is_advance += 4;
    684 	dst[2] |= data;
    685     }
    686 
    687     return 0;
    688 }
    689 
    690 /*
    691  * fetch_disp: fetch displacement in full extension words
    692  */
    693 static int
    694 fetch_disp(frame, insn, size, res)
    695      struct frame *frame;
    696      struct instruction *insn;
    697      int size, *res;
    698 {
    699     int disp, word;
    700 
    701     if (size == 1) {
    702 	word = fusword((void *) (insn->is_pc + insn->is_advance));
    703 	if (word < 0) {
    704 	    return SIGSEGV;
    705 	}
    706 	disp = word & 0xffff;
    707 	if (disp & 0x8000) {
    708 	    /* sign-extend */
    709 	    disp |= 0xffff0000;
    710 	}
    711 	insn->is_advance += 2;
    712     } else if (size == 2) {
    713 	word = fusword((void *) (insn->is_pc + insn->is_advance));
    714 	if (word < 0) {
    715 	    return SIGSEGV;
    716 	}
    717 	disp = word << 16;
    718 	word = fusword((void *) (insn->is_pc + insn->is_advance + 2));
    719 	if (word < 0) {
    720 	    return SIGSEGV;
    721 	}
    722 	disp |= (word & 0xffff);
    723 	insn->is_advance += 4;
    724     } else {
    725 	disp = 0;
    726     }
    727     *res = disp;
    728     return 0;
    729 }
    730 
    731 /*
    732  * Calculates an effective address for all address modes except for
    733  * register direct, absolute, and immediate modes.  However, it does
    734  * not take care of predecrement/postincrement of register content.
    735  * Returns a signal value (0 == no error).
    736  */
    737 static int
    738 calc_ea(ea, ptr, eaddr)
    739      struct insn_ea *ea;
    740      char *ptr;		/* base address (usually a register content) */
    741      char **eaddr;	/* pointer to result pointer */
    742 {
    743     int data, word;
    744 
    745 #if DEBUG_FPE
    746     printf("calc_ea: reg indirect (reg) = %p\n", ptr);
    747 #endif
    748 
    749     if (ea->ea_flags & EA_OFFSET) {
    750 	/* apply the signed offset */
    751 #if DEBUG_FPE
    752 	printf("calc_ea: offset %d\n", ea->ea_offset);
    753 #endif
    754 	ptr += ea->ea_offset;
    755     } else if (ea->ea_flags & EA_INDEXED) {
    756 #if DEBUG_FPE
    757 	printf("calc_ea: indexed mode\n");
    758 #endif
    759 
    760 	if (ea->ea_flags & EA_BASE_SUPPRSS) {
    761 	    /* base register is suppressed */
    762 	    ptr = (char *)ea->ea_basedisp;
    763 	} else {
    764 	    ptr += ea->ea_basedisp;
    765 	}
    766 
    767 	if (ea->ea_flags & EA_MEM_INDIR) {
    768 #if DEBUG_FPE
    769 	    printf("calc_ea: mem indir mode: basedisp=%08x, outerdisp=%08x\n",
    770 		   ea->ea_basedisp, ea->ea_outerdisp);
    771 	    printf("calc_ea: addr fetched from %p\n", ptr);
    772 #endif
    773 	    /* memory indirect modes */
    774 	    word = fusword(ptr);
    775 	    if (word < 0) {
    776 		return SIGSEGV;
    777 	    }
    778 	    word <<= 16;
    779 	    data = fusword(ptr + 2);
    780 	    if (data < 0) {
    781 		return SIGSEGV;
    782 	    }
    783 	    word |= data;
    784 #if DEBUG_FPE
    785 	    printf("calc_ea: fetched ptr 0x%08x\n", word);
    786 #endif
    787 	    ptr = (char *)word + ea->ea_outerdisp;
    788 	}
    789     }
    790 
    791     *eaddr = ptr;
    792 
    793     return 0;
    794 }
    795